Browse Source

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.
hk-custom-address
Daniel Cousens 11 years ago
parent
commit
76323a07d0
  1. 39
      src/transaction.js
  2. 12
      test/transaction.js

39
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 = {

12
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")
})
})
})
})

Loading…
Cancel
Save