Submitting transactions
Advanced
Bitcoin
Tutorial
To submit transactions to the Bitcoin network, the Bitcoin integration API exposes the bitcoin_send_transaction
method.
To test canisters locally that use the following code snippets, you will need to enable local Bitcoin development. To do this, you can either start the local development environment with dfx start --enable-bitcoin
or you can include the following configuration in the project's dfx.json
file:
motoko/basic_bitcoin/dfx.json
"defaults": {
"bitcoin": {
"enabled": true,
"nodes": [
"127.0.0.1:18444"
],
"log_level": "info"
},
The following snippet shows how to send a signed transaction to the Bitcoin network:
- Motoko
- Rust
motoko/basic_bitcoin/src/basic_bitcoin/src/BitcoinApi.mo
public func send_transaction(network : Network, transaction : [Nat8]) : async () {
let transaction_fee =
SEND_TRANSACTION_BASE_COST_CYCLES + transaction.size() * SEND_TRANSACTION_COST_CYCLES_PER_BYTE;
ExperimentalCycles.add(transaction_fee);
await management_canister_actor.bitcoin_send_transaction({
network;
transaction;
})
};
}
rust/basic_bitcoin/src/service/send_from_p2pkh_address.rs
// Send the transaction to the Bitcoin API.
bitcoin_send_transaction(&SendTransactionRequest {
network: ctx.network,
transaction: serialize(&signed_transaction),
})
.await
.unwrap();
// Return the transaction ID.
signed_transaction.compute_txid().to_string()
}