alexandria_btc 0.6.0
Bitcoin utilities for Cairo - address generation, key management, and more
This package provides a comprehensive set of Cairo modules for Bitcoin protocol operations and cryptographic primitives. It is designed to enable Bitcoin functionality on Starknet by providing utilities for:
packages/btc/
├── address.cairo # Bitcoin address generation and validation
├── bip340.cairo # BIP-340 Schnorr signatures for Taproot
├── bip322.cairo # BIP-322 message hashing for Taproot
├── decoder.cairo # Bitcoin transaction decoding logic
├── encoder.cairo # Bitcoin transaction encoding logic
├── hash.cairo # Bitcoin cryptographic hash functions
├── keys.cairo # Bitcoin key generation and management
├── legacy_signature.cairo # Bitcoin ECDSA signature operations
├── taproot.cairo # Taproot address and script operations
├── types.cairo # Bitcoin data structures and types
address.cairo
— Bitcoin Address GenerationThis module provides functionality for generating and validating Bitcoin addresses across all standard formats.
FeaturesP2PKH
(Pay to Public Key Hash) - Legacy addressesP2SH
(Pay to Script Hash) - Script addressesP2WPKH
(Pay to Witness Public Key Hash) - SegWit v0P2WSH
(Pay to Witness Script Hash) - SegWit v0 scriptsP2TR
(Pay to Taproot) - SegWit v1 Taprootlet private_key = create_private_key(0x1234..., BitcoinNetwork::Mainnet, true);
let address = private_key_to_address(private_key, BitcoinAddressType::P2WPKH);
keys.cairo
— Bitcoin Key ManagementThis module handles Bitcoin private/public key operations using secp256k1 elliptic curve cryptography.
Featureslet private_key = create_private_key(key_value, BitcoinNetwork::Mainnet, true);
let public_key = private_key_to_public_key(private_key);
let pubkey_hash = public_key_hash(public_key);
legacy_signature.cairo
— ECDSA Signature OperationsThis module provides Bitcoin ECDSA signature verification and transaction signing capabilities.
FeaturesSIGHASH_ALL
, SIGHASH_NONE
, etc.)let signature = Signature { r, s, y_parity };
let valid = verify_ecdsa_signature(message_hash, signature, public_key);
let tx_hash = create_signature_hash(transaction_data, SIGHASH_ALL);
bip322.cairo
— BIP322 Message hashThis module implements BIP-322 message hasing used in Bitcoin Taproot.
Featureslet pubkey: u256 = 0xdff1d77f2a671c5f36183726db2341be58feae1da2deced843240f7b502ba659;
let message = "Message to sign";
let message_hash = bip322_msg_hash(pubkey, message);
// Process hash sig verify or other flows
bip340.cairo
— Schnorr SignaturesThis module implements BIP-340 Schnorr signatures used in Bitcoin Taproot.
Featureslet pubkey: u256 = 0xdff1d77f2a671c5f36183726db2341be58feae1da2deced843240f7b502ba659;
let signature = Signature { r, s, y_parity };
verify_bip340_signature(message, signature, pubkey);
taproot.cairo
— Taproot OperationsThis module provides Taproot address generation and script tree operations for Bitcoin's latest upgrade.
Featureslet internal_key: u256 = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798;
let output_key = create_key_path_output(internal_key);
let script_tree = create_script_tree(scripts.span());
encoder.cairo
/ decoder.cairo
— Transaction ProcessingThese modules handle Bitcoin transaction serialization and deserialization in the standard Bitcoin wire format.
Features// Encoding
let mut encoder = TransactionEncoderTrait::new();
let encoded_data = encoder.encode_transaction(transaction);
// Decoding
let mut decoder = TransactionDecoderTrait::new(raw_data);
let decoded_tx = decoder.decode_transaction();
hash.cairo
— Cryptographic HashingThis module provides Bitcoin-specific cryptographic hash functions.
Featureshash160
(RIPEMD160 of SHA256) for address generationhash256
(double SHA256) for transaction hashinglet pubkey_bytes = public_key_to_bytes(public_key);
let pubkey_hash = hash160(pubkey_bytes.span());
let tx_hash = hash256(transaction_data.span());
types.cairo
— Bitcoin Data StructuresThis module defines all Bitcoin data structures and type definitions.
Key TypesBitcoinPublicKey
- 33/65 byte public key representationBitcoinPrivateKey
- Private key with network and compression infoBitcoinAddress
- Complete address with type and script dataBitcoinTransaction
- Full transaction structureTransactionInput
/TransactionOutput
- Transaction componentsMake sure to import the relevant modules in your own contract/project:
use alexandria_btc::address;
use alexandria_btc::keys;
use alexandria_btc::legacy_signature;
use alexandria_btc::bip322;
use alexandria_btc::bip340;
use alexandria_btc::taproot;
use alexandria_btc::encoder;
use alexandria_btc::decoder;
use alexandria_btc::hash;
use alexandria_btc::types::{BitcoinNetwork, BitcoinAddressType, BitcoinPublicKey};
// 1. Create or import private key
let private_key = create_private_key(
0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef,
BitcoinNetwork::Mainnet,
true // compressed
);
// 2. Generate address for desired type
let address = private_key_to_address(private_key, BitcoinAddressType::P2WPKH);
// 1. Parse signature and public key
let signature = parse_der_signature(der_bytes.span()).unwrap();
let public_key = BitcoinPublicKeyTrait::from_hex(pubkey_hex);
// 2. Create transaction hash
let sighash = create_signature_hash(tx_data.span(), SIGHASH_ALL);
// 3. Verify signature
let valid = verify_ecdsa_signature(sighash, signature, public_key);
// 1. Create decoder with raw transaction bytes
let mut decoder = TransactionDecoderTrait::new(raw_tx_data);
// 2. Decode transaction
let transaction = decoder.decode_transaction();
// 3. Access transaction components
let inputs = transaction.inputs;
let outputs = transaction.outputs;
let is_segwit = transaction.is_segwit;
Version 0.6.0
Uploaded 1 day ago
Cairo version ^2.12.0
Size 39.2 KB
Run the following command in your project dir
scarb add alexandria_btc@0.6.0
Or add the following line to your Scarb.toml
alexandria_btc = "0.6.0"