user91116: I have a very similar problem of my last post before post again, I tried a lot of time but whitout success. I studied this link but I don't find solution.
I have 50 bitcoins (total Amount) and I want to move 49.999991
It's my compressed public key 02c142cea1c25689d233b35c0021c47194ebc99cbc6154f7de66f2c078c1e773f3
And It's my transaction data
{
  "txid": "b4a786ec1a4263cec671aca7865ec71058230596e134c5b6bdf82d7cdc324ab6",
  "hash": "b4a786ec1a4263cec671aca7865ec71058230596e134c5b6bdf82d7cdc324ab6",
  "version": 2,
  "size": 83,
  "vsize": 83,
  "weight": 332,
  "locktime": 0,
  "vin": [
    {
      "txid": "414731e9d65e29e8830052333b316e9d882a6e93e068c4b63130a69ec67afba2",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 49.99999100,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 a37ff6b27599b48c2e0f30fc2e4255d0d81865fb OP_EQUAL",
        "hex": "a914a37ff6b27599b48c2e0f30fc2e4255d0d81865fb87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2N89jRd76YdyNA7Ztw1hQJ3ofLRUHqskiZ2"
        ]
      }
    }
  ]
}

0200000001a2fb7ac69ea63031b6c468e0936e2a889d6e313b33520083e8295ed6e93147410000000000ffffffff017cee052a0100000017a914a37ff6b27599b48c2e0f30fc2e4255d0d81865fb8700000000

The new transaction digest:
TX_VERSION: 02000000

SEQUENCE: ffffffff

OUTPOINT UTXO: a2fb7ac69ea63031b6c468e0936e2a889d6e313b33520083e8295ed6e931474100000000

HASH_PREV_OUT: 4ba3331b8f45c53aabdf26d43ff3ac93c839825cf400eddb935bf47685bc9412
(HASH_PREV_OUT is SHA256 twice of $OUTPOINT)

HASH_SEQUENCE: 3bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e70665044
(HASH_SEQUENCE is SHA256 twice of $SEQUENCE)

SCRIPTCODE: 1976a914555dbe4f4b6c5c8919df01dd15c2490b34383b83ac
(SCRIPTCODE is P2PKH script: OP_DUP OP_HASH160 <Public KeyHash> OP_EQUAL OP_CHECKSIG

AMOUNT: 7CEE052A01000000
(49.99999100000000000000)

OUTPUT: 17a914a37ff6b27599b48c2e0f30fc2e4255d0d81865fb87

OUTPUT_HASH: d7515382a6eed4652620ceb623aff05eb2f872e9185220ab2e456eebf2383197
(OUTPUT_HASH is SHA256 twice of $AMOUNT$OUTPUT)

LOCKTIME_PART: 00000000

SIGHASH: 01000000

Then "merge" all values and sign it.
where SIG_AMOUNT is 00F2052A01000000 (50 bitcoins)
$ WITNESS_V0_DIGEST=$TX_VERSION$HASH_PREV_OUT$HASH_SEQUENCE$OUTPOINT$SCRIPTCODE$SIG_AMOUNT$SEQUENCE$OUTPUT_HASH$LOCKTIME_PART$SIGHASH
echo $WITNESS_V0_DIGEST
020000004ba3331b8f45c53aabdf26d43ff3ac93c839825cf400eddb935bf47685bc94123bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e70665044a2fb7ac69ea63031b6c468e0936e2a889d6e313b33520083e8295ed6e9314741000000001976a914555dbe4f4b6c5c8919df01dd15c2490b34383b83ac00F2052A01000000ffffffffd7515382a6eed4652620ceb623aff05eb2f872e9185220ab2e456eebf23831970000000001000000

The sign it, ad put the signature, the public key, the redeem script inside my transaction.
My Signed transaction is:
02000000000101a2fb7ac69ea63031b6c468e0936e2a889d6e313b33520083e8295ed6e93147410000000017160014555dbe4f4b6c5c8919df01dd15c2490b34383b83ffffffff017cee052a0100000017a914a37ff6b27599b48c2e0f30fc2e4255d0d81865fb870247304402207f49b94bf1b7251dbb56b56c32f1183cfbdf1ed379b4c118d29430f355d6b05a02201800c4c05c6ba969e7d971f52a5b52aa096d9f392122b53129c11727851c983a012102c142cea1c25689d233b35c0021c47194ebc99cbc6154f7de66f2c078c1e773f300000000
{
  "txid": "ea46711ec5e4896632c6d2b1dead1ccf954d4576d2f2c23b1790813116e7ca6d",
  "hash": "50135ee943a252816b41f7f1b25f7ec95d178178427c2b5cc6a3c2a1041f71fe",
  "version": 2,
  "size": 215,
  "vsize": 134,
  "weight": 533,
  "locktime": 0,
  "vin": [
    {
      "txid": "414731e9d65e29e8830052333b316e9d882a6e93e068c4b63130a69ec67afba2",
      "vout": 0,
      "scriptSig": {
        "asm": "0014555dbe4f4b6c5c8919df01dd15c2490b34383b83",
        "hex": "160014555dbe4f4b6c5c8919df01dd15c2490b34383b83"
      },
      "txinwitness": [
        "304402207f49b94bf1b7251dbb56b56c32f1183cfbdf1ed379b4c118d29430f355d6b05a02201800c4c05c6ba969e7d971f52a5b52aa096d9f392122b53129c11727851c983a01",
        "02c142cea1c25689d233b35c0021c47194ebc99cbc6154f7de66f2c078c1e773f3"
      ],
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 49.999991,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 a37ff6b27599b48c2e0f30fc2e4255d0d81865fb OP_EQUAL",
        "hex": "a914a37ff6b27599b48c2e0f30fc2e4255d0d81865fb87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2N89jRd76YdyNA7Ztw1hQJ3ofLRUHqskiZ2"
        ]
      }
    }
  ]
}

it's seem well format but when send it, I receive that error
error code: -26
error message:
non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation) (code 64)

Im sorry to open a very similar post, but i slightly different.
Another question is: If I obtain the well format transaction (I can do decoderawtransaction without problem) it means that the problem is the signature?

user87716: 
SCRIPTCODE: 1976a914555dbe4f4b6c5c8919df01dd15c2490b34383b83ac
  (SCRIPTCODE is P2PKH script: OP_DUP OP_HASH160  OP_EQUAL OP_CHECKSIG

Your script code is missing the OP_CheckSig that is 0x88 so consequently your final hash (hashPreimage) is wrong and the signature is produced from an entirely different hash digest hence is invalid.

91116: <p>I have a very similar problem of my <a href="https://bitcoin.stackexchange.com/questions/95360/sign-manually-p2wsh-native-with-openssl">last post</a> before post again, I tried a lot of time but whitout success. I studied this <a href="https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#p2sh-p2wpkhP2PKH" rel="nofollow noreferrer">link</a> but I don't find solution.</p>

<p>I have 50 bitcoins (total Amount) and I want to move 49.999991
It's my compressed public key <code>02c142cea1c25689d233b35c0021c47194ebc99cbc6154f7de66f2c078c1e773f3</code></p>

<p>And It's my transaction data</p>

<pre><code>{
  "txid": "b4a786ec1a4263cec671aca7865ec71058230596e134c5b6bdf82d7cdc324ab6",
  "hash": "b4a786ec1a4263cec671aca7865ec71058230596e134c5b6bdf82d7cdc324ab6",
  "version": 2,
  "size": 83,
  "vsize": 83,
  "weight": 332,
  "locktime": 0,
  "vin": [
    {
      "txid": "414731e9d65e29e8830052333b316e9d882a6e93e068c4b63130a69ec67afba2",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 49.99999100,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 a37ff6b27599b48c2e0f30fc2e4255d0d81865fb OP_EQUAL",
        "hex": "a914a37ff6b27599b48c2e0f30fc2e4255d0d81865fb87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2N89jRd76YdyNA7Ztw1hQJ3ofLRUHqskiZ2"
        ]
      }
    }
  ]
}

0200000001a2fb7ac69ea63031b6c468e0936e2a889d6e313b33520083e8295ed6e93147410000000000ffffffff017cee052a0100000017a914a37ff6b27599b48c2e0f30fc2e4255d0d81865fb8700000000
</code></pre>

<p>The new transaction digest:</p>

<pre><code>TX_VERSION: 02000000

SEQUENCE: ffffffff

OUTPOINT UTXO: a2fb7ac69ea63031b6c468e0936e2a889d6e313b33520083e8295ed6e931474100000000

HASH_PREV_OUT: 4ba3331b8f45c53aabdf26d43ff3ac93c839825cf400eddb935bf47685bc9412
(HASH_PREV_OUT is SHA256 twice of $OUTPOINT)

HASH_SEQUENCE: 3bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e70665044
(HASH_SEQUENCE is SHA256 twice of $SEQUENCE)

SCRIPTCODE: 1976a914555dbe4f4b6c5c8919df01dd15c2490b34383b83ac
(SCRIPTCODE is P2PKH script: OP_DUP OP_HASH160 &lt;Public KeyHash&gt; OP_EQUAL OP_CHECKSIG

AMOUNT: 7CEE052A01000000
(49.99999100000000000000)

OUTPUT: 17a914a37ff6b27599b48c2e0f30fc2e4255d0d81865fb87

OUTPUT_HASH: d7515382a6eed4652620ceb623aff05eb2f872e9185220ab2e456eebf2383197
(OUTPUT_HASH is SHA256 twice of $AMOUNT$OUTPUT)

LOCKTIME_PART: 00000000

SIGHASH: 01000000
</code></pre>

<p>Then "merge" all values and sign it.
where SIG_AMOUNT is <code>00F2052A01000000</code> (50 bitcoins)</p>

<pre><code>$ WITNESS_V0_DIGEST=$TX_VERSION$HASH_PREV_OUT$HASH_SEQUENCE$OUTPOINT$SCRIPTCODE$SIG_AMOUNT$SEQUENCE$OUTPUT_HASH$LOCKTIME_PART$SIGHASH
echo $WITNESS_V0_DIGEST
020000004ba3331b8f45c53aabdf26d43ff3ac93c839825cf400eddb935bf47685bc94123bb13029ce7b1f559ef5e747fcac439f1455a2ec7c5f09b72290795e70665044a2fb7ac69ea63031b6c468e0936e2a889d6e313b33520083e8295ed6e9314741000000001976a914555dbe4f4b6c5c8919df01dd15c2490b34383b83ac00F2052A01000000ffffffffd7515382a6eed4652620ceb623aff05eb2f872e9185220ab2e456eebf23831970000000001000000
</code></pre>

<p>The sign it, ad put the signature, the public key, the redeem script inside my transaction.
My Signed transaction is:</p>

<pre><code>02000000000101a2fb7ac69ea63031b6c468e0936e2a889d6e313b33520083e8295ed6e93147410000000017160014555dbe4f4b6c5c8919df01dd15c2490b34383b83ffffffff017cee052a0100000017a914a37ff6b27599b48c2e0f30fc2e4255d0d81865fb870247304402207f49b94bf1b7251dbb56b56c32f1183cfbdf1ed379b4c118d29430f355d6b05a02201800c4c05c6ba969e7d971f52a5b52aa096d9f392122b53129c11727851c983a012102c142cea1c25689d233b35c0021c47194ebc99cbc6154f7de66f2c078c1e773f300000000
{
  "txid": "ea46711ec5e4896632c6d2b1dead1ccf954d4576d2f2c23b1790813116e7ca6d",
  "hash": "50135ee943a252816b41f7f1b25f7ec95d178178427c2b5cc6a3c2a1041f71fe",
  "version": 2,
  "size": 215,
  "vsize": 134,
  "weight": 533,
  "locktime": 0,
  "vin": [
    {
      "txid": "414731e9d65e29e8830052333b316e9d882a6e93e068c4b63130a69ec67afba2",
      "vout": 0,
      "scriptSig": {
        "asm": "0014555dbe4f4b6c5c8919df01dd15c2490b34383b83",
        "hex": "160014555dbe4f4b6c5c8919df01dd15c2490b34383b83"
      },
      "txinwitness": [
        "304402207f49b94bf1b7251dbb56b56c32f1183cfbdf1ed379b4c118d29430f355d6b05a02201800c4c05c6ba969e7d971f52a5b52aa096d9f392122b53129c11727851c983a01",
        "02c142cea1c25689d233b35c0021c47194ebc99cbc6154f7de66f2c078c1e773f3"
      ],
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 49.999991,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 a37ff6b27599b48c2e0f30fc2e4255d0d81865fb OP_EQUAL",
        "hex": "a914a37ff6b27599b48c2e0f30fc2e4255d0d81865fb87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2N89jRd76YdyNA7Ztw1hQJ3ofLRUHqskiZ2"
        ]
      }
    }
  ]
}
</code></pre>

<p>it's seem well format but when send it, I receive that error</p>

<pre><code>error code: -26
error message:
non-mandatory-script-verify-flag (Signature must be zero for failed CHECK(MULTI)SIG operation) (code 64)
</code></pre>

<p>Im sorry to open a very similar post, but i slightly different.
Another question is: If I obtain the well format transaction (I can do decoderawtransaction without problem) it means that the problem is the signature?</p>
 87716: <blockquote>
  <p>SCRIPTCODE: 1976a914555dbe4f4b6c5c8919df01dd15c2490b34383b83ac<br>
  (SCRIPTCODE is P2PKH script: OP_DUP OP_HASH160  OP_EQUAL OP_CHECKSIG</p>
</blockquote>

<p>Your script code is missing the <code>OP_CheckSig</code> that is <code>0x88</code> so consequently your final hash (<code>hashPreimage</code>) is wrong and the signature is produced from an entirely different hash digest hence is invalid.</p>
