From 913b48e87fede1f41948cfd295952f5aa05daf4b Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Mon, 24 Mar 2014 01:29:10 +0800 Subject: [PATCH] sign tx --- package.json | 3 ++- src/wallet.js | 3 ++- test/wallet.js | 37 ++++++++++++++++++++++++++++--------- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 36d1f36..159e326 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "mocha": "1.18.2", "istanbul": "0.1.30", "uglify-js": "2.4.13", - "node-browserify": "https://github.com/substack/node-browserify/tarball/master" + "node-browserify": "https://github.com/substack/node-browserify/tarball/master", + "sinon": "^1.9.0" }, "testling": { "browsers": [ diff --git a/src/wallet.js b/src/wallet.js index e3180cc..87df650 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -201,7 +201,8 @@ var Wallet = function (seed, options) { break; } - // TODO: sign tx + this.sign(tx) + return tx } diff --git a/test/wallet.js b/test/wallet.js index 841d1cf..dbee547 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -13,6 +13,8 @@ var fixtureTxes = require('./fixtures/mainnet_tx') var fixtureTx1Hex = fixtureTxes.prevTx var fixtureTx2Hex = fixtureTxes.tx +var sinon = require('sinon') + describe('Wallet', function() { var seed, wallet; beforeEach(function(){ @@ -318,37 +320,38 @@ describe('Wallet', function() { }) describe('createTx', function(){ - var to, value, prevTx; + var to, value; + var address1, address2; beforeEach(function(){ to = '15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3' value = 500000 // generate 2 addresses - wallet.generateAddress() - wallet.generateAddress() + address1 = wallet.generateAddress() + address2 = wallet.generateAddress() // set up 3 utxo utxo = [ { "hash": fakeTxHash(1), "outputIndex": 0, - "scriptPubKey": scriptPubKeyFor(wallet.addresses[0], 300000), - "address" : wallet.addresses[0], + "scriptPubKey": scriptPubKeyFor(address1, 300000), + "address" : address1, "value": 400000 // not enough for value }, { "hash": fakeTxHash(2), "outputIndex": 1, - "scriptPubKey": scriptPubKeyFor(wallet.addresses[0], 500000), - "address" : wallet.addresses[0], + "scriptPubKey": scriptPubKeyFor(address1, 500000), + "address" : address1, "value": 500000 // enough for only value }, { "hash": fakeTxHash(3), "outputIndex": 0, - "scriptPubKey": scriptPubKeyFor(wallet.addresses[1], 520000), - "address" : wallet.addresses[1], + "scriptPubKey": scriptPubKeyFor(address2, 520000), + "address" : address2, "value": 520000 // enough for value and fee } ] @@ -422,6 +425,22 @@ describe('Wallet', function() { }) }) }) + + describe('signing', function(){ + afterEach(function(){ + Transaction.prototype.sign.restore() + }) + + it('signes the inputs with respective keys', function(){ + var fee = 30000 + sinon.stub(Transaction.prototype, "sign") + + var tx = wallet.createTx(to, value, fee) + + assert(Transaction.prototype.sign.calledWith(0, wallet.getPrivateKeyForAddress(address2))) + assert(Transaction.prototype.sign.calledWith(1, wallet.getPrivateKeyForAddress(address1))) + }) + }) }) function assertEqual(obj1, obj2){