-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathbitgoAPI.ts
More file actions
873 lines (735 loc) · 29.7 KB
/
bitgoAPI.ts
File metadata and controls
873 lines (735 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
import 'should';
import { BitGoAPI } from '../../src/bitgoAPI';
import { ProxyAgent } from 'proxy-agent';
import * as sinon from 'sinon';
import nock from 'nock';
import type { IHmacAuthStrategy } from '@bitgo/sdk-hmac';
import { GlobalCoinFactory, CoinConstructor } from '@bitgo/sdk-core';
describe('Constructor', function () {
describe('cookiesPropagationEnabled argument', function () {
it('cookiesPropagationEnabled is enabled explicitly', function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
cookiesPropagationEnabled: true,
});
bitgo.should.have.property('cookiesPropagationEnabled');
bitgo.cookiesPropagationEnabled.should.equal(true);
});
it('cookiesPropagationEnabled is disabled explicitly', function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
cookiesPropagationEnabled: false,
});
bitgo.should.have.property('cookiesPropagationEnabled');
bitgo.cookiesPropagationEnabled.should.equal(false);
});
it('cookiesPropagationEnabled is disabled by default', function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
});
bitgo.should.have.property('cookiesPropagationEnabled');
bitgo.cookiesPropagationEnabled.should.equal(false);
});
});
describe('requestIdPrefix argument', function () {
afterEach(function () {
nock.cleanAll();
});
it('should prepend requestIdPrefix to Request-ID header when set', async function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
requestIdPrefix: 'test-prefix-',
});
const scope = nock('https://app.example.local')
.get('/api/v1/ping')
.matchHeader('Request-ID', /^test-prefix-/)
.reply(200, { status: 'ok' });
await bitgo.ping({
reqId: {
toString: () => '12345',
inc: () => {
/* mock */
},
} as any,
});
scope.isDone().should.be.true();
});
it('should not modify Request-ID header when requestIdPrefix is not set', async function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
});
const scope = nock('https://app.example.local')
.get('/api/v1/ping')
.matchHeader('Request-ID', /^12345$/)
.reply(200, { status: 'ok' });
await bitgo.ping({
reqId: {
toString: () => '12345',
inc: () => {
/* mock */
},
} as any,
});
scope.isDone().should.be.true();
});
it('should correctly format Request-ID with prefix and numeric sequence', async function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
requestIdPrefix: 'myapp-v1-',
});
const scope = nock('https://app.example.local')
.get('/api/v1/ping')
.matchHeader('Request-ID', 'myapp-v1-trace-123')
.reply(200, { status: 'ok' });
await bitgo.ping({
reqId: {
toString: () => 'trace-123',
inc: () => {
/* mock */
},
} as any,
});
scope.isDone().should.be.true();
});
it('should work with empty string prefix', async function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
requestIdPrefix: '',
});
const scope = nock('https://app.example.local')
.get('/api/v1/ping')
.matchHeader('Request-ID', 'abc-123')
.reply(200, { status: 'ok' });
await bitgo.ping({
reqId: {
toString: () => 'abc-123',
inc: () => {
/* mock */
},
} as any,
});
scope.isDone().should.be.true();
});
});
describe('http proxy agent', function () {
it('http proxy agent shall be created when proxy(customProxyagent) is set', function () {
const customProxyAgent = new ProxyAgent({
getProxyForUrl: () => 'http://localhost:3000',
});
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
customProxyAgent,
});
bitgo.should.have.property('_customProxyAgent', customProxyAgent);
});
it('bitgo api is still initiated when proxy(customProxyAgent) is not set', function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
});
bitgo.should.have.property('_customProxyAgent', undefined);
});
});
describe('verifyAddress', function () {
it('should successfully verify a base58 address', function () {
const bitgo = new BitGoAPI({
env: 'test',
});
bitgo.verifyAddress({ address: '2N6paT2TU4N1XpaZjJiApWJXoeyrL3UWpkZ' }).should.be.true();
});
it('should successfully verify a bech32 address', function () {
const bitgo = new BitGoAPI({
env: 'test',
});
bitgo
.verifyAddress({ address: 'tb1qguzyk4w6kaqtpsczs5aj0w8r7598jq36egm8e98wqph3rwmex68seslgsg' })
.should.be.true();
});
});
describe('url', function () {
it('should return the correct URL for version 1', function () {
const bitgo = new BitGoAPI({
env: 'test',
customRootURI: 'https://test.bitgo.com',
});
const path = '/test-path';
const expectedUrl = 'https://test.bitgo.com/api/v1/test-path';
const result = bitgo.url(path, 1);
result.should.equal(expectedUrl);
});
it('should return the correct URL for version 2', function () {
const bitgo = new BitGoAPI({
env: 'test',
customRootURI: 'https://test.bitgo.com',
});
const path = '/test-path';
const expectedUrl = 'https://test.bitgo.com/api/v2/test-path';
const result = bitgo.url(path, 2);
result.should.equal(expectedUrl);
});
it('should return the correct URL for version 3', function () {
const bitgo = new BitGoAPI({
env: 'test',
customRootURI: 'https://test.bitgo.com',
});
const path = '/test-path';
const expectedUrl = 'https://test.bitgo.com/api/v3/test-path';
const result = bitgo.url(path, 3);
result.should.equal(expectedUrl);
});
it('should default to version 1 if no version is provided', function () {
const bitgo = new BitGoAPI({
env: 'test',
customRootURI: 'https://test.bitgo.com',
});
const path = '/test-path';
const expectedUrl = 'https://test.bitgo.com/api/v1/test-path';
const result = bitgo.url(path);
result.should.equal(expectedUrl);
});
});
describe('decryptKeys', function () {
let bitgo: BitGoAPI;
beforeEach(function () {
bitgo = new BitGoAPI({
env: 'test',
});
});
afterEach(function () {
sinon.restore();
});
it('should throw if no params are provided', function () {
try {
// @ts-expect-error - intentionally calling with no params for test
bitgo.decryptKeys();
throw new Error('Expected error but got none');
} catch (e) {
e.message.should.containEql('Missing parameter');
}
});
it('should throw if walletIdEncryptedKeyPairs is missing', function () {
try {
// @ts-expect-error - intentionally missing required param
bitgo.decryptKeys({ password: 'password123' });
throw new Error('Expected error but got none');
} catch (e) {
e.message.should.containEql('Missing parameter: walletIdEncryptedKeyPairs');
}
});
it('should throw if password is missing', function () {
try {
// @ts-expect-error - intentionally missing required param
bitgo.decryptKeys({ walletIdEncryptedKeyPairs: [] });
throw new Error('Expected error but got none');
} catch (e) {
e.message.should.containEql('Missing parameter: password');
}
});
it('should throw if walletIdEncryptedKeyPairs is not an array', function () {
try {
// @ts-expect-error - intentionally providing wrong type
bitgo.decryptKeys({ walletIdEncryptedKeyPairs: 'not an array', password: 'password123' });
throw new Error('Expected error but got none');
} catch (e) {
e.message.should.equal('walletIdEncryptedKeyPairs must be an array');
}
});
it('should return empty array for empty walletIdEncryptedKeyPairs', function () {
const result = bitgo.decryptKeys({ walletIdEncryptedKeyPairs: [], password: 'password123' });
result.should.be.an.Array();
result.should.be.empty();
});
it('should throw if any walletId is missing or not a string', function () {
try {
bitgo.decryptKeys({
walletIdEncryptedKeyPairs: [
// @ts-expect-error - intentionally missing walletId
{
encryptedPrv: 'encrypted-data',
},
],
password: 'password123',
});
throw new Error('Expected error but got none');
} catch (e) {
e.message.should.equal('each key pair must have a string walletId');
}
try {
bitgo.decryptKeys({
walletIdEncryptedKeyPairs: [
{
// @ts-expect-error - intentionally providing wrong type
walletId: 123,
encryptedPrv: 'encrypted-data',
},
],
password: 'password123',
});
throw new Error('Expected error but got none');
} catch (e) {
e.message.should.equal('each key pair must have a string walletId');
}
});
it('should throw if any encryptedPrv is missing or not a string', function () {
try {
bitgo.decryptKeys({
walletIdEncryptedKeyPairs: [
// @ts-expect-error - intentionally missing encryptedPrv
{
walletId: 'wallet-id-1',
},
],
password: 'password123',
});
throw new Error('Expected error but got none');
} catch (e) {
e.message.should.equal('each key pair must have a string encryptedPrv');
}
try {
bitgo.decryptKeys({
walletIdEncryptedKeyPairs: [
{
walletId: 'wallet-id-1',
// @ts-expect-error - intentionally providing wrong type
encryptedPrv: 123,
},
],
password: 'password123',
});
throw new Error('Expected error but got none');
} catch (e) {
e.message.should.equal('each key pair must have a string encryptedPrv');
}
});
it('should return walletIds of keys that failed to decrypt', function () {
// Create a stub for the decrypt method
const decryptStub = sinon.stub(bitgo, 'decrypt');
// Make it succeed for first wallet and fail for second wallet
decryptStub.onFirstCall().returns('decrypted-key-1');
decryptStub.onSecondCall().throws(new Error('decryption failed'));
const result = bitgo.decryptKeys({
walletIdEncryptedKeyPairs: [
{ walletId: 'wallet-id-1', encryptedPrv: 'encrypted-data-1' },
{ walletId: 'wallet-id-2', encryptedPrv: 'encrypted-data-2' },
],
password: 'password123',
});
result.should.be.an.Array();
result.should.have.length(1);
result[0].should.equal('wallet-id-2');
});
it('should correctly process multiple wallet keys', function () {
// Create a spy on the decrypt method
const decryptStub = sinon.stub(bitgo, 'decrypt');
// Configure the stub to throw for specific wallets
decryptStub
.withArgs({ input: 'encrypted-data-2', password: 'password123' })
.throws(new Error('decryption failed'));
decryptStub
.withArgs({ input: 'encrypted-data-4', password: 'password123' })
.throws(new Error('decryption failed'));
decryptStub.returns('success'); // Default return for other calls
const result = bitgo.decryptKeys({
walletIdEncryptedKeyPairs: [
{ walletId: 'wallet-id-1', encryptedPrv: 'encrypted-data-1' },
{ walletId: 'wallet-id-2', encryptedPrv: 'encrypted-data-2' },
{ walletId: 'wallet-id-3', encryptedPrv: 'encrypted-data-3' },
{ walletId: 'wallet-id-4', encryptedPrv: 'encrypted-data-4' },
],
password: 'password123',
});
// Should be called once for each wallet
decryptStub.callCount.should.equal(4);
// Should include only the failed wallet IDs
result.should.be.an.Array();
result.should.have.length(2);
result.should.containDeep(['wallet-id-2', 'wallet-id-4']);
});
});
describe('User-Agent header based on environment', function () {
afterEach(function () {
nock.cleanAll();
sinon.restore();
});
it('should set User-Agent header when running in Node.js (typeof window === undefined)', async function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
userAgent: 'TestAgent/1.0',
});
// Ensure we're in a Node.js environment by verifying window is undefined
(typeof window).should.equal('undefined');
const scope = nock('https://app.example.local')
.get('/api/v1/ping')
.matchHeader('User-Agent', 'TestAgent/1.0')
.reply(200, { status: 'ok' });
await bitgo.ping({
reqId: {
toString: () => 'test-123',
inc: () => {
/* mock */
},
} as any,
});
scope.isDone().should.be.true();
});
it('should not set User-Agent header when running in browser (typeof window !== undefined)', async function () {
// Mock the window object to simulate browser environment
const windowStub = { location: 'mock' };
(global as any).window = windowStub;
try {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
userAgent: 'TestAgent/1.0',
});
const scope = nock('https://app.example.local')
.get('/api/v1/ping')
.reply(function () {
// Verify User-Agent header is NOT set to our custom value
const userAgent = this.req.headers['user-agent'];
if (userAgent && userAgent.includes('TestAgent/1.0')) {
throw new Error('User-Agent should not be set in browser environment');
}
return [200, { status: 'ok' }];
});
await bitgo.ping({
reqId: {
toString: () => 'test-123',
inc: () => {
/* mock */
},
} as any,
});
scope.isDone().should.be.true();
} finally {
// Clean up the global window mock
delete (global as any).window;
}
});
});
describe('hmacAuthStrategy token lifecycle', function () {
const ROOT = 'https://app.example.local';
// Builds a mock strategy whose setToken / clearToken are sinon stubs.
function makeStrategy(overrides: Partial<IHmacAuthStrategy> = {}): {
strategy: IHmacAuthStrategy;
setTokenStub: sinon.SinonStub;
clearTokenStub: sinon.SinonStub;
} {
const setTokenStub = sinon.stub().resolves();
const clearTokenStub = sinon.stub().resolves();
const strategy: IHmacAuthStrategy = {
calculateRequestHeaders: sinon.stub().resolves({ hmac: 'hmac', timestamp: 1, tokenHash: 'hash' }),
verifyResponse: sinon.stub().resolves({
isValid: true,
expectedHmac: 'hmac',
signatureSubject: '',
isInResponseValidityWindow: true,
verificationTime: Date.now(),
}),
calculateHMAC: sinon.stub().resolves('hashed-pw'),
setToken: setTokenStub,
clearToken: clearTokenStub,
...overrides,
};
return { strategy, setTokenStub, clearTokenStub };
}
afterEach(function () {
nock.cleanAll();
sinon.restore();
});
describe('authenticate()', function () {
it('calls setToken with the access_token received from the server', async function () {
const { strategy, setTokenStub } = makeStrategy();
const bitgo = new BitGoAPI({ env: 'custom', customRootURI: ROOT, hmacAuthStrategy: strategy });
nock(ROOT)
.post('/api/auth/v1/session')
.reply(200, {
user: { username: 'test@example.com' },
access_token: 'v2xmyaccesstoken',
});
await bitgo.authenticate({ username: 'test@example.com', password: 'hunter2' });
setTokenStub.calledOnce.should.be.true();
setTokenStub.firstCall.args[0].should.equal('v2xmyaccesstoken');
});
it('awaits setToken before making ensureEcdhKeychain requests', async function () {
// This is the core regression test: if setToken is not awaited, the
// strategy's key material won't be ready before calculateRequestHeaders
// is called for the GET /user/settings request, and it would throw.
let keyReady = false;
const { strategy } = makeStrategy({
setToken: sinon.stub().callsFake(async () => {
// Simulate non-trivial async key derivation (like crypto.subtle.importKey).
await new Promise<void>((resolve) => setImmediate(resolve));
keyReady = true;
}),
calculateRequestHeaders: sinon.stub().callsFake(async () => {
if (!keyReady) {
throw new Error('No token available. Call setToken() or restoreToken() first.');
}
return { hmac: 'hmac', timestamp: Date.now(), tokenHash: 'hash' };
}),
});
const bitgo = new BitGoAPI({ env: 'custom', customRootURI: ROOT, hmacAuthStrategy: strategy });
nock(ROOT)
.post('/api/auth/v1/session')
.reply(200, {
user: { username: 'test@example.com' },
access_token: 'v2xmytoken',
});
// The GET /user/settings request made by ensureUserEcdhKeychainIsCreated
// must succeed — it would throw if setToken wasn't awaited first.
nock(ROOT)
.get('/api/v1/user/settings')
.reply(200, {
settings: { ecdhKeychain: 'xpub123' },
});
await bitgo.authenticate({
username: 'test@example.com',
password: 'hunter2',
ensureEcdhKeychain: true,
});
keyReady.should.be.true();
});
});
describe('authenticateWithPasskey()', function () {
const validPasskey = JSON.stringify({
id: 'credential-id',
rawId: 'raw-id',
type: 'public-key',
response: {
authenticatorData: 'auth-data',
clientDataJSON: 'client-data',
signature: 'sig',
userHandle: 'user-handle-123',
},
});
it('calls setToken with the access_token received from the server', async function () {
const { strategy, setTokenStub } = makeStrategy();
const bitgo = new BitGoAPI({ env: 'custom', customRootURI: ROOT, hmacAuthStrategy: strategy });
nock(ROOT)
.post('/api/auth/v1/session')
.reply(200, {
user: { username: 'test@example.com' },
access_token: 'v2xpasskeytoken',
});
await bitgo.authenticateWithPasskey(validPasskey);
setTokenStub.calledOnce.should.be.true();
setTokenStub.firstCall.args[0].should.equal('v2xpasskeytoken');
});
});
describe('clearAsync()', function () {
it('clears _token and calls clearToken on the strategy', async function () {
const { strategy, clearTokenStub } = makeStrategy();
const bitgo = new BitGoAPI({ env: 'custom', customRootURI: ROOT, hmacAuthStrategy: strategy });
bitgo.authenticateWithAccessToken({ accessToken: 'v2xsometoken' });
(bitgo as any)._token.should.equal('v2xsometoken');
await bitgo.clearAsync();
((bitgo as any)._token === undefined).should.be.true();
clearTokenStub.calledOnce.should.be.true();
});
});
describe('refreshToken()', function () {
it('calls setToken with the new access_token from the OAuth response', async function () {
const { strategy, setTokenStub } = makeStrategy();
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: ROOT,
hmacAuthStrategy: strategy,
clientId: 'client-id',
clientSecret: 'client-secret',
});
(bitgo as any)._refreshToken = 'old-refresh-token';
nock(ROOT).post('/oauth/token').reply(200, {
access_token: 'v2xnewtoken',
refresh_token: 'new-refresh-token',
});
await bitgo.refreshToken();
setTokenStub.calledOnce.should.be.true();
setTokenStub.firstCall.args[0].should.equal('v2xnewtoken');
});
});
describe('authenticateWithAuthCode()', function () {
it('calls setToken with the access_token from the OAuth response', async function () {
const { strategy, setTokenStub } = makeStrategy();
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: ROOT,
hmacAuthStrategy: strategy,
clientId: 'client-id',
clientSecret: 'client-secret',
});
nock(ROOT).post('/oauth/token').reply(200, {
access_token: 'v2xauthcodetoken',
refresh_token: 'refresh-token',
});
// authenticateWithAuthCode calls this.me() after setting the token
nock(ROOT)
.get('/api/v1/user/me')
.reply(200, {
user: { username: 'test@example.com' },
});
await bitgo.authenticateWithAuthCode({ authCode: 'my-auth-code' });
setTokenStub.calledOnce.should.be.true();
setTokenStub.firstCall.args[0].should.equal('v2xauthcodetoken');
});
});
describe('sync token-setting methods', function () {
it('authenticateWithAccessToken does not call setToken (synchronous — caller must invoke setToken on the strategy manually)', function () {
const { strategy, setTokenStub } = makeStrategy();
const bitgo = new BitGoAPI({ env: 'custom', customRootURI: ROOT, hmacAuthStrategy: strategy });
bitgo.authenticateWithAccessToken({ accessToken: 'v2xsynctoken' });
setTokenStub.called.should.be.false();
});
it('fromJSON does not call setToken (synchronous — caller must invoke setToken on the strategy manually)', function () {
const { strategy, setTokenStub } = makeStrategy();
const bitgo = new BitGoAPI({ env: 'custom', customRootURI: ROOT, hmacAuthStrategy: strategy });
(bitgo as any).fromJSON({ user: { username: 'test@example.com' }, token: 'v2xjsontoken' });
setTokenStub.called.should.be.false();
});
});
});
describe('constants parameter', function () {
it('should allow passing constants via options and expose via fetchConstants', async function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
clientConstants: { maxFeeRate: '123123123123123' },
});
const constants = await bitgo.fetchConstants();
constants.should.have.property('maxFeeRate', '123123123123123');
});
it('should refresh constants when cache has expired', async function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
});
// Set up cached constants with an expired cache
(BitGoAPI as any)._constants = (BitGoAPI as any)._constants || {};
(BitGoAPI as any)._constantsExpire = (BitGoAPI as any)._constantsExpire || {};
(BitGoAPI as any)._constants['custom'] = { maxFeeRate: 'old-value' };
(BitGoAPI as any)._constantsExpire['custom'] = new Date(Date.now() - 1000); // Expired 1 second ago
const scope = nock('https://app.example.local')
.get('/api/v1/client/constants')
.reply(200, {
constants: { maxFeeRate: 'new-value', newConstant: 'added' },
});
const constants = await bitgo.fetchConstants();
// Should return the new constants from the server
constants.should.have.property('maxFeeRate', 'new-value');
constants.should.have.property('newConstant', 'added');
scope.isDone().should.be.true();
nock.cleanAll();
});
it('should use cached constants when cache is still valid', async function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
});
// Set up cached constants with a future expiry
const cachedConstants = { maxFeeRate: 'cached-value', anotherSetting: 'cached-setting' };
(BitGoAPI as any)._constants = (BitGoAPI as any)._constants || {};
(BitGoAPI as any)._constantsExpire = (BitGoAPI as any)._constantsExpire || {};
(BitGoAPI as any)._constants['custom'] = cachedConstants;
(BitGoAPI as any)._constantsExpire['custom'] = new Date(Date.now() + 5 * 60 * 1000); // Valid for 5 more minutes
const scope = nock('https://app.example.local')
.get('/api/v1/client/constants')
.reply(200, { constants: { shouldNotBeUsed: true } });
const constants = await bitgo.fetchConstants();
// Should return the cached constants
constants.should.deepEqual(cachedConstants);
// Verify that no HTTP request was made (since cache was valid)
scope.isDone().should.be.false();
nock.cleanAll();
});
it('should use cached constants when no cache expiry is set', async function () {
const bitgo = new BitGoAPI({
env: 'custom',
customRootURI: 'https://app.example.local',
});
// Set up cached constants with no expiry
const cachedConstants = { maxFeeRate: 'no-expiry-value' };
(BitGoAPI as any)._constants = (BitGoAPI as any)._constants || {};
(BitGoAPI as any)._constantsExpire = (BitGoAPI as any)._constantsExpire || {};
(BitGoAPI as any)._constants['custom'] = cachedConstants;
(BitGoAPI as any)._constantsExpire['custom'] = undefined;
const scope = nock('https://app.example.local')
.get('/api/v1/client/constants')
.reply(200, { constants: { shouldNotBeUsed: true } });
const constants = await bitgo.fetchConstants();
// Should return the cached constants
constants.should.deepEqual(cachedConstants);
// Verify that no HTTP request was made (since no expiry means cache is always valid)
scope.isDone().should.be.false();
nock.cleanAll();
});
});
describe('registerToken', function () {
let bitgo: BitGoAPI;
let sandbox: sinon.SinonSandbox;
beforeEach(function () {
bitgo = new BitGoAPI({ env: 'custom', customRootURI: 'https://app.example.local' });
sandbox = sinon.createSandbox();
});
afterEach(function () {
sandbox.restore();
});
it('should call GlobalCoinFactory.registerToken and allow sdk.coin() to resolve the registered token', function () {
const mockCoinInstance = { type: 'test-ams-dynamic-token' } as any;
const mockConstructor = sandbox.stub().returns(mockCoinInstance) as unknown as CoinConstructor;
const mockCoinConfig = { name: 'test-ams-dynamic-token', id: 'test-ams-dynamic-token-id' } as any;
sandbox.stub(GlobalCoinFactory, 'registerToken');
sandbox.stub(GlobalCoinFactory, 'getInstance').callsFake((_bitgo, name) => {
if (name === 'test-ams-dynamic-token') {
return mockConstructor(_bitgo, mockCoinConfig);
}
throw new Error(`Unsupported coin: ${name}`);
});
bitgo.registerToken(mockCoinConfig, mockConstructor);
const coin = bitgo.coin('test-ams-dynamic-token');
coin.should.equal(mockCoinInstance);
(GlobalCoinFactory.registerToken as sinon.SinonStub)
.calledOnceWith(mockCoinConfig, mockConstructor)
.should.be.true();
});
it('should be idempotent — calling registerToken twice for same token does not throw', function () {
const mockCoinConfig = { name: 'test-ams-idempotent-token', id: 'test-ams-idempotent-token-id' } as any;
const mockConstructor = sandbox.stub() as unknown as CoinConstructor;
sandbox.stub(GlobalCoinFactory, 'registerToken');
(() => {
bitgo.registerToken(mockCoinConfig, mockConstructor);
bitgo.registerToken(mockCoinConfig, mockConstructor);
}).should.not.throw();
(GlobalCoinFactory.registerToken as sinon.SinonStub).callCount.should.equal(2);
});
it('should not affect existing statics coins after registerToken calls', function () {
const newCoinConfig = { name: 'test-ams-new-token', id: 'test-ams-new-token-id' } as any;
const newConstructor = sandbox.stub() as unknown as CoinConstructor;
const existingCoinInstance = { type: 'eth' } as any;
const existingConstructor = sandbox.stub().returns(existingCoinInstance) as unknown as CoinConstructor;
const registerTokenStub = sandbox.stub(GlobalCoinFactory, 'registerToken');
sandbox.stub(GlobalCoinFactory, 'getInstance').callsFake((_bitgo, name) => {
if (name === 'eth') {
return existingConstructor(_bitgo, undefined);
}
throw new Error(`Unsupported coin: ${name}`);
});
// Register a new AMS token
bitgo.registerToken(newCoinConfig, newConstructor);
// Existing statics coin should still resolve correctly
const ethCoin = bitgo.coin('eth');
ethCoin.should.equal(existingCoinInstance);
// registerToken was called only once (for the new token, not for eth)
registerTokenStub.calledOnce.should.be.true();
registerTokenStub.calledWith(newCoinConfig, newConstructor).should.be.true();
});
});
});