Skip to content

Instantly share code, notes, and snippets.

@ZhenQian-keystone
Last active June 20, 2024 06:53
Show Gist options
  • Save ZhenQian-keystone/f4c464df0dd0216ef809856f9b08fbc4 to your computer and use it in GitHub Desktop.
Save ZhenQian-keystone/f4c464df0dd0216ef809856f9b08fbc4 to your computer and use it in GitHub Desktop.
Implement the creator role in bip174.
/// impl bip174 by rust-bitcoin crate
/// Creates the PSBT, in BIP174 parlance this is the 'Creator'.
/// reference
/// https://crates.io/crates/bitcoin
/// https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki
use third_party::bitcoin::psbt::{Psbt};
use third_party::bitcoin::{Amount, OutPoint, ScriptBuf, Sequence, Transaction, Txid, TxIn, TxOut, Witness};
use third_party::bitcoin_hashes::hex::FromHex;
use third_party::{hex};
#[test]
fn test_construct_unsigned_psbt(){
/// Creates the PSBT, in BIP174 parlance this is the 'Creator'.
let unsigned_tx = Transaction{
version:third_party::bitcoin::transaction::Version::TWO,
lock_time: third_party::bitcoin::blockdata::locktime::absolute::LockTime::ZERO,
input: vec![
TxIn{
previous_output: OutPoint{
txid: Txid::from_str("75ddabb27b8845f5247975c8a5ba7c6f336c4570708ebe230caf6db5217ae858").unwrap(),
vout: 0,
},
script_sig: ScriptBuf::new(),
sequence:Sequence::MAX,
witness: Witness::default(),
},
TxIn{
previous_output: OutPoint{
txid: Txid::from_str("1dea7cd05979072a3578cab271c02244ea8a090bbb46aa680a65ecd027048d83").unwrap(),
vout: 1,
},
script_sig: ScriptBuf::new(),
sequence:Sequence::MAX,
witness: Witness::default(),
}
],
output: vec![
TxOut {
value: Amount::from_sat(149990000),
script_pubkey: ScriptBuf::from_hex("0014d85c2b71d0060b09c9886aeb815e50991dda124d").unwrap(),
},
TxOut {
value: Amount::from_sat(100000000),
script_pubkey: ScriptBuf::from_hex("001400aea9a2e5f0f876a588df5546e8742d1d87008f").unwrap(),
},
],
};
let psbt = Psbt::from_unsigned_tx(unsigned_tx).unwrap();
// serialize psbt
let serialized_psbt = psbt.serialize();
let hex_str = hex::encode(serialized_psbt);
assert_eq!(
"70736274ff01009a020000000258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd750000000000ffffffff838d0427d0ec650a68aa46bb0b098aea4422c071b2ca78352a077959d07cea1d0100000000ffffffff0270aaf00800000000160014d85c2b71d0060b09c9886aeb815e50991dda124d00e1f5050000000016001400aea9a2e5f0f876a588df5546e8742d1d87008f000000000000000000",
hex_str
)
}
@ZhenQian-keystone
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment