Bitcoinjs-lib: Error validating transaction: Error running script for input X referencing Y

Created on 11 Nov 2017  路  4Comments  路  Source: bitcoinjs/bitcoinjs-lib

Hi! I am trying to use bitcoinjs to create my transactions and can't get it to work with blockcypher's broadcast endpoint. I have tried with both Bitcoin testnet3 and Blockcypher test networks, and they both refuse the transaction hex, saying: 'Error validating transaction: Error running script for input 0 referencing e23212c57ccc622bae5470324f085e0fe825d6283c72b1cf1dfa475530a26e24 at 0: Script was NOT verified successfully..'

I have read that this kind of error could be related to missing a signature for an input or using the wrong keys to sign, but i couldn't figure it out by myself :P. Could anyone please give me a hint at what i may be doing wrong?

this is a sample of the transaction creation:

const blockcypherNetwork = {
    messagePrefix: '\x18Bitcoin Signed Message:\n',
    bech32: 'bc',
    bip32: {
        public: 0x0488b21e,
        private: 0x0488ade4
    },
    pubKeyHash: 0x1b,
    scriptHash: 0x1f,
    wif: 0x80
}

const tx = new bitcoin.TransactionBuilder(
    ApiUtil.isSandbox()
        ? blockcypherNetwork
        : bitcoin.networks.bitcoin
)

_.forEach(utxos, (ut: UXTO) => tx.addInput(ut.hash, ut.index))
_.forEach(outputs, (o: TransactionOutput) => tx.addOutput(o.address, o.value))

...
...

_.forEach(utxos, (_1, i) => tx.sign(i, getRoot().keyPair))

return tx.build()

getRoot method:

const seed = bip39.mnemonicToSeed(mnemonic)
return bitcoin.HDNode.fromSeedBuffer(
    seed,
    ApiUtil.isSandbox()
        ? blockcypherNetwork
        : bitcoin.networks.bitcoin
)

how i generate new addresses:

const newAddresses = (pathIndex: number) => {
const root = getRoot()

const basePath =
    ApiUtil.isSandbox()
        ? "m/44'/1'/0'"
        : "m/44'/0'/0'"

return {
    receiveAddress: root.derivePath(`${basePath}/0/${pathIndex}`).getAddress(),
    changeAddress: root.derivePath(`${basePath}/1/${pathIndex}`).getAddress(),
}

}

and finally, here is an example of the utxos and outputs:

utxos = [ 
    { hash: 'e23212c57ccc622bae5470324f085e0fe825d6283c72b1cf1dfa475530a26e24',
      index: 0,
      amount: 100000 },
   { hash: 'a8a0d42d748c432178b925e0706242eaf876c695c7bc7a76e8a41a7209a4bfb4',
     index: 0,
     amount: 1000 } ]

outputs = [ { address: 'BsBkvMgqxNDeo2YpRxQmWCQfUicgqmsJau', value: 100 } ]

PS: I am kinda new to blockchain :P

how to / question / docs

Most helpful comment

Oh! You are right! @dcousens Thanks a lot! I really appreciate it

All 4 comments

You are signing with m, but your UTXOs are probably for m/44'/i/k ... you need to sign each UTXO with its respective key pair, not the root one.

Oh! You are right! @dcousens Thanks a lot! I really appreciate it

@edunuzzi haha I made that exact same mistake 3 years ago when BIP32 was brand new, and I think bitcoinjs-lib had just added it.

Happy to help @edunuzzi :smiley: - well written issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

panpan2 picture panpan2  路  3Comments

ghost picture ghost  路  3Comments

silence-may picture silence-may  路  3Comments

askucher picture askucher  路  3Comments

thrastarson picture thrastarson  路  3Comments