<p>The witness script is the last element pushed on witness, and in your case is <code>a821...</code>.</p>
<p>Your address is <code>tb1qlwf...</code></p>
<p>A P2WSH address is <code>sha256(witness script)</code>, let's verify that:</p>
<pre><code>$ hal address -t create --script a821876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e5090087

{
  &quot;p2sh&quot;: &quot;2NGYBv4ryXa55wg8wTfMUhUSEAqJhe8AB4j&quot;,
  &quot;p2wsh&quot;: &quot;tb1qlwfxkadgkjykp24pcs699x3yx7egvm26khcpjheysknwv0ahh4zqmnc7xe&quot;,
  &quot;p2shwsh&quot;: &quot;2NAx9vPRkc5obFLPYG98YnpRFo7TodYkLqx&quot;,
  &quot;p2tr&quot;: &quot;tb1prrmgv7lpzwqgt7vkkdfndgzpwmpz4lts79j8f8htsaz8p08fmv9sjvqhky&quot;
}
</code></pre>
<p>Yes, the value for <code>p2wsh</code> (<code>tb1qlwf...</code>) matches the address. So far so good.</p>
<p>Now let's decode your witness script <code>a821...</code>:</p>
<pre><code>$ hal script decode a821876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e5090087
 
OP_SHA256 OP_PUSHBYTES_33 876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e50900 OP_EQUAL
</code></pre>
<p>This means it expects some value, which sha256 hash will be equal to <code>876b...</code>.</p>
<p>The value your are passing is <code>876b...</code>, 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 <code>OP_EQUAL</code></p>
<p>Let's verify that:</p>
<pre><code>$ hal hash -t sha256 876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e50900

80afc01a83b056ec8861420311387ab2dc57aab6b4b564c060b65049a8eda1de
</code></pre>
<p>Since <code>876b...</code> and <code>80af...</code> are different, the script fails.</p>
<p>To fix this, on the first element of the witness you need to pass the value which sha256 is equal to <code>876b...</code></p>

CC BY-SA 4.0
2024-02-28T19:50:42.097Z
2024-04-17T05:26:21.177Z
2024-04-17T05:26:21.177Z
bordalix
<p>Greeting,</p>
<p>I hope someone can guide me about what is wrong with my p2wsh transaction hex below</p>
<pre><code>020000000001014cf3b8d35c7b67fa3b669900f2a42b2e8b8a90487c2fca2b913b4e9d24a7b94e5d00000000ffffffff01e80300000000000022002053461e7b7bebf5089234af05cc4bfa5447ec7a9bc611c1f8d47dfc570a0339430221876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e5090024a821876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e509008700000000
</code></pre>
<p>i tried decode the hex and got result below :</p>
<pre><code>{
&quot;addresses&quot;: [
    &quot;tb1qlwfxkadgkjykp24pcs699x3yx7egvm26khcpjheysknwv0ahh4zqmnc7xe&quot;,
    &quot;tb1q2drpu7mma06s3y354uzucjl623r7c75mccgur7x50h79wzsr89psmcywrq&quot;
],
&quot;block_height&quot;: -1,
&quot;block_index&quot;: -1,
&quot;confirmations&quot;: 0,
&quot;double_spend&quot;: false,
&quot;fees&quot;: 1000,
&quot;hash&quot;: &quot;6ad4fce6043d8012105d6b2da5da3d8519124d8bf8c97fa5c1efad27a3dd72d9&quot;,
&quot;inputs&quot;: [
    {
        &quot;addresses&quot;: [
            &quot;tb1qlwfxkadgkjykp24pcs699x3yx7egvm26khcpjheysknwv0ahh4zqmnc7xe&quot;
        ],
        &quot;age&quot;: 2579745,
        &quot;output_index&quot;: 93,
        &quot;output_value&quot;: 2000,
        &quot;prev_hash&quot;: &quot;4eb9a7249d4e3b912bca2f7c48908a8b2e2ba4f20099663bfa677b5cd3b8f34c&quot;,
        &quot;script_type&quot;: &quot;pay-to-witness-script-hash&quot;,
        &quot;sequence&quot;: 4294967295,
        &quot;witness&quot;: [
            &quot;876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e50900&quot;,
            &quot;a821876bf128fa0fe05d617494045dc8dc27a530ec4d9c80e8d35eac377bd9c4e5090087&quot;
        ]
    }
],
&quot;outputs&quot;: [
    {
        &quot;addresses&quot;: [
            &quot;tb1q2drpu7mma06s3y354uzucjl623r7c75mccgur7x50h79wzsr89psmcywrq&quot;
        ],
        &quot;script&quot;: &quot;002053461e7b7bebf5089234af05cc4bfa5447ec7a9bc611c1f8d47dfc570a033943&quot;,
        &quot;script_type&quot;: &quot;pay-to-witness-script-hash&quot;,
        &quot;value&quot;: 1000
    }
],
&quot;preference&quot;: &quot;low&quot;,
&quot;received&quot;: &quot;2024-02-28T06:29:26.761160172Z&quot;,
&quot;relayed_by&quot;: &quot;54.226.234.175&quot;,
&quot;size&quot;: 168,
&quot;total&quot;: 1000,
&quot;ver&quot;: 2,
&quot;vin_sz&quot;: 1,
&quot;vout_sz&quot;: 1,
&quot;vsize&quot;: 113}
</code></pre>
<p>When i tried broadcast the raw transaction hex above, i got error &quot;<strong>non-mandatory-script-verify-flag (Script evaluated without error but finished with a false/empty top stack element)</strong>&quot;</p>
<p>I'm new to bitcoin development, and still struggle to understand by example.</p>

<bitcoinjs><bip174-psbt><p2wsh>
broadcast ps2wsh transaction failed
ron s
# broadcast ps2wsh transaction failed

(Asked by: username_0 on 2024-02-28)

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.

**Tags:** bitcoinjs, bip174-psbt, p2wsh


---

## 1 answers

### Answer by username_1 on 2024-02-28. Score: 1 | Accepted answer

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...


---

