From 68b08b638a9eccd7eaae66f1a51f637be0dada31 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Tue, 25 Mar 2014 13:39:27 +0800 Subject: [PATCH] add async version of createTx --- src/wallet.js | 16 ++++++++++++++++ test/wallet.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/wallet.js b/src/wallet.js index 9e02c6a..ef1405a 100644 --- a/src/wallet.js +++ b/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 diff --git a/test/wallet.js b/test/wallet.js index 8966dd5..f19bc3b 100644 --- a/test/wallet.js +++ b/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()) }