From 76323a07d04925e2448a22836cad14160e367a14 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 5 May 2014 21:19:09 +1000 Subject: [PATCH] Transaction: restrict TxIn/TxOut constructor params To keep this change minimal, both TxIn/TxOut still use the parameter object for initialization. TxOut accepts only the types it uses internally, and not hex or byte arrays for scripts. The clone is unnecessary as a TransactionOut is never mutated after its creation. This resulted in TransactionOut.scriptPubKey no longer being needed, and was removed. To access the scriptPubKey as a byte buffer, a user can simply use: TransactionOut.script.toBuffer() Unfortunately, this leaves TransactionOut in a sorry state of test. Something that needs to be fixed. --- src/transaction.js | 39 +++++++++------------------------------ test/transaction.js | 12 ------------ 2 files changed, 9 insertions(+), 42 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index 72f09fc..64bb41a 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -458,14 +458,8 @@ var TransactionIn = function (data) { this.outpoint = { hash: data.hash, index: data.index } } - if (data.scriptSig) { - this.script = Script.fromScriptSig(data.scriptSig) - } else if (data.script) { - this.script = data.script - } else { - this.script = new Script(data.script) - } - + assert(data.script, 'Invalid TxIn parameters') + this.script = data.script this.sequence = data.sequence || this.defaultSequence } @@ -480,38 +474,23 @@ TransactionIn.prototype.clone = function () { }) } -// FIXME: Support for alternate networks -var TransactionOut = function (data) { - this.script = - data.script instanceof Script ? data.script.clone() - : Array.isArray(data.script) ? new Script(data.script) - : typeof data.script == "string" ? new Script(convert.hexToBytes(data.script)) - : data.scriptPubKey ? Script.fromScriptSig(data.scriptPubKey) - : data.address ? Script.createOutputScript(data.address) - : new Script() +function TransactionOut(data) { + this.script = data.script + this.value = data.value + this.address = data.address var network = data.network || Network.bitcoin if (this.script.buffer.length > 0) { this.address = this.script.getToAddress(network) } - - this.value = - Array.isArray(data.value) ? convert.bytesToNum(data.value) - : "string" == typeof data.value ? parseInt(data.value) - : data.value instanceof BigInteger ? parseInt(data.value.toString()) - : data.value } TransactionOut.prototype.clone = function() { - var newTxout = new TransactionOut({ + return new TransactionOut({ script: this.script.clone(), - value: this.value + value: this.value, + address: this.address }) - return newTxout -} - -TransactionOut.prototype.scriptPubKey = function() { - return convert.bytesToHex(this.script.buffer) } module.exports = { diff --git a/test/transaction.js b/test/transaction.js index e672411..234d925 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -279,18 +279,6 @@ describe('Transaction', function() { }) describe('TransactionOut', function() { - describe('scriptPubKey', function() { - it('returns hex string', function() { - var address = Address.fromBase58Check("1AZpKpcfCzKDUeTFBQUL4MokQai3m3HMXv") - - var txOut = new TransactionOut({ - value: 50000, - script: Script.createOutputScript(address) - }) - - assert.equal(txOut.scriptPubKey(), "76a91468edf28474ee22f68dfe7e56e76c017c1701b84f88ac") - }) - }) }) })