I'm trying to sign a segwit transaction in rust-bitcoin. To start with, my transaction setup looks as follows:
let previous_output = OutPoint::new(Txid::from_hex("991ab2b13f6bc6c13002d79d5e9775626a5e7328e14cd16837d50d1cc637dc6a").unwrap(), 0);
let tx_input = TxIn {
previous_output,
script_sig: Default::default(),
sequence: 0xffffffff,
witness: Default::default()
};
let tx_output = TxOut {
value: 4999995000,
script_pubkey: output_details.script_pubkey() // don't worry about this
};
let mut transaction = Transaction {
version: 2,
lock_time: 0,
input: vec![tx_input],
output: vec![tx_output]
};
In order to sign the transaction (using Schnorr, by the way), I extract the sighash as follows:
let signature_hash = transaction.signature_hash(0, &output_details.script_pubkey(), 0); // is that really the most idiomatic way of passing a sighash flag in rust-bitcoin?
let message = Message::from_slice(&signature_hash.to_vec()).unwrap();
When I print the hex-serialized message
, I get the following value: fa42b9b0f54972b31712b2efbc86db1bf78aa833bc9c969ea7860ef38a25fbaf
.
However, in order to sign a segwit transaction, the previous output's amount must also be committed to, and I'm not seeing a way to do that. As far as I can tell, the sighash I should be signing is in fact 61746ec9baa14bbe82586c7f149926b1492871c67dc7fe21e7cfe2f2260a1405
.
Finally, though less relevantly, if things were to work fine, here's how my signature process would conlude:
let signature = private_key.sign_schnorr(message);
let signature_vec = signature.as_ref().to_vec();
transaction.input[0].witness = Witness::from_vec(vec![signature_vec]);
Thanks in advance for helping me figure this out!