Generating a Bitcoin address
Bitcoin doesn't use accounts; instead, it uses a UTXO model. A UTXO is an unspent transaction output.
Each UTXO is associated with a Bitcoin address that is derived from either a public key or a script that defines the conditions under which the UTXO can be spent. A Bitcoin address is often used as a single-use invoice instead of a persistent address to increase privacy.
Bitcoin legacy addresses
Legacy addresses start with a 1
and are called P2PKH
(Pay to public key hash) addresses. They encode the hash of an ECDSA public key.
There is also another type of legacy address that starts with a 3
called P2SH
(Pay to script hash) that encodes the hash of a
script. The script can define complex conditions such as multisig or timelocks.
Bitcoin SegWit addresses
SegWit addresses follow the Bech32
format and start with bc1
. They are cheaper to spend than legacy addresses and solve problems regarding transaction malleability, which is important for advanced use cases like partially signed Bitcoin transactions (PSBT) or the Lightning Network.
SegWit addresses can be of three types:
P2WPKH
(Pay to witness public key hash): A SegWit address that encodes the hash of an ECDSA public key.P2WSH
(Pay to witness script hash): A SegWit address that encodes the hash of a script.P2TR
(Pay to taproot): A SegWit address that can be unlocked by a Schnorr signature or a script.
Generating addresses with threshold ECDSA
To generate a Bitcoin address that can only be spent by a specific smart contract or a specific caller of a smart contract, you need to derive the address from the smart contract's public key.
An ECDSA public key can be retrieved using the ecdsa_public_key
system API endpoint. The basic Bitcoin example demonstrates how to generate a P2PKH
address from a smart contract's public key.
- Motoko
- Rust
/// Returns the P2PKH address of this canister at a specific derivation path.
public func get_p2pkh_address() : async BitcoinAddress {
await P2pkh.get_address(ecdsa_canister_actor, NETWORK, KEY_NAME, p2pkhDerivationPath());
};
#[update]
pub async fn get_p2pkh_address() -> String {
let ctx = BTC_CONTEXT.with(|ctx| ctx.get());
// Unique derivation paths are used for every address type generated, to ensure
// each address has its own unique key pair.
let derivation_path = DerivationPath::p2pkh(0, 0);
// Get the ECDSA public key of this smart contract at the given derivation path
let public_key = get_ecdsa_public_key(&ctx, derivation_path.to_vec_u8_path()).await;
// Convert the public key to the format used by the Bitcoin library
let public_key = PublicKey::from_slice(&public_key).unwrap();
// Generate a legacy P2PKH address from the public key.
// The address encoding (Base58) depends on the network type.
Address::p2pkh(public_key, ctx.bitcoin_network).to_string()
}
Generating addresses with threshold Schnorr
A Schnorr public key can be retrieved using the schnorr_public_key
system API endpoint. The basic Bitcoin example demonstrates how to generate two different types of P2TR
addresses from a smart contract's public key: a key-only address and an address using a key or script.
Generating a key-only P2TR address
- Motoko
- Rust
public func get_p2tr_key_only_address() : async BitcoinAddress {
await P2trKeyOnly.get_address_key_only(schnorr_canister_actor, NETWORK, KEY_NAME, p2trKeyOnlyDerivationPath());
};
use bitcoin::{key::Secp256k1, Address, PublicKey, XOnlyPublicKey};
use ic_cdk::update;
use crate::{common::DerivationPath, schnorr::get_schnorr_public_key, BTC_CONTEXT};
/// Returns a Taproot (P2TR) address of this smart contract that supports **key path spending only**.
///
/// This address does not commit to a script path (it commits to an unspendable path per BIP-341).
/// It allows spending using a single Schnorr signature corresponding to the internal key.
#[update]
pub async fn get_p2tr_key_path_only_address() -> String {
let ctx = BTC_CONTEXT.with(|ctx| ctx.get());
// Derivation path strategy:
// We assign fixed address indexes for key roles within Taproot:
// - Index 0: key-path-only Taproot (no script tree committed)
// - Index 1: internal key for a Taproot output that includes a script tree
// - Index 2: script leaf key committed to in the Merkle tree
let internal_key_path = DerivationPath::p2tr(0, 0);
// Derive the public key used as the internal key (untweaked key path base).
// This key is used for key path spending only, without any committed script tree.
let internal_key = get_schnorr_public_key(&ctx, internal_key_path.to_vec_u8_path()).await;
// Convert the internal key to an x-only public key, as required by Taproot (BIP-341).
let internal_key = XOnlyPublicKey::from(PublicKey::from_slice(&internal_key).unwrap());
// Create a Taproot address using the internal key only.
// We pass `None` as the Merkle root, which per BIP-341 means the address commits
// to an unspendable script path, enabling only key path spending.
let secp256k1_engine = Secp256k1::new();
Address::p2tr(&secp256k1_engine, internal_key, None, ctx.bitcoin_network).to_string()
}
Generating a key or script P2TR address
- Motoko
- Rust
public func get_p2tr_address() : async BitcoinAddress {
await P2tr.get_address(schnorr_canister_actor, NETWORK, KEY_NAME, p2trDerivationPaths());
};
use crate::{common::DerivationPath, p2tr, schnorr::get_schnorr_public_key, BTC_CONTEXT};
use bitcoin::Address;
use ic_cdk::update;
/// Returns a Taproot (P2TR) address with a spendable script path.
///
/// This address supports:
/// - Key path spending via a tweaked internal key (standard Taproot path)
/// - Script path spending via a single script leaf: `<script_leaf_key> CHECKSIG`
///
/// The two public keys are derived from distinct derivation paths:
/// - Internal key: p2tr(0, 1) — used for tweaking and key path spending
/// - Script leaf key: p2tr(0, 2) — used in the script tree (as `<key> OP_CHECKSIG`)
#[update]
pub async fn get_p2tr_script_path_enabled_address() -> String {
let ctx = BTC_CONTEXT.with(|ctx| ctx.get());
// Derivation path strategy:
// We assign fixed address indexes for key roles within Taproot:
// - Index 0: key-path-only Taproot (no script tree committed)
// - Index 1: internal key for a Taproot output that includes a script tree
// - Index 2: script leaf key committed to in the Merkle tree
let internal_key_path = DerivationPath::p2tr(0, 1);
let script_leaf_key_path = DerivationPath::p2tr(0, 2);
// Derive the Schnorr public keys used in this Taproot output:
// - `internal_key` is used as the untweaked base key (for key path spending)
// - `script_key` is used inside a Taproot leaf script (for script path spending)
let internal_key = get_schnorr_public_key(&ctx, internal_key_path.to_vec_u8_path()).await;
let script_key = get_schnorr_public_key(&ctx, script_leaf_key_path.to_vec_u8_path()).await;
// Construct the Taproot leaf script: <script_key> OP_CHECKSIG
// This is a simple script that allows spending via the script_key alone.
let taproot_spend_info = p2tr::create_taproot_spend_info(&internal_key, &script_key);
// Construct and return the final Taproot address.
// The address encodes the tweaked output key and is network-aware (mainnet, testnet, etc.).
Address::p2tr_tweaked(taproot_spend_info.output_key(), ctx.bitcoin_network).to_string()
}
Resources
Learn more about Bitcoin addresses using ECDSA.
Learn more about Bitcoin addresses using Schnorr: