Browse Source

add async version of createTx

hk-custom-address
Wei Lu 11 years ago
parent
commit
68b08b638a
  1. 16
      src/wallet.js
  2. 44
      test/wallet.js

16
src/wallet.js

@ -200,6 +200,22 @@ var Wallet = function (seed, options) {
return tx
}
this.createTxAsync = function(to, value, fixedFee, callback){
if(fixedFee instanceof Function) {
callback = fixedFee
fixedFee = undefined
}
var tx = null
try {
tx = this.createTx(to, value, fixedFee)
} catch(err) {
return callback(err)
}
callback(null, tx)
}
this.dustThreshold = 5430
function isDust(amount) {
return amount <= me.dustThreshold

44
test/wallet.js

@ -536,6 +536,50 @@ describe('Wallet', function() {
}
})
describe('createTxAsync', function(){
var to, value, fee;
beforeEach(function(){
to = '15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3'
value = 500000
fee = 10000
})
afterEach(function(){
wallet.createTx.restore()
})
it('calls createTx', function(){
sinon.stub(wallet, "createTx").returns("fakeTx")
var callback = sinon.spy()
var tx = wallet.createTxAsync(to, value, callback)
assert(wallet.createTx.calledWith(to, value))
assert(callback.calledWith(null, "fakeTx"))
})
it('calls createTx correctly when fee is specified', function(){
sinon.stub(wallet, "createTx").returns("fakeTx")
var callback = sinon.spy()
var tx = wallet.createTxAsync(to, value, fee, callback)
assert(wallet.createTx.calledWith(to, value, fee))
assert(callback.calledWith(null, "fakeTx"))
})
it('when createTx throws an error, it invokes callback with error', function(){
sinon.stub(wallet, "createTx").throws()
var callback = sinon.spy()
var tx = wallet.createTxAsync(to, value, callback)
assert(callback.called)
assert(callback.args[0][0] instanceof Error)
})
})
function assertEqual(obj1, obj2){
assert.equal(obj1.toString(), obj2.toString())
}

Loading…
Cancel
Save