Wallet: use TxBuilder instead
This commit is contained in:
parent
4e3a6c9557
commit
26b028adcf
2 changed files with 16 additions and 18 deletions
|
@ -4,7 +4,7 @@ var networks = require('./networks')
|
|||
|
||||
var Address = require('./address')
|
||||
var HDNode = require('./hdnode')
|
||||
var Transaction = require('./transaction')
|
||||
var TransactionBuilder = require('./transaction_builder')
|
||||
var Script = require('./script')
|
||||
|
||||
function Wallet(seed, network, unspents) {
|
||||
|
@ -57,17 +57,17 @@ Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) {
|
|||
var subTotal = value
|
||||
var addresses = []
|
||||
|
||||
var tx = new Transaction()
|
||||
tx.addOutput(to, value)
|
||||
var txb = new TransactionBuilder()
|
||||
txb.addOutput(to, value)
|
||||
|
||||
for (var i = 0; i < utxos.length; ++i) {
|
||||
var utxo = utxos[i]
|
||||
addresses.push(utxo.address)
|
||||
|
||||
var outpoint = utxo.from.split(':')
|
||||
tx.addInput(outpoint[0], parseInt(outpoint[1]))
|
||||
txb.addInput(outpoint[0], parseInt(outpoint[1]))
|
||||
|
||||
var fee = fixedFee === undefined ? estimatePaddedFee(tx, this.network) : fixedFee
|
||||
var fee = fixedFee === undefined ? estimatePaddedFee(txb.buildIncomplete(), this.network) : fixedFee
|
||||
|
||||
accum += utxo.value
|
||||
subTotal = value + fee
|
||||
|
@ -75,7 +75,7 @@ Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) {
|
|||
var change = accum - subTotal
|
||||
|
||||
if (change > this.network.dustThreshold) {
|
||||
tx.addOutput(changeAddress || this.getChangeAddress(), change)
|
||||
txb.addOutput(changeAddress || this.getChangeAddress(), change)
|
||||
}
|
||||
|
||||
break
|
||||
|
@ -84,8 +84,7 @@ Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) {
|
|||
|
||||
assert(accum >= subTotal, 'Not enough funds (incl. fee): ' + accum + ' < ' + subTotal)
|
||||
|
||||
this.signWith(tx, addresses)
|
||||
return tx
|
||||
return this.signWith(txb, addresses).build()
|
||||
}
|
||||
|
||||
Wallet.prototype.processPendingTx = function(tx){
|
||||
|
@ -219,16 +218,14 @@ Wallet.prototype.setUnspentOutputs = function(utxo) {
|
|||
this.outputs = processUnspentOutputs(utxo)
|
||||
}
|
||||
|
||||
Wallet.prototype.signWith = function(tx, addresses) {
|
||||
assert.equal(tx.ins.length, addresses.length, 'Number of addresses must match number of transaction inputs')
|
||||
|
||||
Wallet.prototype.signWith = function(txb, addresses) {
|
||||
addresses.forEach(function(address, i) {
|
||||
var key = this.getPrivateKeyForAddress(address)
|
||||
var privKey = this.getPrivateKeyForAddress(address)
|
||||
|
||||
tx.sign(i, key)
|
||||
txb.sign(i, privKey)
|
||||
}, this)
|
||||
|
||||
return tx
|
||||
return txb
|
||||
}
|
||||
|
||||
function outputToUnspentOutput(output){
|
||||
|
|
|
@ -7,6 +7,7 @@ var scripts = require('../src/scripts')
|
|||
var Address = require('../src/address')
|
||||
var HDNode = require('../src/hdnode')
|
||||
var Transaction = require('../src/transaction')
|
||||
var TransactionBuilder = require('../src/transaction_builder')
|
||||
var Wallet = require('../src/wallet')
|
||||
|
||||
var fixtureTxes = require('./fixtures/mainnet_tx')
|
||||
|
@ -623,17 +624,17 @@ describe('Wallet', function() {
|
|||
|
||||
describe('signing', function(){
|
||||
afterEach(function(){
|
||||
Transaction.prototype.sign.restore()
|
||||
TransactionBuilder.prototype.sign.restore()
|
||||
})
|
||||
|
||||
it('signes the inputs with respective keys', function(){
|
||||
var fee = 30000
|
||||
sinon.stub(Transaction.prototype, "sign")
|
||||
sinon.spy(TransactionBuilder.prototype, "sign")
|
||||
|
||||
var tx = wallet.createTx(to, value, fee)
|
||||
|
||||
assert(Transaction.prototype.sign.calledWith(0, wallet.getPrivateKeyForAddress(address2)))
|
||||
assert(Transaction.prototype.sign.calledWith(1, wallet.getPrivateKeyForAddress(address1)))
|
||||
assert(TransactionBuilder.prototype.sign.calledWith(0, wallet.getPrivateKeyForAddress(address2)))
|
||||
assert(TransactionBuilder.prototype.sign.calledWith(1, wallet.getPrivateKeyForAddress(address1)))
|
||||
})
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue