Skip to main content

Generate Bitcoin Address

Derive a deterministic Bitcoin address from an MPC key share. Supports all address formats — Legacy (P2PKH), SegWit (P2WPKH), Nested SegWit (P2SH-P2WPKH), and Taproot (P2TR).

Components used:


Code

import { WorkspaceClient, ComponentModule } from 'caller-sdk';

const workspace = new WorkspaceClient({ apiKey: process.env.WR_API_KEY! });

type PaymentType = 'p2pkh' | 'p2sh-p2wpkh' | 'p2wpkh' | 'p2tr';
type Network = 'mainnet' | 'testnet';

async function generateBitcoinAddress(
keyId: string,
addressIndex: number,
paymentType: PaymentType = 'p2wpkh',
network: Network = 'mainnet',
): Promise<string> {
// 1. Build BIP-44/49/84/86 derivation path based on payment type
// p2wpkh → m/84'/0'/0'/0/<addressIndex>
// p2tr → m/86'/0'/0'/0/<addressIndex>
// p2pkh → m/44'/0'/0'/0/<addressIndex>
const { derivationPath } = await workspace
.call(ComponentModule.GET_BITCOIN_DERIVATION_PATH, {
accountIndex: 0,
changeIndex: 0, // 0 = external (receiving), 1 = internal (change)
addressIndex,
})
.promise();

// 2. Derive the compressed public key from the MPC key share
const { publicKey } = await workspace
.call(ComponentModule.COMPUTE_PUBLIC_KEY, { keyId, derivationPath })
.promise();

// 3. Convert public key to a Bitcoin address
const { address } = await workspace
.call(ComponentModule.COMPUTE_BITCOIN_ADDRESS, { publicKey })
.promise();

return address;
}

// Native SegWit (bc1q...) — recommended for lowest fees
const address = await generateBitcoinAddress('your-key-id', 0, 'p2wpkh', 'mainnet');
console.log('Bitcoin address:', address); // bc1q...

Address format comparison

Payment typeBIPExample prefixNotes
p2pkh441...Legacy — highest fees
p2sh-p2wpkh493...Nested SegWit — wide compatibility
p2wpkh84bc1q...Native SegWit — lowest fees
p2tr86bc1p...Taproot — most private

In a workflow

[Trigger: addressIndex, paymentType]


GET_BITCOIN_DERIVATION_PATH ← paymentType (config)
│ derivationPath

COMPUTE_PUBLIC_KEY ← keyId (constant)
│ publicKey

COMPUTE_BITCOIN_ADDRESS ← paymentType, network (config)
│ address

[Output]