<p>You have the wrong public key inside your SignatureScript:  </p>

<pre><code>Sig: 48-304502210093b8ff9180a8a31a5ee7b0fb6c560aef888a3d53576e530cb4601dce2d3c51da02204fd8a28743f1d1100a2f56428e743501091e833550df6cf540bad4f5248a6d3c-01
pubkey: 21-02d794c7e1a8068782ef6ee68acf9f6ad9c84c78349671c1a7bf60418a8e1ed108
</code></pre>

<p>The outpoint you are spending (<code>2611[...]fd57:0</code>) has the following PubkeyScript: </p>

<pre><code>76-a9-14-2937902660c169b9381a5b3eb35cc889a4686a85-88-ac
</code></pre>

<p>When the script runs it fails on 0x88 (or OP_EqualVerify) because HASH160 of the given public key (02d7[...]d108) is <code>e652ed9163ac72629584ec92f47602cca45aa969</code></p>

CC BY-SA 4.0
2020-01-10T07:16:02.540Z
amazing feedback.  I will try to follow your logic and figure it out.  thanks a million
ekkis
CC BY-SA 4.0
2020-01-10T07:43:53.520Z
I've added code to the post to clarify how I'm building the transaction and what I'm using to sign
ekkis
CC BY-SA 4.0
2020-01-10T08:00:37.043Z
ok.  I've come to understand what you said, and I gather that the field `inputs[0].script` is populated by my call to the transaction builder's `sign()` function.  that doesn't match the script-pubkey that Block.io generated when I sent coins from there to my address `mjGtZWXgPdbLUZYWtDqVTfbFieVzXFfSEj`... so the question is why that would happen (and how to fix it)
ekkis
CC BY-SA 4.0
2020-01-10T08:06:55.117Z
FWIW, if I `console.log(pubkey.toString('hex')` I get:  `026c4df6ad30a627bcb045faf913bb25aad26ef9d06a838f8465aead0a63c869ad`... which seems to be the Hash160 of `15iBr8YmiWhXbCWRtZhuY9zowF412EwCypjNvY3xX79CX12K8ZH`
ekkis
CC BY-SA 4.0
2020-01-10T08:17:49.077Z
as you can see, I'm passing the root directly to the signature function.  originally I was converting to a WIF and then to a key, but I was told I could pass the root directly.  see: https://github.com/bitcoinjs/bip39/issues/129#issuecomment-572365691
ekkis
CC BY-SA 4.0
2020-01-10T08:58:58.080Z
I'm not familiar with bitcoinjs library you have to check the source code/read its docs. But it seems to me that you are passing the "root key" in both your tries. To sign a transaction you need the same "child" key that was used to create the address not the root.
Coding Enthusiast
CC BY-SA 4.0
2020-01-10T22:43:56.193Z
yes.  you're correct.  it took me a while to figure out how to fix it but you gave me all I needed.  I owe you a beer
ekkis
CC BY-SA 4.0
2020-01-10T06:38:36.760Z
2020-01-10T06:38:36.760Z
Coding Enthusiast
<p>I've created a transaction on the TESTNET and am trying to push it via BlockCyphers facility at:</p>

<p><a href="https://live.blockcypher.com/btc-testnet/pushtx/" rel="nofollow noreferrer">https://live.blockcypher.com/btc-testnet/pushtx/</a></p>

<p>when I make the attempt, I get the following error:</p>

<blockquote>
  <p>Error validating transaction: Error running script for input 0
  referencing
  261190892daa2c9aaaf25c20624f7f2941d2ad58dec883b0c929d1fa9306fd57 at 0:
  Script was NOT verified successfully..</p>
</blockquote>

<p>here's the transaction.  I don't see what the matter with it is and not sure I understand the error.  can anyone help?</p>

