receiving/
receiving.rs

1use std::collections::HashSet;
2
3use bitcoin::hashes::hex::FromHex;
4use bitcoin::secp256k1::Secp256k1;
5use bitcoin::{Network, Script};
6
7/// This example uses test values from the BIP specification.
8/// https://github.com/bitcoin/bips/blob/master/bip-0351.mediawiki#appendix-a-test-vectors
9fn main() -> Result<(), bip351::Error> {
10    let secp = Secp256k1::new();
11
12    let mut accepted_addresses = HashSet::new();
13    accepted_addresses.insert(bip351::AddressType::P2wpkh);
14
15    let recipient =
16        bip351::Recipient::from_seed(&secp, &[0xFF], Network::Bitcoin, 0, accepted_addresses)?;
17
18    // Recipient finds a valid notification addressed to them.
19    let notification_script = Script::from_hex(
20        "6a28505049cb55bb02e3217349724307eed5514b53b1f53f0802672a9913d9bbb76afecc86be23f46401",
21    )
22    .unwrap();
23
24    let recipient_commitment = recipient
25        .detect_notification(&secp, &notification_script)
26        .unwrap();
27
28    // Recipient starts deriving addresses for a particular commitment starting with index `0` (first address).
29    let (address_0, _pubkey, _privkey) = recipient.key_info(&secp, &recipient_commitment, 0)?;
30    assert_eq!(
31        address_0.to_string(),
32        "bc1qw7ld5h9tj2ruwxqvetznjfq9g5jyp0gjhrs30w"
33    );
34
35    Ok(())
36}