From f7d4895b7463c0e4867a2ba59931b4ff24ee9d92 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Tue, 25 Mar 2014 13:22:54 +0800 Subject: [PATCH] add async version of setUnspentOutputs --- src/wallet.js | 10 ++++++++++ test/wallet.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/wallet.js b/src/wallet.js index 0021727..9e02c6a 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -83,6 +83,16 @@ var Wallet = function (seed, options) { this.outputs = outputs } + this.setUnspentOutputsAsync = function(utxo, callback) { + try { + this.setUnspentOutputs(utxo) + } catch(err) { + return callback(err) + } + + return callback() + } + function outputToUnspentOutput(output){ var hashAndIndex = output.receive.split(":") diff --git a/test/wallet.js b/test/wallet.js index 6f8d558..8966dd5 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -273,6 +273,37 @@ describe('Wallet', function() { assert.equal(output.address, utxo[0].address) } }) + + describe('setUnspentOutputsAsync', function(){ + var utxo; + beforeEach(function(){ + utxo = cloneObject([expectedUtxo]) + }) + + afterEach(function(){ + wallet.setUnspentOutputs.restore() + }) + + it('calls setUnspentOutputs', function(){ + sinon.stub(wallet, "setUnspentOutputs") + + var callback = sinon.spy() + var tx = wallet.setUnspentOutputsAsync(utxo, callback) + + assert(wallet.setUnspentOutputs.calledWith(utxo)) + assert(callback.called) + }) + + it('when setUnspentOutputs throws an error, it invokes callback with error', function(){ + sinon.stub(wallet, "setUnspentOutputs").throws() + + var callback = sinon.spy() + var tx = wallet.setUnspentOutputsAsync(utxo, callback) + + assert(callback.called) + assert(callback.args[0][0] instanceof Error) + }) + }) }) describe('processTx', function(){