@imtbl/wallet 2.10.7-alpha.2
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/.eslintrc.cjs +18 -0
- package/LICENSE.md +176 -0
- package/dist/browser/index.mjs +21 -0
- package/dist/node/index.js +71 -0
- package/dist/node/index.mjs +22 -0
- package/dist/types/config.d.ts +13 -0
- package/dist/types/errors.d.ts +14 -0
- package/dist/types/guardian/index.d.ts +57 -0
- package/dist/types/index.d.ts +12 -0
- package/dist/types/magic/index.d.ts +1 -0
- package/dist/types/magic/magicTEESigner.d.ts +24 -0
- package/dist/types/network/chains.d.ts +32 -0
- package/dist/types/network/constants.d.ts +3 -0
- package/dist/types/network/retry.d.ts +8 -0
- package/dist/types/provider/eip6963.d.ts +3 -0
- package/dist/types/types.d.ts +163 -0
- package/dist/types/utils/metrics.d.ts +3 -0
- package/dist/types/utils/string.d.ts +1 -0
- package/dist/types/utils/typedEventEmitter.d.ts +6 -0
- package/dist/types/zkEvm/JsonRpcError.d.ts +25 -0
- package/dist/types/zkEvm/index.d.ts +2 -0
- package/dist/types/zkEvm/personalSign.d.ts +15 -0
- package/dist/types/zkEvm/provider/eip6963.d.ts +3 -0
- package/dist/types/zkEvm/relayerClient.d.ts +60 -0
- package/dist/types/zkEvm/sendDeployTransactionAndPersonalSign.d.ts +6 -0
- package/dist/types/zkEvm/sendTransaction.d.ts +6 -0
- package/dist/types/zkEvm/sessionActivity/errorBoundary.d.ts +1 -0
- package/dist/types/zkEvm/sessionActivity/request.d.ts +15 -0
- package/dist/types/zkEvm/sessionActivity/sessionActivity.d.ts +2 -0
- package/dist/types/zkEvm/signEjectionTransaction.d.ts +6 -0
- package/dist/types/zkEvm/signTypedDataV4.d.ts +14 -0
- package/dist/types/zkEvm/transactionHelpers.d.ts +31 -0
- package/dist/types/zkEvm/types.d.ts +120 -0
- package/dist/types/zkEvm/user/index.d.ts +1 -0
- package/dist/types/zkEvm/user/registerZkEvmUser.d.ts +13 -0
- package/dist/types/zkEvm/walletHelpers.d.ts +33 -0
- package/dist/types/zkEvm/zkEvmProvider.d.ts +25 -0
- package/package.json +55 -0
- package/src/config.ts +51 -0
- package/src/errors.ts +33 -0
- package/src/guardian/index.ts +358 -0
- package/src/index.ts +27 -0
- package/src/magic/index.ts +1 -0
- package/src/magic/magicTEESigner.ts +214 -0
- package/src/network/chains.ts +33 -0
- package/src/network/constants.ts +28 -0
- package/src/network/retry.ts +37 -0
- package/src/provider/eip6963.ts +25 -0
- package/src/types.ts +192 -0
- package/src/utils/metrics.ts +57 -0
- package/src/utils/string.ts +12 -0
- package/src/utils/typedEventEmitter.ts +26 -0
- package/src/zkEvm/JsonRpcError.ts +33 -0
- package/src/zkEvm/index.ts +2 -0
- package/src/zkEvm/personalSign.ts +62 -0
- package/src/zkEvm/provider/eip6963.ts +25 -0
- package/src/zkEvm/relayerClient.ts +216 -0
- package/src/zkEvm/sendDeployTransactionAndPersonalSign.ts +44 -0
- package/src/zkEvm/sendTransaction.ts +34 -0
- package/src/zkEvm/sessionActivity/errorBoundary.ts +33 -0
- package/src/zkEvm/sessionActivity/request.ts +62 -0
- package/src/zkEvm/sessionActivity/sessionActivity.ts +140 -0
- package/src/zkEvm/signEjectionTransaction.ts +33 -0
- package/src/zkEvm/signTypedDataV4.ts +103 -0
- package/src/zkEvm/transactionHelpers.ts +295 -0
- package/src/zkEvm/types.ts +136 -0
- package/src/zkEvm/user/index.ts +1 -0
- package/src/zkEvm/user/registerZkEvmUser.ts +75 -0
- package/src/zkEvm/walletHelpers.ts +243 -0
- package/src/zkEvm/zkEvmProvider.ts +453 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { AbstractSigner, Signer } from 'ethers';
|
|
2
|
+
import { MagicTeeApiClients } from '@imtbl/generated-clients';
|
|
3
|
+
import { isAxiosError } from 'axios';
|
|
4
|
+
import { Flow, trackDuration } from '@imtbl/metrics';
|
|
5
|
+
import { WalletError, WalletErrorType } from '../errors';
|
|
6
|
+
import { AuthManager } from '@imtbl/auth';
|
|
7
|
+
import { withMetricsAsync } from '../utils/metrics';
|
|
8
|
+
import { isUserImx, isUserZkEvm, User } from '../types';
|
|
9
|
+
|
|
10
|
+
const CHAIN_IDENTIFIER = 'ETH';
|
|
11
|
+
|
|
12
|
+
interface UserWallet {
|
|
13
|
+
userIdentifier: string;
|
|
14
|
+
walletAddress: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default class MagicTEESigner extends AbstractSigner {
|
|
18
|
+
private readonly authManager: AuthManager;
|
|
19
|
+
|
|
20
|
+
private readonly magicTeeApiClient: MagicTeeApiClients;
|
|
21
|
+
|
|
22
|
+
private userWallet: UserWallet | null = null;
|
|
23
|
+
|
|
24
|
+
private createWalletPromise: Promise<UserWallet> | null = null;
|
|
25
|
+
|
|
26
|
+
constructor(authManager: AuthManager, magicTeeApiClient: MagicTeeApiClients) {
|
|
27
|
+
super();
|
|
28
|
+
this.authManager = authManager;
|
|
29
|
+
this.magicTeeApiClient = magicTeeApiClient;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private async getUserWallet(): Promise<UserWallet> {
|
|
33
|
+
let { userWallet } = this;
|
|
34
|
+
if (!userWallet) {
|
|
35
|
+
userWallet = await this.createWallet();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Check if the user has changed since the last createWallet request was made. If so, initialise the new user's wallet.
|
|
39
|
+
const user = await this.getUserOrThrow();
|
|
40
|
+
if (user.profile.sub !== userWallet.userIdentifier) {
|
|
41
|
+
userWallet = await this.createWallet(user);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (isUserImx(user) && user.imx.userAdminAddress.toLowerCase() !== userWallet.walletAddress.toLowerCase()) {
|
|
45
|
+
throw new WalletError(
|
|
46
|
+
'Wallet address mismatch.'
|
|
47
|
+
+ `Rollup: IMX, TEE address: ${userWallet.walletAddress}, profile address: ${user.imx.userAdminAddress}`,
|
|
48
|
+
WalletErrorType.WALLET_CONNECTION_ERROR,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (isUserZkEvm(user) && user.zkEvm.userAdminAddress.toLowerCase() !== userWallet.walletAddress.toLowerCase()) {
|
|
53
|
+
throw new WalletError(
|
|
54
|
+
'Wallet address mismatch.'
|
|
55
|
+
+ `Rollup: zkEVM, TEE address: ${userWallet.walletAddress}, profile address: ${user.zkEvm.userAdminAddress}`,
|
|
56
|
+
WalletErrorType.WALLET_CONNECTION_ERROR,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return userWallet;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* This method calls the createWallet endpoint. The user's wallet must be created before it can be used to sign messages.
|
|
65
|
+
* The createWallet endpoint is idempotent, so it can be called multiple times without causing an error.
|
|
66
|
+
* If a createWallet request is already in flight, return the existing promise.
|
|
67
|
+
*/
|
|
68
|
+
private async createWallet(user?: User): Promise<UserWallet> {
|
|
69
|
+
if (this.createWalletPromise) return this.createWalletPromise;
|
|
70
|
+
|
|
71
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
72
|
+
this.createWalletPromise = new Promise(async (resolve, reject) => {
|
|
73
|
+
try {
|
|
74
|
+
this.userWallet = null;
|
|
75
|
+
|
|
76
|
+
const authenticatedUser = user || await this.getUserOrThrow();
|
|
77
|
+
const headers = MagicTEESigner.getHeaders(authenticatedUser);
|
|
78
|
+
|
|
79
|
+
await withMetricsAsync(async (flow: Flow) => {
|
|
80
|
+
try {
|
|
81
|
+
const startTime = performance.now();
|
|
82
|
+
// The createWallet endpoint is idempotent, so it can be called multiple times without causing an error.
|
|
83
|
+
const response = await this.magicTeeApiClient.walletApi.createWalletV1WalletPost(
|
|
84
|
+
{
|
|
85
|
+
xMagicChain: CHAIN_IDENTIFIER,
|
|
86
|
+
},
|
|
87
|
+
{ headers },
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
trackDuration(
|
|
91
|
+
'passport',
|
|
92
|
+
flow.details.flowName,
|
|
93
|
+
Math.round(performance.now() - startTime),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
this.userWallet = {
|
|
97
|
+
userIdentifier: authenticatedUser.profile.sub,
|
|
98
|
+
walletAddress: response.data.public_address,
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
return resolve(this.userWallet);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
let errorMessage: string = 'MagicTEE: Failed to initialise EOA';
|
|
104
|
+
|
|
105
|
+
if (isAxiosError(error)) {
|
|
106
|
+
if (error.response) {
|
|
107
|
+
errorMessage += ` with status ${error.response.status}: ${JSON.stringify(error.response.data)}`;
|
|
108
|
+
} else {
|
|
109
|
+
errorMessage += `: ${error.message}`;
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
errorMessage += `: ${(error as Error).message}`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return reject(new Error(errorMessage));
|
|
116
|
+
}
|
|
117
|
+
}, 'magicCreateWallet');
|
|
118
|
+
} catch (error) {
|
|
119
|
+
reject(error);
|
|
120
|
+
} finally {
|
|
121
|
+
this.createWalletPromise = null;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
return this.createWalletPromise;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private async getUserOrThrow(): Promise<User> {
|
|
129
|
+
const user = await this.authManager.getUser();
|
|
130
|
+
if (!user) {
|
|
131
|
+
throw new WalletError(
|
|
132
|
+
'User has been logged out',
|
|
133
|
+
WalletErrorType.NOT_LOGGED_IN_ERROR,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
return user;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private static getHeaders(user: User): Record<string, string> {
|
|
140
|
+
if (!user) {
|
|
141
|
+
throw new WalletError(
|
|
142
|
+
'User has been logged out',
|
|
143
|
+
WalletErrorType.NOT_LOGGED_IN_ERROR,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
Authorization: `Bearer ${user.idToken}`,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
public async getAddress(): Promise<string> {
|
|
152
|
+
const userWallet = await this.getUserWallet();
|
|
153
|
+
return userWallet.walletAddress;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public async signMessage(message: string | Uint8Array): Promise<string> {
|
|
157
|
+
// Call getUserWallet to ensure that the createWallet endpoint has been called at least once,
|
|
158
|
+
// as this is a prerequisite for signing messages.
|
|
159
|
+
await this.getUserWallet();
|
|
160
|
+
|
|
161
|
+
const messageToSign = message instanceof Uint8Array ? `0x${Buffer.from(message).toString('hex')}` : message;
|
|
162
|
+
const user = await this.getUserOrThrow();
|
|
163
|
+
const headers = await MagicTEESigner.getHeaders(user);
|
|
164
|
+
|
|
165
|
+
return withMetricsAsync(async (flow: Flow) => {
|
|
166
|
+
try {
|
|
167
|
+
const startTime = performance.now();
|
|
168
|
+
const response = await this.magicTeeApiClient.signOperationsApi.signMessageV1WalletSignMessagePost({
|
|
169
|
+
signMessageRequest: {
|
|
170
|
+
message_base64: Buffer.from(messageToSign, 'utf-8').toString('base64'),
|
|
171
|
+
},
|
|
172
|
+
xMagicChain: CHAIN_IDENTIFIER,
|
|
173
|
+
}, { headers });
|
|
174
|
+
|
|
175
|
+
trackDuration(
|
|
176
|
+
'passport',
|
|
177
|
+
flow.details.flowName,
|
|
178
|
+
Math.round(performance.now() - startTime),
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
return response.data.signature;
|
|
182
|
+
} catch (error) {
|
|
183
|
+
let errorMessage: string = 'MagicTEE: Failed to sign message using EOA';
|
|
184
|
+
|
|
185
|
+
if (isAxiosError(error)) {
|
|
186
|
+
if (error.response) {
|
|
187
|
+
errorMessage += ` with status ${error.response.status}: ${JSON.stringify(error.response.data)}`;
|
|
188
|
+
} else {
|
|
189
|
+
errorMessage += `: ${error.message}`;
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
errorMessage += `: ${(error as Error).message}`;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
throw new Error(errorMessage);
|
|
196
|
+
}
|
|
197
|
+
}, 'magicSignMessage');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// eslint-disable-next-line class-methods-use-this
|
|
201
|
+
connect(): Signer {
|
|
202
|
+
throw new Error('Method not implemented.');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// eslint-disable-next-line class-methods-use-this
|
|
206
|
+
signTransaction(): Promise<string> {
|
|
207
|
+
throw new Error('Method not implemented.');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// eslint-disable-next-line class-methods-use-this
|
|
211
|
+
signTypedData(): Promise<string> {
|
|
212
|
+
throw new Error('Method not implemented.');
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enum representing different chain IDs.
|
|
3
|
+
* @enum {number}
|
|
4
|
+
* @property {number} IMTBL_ZKEVM_MAINNET - The chain ID for IMTBL ZKEVM Mainnet.
|
|
5
|
+
* @property {number} IMTBL_ZKEVM_TESTNET - The chain ID for IMTBL ZKEVM Testnet.
|
|
6
|
+
* @property {number} IMTBL_ZKEVM_DEVNET - The chain ID for IMTBL ZKEVM Devnet.
|
|
7
|
+
* @property {number} ETHEREUM - The chain ID for Ethereum.
|
|
8
|
+
* @property {number} SEPOLIA - The chain ID for Sepolia.
|
|
9
|
+
*/
|
|
10
|
+
export enum ChainId {
|
|
11
|
+
IMTBL_ZKEVM_MAINNET = 13371,
|
|
12
|
+
IMTBL_ZKEVM_TESTNET = 13473,
|
|
13
|
+
IMTBL_ZKEVM_DEVNET = 15003,
|
|
14
|
+
ETHEREUM = 1,
|
|
15
|
+
SEPOLIA = 11155111,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Enum representing different chain names.
|
|
20
|
+
* @enum {number}
|
|
21
|
+
* @property {number} IMTBL_ZKEVM_MAINNET - The chain name for IMTBL ZKEVM Mainnet.
|
|
22
|
+
* @property {number} IMTBL_ZKEVM_TESTNET - The chain name for IMTBL ZKEVM Testnet.
|
|
23
|
+
* @property {number} IMTBL_ZKEVM_DEVNET - The chain name for IMTBL ZKEVM Devnet.
|
|
24
|
+
* @property {number} ETHEREUM - The chain name for Ethereum.
|
|
25
|
+
* @property {number} SEPOLIA - The chain name for Sepolia.
|
|
26
|
+
*/
|
|
27
|
+
export enum ChainName {
|
|
28
|
+
ETHEREUM = 'Ethereum',
|
|
29
|
+
SEPOLIA = 'Sepolia',
|
|
30
|
+
IMTBL_ZKEVM_TESTNET = 'Immutable zkEVM Test',
|
|
31
|
+
IMTBL_ZKEVM_DEVNET = 'Immutable zkEVM Dev',
|
|
32
|
+
IMTBL_ZKEVM_MAINNET = 'Immutable zkEVM',
|
|
33
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ChainId, ChainName } from './chains';
|
|
2
|
+
|
|
3
|
+
export type ChainMap = Map<ChainId, ChainName>;
|
|
4
|
+
export const CHAIN_NAME_MAP: ChainMap = new Map<
|
|
5
|
+
ChainId,
|
|
6
|
+
ChainName
|
|
7
|
+
>([
|
|
8
|
+
[
|
|
9
|
+
ChainId.ETHEREUM,
|
|
10
|
+
ChainName.ETHEREUM,
|
|
11
|
+
],
|
|
12
|
+
[
|
|
13
|
+
ChainId.SEPOLIA,
|
|
14
|
+
ChainName.SEPOLIA,
|
|
15
|
+
],
|
|
16
|
+
[
|
|
17
|
+
ChainId.IMTBL_ZKEVM_MAINNET,
|
|
18
|
+
ChainName.IMTBL_ZKEVM_MAINNET,
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
ChainId.IMTBL_ZKEVM_TESTNET,
|
|
22
|
+
ChainName.IMTBL_ZKEVM_TESTNET,
|
|
23
|
+
],
|
|
24
|
+
[
|
|
25
|
+
ChainId.IMTBL_ZKEVM_DEVNET,
|
|
26
|
+
ChainName.IMTBL_ZKEVM_DEVNET,
|
|
27
|
+
],
|
|
28
|
+
]);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const POLL_INTERVAL = 1 * 1000; // every 1 second
|
|
2
|
+
export const MAX_RETRIES = 3;
|
|
3
|
+
|
|
4
|
+
export type RetryOption = {
|
|
5
|
+
retries?: number;
|
|
6
|
+
interval?: number;
|
|
7
|
+
finalErr?: Error;
|
|
8
|
+
finallyFn?: () => void;
|
|
9
|
+
};
|
|
10
|
+
const wait = (ms: number) => new Promise<void>((resolve) => {
|
|
11
|
+
setTimeout(() => resolve(), ms);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
export const retryWithDelay = async <T>(
|
|
15
|
+
fn: () => Promise<T>,
|
|
16
|
+
options?: RetryOption,
|
|
17
|
+
): Promise<T> => {
|
|
18
|
+
const {
|
|
19
|
+
retries = MAX_RETRIES,
|
|
20
|
+
interval = POLL_INTERVAL,
|
|
21
|
+
finalErr = Error('Retry failed'),
|
|
22
|
+
finallyFn = () => {},
|
|
23
|
+
} = options || {};
|
|
24
|
+
try {
|
|
25
|
+
return await fn();
|
|
26
|
+
} catch (err) {
|
|
27
|
+
if (retries <= 0) {
|
|
28
|
+
return Promise.reject(finalErr);
|
|
29
|
+
}
|
|
30
|
+
await wait(interval);
|
|
31
|
+
return retryWithDelay(fn, { retries: retries - 1, finalErr, finallyFn });
|
|
32
|
+
} finally {
|
|
33
|
+
if (retries <= 0) {
|
|
34
|
+
finallyFn();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
+
import { EIP6963ProviderDetail, EIP6963ProviderInfo } from '../types';
|
|
3
|
+
|
|
4
|
+
export const passportProviderInfo = {
|
|
5
|
+
// eslint-disable-next-line max-len
|
|
6
|
+
icon: 'data:image/svg+xml,<svg viewBox="0 0 48 48" class="SvgIcon undefined Logo Logo--PassportSymbolOutlined css-1dn9atd" xmlns="http://www.w3.org/2000/svg"><g data-testid="undefined__g"><circle cx="24" cy="24" r="22.5" fill="url(%23paint0_radial_6324_83922)"></circle><circle cx="24" cy="24" r="22.5" fill="url(%23paint1_radial_6324_83922)"></circle><path d="M24 0C10.7452 0 0 10.7452 0 24C0 37.2548 10.7452 48 24 48C37.2548 48 48 37.2548 48 24C48 10.7452 37.2548 0 24 0ZM23.0718 9.16608C23.7383 8.83951 24.4406 8.86188 25.087 9.2287C27.3282 10.5059 29.5627 11.7942 31.786 13.096C32.5018 13.5165 32.8686 14.1897 32.8708 15.0173C32.8843 17.9184 32.8798 20.8171 32.8708 23.7182C32.8708 23.8255 32.8015 23.9821 32.7143 24.0335C31.8531 24.548 30.9808 25.0423 30.0347 25.5881V25.1318C30.0347 22.148 30.0257 19.1664 30.0414 16.1827C30.0436 15.6101 29.8468 15.241 29.339 14.9525C26.7377 13.474 24.1499 11.9687 21.5575 10.4723C21.4457 10.4075 21.3361 10.3381 21.1661 10.2352C21.8326 9.85722 22.4321 9.47698 23.0673 9.16608H23.0718ZM22.5953 38.8451C22.45 38.7713 22.3426 38.7198 22.2375 38.6595C18.8041 36.68 15.3752 34.687 11.9307 32.7232C10.9644 32.173 10.5238 31.3879 10.5349 30.2852C10.5551 27.9411 10.5484 25.597 10.5372 23.2507C10.5327 22.1927 10.9622 21.4255 11.8926 20.8977C14.3105 19.5221 16.715 18.1264 19.1195 16.7284C19.3275 16.6076 19.4796 16.5875 19.6965 16.7172C20.5264 17.216 21.3719 17.6924 22.2554 18.2024C22.0876 18.3031 21.9601 18.3791 21.8304 18.4552C19.2268 19.9582 16.6278 21.4658 14.0175 22.9599C13.5903 23.2037 13.3912 23.5213 13.3957 24.0179C13.4091 25.8654 13.4114 27.713 13.3957 29.5605C13.3912 30.0705 13.5948 30.3948 14.0332 30.6453C16.7866 32.2199 19.5288 33.8125 22.28 35.3916C22.5126 35.5258 22.611 35.6645 22.6065 35.9418C22.5864 36.888 22.5998 37.8363 22.5998 38.8473L22.5953 38.8451ZM22.5953 33.553C22.356 33.4166 22.1838 33.3204 22.0116 33.2198C19.8285 31.9605 17.6477 30.6967 15.4602 29.4464C15.2231 29.3122 15.1359 29.1668 15.1381 28.8917C15.1538 27.4714 15.1471 26.0511 15.1426 24.6308C15.1426 24.4384 15.1717 24.3064 15.3618 24.1991C16.167 23.7495 16.9633 23.2798 17.7618 22.8212C17.8199 22.7877 17.8826 22.7631 17.9877 22.7116V24.3064C17.9877 25.1698 18.0011 26.0354 17.9832 26.8988C17.972 27.3909 18.1622 27.7241 18.5916 27.9657C19.8285 28.6636 21.0498 29.3883 22.2867 30.0839C22.5305 30.2203 22.6043 30.3724 22.5998 30.6408C22.5842 31.5847 22.5931 32.5308 22.5931 33.5508L22.5953 33.553ZM20.0746 14.91C19.6116 14.6371 19.2157 14.6393 18.7527 14.91C16.1581 16.4265 13.5523 17.9228 10.9487 19.4259C10.8391 19.4908 10.7251 19.5489 10.5305 19.6541C10.5998 18.6654 10.3873 17.7327 10.7251 16.8291C10.9085 16.3348 11.2529 15.9635 11.7092 15.6995C13.8811 14.4447 16.0507 13.1877 18.227 11.9396C19.0211 11.4833 19.8308 11.4945 20.6248 11.953C23.0964 13.3756 25.5657 14.8026 28.0306 16.2341C28.1357 16.2945 28.2677 16.4309 28.2677 16.5338C28.2856 17.5493 28.2788 18.567 28.2788 19.6563C27.3819 19.1396 26.5543 18.6609 25.7267 18.1823C23.8412 17.093 21.9512 16.0149 20.0746 14.91ZM37.4427 30.8779C37.3778 31.6764 36.9103 32.2423 36.2192 32.6404C33.5732 34.1614 30.9294 35.6913 28.2856 37.2168C27.4557 37.6954 26.6259 38.1741 25.7938 38.6527C25.6932 38.7109 25.5903 38.7601 25.4539 38.8317C25.4449 38.693 25.4337 38.5924 25.4337 38.4917C25.4337 37.6149 25.4382 36.7404 25.4293 35.8636C25.4293 35.6645 25.4762 35.5437 25.6596 35.4386C29.5157 33.2198 33.3696 30.9942 37.2212 28.7709C37.2794 28.7374 37.3443 28.7105 37.4539 28.6591C37.4539 29.4375 37.4986 30.1622 37.4427 30.8779ZM37.4628 25.3577C37.4561 26.2658 36.9663 26.9033 36.1901 27.3506C33.175 29.0841 30.1622 30.8265 27.1493 32.5666C26.5991 32.8842 26.0466 33.1996 25.4561 33.5396C25.4472 33.3897 25.436 33.2913 25.436 33.1907C25.436 32.3273 25.4449 31.4617 25.4293 30.5983C25.4248 30.3523 25.5075 30.2226 25.72 30.0995C28.46 28.5271 31.1911 26.9368 33.9355 25.3733C34.4231 25.096 34.6378 24.7538 34.6334 24.1812C34.6132 21.1974 34.6244 18.2136 34.6244 15.2298V14.7087C35.3402 15.1404 36.0112 15.496 36.624 15.9299C37.1832 16.3258 37.465 16.9253 37.4673 17.6164C37.4762 20.1976 37.4829 22.7788 37.465 25.3599L37.4628 25.3577Z" fill="%230D0D0D"></path><path fill-rule="evenodd" d="M24 0C10.7452 0 0 10.7452 0 24C0 37.2548 10.7452 48 24 48C37.2548 48 48 37.2548 48 24C48 10.7452 37.2548 0 24 0ZM24 2C11.8497 2 2 11.8497 2 24C2 36.1503 11.8497 46 24 46C36.1503 46 46 36.1503 46 24C46 11.8497 36.1503 2 24 2Z" fill="url(%23paint2_radial_6324_83922)"></path><path fill-rule="evenodd" d="M24 0C10.7452 0 0 10.7452 0 24C0 37.2548 10.7452 48 24 48C37.2548 48 48 37.2548 48 24C48 10.7452 37.2548 0 24 0ZM24 2C11.8497 2 2 11.8497 2 24C2 36.1503 11.8497 46 24 46C36.1503 46 46 36.1503 46 24C46 11.8497 36.1503 2 24 2Z" fill="url(%23paint3_radial_6324_83922)"></path><defs><radialGradient id="paint0_radial_6324_83922" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(13.4442 13.3899) rotate(44.9817) scale(46.7487 99.1435)"><stop stop-color="%23A3EEF8"></stop><stop offset="0.177083" stop-color="%23A4DCF5"></stop><stop offset="0.380208" stop-color="%23A6AEEC"></stop><stop offset="1" stop-color="%23ECBEE1"></stop></radialGradient><radialGradient id="paint1_radial_6324_83922" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(25.9515 43.7068) rotate(84.265) scale(24.2138 46.3215)"><stop stop-color="%23FCF5EE"></stop><stop offset="0.715135" stop-color="%23ECBEE1" stop-opacity="0"></stop></radialGradient><radialGradient id="paint2_radial_6324_83922" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(12.7405 12.6825) rotate(44.9817) scale(49.8653 105.753)"><stop stop-color="%23A3EEF8"></stop><stop offset="0.177083" stop-color="%23A4DCF5"></stop><stop offset="0.380208" stop-color="%23A6AEEC"></stop><stop offset="1" stop-color="%23ECBEE1"></stop></radialGradient><radialGradient id="paint3_radial_6324_83922" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(26.0816 45.0206) rotate(84.265) scale(25.828 49.4096)"><stop stop-color="%23FCF5EE"></stop><stop offset="0.715135" stop-color="%23ECBEE1" stop-opacity="0"></stop></radialGradient></defs></g></svg>',
|
|
7
|
+
name: 'Immutable Passport',
|
|
8
|
+
rdns: 'com.immutable.passport',
|
|
9
|
+
uuid: uuidv4(),
|
|
10
|
+
} as EIP6963ProviderInfo;
|
|
11
|
+
|
|
12
|
+
export function announceProvider(
|
|
13
|
+
detail: EIP6963ProviderDetail,
|
|
14
|
+
) {
|
|
15
|
+
if (typeof window === 'undefined') return;
|
|
16
|
+
const event: CustomEvent<EIP6963ProviderDetail> = new CustomEvent(
|
|
17
|
+
'eip6963:announceProvider',
|
|
18
|
+
{ detail: Object.freeze(detail) },
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
window.dispatchEvent(event);
|
|
22
|
+
|
|
23
|
+
const handler = () => window.dispatchEvent(event);
|
|
24
|
+
window.addEventListener('eip6963:requestProvider', handler);
|
|
25
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Environment, ModuleConfiguration } from '@imtbl/config';
|
|
2
|
+
import { Flow } from '@imtbl/metrics';
|
|
3
|
+
import { User } from '@imtbl/auth';
|
|
4
|
+
import { BigNumberish } from 'ethers';
|
|
5
|
+
import { JsonRpcError } from './zkEvm/JsonRpcError';
|
|
6
|
+
|
|
7
|
+
// Re-export auth types for convenience
|
|
8
|
+
export type {
|
|
9
|
+
User, UserProfile, UserImx, UserZkEvm, DirectLoginMethod,
|
|
10
|
+
} from '@imtbl/auth';
|
|
11
|
+
export { isUserImx, isUserZkEvm } from '@imtbl/auth';
|
|
12
|
+
export type { RollupType } from '@imtbl/auth';
|
|
13
|
+
|
|
14
|
+
export enum PassportEvents {
|
|
15
|
+
LOGGED_OUT = 'loggedOut',
|
|
16
|
+
LOGGED_IN = 'loggedIn',
|
|
17
|
+
ACCOUNTS_REQUESTED = 'accountsRequested',
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type AccountsRequestedEvent = {
|
|
21
|
+
environment: Environment;
|
|
22
|
+
sendTransaction: (params: Array<any>, flow: Flow) => Promise<string>;
|
|
23
|
+
walletAddress: string;
|
|
24
|
+
passportClient: string;
|
|
25
|
+
flow?: Flow;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export interface PassportEventMap extends Record<string, any> {
|
|
29
|
+
[PassportEvents.LOGGED_OUT]: [];
|
|
30
|
+
[PassportEvents.LOGGED_IN]: [User];
|
|
31
|
+
[PassportEvents.ACCOUNTS_REQUESTED]: [AccountsRequestedEvent];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// zkEVM/Wallet specific types
|
|
35
|
+
export type Provider = {
|
|
36
|
+
request: (request: RequestArguments) => Promise<any>;
|
|
37
|
+
on: (event: string, listener: (...args: any[]) => void) => void;
|
|
38
|
+
removeListener: (event: string, listener: (...args: any[]) => void) => void;
|
|
39
|
+
isPassport: boolean;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export interface RequestArguments {
|
|
43
|
+
method: string;
|
|
44
|
+
params?: Array<any>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type JsonRpcRequestPayload = RequestArguments & {
|
|
48
|
+
jsonrpc?: string;
|
|
49
|
+
id?: string | number;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export interface JsonRpcRequestCallback {
|
|
53
|
+
(
|
|
54
|
+
err: JsonRpcError | null,
|
|
55
|
+
result?: JsonRpcResponsePayload | (JsonRpcResponsePayload | null)[] | null
|
|
56
|
+
): void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface JsonRpcResponsePayload {
|
|
60
|
+
result?: Array<any> | null;
|
|
61
|
+
error?: JsonRpcError | null;
|
|
62
|
+
jsonrpc?: string;
|
|
63
|
+
id?: string | number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// https://eips.ethereum.org/EIPS/eip-712
|
|
67
|
+
export interface TypedDataPayload {
|
|
68
|
+
types: {
|
|
69
|
+
EIP712Domain: Array<{ name: string; type: string }>;
|
|
70
|
+
[key: string]: Array<{ name: string; type: string }>;
|
|
71
|
+
};
|
|
72
|
+
domain: {
|
|
73
|
+
name?: string;
|
|
74
|
+
version?: string;
|
|
75
|
+
chainId?: number | string;
|
|
76
|
+
verifyingContract?: string;
|
|
77
|
+
salt?: string;
|
|
78
|
+
} | {
|
|
79
|
+
name?: string;
|
|
80
|
+
version?: string;
|
|
81
|
+
chainId?: number;
|
|
82
|
+
verifyingContract?: string;
|
|
83
|
+
salt?: string;
|
|
84
|
+
};
|
|
85
|
+
primaryType: string;
|
|
86
|
+
message: Record<string, any>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface MetaTransaction {
|
|
90
|
+
to: string;
|
|
91
|
+
value?: BigNumberish | null;
|
|
92
|
+
data?: string | null;
|
|
93
|
+
nonce?: BigNumberish;
|
|
94
|
+
gasLimit?: BigNumberish;
|
|
95
|
+
delegateCall?: boolean;
|
|
96
|
+
revertOnError?: boolean;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface MetaTransactionNormalised {
|
|
100
|
+
delegateCall: boolean;
|
|
101
|
+
revertOnError: boolean;
|
|
102
|
+
gasLimit: BigNumberish;
|
|
103
|
+
target: string;
|
|
104
|
+
value: BigNumberish;
|
|
105
|
+
data: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export enum ProviderEvent {
|
|
109
|
+
ACCOUNTS_CHANGED = 'accountsChanged',
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export type AccountsChangedEvent = Array<string>;
|
|
113
|
+
|
|
114
|
+
export interface ProviderEventMap extends Record<string, any> {
|
|
115
|
+
[ProviderEvent.ACCOUNTS_CHANGED]: [AccountsChangedEvent];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export enum RelayerTransactionStatus {
|
|
119
|
+
PENDING = 'PENDING',
|
|
120
|
+
SUBMITTED = 'SUBMITTED',
|
|
121
|
+
SUCCESSFUL = 'SUCCESSFUL',
|
|
122
|
+
REVERTED = 'REVERTED',
|
|
123
|
+
FAILED = 'FAILED',
|
|
124
|
+
CANCELLED = 'CANCELLED',
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface RelayerTransaction {
|
|
128
|
+
status: RelayerTransactionStatus;
|
|
129
|
+
chainId: string;
|
|
130
|
+
relayerId: string;
|
|
131
|
+
hash: string;
|
|
132
|
+
statusMessage?: string;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface FeeOption {
|
|
136
|
+
tokenPrice: string;
|
|
137
|
+
tokenSymbol: string;
|
|
138
|
+
tokenDecimals: number;
|
|
139
|
+
tokenAddress: string;
|
|
140
|
+
recipientAddress: string;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Event detail from the `eip6963:announceProvider` event.
|
|
145
|
+
*/
|
|
146
|
+
export interface EIP6963ProviderDetail {
|
|
147
|
+
info: EIP6963ProviderInfo;
|
|
148
|
+
provider: Provider;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Metadata of the EIP-1193 Provider.
|
|
153
|
+
*/
|
|
154
|
+
export interface EIP6963ProviderInfo {
|
|
155
|
+
icon: `data:image/${string}`; // RFC-2397
|
|
156
|
+
name: string;
|
|
157
|
+
rdns: string;
|
|
158
|
+
uuid: string;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Event type to announce an EIP-1193 Provider.
|
|
163
|
+
*/
|
|
164
|
+
export interface EIP6963AnnounceProviderEvent
|
|
165
|
+
extends CustomEvent<EIP6963ProviderDetail> {
|
|
166
|
+
type: 'eip6963:announceProvider';
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Wallet configuration types
|
|
170
|
+
export interface WalletOverrides {
|
|
171
|
+
passportDomain: string;
|
|
172
|
+
zkEvmRpcUrl: string;
|
|
173
|
+
relayerUrl: string;
|
|
174
|
+
indexerMrBasePath: string;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface WalletModuleConfiguration extends ModuleConfiguration<WalletOverrides> {
|
|
178
|
+
/**
|
|
179
|
+
* Optional referrer URL to be sent with JSON-RPC requests.
|
|
180
|
+
*/
|
|
181
|
+
jsonRpcReferrer?: string;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* If true, forces SCW deployment before allowing message signature.
|
|
185
|
+
*/
|
|
186
|
+
forceScwDeployBeforeMessageSignature?: boolean;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* This flag indicates that Wallet is being used in a cross-sdk bridge scenario.
|
|
190
|
+
*/
|
|
191
|
+
crossSdkBridgeEnabled?: boolean;
|
|
192
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Flow, trackError, trackFlow } from '@imtbl/metrics';
|
|
2
|
+
|
|
3
|
+
export const withMetrics = <T>(
|
|
4
|
+
fn: (flow: Flow) => T,
|
|
5
|
+
flowName: string,
|
|
6
|
+
trackStartEvent: boolean = true,
|
|
7
|
+
trackEndEvent: boolean = true,
|
|
8
|
+
): T => {
|
|
9
|
+
const flow: Flow = trackFlow(
|
|
10
|
+
'passport',
|
|
11
|
+
flowName,
|
|
12
|
+
trackStartEvent,
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
return fn(flow);
|
|
17
|
+
} catch (error) {
|
|
18
|
+
if (error instanceof Error) {
|
|
19
|
+
trackError('passport', flowName, error, { flowId: flow.details.flowId });
|
|
20
|
+
} else {
|
|
21
|
+
flow.addEvent('errored');
|
|
22
|
+
}
|
|
23
|
+
throw error;
|
|
24
|
+
} finally {
|
|
25
|
+
if (trackEndEvent) {
|
|
26
|
+
flow.addEvent('End');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const withMetricsAsync = async <T>(
|
|
32
|
+
fn: (flow: Flow) => Promise<T>,
|
|
33
|
+
flowName: string,
|
|
34
|
+
trackStartEvent: boolean = true,
|
|
35
|
+
trackEndEvent: boolean = true,
|
|
36
|
+
): Promise<T> => {
|
|
37
|
+
const flow: Flow = trackFlow(
|
|
38
|
+
'passport',
|
|
39
|
+
flowName,
|
|
40
|
+
trackStartEvent,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
return await fn(flow);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (error instanceof Error) {
|
|
47
|
+
trackError('passport', flowName, error, { flowId: flow.details.flowId });
|
|
48
|
+
} else {
|
|
49
|
+
flow.addEvent('errored');
|
|
50
|
+
}
|
|
51
|
+
throw error;
|
|
52
|
+
} finally {
|
|
53
|
+
if (trackEndEvent) {
|
|
54
|
+
flow.addEvent('End');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { getBytes, stripZerosLeft, toUtf8String } from 'ethers';
|
|
2
|
+
|
|
3
|
+
export const hexToString = (hex: string) => {
|
|
4
|
+
if (!hex) return hex;
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
const stripped = stripZerosLeft(getBytes(hex));
|
|
8
|
+
return toUtf8String(stripped);
|
|
9
|
+
} catch (e) {
|
|
10
|
+
return hex;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
|
|
3
|
+
export default class TypedEventEmitter<TEvents extends Record<string, any>> {
|
|
4
|
+
private emitter = new EventEmitter();
|
|
5
|
+
|
|
6
|
+
emit<TEventName extends keyof TEvents & string>(
|
|
7
|
+
eventName: TEventName,
|
|
8
|
+
...eventArg: TEvents[TEventName]
|
|
9
|
+
) {
|
|
10
|
+
this.emitter.emit(eventName, ...(eventArg as []));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
on<TEventName extends keyof TEvents & string>(
|
|
14
|
+
eventName: TEventName,
|
|
15
|
+
handler: (...eventArg: TEvents[TEventName]) => void,
|
|
16
|
+
) {
|
|
17
|
+
this.emitter.on(eventName, handler as any);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
removeListener<TEventName extends keyof TEvents & string>(
|
|
21
|
+
eventName: TEventName,
|
|
22
|
+
handler: (...eventArg: TEvents[TEventName]) => void,
|
|
23
|
+
) {
|
|
24
|
+
this.emitter.removeListener(eventName, handler as any);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ProviderErrors should take priority over RpcErrorCodes
|
|
3
|
+
* https://eips.ethereum.org/EIPS/eip-1193#provider-errors
|
|
4
|
+
* https://eips.ethereum.org/EIPS/eip-1474#error-codes
|
|
5
|
+
*/
|
|
6
|
+
export enum ProviderErrorCode {
|
|
7
|
+
USER_REJECTED_REQUEST = 4001,
|
|
8
|
+
UNAUTHORIZED = 4100,
|
|
9
|
+
UNSUPPORTED_METHOD = 4200,
|
|
10
|
+
DISCONNECTED = 4900,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export enum RpcErrorCode {
|
|
14
|
+
RPC_SERVER_ERROR = -32000,
|
|
15
|
+
INVALID_REQUEST = -32600,
|
|
16
|
+
METHOD_NOT_FOUND = -32601,
|
|
17
|
+
INVALID_PARAMS = -32602,
|
|
18
|
+
INTERNAL_ERROR = -32603,
|
|
19
|
+
PARSE_ERROR = -32700,
|
|
20
|
+
TRANSACTION_REJECTED = -32003,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class JsonRpcError extends Error {
|
|
24
|
+
public readonly message: string;
|
|
25
|
+
|
|
26
|
+
public readonly code: ProviderErrorCode | RpcErrorCode;
|
|
27
|
+
|
|
28
|
+
constructor(code: ProviderErrorCode | RpcErrorCode, message: string) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.message = message;
|
|
31
|
+
this.code = code;
|
|
32
|
+
}
|
|
33
|
+
}
|