segregated witness – Utilizing the Bitcoin Crate to Signal a Segwit Transaction

on

|

views

and

comments


Utilizing the Bitcoin crate, I am making an attempt to create a transaction programmatically in Rust that spends an output related to a P2WPKH handle. That is the related code snippet:

fn sign_transaction<SignFun>(
    own_public_key: &[u8],
    own_address: &Tackle,
    own_utxos: &[Utxo],
    mut transaction: Transaction,
    key_name: String,
    derivation_path: Vec<Vec<u8>>,
    signer: SignFun,
) -> Transaction
the place
    SignFun: Fn(String, Vec<Vec<u8>>, Vec<u8>) -> Vec<u8>,
{

    let txclone = transaction.clone();
    let mut hash_cache = sighash::SighashCache::new(&txclone);
    for (index, enter) in transaction.enter.iter_mut().enumerate() {
        let worth = get_value(enter, own_utxos);    // Lookup the worth by discovering the corresponding UTXO
        let sighash = hash_cache
            .segwit_signature_hash(index, &own_address.script_pubkey(), worth, SIG_HASH_TYPE)
            .anticipate("Creating the segwit signature hash failed.");

        let signature = signer(key_name.clone(), derivation_path.clone(), sighash.to_vec()).await;

        // Convert signature to DER.
        let der_signature = sec1_to_der(signature);

        let mut sig_with_hashtype = der_signature;
        sig_with_hashtype.push(SIG_HASH_TYPE.to_u32() as u8);
        let witness_bytes = vec![sig_with_hashtype, own_public_key.to_vec()];
        enter.witness = Witness::from_vec(witness_bytes);
    }

    transaction
}

When sending a signed transaction to my native Bitcoin node in RegTest mode, I get the next error:

error code: -26
error message:
non-mandatory-script-verify-flag (Signature should be zero for failed CHECK(MULTI)SIG operation)

I logged the next info:

  • Public key: 0377f5de845ac601f24e7cbf2e4abcc9e1040cd4ae971ecaa00837b1c74684e15b
  • Tackle: bcrt1qh3zle7xs34azdyycg8cpf9wx5nxjpcqyqv4eyc
  • Enter spent with worth: 625000000
  • Transaction to signal: 0100000001ceac446d9350730c2a886220bed7ae154ca3f717897819091d5e72dcd0f0895e00000 00000ffffffff0200e1f505000000001600148be949ae15ee4b5da9af0ce2bf8d3f3c43c582da26 dc4a1f00000000160014bc45fcf8d08d7a26909841f01495c6a4cd20e00400000000
  • Sighash: d7e5696f18363b58c84b8d57014d291c9f7ebbac562d219f7e7014b9a5685bbf
  • SEC1 signature: c10c09b210914e49f295c07c9f96352e085df9d2c4272292239445d6f89483bc64c9903bebaba4b bf998d217c80375c36b60b212a824b63435e30205b2ed5a6a
  • DER signature: 3045022100c10c09b210914e49f295c07c9f96352e085df9d2c4272292239445d6f89483bc02206 4c9903bebaba4bbf998d217c80375c36b60b212a824b63435e30205b2ed5a6a
  • DER signature with Sighash kind: 3045022100c10c09b210914e49f295c07c9f96352e085df9d2c4272292239445d6f89483bc02206 4c9903bebaba4bbf998d217c80375c36b60b212a824b63435e30205b2ed5a6a01
  • Signed transaction: 01000000000101ceac446d9350730c2a886220bed7ae154ca3f717897819091d5e72dcd0f0895e0 000000000ffffffff0200e1f505000000001600148be949ae15ee4b5da9af0ce2bf8d3f3c43c582 da26dc4a1f00000000160014bc45fcf8d08d7a26909841f01495c6a4cd20e00402483045022100c 10c09b210914e49f295c07c9f96352e085df9d2c4272292239445d6f89483bc022064c9903bebab a4bbf998d217c80375c36b60b212a824b63435e30205b2ed5a6a01210377f5de845ac601f24e7cb f2e4abcc9e1040cd4ae971ecaa00837b1c74684e15b00000000

Observe {that a} comparable piece of code for legacy (P2PKH) transactions utilizing the identical ECDSA signer works completely, so I am assuming the signer is okay.

Any assist to determine the place the issue lies could be drastically appreciated!

Share this
Tags

Must-read

Waymo is attempting to seduce me. However an alternative choice is staring us within the face | Dave Schilling

It’s Tremendous Bowl weekend right here in America, which suggests a number of issues: copious quantities of gut-busting meals, controversial half-time present performances,...

Waymo raises $16bn to gas international robotaxi enlargement | Know-how

Self-driving automobile firm Waymo on Monday stated it raised $16bn in a funding spherical that valued the Alphabet subsidiary at $126bn.Waymo co-chief executives...

Self-driving taxis are coming to London – ought to we be anxious? | Jack Stilgoe

At the top of the nineteenth century, the world’s main cities had an issue. The streets had been flooded with manure, the unintended...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here