|
| 1 | +// Miniscript |
| 2 | +// Written in 2019 by |
| 3 | +// Andrew Poelstra <apoelstra@wpsoftware.net> |
| 4 | +// |
| 5 | +// To the extent possible under law, the author(s) have dedicated all |
| 6 | +// copyright and related and neighboring rights to this software to |
| 7 | +// the public domain worldwide. This software is distributed without |
| 8 | +// any warranty. |
| 9 | +// |
| 10 | +// You should have received a copy of the CC0 Public Domain Dedication |
| 11 | +// along with this software. |
| 12 | +// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. |
| 13 | +// |
| 14 | + |
| 15 | +//! Example: Parsing a xpub and getting address |
| 16 | +
|
| 17 | +extern crate miniscript; |
| 18 | + |
| 19 | +use miniscript::bitcoin::{self, secp256k1}; |
| 20 | +use miniscript::{Descriptor, DescriptorPublicKey, DescriptorPublicKeyCtx}; |
| 21 | + |
| 22 | +use bitcoin::util::bip32; |
| 23 | + |
| 24 | +use std::str::FromStr; |
| 25 | +fn main() { |
| 26 | + // For deriving from descriptors, we need to provide a secp context |
| 27 | + let secp_ctx = secp256k1::Secp256k1::verification_only(); |
| 28 | + // Child number to derive public key |
| 29 | + // This is used only when xpub is wildcard |
| 30 | + let child_number = bip32::ChildNumber::from_normal_idx(0).unwrap(); |
| 31 | + let desc_ctx = DescriptorPublicKeyCtx::new(&secp_ctx, child_number); |
| 32 | + // P2WSH and single xpubs |
| 33 | + let addr_one = Descriptor::<DescriptorPublicKey>::from_str( |
| 34 | + "wsh(sortedmulti(1,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH))", |
| 35 | + ) |
| 36 | + .unwrap() |
| 37 | + .address(bitcoin::Network::Bitcoin, desc_ctx).unwrap(); |
| 38 | + |
| 39 | + let addr_two = Descriptor::<DescriptorPublicKey>::from_str( |
| 40 | + "wsh(sortedmulti(1,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB))", |
| 41 | + ) |
| 42 | + .unwrap() |
| 43 | + .address(bitcoin::Network::Bitcoin, desc_ctx).unwrap(); |
| 44 | + let expected = bitcoin::Address::from_str( |
| 45 | + "bc1qpq2cfgz5lktxzr5zqv7nrzz46hsvq3492ump9pz8rzcl8wqtwqcspx5y6a", |
| 46 | + ) |
| 47 | + .unwrap(); |
| 48 | + assert_eq!(addr_one, expected); |
| 49 | + assert_eq!(addr_two, expected); |
| 50 | + |
| 51 | + // P2WSH-P2SH and ranged xpubs |
| 52 | + let addr_one = Descriptor::<DescriptorPublicKey>::from_str( |
| 53 | + "sh(wsh(sortedmulti(1,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/1/0/*,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH/0/0/*)))", |
| 54 | + ) |
| 55 | + .unwrap() |
| 56 | + .derive(bitcoin::util::bip32::ChildNumber::from_normal_idx(5).unwrap()) |
| 57 | + .address(bitcoin::Network::Bitcoin, desc_ctx).unwrap(); |
| 58 | + |
| 59 | + let addr_two = Descriptor::<DescriptorPublicKey>::from_str( |
| 60 | + "sh(wsh(sortedmulti(1,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH/0/0/*,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/1/0/*)))", |
| 61 | + ) |
| 62 | + .unwrap() |
| 63 | + .derive(bitcoin::util::bip32::ChildNumber::from_normal_idx(5).unwrap()) |
| 64 | + .address(bitcoin::Network::Bitcoin, desc_ctx).unwrap(); |
| 65 | + let expected = bitcoin::Address::from_str("325zcVBN5o2eqqqtGwPjmtDd8dJRyYP82s").unwrap(); |
| 66 | + assert_eq!(addr_one, expected); |
| 67 | + assert_eq!(addr_two, expected); |
| 68 | +} |
0 commit comments