Skip to content

Commit 09c5ff1

Browse files
authored
Merge pull request bitcoinjs#1038 from bitcoinjs/__tx
Privatise TransactionBuilder internals
2 parents 6b3c41a + 024c541 commit 09c5ff1

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

src/transaction_builder.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -468,37 +468,37 @@ function buildInput (input, allowIncomplete) {
468468
}
469469

470470
function TransactionBuilder (network, maximumFeeRate) {
471-
this.prevTxMap = {}
471+
this.__prevTxSet = {}
472472
this.network = network || networks.bitcoin
473473

474474
// WARNING: This is __NOT__ to be relied on, its just another potential safety mechanism (safety in-depth)
475475
this.maximumFeeRate = maximumFeeRate || 2500
476476

477-
this.inputs = []
478-
this.tx = new Transaction()
477+
this.__inputs = []
478+
this.__tx = new Transaction()
479479
this.tx.version = 2
480480
}
481481

482482
TransactionBuilder.prototype.setLockTime = function (locktime) {
483483
typeforce(types.UInt32, locktime)
484484

485485
// if any signatures exist, throw
486-
if (this.inputs.some(function (input) {
486+
if (this.__inputs.some(function (input) {
487487
if (!input.signatures) return false
488488

489489
return input.signatures.some(function (s) { return s })
490490
})) {
491491
throw new Error('No, this would invalidate signatures')
492492
}
493493

494-
this.tx.locktime = locktime
494+
this.__tx.locktime = locktime
495495
}
496496

497497
TransactionBuilder.prototype.setVersion = function (version) {
498498
typeforce(types.UInt32, version)
499499

500500
// XXX: this might eventually become more complex depending on what the versions represent
501-
this.tx.version = version
501+
this.__tx.version = version
502502
}
503503

504504
TransactionBuilder.fromTransaction = function (transaction, network) {
@@ -523,7 +523,7 @@ TransactionBuilder.fromTransaction = function (transaction, network) {
523523
})
524524

525525
// fix some things not possible through the public API
526-
txb.inputs.forEach(function (input, i) {
526+
txb.__inputs.forEach(function (input, i) {
527527
fixMultisigOrder(input, transaction, i)
528528
})
529529

@@ -564,7 +564,7 @@ TransactionBuilder.prototype.__addInputUnsafe = function (txHash, vout, options)
564564
}
565565

566566
var prevTxOut = txHash.toString('hex') + ':' + vout
567-
if (this.prevTxMap[prevTxOut] !== undefined) throw new Error('Duplicate TxOut: ' + prevTxOut)
567+
if (this.__prevTxSet[prevTxOut] !== undefined) throw new Error('Duplicate TxOut: ' + prevTxOut)
568568

569569
var input = {}
570570

@@ -597,9 +597,9 @@ TransactionBuilder.prototype.__addInputUnsafe = function (txHash, vout, options)
597597
input.prevOutType = prevOutType || btemplates.classifyOutput(options.prevOutScript)
598598
}
599599

600-
var vin = this.tx.addInput(txHash, vout, options.sequence, options.scriptSig)
601-
this.inputs[vin] = input
602-
this.prevTxMap[prevTxOut] = vin
600+
var vin = this.__tx.addInput(txHash, vout, options.sequence, options.scriptSig)
601+
this.__inputs[vin] = input
602+
this.__prevTxSet[prevTxOut] = true
603603
return vin
604604
}
605605

@@ -613,7 +613,7 @@ TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) {
613613
scriptPubKey = baddress.toOutputScript(scriptPubKey, this.network)
614614
}
615615

616-
return this.tx.addOutput(scriptPubKey, value)
616+
return this.__tx.addOutput(scriptPubKey, value)
617617
}
618618

