Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #152 from isocolsky/fix/build-tx-from-pk
Browse files Browse the repository at this point in the history
Check funds before building tx from private key
  • Loading branch information
matiu committed Oct 7, 2015
2 parents b263ebe + 73c8e0f commit 2214f21
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ API.prototype.buildTxFromPrivateKey = function(privateKey, destinationAddress, o

var fee = opts.fee || 10000;
var amount = _.sum(utxos, 'satoshis') - fee;
if (amount <= 0) return next(Errors.INSUFFICIENT_FUNDS);

var tx;
try {
Expand All @@ -473,7 +474,8 @@ API.prototype.buildTxFromPrivateKey = function(privateKey, destinationAddress, o
tx.serialize();

} catch (ex) {
return next(new Error('Could not build transaction', ex));
log.error('Could not build transaction from private key', ex);
return next(Errors.COULD_NOT_BUILD_TRANSACTION);
}
return next(null, tx);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/errors/errordefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var errors = {
MISSING_PRIVATE_KEY: 'Missing private keys to sign',
ENCRYPTED_PRIVATE_KEY: 'Private key is encrypted, cannot sign',
SERVER_COMPROMISED: 'Server response could not be verified',
COULD_NOT_BUILD_TRANSACTION: 'Could not build transaction',
INSUFFICIENT_FUNDS: 'Insufficient funds',
};

var errorObjects = _.zipObject(_.map(errors, function(msg, code) {
Expand Down
16 changes: 16 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3043,5 +3043,21 @@ describe('client API', function() {
});
});
});
it('should fail to build tx for single private key if insufficient funds', function(done) {
var address = {
address: '1PuKMvRFfwbLXyEPXZzkGi111gMUCs6uE3',
type: 'P2PKH',
};
helpers.createAndJoinWallet(clients, 1, 1, function() {
blockchainExplorerMock.setUtxo(address, 123 / 1e8, 1);
clients[0].buildTxFromPrivateKey('5KjBgBiadWGhjWmLN1v4kcEZqWSZFqzgv7cSUuZNJg4tD82c4xp', '1GG3JQikGC7wxstyavUBDoCJ66bWLLENZC', {
fee: 500
}, function(err, tx) {
should.exist(err);
err.code.should.equal('INSUFFICIENT_FUNDS');
done();
});
});
});
});
});

0 comments on commit 2214f21

Please sign in to comment.