Txbox

Hex.pmMIT LicenseGitHub Workflow Status

Txbox is an Elixir implementation of the TXT Semantic Bitcoin Storage schema. Txbox lets you store Bitcoin transactions in your application's database with searchable and filterable semantic metadata.

Installation

The package can be installed by adding txbox to your list of dependencies in mix.exs.

def deps do
[
{:txbox, "~> 0.1"}
]
end

Once installed, run the following tasks to generate and run the required database migrations.

mix txbox.gen.migration
mix ecto.migrate

Update your application's configuration, making sure Txbox knows which Repo to use.

# config/config.exs
config :txbox, repo: MyApp.Repo

Finally, add Txbox to your application's supervision tree.

children = [
{Txbox, [
# Manic miner configuration (required)
miner: {:taal, headers: [{"token", "MYTOKEN"}]},
# Number of times to attempt polling the miner (default is 20)
max_retries: 20,
# Number of seconds to wait before re-polling the miner (default is 300 - 5 minutes)
retry_after: 300
]}
]
Supervisor.start_link(children, strategy: :one_for_one)

Usage

Once up an running, using Txbox is simple. The Txbox modules provides three functions for creating and finding transactions: set/2, get/2, and all/2.

To add a transaction to Txbox, the minimum required is to give a txid.

iex> Txbox.set(%{
...> txid: "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110"
...> })
{:ok, %Tx{}}

Once a transaction is added, Txbox automatically syncs with the Miner API of your choice, updating the transaction's status until it is confirmed in a block.

When a channel name is ommitted, transactions are added to the default_channel/0 ("txbox"), but by specifiying a channel name as the first argument, that transaction will be added to that channel. You can provide additional metadata about the transaction, as well as attach the raw transaction binary.

iex> Txbox.set("photos", %{
...> txid: "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110",
...> rawtx: <<...>>,
...> tags: ["hubble", "universe"],
...> meta: %{
...> title: "Hubble Ultra-Deep Field"
...> },
...> data: %{
...> bitfs: "https://x.bitfs.network/6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110.out.0.3"
...> }
...> })
{:ok, %Tx{}}

The transaction can be retrieved by the txid too.

iex> Txbox.get("6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110")
{:ok, %Tx{}}

As before, omitting the channel scopes the query to the default_channel/0 ("txbox"). Alterntively you can pass the channel name as the first argument, or use "_" which is the TXT syntax for global scope.

iex> Txbox.get("_", "6dfccf46359e033053ab1975c1e008ddc98560f591e8ed1c8bd051050992c110")
{:ok, %Tx{}}

A list of transactions can be returned using all/2. The second parameter must be a t:map/0 of query parameters to filter and search by.

iex> Txbox.all("photos", %{
...> from: 636400,
...> tagged: "hubble",
...> limit: 5
...> })
{:ok, [%Tx{}, ...]}

A full text search can be made by using the :search filter parameter.

iex> Txbox.all("_", %{
...> search: "hubble deep field"
...> })
{:ok, [%Tx{}, ...]}

Filtering and searching

Txbox adopts the same syntax and query modifiers used by TXT. Txbox automatically normalizes the query map, so keys can be specifiied either as atoms or strings. Here are a few examples:

License

MIT License.

© Copyright 2020 libitx.