· 7 years ago · Dec 21, 2018, 02:42 PM
1#[test]
2fn redeem_htlc_with_secret() {
3 let _ = pretty_env_logger::try_init();
4 let docker = Cli::default();
5
6 let container = docker.run(BitcoinCore::default());
7 let client = tc_bitcoincore_client::new(&container);
8 client.generate(432).unwrap().unwrap();
9
10 let (txid, vout, input_amount, htlc, _, secret, keypair, _) = fund_htlc(&client);
11
12 assert!(
13 htlc.can_be_unlocked_with(secret, keypair).is_ok(),
14 "Should be unlockable with the given secret and secret_key"
15 );
16
17 let alice_addr: Address = client.get_new_address().unwrap().unwrap().into();
18
19 let fee = BitcoinQuantity::from_satoshi(1000);
20
21 let redeem_tx = PrimedTransaction {
22 inputs: vec![PrimedInput::new(
23 OutPoint { txid, vout: vout.n },
24 input_amount,
25 htlc.unlock_with_secret(keypair, &secret),
26 )],
27 output_address: alice_addr.clone(),
28 locktime: 0,
29 }
30 .sign_with_fee(fee);
31
32 let redeem_tx_hex = serialize_hex(&redeem_tx).unwrap();
33
34 let raw_redeem_tx = rpc::SerializedRawTransaction(redeem_tx_hex);
35
36 let rpc_redeem_txid = client.send_raw_transaction(raw_redeem_tx).unwrap().unwrap();
37
38 client.generate(1).unwrap().unwrap();
39
40 assert!(
41 client
42 .find_utxo_at_tx_for_address(&rpc_redeem_txid, &alice_addr)
43 .is_some(),
44 "utxo should exist after redeeming htlc"
45 );
46}