@imtbl/sdk 0.1.4-alpha → 0.1.5-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/checkout_sdk.d.ts +57 -3
- package/dist/checkout_sdk.js +22 -5
- package/dist/checkout_widgets.d.ts +600 -1
- package/dist/checkout_widgets.js +193 -142
- package/dist/immutablex_client.d.ts +9 -9
- package/dist/immutablex_client.js +34 -31
- package/dist/index.cjs +377 -161
- package/dist/index.d.ts +118 -13
- package/dist/index.js +385 -178
- package/dist/passport.d.ts +9 -9
- package/dist/passport.js +233 -202
- package/package.json +3 -3
package/dist/passport.js
CHANGED
|
@@ -1,71 +1,10 @@
|
|
|
1
|
+
import { ImmutableX, Config, generateLegacyStarkPrivateKey, createStarkSigner } from '@imtbl/core-sdk';
|
|
1
2
|
import { UserManager } from 'oidc-client-ts';
|
|
2
3
|
import { ethers } from 'ethers';
|
|
3
4
|
import { Magic } from 'magic-sdk';
|
|
4
5
|
import { OpenIdExtension } from '@magic-ext/oidc';
|
|
5
6
|
import BN from 'bn.js';
|
|
6
7
|
import * as encUtils from 'enc-utils';
|
|
7
|
-
import { ImmutableX, Config, generateLegacyStarkPrivateKey, createStarkSigner } from '@imtbl/core-sdk';
|
|
8
|
-
|
|
9
|
-
// used to sign message with L1 keys. Used for registration
|
|
10
|
-
function serializeEthSignature(sig) {
|
|
11
|
-
// This is because golang appends a recovery param
|
|
12
|
-
// https://github.com/ethers-io/ethers.js/issues/823
|
|
13
|
-
return encUtils.addHexPrefix(encUtils.padLeft(sig.r.toString(16), 64) +
|
|
14
|
-
encUtils.padLeft(sig.s.toString(16), 64) +
|
|
15
|
-
encUtils.padLeft(sig.recoveryParam?.toString(16) || '', 2));
|
|
16
|
-
}
|
|
17
|
-
function importRecoveryParam(v) {
|
|
18
|
-
return v.trim()
|
|
19
|
-
? new BN(v, 16).cmp(new BN(27)) !== -1
|
|
20
|
-
? new BN(v, 16).sub(new BN(27)).toNumber()
|
|
21
|
-
: new BN(v, 16).toNumber()
|
|
22
|
-
: undefined;
|
|
23
|
-
}
|
|
24
|
-
// used chained with serializeEthSignature. serializeEthSignature(deserializeSignature(...))
|
|
25
|
-
function deserializeSignature(sig, size = 64) {
|
|
26
|
-
sig = encUtils.removeHexPrefix(sig);
|
|
27
|
-
return {
|
|
28
|
-
r: new BN(sig.substring(0, size), 'hex'),
|
|
29
|
-
s: new BN(sig.substring(size, size * 2), 'hex'),
|
|
30
|
-
recoveryParam: importRecoveryParam(sig.substring(size * 2, size * 2 + 2)),
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
async function signRaw(payload, signer) {
|
|
34
|
-
const signature = deserializeSignature(await signer.signMessage(payload));
|
|
35
|
-
return serializeEthSignature(signature);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Helper method to convert token type to a SignableToken type
|
|
40
|
-
* @param token - the token type to convert to a SignableToken type
|
|
41
|
-
* @returns the converted SignableToken
|
|
42
|
-
*/
|
|
43
|
-
function convertToSignableToken(token) {
|
|
44
|
-
switch (token.type) {
|
|
45
|
-
case 'ERC721':
|
|
46
|
-
return {
|
|
47
|
-
type: 'ERC721',
|
|
48
|
-
data: {
|
|
49
|
-
token_id: token.tokenId,
|
|
50
|
-
token_address: token.tokenAddress,
|
|
51
|
-
},
|
|
52
|
-
};
|
|
53
|
-
case 'ERC20':
|
|
54
|
-
return {
|
|
55
|
-
type: 'ERC20',
|
|
56
|
-
data: {
|
|
57
|
-
token_address: token.tokenAddress,
|
|
58
|
-
},
|
|
59
|
-
};
|
|
60
|
-
case 'ETH':
|
|
61
|
-
return {
|
|
62
|
-
type: 'ETH',
|
|
63
|
-
data: {
|
|
64
|
-
decimals: 18,
|
|
65
|
-
},
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
8
|
|
|
70
9
|
var Environment;
|
|
71
10
|
(function (Environment) {
|
|
@@ -73,26 +12,26 @@ var Environment;
|
|
|
73
12
|
Environment["SANDBOX"] = "sandbox";
|
|
74
13
|
})(Environment || (Environment = {}));
|
|
75
14
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
};
|
|
15
|
+
/**
|
|
16
|
+
* Set `sdkVersion` on config
|
|
17
|
+
*/
|
|
18
|
+
const production = () => Config.createConfig({
|
|
19
|
+
basePath: 'https://api.x.immutable.com',
|
|
20
|
+
chainID: 1,
|
|
21
|
+
coreContractAddress: '0x5FDCCA53617f4d2b9134B29090C87D01058e27e9',
|
|
22
|
+
registrationContractAddress: '0x72a06bf2a1CE5e39cBA06c0CAb824960B587d64c',
|
|
23
|
+
sdkVersion: 'ts-immutable-sdk-0.1.5-alpha',
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* Set `sdkVersion` on config
|
|
27
|
+
*/
|
|
28
|
+
const sandbox = () => Config.createConfig({
|
|
29
|
+
basePath: 'https://api.sandbox.x.immutable.com',
|
|
30
|
+
chainID: 5,
|
|
31
|
+
coreContractAddress: '0x7917eDb51ecD6CdB3F9854c3cc593F33de10c623',
|
|
32
|
+
registrationContractAddress: '0x1C97Ada273C9A52253f463042f29117090Cd7D83',
|
|
33
|
+
sdkVersion: 'ts-immutable-sdk-0.1.5-alpha',
|
|
34
|
+
});
|
|
96
35
|
class ImxConfiguration {
|
|
97
36
|
immutableXConfig;
|
|
98
37
|
baseConfig;
|
|
@@ -104,13 +43,16 @@ class ImxConfiguration {
|
|
|
104
43
|
else {
|
|
105
44
|
switch (baseConfig.environment) {
|
|
106
45
|
case Environment.SANDBOX: {
|
|
107
|
-
this.immutableXConfig =
|
|
46
|
+
this.immutableXConfig = sandbox();
|
|
108
47
|
break;
|
|
109
48
|
}
|
|
110
49
|
case Environment.PRODUCTION: {
|
|
111
|
-
this.immutableXConfig =
|
|
50
|
+
this.immutableXConfig = production();
|
|
112
51
|
break;
|
|
113
52
|
}
|
|
53
|
+
default: {
|
|
54
|
+
this.immutableXConfig = sandbox();
|
|
55
|
+
}
|
|
114
56
|
}
|
|
115
57
|
}
|
|
116
58
|
}
|
|
@@ -165,7 +107,7 @@ class ImmutableXClient {
|
|
|
165
107
|
}
|
|
166
108
|
/**
|
|
167
109
|
* Get a list of Deposits
|
|
168
|
-
* @param request - optional
|
|
110
|
+
* @param request - optional object containing the parameters to be provided in the API request
|
|
169
111
|
* @returns a promise that resolves with the requested list of Deposits
|
|
170
112
|
* @throws {@link index.IMXError}
|
|
171
113
|
*/
|
|
@@ -192,7 +134,7 @@ class ImmutableXClient {
|
|
|
192
134
|
}
|
|
193
135
|
/**
|
|
194
136
|
* Get a list of Assets
|
|
195
|
-
* @param request - optional
|
|
137
|
+
* @param request - optional object containing the parameters to be provided in the API request
|
|
196
138
|
* @returns a promise that resolves with the requested list of Assets
|
|
197
139
|
* @throws {@link index.IMXError}
|
|
198
140
|
*/
|
|
@@ -229,7 +171,7 @@ class ImmutableXClient {
|
|
|
229
171
|
}
|
|
230
172
|
/**
|
|
231
173
|
* Get a list of Collections
|
|
232
|
-
* @param request - optional
|
|
174
|
+
* @param request - optional object containing the parameters to be provided in the API request
|
|
233
175
|
* @returns a promise that resolves with the requested list of Collections
|
|
234
176
|
* @throws {@link index.IMXError}
|
|
235
177
|
*/
|
|
@@ -385,7 +327,7 @@ class ImmutableXClient {
|
|
|
385
327
|
}
|
|
386
328
|
/**
|
|
387
329
|
* Get a list of Mints
|
|
388
|
-
* @param request optional
|
|
330
|
+
* @param request optional object containing the parameters to be provided in the API request
|
|
389
331
|
* @returns a promise that resolves with the requested list of Mints
|
|
390
332
|
* @throws {@link index.IMXError}
|
|
391
333
|
*/
|
|
@@ -404,7 +346,7 @@ class ImmutableXClient {
|
|
|
404
346
|
}
|
|
405
347
|
/**
|
|
406
348
|
* Get a list of Withdrawals
|
|
407
|
-
* @param request - optional
|
|
349
|
+
* @param request - optional object containing the parameters to be provided in the API request
|
|
408
350
|
* @returns a promise that resolves with the requested list of Withdrawals
|
|
409
351
|
* @throws {@link index.IMXError}
|
|
410
352
|
*/
|
|
@@ -431,7 +373,7 @@ class ImmutableXClient {
|
|
|
431
373
|
}
|
|
432
374
|
/**
|
|
433
375
|
* Get a list of Orders
|
|
434
|
-
* @param request - optional
|
|
376
|
+
* @param request - optional object containing the parameters to be provided in the API request
|
|
435
377
|
* @returns a promise that resolves with the requested list of Orders
|
|
436
378
|
* @throws {@link index.IMXError}
|
|
437
379
|
*/
|
|
@@ -449,7 +391,7 @@ class ImmutableXClient {
|
|
|
449
391
|
}
|
|
450
392
|
/**
|
|
451
393
|
* Get a list of Trades
|
|
452
|
-
* @param request - optional
|
|
394
|
+
* @param request - optional object containing the parameters to be provided in the API request
|
|
453
395
|
* @returns a promise that resolves with the requested list of Trades
|
|
454
396
|
* @throws {@link index.IMXError}
|
|
455
397
|
*/
|
|
@@ -467,7 +409,7 @@ class ImmutableXClient {
|
|
|
467
409
|
}
|
|
468
410
|
/**
|
|
469
411
|
* Get a list of Tokens
|
|
470
|
-
* @param request - optional
|
|
412
|
+
* @param request - optional object containing the parameters to be provided in the API request
|
|
471
413
|
* @returns a promise that resolves with the requested list of Tokens
|
|
472
414
|
* @throws {@link index.IMXError}
|
|
473
415
|
*/
|
|
@@ -485,7 +427,7 @@ class ImmutableXClient {
|
|
|
485
427
|
}
|
|
486
428
|
/**
|
|
487
429
|
* Get a list of Transfers
|
|
488
|
-
* @param request - optional
|
|
430
|
+
* @param request - optional object containing the parameters to be provided in the API request
|
|
489
431
|
* @returns a promise that resolves with the requested list of Transfers
|
|
490
432
|
* @throws {@link index.IMXError}
|
|
491
433
|
*/
|
|
@@ -573,6 +515,67 @@ class ImmutableXClient {
|
|
|
573
515
|
}
|
|
574
516
|
}
|
|
575
517
|
|
|
518
|
+
// used to sign message with L1 keys. Used for registration
|
|
519
|
+
function serializeEthSignature(sig) {
|
|
520
|
+
// This is because golang appends a recovery param
|
|
521
|
+
// https://github.com/ethers-io/ethers.js/issues/823
|
|
522
|
+
return encUtils.addHexPrefix(encUtils.padLeft(sig.r.toString(16), 64) +
|
|
523
|
+
encUtils.padLeft(sig.s.toString(16), 64) +
|
|
524
|
+
encUtils.padLeft(sig.recoveryParam?.toString(16) || '', 2));
|
|
525
|
+
}
|
|
526
|
+
function importRecoveryParam(v) {
|
|
527
|
+
return v.trim()
|
|
528
|
+
? new BN(v, 16).cmp(new BN(27)) !== -1
|
|
529
|
+
? new BN(v, 16).sub(new BN(27)).toNumber()
|
|
530
|
+
: new BN(v, 16).toNumber()
|
|
531
|
+
: undefined;
|
|
532
|
+
}
|
|
533
|
+
// used chained with serializeEthSignature. serializeEthSignature(deserializeSignature(...))
|
|
534
|
+
function deserializeSignature(sig, size = 64) {
|
|
535
|
+
sig = encUtils.removeHexPrefix(sig);
|
|
536
|
+
return {
|
|
537
|
+
r: new BN(sig.substring(0, size), 'hex'),
|
|
538
|
+
s: new BN(sig.substring(size, size * 2), 'hex'),
|
|
539
|
+
recoveryParam: importRecoveryParam(sig.substring(size * 2, size * 2 + 2)),
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
async function signRaw(payload, signer) {
|
|
543
|
+
const signature = deserializeSignature(await signer.signMessage(payload));
|
|
544
|
+
return serializeEthSignature(signature);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Helper method to convert token type to a SignableToken type
|
|
549
|
+
* @param token - the token type to convert to a SignableToken type
|
|
550
|
+
* @returns the converted SignableToken
|
|
551
|
+
*/
|
|
552
|
+
function convertToSignableToken(token) {
|
|
553
|
+
switch (token.type) {
|
|
554
|
+
case 'ERC721':
|
|
555
|
+
return {
|
|
556
|
+
type: 'ERC721',
|
|
557
|
+
data: {
|
|
558
|
+
token_id: token.tokenId,
|
|
559
|
+
token_address: token.tokenAddress,
|
|
560
|
+
},
|
|
561
|
+
};
|
|
562
|
+
case 'ERC20':
|
|
563
|
+
return {
|
|
564
|
+
type: 'ERC20',
|
|
565
|
+
data: {
|
|
566
|
+
token_address: token.tokenAddress,
|
|
567
|
+
},
|
|
568
|
+
};
|
|
569
|
+
case 'ETH':
|
|
570
|
+
return {
|
|
571
|
+
type: 'ETH',
|
|
572
|
+
data: {
|
|
573
|
+
decimals: 18,
|
|
574
|
+
},
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
576
579
|
var PassportErrorType;
|
|
577
580
|
(function (PassportErrorType) {
|
|
578
581
|
PassportErrorType["AUTHENTICATION_ERROR"] = "AUTHENTICATION_ERROR";
|
|
@@ -636,9 +639,9 @@ const getAuthConfiguration = ({ oidcConfiguration, authenticationDomain, }) => {
|
|
|
636
639
|
authorization_endpoint: `${authenticationDomain}/authorize`,
|
|
637
640
|
token_endpoint: `${authenticationDomain}/oauth/token`,
|
|
638
641
|
userinfo_endpoint: `${authenticationDomain}/userinfo`,
|
|
639
|
-
end_session_endpoint: `${authenticationDomain}/v2/logout`
|
|
640
|
-
`?returnTo=${encodeURIComponent(oidcConfiguration.logoutRedirectUri)}`
|
|
641
|
-
`&client_id=${oidcConfiguration.clientId}`,
|
|
642
|
+
end_session_endpoint: `${authenticationDomain}/v2/logout`
|
|
643
|
+
+ `?returnTo=${encodeURIComponent(oidcConfiguration.logoutRedirectUri)}`
|
|
644
|
+
+ `&client_id=${oidcConfiguration.clientId}`,
|
|
642
645
|
},
|
|
643
646
|
mergeClaims: true,
|
|
644
647
|
loadUserInfo: true,
|
|
@@ -658,7 +661,7 @@ class AuthManager {
|
|
|
658
661
|
this.config = config;
|
|
659
662
|
this.userManager = new UserManager(getAuthConfiguration(config));
|
|
660
663
|
}
|
|
661
|
-
mapOidcUserToDomainModel = (oidcUser) => {
|
|
664
|
+
static mapOidcUserToDomainModel = (oidcUser) => {
|
|
662
665
|
const passport = oidcUser.profile?.passport;
|
|
663
666
|
return {
|
|
664
667
|
expired: oidcUser.expired,
|
|
@@ -679,7 +682,7 @@ class AuthManager {
|
|
|
679
682
|
const oidcUser = await this.userManager.signinPopup({
|
|
680
683
|
popupWindowFeatures,
|
|
681
684
|
});
|
|
682
|
-
return
|
|
685
|
+
return AuthManager.mapOidcUserToDomainModel(oidcUser);
|
|
683
686
|
}, PassportErrorType.AUTHENTICATION_ERROR);
|
|
684
687
|
}
|
|
685
688
|
async loginCallback() {
|
|
@@ -698,7 +701,7 @@ class AuthManager {
|
|
|
698
701
|
if (!oidcUser) {
|
|
699
702
|
return null;
|
|
700
703
|
}
|
|
701
|
-
return
|
|
704
|
+
return AuthManager.mapOidcUserToDomainModel(oidcUser);
|
|
702
705
|
}, PassportErrorType.SILENT_LOGIN_ERROR);
|
|
703
706
|
}
|
|
704
707
|
async getUser() {
|
|
@@ -707,7 +710,7 @@ class AuthManager {
|
|
|
707
710
|
if (!oidcUser) {
|
|
708
711
|
return null;
|
|
709
712
|
}
|
|
710
|
-
return
|
|
713
|
+
return AuthManager.mapOidcUserToDomainModel(oidcUser);
|
|
711
714
|
}, PassportErrorType.NOT_LOGGED_IN_ERROR);
|
|
712
715
|
}
|
|
713
716
|
async requestRefreshTokenAfterRegistration() {
|
|
@@ -715,18 +718,18 @@ class AuthManager {
|
|
|
715
718
|
const updatedUser = await retryWithDelay(async () => {
|
|
716
719
|
const user = await this.userManager.signinSilent();
|
|
717
720
|
const passportMetadata = user?.profile?.passport;
|
|
718
|
-
const metadataExists = !!passportMetadata?.ether_key
|
|
719
|
-
!!passportMetadata?.stark_key
|
|
720
|
-
!!passportMetadata?.user_admin_key;
|
|
721
|
+
const metadataExists = !!passportMetadata?.ether_key
|
|
722
|
+
&& !!passportMetadata?.stark_key
|
|
723
|
+
&& !!passportMetadata?.user_admin_key;
|
|
721
724
|
if (metadataExists) {
|
|
722
725
|
return user;
|
|
723
726
|
}
|
|
724
|
-
return Promise.reject('user wallet addresses not exist');
|
|
727
|
+
return Promise.reject(new Error('user wallet addresses not exist'));
|
|
725
728
|
});
|
|
726
729
|
if (!updatedUser) {
|
|
727
730
|
return null;
|
|
728
731
|
}
|
|
729
|
-
return
|
|
732
|
+
return AuthManager.mapOidcUserToDomainModel(updatedUser);
|
|
730
733
|
}, PassportErrorType.REFRESH_TOKEN_ERROR);
|
|
731
734
|
}
|
|
732
735
|
}
|
|
@@ -765,12 +768,24 @@ var SendMessage;
|
|
|
765
768
|
})(SendMessage || (SendMessage = {}));
|
|
766
769
|
var TransactionTypes;
|
|
767
770
|
(function (TransactionTypes) {
|
|
771
|
+
// TODO: remove this once the naming has been fixed
|
|
772
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
768
773
|
TransactionTypes["CancelOrder"] = "delete:v1/orders";
|
|
774
|
+
// TODO: remove this once the naming has been fixed
|
|
775
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
769
776
|
TransactionTypes["CreateOrder"] = "post:v1/orders";
|
|
777
|
+
// TODO: remove this once the naming has been fixed
|
|
778
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
770
779
|
TransactionTypes["CreateTrade"] = "post:v1/trades";
|
|
780
|
+
// TODO: remove this once the naming has been fixed
|
|
781
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
771
782
|
TransactionTypes["CreateTransfer"] = "post:v1/transfers";
|
|
783
|
+
// TODO: remove this once the naming has been fixed
|
|
784
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
772
785
|
TransactionTypes["CreateBatchTransfer"] = "post:v2/transfers";
|
|
773
786
|
})(TransactionTypes || (TransactionTypes = {}));
|
|
787
|
+
// TODO: remove this once the naming has been fixed
|
|
788
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
774
789
|
const PassportEventType = 'imx_passport_confirmation';
|
|
775
790
|
|
|
776
791
|
const openPopupCenter = ({ url, title, width, height, }) => {
|
|
@@ -790,10 +805,10 @@ const openPopupCenter = ({ url, title, width, height, }) => {
|
|
|
790
805
|
return newWindow;
|
|
791
806
|
};
|
|
792
807
|
|
|
793
|
-
const
|
|
794
|
-
const
|
|
795
|
-
const
|
|
796
|
-
const
|
|
808
|
+
const CONFIRMATION_WINDOW_TITLE = 'Confirm this transaction';
|
|
809
|
+
const CONFIRMATION_WINDOW_HEIGHT = 380;
|
|
810
|
+
const CONFIRMATION_WINDOW_WIDTH = 480;
|
|
811
|
+
const CONFIRMATION_WINDOW_CLOSED_POLLING_DURATION = 1000;
|
|
797
812
|
class ConfirmationScreen {
|
|
798
813
|
config;
|
|
799
814
|
constructor(config) {
|
|
@@ -809,12 +824,14 @@ class ConfirmationScreen {
|
|
|
809
824
|
startTransaction(accessToken, transaction, popupOptions) {
|
|
810
825
|
return new Promise((resolve, reject) => {
|
|
811
826
|
const messageHandler = ({ data, origin }) => {
|
|
812
|
-
if (origin
|
|
813
|
-
data.eventType
|
|
827
|
+
if (origin !== this.config.passportDomain
|
|
828
|
+
|| data.eventType !== PassportEventType) {
|
|
814
829
|
return;
|
|
815
830
|
}
|
|
816
831
|
switch (data.messageType) {
|
|
817
832
|
case ReceiveMessage.CONFIRMATION_WINDOW_READY: {
|
|
833
|
+
// TODO: remove once fixed
|
|
834
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
818
835
|
this.postMessage(confirmationWindow, accessToken, {
|
|
819
836
|
messageType: SendMessage.TRANSACTION_START,
|
|
820
837
|
messageData: transaction,
|
|
@@ -836,87 +853,87 @@ class ConfirmationScreen {
|
|
|
836
853
|
window.addEventListener('message', messageHandler);
|
|
837
854
|
const confirmationWindow = openPopupCenter({
|
|
838
855
|
url: `${this.config.passportDomain}/transaction-confirmation`,
|
|
839
|
-
title:
|
|
840
|
-
width: popupOptions?.width ||
|
|
841
|
-
height: popupOptions?.height ||
|
|
856
|
+
title: CONFIRMATION_WINDOW_TITLE,
|
|
857
|
+
width: popupOptions?.width || CONFIRMATION_WINDOW_WIDTH,
|
|
858
|
+
height: popupOptions?.height || CONFIRMATION_WINDOW_HEIGHT,
|
|
842
859
|
});
|
|
843
860
|
// https://stackoverflow.com/questions/9388380/capture-the-close-event-of-popup-window-in-javascript/48240128#48240128
|
|
844
|
-
const timer = setInterval(
|
|
861
|
+
const timer = setInterval(() => {
|
|
845
862
|
if (confirmationWindow.closed) {
|
|
846
863
|
clearInterval(timer);
|
|
847
864
|
window.removeEventListener('message', messageHandler);
|
|
848
865
|
resolve({ confirmed: false });
|
|
849
866
|
}
|
|
850
|
-
},
|
|
867
|
+
}, CONFIRMATION_WINDOW_CLOSED_POLLING_DURATION);
|
|
851
868
|
});
|
|
852
869
|
}
|
|
853
870
|
}
|
|
854
871
|
|
|
855
872
|
const ERC721$1 = 'ERC721';
|
|
856
|
-
const transfer = ({ request, transfersApi, starkSigner, user, passportConfig,
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
873
|
+
const transfer = ({ request, transfersApi, starkSigner, user, passportConfig,
|
|
874
|
+
// TODO: remove this eslint disable once we have a better solution
|
|
875
|
+
// eslint-disable-next-line max-len
|
|
876
|
+
}) => withPassportError(async () => {
|
|
877
|
+
const transferAmount = request.type === ERC721$1 ? '1' : request.amount;
|
|
878
|
+
const getSignableTransferRequest = {
|
|
879
|
+
sender: user.etherKey,
|
|
880
|
+
token: convertToSignableToken(request),
|
|
881
|
+
amount: transferAmount,
|
|
882
|
+
receiver: request.receiver,
|
|
883
|
+
};
|
|
884
|
+
const signableResult = await transfersApi.getSignableTransferV1({
|
|
885
|
+
getSignableTransferRequest,
|
|
886
|
+
});
|
|
887
|
+
const confirmationScreen = new ConfirmationScreen(passportConfig);
|
|
888
|
+
const confirmationResult = await confirmationScreen.startTransaction(user.accessToken, {
|
|
889
|
+
transactionType: TransactionTypes.CreateTransfer,
|
|
890
|
+
transactionData: getSignableTransferRequest,
|
|
891
|
+
});
|
|
892
|
+
if (!confirmationResult.confirmed) {
|
|
893
|
+
throw new Error('Transaction rejected by user');
|
|
894
|
+
}
|
|
895
|
+
const signableResultData = signableResult.data;
|
|
896
|
+
const { payload_hash: payloadHash } = signableResultData;
|
|
897
|
+
const starkSignature = await starkSigner.signMessage(payloadHash);
|
|
898
|
+
const senderStarkKey = await starkSigner.getAddress();
|
|
899
|
+
const transferSigningParams = {
|
|
900
|
+
sender_stark_key: signableResultData.sender_stark_key || senderStarkKey,
|
|
901
|
+
sender_vault_id: signableResultData.sender_vault_id,
|
|
902
|
+
receiver_stark_key: signableResultData.receiver_stark_key,
|
|
903
|
+
receiver_vault_id: signableResultData.receiver_vault_id,
|
|
904
|
+
asset_id: signableResultData.asset_id,
|
|
905
|
+
amount: signableResultData.amount,
|
|
906
|
+
nonce: signableResultData.nonce,
|
|
907
|
+
expiration_timestamp: signableResultData.expiration_timestamp,
|
|
908
|
+
stark_signature: starkSignature,
|
|
909
|
+
};
|
|
910
|
+
const createTransferRequest = {
|
|
911
|
+
createTransferRequest: transferSigningParams,
|
|
912
|
+
};
|
|
913
|
+
const headers = {
|
|
914
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
915
|
+
Authorization: `Bearer ${user.accessToken}`,
|
|
916
|
+
};
|
|
917
|
+
const { data: responseData } = await transfersApi.createTransferV1(createTransferRequest, { headers });
|
|
918
|
+
return {
|
|
919
|
+
sent_signature: responseData.sent_signature,
|
|
920
|
+
status: responseData.status?.toString(),
|
|
921
|
+
time: responseData.time,
|
|
922
|
+
transfer_id: responseData.transfer_id,
|
|
923
|
+
};
|
|
924
|
+
}, PassportErrorType.TRANSFER_ERROR);
|
|
906
925
|
async function batchNftTransfer({ user, starkSigner, request, transfersApi, passportConfig, }) {
|
|
907
926
|
return withPassportError(async () => {
|
|
908
927
|
const ethAddress = user.etherKey;
|
|
909
|
-
const signableRequests = request.map((nftTransfer) => {
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
};
|
|
919
|
-
});
|
|
928
|
+
const signableRequests = request.map((nftTransfer) => ({
|
|
929
|
+
amount: '1',
|
|
930
|
+
token: convertToSignableToken({
|
|
931
|
+
type: ERC721$1,
|
|
932
|
+
tokenId: nftTransfer.tokenId,
|
|
933
|
+
tokenAddress: nftTransfer.tokenAddress,
|
|
934
|
+
}),
|
|
935
|
+
receiver: nftTransfer.receiver,
|
|
936
|
+
}));
|
|
920
937
|
const getSignableTransferRequestV2 = {
|
|
921
938
|
sender_ether_key: ethAddress,
|
|
922
939
|
signable_requests: signableRequests,
|
|
@@ -951,7 +968,8 @@ async function batchNftTransfer({ user, starkSigner, request, transfersApi, pass
|
|
|
951
968
|
requests,
|
|
952
969
|
};
|
|
953
970
|
const headers = {
|
|
954
|
-
|
|
971
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
972
|
+
Authorization: `Bearer ${user.accessToken}`,
|
|
955
973
|
};
|
|
956
974
|
const response = await transfersApi.createTransfer({
|
|
957
975
|
createTransferRequestV2: transferSigningParams,
|
|
@@ -1008,7 +1026,8 @@ async function createOrder({ starkSigner, user, request, ordersApi, passportConf
|
|
|
1008
1026
|
},
|
|
1009
1027
|
};
|
|
1010
1028
|
const headers = {
|
|
1011
|
-
|
|
1029
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1030
|
+
Authorization: `Bearer ${user.accessToken}`,
|
|
1012
1031
|
};
|
|
1013
1032
|
const createOrderResponse = await ordersApi.createOrder(orderParams, {
|
|
1014
1033
|
headers,
|
|
@@ -1037,7 +1056,8 @@ async function cancelOrder({ user, starkSigner, request, ordersApi, passportConf
|
|
|
1037
1056
|
const { payload_hash: payloadHash } = getSignableCancelOrderResponse.data;
|
|
1038
1057
|
const starkSignature = await starkSigner.signMessage(payloadHash);
|
|
1039
1058
|
const headers = {
|
|
1040
|
-
|
|
1059
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1060
|
+
Authorization: `Bearer ${user.accessToken}`,
|
|
1041
1061
|
};
|
|
1042
1062
|
const cancelOrderResponse = await ordersApi.cancelOrder({
|
|
1043
1063
|
id: request.order_id.toString(),
|
|
@@ -1081,7 +1101,8 @@ async function exchangeTransfer({ user, starkSigner, request, exchangesApi, }) {
|
|
|
1081
1101
|
stark_signature: starkSignature,
|
|
1082
1102
|
};
|
|
1083
1103
|
const headers = {
|
|
1084
|
-
|
|
1104
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1105
|
+
Authorization: `Bearer ${user.accessToken}`,
|
|
1085
1106
|
};
|
|
1086
1107
|
const response = await exchangesApi.createExchangeTransfer({
|
|
1087
1108
|
id: request.transactionID,
|
|
@@ -1137,7 +1158,8 @@ async function createTrade({ request, tradesApi, user, starkSigner, passportConf
|
|
|
1137
1158
|
vault_id_sell: signableResultData.vault_id_sell,
|
|
1138
1159
|
},
|
|
1139
1160
|
};
|
|
1140
|
-
|
|
1161
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1162
|
+
const headers = { Authorization: `Bearer ${user.accessToken}` };
|
|
1141
1163
|
const { data: createTradeResponse } = await tradesApi.createTradeV3(tradeParams, {
|
|
1142
1164
|
headers,
|
|
1143
1165
|
});
|
|
@@ -1165,9 +1187,13 @@ class PassportImxProvider {
|
|
|
1165
1187
|
passportConfig: this.passportConfig,
|
|
1166
1188
|
});
|
|
1167
1189
|
}
|
|
1190
|
+
// TODO: Remove once implemented
|
|
1191
|
+
// eslint-disable-next-line class-methods-use-this
|
|
1168
1192
|
registerOffchain() {
|
|
1169
1193
|
throw new PassportError('Operation not supported', PassportErrorType.OPERATION_NOT_SUPPORTED_ERROR);
|
|
1170
1194
|
}
|
|
1195
|
+
// TODO: Remove once implemented
|
|
1196
|
+
// eslint-disable-next-line class-methods-use-this
|
|
1171
1197
|
isRegisteredOnchain() {
|
|
1172
1198
|
throw new PassportError('Operation not supported', PassportErrorType.OPERATION_NOT_SUPPORTED_ERROR);
|
|
1173
1199
|
}
|
|
@@ -1215,14 +1241,18 @@ class PassportImxProvider {
|
|
|
1215
1241
|
exchangesApi: this.immutableXClient.exchangeApi,
|
|
1216
1242
|
});
|
|
1217
1243
|
}
|
|
1218
|
-
//
|
|
1244
|
+
// TODO: Remove once implemented
|
|
1245
|
+
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
|
|
1219
1246
|
deposit(deposit) {
|
|
1220
1247
|
throw new PassportError('Operation not supported', PassportErrorType.OPERATION_NOT_SUPPORTED_ERROR);
|
|
1221
1248
|
}
|
|
1222
|
-
//
|
|
1249
|
+
// TODO: Remove once implemented
|
|
1250
|
+
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
|
|
1223
1251
|
prepareWithdrawal(request) {
|
|
1224
1252
|
throw new PassportError('Operation not supported', PassportErrorType.OPERATION_NOT_SUPPORTED_ERROR);
|
|
1225
1253
|
}
|
|
1254
|
+
// TODO: Remove once implemented
|
|
1255
|
+
// eslint-disable-next-line class-methods-use-this
|
|
1226
1256
|
completeWithdrawal(
|
|
1227
1257
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1228
1258
|
starkPublicKey,
|
|
@@ -1285,20 +1315,21 @@ class PassportConfiguration {
|
|
|
1285
1315
|
}
|
|
1286
1316
|
else {
|
|
1287
1317
|
switch (baseConfig.environment) {
|
|
1288
|
-
case Environment.
|
|
1289
|
-
this.network = Networks.
|
|
1318
|
+
case Environment.PRODUCTION: {
|
|
1319
|
+
this.network = Networks.PRODUCTION;
|
|
1290
1320
|
this.authenticationDomain = 'https://auth.immutable.com';
|
|
1291
1321
|
this.magicPublishableApiKey = 'pk_live_10F423798A540ED7';
|
|
1292
1322
|
this.magicProviderId = 'fSMzaRQ4O7p4fttl7pCyGVtJS_G70P8SNsLXtPPGHo0=';
|
|
1293
|
-
this.passportDomain = 'https://passport.
|
|
1323
|
+
this.passportDomain = 'https://passport.immutable.com';
|
|
1294
1324
|
break;
|
|
1295
1325
|
}
|
|
1296
|
-
case Environment.
|
|
1297
|
-
|
|
1326
|
+
case Environment.SANDBOX:
|
|
1327
|
+
default: {
|
|
1328
|
+
this.network = Networks.SANDBOX;
|
|
1298
1329
|
this.authenticationDomain = 'https://auth.immutable.com';
|
|
1299
1330
|
this.magicPublishableApiKey = 'pk_live_10F423798A540ED7';
|
|
1300
1331
|
this.magicProviderId = 'fSMzaRQ4O7p4fttl7pCyGVtJS_G70P8SNsLXtPPGHo0=';
|
|
1301
|
-
this.passportDomain = 'https://passport.immutable.com';
|
|
1332
|
+
this.passportDomain = 'https://passport.sandbox.immutable.com';
|
|
1302
1333
|
break;
|
|
1303
1334
|
}
|
|
1304
1335
|
}
|
|
@@ -1306,12 +1337,12 @@ class PassportConfiguration {
|
|
|
1306
1337
|
}
|
|
1307
1338
|
}
|
|
1308
1339
|
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
};
|
|
1340
|
+
// TODO: remove this eslint disable once we have a better solution
|
|
1341
|
+
// eslint-disable-next-line max-len
|
|
1342
|
+
const getStarkSigner = async (signer) => withPassportError(async () => {
|
|
1343
|
+
const privateKey = await generateLegacyStarkPrivateKey(signer);
|
|
1344
|
+
return createStarkSigner(privateKey);
|
|
1345
|
+
}, PassportErrorType.WALLET_CONNECTION_ERROR);
|
|
1315
1346
|
|
|
1316
1347
|
async function registerPassport({ ethSigner, starkSigner, usersApi }, authorization) {
|
|
1317
1348
|
return withPassportError(async () => {
|
|
@@ -1327,7 +1358,7 @@ async function registerPassport({ ethSigner, starkSigner, usersApi }, authorizat
|
|
|
1327
1358
|
const ethSignature = await signRaw(signableMessage, ethSigner);
|
|
1328
1359
|
const starkSignature = await starkSigner.signMessage(payloadHash);
|
|
1329
1360
|
const response = await usersApi.registerPassportUser({
|
|
1330
|
-
authorization: `Bearer
|
|
1361
|
+
authorization: `Bearer ${authorization}`,
|
|
1331
1362
|
registerPassportUserRequest: {
|
|
1332
1363
|
eth_signature: ethSignature,
|
|
1333
1364
|
ether_key: userAddress,
|
|
@@ -1339,6 +1370,7 @@ async function registerPassport({ ethSigner, starkSigner, usersApi }, authorizat
|
|
|
1339
1370
|
}, PassportErrorType.USER_REGISTRATION_ERROR);
|
|
1340
1371
|
}
|
|
1341
1372
|
|
|
1373
|
+
// TODO: Remove this once the dependency has been added
|
|
1342
1374
|
class Passport {
|
|
1343
1375
|
authManager;
|
|
1344
1376
|
magicAdapter;
|
|
@@ -1348,11 +1380,10 @@ class Passport {
|
|
|
1348
1380
|
this.config = new PassportConfiguration(passportModuleConfiguration);
|
|
1349
1381
|
this.authManager = new AuthManager(this.config);
|
|
1350
1382
|
this.magicAdapter = new MagicAdapter(this.config);
|
|
1351
|
-
this.immutableXClient =
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
});
|
|
1383
|
+
this.immutableXClient = passportModuleConfiguration.overrides?.immutableXClient
|
|
1384
|
+
|| new ImmutableXClient({
|
|
1385
|
+
baseConfig: passportModuleConfiguration.baseConfig,
|
|
1386
|
+
});
|
|
1356
1387
|
}
|
|
1357
1388
|
async getImxProvider(user) {
|
|
1358
1389
|
if (!user || !user.idToken) {
|