Wallet: use TxBuilder instead

This commit is contained in:
Daniel Cousens 2014-07-16 22:43:40 +10:00
parent 4e3a6c9557
commit 26b028adcf
2 changed files with 16 additions and 18 deletions

View file

@ -4,7 +4,7 @@ var networks = require('./networks')
var Address = require('./address') var Address = require('./address')
var HDNode = require('./hdnode') var HDNode = require('./hdnode')
var Transaction = require('./transaction') var TransactionBuilder = require('./transaction_builder')
var Script = require('./script') var Script = require('./script')
function Wallet(seed, network, unspents) { function Wallet(seed, network, unspents) {
@ -57,17 +57,17 @@ Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) {
var subTotal = value var subTotal = value
var addresses = [] var addresses = []
var tx = new Transaction() var txb = new TransactionBuilder()
tx.addOutput(to, value) txb.addOutput(to, value)
for (var i = 0; i < utxos.length; ++i) { for (var i = 0; i < utxos.length; ++i) {
var utxo = utxos[i] var utxo = utxos[i]
addresses.push(utxo.address) addresses.push(utxo.address)
var outpoint = utxo.from.split(':') 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 accum += utxo.value
subTotal = value + fee subTotal = value + fee
@ -75,7 +75,7 @@ Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) {
var change = accum - subTotal var change = accum - subTotal
if (change > this.network.dustThreshold) { if (change > this.network.dustThreshold) {
tx.addOutput(changeAddress || this.getChangeAddress(), change) txb.addOutput(changeAddress || this.getChangeAddress(), change)
} }
break break
@ -84,8 +84,7 @@ Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) {
assert(accum >= subTotal, 'Not enough funds (incl. fee): ' + accum + ' < ' + subTotal) assert(accum >= subTotal, 'Not enough funds (incl. fee): ' + accum + ' < ' + subTotal)
this.signWith(tx, addresses) return this.signWith(txb, addresses).build()
return tx
} }
Wallet.prototype.processPendingTx = function(tx){ Wallet.prototype.processPendingTx = function(tx){
@ -219,16 +218,14 @@ Wallet.prototype.setUnspentOutputs = function(utxo) {
this.outputs = processUnspentOutputs(utxo) this.outputs = processUnspentOutputs(utxo)
} }
Wallet.prototype.signWith = function(tx, addresses) { Wallet.prototype.signWith = function(txb, addresses) {
assert.equal(tx.ins.length, addresses.length, 'Number of addresses must match number of transaction inputs')
addresses.forEach(function(address, i) { addresses.forEach(function(address, i) {
var key = this.getPrivateKeyForAddress(address) var privKey = this.getPrivateKeyForAddress(address)
tx.sign(i, key) txb.sign(i, privKey)
}, this) }, this)
return tx return txb
} }
function outputToUnspentOutput(output){ function outputToUnspentOutput(output){

View file

@ -7,6 +7,7 @@ var scripts = require('../src/scripts')
var Address = require('../src/address') var Address = require('../src/address')
var HDNode = require('../src/hdnode') var HDNode = require('../src/hdnode')
var Transaction = require('../src/transaction') var Transaction = require('../src/transaction')
var TransactionBuilder = require('../src/transaction_builder')
var Wallet = require('../src/wallet') var Wallet = require('../src/wallet')
var fixtureTxes = require('./fixtures/mainnet_tx') var fixtureTxes = require('./fixtures/mainnet_tx')
@ -623,17 +624,17 @@ describe('Wallet', function() {
describe('signing', function(){ describe('signing', function(){
afterEach(function(){ afterEach(function(){
Transaction.prototype.sign.restore() TransactionBuilder.prototype.sign.restore()
}) })
it('signes the inputs with respective keys', function(){ it('signes the inputs with respective keys', function(){
var fee = 30000 var fee = 30000
sinon.stub(Transaction.prototype, "sign") sinon.spy(TransactionBuilder.prototype, "sign")
var tx = wallet.createTx(to, value, fee) var tx = wallet.createTx(to, value, fee)
assert(Transaction.prototype.sign.calledWith(0, wallet.getPrivateKeyForAddress(address2))) assert(TransactionBuilder.prototype.sign.calledWith(0, wallet.getPrivateKeyForAddress(address2)))
assert(Transaction.prototype.sign.calledWith(1, wallet.getPrivateKeyForAddress(address1))) assert(TransactionBuilder.prototype.sign.calledWith(1, wallet.getPrivateKeyForAddress(address1)))
}) })
}) })