rustywallet-taproot
Taproot (BIP340/341/342) implementation for Bitcoin wallet development.
Features
- X-only Public Keys (BIP340): 32-byte public key representation
- Schnorr Signatures (BIP340): Sign and verify with Schnorr
- Tagged Hashes: Domain-separated hashing for BIP340/341
- Key Tweaking: Taproot key tweaking with merkle roots
- TapTree: Build and manage Taproot script trees
- Control Blocks: Script path spending proofs
- P2TR Addresses: Generate bech32m addresses
- Signature Hashes: BIP341 sighash computation
Installation
[]
= "0.1"
Quick Start
Key Path Spending
use ;
use ;
// Generate internal key
let secp = new;
let secret_key = new;
let public_key = secret_key.public_key;
let = public_key.x_only_public_key;
let internal_key = from_inner;
// Create key-path only output
let output = key_path_only.unwrap;
// Generate P2TR address
let address = output.address.unwrap;
println!; // bc1p...
Schnorr Signing
use ;
use ;
let secp = new;
let secret_key = new;
let message = ;
// Sign
let signature = schnorr_sign.unwrap;
// Verify
let public_key = secret_key.public_key;
let = public_key.x_only_public_key;
let xonly_key = from_inner;
assert!;
Script Path Spending
use ;
// Create a script tree with two leaves
let script1 = vec!; // OP_1
let script2 = vec!; // OP_2
let tree = two_leaf_tree;
// Create output with script tree
let output = with_script_tree.unwrap;
// Get control block for spending via script1
let leaf = new;
let control_block = for_leaf.unwrap;
// Verify control block
assert!;
API Overview
Types
| Type | Description |
|---|---|
XOnlyPublicKey |
32-byte x-only public key (BIP340) |
Parity |
Y-coordinate parity (Even/Odd) |
SchnorrSignature |
64-byte Schnorr signature |
TaprootOutput |
Taproot output with spending info |
TapTree |
Taproot script tree |
TapLeaf |
Single script leaf |
ControlBlock |
Script path spending proof |
TaprootSighashType |
Signature hash types |
Functions
| Function | Description |
|---|---|
schnorr_sign |
Create Schnorr signature |
schnorr_verify |
Verify Schnorr signature |
tweak_public_key |
Tweak public key with merkle root |
tweak_private_key |
Tweak private key for signing |
tagged_hash |
Compute BIP340 tagged hash |
parse_address |
Parse P2TR address |
create_address |
Create P2TR address |
BIP Compliance
- BIP340: Schnorr Signatures for secp256k1
- BIP341: Taproot: SegWit version 1 spending rules
- BIP342: Validation of Taproot Scripts
License
MIT