Skip to content

Commit 4cc9494

Browse files
author
Kevin Daniel
committed
refactor(sdk-api): replace bluebird with native promises
Ticket: DX-1130 TICKET: DX-1130
1 parent 32d7458 commit 4cc9494

File tree

14 files changed

+581
-226
lines changed

14 files changed

+581
-226
lines changed

modules/sdk-api/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"@bitgo/utxo-lib": "^11.2.4",
4848
"@types/superagent": "4.1.15",
4949
"bitcoinjs-message": "npm:@bitgo-forks/[email protected]",
50-
"bluebird": "^3.5.3",
5150
"debug": "3.1.0",
5251
"eol": "^0.5.0",
5352
"lodash": "^4.17.15",

modules/sdk-api/src/util.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,23 @@ export function getNetwork(network?: V1Network): utxolib.Network {
55
network = network || common.getNetwork();
66
return utxolib.networks[network];
77
}
8+
9+
/**
10+
* A native Promise-based replacement for Bluebird.try
11+
*
12+
* This function executes the provided function and returns a Promise.
13+
* If the function throws synchronously, the exception is caught and returned as a rejected Promise.
14+
* If the function returns a Promise, that Promise is returned.
15+
* If the function returns any other value, a resolved Promise with that value is returned.
16+
*
17+
* @param {Function} fn - The function to execute
18+
* @returns {Promise<T>} A promise that resolves with the return value of fn or rejects with any error thrown
19+
*/
20+
export function tryPromise<T>(fn: () => T | Promise<T>): Promise<T> {
21+
try {
22+
const result = fn();
23+
return Promise.resolve(result);
24+
} catch (error) {
25+
return Promise.reject(error);
26+
}
27+
}

modules/sdk-api/src/v1/blockchain.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
// Copyright 2014, BitGo, Inc. All Rights Reserved.
1313
//
1414

15-
import Bluebird from 'bluebird';
1615
import _ from 'lodash';
1716

1817
import { common } from '@bitgo/sdk-core';
@@ -37,7 +36,9 @@ Blockchain.prototype.getAddress = function (params, callback) {
3736
params = params || {};
3837
common.validateParams(params, ['address'], [], callback);
3938

40-
return Bluebird.resolve(this.bitgo.get(this.bitgo.url('/address/' + params.address)).result()).nodeify(callback);
39+
return Promise.resolve(this.bitgo.get(this.bitgo.url('/address/' + params.address)).result())
40+
.then(callback)
41+
.catch(callback);
4142
};
4243

4344
//
@@ -51,9 +52,9 @@ Blockchain.prototype.getAddressTransactions = function (params, callback) {
5152
common.validateParams(params, ['address'], [], callback);
5253

5354
// TODO: support start and limit params
54-
return Bluebird.resolve(this.bitgo.get(this.bitgo.url('/address/' + params.address + '/tx')).result()).nodeify(
55-
callback
56-
);
55+
return Promise.resolve(this.bitgo.get(this.bitgo.url('/address/' + params.address + '/tx')).result())
56+
.then(callback)
57+
.catch(callback);
5758
};
5859

5960
//
@@ -75,11 +76,12 @@ Blockchain.prototype.getAddressUnspents = function (params, callback) {
7576
url += '?limit=' + params.limit * 1e8;
7677
}
7778

78-
return Bluebird.resolve(this.bitgo.get(url).result())
79+
return Promise.resolve(this.bitgo.get(url).result())
7980
.then(function (body) {
8081
return body.unspents;
8182
})
82-
.nodeify(callback);
83+
.then(callback)
84+
.catch(callback);
8385
};
8486

8587
//
@@ -93,7 +95,9 @@ Blockchain.prototype.getTransaction = function (params, callback) {
9395
params = params || {};
9496
common.validateParams(params, ['id'], [], callback);
9597

96-
return Bluebird.resolve(this.bitgo.get(this.bitgo.url('/tx/' + params.id)).result()).nodeify(callback);
98+
return Promise.resolve(this.bitgo.get(this.bitgo.url('/tx/' + params.id)).result())
99+
.then(callback)
100+
.catch(callback);
97101
};
98102

