Skip to content

Instantly share code, notes, and snippets.

@Siimone
Created December 21, 2018 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Siimone/cbb5b6a199e7da7fe40dae55fa628464 to your computer and use it in GitHub Desktop.
Save Siimone/cbb5b6a199e7da7fe40dae55fa628464 to your computer and use it in GitHub Desktop.
redeem_htlc_with_secret - Bitcoin HTLC - COMIT
#[test]
fn redeem_htlc_with_secret() {
let _ = pretty_env_logger::try_init();
let docker = Cli::default();
let container = docker.run(BitcoinCore::default());
let client = tc_bitcoincore_client::new(&container);
client.generate(432).unwrap().unwrap();
let (txid, vout, input_amount, htlc, _, secret, keypair, _) = fund_htlc(&client);
assert!(
htlc.can_be_unlocked_with(secret, keypair).is_ok(),
"Should be unlockable with the given secret and secret_key"
);
let alice_addr: Address = client.get_new_address().unwrap().unwrap().into();
let fee = BitcoinQuantity::from_satoshi(1000);
let redeem_tx = PrimedTransaction {
inputs: vec![PrimedInput::new(
OutPoint { txid, vout: vout.n },
input_amount,
htlc.unlock_with_secret(keypair, &secret),
)],
output_address: alice_addr.clone(),
locktime: 0,
}
.sign_with_fee(fee);
let redeem_tx_hex = serialize_hex(&redeem_tx).unwrap();
let raw_redeem_tx = rpc::SerializedRawTransaction(redeem_tx_hex);
let rpc_redeem_txid = client.send_raw_transaction(raw_redeem_tx).unwrap().unwrap();
client.generate(1).unwrap().unwrap();
assert!(
client
.find_utxo_at_tx_for_address(&rpc_redeem_txid, &alice_addr)
.is_some(),
"utxo should exist after redeeming htlc"
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment