I tried to create transaction which spends p2sh(p2wpkh) output:

  1. from wif I derived compressed pub key: "035476e55521c97cf0489e6c57893a99d67e84d306a69e6b6904f324c11b3cef8d".

  2. created witness program: "813798fcae182d1a36c6b4bb5f69e03bb9ec4124".

  3. created script: "0014813798fcae182d1a36c6b4bb5f69e03bb9ec4124" and sigscript from script which should be specified to spend this ouput: "160014813798fcae182d1a36c6b4bb5f69e03bb9ec4124".

  4. added tx input: {hash: "863fea23ba1c176bda5e360b0be25cacc405877aa3e46a3e1c60196c3b6cd7b6", vout: 0, signatureScript: scriptSig, witeness: nil}

  5. created pkscript for address "14s9ocrbTX3ZCybtcN1jmtdxxJwKuyM3Em" (p2sh-p2wpkh): "a914809800f62119afdcf2317882eda5efd7a9a2163487".

  6. added txout {amount: 11000, scriptPubKey: pkscript}

  7. calculated txSigHashes := txscript.NewTxSigHashes(tx)

  8. decoded utxo subscript into bytes: "a914bb4d332112bde9787ac2da5954ee5a2c5fbada6987".

  9. derived witness signature: wit := txscript.WitnessSignature(tx, txSigHashes, 0, amount, subscript, txscript.SigHashAll, wif.PrivKey, true) result: "3045022100bae83fd9e87751350a09bc4c120b29decadb13fb389541c3025a8982906602d5022005dc688901fd27b749cfd4ce14a62587f267a070984654271454e88cc3f9ed4e01 035476e55521c97cf0489e6c57893a99d67e84d306a69e6b6904f324c11b3cef8d".

  10. added witness signature to tx.TxIn[0].Witness

  11. Serializied signed tx: "01000000000101b6d76c3b6c19601c3e6ae4a37a8705c4ac5ce20b0b365eda6b171cba23ea3f860000000017160014813798fcae182d1a36c6b4bb5f69e03bb9ec4124ffffffff01f82a000000000000434104b2534d287f2d9fa908319557298591143fecbd3f6497d6d11f283b80c3248c35f580ff376805418c3610256bc102c0beb3e9f745d493aed81127f5d4a4921bebac02483045022100bae83fd9e87751350a09bc4c120b29decadb13fb389541c3025a8982906602d5022005dc688901fd27b749cfd4ce14a62587f267a070984654271454e88cc3f9ed4e0121035476e55521c97cf0489e6c57893a99d67e84d306a69e6b6904f324c11b3cef8d00000000"

Even if I followed all the rules to create this transaction any try to push it into mainnet (via Blockcypher and other services) failed because of this error: "Error validating transaction: Error running script for input 0 referencing 863fea23ba1c176bda5e360b0be25cacc405877aa3e46a3e1c60196c3b6cd7b6 at 0: Script was NOT verified successfully"

So my question is where have I wrong done? Which step is wrong and how to do it in a right way?

  • In step 9, are you signing with the entire value of the input under amount?
    – m1xolyd1an
    Commented Jul 22, 2018 at 2:27
  • yep, the amount is equal to 12780 satoshis
    – ziko
    Commented Jul 22, 2018 at 9:25
  • Does this "outpoint 0" really contains funds? When looking at blockchain.info, I thought I can see, that the funds for tx 863fea23ba1c176bda5e360b0be25cacc405877aa3e46a3e1c60196c3b6cd7b6 might have been sent already. Can you do a "listunspent | grep -A9 -B1 $tx_id" ? (where $tx_id equals the tx number from step 4). You may edit your original question and copy&paste output of the command... Commented Jul 23, 2018 at 6:40

1 Answer 1


I found that witness didn't include serializied script: that func for some reason just ingnored writing script into buffer, that means algorithm specified here(https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki, specification paragraph) ignored 5th step. So I removed if statement, and instead of (https://github.com/btcsuite/btcd/blob/master/txscript/script.go#L496) pasted writing has160 of pub key and that worked for me

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.