99103
//
@@ -110,9 +114,9 @@ Blockchain.prototype.getTransactionByInput = function (params, callback) {
110114
if (!_.isInteger(params.vout)) {
111115
throw new Error('invalid vout - number expected');
112116
}
113-
return Bluebird.resolve(
114-
this.bitgo.get(this.bitgo.url('/tx/input/' + params.txid + '/' + params.vout)).result()
115-
).nodeify(callback);
117+
return Promise.resolve(this.bitgo.get(this.bitgo.url('/tx/input/' + params.txid + '/' + params.vout)).result())
118+
.then(callback)
119+
.catch(callback);
116120
};
117121

118122
//
@@ -126,7 +130,9 @@ Blockchain.prototype.getBlock = function (params, callback) {
126130
params = params || {};
127131
common.validateParams(params, ['id'], [], callback);
128132

129-
return Bluebird.resolve(this.bitgo.get(this.bitgo.url('/block/' + params.id)).result()).nodeify(callback);
133+
return Promise.resolve(this.bitgo.get(this.bitgo.url('/block/' + params.id)).result())
134+
.then(callback)
135+
.catch(callback);
130136
};
131137

132138
module.exports = Blockchain;

modules/sdk-api/src/v1/keychains.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import { bip32 } from '@bitgo/utxo-lib';
1515
import { randomBytes } from 'crypto';
1616
import { common, Util, sanitizeLegacyPath } from '@bitgo/sdk-core';
1717
const _ = require('lodash');
18-
import Bluebird from 'bluebird';
19-
const co = Bluebird.coroutine;
2018

2119
//
2220
// Constructor
@@ -150,7 +148,7 @@ Keychains.prototype.list = function (params, callback) {
150148
params = params || {};
151149
common.validateParams(params, [], [], callback);
152150

153-
return Bluebird.resolve(this.bitgo.get(this.bitgo.url('/keychain')).result('keychains'))
151+
return Promise.resolve(this.bitgo.get(this.bitgo.url('/keychain')).result('keychains'))
154152
.then(function (keychains) {
155153
keychains.map(function (keychain) {
156154
if (
@@ -164,7 +162,8 @@ Keychains.prototype.list = function (params, callback) {
164162
});
165163
return keychains;
166164
})
167-
.nodeify(callback);
165+
.then(callback)
166+
.catch(callback);
168167
};
169168

170169
/**
@@ -182,10 +181,10 @@ Keychains.prototype.list = function (params, callback) {
182181
* @returns result.version {Number}
183182
*/
184183
Keychains.prototype.updatePassword = function (params, callback) {
185-
return co(function* coUpdatePassword() {
184+
return async function coUpdatePassword() {
186185
common.validateParams(params, ['oldPassword', 'newPassword'], [], callback);
187186
// @ts-expect-error - no implicit this
188-
const encrypted = yield this.bitgo.post(this.bitgo.url('/user/encrypted')).result();
187+
const encrypted = await this.bitgo.post(this.bitgo.url('/user/encrypted')).result();
189188
const newKeychains = {};
190189
// @ts-expect-error - no implicit this
191190
const self = this;
@@ -200,9 +199,10 @@ Keychains.prototype.updatePassword = function (params, callback) {
200199
}
201200
});
202201
return { keychains: newKeychains, version: (encrypted as any).version };
203-
})
202+
}
204203
.call(this)
205-
.asCallback(callback);
204+
.then(callback)
205+
.catch(callback);
206206
};
207207