<pre><code>{
    "addresses": [
        "mjGtZWXgPdbLUZYWtDqVTfbFieVzXFfSEj", 
        "2N7nSZjfqikj4oVkCqaLB3CNMY3TA72PbNY", 
        "msp7depXvcjLqykZxYVrWNG9QgKqLWWKEN"
    ], 
    "block_height": -1, 
    "block_index": -1, 
    "confirmations": 0, 
    "double_spend": false, 
    "fees": 12375, 
    "hash": "463372d7a8f456c882179635aabbeca4146396b1f81c05699a2e885e3f84d6e6", 
    "inputs": [
        {
            "addresses": [
                "mjGtZWXgPdbLUZYWtDqVTfbFieVzXFfSEj"
            ], 
            "age": 1657999, 
            "output_index": 0, 
            "output_value": 5000000, 
            "prev_hash": "261190892daa2c9aaaf25c20624f7f2941d2ad58dec883b0c929d1fa9306fd57", 
            "script": "48304502210093b8ff9180a8a31a5ee7b0fb6c560aef888a3d53576e530cb4601dce2d3c51da02204fd8a28743f1d1100a2f56428e743501091e833550df6cf540bad4f5248a6d3c012102d794c7e1a8068782ef6ee68acf9f6ad9c84c78349671c1a7bf60418a8e1ed108", 
            "script_type": "pay-to-pubkey-hash", 
            "sequence": 4294967295
        }
    ], 
    "outputs": [
        {
            "addresses": [
                "2N7nSZjfqikj4oVkCqaLB3CNMY3TA72PbNY"
            ], 
            "script": "a9149f792783320ffb5a4f3ee4c69525d4932ac0497087", 
            "script_type": "pay-to-script-hash", 
            "value": 500
        }, 
        {
            "addresses": [
                "msp7depXvcjLqykZxYVrWNG9QgKqLWWKEN"
            ], 
            "script": "76a91486e0602ab38efd1811ff4db76f7d58c717c8598888ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 4987125
        }
    ], 
    "preference": "high", 
    "received": "2020-01-10T05:09:49.945289619Z", 
    "relayed_by": "54.197.194.161", 
    "size": 224, 
    "total": 4987625, 
    "ver": 2, 
    "vin_sz": 1, 
    "vout_sz": 2
}
</code></pre>

<p><strong>- Addendum I -</strong></p>

<p>to help narrow down the solution, here's the code I'm using to produce the transaction:
I first produce an address where to receive some coin:</p>



<pre><code>const btc = require('bitcoinjs-lib');
const bip39 = require('bip39');
const bip32 = require('bip32');

var mnem = 'my-12-word-bip39-seed-phrase'
var root = = bip32.fromSeed(
  bip39.mnemonicToSeedSync(mnem), bitcoin.networks.testnet
);
var p = "m/44'/1'/0'/0/0"
var pubkey = root.derivePath(p).publicKey;
var {address} = btc.payments.p2pkh({pubkey, network: btc.networks.testnet});
console.log(address);
</code></pre>

<p>which yields <code>mjGtZWXgPdbLUZYWtDqVTfbFieVzXFfSEj</code>.  I then send some coin to that address and attempt to create my transaction:</p>



<pre><code>const CS = require('coinselect');
var feeRate = 55;
var env = 'test3'
var src = 'mjGtZWXgPdbLUZYWtDqVTfbFieVzXFfSEj'
var url = `https://api.blockcypher.com/v1/btc/${env}/addrs/${src}?unspendOnly=true`;
var o = await fetch(url).then(res =&gt; res.json());
var utxos = o.txrefs.map(o =&gt; ({
    txId: o.tx_hash, 
    vout: parseInt(o.tx_output_n), 
    value: parseInt(o.value)
}));
var targets = [{address: '2N7nSZjfqikj4oVkCqaLB3CNMY3TA72PbNY', value: 500}];
var {inputs, outputs, fee} = CS(utxos, targets, feeRate);
if (ret.outputs) ret.outputs.forEach(o =&gt; {
    if (!o.address) o.address = 'msp7depXvcjLqykZxYVrWNG9QgKqLWWKEN';
})

// builds the transaction
var tx = new btc.TransactionBuilder(btc.networks.testnet);
inputs.forEach(o =&gt; tx.addInput(o.txId, o.vout));
outputs.forEach(o =&gt; tx.addOutput(o.address, o.value));
tx.sign(0, root);
console.log(tx.build().toHex());
</code></pre>

