diff --git a/latest/spend_p2sh_p2wpkh.js b/latest/spend_p2sh_p2wpkh.js index ad8745f..572e5e0 100644 --- a/latest/spend_p2sh_p2wpkh.js +++ b/latest/spend_p2sh_p2wpkh.js @@ -37,6 +37,7 @@ txb.addOutput(myaddress, txOut.value - 5000) var unsigned = txb.buildIncomplete() var signer = new TxSigner(unsigned) + signer.sign(0, root.keyPair, { scriptPubKey: txOut.script, redeemScript: toSegwitPubkey, diff --git a/src/transaction_signer.js b/src/transaction_signer.js index 172884a..0d61131 100644 --- a/src/transaction_signer.js +++ b/src/transaction_signer.js @@ -110,6 +110,9 @@ function InSigner (tx, nIn, opts) { if ((tx instanceof Transaction) === false) { throw new Error('A transaction is required for InSigner') } + if (tx.ins[nIn] === undefined) { + throw new Error('No transaction input at this index') + } if (opts.scriptPubKey === undefined) { throw new Error('A value for scriptPubKey is required') } @@ -440,6 +443,12 @@ function TxSigner (tx) { this.states = [] } +TxSigner.prototype.signer = function (nIn, opts) { + if (this.states[nIn] === undefined) { + this.states[nIn] = new InSigner(this.tx, nIn, opts) + } + return this.states[nIn] +} /** * Sign a transaction. * @@ -456,11 +465,8 @@ TxSigner.prototype.sign = function (nIn, key, opts, sigHashType) { } // You can probably make this work with the current library, if you can work out the witnessScript above! // generate opts for the internal signer based off older way of positional arguments to TxSigner.sign - if (this.states[nIn] === undefined) { - this.states[nIn] = new InSigner(this.tx, nIn, opts) - } - if (!this.states[nIn].sign(key, sigHashType)) { + if (!this.signer(nIn, opts).sign(key, sigHashType)) { throw new Error('Unsignable input: ', nIn) }