bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.62k stars 2.09k forks source link

TypeError('Invalid script') Error when trying to test out bech32 to bech32 transaction #1193

Closed PeterGarner closed 5 years ago

PeterGarner commented 5 years ago

trace

/Users/peter/Desktop/MSC_Project_writeup/script_files/back-end/node_modules/bitcoinjs-lib/src/classify.js:31
  if (!chunks) throw new TypeError('Invalid script')
               ^
TypeError: Invalid script
    at Object.classifyOutput [as output] (/Users/peter/Desktop/MSC_Project_writeup/script_files/back-end/node_modules/bitcoinjs-lib/src/classify.js:31:22)
    at expandOutput (/Users/peter/Desktop/MSC_Project_writeup/script_files/back-end/node_modules/bitcoinjs-lib/src/transaction_builder.js:163:25)
    at TransactionBuilder.__addInputUnsafe (/Users/peter/Desktop/MSC_Project_writeup/script_files/back-end/node_modules/bitcoinjs-lib/src/transaction_builder.js:547:24)
    at TransactionBuilder.addInput (/Users/peter/Desktop/MSC_Project_writeup/script_files/back-end/node_modules/bitcoinjs-lib/src/transaction_builder.js:515:15)

code

const pubBuffAddress = keyMaker.makeKey({ 'master': 'tprv8Z...', 'buffer': true })
const privateKey = keyMaker.makeKey({ 'master': 'tprv8Z...', 'buffer': 'private' })

let txb = new bitcoin.TransactionBuilder(testnet)

txb.addInput("b43badc0e9f2421a77be3fccd5d6b8345d4144c6bfa2069777dd39bf1283a22f",
 0, null, pubBuffAddress.pubkey)

txb.addOutput("tb1qxkt4dgqk2h8ta8rakk32sfj60gmhhwr7wddvpu", 99943000)
txb.sign(i, privateKey, null, null, 100000000)

const tx = txb.build() 
const txt = tx.toHex()

makekey

  makeKey: function(keyType) { 
    function getPubAddress(derived, buffer) {
      if (buffer) { 
        const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: derived.publicKey, network: bitcoin.networks.testnet })
        return p2wpkh 
      }
      const { address } = bitcoin.payments.p2wpkh({ pubkey: derived.publicKey, network: bitcoin.networks.testnet })
      return address 
    }
    const master = bip32.fromBase58(keyType.master, testnet)
    const derived = master.derivePath("m/44'/0'/0'/0/0") 
    if (keyType.buffer === 'private') { 
      return derived
    }
    return getPubAddress(derived, keyType.buffer)
  }
dabura667 commented 5 years ago

pubBuffAddress.pubkey is not a valid script. If your input is a P2WPKH then you need the proper script for that.

PeterGarner commented 5 years ago

sorry, fixed this now. was not following the example, compounded by bad scripting

PeterGarner commented 5 years ago
const pubBuffAddress = keyMaker.makeKey({ 'master': 'tprv8...', 'buffer': true })
 const privateKey = keyMaker.makeKey({ 'master': 'tprv8..', 'buffer': 'private' })

let txb = new bitcoin.TransactionBuilder(testnet) 

txb.addInput("b43badc0e9f2421a77be3fccd5d6b8345d4144c6bfa2069777dd39bf1283a22f", 0, null, pubBuffAddress.output)

txb.addOutput("tb1qxkt4dgqk2h8ta8rakk32sfj60gmhhwr7wddvpu", 99943000)
txb.sign(0, privateKey, null, null, 100000000)

pubBuffAddress.output was required