multi signature – Code for computing SigHash for numerous enter varieties

0
113
multi signature – Code for computing SigHash for numerous enter varieties


I’ve the next code laying round for the transaction 663becacc6368150a46725e404ccdfa34d1fffbececa784c31f0a7849b4dad08, which I picked off my mempool the opposite day, whose hex is:

020000000001015ce1d4ffc716022f83cc0d557e6dad0500eeff9e9623bde014bdc09c5b672d750000000000fdffffff025fb7460b000000001600142cf4c1dc0352e0658971ca62a7457a1cd8c3389c4ce3a2000000000016001433f57fe374c6ceab61c8639128c038ac2a8c8db60247304402203cb50efb5c4a9aa7fd369ab6f4b226db99f44f9c610b5b50bc42f343a6aa401302201af791542eee6c1b11705e8895cc5adc36458910dc91aadcafb76a6478a29b9f01210242e811e66fd17e9a6e4ef772766c668d6e0595ca1d7f0583148bc460b575fbfdf0df0b00

use bitcoin::consensus::deserialize;
use bitcoin::hashes::{hex, sha256d, Hash};
use bitcoin::util::sighash;
use std::env;
use std::str::FromStr;

fn primary() { 
    let rawtx = env::args().skip(1).subsequent().unwrap();

    let bytes: Vec<u8> = hex::FromHex::from_hex(&rawtx)
        .anticipate("hex decoding");
    let tx: bitcoin::Transaction = deserialize(&bytes)
        .anticipate("tx deserialization");

    let pk = bitcoin::secp256k1::PublicKey::from_str(
        "0242e811e66fd17e9a6e4ef772766c668d6e0595ca1d7f0583148bc460b575fbfd",
    ).unwrap();

    let mut sighash = sighash::SighashCache::new(&tx);
    let mut out_bytes = vec![];
    sighash.segwit_encode_signing_data_to(
        &mut out_bytes,
        0,
        &bitcoin::Script::from_str("76a914f5693fbaf062221baf891d813d5856e4f8ab54eb88ac").unwrap(),
        200000000,
        bitcoin::EcdsaSighashType::All,
    ).anticipate("computing sighash");


    println!("{}", hex::ToHex::to_hex(&out_bytes[..]));

    let sig = bitcoin::secp256k1::ecdsa::Signature::from_str(
        "304402203cb50efb5c4a9aa7fd369ab6f4b226db99f44f9c610b5b50bc42f343a6aa401302201af791542eee6c1b11705e8895cc5adc36458910dc91aadcafb76a6478a29b9f",
    ).unwrap();
    let secp = bitcoin::secp256k1::Secp256k1::new();

    let hash = sha256d::Hash::hash(&out_bytes);
    let msg = bitcoin::secp256k1::Message::from_slice(&hash[..]).unwrap();
    secp.verify_ecdsa(
        &msg,
        &sig,
        &pk,
    ).unwrap();
}

This works just for this particular p2wpkh transaction, however it could show you how to get began. The bizarre factor is that the script_code is created by synthethizing a p2pkh output from the p2wpkh output. For p2wsh or p2sh (as any CHECKMULTISIG) script shall be, that is less complicated: you may simply use the redeem/witness script right here.

LEAVE A REPLY

Please enter your comment!
Please enter your name here