<p>from which I get a numerical string that converts to the above transaction.  as can be seen, I'm using the same root to sign the transaction that was used to create the source address</p>

CC BY-SA 4.0
2020-01-10T05:36:18.833Z
Can you show or tell us how you are creating your transaction? Hard to say without more info.
m1xolyd1an
CC BY-SA 4.0
2020-01-10T07:27:36.593Z
yes.  I will post code to help identify the issue
ekkis
<raw-transaction>
Transaction fails to validate (Error running script)
ekkis
# Transaction fails to validate (Error running script)

(Asked by: username_0 on 2020-01-10)

I've created a transaction on the TESTNET and am trying to push it via BlockCyphers facility at:

<https://live.blockcypher.com/btc-testnet/pushtx/>

when I make the attempt, I get the following error:

> Error validating transaction: Error running script for input 0
> referencing
> 261190892daa2c9aaaf25c20624f7f2941d2ad58dec883b0c929d1fa9306fd57 at 0:
> Script was NOT verified successfully..

here's the transaction. I don't see what the matter with it is and not sure I understand the error. can anyone help?

```
{
    "addresses": [
        "mjGtZWXgPdbLUZYWtDqVTfbFieVzXFfSEj", 
        "2N7nSZjfqikj4oVkCqaLB3CNMY3TA72PbNY", 
        "msp7depXvcjLqykZxYVrWNG9QgKqLWWKEN"
    ], 
    "block_height": -1, 
    "block_index": -1, 
    "confirmations": 0, 
    "double_spend": false, 
    "fees": 12375, 
    "hash": "463372d7a8f456c882179635aabbeca4146396b1f81c05699a2e885e3f84d6e6", 
    "inputs": [
        {
            "addresses": [
                "mjGtZWXgPdbLUZYWtDqVTfbFieVzXFfSEj"
            ], 
            "age": 1657999, 
            "output_index": 0, 
            "output_value": 5000000, 
            "prev_hash": "261190892daa2c9aaaf25c20624f7f2941d2ad58dec883b0c929d1fa9306fd57", 
            "script": "48304502210093b8ff9180a8a31a5ee7b0fb6c560aef888a3d53576e530cb4601dce2d3c51da02204fd8a28743f1d1100a2f56428e743501091e833550df6cf540bad4f5248a6d3c012102d794c7e1a8068782ef6ee68acf9f6ad9c84c78349671c1a7bf60418a8e1ed108", 
            "script_type": "pay-to-pubkey-hash", 
            "sequence": 4294967295
        }
    ], 
    "outputs": [
        {
            "addresses": [
                "2N7nSZjfqikj4oVkCqaLB3CNMY3TA72PbNY"
            ], 
            "script": "a9149f792783320ffb5a4f3ee4c69525d4932ac0497087", 
            "script_type": "pay-to-script-hash", 
            "value": 500
        }, 
        {
            "addresses": [
                "msp7depXvcjLqykZxYVrWNG9QgKqLWWKEN"
            ], 
            "script": "76a91486e0602ab38efd1811ff4db76f7d58c717c8598888ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 4987125
        }
    ], 
    "preference": "high", 
    "received": "2020-01-10T05:09:49.945289619Z", 
    "relayed_by": "54.197.194.161", 
    "size": 224, 
    "total": 4987625, 
    "ver": 2, 
    "vin_sz": 1, 
    "vout_sz": 2
}

```

**- Addendum I -**

to help narrow down the solution, here's the code I'm using to produce the transaction:
I first produce an address where to receive some coin:

```
const btc = require('bitcoinjs-lib');
const bip39 = require('bip39');
const bip32 = require('bip32');

var mnem = 'my-12-word-bip39-seed-phrase'
var root = = bip32.fromSeed(
  bip39.mnemonicToSeedSync(mnem), bitcoin.networks.testnet
);
var p = "m/44'/1'/0'/0/0"
var pubkey = root.derivePath(p).publicKey;
var {address} = btc.payments.p2pkh({pubkey, network: btc.networks.testnet});
console.log(address);

```

