1

Greeting,

I hope someone can guide me about what is wrong with my p2wsh transaction hex below

020000000001014cf3b8d35c7b67fa3b669900f2a42b2e8b8a90487c2fca2b913b4e9d24a7b94e5d00000000ffffffff01e80300000000000022002053461e7b7bebf5089234af05cc4bfa5447ec7a9bc611c1f8d47dfc570a0339430221876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e5090024a821876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e509008700000000

i tried decode the hex and got result below :

{
"addresses": [
    "tb1qlwfxkadgkjykp24pcs699x3yx7egvm26khcpjheysknwv0ahh4zqmnc7xe",
    "tb1q2drpu7mma06s3y354uzucjl623r7c75mccgur7x50h79wzsr89psmcywrq"
],
"block_height": -1,
"block_index": -1,
"confirmations": 0,
"double_spend": false,
"fees": 1000,
"hash": "6ad4fce6043d8012105d6b2da5da3d8519124d8bf8c97fa5c1efad27a3dd72d9",
"inputs": [
    {
        "addresses": [
            "tb1qlwfxkadgkjykp24pcs699x3yx7egvm26khcpjheysknwv0ahh4zqmnc7xe"
        ],
        "age": 2579745,
        "output_index": 93,
        "output_value": 2000,
        "prev_hash": "4eb9a7249d4e3b912bca2f7c48908a8b2e2ba4f20099663bfa677b5cd3b8f34c",
        "script_type": "pay-to-witness-script-hash",
        "sequence": 4294967295,
        "witness": [
            "876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e50900",
            "a821876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e5090087"
        ]
    }
],
"outputs": [
    {
        "addresses": [
            "tb1q2drpu7mma06s3y354uzucjl623r7c75mccgur7x50h79wzsr89psmcywrq"
        ],
        "script": "002053461e7b7bebf5089234af05cc4bfa5447ec7a9bc611c1f8d47dfc570a033943",
        "script_type": "pay-to-witness-script-hash",
        "value": 1000
    }
],
"preference": "low",
"received": "2024-02-28T06:29:26.761160172Z",
"relayed_by": "54.226.234.175",
"size": 168,
"total": 1000,
"ver": 2,
"vin_sz": 1,
"vout_sz": 1,
"vsize": 113}

When i tried broadcast the raw transaction hex above, i got error "non-mandatory-script-verify-flag (Script evaluated without error but finished with a false/empty top stack element)"

I'm new to bitcoin development, and still struggle to understand by example.

1 Answer 1

1

The witness script is the last element pushed on witness, and in your case is a821....

Your address is tb1qlwf...

A P2WSH address is sha256(witness script), let's verify that:

$ hal address -t create --script a821876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e5090087

{
  "p2sh": "2NGYBv4ryXa55wg8wTfMUhUSEAqJhe8AB4j",
  "p2wsh": "tb1qlwfxkadgkjykp24pcs699x3yx7egvm26khcpjheysknwv0ahh4zqmnc7xe",
  "p2shwsh": "2NAx9vPRkc5obFLPYG98YnpRFo7TodYkLqx",
  "p2tr": "tb1prrmgv7lpzwqgt7vkkdfndgzpwmpz4lts79j8f8htsaz8p08fmv9sjvqhky"
}

Yes, the value for p2wsh (tb1qlwf...) matches the address. So far so good.

Now let's decode your witness script a821...:

$ hal script decode a821876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e5090087
 
OP_SHA256 OP_PUSHBYTES_33 876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e50900 OP_EQUAL

This means it expects some value, which sha256 hash will be equal to 876b....

The value your are passing is 876b..., i.e., the same value as the hash you're expecting. But this value, after being sha256 will be totally different, so the script will return false on OP_EQUAL

Let's verify that:

$ hal hash -t sha256 876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e50900

80afc01a83b056ec8861420311387ab2dc57aab6b4b564c060b65049a8eda1de

Since 876b... and 80af... are different, the script fails.

To fix this, on the first element of the witness you need to pass the value which sha256 is equal to 876b...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.