diff --git a/src/wallet.js b/src/wallet.js index 5607c54..4748099 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -203,6 +203,8 @@ var Wallet = function (seed, options) { break; } + checkInsufficientFund(totalInValue, value, fee) + this.sign(tx) return tx @@ -214,6 +216,13 @@ var Wallet = function (seed, options) { } } + function checkInsufficientFund(totalInValue, value, fee) { + if(totalInValue < value + fee) { + throw new Error('Not enough money to send funds including transaction fee. Have: ' + + totalInValue + ', needed: ' + (value + fee)) + } + } + function estimateFeePadChangeOutput(tx){ var tmpTx = tx.clone() tmpTx.addOutput(getChangeAddress(), 0) diff --git a/test/wallet.js b/test/wallet.js index 97b6e66..4885931 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -448,6 +448,16 @@ describe('Wallet', function() { }) }) + describe('when there is not enough money', function(){ + it('throws an error', function(){ + var value = 1400001 + + assert.throws(function() { + wallet.createTx(to, value) + }, Error, 'Not enough money to send funds including transaction fee. Have: 1420000, needed: 1420001') + }) + }) + function fakeTxHash(i) { return "txtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtx" + i }