You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For a deeper understanding of the ICP < > BTC integration, see the [Bitcoin integration documentation](https://wiki.internetcomputer.org/wiki/Bitcoin_Integration).
18
21
19
22
## Prerequisites
20
23
21
-
*[x] Install the [IC SDK](https://internetcomputer.org/docs/current/developer-docs/setup/install/index.mdx).
For local testing, `dfx >= 0.22.0-beta.0` is required.
22
27
23
28
## Step 1: Building and deploying sample code
24
29
25
30
### Clone the smart contract
26
31
27
-
This tutorial has the **same smart contract** written in different programming languages: in [Motoko](https://internetcomputer.org/docs/current/developer-docs/backend/motoko/index.md) and [Rust](https://internetcomputer.org/docs/current/developer-docs/backend/rust/index.md).
32
+
This tutorial has the same smart contract written in
**If you choose Rust and are using MacOS, you'll need to install Homebrew and run `brew install llvm` to be able to compile the example.**
40
-
41
46
### Acquire cycles to deploy
42
47
43
48
Deploying to the Internet Computer requires [cycles](https://internetcomputer.org/docs/current/developer-docs/setup/cycles) (the equivalent of "gas" in other blockchains). You can get free cycles from the [cycles faucet](https://internetcomputer.org/docs/current/developer-docs/setup/cycles/cycles-faucet.md).
-`--network=ic` tells the command line to deploy the smart contract to the mainnet ICP blockchain
54
59
-`--argument '(variant { testnet })'` passes the argument `testnet` to initialize the smart contract, telling it to connect to the Bitcoin testnet
55
60
56
-
**We're initializing the canister with `variant { testnet }` so that the canister connects to the [Bitcoin testnet](https://en.bitcoin.it/wiki/Testnet). To be specific, this connects to `Testnet3`, which is the current Bitcoin test network used by the Bitcoin community.**
57
-
61
+
**We're initializing the canister with `variant { testnet }` so that the
62
+
canister connects to the [Bitcoin testnet](https://en.bitcoin.it/wiki/Testnet).
63
+
To be specific, this connects to `Testnet3`, which is the current Bitcoin test
64
+
network used by the Bitcoin community. This means that the addresses generated
65
+
in the smart contract can only be used to receive or send funds only on Bitcoin
66
+
testnet.**
58
67
59
68
If successful, you should see an output that looks like this:
60
69
@@ -70,80 +79,134 @@ Candid:
70
79
71
80
Your canister is live and ready to use! You can interact with it using either the command line or using the Candid UI, which is the link you see in the output above.
72
81
73
-
In the output above, to see the Candid Web UI for your bitcoin canister, you would use the URL `https://a4gq6-oaaaa-aaaab-qaa4q-cai.raw.icp0.io/?id=<YOUR-CANISTER-ID>`. Here are the two methods you will see:
82
+
In the output above, to see the Candid Web UI for your bitcoin canister, you
and serves up the Candid web UI for this particular canister deployed on
91
+
mainnet.
77
92
78
93
## Step 2: Generating a Bitcoin address
79
94
80
-
Bitcoin has different types of addresses (e.g. P2PKH, P2SH). Most of these
81
-
addresses can be generated from an ECDSA public key. The example code
82
-
showcases how your canister can generate a [P2PKH address](https://en.bitcoin.it/wiki/Transaction#Pay-to-PubkeyHash) using the [ecdsa_public_key](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-ecdsa_public_key) API.
83
-
84
-
On the Candid UI of your canister, click the "Call" button under `get_p2pkh_address` to
85
-
generate a P2PKH Bitcoin address.
95
+
Bitcoin has different types of addresses (e.g. P2PKH, P2SH, P2TR). You may want
Now that the canister is deployed and you have a Bitcoin address, it's time to receive
101
144
some testnet bitcoin. You can use one of the Bitcoin faucets, such as [coinfaucet.eu](https://coinfaucet.eu),
102
145
to receive some bitcoin.
103
146
104
-
Enter your address and click on "Send testnet bitcoins". This example will use the Bitcoin address `n31eU1K11m1r58aJMgTyxGonu7wSMoUYe7`, but you will use your address. The canister will be receiving 0.011 test BTC on the Bitcoin Testnet.
147
+
Enter your address and click on "Send testnet bitcoins". This example will use
148
+
the Bitcoin P2PHK address `mot21Ef7HNDpDJa4CBzt48WpEX7AxNyaqx`, but you will use
149
+
your address. The Bitcoin address you see will be different from the one above
150
+
because the ECDSA/Schnorr public key your canister retrieves is unique.
105
151
106
152
Once the transaction has at least one confirmation, which can take a few minutes,
107
153
you'll be able to see it in your canister's balance.
108
154
155
+
The addresses that have been used for the testing of this canister on Bitcoin
156
+
testnet are `mot21Ef7HNDpDJa4CBzt48WpEX7AxNyaqx` (P2PKH,
Checking the balance of a Bitcoin address relies on the [bitcoin_get_balance](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_get_balance) API.
122
180
123
181
## Step 5: Sending bitcoin
124
182
125
-
You can send bitcoin using the `send` endpoint on your canister.
183
+
You can send bitcoin using the `send_from_${type}` endpoint on your canister, where
184
+
`${type}` is on of `[p2pkh, p2tr_raw_key_spend, p2tr_script_spend]`.
126
185
127
186
In the Candid UI, add a destination address and an amount to send. In the example
128
187
below, we're sending 4'321 Satoshi (0.00004321 BTC) back to the testnet faucet.
129
188
130
189
Via command line, the same call would look like this:
The `send_from_${type}` endpoint can send bitcoin by:
137
196
138
-
1. Getting the percentiles of the most recent fees on the Bitcoin network using the [bitcoin_get_current_fee_percentiles API](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_get_current_fee_percentiles).
139
-
2. Fetching your unspent transaction outputs (UTXOs), using the [bitcoin_get_utxos API](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_get_utxos).
197
+
1. Getting the percentiles of the most recent fees on the Bitcoin network using the [bitcoin_get_current_fee_percentiles API](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-method-bitcoin_get_current_fee_percentiles).
198
+
2. Fetching your unspent transaction outputs (UTXOs), using the [bitcoin_get_utxos API](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-method-bitcoin_get_utxos).
140
199
3. Building a transaction, using some of the UTXOs from step 2 as input and the destination address and amount to send as output.
141
200
The fee percentiles obtained from step 1 is used to set an appropriate fee.
142
-
4. Signing the inputs of the transaction using the [sign_with_ecdsa API](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-sign_with_ecdsa).
143
-
5. Sending the signed transaction to the Bitcoin network using the [bitcoin_send_transaction API](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-bitcoin_send_transaction).
144
-
145
-
The `send` endpoint returns the ID of the transaction it sent to the network.
146
-
You can track the status of this transaction using a block explorer. Once the
201
+
4. Signing the inputs of the transaction using the
5. Sending the signed transaction to the Bitcoin network using the [bitcoin_send_transaction API](https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-method-bitcoin_send_transaction).
206
+
207
+
This canister's `send_from_${type}` endpoint returns the ID of the transaction
208
+
it sent to the network. You can track the status of this transaction using a
209
+
[block explorer](https://en.bitcoin.it/wiki/Block_chain_browser). Once the
147
210
transaction has at least one confirmation, you should be able to see it
148
211
reflected in your current balance.
149
212
@@ -156,7 +219,7 @@ In this tutorial, you were able to:
156
219
* Connect the canister to the Bitcoin testnet.
157
220
* Send the canister some testnet BTC.
158
221
* Check the testnet BTC balance of the canister.
159
-
* Use the canister to send testnet BTC to another BTC address.
222
+
* Use the canister to send testnet BTC to another testnet BTC address.
160
223
161
224
This example is extensively documented in the following tutorials:
0 commit comments