<p>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(<a href="https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki" rel="nofollow noreferrer">https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki</a>, specification paragraph) ignored 5th step. So I removed if statement, and instead of (<a href="https://github.com/btcsuite/btcd/blob/master/txscript/script.go#L496" rel="nofollow noreferrer">https://github.com/btcsuite/btcd/blob/master/txscript/script.go#L496</a>) pasted writing has160 of pub key and that worked for me</p>

CC BY-SA 4.0
2018-07-23T09:28:24.343Z
2018-07-25T18:46:08.410Z
2018-07-25T18:46:08.410Z
ziko
<p>I tried to create transaction which spends p2sh(p2wpkh) output:</p>

<ol>
<li><p>from wif I derived compressed pub key: "035476e55521c97cf0489e6c57893a99d67e84d306a69e6b6904f324c11b3cef8d".</p></li>
<li><p>created witness program: "813798fcae182d1a36c6b4bb5f69e03bb9ec4124".</p></li>
<li><p>created script: "0014813798fcae182d1a36c6b4bb5f69e03bb9ec4124" and sigscript from script which should be specified to spend this ouput: "160014813798fcae182d1a36c6b4bb5f69e03bb9ec4124".</p></li>
<li><p>added tx input: {hash: "863fea23ba1c176bda5e360b0be25cacc405877aa3e46a3e1c60196c3b6cd7b6", vout: 0, signatureScript: scriptSig, witeness: nil}</p></li>
<li><p>created pkscript for address "14s9ocrbTX3ZCybtcN1jmtdxxJwKuyM3Em" (p2sh-p2wpkh): "a914809800f62119afdcf2317882eda5efd7a9a2163487". </p></li>
<li><p>added txout {amount: 11000, scriptPubKey: pkscript}</p></li>
<li><p>calculated txSigHashes := txscript.NewTxSigHashes(tx)</p></li>
<li><p>decoded utxo subscript into bytes: "a914bb4d332112bde9787ac2da5954ee5a2c5fbada6987".</p></li>
<li><p>derived witness signature: wit := txscript.WitnessSignature(tx, txSigHashes, 0, amount, subscript, txscript.SigHashAll, wif.PrivKey, true)
result: "3045022100bae83fd9e87751350a09bc4c120b29decadb13fb389541c3025a8982906602d5022005dc688901fd27b749cfd4ce14a62587f267a070984654271454e88cc3f9ed4e01 035476e55521c97cf0489e6c57893a99d67e84d306a69e6b6904f324c11b3cef8d".</p></li>
<li><p>added witness signature to tx.TxIn[0].Witness</p></li>
<li><p>Serializied signed tx: "01000000000101b6d76c3b6c19601c3e6ae4a37a8705c4ac5ce20b0b365eda6b171cba23ea3f860000000017160014813798fcae182d1a36c6b4bb5f69e03bb9ec4124ffffffff01f82a000000000000434104b2534d287f2d9fa908319557298591143fecbd3f6497d6d11f283b80c3248c35f580ff376805418c3610256bc102c0beb3e9f745d493aed81127f5d4a4921bebac02483045022100bae83fd9e87751350a09bc4c120b29decadb13fb389541c3025a8982906602d5022005dc688901fd27b749cfd4ce14a62587f267a070984654271454e88cc3f9ed4e0121035476e55521c97cf0489e6c57893a99d67e84d306a69e6b6904f324c11b3cef8d00000000"</p></li>
</ol>

<p>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"</p>

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

CC BY-SA 4.0
2018-07-22T02:27:27.187Z
In step 9, are you signing with the *entire* value of the input under `amount`?
m1xolyd1an
CC BY-SA 4.0
2018-07-22T09:25:56.910Z
yep, the amount is equal to 12780 satoshis
ziko
CC BY-SA 4.0
2018-07-23T06:40:41.867Z
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...
pebwindkraft
<transactions><segregated-witness><raw-transaction>
Segwit transaction in golang
ziko
# Segwit transaction in golang

(Asked by: username_0 on 2018-07-20)

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?

**Tags:** transactions, segregated-witness, raw-transaction

> **Comments:**
> **username_1**: In step 9, are you signing with the *entire* value of the input under `amount`?  
> **username_0**: yep, the amount is equal to 12780 satoshis  
> **username_2**: 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...  

---

## 1 answers

### Answer by username_0 on 2018-07-23. Score: 2 | Accepted answer

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


---