208208
//
@@ -213,7 +213,7 @@ Keychains.prototype.add = function (params, callback) {
213213
params = params || {};
214214
common.validateParams(params, ['xpub'], ['encryptedXprv', 'type', 'isLedger'], callback);
215215

216-
return Bluebird.resolve(
216+
return Promise.resolve(
217217
this.bitgo
218218
.post(this.bitgo.url('/keychain'))
219219
.send({
@@ -236,7 +236,8 @@ Keychains.prototype.add = function (params, callback) {
236236
}
237237
return keychain;
238238
})
239-
.nodeify(callback);
239+
.then(callback)
240+
.catch(callback);
240241
};
241242

242243
//
@@ -247,7 +248,7 @@ Keychains.prototype.createBitGo = function (params, callback) {
247248
params = params || {};
248249
common.validateParams(params, [], [], callback);
249250

250-
return Bluebird.resolve(this.bitgo.post(this.bitgo.url('/keychain/bitgo')).send(params).result())
251+
return Promise.resolve(this.bitgo.post(this.bitgo.url('/keychain/bitgo')).send(params).result())
251252
.then(function (keychain) {
252253
if (
253254
keychain.xpub &&
@@ -259,7 +260,8 @@ Keychains.prototype.createBitGo = function (params, callback) {
259260
}
260261
return keychain;
261262
})
262-
.nodeify(callback);
263+
.then(callback)
264+
.catch(callback);
263265
};
264266

265267
//
@@ -270,7 +272,7 @@ Keychains.prototype.createBackup = function (params, callback) {
270272
params = params || {};
271273
common.validateParams(params, ['provider'], [], callback);
272274

273-
return Bluebird.resolve(this.bitgo.post(this.bitgo.url('/keychain/backup')).send(params).result())
275+
return Promise.resolve(this.bitgo.post(this.bitgo.url('/keychain/backup')).send(params).result())
274276
.then(function (keychain) {
275277
// not all keychains have an xpub
276278
if (
@@ -283,7 +285,8 @@ Keychains.prototype.createBackup = function (params, callback) {
283285
}
284286
return keychain;
285287
})
286-
.nodeify(callback);
288+
.then(callback)
289+
.catch(callback);
287290
};
288291

289292
//
@@ -301,7 +304,7 @@ Keychains.prototype.get = function (params, callback) {
301304
}
302305

303306
const id = params.xpub || params.ethAddress;
304-
return Bluebird.resolve(
307+
return Promise.resolve(
305308
this.bitgo
306309
.post(this.bitgo.url('/keychain/' + encodeURIComponent(id)))
307310
.send({})
@@ -318,7 +321,8 @@ Keychains.prototype.get = function (params, callback) {
318321
}
319322
return keychain;
320323
})
321-
.nodeify(callback);
324+
.then(callback)
325+
.catch(callback);
322326
};
323327

324328
//
@@ -331,7 +335,7 @@ Keychains.prototype.update = function (params, callback) {
331335
params = params || {};
332336
common.validateParams(params, ['xpub'], ['encryptedXprv'], callback);
333337

334-
return Bluebird.resolve(
338+
return Promise.resolve(
335339
this.bitgo
336340
.put(this.bitgo.url('/keychain/' + params.xpub))
337341
.send({
@@ -350,7 +354,8 @@ Keychains.prototype.update = function (params, callback) {
350354
}
351355
return keychain;
352356
})
353-
.nodeify(callback);
357+
.then(callback)
358+
.catch(callback);
354359
};
355360

356361
module.exports = Keychains;

modules/sdk-api/src/v1/markets.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
// Copyright 2015, BitGo, Inc. All Rights Reserved.
1212
//
1313

14-
import Bluebird from 'bluebird';
15-
1614
import { common } from '@bitgo/sdk-core';
1715

1816
//
@@ -34,7 +32,9 @@ Markets.prototype.latest = function (params, callback) {
3432
params = params || {};
3533
common.validateParams(params, [], [], callback);
3634

37-
return Bluebird.resolve(this.bitgo.get(this.bitgo.url('/market/latest')).result()).nodeify(callback);
35+
return Promise.resolve(this.bitgo.get(this.bitgo.url('/market/latest')).result())
36+
.then(callback)
37+
.catch(callback);
3838
};
3939

4040
/**
@@ -48,7 +48,9 @@ Markets.prototype.yesterday = function (params, callback) {
4848
params = params || {};
4949
common.validateParams(params, [], [], callback);
5050

51-
return Bluebird.resolve(this.bitgo.get(this.bitgo.url('/market/yesterday')).result()).nodeify(callback);
51+
return Promise.resolve(this.bitgo.get(this.bitgo.url('/market/yesterday')).result())
52+
.then(callback)
53+
.catch(callback);
5254
};
5355

5456
/**
@@ -67,9 +69,9 @@ Markets.prototype.lastDays = function (params, callback) {
6769
throw new Error('must use a non-negative number of days');
6870
}
6971

70-
return Bluebird.resolve(
71-
this.bitgo.get(this.bitgo.url('/market/last/' + days + '/' + params.currencyName)).result()
72-
).nodeify(callback);
72+
return Promise.resolve(this.bitgo.get(this.bitgo.url('/market/last/' + days + '/' + params.currencyName)).result())
73+
.then(callback)
74+
.catch(callback);
7375
};
7476

7577
module.exports = Markets;

modules/sdk-api/src/v1/pendingapproval.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import { common } from '@bitgo/sdk-core';
1313
import * as utxolib from '@bitgo/utxo-lib';
1414

15-
import Bluebird from 'bluebird';
1615
import _ from 'lodash';
16+
import { tryPromise } from '../util';
1717

1818
//
1919
// Constructor
@@ -134,12 +134,13 @@ PendingApproval.prototype.get = function (params, callback) {
134134
common.validateParams(params, [], [], callback);
135135

136136
const self = this;
137-
return Bluebird.resolve(this.bitgo.get(this.url()).result())
137+
return Promise.resolve(this.bitgo.get(this.url()).result())
138138
.then(function (res) {
139139
self.pendingApproval = res;
140140
return self;
141141
})
142-
.nodeify(callback);
142+
.then(callback)
143+
.catch(callback);
143144
};
144145

145146
//
@@ -184,7 +185,7 @@ PendingApproval.prototype.recreateAndSignTransaction = function (params, callbac
184185

185186
const self = this;
186187

187-
return Bluebird.try(function () {
188+
return tryPromise(function () {
188189
if (self.info().transactionRequest.recipients) {
189190
// recipients object found on the pending approvals - use it
190191
params.recipients = self.info().transactionRequest.recipients;
@@ -240,7 +241,7 @@ PendingApproval.prototype.constructApprovalTx = function (params, callback) {
240241
}
241242

242243
const self = this;
243-
return Bluebird.try(function () {
244+
return tryPromise(function () {
244245
if (self.type() === 'transactionRequest') {
245246
const extendParams: any = { txHex: self.info().transactionRequest.transaction };
246247
if (params.useOriginalFee) {
@@ -280,7 +281,7 @@ PendingApproval.prototype.approve = function (params, callback) {
280281
}
281282

282283
const self = this;
283-
return Bluebird.try(function () {
284+
return tryPromise(function () {
284285
if (self.type() === 'transactionRequest') {
285286
if (params.tx) {
286287
// the approval tx was reconstructed and explicitly specified - pass it through
@@ -318,7 +319,7 @@ PendingApproval.prototype.approve = function (params, callback) {
318319
if (transaction) {
319320
approvalParams.tx = transaction.tx;
320321
}
321-
return Bluebird.resolve(self.bitgo.put(self.url()).send(approvalParams).result()).nodeify(callback);
322+
return Promise.resolve(self.bitgo.put(self.url()).send(approvalParams).result()).then(callback).catch(callback);
322323
})
323324
.catch(function (error) {
324325
if (
@@ -351,7 +352,9 @@ PendingApproval.prototype.reject = function (params, callback) {
351352
params = params || {};
352353
common.validateParams(params, [], [], callback);
353354

354-
return Bluebird.resolve(this.bitgo.put(this.url()).send({ state: 'rejected' }).result()).nodeify(callback);
355+
return Promise.resolve(this.bitgo.put(this.url()).send({ state: 'rejected' }).result())
356+
.then(callback)
357+
.catch(callback);
355358
};
356359

357360
//

0 commit comments

Comments
 (0)