diff --git a/lib/services/address/index.js b/lib/services/address/index.js index fa315217..4477e1ba 100644 --- a/lib/services/address/index.js +++ b/lib/services/address/index.js @@ -1309,7 +1309,8 @@ AddressService.prototype.isSpent = function(output, options, callback) { var txid = output.prevTxId ? output.prevTxId.toString('hex') : output.txid; var spent = self.node.services.bitcoind.isSpent(txid, output.outputIndex); if (!spent && queryMempool) { - var spentIndexSyncKey = this._encodeSpentIndexSyncKey(output.prevTxId, output.outputIndex); + var txidBuffer = new Buffer(txid, 'hex'); + var spentIndexSyncKey = this._encodeSpentIndexSyncKey(txidBuffer, output.outputIndex); spent = self.mempoolSpentIndex[spentIndexSyncKey] ? true : false; } setImmediate(function() { diff --git a/test/services/address/index.unit.js b/test/services/address/index.unit.js index c3bdcdee..d69fa63d 100644 --- a/test/services/address/index.unit.js +++ b/test/services/address/index.unit.js @@ -1545,16 +1545,47 @@ describe('Address Service', function() { } } }; - it('should give true if bitcoind.isSpent gives true', function(done) { + it('should give true if bitcoind.isSpent gives true (with output info)', function(done) { var am = new AddressService({ mempoolMemoryIndex: true, node: testnode }); + var isSpent = sinon.stub().returns(true); am.node.services.bitcoind = { - isSpent: sinon.stub().returns(true), + isSpent: isSpent, on: sinon.stub() }; - am.isSpent({}, {}, function(spent) { + var output = { + txid: '4228d3f41051f914f71a1dcbbe4098e29a07cc2672fdadab0763d88ffd6ffa57', + outputIndex: 3 + }; + am.isSpent(output, {}, function(spent) { + isSpent.callCount.should.equal(1); + isSpent.args[0][0].should.equal(output.txid); + isSpent.args[0][1].should.equal(output.outputIndex); + spent.should.equal(true); + done(); + }); + }); + it('should give true if bitcoind.isSpent gives true (with input)', function(done) { + var am = new AddressService({ + mempoolMemoryIndex: true, + node: testnode + }); + var isSpent = sinon.stub().returns(true); + am.node.services.bitcoind = { + isSpent: isSpent, + on: sinon.stub() + }; + var txid = '4228d3f41051f914f71a1dcbbe4098e29a07cc2672fdadab0763d88ffd6ffa57'; + var output = { + prevTxId: new Buffer(txid, 'hex'), + outputIndex: 4 + }; + am.isSpent(output, {}, function(spent) { + isSpent.callCount.should.equal(1); + isSpent.args[0][0].should.equal(txid); + isSpent.args[0][1].should.equal(output.outputIndex); spent.should.equal(true); done(); });