Skip to content

Create Bitcoin Anchor

Xiao Shengguang edited this page Feb 25, 2021 · 7 revisions

Create Bitcoin Anchor

Prerequisites

  1. The blockchain nodes much start defid with -spv enabled.

  2. If create anchor using the defi-btc-anchorer tool, then need to run a bitcoin node. If create anchor on bitcoin testnet, then need to run a bitcoin testnet node.

Check DeFiChain node SPV status

Before create anchor, it is recommended to using command to check the SPV status

  1. spv_syncstatus to check the spv sync status, it should return some results like below, the connected should be true.
{
  "connected": true,
  "current": 1937475,
  "estimated": 1937475
}
  1. spv_listanchorauths, to list anchor auths, it is the quorum team to verify the anchor, so it should not be empty. If it empty, then need to wait for 15 blocks to let your node to receive the anchor auths message.
[
  {
    "blockHeight": 232440,
    "blockHash": "3acdf9c999a543e08b840485e04cd846c33ab59ebc3e596efc105019f8a87ef4",
    "creationHeight": 232725,
    "signers": 4,
    "signees": [
      "7MYdTGv3bv3z65ai6y5J1NFiARg8PYu4hK",
      "7KEu9JMKCx6aJ9wyg138W3p42rjg19DR5D",
      "7GULFtS6LuJfJEikByKKg8psscg84jnfHs",
      "78MWNEcAAJxihddCw1UnZD8T7fMWmUuBro"
    ]
  }
]

Create Anchor without tool

Using this method will use the below data struct to connect to bitcoin mainnet or testnet to create anchor.

typedef struct {
    const char * const *dnsSeeds; // NULL terminated array of dns seeds
    uint16_t standardPort;
    uint32_t magicNumber;
    uint64_t services;
    int (*verifyDifficulty)(const BRMerkleBlock *block, const BRSet *blockSet); // blockSet must have last 2016 blocks
    const BRCheckPoint *checkpoints;
    size_t checkpointsCount;
    // prefixes
    uint8_t privkey, base58_p2pkh, base58_p2sh;
    char const * const bip32_xprv;
    char const * const bip32_xpub;
    char const * const bech32;
} BRChainParams;

The mainnet bitcoin parameters is in data struct BRMainNetParamsRecord, testnet parameters is in data struct BRTestNetParamsRecord.

The anchor is created with API command spv_createanchor. The first and second parameters are compulsary. Take node the first parameter details are all from bitcoin blockchain.

{
  "txid": "hex",       (string, required) The transaction id of the bitcoin UTXO to spend
  "vout": n,           (numeric, required) The output index to spend in UTXO
  "amount": n,         (numeric, required) Amount of output in satoshis
  "privkey": "str",    (string, required) WIF private key of bitcoin for signing this output
}

An example on regtest

./defi-cli -regtest -rpcport=19554 spv_createanchor "[{\"txid\":\"528e61b53dcf04448693247866198ed0cf6488c42eb4f493f25da251f0a3af59\",\"vout\":3,\"amount\":10000,\"privkey\":\"your_private_key\"}]" mswsMVsyGMj1FzDMbbxw2QW3KvQAv2FKiy true 5000

Example on testnet:

./src/defi-cli -testnet spv_createanchor "[{\"txid\":\"528e61b53dcf04448693247866198ed0cf6488c42eb4f493f25da251f0a3af59\",\"vout\":3,\"amount\":106870,\"privkey\":\"your_private_key\"}]" 7AJwxLqAcN1zbkA8m33c5wFy8EGkzRghVX true 5000

Create Anchor with tool

We have an automation tool to create anchor. It is convenient if user want to create anchor periodically.

git clone https://github.com/DeFiCh/defi-btc-anchorer.git

Follow the readme to setup the parameter in config.toml then use the tool to create anchor. Below is the command to create an anchor every one hour (3600 seconds is an hour).

./main.py --config ./config.toml --repeat 3600

Check Create Anchor Result

Using API spv_listanchors to get the list of anchors, from the result can find out the status of the anchor. If the confirmations larger than 0 and active is true means the anchor creation is succeful. For example

./defi-cli -testnet spv_listanchors

Results:

[
  {
    "btcBlockHeight": 1896976,
    "btcTxHash": "7af242e42795303cb51eb62300e4c8e776d65c3ad2a6191e74af4d2e59debb33",
    "defiBlockHeight": 116790,
    "defiBlockHash": "4c3f370d801a119b427eaaaffb0a24819c26497dd23d08a99e2153e2e571a44a",
    "rewardAddress": "74pBZ3tYbRvCuuagy65R9DjbmZpxMbnFaq",
    "confirmations": 56,
    "active": true
  },
  {
    "btcBlockHeight": 1896971,
    "btcTxHash": "56ff5d4dc8155b608d1f8f30c7f892ef80b99652efa4ce5880b41f12bbae4c08",
    "defiBlockHeight": 116730,
    "defiBlockHash": "c4f509d43747d0196b2aef2d3b80aeff79518f6346b89b8b4c1f52e0e5fe7909",
    "rewardAddress": "74pBZ3tYbRvCuuagy65R9DjbmZpxMbnFaq",
    "confirmations": 61,
    "active": true
  }
]