619619
TransactionBuilder.prototype.build = function () {
@@ -625,13 +625,13 @@ TransactionBuilder.prototype.buildIncomplete = function () {
625625

626626
TransactionBuilder.prototype.__build = function (allowIncomplete) {
627627
if (!allowIncomplete) {
628-
if (!this.tx.ins.length) throw new Error('Transaction has no inputs')
629-
if (!this.tx.outs.length) throw new Error('Transaction has no outputs')
628+
if (!this.__tx.ins.length) throw new Error('Transaction has no inputs')
629+
if (!this.__tx.outs.length) throw new Error('Transaction has no outputs')
630630
}
631631

632-
var tx = this.tx.clone()
632+
var tx = this.__tx.clone()
633633
// Create script signatures from inputs
634-
this.inputs.forEach(function (input, i) {
634+
this.__inputs.forEach(function (input, i) {
635635
var scriptType = input.witnessScriptType || input.redeemScriptType || input.prevOutType
636636
if (!scriptType && !allowIncomplete) throw new Error('Transaction is not complete')
637637
var result = buildInput(input, allowIncomplete)
@@ -673,10 +673,10 @@ function canSign (input) {
673673
TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashType, witnessValue, witnessScript) {
674674
// TODO: remove keyPair.network matching in 4.0.0
675675
if (keyPair.network && keyPair.network !== this.network) throw new TypeError('Inconsistent network')
676-
if (!this.inputs[vin]) throw new Error('No input at index: ' + vin)
676+
if (!this.__inputs[vin]) throw new Error('No input at index: ' + vin)
677677
hashType = hashType || Transaction.SIGHASH_ALL
678678

679-
var input = this.inputs[vin]
679+
var input = this.__inputs[vin]
680680

681681
// if redeemScript was previously provided, enforce consistency
682682
if (input.redeemScript !== undefined &&
@@ -700,9 +700,9 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
700700
// ready to sign
701701
var signatureHash
702702
if (input.witness) {
703-
signatureHash = this.tx.hashForWitnessV0(vin, input.signScript, input.value, hashType)
703+
signatureHash = this.__tx.hashForWitnessV0(vin, input.signScript, input.value, hashType)
704704
} else {
705-
signatureHash = this.tx.hashForSignature(vin, input.signScript, hashType)
705+
signatureHash = this.__tx.hashForSignature(vin, input.signScript, hashType)
706706
}
707707

708708
// enforce in order signing of public keys
@@ -731,7 +731,7 @@ function signatureHashType (buffer) {
731731
}
732732

733733
TransactionBuilder.prototype.__canModifyInputs = function () {
734-
return this.inputs.every(function (input) {
734+
return this.__inputs.every(function (input) {
735735
// any signatures?
736736
if (input.signatures === undefined) return true
737737

@@ -747,10 +747,10 @@ TransactionBuilder.prototype.__canModifyInputs = function () {
747747
}
748748

749749
TransactionBuilder.prototype.__canModifyOutputs = function () {
750-
var nInputs = this.tx.ins.length
751-
var nOutputs = this.tx.outs.length
750+
var nInputs = this.__tx.ins.length
751+
var nOutputs = this.__tx.outs.length
752752

753-
return this.inputs.every(function (input) {
753+
return this.__inputs.every(function (input) {
754754
if (input.signatures === undefined) return true
755755

756756
return input.signatures.every(function (signature) {
@@ -771,11 +771,11 @@ TransactionBuilder.prototype.__canModifyOutputs = function () {
771771

772772
TransactionBuilder.prototype.__overMaximumFees = function (bytes) {
773773
// not all inputs will have .value defined
774-
var incoming = this.inputs.reduce(function (a, x) { return a + (x.value >>> 0) }, 0)
774+
var incoming = this.__inputs.reduce(function (a, x) { return a + (x.value >>> 0) }, 0)
775775

776776
// but all outputs do, and if we have any input value
777777
// we can immediately determine if the outputs are too small
778-
var outgoing = this.tx.outs.reduce(function (a, x) { return a + x.value }, 0)
778+
var outgoing = this.__tx.outs.reduce(function (a, x) { return a + x.value }, 0)
779779
var fee = incoming - outgoing
780780
var feeRate = fee / bytes
781781

test/transaction_builder.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ describe('TransactionBuilder', function () {
133133
})
134134
})
135135

136-
it('correctly classifies transaction inputs', function () {
136+
it('classifies transaction inputs', function () {
137137
var tx = Transaction.fromHex(fixtures.valid.classification.hex)
138138
var txb = TransactionBuilder.fromTransaction(tx)
139-
txb.inputs.forEach(function (i) {
139+
140+
txb.__inputs.forEach(function (i) {
140141
assert.strictEqual(i.prevOutType, 'scripthash')
141142
assert.strictEqual(i.redeemScriptType, 'multisig')
142143
assert.strictEqual(i.signType, 'multisig')
@@ -164,22 +165,22 @@ describe('TransactionBuilder', function () {
164165
var vin = txb.addInput(txHash, 1, 54)
165166
assert.strictEqual(vin, 0)
166167

167-
var txIn = txb.tx.ins[0]
168+
var txIn = txb.__tx.ins[0]
168169
assert.strictEqual(txIn.hash, txHash)
169170
assert.strictEqual(txIn.index, 1)
170171
assert.strictEqual(txIn.sequence, 54)
171-
assert.strictEqual(txb.inputs[0].prevOutScript, undefined)
172+
assert.strictEqual(txb.__inputs[0].prevOutScript, undefined)
172173
})
173174

174175
it('accepts a txHash, index [, sequence number and scriptPubKey]', function () {
175176
var vin = txb.addInput(txHash, 1, 54, scripts[1])
176177
assert.strictEqual(vin, 0)
177178

178-
var txIn = txb.tx.ins[0]
179+
var txIn = txb.__tx.ins[0]
179180
assert.strictEqual(txIn.hash, txHash)
180181
assert.strictEqual(txIn.index, 1)
181182
assert.strictEqual(txIn.sequence, 54)
182-
assert.strictEqual(txb.inputs[0].prevOutScript, scripts[1])
183+
assert.strictEqual(txb.__inputs[0].prevOutScript, scripts[1])
183184
})
184185

185186
it('accepts a prevTx, index [and sequence number]', function () {
@@ -190,11 +191,11 @@ describe('TransactionBuilder', function () {
190191
var vin = txb.addInput(prevTx, 1, 54)
191192
assert.strictEqual(vin, 0)
192193

193-
var txIn = txb.tx.ins[0]
194+
var txIn = txb.__tx.ins[0]
194195
assert.deepEqual(txIn.hash, prevTx.getHash())
195196
assert.strictEqual(txIn.index, 1)
196197
assert.strictEqual(txIn.sequence, 54)
197-
assert.strictEqual(txb.inputs[0].prevOutScript, scripts[1])
198+
assert.strictEqual(txb.__inputs[0].prevOutScript, scripts[1])
198199
})
199200

200201
it('returns the input index', function () {
@@ -222,7 +223,7 @@ describe('TransactionBuilder', function () {
222223
var vout = txb.addOutput(keyPair.getAddress(), 1000)
223224
assert.strictEqual(vout, 0)
224225

225-
var txout = txb.tx.outs[0]
226+
var txout = txb.__tx.outs[0]
226227
assert.deepEqual(txout.script, scripts[0])
227228
assert.strictEqual(txout.value, 1000)
228229
})
@@ -231,7 +232,7 @@ describe('TransactionBuilder', function () {
231232
var vout = txb.addOutput(scripts[0], 1000)
232233
assert.strictEqual(vout, 0)
233234

234-
var txout = txb.tx.outs[0]
235+
var txout = txb.__tx.outs[0]
235236
assert.deepEqual(txout.script, scripts[0])
236237
assert.strictEqual(txout.value, 1000)
237238
})
@@ -505,10 +506,10 @@ describe('TransactionBuilder', function () {
505506
'194a565cd6aa4cc38b8eaffa343402201c5b4b61d73fa38e49c1ee68cc0e6dfd2f5dae453dd86eb142e87a' +
506507
'0bafb1bc8401210283409659355b6d1cc3c32decd5d561abaac86c37a353b52895a5e6c196d6f44800000000'
507508
var txb = TransactionBuilder.fromTransaction(Transaction.fromHex(rawtx))
508-
txb.inputs[0].value = 241530
509-
txb.inputs[1].value = 241530
510-
txb.inputs[2].value = 248920
511-
txb.inputs[3].value = 248920
509+
txb.__inputs[0].value = 241530
510+
txb.__inputs[1].value = 241530
511+
txb.__inputs[2].value = 248920
512+
txb.__inputs[3].value = 248920
512513

513514
assert.throws(function () {
514515
txb.build()
@@ -532,7 +533,7 @@ describe('TransactionBuilder', function () {
532533
var txHex = tx.toHex()
533534
var newTxb = TransactionBuilder.fromTransaction(Transaction.fromHex(txHex))
534535
// input should have the key 'witness' set to true
535-
assert.equal(newTxb.inputs[0].witness, true)
536+
assert.equal(newTxb.__inputs[0].witness, true)
536537
})
537538

538539
it('should handle badly pre-filled OP_0s', function () {

0 commit comments

Comments
 (0)