sending/
sending.rs

1use std::str::FromStr;
2
3use bitcoin::hashes::hex::ToHex;
4use bitcoin::secp256k1::Secp256k1;
5use bitcoin::Network;
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 sender = bip351::Sender::from_seed(&secp, &[0xFE], Network::Bitcoin, 0)?;
13
14    let recipient = bip351::PaymentCode::from_str(
15        "pay1qqpsxq4730l4yre4lt3588eyt3f2lwggtfalvtgfns04a8smzkn7yys6xv2gs8",
16    )?;
17
18    // Sender makes sure the recipient supports segwit addresses.
19    let p2wpkh_addr_type = recipient
20        .address_types()
21        .get(&bip351::AddressType::P2wpkh)
22        .unwrap();
23
24    let (notification_txout, sender_recipient_commitment) =
25        sender.notify(&secp, &recipient, 0, p2wpkh_addr_type.clone())?;
26
27    // Here the sender would add `notification_txout` to a transaction and broadcast it.
28    // wallet.broadcast(tx)...
29
30    let payload = notification_txout.script_pubkey.as_bytes();
31    assert_eq!(
32        payload[2..].to_hex(),
33        "505049cb55bb02e3217349724307eed5514b53b1f53f0802672a9913d9bbb76afecc86be23f46401"
34    );
35
36    // At this point the recipient is notified. Sender can now send funds to their secret address at index `0`.
37    let recipient_addr_0 = sender.address(&secp, &sender_recipient_commitment, 0)?;
38    assert_eq!(
39        recipient_addr_0.to_string(),
40        "bc1qw7ld5h9tj2ruwxqvetznjfq9g5jyp0gjhrs30w"
41    );
42
43    // wallet.send(&recipient_addr_0, 100000);
44
45    Ok(())
46}