Skip to content

Commit 080c727

Browse files
author
Alex Melville
committed
add check for 0 unspents in createTransaction
Summary: It is clearer if we throw an Error here early on in the createTransaction function because then we won't try to build a transaction using 0 unspents (which results in a transaction with 0 inputs!) Reviewers: ben Reviewed By: ben Subscribers: ben Differential Revision: https://phabricator.bitgo.com/D6193
1 parent 1d2adc2 commit 080c727

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitgo",
3-
"version": "3.5.1",
3+
"version": "3.5.2",
44
"description": "BitGo Javascript SDK",
55
"main": "./src/index.js",
66
"keywords": [

src/transactionBuilder.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ exports.createTransaction = function(params) {
286286
return confirms >= minConfirms;
287287
});
288288

289+
// abort early if there's no viable unspents, because it won't be possible to create the txn later
290+
if (unspents.length === 0) {
291+
throw Error('0 unspents available for transaction creation');
292+
}
293+
289294
// create array of unconfirmed unspent ID strings of the form "txHash:outputIndex"
290295
zeroConfUnspentTxIds = _(results.unspents).filter(function(u) {
291296
return !u.confirmations;

test/wallet.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2261,11 +2261,22 @@ describe('Wallet API', function() {
22612261
err.message.should.include("invalid bitcoin address");
22622262
return wallet1.sendMany({ recipients: [{ address: TestBitGo.TEST_WALLET2_ADDRESS, amount: -100 }], walletPassphrase: TestBitGo.TEST_WALLET1_PASSCODE });
22632263
})
2264-
.then(function(result) {
2264+
.then(function() {
22652265
throw new Error("Unexpected result - expected to catch bad amount");
22662266
})
22672267
.catch(function(err) {
22682268
err.message.should.include("invalid amount");
2269+
// use a ridiculously high number for the minConfirms so that no viable unspents are returned
2270+
return bitgo.unlock({ otp: '0000000' });
2271+
})
2272+
.then(function() {
2273+
return wallet1.sendMany({ recipients: [{ address: TestBitGo.TEST_WALLET2_ADDRESS, amount: 10000 }], walletPassphrase: TestBitGo.TEST_WALLET1_PASSCODE, enforceMinConfirmsForChange: true, minConfirms: 999999999 });
2274+
})
2275+
.then(function() {
2276+
throw new Error("Unexpected result - expected to catch 0 unspents");
2277+
})
2278+
.catch(function(err) {
2279+
err.message.should.include('0 unspents available for transaction creation');
22692280
});
22702281
});
22712282

0 commit comments

Comments
 (0)