Generating a Bitcoin address
Overview
A Bitcoin address is a hash value. There are currently two types of Bitcoin addresses: P2SH and P2PKH. An address's full public key cannot be provided when sending transactions, so to redeem transactions sent to your Bitcoin address, you must provide both the transaction signature and the public key used to sign the transaction.
To submit and receive Bitcoin transactions in a canister, you need to generate a Bitcoin address for that canister.
Currently, the ecdsa_public_key
API supports generating P2PKH addresses.
Generating a P2PKH address
To generate a P2PKH address for a canister, make a call to the ecdsa_public_key_api
:
- Motoko
- Rust
/// Returns the P2PKH address of this canister at a specific derivation path.
public func get_p2pkh_address() : async BitcoinAddress {
await BitcoinWallet.get_p2pkh_address(NETWORK, KEY_NAME, DERIVATION_PATH)
};
/// Returns the P2PKH address of this canister at a specific derivation path.
#[update]
pub async fn get_p2pkh_address() -> String {
let derivation_path = DERIVATION_PATH.with(|d| d.clone());
let key_name = KEY_NAME.with(|kn| kn.borrow().to_string());
let network = NETWORK.with(|n| n.get());
bitcoin_wallet::get_p2pkh_address(network, key_name, derivation_path).await
}
Learn more about Bitcoin addresses.
Learn more about the ecdsa_public_key
API.
Bitcoin addresses associated with Internet Identity
By default, your Internet Identity will have a Bitcoin address that can be used to send and recieve BTC. You can view this address by logging into the NNS frontend dapp with your Internet Identity.
Next steps
You can use this address to make Bitcoin transactions from your canister.