GithubHelp home page GithubHelp logo

ic-btc-canister's Introduction

The Bitcoin Canister

Overview

The Bitcoin canister is the core component of the Bitcoin integration project. It enables other canisters deployed on the Internet Computer to use Bitcoin and interact with the Bitcoin network.

To this end, it provides a low-level API with a small set of functions, which serve as the foundation to build powerful Bitcoin libraries and other development tools, and Bitcoin smart contracts running on the Internet Computer.

API

The Bitcoin canister exposes the following functions:

  • get_utxos: The function returns the unspent transaction outputs (UTXOs) of a given Bitcoin address.

  • get_balance: The function returns the balance of a given Bitcoin address.

  • send_transaction: The function sends the given transaction to the Bitcoin network.

Note that only canisters can call these functions, incurring a cost in cycles. In other words, it is not possible for a developer to invoke the functions using a non-canister principal ID.

The full interface description can be found here, expressed in Candid syntax.

More details about the functions are provided below.

Get Unspent Transaction Outputs of a Bitcoin Address

Given a base58-encoded address as part of a GetUtxosRequest, the function returns all UTXOs associated with the provided address.

type Satoshi = nat64;

type OutPoint = record {
  txid : blob;
  vout : nat32
};

type Utxo = record {
  outpoint: OutPoint;
  value: Satoshi;
  height: nat32;
  confirmations: nat32;
};

type GetUtxosRequest = record {
  address : text;
  min_confirmations: opt nat32;
  offset: opt nat32;
};

type GetUtxosError = variant {
  MalformedAddress;
  // More error types to be added here.
};

get_utxos: (GetUtxosRequest) -> (variant {
  Ok : record {
    utxos: vec Utxo;
    total_count: nat32;
  };
  Err : opt GetUtxosError;
});

If the call fails, e.g., because the address is malformed, a GetUtxosError is returned, indicating the reason for the failed call.

The optional min_confirmations parameter can be used to limit the returned UTXOs to those with at least the provided number of confirmations. If this parameter is not used, the default value is 0.

The optional offset parameter can be used to specify a starting offset in the list of UTXOs. This parameter is useful for addresses with many UTXOs.
Note that there is no guarantee that the set of UTXOs will remain unchanged between function calls with different offsets, i.e., every call will return the UTXOs starting from the provided offset based on the current view. If this parameter is not used, the default value is 0.

Get the Balance of a Bitcoin Address

Given a base58-encoded address as part of a GetBalanceRequest , the function returns the current balance of this address in Satoshi (100,000,000 Satoshi = 1 Bitcoin).

type GetBalanceRequest = record {
  address : text;
  min_confirmations: opt nat32;
};

type GetBalanceError = variant {
  MalformedAddress;
  // More error types to be added here.
};

get_balance: (GetBalanceRequest) -> (variant {
  Ok : Satoshi;
  Err: opt GetBalanceError;
});

If the call fails, e.g., because the address is malformed, a GetBalanceError is returned, indicating the reason for the failed call.

The optional min_confirmations parameter can be used to limit the set of considered UTXOs for the calculation of the balance to those with at least the provided number of confirmations.

Send a Bitcoin Transaction

Given a SendTransactionRequest containing the the raw bytes of a Bitcoin transaction, the transaction is forwarded to the Bitcoin network if it passes a set of validity checks.

type SendTransactionRequest = record {
  transaction: blob;
};

type SendTransactionError = variant {
  MalformedTransaction;
  // More error types to be added here.
};

send_transaction: (SendTransactionRequest) -> (variant {
  Ok : null;
  Err : opt SendTransactionError;
});

The following validity checks are performed:

  • The transaction is well-formed.

  • The transaction only consumes unspent outputs.

  • All signatures are correct.

  • There is a positive transaction fee.

  • The transaction does not create dust, i.e., an output that holds a smaller Bitcoin amount than it costs to spend the Bitcoin in the output.

Note
The Bitcoin canister provided as part of the developer preview only checks that the transaction is well-formed.

If at least one of these checks fails, a SendTransactionError is returned, indicating the reason for the failed call.

The Bitcoin canister caches the transaction and periodically forwards the transaction until the transaction appears in a block or the transaction times out after 24 hours, at which point the transaction is removed from the cache.

Note
The Bitcoin canister provided as part of the developer preview does not cache transactions.

ic-btc-canister's People

Contributors

noobmdev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.