which yields `mjGtZWXgPdbLUZYWtDqVTfbFieVzXFfSEj`. I then send some coin to that address and attempt to create my transaction:

```
const CS = require('coinselect');
var feeRate = 55;
var env = 'test3'
var src = 'mjGtZWXgPdbLUZYWtDqVTfbFieVzXFfSEj'
var url = `https://api.blockcypher.com/v1/btc/${env}/addrs/${src}?unspendOnly=true`;
var o = await fetch(url).then(res => res.json());
var utxos = o.txrefs.map(o => ({
    txId: o.tx_hash, 
    vout: parseInt(o.tx_output_n), 
    value: parseInt(o.value)
}));
var targets = [{address: '2N7nSZjfqikj4oVkCqaLB3CNMY3TA72PbNY', value: 500}];
var {inputs, outputs, fee} = CS(utxos, targets, feeRate);
if (ret.outputs) ret.outputs.forEach(o => {
    if (!o.address) o.address = 'msp7depXvcjLqykZxYVrWNG9QgKqLWWKEN';
})

// builds the transaction
var tx = new btc.TransactionBuilder(btc.networks.testnet);
inputs.forEach(o => tx.addInput(o.txId, o.vout));
outputs.forEach(o => tx.addOutput(o.address, o.value));
tx.sign(0, root);
console.log(tx.build().toHex());

```

from which I get a numerical string that converts to the above transaction. as can be seen, I'm using the same root to sign the transaction that was used to create the source address

**Tags:** raw-transaction

> **Comments:**
> **username_1**: Can you show or tell us how you are creating your transaction? Hard to say without more info.  
> **username_0**: yes.  I will post code to help identify the issue  

---

## 1 answers

### Answer by username_2 on 2020-01-10. Score: 1 | Accepted answer

You have the wrong public key inside your SignatureScript:

```
Sig: 48-304502210093b8ff9180a8a31a5ee7b0fb6c560aef888a3d53576e530cb4601dce2d3c51da02204fd8a28743f1d1100a2f56428e743501091e833550df6cf540bad4f5248a6d3c-01
pubkey: 21-02d794c7e1a8068782ef6ee68acf9f6ad9c84c78349671c1a7bf60418a8e1ed108

```

The outpoint you are spending (`2611[...]fd57:0`) has the following PubkeyScript:

```
76-a9-14-2937902660c169b9381a5b3eb35cc889a4686a85-88-ac

```

When the script runs it fails on 0x88 (or OP\_EqualVerify) because HASH160 of the given public key (02d7[...]d108) is `e652ed9163ac72629584ec92f47602cca45aa969`

> **Comments:**
> **username_0**: amazing feedback.  I will try to follow your logic and figure it out.  thanks a million  
> **username_0**: I've added code to the post to clarify how I'm building the transaction and what I'm using to sign  
> **username_0**: ok.  I've come to understand what you said, and I gather that the field `inputs[0].script` is populated by my call to the transaction builder's `sign()` function.  that doesn't match the script-pubkey that Block.io generated when I sent coins from there to my address `mjGtZWXgPdbLUZYWtDqVTfbFieVzXFfSEj`... so the question is why that would happen (and how to fix it)  
> **username_0**: FWIW, if I `console.log(pubkey.toString('hex')` I get:  `026c4df6ad30a627bcb045faf913bb25aad26ef9d06a838f8465aead0a63c869ad`... which seems to be the Hash160 of `15iBr8YmiWhXbCWRtZhuY9zowF412EwCypjNvY3xX79CX12K8ZH`  
> **username_0**: as you can see, I'm passing the root directly to the signature function.  originally I was converting to a WIF and then to a key, but I was told I could pass the root directly.  see: https://github.com/bitcoinjs/bip39/issues/129#issuecomment-572365691  
> **username_2**: I'm not familiar with bitcoinjs library you have to check the source code/read its docs. But it seems to me that you are passing the "root key" in both your tries. To sign a transaction you need the same "child" key that was used to create the address not the root.  
> **username_0**: yes.  you're correct.  it took me a while to figure out how to fix it but you gave me all I needed.  I owe you a beer  

---

