Skip to content

Commit 5af87c0

Browse files
committed
privatise internals for TxBuilder
1 parent 93b815c commit 5af87c0

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,36 +468,36 @@ 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
}
480480

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

484484
// if any signatures exist, throw
485-
if (this.inputs.some(function (input) {
485+
if (this.__inputs.some(function (input) {
486486
if (!input.signatures) return false
487487

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

493-
this.tx.locktime = locktime
493+
this.__tx.locktime = locktime
494494
}
495495

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

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

503503
TransactionBuilder.fromTransaction = function (transaction, network) {
@@ -522,7 +522,7 @@ TransactionBuilder.fromTransaction = function (transaction, network) {
522522
})
523523

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

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

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

568568
var input = {}
569569

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

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

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

615-
return this.tx.addOutput(scriptPubKey, value)
615+
return this.__tx.addOutput(scriptPubKey, value)
616616
}
617617

618618
TransactionBuilder.prototype.build = function () {
@@ -624,13 +624,13 @@ TransactionBuilder.prototype.buildIncomplete = function () {
624624

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

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

678-
var input = this.inputs[vin]
678+
var input = this.__inputs[vin]
679679

680680
// if redeemScript was previously provided, enforce consistency
681681
if (input.redeemScript !== undefined &&
@@ -699,9 +699,9 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
699699
// ready to sign
700700
var signatureHash
701701
if (input.witness) {
702-
signatureHash = this.tx.hashForWitnessV0(vin, input.signScript, input.value, hashType)
702+
signatureHash = this.__tx.hashForWitnessV0(vin, input.signScript, input.value, hashType)
703703
} else {
704-
signatureHash = this.tx.hashForSignature(vin, input.signScript, hashType)
704+
signatureHash = this.__tx.hashForSignature(vin, input.signScript, hashType)
705705
}
706706

707707
// enforce in order signing of public keys
@@ -730,7 +730,7 @@ function signatureHashType (buffer) {
730730
}
731731

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

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

748748
TransactionBuilder.prototype.__canModifyOutputs = function () {
749-
var nInputs = this.tx.ins.length
750-
var nOutputs = this.tx.outs.length
749+
var nInputs = this.__tx.ins.length
750+
var nOutputs = this.__tx.outs.length
751751

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

755755
return input.signatures.every(function (signature) {
@@ -770,11 +770,11 @@ TransactionBuilder.prototype.__canModifyOutputs = function () {
770770

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

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

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
})
@@ -504,10 +505,10 @@ describe('TransactionBuilder', function () {
504505
'194a565cd6aa4cc38b8eaffa343402201c5b4b61d73fa38e49c1ee68cc0e6dfd2f5dae453dd86eb142e87a' +
505506
'0bafb1bc8401210283409659355b6d1cc3c32decd5d561abaac86c37a353b52895a5e6c196d6f44800000000'
506507
var txb = TransactionBuilder.fromTransaction(Transaction.fromHex(rawtx))
507-
txb.inputs[0].value = 241530
508-
txb.inputs[1].value = 241530
509-
txb.inputs[2].value = 248920
510-
txb.inputs[3].value = 248920
508+
txb.__inputs[0].value = 241530
509+
txb.__inputs[1].value = 241530
510+
txb.__inputs[2].value = 248920
511+
txb.__inputs[3].value = 248920
511512

512513
assert.throws(function () {
513514
txb.build()
@@ -530,7 +531,7 @@ describe('TransactionBuilder', function () {
530531
var txHex = tx.toHex()
531532
var newTxb = TransactionBuilder.fromTransaction(Transaction.fromHex(txHex))
532533
// input should have the key 'witness' set to true
533-
assert.equal(newTxb.inputs[0].witness, true)
534+
assert.equal(newTxb.__inputs[0].witness, true)
534535
})
535536

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

0 commit comments

Comments
 (0)