@0xsquid/react-hooks 8.3.1-embed-tanstack-build.1 → 8.4.0
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/core/constants.d.ts +1 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/types/transaction.d.ts +19 -1
- package/dist/hooks/hedera/useHederaAccountActivation.d.ts +10 -0
- package/dist/hooks/index.d.ts +2 -1
- package/dist/hooks/tokens/useNativeBalance.d.ts +2 -0
- package/dist/hooks/transaction/useGetRoute.d.ts +18 -7
- package/dist/{index-CBdXaMut.js → index-BdrSk8Ed.js} +4870 -12873
- package/dist/index-BdrSk8Ed.js.map +1 -0
- package/dist/{index-CHFbvaAT.js → index-BxJVsDEs.js} +4773 -12780
- package/dist/index-BxJVsDEs.js.map +1 -0
- package/dist/{index.es-CJQ994zu.js → index.es-CFf55ppw.js} +5 -4
- package/dist/index.es-CFf55ppw.js.map +1 -0
- package/dist/{index.es-Db1itMBd.js → index.es-DhiQj1ju.js} +5 -4
- package/dist/index.es-DhiQj1ju.js.map +1 -0
- package/dist/index.esm.js +4 -3
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +18 -3
- package/dist/index.js.map +1 -1
- package/dist/{secretService-C_xxV54S.js → secretService-D0xrwhEv.js} +5 -4
- package/dist/{secretService-C_xxV54S.js.map → secretService-D0xrwhEv.js.map} +1 -1
- package/dist/{secretService-BEHJHJzr.js → secretService-DRTd4cda.js} +5 -4
- package/dist/{secretService-BEHJHJzr.js.map → secretService-DRTd4cda.js.map} +1 -1
- package/dist/services/index.d.ts +1 -0
- package/dist/services/internal/hederaService.d.ts +2 -0
- package/dist/services/internal/routeService.d.ts +8 -0
- package/dist/services/internal/transactionService.d.ts +14 -1
- package/dist/{stellarService.client-DKE1A5qx.js → stellarService.client-CRFUb2R6.js} +5 -4
- package/dist/{stellarService.client-DKe-JCHP.js.map → stellarService.client-CRFUb2R6.js.map} +1 -1
- package/dist/{stellarService.client-DKe-JCHP.js → stellarService.client-xMf7fjCz.js} +5 -4
- package/dist/{stellarService.client-DKE1A5qx.js.map → stellarService.client-xMf7fjCz.js.map} +1 -1
- package/package.json +4 -4
- package/dist/browser-BpOc5ZYm.js +0 -728
- package/dist/browser-BpOc5ZYm.js.map +0 -1
- package/dist/browser-C2k8mMWG.js +0 -731
- package/dist/browser-C2k8mMWG.js.map +0 -1
- package/dist/clsx.m-5lBZSkly.js +0 -13
- package/dist/clsx.m-5lBZSkly.js.map +0 -1
- package/dist/clsx.m-C1RYtb0y.js +0 -10
- package/dist/clsx.m-C1RYtb0y.js.map +0 -1
- package/dist/index-CBdXaMut.js.map +0 -1
- package/dist/index-CHFbvaAT.js.map +0 -1
- package/dist/index-CJc-nZ2s.js +0 -20967
- package/dist/index-CJc-nZ2s.js.map +0 -1
- package/dist/index-CQhbmLAK.js +0 -20945
- package/dist/index-CQhbmLAK.js.map +0 -1
- package/dist/index-Dehij_6h.js +0 -4628
- package/dist/index-Dehij_6h.js.map +0 -1
- package/dist/index-T4Aq984K.js +0 -4630
- package/dist/index-T4Aq984K.js.map +0 -1
- package/dist/index.es-CJQ994zu.js.map +0 -1
- package/dist/index.es-Db1itMBd.js.map +0 -1
- package/dist/metamask-sdk-BD7Q6BkI.js +0 -6353
- package/dist/metamask-sdk-BD7Q6BkI.js.map +0 -1
- package/dist/metamask-sdk-DFVIZ2SC.js +0 -6359
- package/dist/metamask-sdk-DFVIZ2SC.js.map +0 -1
package/dist/index-Dehij_6h.js
DELETED
|
@@ -1,4628 +0,0 @@
|
|
|
1
|
-
import { g as getDefaultExportFromCjs, E as EventEmitter } from './index-CHFbvaAT.js';
|
|
2
|
-
import { c as clsx } from './clsx.m-C1RYtb0y.js';
|
|
3
|
-
import '@0xsquid/squid-types';
|
|
4
|
-
import 'viem';
|
|
5
|
-
import 'react';
|
|
6
|
-
import '@cosmjs/encoding';
|
|
7
|
-
import 'bitcoinjs-lib';
|
|
8
|
-
import '@bitcoinerlab/secp256k1';
|
|
9
|
-
import 'axios';
|
|
10
|
-
import '@walletconnect/modal';
|
|
11
|
-
import '@walletconnect/universal-provider';
|
|
12
|
-
import 'ripple-address-codec';
|
|
13
|
-
import '@solana/spl-token';
|
|
14
|
-
import '@solana/wallet-standard-wallet-adapter-base';
|
|
15
|
-
import '@solana/web3.js';
|
|
16
|
-
import '@stellar/stellar-sdk';
|
|
17
|
-
import '@mysten/wallet-standard';
|
|
18
|
-
import 'ethers';
|
|
19
|
-
import 'bignumber.js';
|
|
20
|
-
import 'countries-list';
|
|
21
|
-
import 'currency-symbol-map';
|
|
22
|
-
import 'fuse.js';
|
|
23
|
-
import 'zustand';
|
|
24
|
-
import 'zustand/middleware';
|
|
25
|
-
import '@safe-global/safe-apps-sdk';
|
|
26
|
-
import '@wallet-standard/core';
|
|
27
|
-
import '@mysten/slush-wallet';
|
|
28
|
-
import 'viem/utils';
|
|
29
|
-
import '@cosmjs/stargate';
|
|
30
|
-
import '@mysten/sui/client';
|
|
31
|
-
import 'viem/actions';
|
|
32
|
-
import '@cosmjs/proto-signing';
|
|
33
|
-
import '@mysten/sui/transactions';
|
|
34
|
-
import '@cosmjs/cosmwasm-stargate';
|
|
35
|
-
import '@injectivelabs/sdk-ts/dist/cjs/core/stargate/index.js';
|
|
36
|
-
import '@0xsquid/sdk';
|
|
37
|
-
import 'viem/chains';
|
|
38
|
-
|
|
39
|
-
// Copyright (c) 2018-2024 Coinbase, Inc. <https://www.coinbase.com/>
|
|
40
|
-
// TODO: clean up, or possibly deprecate Storage class
|
|
41
|
-
class ScopedLocalStorage {
|
|
42
|
-
constructor(scope, module) {
|
|
43
|
-
this.scope = scope;
|
|
44
|
-
this.module = module;
|
|
45
|
-
}
|
|
46
|
-
storeObject(key, item) {
|
|
47
|
-
this.setItem(key, JSON.stringify(item));
|
|
48
|
-
}
|
|
49
|
-
loadObject(key) {
|
|
50
|
-
const item = this.getItem(key);
|
|
51
|
-
return item ? JSON.parse(item) : undefined;
|
|
52
|
-
}
|
|
53
|
-
setItem(key, value) {
|
|
54
|
-
localStorage.setItem(this.scopedKey(key), value);
|
|
55
|
-
}
|
|
56
|
-
getItem(key) {
|
|
57
|
-
return localStorage.getItem(this.scopedKey(key));
|
|
58
|
-
}
|
|
59
|
-
removeItem(key) {
|
|
60
|
-
localStorage.removeItem(this.scopedKey(key));
|
|
61
|
-
}
|
|
62
|
-
clear() {
|
|
63
|
-
const prefix = this.scopedKey('');
|
|
64
|
-
const keysToRemove = [];
|
|
65
|
-
for (let i = 0; i < localStorage.length; i++) {
|
|
66
|
-
const key = localStorage.key(i);
|
|
67
|
-
if (typeof key === 'string' && key.startsWith(prefix)) {
|
|
68
|
-
keysToRemove.push(key);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
keysToRemove.forEach((key) => localStorage.removeItem(key));
|
|
72
|
-
}
|
|
73
|
-
scopedKey(key) {
|
|
74
|
-
return `-${this.scope}${this.module ? `:${this.module}` : ''}:${key}`;
|
|
75
|
-
}
|
|
76
|
-
static clearAll() {
|
|
77
|
-
new ScopedLocalStorage('CBWSDK').clear();
|
|
78
|
-
new ScopedLocalStorage('walletlink').clear();
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const standardErrorCodes = {
|
|
83
|
-
rpc: {
|
|
84
|
-
invalidInput: -32e3,
|
|
85
|
-
resourceNotFound: -32001,
|
|
86
|
-
resourceUnavailable: -32002,
|
|
87
|
-
transactionRejected: -32003,
|
|
88
|
-
methodNotSupported: -32004,
|
|
89
|
-
limitExceeded: -32005,
|
|
90
|
-
parse: -32700,
|
|
91
|
-
invalidRequest: -32600,
|
|
92
|
-
methodNotFound: -32601,
|
|
93
|
-
invalidParams: -32602,
|
|
94
|
-
internal: -32603,
|
|
95
|
-
},
|
|
96
|
-
provider: {
|
|
97
|
-
userRejectedRequest: 4001,
|
|
98
|
-
unauthorized: 4100,
|
|
99
|
-
unsupportedMethod: 4200,
|
|
100
|
-
disconnected: 4900,
|
|
101
|
-
chainDisconnected: 4901,
|
|
102
|
-
unsupportedChain: 4902,
|
|
103
|
-
},
|
|
104
|
-
};
|
|
105
|
-
const errorValues = {
|
|
106
|
-
'-32700': {
|
|
107
|
-
standard: 'JSON RPC 2.0',
|
|
108
|
-
message: 'Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.',
|
|
109
|
-
},
|
|
110
|
-
'-32600': {
|
|
111
|
-
standard: 'JSON RPC 2.0',
|
|
112
|
-
message: 'The JSON sent is not a valid Request object.',
|
|
113
|
-
},
|
|
114
|
-
'-32601': {
|
|
115
|
-
standard: 'JSON RPC 2.0',
|
|
116
|
-
message: 'The method does not exist / is not available.',
|
|
117
|
-
},
|
|
118
|
-
'-32602': {
|
|
119
|
-
standard: 'JSON RPC 2.0',
|
|
120
|
-
message: 'Invalid method parameter(s).',
|
|
121
|
-
},
|
|
122
|
-
'-32603': {
|
|
123
|
-
standard: 'JSON RPC 2.0',
|
|
124
|
-
message: 'Internal JSON-RPC error.',
|
|
125
|
-
},
|
|
126
|
-
'-32000': {
|
|
127
|
-
standard: 'EIP-1474',
|
|
128
|
-
message: 'Invalid input.',
|
|
129
|
-
},
|
|
130
|
-
'-32001': {
|
|
131
|
-
standard: 'EIP-1474',
|
|
132
|
-
message: 'Resource not found.',
|
|
133
|
-
},
|
|
134
|
-
'-32002': {
|
|
135
|
-
standard: 'EIP-1474',
|
|
136
|
-
message: 'Resource unavailable.',
|
|
137
|
-
},
|
|
138
|
-
'-32003': {
|
|
139
|
-
standard: 'EIP-1474',
|
|
140
|
-
message: 'Transaction rejected.',
|
|
141
|
-
},
|
|
142
|
-
'-32004': {
|
|
143
|
-
standard: 'EIP-1474',
|
|
144
|
-
message: 'Method not supported.',
|
|
145
|
-
},
|
|
146
|
-
'-32005': {
|
|
147
|
-
standard: 'EIP-1474',
|
|
148
|
-
message: 'Request limit exceeded.',
|
|
149
|
-
},
|
|
150
|
-
'4001': {
|
|
151
|
-
standard: 'EIP-1193',
|
|
152
|
-
message: 'User rejected the request.',
|
|
153
|
-
},
|
|
154
|
-
'4100': {
|
|
155
|
-
standard: 'EIP-1193',
|
|
156
|
-
message: 'The requested account and/or method has not been authorized by the user.',
|
|
157
|
-
},
|
|
158
|
-
'4200': {
|
|
159
|
-
standard: 'EIP-1193',
|
|
160
|
-
message: 'The requested method is not supported by this Ethereum provider.',
|
|
161
|
-
},
|
|
162
|
-
'4900': {
|
|
163
|
-
standard: 'EIP-1193',
|
|
164
|
-
message: 'The provider is disconnected from all chains.',
|
|
165
|
-
},
|
|
166
|
-
'4901': {
|
|
167
|
-
standard: 'EIP-1193',
|
|
168
|
-
message: 'The provider is disconnected from the specified chain.',
|
|
169
|
-
},
|
|
170
|
-
'4902': {
|
|
171
|
-
standard: 'EIP-3085',
|
|
172
|
-
message: 'Unrecognized chain ID.',
|
|
173
|
-
},
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
const FALLBACK_MESSAGE = 'Unspecified error message.';
|
|
177
|
-
const JSON_RPC_SERVER_ERROR_MESSAGE = 'Unspecified server error.';
|
|
178
|
-
/**
|
|
179
|
-
* Gets the message for a given code, or a fallback message if the code has
|
|
180
|
-
* no corresponding message.
|
|
181
|
-
*/
|
|
182
|
-
function getMessageFromCode(code, fallbackMessage = FALLBACK_MESSAGE) {
|
|
183
|
-
if (code && Number.isInteger(code)) {
|
|
184
|
-
const codeString = code.toString();
|
|
185
|
-
if (hasKey(errorValues, codeString)) {
|
|
186
|
-
return errorValues[codeString].message;
|
|
187
|
-
}
|
|
188
|
-
if (isJsonRpcServerError(code)) {
|
|
189
|
-
return JSON_RPC_SERVER_ERROR_MESSAGE;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
return fallbackMessage;
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Returns whether the given code is valid.
|
|
196
|
-
* A code is only valid if it has a message.
|
|
197
|
-
*/
|
|
198
|
-
function isValidCode(code) {
|
|
199
|
-
if (!Number.isInteger(code)) {
|
|
200
|
-
return false;
|
|
201
|
-
}
|
|
202
|
-
const codeString = code.toString();
|
|
203
|
-
if (errorValues[codeString]) {
|
|
204
|
-
return true;
|
|
205
|
-
}
|
|
206
|
-
if (isJsonRpcServerError(code)) {
|
|
207
|
-
return true;
|
|
208
|
-
}
|
|
209
|
-
return false;
|
|
210
|
-
}
|
|
211
|
-
function serialize(error, { shouldIncludeStack = false } = {}) {
|
|
212
|
-
const serialized = {};
|
|
213
|
-
if (error &&
|
|
214
|
-
typeof error === 'object' &&
|
|
215
|
-
!Array.isArray(error) &&
|
|
216
|
-
hasKey(error, 'code') &&
|
|
217
|
-
isValidCode(error.code)) {
|
|
218
|
-
const _error = error;
|
|
219
|
-
serialized.code = _error.code;
|
|
220
|
-
if (_error.message && typeof _error.message === 'string') {
|
|
221
|
-
serialized.message = _error.message;
|
|
222
|
-
if (hasKey(_error, 'data')) {
|
|
223
|
-
serialized.data = _error.data;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
else {
|
|
227
|
-
serialized.message = getMessageFromCode(serialized.code);
|
|
228
|
-
serialized.data = { originalError: assignOriginalError(error) };
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
else {
|
|
232
|
-
serialized.code = standardErrorCodes.rpc.internal;
|
|
233
|
-
serialized.message = hasStringProperty(error, 'message') ? error.message : FALLBACK_MESSAGE;
|
|
234
|
-
serialized.data = { originalError: assignOriginalError(error) };
|
|
235
|
-
}
|
|
236
|
-
if (shouldIncludeStack) {
|
|
237
|
-
serialized.stack = hasStringProperty(error, 'stack') ? error.stack : undefined;
|
|
238
|
-
}
|
|
239
|
-
return serialized;
|
|
240
|
-
}
|
|
241
|
-
// Internal
|
|
242
|
-
function isJsonRpcServerError(code) {
|
|
243
|
-
return code >= -32099 && code <= -32e3;
|
|
244
|
-
}
|
|
245
|
-
function assignOriginalError(error) {
|
|
246
|
-
if (error && typeof error === 'object' && !Array.isArray(error)) {
|
|
247
|
-
return Object.assign({}, error);
|
|
248
|
-
}
|
|
249
|
-
return error;
|
|
250
|
-
}
|
|
251
|
-
function hasKey(obj, key) {
|
|
252
|
-
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
253
|
-
}
|
|
254
|
-
function hasStringProperty(obj, prop) {
|
|
255
|
-
return (typeof obj === 'object' && obj !== null && prop in obj && typeof obj[prop] === 'string');
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
const standardErrors = {
|
|
259
|
-
rpc: {
|
|
260
|
-
parse: (arg) => getEthJsonRpcError(standardErrorCodes.rpc.parse, arg),
|
|
261
|
-
invalidRequest: (arg) => getEthJsonRpcError(standardErrorCodes.rpc.invalidRequest, arg),
|
|
262
|
-
invalidParams: (arg) => getEthJsonRpcError(standardErrorCodes.rpc.invalidParams, arg),
|
|
263
|
-
methodNotFound: (arg) => getEthJsonRpcError(standardErrorCodes.rpc.methodNotFound, arg),
|
|
264
|
-
internal: (arg) => getEthJsonRpcError(standardErrorCodes.rpc.internal, arg),
|
|
265
|
-
server: (opts) => {
|
|
266
|
-
if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {
|
|
267
|
-
throw new Error('Ethereum RPC Server errors must provide single object argument.');
|
|
268
|
-
}
|
|
269
|
-
const { code } = opts;
|
|
270
|
-
if (!Number.isInteger(code) || code > -32005 || code < -32099) {
|
|
271
|
-
throw new Error('"code" must be an integer such that: -32099 <= code <= -32005');
|
|
272
|
-
}
|
|
273
|
-
return getEthJsonRpcError(code, opts);
|
|
274
|
-
},
|
|
275
|
-
invalidInput: (arg) => getEthJsonRpcError(standardErrorCodes.rpc.invalidInput, arg),
|
|
276
|
-
resourceNotFound: (arg) => getEthJsonRpcError(standardErrorCodes.rpc.resourceNotFound, arg),
|
|
277
|
-
resourceUnavailable: (arg) => getEthJsonRpcError(standardErrorCodes.rpc.resourceUnavailable, arg),
|
|
278
|
-
transactionRejected: (arg) => getEthJsonRpcError(standardErrorCodes.rpc.transactionRejected, arg),
|
|
279
|
-
methodNotSupported: (arg) => getEthJsonRpcError(standardErrorCodes.rpc.methodNotSupported, arg),
|
|
280
|
-
limitExceeded: (arg) => getEthJsonRpcError(standardErrorCodes.rpc.limitExceeded, arg),
|
|
281
|
-
},
|
|
282
|
-
provider: {
|
|
283
|
-
userRejectedRequest: (arg) => {
|
|
284
|
-
return getEthProviderError(standardErrorCodes.provider.userRejectedRequest, arg);
|
|
285
|
-
},
|
|
286
|
-
unauthorized: (arg) => {
|
|
287
|
-
return getEthProviderError(standardErrorCodes.provider.unauthorized, arg);
|
|
288
|
-
},
|
|
289
|
-
unsupportedMethod: (arg) => {
|
|
290
|
-
return getEthProviderError(standardErrorCodes.provider.unsupportedMethod, arg);
|
|
291
|
-
},
|
|
292
|
-
disconnected: (arg) => {
|
|
293
|
-
return getEthProviderError(standardErrorCodes.provider.disconnected, arg);
|
|
294
|
-
},
|
|
295
|
-
chainDisconnected: (arg) => {
|
|
296
|
-
return getEthProviderError(standardErrorCodes.provider.chainDisconnected, arg);
|
|
297
|
-
},
|
|
298
|
-
unsupportedChain: (arg) => {
|
|
299
|
-
return getEthProviderError(standardErrorCodes.provider.unsupportedChain, arg);
|
|
300
|
-
},
|
|
301
|
-
custom: (opts) => {
|
|
302
|
-
if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {
|
|
303
|
-
throw new Error('Ethereum Provider custom errors must provide single object argument.');
|
|
304
|
-
}
|
|
305
|
-
const { code, message, data } = opts;
|
|
306
|
-
if (!message || typeof message !== 'string') {
|
|
307
|
-
throw new Error('"message" must be a nonempty string');
|
|
308
|
-
}
|
|
309
|
-
return new EthereumProviderError(code, message, data);
|
|
310
|
-
},
|
|
311
|
-
},
|
|
312
|
-
};
|
|
313
|
-
// Internal
|
|
314
|
-
function getEthJsonRpcError(code, arg) {
|
|
315
|
-
const [message, data] = parseOpts(arg);
|
|
316
|
-
return new EthereumRpcError(code, message || getMessageFromCode(code), data);
|
|
317
|
-
}
|
|
318
|
-
function getEthProviderError(code, arg) {
|
|
319
|
-
const [message, data] = parseOpts(arg);
|
|
320
|
-
return new EthereumProviderError(code, message || getMessageFromCode(code), data);
|
|
321
|
-
}
|
|
322
|
-
function parseOpts(arg) {
|
|
323
|
-
if (arg) {
|
|
324
|
-
if (typeof arg === 'string') {
|
|
325
|
-
return [arg];
|
|
326
|
-
}
|
|
327
|
-
else if (typeof arg === 'object' && !Array.isArray(arg)) {
|
|
328
|
-
const { message, data } = arg;
|
|
329
|
-
if (message && typeof message !== 'string') {
|
|
330
|
-
throw new Error('Must specify string message.');
|
|
331
|
-
}
|
|
332
|
-
return [message || undefined, data];
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
return [];
|
|
336
|
-
}
|
|
337
|
-
class EthereumRpcError extends Error {
|
|
338
|
-
constructor(code, message, data) {
|
|
339
|
-
if (!Number.isInteger(code)) {
|
|
340
|
-
throw new Error('"code" must be an integer.');
|
|
341
|
-
}
|
|
342
|
-
if (!message || typeof message !== 'string') {
|
|
343
|
-
throw new Error('"message" must be a nonempty string.');
|
|
344
|
-
}
|
|
345
|
-
super(message);
|
|
346
|
-
this.code = code;
|
|
347
|
-
if (data !== undefined) {
|
|
348
|
-
this.data = data;
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
class EthereumProviderError extends EthereumRpcError {
|
|
353
|
-
/**
|
|
354
|
-
* Create an Ethereum Provider JSON-RPC error.
|
|
355
|
-
* `code` must be an integer in the 1000 <= 4999 range.
|
|
356
|
-
*/
|
|
357
|
-
constructor(code, message, data) {
|
|
358
|
-
if (!isValidEthProviderCode(code)) {
|
|
359
|
-
throw new Error('"code" must be an integer such that: 1000 <= code <= 4999');
|
|
360
|
-
}
|
|
361
|
-
super(code, message, data);
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
function isValidEthProviderCode(code) {
|
|
365
|
-
return Number.isInteger(code) && code >= 1000 && code <= 4999;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
function OpaqueType() {
|
|
369
|
-
return (value) => value;
|
|
370
|
-
}
|
|
371
|
-
const HexString = OpaqueType();
|
|
372
|
-
const AddressString = OpaqueType();
|
|
373
|
-
const BigIntString = OpaqueType();
|
|
374
|
-
function IntNumber(num) {
|
|
375
|
-
return Math.floor(num);
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
379
|
-
// Copyright (c) 2018-2023 Coinbase, Inc. <https://www.coinbase.com/>
|
|
380
|
-
const INT_STRING_REGEX = /^[0-9]*$/;
|
|
381
|
-
const HEXADECIMAL_STRING_REGEX = /^[a-f0-9]*$/;
|
|
382
|
-
/**
|
|
383
|
-
* @param length number of bytes
|
|
384
|
-
*/
|
|
385
|
-
function randomBytesHex(length) {
|
|
386
|
-
return uint8ArrayToHex(crypto.getRandomValues(new Uint8Array(length)));
|
|
387
|
-
}
|
|
388
|
-
function uint8ArrayToHex(value) {
|
|
389
|
-
return [...value].map((b) => b.toString(16).padStart(2, '0')).join('');
|
|
390
|
-
}
|
|
391
|
-
function hexStringToUint8Array(hexString) {
|
|
392
|
-
return new Uint8Array(hexString.match(/.{1,2}/g).map((byte) => Number.parseInt(byte, 16)));
|
|
393
|
-
}
|
|
394
|
-
function hexStringFromBuffer(buf, includePrefix = false) {
|
|
395
|
-
const hex = buf.toString('hex');
|
|
396
|
-
return HexString(includePrefix ? `0x${hex}` : hex);
|
|
397
|
-
}
|
|
398
|
-
function encodeToHexString(str) {
|
|
399
|
-
return hexStringFromBuffer(ensureBuffer(str), true);
|
|
400
|
-
}
|
|
401
|
-
function bigIntStringFromBigInt(bi) {
|
|
402
|
-
return BigIntString(bi.toString(10));
|
|
403
|
-
}
|
|
404
|
-
function hexStringFromNumber(num) {
|
|
405
|
-
return HexString(`0x${BigInt(num).toString(16)}`);
|
|
406
|
-
}
|
|
407
|
-
function has0xPrefix(str) {
|
|
408
|
-
return str.startsWith('0x') || str.startsWith('0X');
|
|
409
|
-
}
|
|
410
|
-
function strip0x(hex) {
|
|
411
|
-
if (has0xPrefix(hex)) {
|
|
412
|
-
return hex.slice(2);
|
|
413
|
-
}
|
|
414
|
-
return hex;
|
|
415
|
-
}
|
|
416
|
-
function prepend0x(hex) {
|
|
417
|
-
if (has0xPrefix(hex)) {
|
|
418
|
-
return `0x${hex.slice(2)}`;
|
|
419
|
-
}
|
|
420
|
-
return `0x${hex}`;
|
|
421
|
-
}
|
|
422
|
-
function isHexString$1(hex) {
|
|
423
|
-
if (typeof hex !== 'string') {
|
|
424
|
-
return false;
|
|
425
|
-
}
|
|
426
|
-
const s = strip0x(hex).toLowerCase();
|
|
427
|
-
return HEXADECIMAL_STRING_REGEX.test(s);
|
|
428
|
-
}
|
|
429
|
-
function ensureHexString(hex, includePrefix = false) {
|
|
430
|
-
if (typeof hex === 'string') {
|
|
431
|
-
const s = strip0x(hex).toLowerCase();
|
|
432
|
-
if (HEXADECIMAL_STRING_REGEX.test(s)) {
|
|
433
|
-
return HexString(includePrefix ? `0x${s}` : s);
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
throw standardErrors.rpc.invalidParams(`"${String(hex)}" is not a hexadecimal string`);
|
|
437
|
-
}
|
|
438
|
-
function ensureEvenLengthHexString(hex, includePrefix = false) {
|
|
439
|
-
let h = ensureHexString(hex, false);
|
|
440
|
-
if (h.length % 2 === 1) {
|
|
441
|
-
h = HexString(`0${h}`);
|
|
442
|
-
}
|
|
443
|
-
return includePrefix ? HexString(`0x${h}`) : h;
|
|
444
|
-
}
|
|
445
|
-
function ensureAddressString(str) {
|
|
446
|
-
if (typeof str === 'string') {
|
|
447
|
-
const s = strip0x(str).toLowerCase();
|
|
448
|
-
if (isHexString$1(s) && s.length === 40) {
|
|
449
|
-
return AddressString(prepend0x(s));
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
throw standardErrors.rpc.invalidParams(`Invalid Ethereum address: ${String(str)}`);
|
|
453
|
-
}
|
|
454
|
-
function ensureBuffer(str) {
|
|
455
|
-
if (Buffer.isBuffer(str)) {
|
|
456
|
-
return str;
|
|
457
|
-
}
|
|
458
|
-
if (typeof str === 'string') {
|
|
459
|
-
if (isHexString$1(str)) {
|
|
460
|
-
const s = ensureEvenLengthHexString(str, false);
|
|
461
|
-
return Buffer.from(s, 'hex');
|
|
462
|
-
}
|
|
463
|
-
return Buffer.from(str, 'utf8');
|
|
464
|
-
}
|
|
465
|
-
throw standardErrors.rpc.invalidParams(`Not binary data: ${String(str)}`);
|
|
466
|
-
}
|
|
467
|
-
function ensureIntNumber(num) {
|
|
468
|
-
if (typeof num === 'number' && Number.isInteger(num)) {
|
|
469
|
-
return IntNumber(num);
|
|
470
|
-
}
|
|
471
|
-
if (typeof num === 'string') {
|
|
472
|
-
if (INT_STRING_REGEX.test(num)) {
|
|
473
|
-
return IntNumber(Number(num));
|
|
474
|
-
}
|
|
475
|
-
if (isHexString$1(num)) {
|
|
476
|
-
return IntNumber(Number(BigInt(ensureEvenLengthHexString(num, true))));
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
throw standardErrors.rpc.invalidParams(`Not an integer: ${String(num)}`);
|
|
480
|
-
}
|
|
481
|
-
function ensureBigInt(val) {
|
|
482
|
-
if (val !== null && (typeof val === 'bigint' || isBigNumber(val))) {
|
|
483
|
-
return BigInt(val.toString(10));
|
|
484
|
-
}
|
|
485
|
-
if (typeof val === 'number') {
|
|
486
|
-
return BigInt(ensureIntNumber(val));
|
|
487
|
-
}
|
|
488
|
-
if (typeof val === 'string') {
|
|
489
|
-
if (INT_STRING_REGEX.test(val)) {
|
|
490
|
-
return BigInt(val);
|
|
491
|
-
}
|
|
492
|
-
if (isHexString$1(val)) {
|
|
493
|
-
return BigInt(ensureEvenLengthHexString(val, true));
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
throw standardErrors.rpc.invalidParams(`Not an integer: ${String(val)}`);
|
|
497
|
-
}
|
|
498
|
-
function ensureParsedJSONObject(val) {
|
|
499
|
-
if (typeof val === 'string') {
|
|
500
|
-
return JSON.parse(val);
|
|
501
|
-
}
|
|
502
|
-
if (typeof val === 'object') {
|
|
503
|
-
return val;
|
|
504
|
-
}
|
|
505
|
-
throw standardErrors.rpc.invalidParams(`Not a JSON string or an object: ${String(val)}`);
|
|
506
|
-
}
|
|
507
|
-
function isBigNumber(val) {
|
|
508
|
-
if (val == null || typeof val.constructor !== 'function') {
|
|
509
|
-
return false;
|
|
510
|
-
}
|
|
511
|
-
const { constructor } = val;
|
|
512
|
-
return typeof constructor.config === 'function' && typeof constructor.EUCLID === 'number';
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
async function generateKeyPair() {
|
|
516
|
-
return crypto.subtle.generateKey({
|
|
517
|
-
name: 'ECDH',
|
|
518
|
-
namedCurve: 'P-256',
|
|
519
|
-
}, true, ['deriveKey']);
|
|
520
|
-
}
|
|
521
|
-
async function deriveSharedSecret(ownPrivateKey, peerPublicKey) {
|
|
522
|
-
return crypto.subtle.deriveKey({
|
|
523
|
-
name: 'ECDH',
|
|
524
|
-
public: peerPublicKey,
|
|
525
|
-
}, ownPrivateKey, {
|
|
526
|
-
name: 'AES-GCM',
|
|
527
|
-
length: 256,
|
|
528
|
-
}, false, ['encrypt', 'decrypt']);
|
|
529
|
-
}
|
|
530
|
-
async function encrypt(sharedSecret, plainText) {
|
|
531
|
-
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
532
|
-
const cipherText = await crypto.subtle.encrypt({
|
|
533
|
-
name: 'AES-GCM',
|
|
534
|
-
iv,
|
|
535
|
-
}, sharedSecret, new TextEncoder().encode(plainText));
|
|
536
|
-
return { iv, cipherText };
|
|
537
|
-
}
|
|
538
|
-
async function decrypt(sharedSecret, { iv, cipherText }) {
|
|
539
|
-
const plainText = await crypto.subtle.decrypt({
|
|
540
|
-
name: 'AES-GCM',
|
|
541
|
-
iv,
|
|
542
|
-
}, sharedSecret, cipherText);
|
|
543
|
-
return new TextDecoder().decode(plainText);
|
|
544
|
-
}
|
|
545
|
-
function getFormat(keyType) {
|
|
546
|
-
switch (keyType) {
|
|
547
|
-
case 'public':
|
|
548
|
-
return 'spki';
|
|
549
|
-
case 'private':
|
|
550
|
-
return 'pkcs8';
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
async function exportKeyToHexString(type, key) {
|
|
554
|
-
const format = getFormat(type);
|
|
555
|
-
const exported = await crypto.subtle.exportKey(format, key);
|
|
556
|
-
return uint8ArrayToHex(new Uint8Array(exported));
|
|
557
|
-
}
|
|
558
|
-
async function importKeyFromHexString(type, hexString) {
|
|
559
|
-
const format = getFormat(type);
|
|
560
|
-
const arrayBuffer = hexStringToUint8Array(hexString).buffer;
|
|
561
|
-
return await crypto.subtle.importKey(format, new Uint8Array(arrayBuffer), {
|
|
562
|
-
name: 'ECDH',
|
|
563
|
-
namedCurve: 'P-256',
|
|
564
|
-
}, true, type === 'private' ? ['deriveKey'] : []);
|
|
565
|
-
}
|
|
566
|
-
async function encryptContent(content, sharedSecret) {
|
|
567
|
-
const serialized = JSON.stringify(content, (_, value) => {
|
|
568
|
-
if (!(value instanceof Error))
|
|
569
|
-
return value;
|
|
570
|
-
const error = value;
|
|
571
|
-
return Object.assign(Object.assign({}, (error.code ? { code: error.code } : {})), { message: error.message });
|
|
572
|
-
});
|
|
573
|
-
return encrypt(sharedSecret, serialized);
|
|
574
|
-
}
|
|
575
|
-
async function decryptContent(encryptedData, sharedSecret) {
|
|
576
|
-
return JSON.parse(await decrypt(sharedSecret, encryptedData));
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
const OWN_PRIVATE_KEY = {
|
|
580
|
-
storageKey: 'ownPrivateKey',
|
|
581
|
-
keyType: 'private',
|
|
582
|
-
};
|
|
583
|
-
const OWN_PUBLIC_KEY = {
|
|
584
|
-
storageKey: 'ownPublicKey',
|
|
585
|
-
keyType: 'public',
|
|
586
|
-
};
|
|
587
|
-
const PEER_PUBLIC_KEY = {
|
|
588
|
-
storageKey: 'peerPublicKey',
|
|
589
|
-
keyType: 'public',
|
|
590
|
-
};
|
|
591
|
-
class SCWKeyManager {
|
|
592
|
-
constructor() {
|
|
593
|
-
this.storage = new ScopedLocalStorage('CBWSDK', 'SCWKeyManager');
|
|
594
|
-
this.ownPrivateKey = null;
|
|
595
|
-
this.ownPublicKey = null;
|
|
596
|
-
this.peerPublicKey = null;
|
|
597
|
-
this.sharedSecret = null;
|
|
598
|
-
}
|
|
599
|
-
async getOwnPublicKey() {
|
|
600
|
-
await this.loadKeysIfNeeded();
|
|
601
|
-
return this.ownPublicKey;
|
|
602
|
-
}
|
|
603
|
-
// returns null if the shared secret is not yet derived
|
|
604
|
-
async getSharedSecret() {
|
|
605
|
-
await this.loadKeysIfNeeded();
|
|
606
|
-
return this.sharedSecret;
|
|
607
|
-
}
|
|
608
|
-
async setPeerPublicKey(key) {
|
|
609
|
-
this.sharedSecret = null;
|
|
610
|
-
this.peerPublicKey = key;
|
|
611
|
-
await this.storeKey(PEER_PUBLIC_KEY, key);
|
|
612
|
-
await this.loadKeysIfNeeded();
|
|
613
|
-
}
|
|
614
|
-
async clear() {
|
|
615
|
-
this.ownPrivateKey = null;
|
|
616
|
-
this.ownPublicKey = null;
|
|
617
|
-
this.peerPublicKey = null;
|
|
618
|
-
this.sharedSecret = null;
|
|
619
|
-
this.storage.removeItem(OWN_PUBLIC_KEY.storageKey);
|
|
620
|
-
this.storage.removeItem(OWN_PRIVATE_KEY.storageKey);
|
|
621
|
-
this.storage.removeItem(PEER_PUBLIC_KEY.storageKey);
|
|
622
|
-
}
|
|
623
|
-
async generateKeyPair() {
|
|
624
|
-
const newKeyPair = await generateKeyPair();
|
|
625
|
-
this.ownPrivateKey = newKeyPair.privateKey;
|
|
626
|
-
this.ownPublicKey = newKeyPair.publicKey;
|
|
627
|
-
await this.storeKey(OWN_PRIVATE_KEY, newKeyPair.privateKey);
|
|
628
|
-
await this.storeKey(OWN_PUBLIC_KEY, newKeyPair.publicKey);
|
|
629
|
-
}
|
|
630
|
-
async loadKeysIfNeeded() {
|
|
631
|
-
if (this.ownPrivateKey === null) {
|
|
632
|
-
this.ownPrivateKey = await this.loadKey(OWN_PRIVATE_KEY);
|
|
633
|
-
}
|
|
634
|
-
if (this.ownPublicKey === null) {
|
|
635
|
-
this.ownPublicKey = await this.loadKey(OWN_PUBLIC_KEY);
|
|
636
|
-
}
|
|
637
|
-
if (this.ownPrivateKey === null || this.ownPublicKey === null) {
|
|
638
|
-
await this.generateKeyPair();
|
|
639
|
-
}
|
|
640
|
-
if (this.peerPublicKey === null) {
|
|
641
|
-
this.peerPublicKey = await this.loadKey(PEER_PUBLIC_KEY);
|
|
642
|
-
}
|
|
643
|
-
if (this.sharedSecret === null) {
|
|
644
|
-
if (this.ownPrivateKey === null || this.peerPublicKey === null)
|
|
645
|
-
return;
|
|
646
|
-
this.sharedSecret = await deriveSharedSecret(this.ownPrivateKey, this.peerPublicKey);
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
// storage methods
|
|
650
|
-
async loadKey(item) {
|
|
651
|
-
const key = this.storage.getItem(item.storageKey);
|
|
652
|
-
if (!key)
|
|
653
|
-
return null;
|
|
654
|
-
return importKeyFromHexString(item.keyType, key);
|
|
655
|
-
}
|
|
656
|
-
async storeKey(item, key) {
|
|
657
|
-
const hexString = await exportKeyToHexString(item.keyType, key);
|
|
658
|
-
this.storage.setItem(item.storageKey, hexString);
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
const VERSION = '4.2.3';
|
|
663
|
-
const NAME = '@coinbase/wallet-sdk';
|
|
664
|
-
|
|
665
|
-
async function fetchRPCRequest(request, rpcUrl) {
|
|
666
|
-
const requestBody = Object.assign(Object.assign({}, request), { jsonrpc: '2.0', id: crypto.randomUUID() });
|
|
667
|
-
const res = await window.fetch(rpcUrl, {
|
|
668
|
-
method: 'POST',
|
|
669
|
-
body: JSON.stringify(requestBody),
|
|
670
|
-
mode: 'cors',
|
|
671
|
-
headers: {
|
|
672
|
-
'Content-Type': 'application/json',
|
|
673
|
-
'X-Cbw-Sdk-Version': VERSION,
|
|
674
|
-
'X-Cbw-Sdk-Platform': NAME,
|
|
675
|
-
},
|
|
676
|
-
});
|
|
677
|
-
const { result, error } = await res.json();
|
|
678
|
-
if (error)
|
|
679
|
-
throw error;
|
|
680
|
-
return result;
|
|
681
|
-
}
|
|
682
|
-
function getCoinbaseInjectedLegacyProvider() {
|
|
683
|
-
const window = globalThis;
|
|
684
|
-
return window.coinbaseWalletExtension;
|
|
685
|
-
}
|
|
686
|
-
function getInjectedEthereum() {
|
|
687
|
-
var _a, _b;
|
|
688
|
-
try {
|
|
689
|
-
const window = globalThis;
|
|
690
|
-
return (_a = window.ethereum) !== null && _a !== void 0 ? _a : (_b = window.top) === null || _b === void 0 ? void 0 : _b.ethereum;
|
|
691
|
-
}
|
|
692
|
-
catch (_c) {
|
|
693
|
-
return undefined;
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
function getCoinbaseInjectedProvider({ metadata, preference, }) {
|
|
697
|
-
var _a, _b;
|
|
698
|
-
const { appName, appLogoUrl, appChainIds } = metadata;
|
|
699
|
-
if (preference.options !== 'smartWalletOnly') {
|
|
700
|
-
const extension = getCoinbaseInjectedLegacyProvider();
|
|
701
|
-
if (extension) {
|
|
702
|
-
(_a = extension.setAppInfo) === null || _a === void 0 ? void 0 : _a.call(extension, appName, appLogoUrl, appChainIds, preference);
|
|
703
|
-
return extension;
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
const ethereum = getInjectedEthereum();
|
|
707
|
-
if (ethereum === null || ethereum === void 0 ? void 0 : ethereum.isCoinbaseBrowser) {
|
|
708
|
-
(_b = ethereum.setAppInfo) === null || _b === void 0 ? void 0 : _b.call(ethereum, appName, appLogoUrl, appChainIds, preference);
|
|
709
|
-
return ethereum;
|
|
710
|
-
}
|
|
711
|
-
return undefined;
|
|
712
|
-
}
|
|
713
|
-
/**
|
|
714
|
-
* Validates the arguments for an invalid request and returns an error if any validation fails.
|
|
715
|
-
* Valid request args are defined here: https://eips.ethereum.org/EIPS/eip-1193#request
|
|
716
|
-
* @param args The request arguments to validate.
|
|
717
|
-
* @returns An error object if the arguments are invalid, otherwise undefined.
|
|
718
|
-
*/
|
|
719
|
-
function checkErrorForInvalidRequestArgs(args) {
|
|
720
|
-
if (!args || typeof args !== 'object' || Array.isArray(args)) {
|
|
721
|
-
throw standardErrors.rpc.invalidParams({
|
|
722
|
-
message: 'Expected a single, non-array, object argument.',
|
|
723
|
-
data: args,
|
|
724
|
-
});
|
|
725
|
-
}
|
|
726
|
-
const { method, params } = args;
|
|
727
|
-
if (typeof method !== 'string' || method.length === 0) {
|
|
728
|
-
throw standardErrors.rpc.invalidParams({
|
|
729
|
-
message: "'args.method' must be a non-empty string.",
|
|
730
|
-
data: args,
|
|
731
|
-
});
|
|
732
|
-
}
|
|
733
|
-
if (params !== undefined &&
|
|
734
|
-
!Array.isArray(params) &&
|
|
735
|
-
(typeof params !== 'object' || params === null)) {
|
|
736
|
-
throw standardErrors.rpc.invalidParams({
|
|
737
|
-
message: "'args.params' must be an object or array if provided.",
|
|
738
|
-
data: args,
|
|
739
|
-
});
|
|
740
|
-
}
|
|
741
|
-
switch (method) {
|
|
742
|
-
case 'eth_sign':
|
|
743
|
-
case 'eth_signTypedData_v2':
|
|
744
|
-
case 'eth_subscribe':
|
|
745
|
-
case 'eth_unsubscribe':
|
|
746
|
-
throw standardErrors.provider.unsupportedMethod();
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
|
|
750
|
-
const ACCOUNTS_KEY = 'accounts';
|
|
751
|
-
const ACTIVE_CHAIN_STORAGE_KEY = 'activeChain';
|
|
752
|
-
const AVAILABLE_CHAINS_STORAGE_KEY = 'availableChains';
|
|
753
|
-
const WALLET_CAPABILITIES_STORAGE_KEY = 'walletCapabilities';
|
|
754
|
-
class SCWSigner {
|
|
755
|
-
constructor(params) {
|
|
756
|
-
var _a, _b, _c;
|
|
757
|
-
this.metadata = params.metadata;
|
|
758
|
-
this.communicator = params.communicator;
|
|
759
|
-
this.callback = params.callback;
|
|
760
|
-
this.keyManager = new SCWKeyManager();
|
|
761
|
-
this.storage = new ScopedLocalStorage('CBWSDK', 'SCWStateManager');
|
|
762
|
-
this.accounts = (_a = this.storage.loadObject(ACCOUNTS_KEY)) !== null && _a !== void 0 ? _a : [];
|
|
763
|
-
this.chain = this.storage.loadObject(ACTIVE_CHAIN_STORAGE_KEY) || {
|
|
764
|
-
id: (_c = (_b = params.metadata.appChainIds) === null || _b === void 0 ? void 0 : _b[0]) !== null && _c !== void 0 ? _c : 1,
|
|
765
|
-
};
|
|
766
|
-
this.handshake = this.handshake.bind(this);
|
|
767
|
-
this.request = this.request.bind(this);
|
|
768
|
-
this.createRequestMessage = this.createRequestMessage.bind(this);
|
|
769
|
-
this.decryptResponseMessage = this.decryptResponseMessage.bind(this);
|
|
770
|
-
}
|
|
771
|
-
async handshake(args) {
|
|
772
|
-
var _a, _b;
|
|
773
|
-
const handshakeMessage = await this.createRequestMessage({
|
|
774
|
-
handshake: {
|
|
775
|
-
method: args.method,
|
|
776
|
-
params: Object.assign({}, this.metadata, (_a = args.params) !== null && _a !== void 0 ? _a : {}),
|
|
777
|
-
},
|
|
778
|
-
});
|
|
779
|
-
const response = await this.communicator.postRequestAndWaitForResponse(handshakeMessage);
|
|
780
|
-
// store peer's public key
|
|
781
|
-
if ('failure' in response.content)
|
|
782
|
-
throw response.content.failure;
|
|
783
|
-
const peerPublicKey = await importKeyFromHexString('public', response.sender);
|
|
784
|
-
await this.keyManager.setPeerPublicKey(peerPublicKey);
|
|
785
|
-
const decrypted = await this.decryptResponseMessage(response);
|
|
786
|
-
const result = decrypted.result;
|
|
787
|
-
if ('error' in result)
|
|
788
|
-
throw result.error;
|
|
789
|
-
const accounts = result.value;
|
|
790
|
-
this.accounts = accounts;
|
|
791
|
-
this.storage.storeObject(ACCOUNTS_KEY, accounts);
|
|
792
|
-
(_b = this.callback) === null || _b === void 0 ? void 0 : _b.call(this, 'accountsChanged', accounts);
|
|
793
|
-
}
|
|
794
|
-
async request(request) {
|
|
795
|
-
var _a;
|
|
796
|
-
if (this.accounts.length === 0) {
|
|
797
|
-
throw standardErrors.provider.unauthorized();
|
|
798
|
-
}
|
|
799
|
-
switch (request.method) {
|
|
800
|
-
case 'eth_requestAccounts':
|
|
801
|
-
(_a = this.callback) === null || _a === void 0 ? void 0 : _a.call(this, 'connect', { chainId: hexStringFromNumber(this.chain.id) });
|
|
802
|
-
return this.accounts;
|
|
803
|
-
case 'eth_accounts':
|
|
804
|
-
return this.accounts;
|
|
805
|
-
case 'eth_coinbase':
|
|
806
|
-
return this.accounts[0];
|
|
807
|
-
case 'net_version':
|
|
808
|
-
return this.chain.id;
|
|
809
|
-
case 'eth_chainId':
|
|
810
|
-
return hexStringFromNumber(this.chain.id);
|
|
811
|
-
case 'wallet_getCapabilities':
|
|
812
|
-
return this.storage.loadObject(WALLET_CAPABILITIES_STORAGE_KEY);
|
|
813
|
-
case 'wallet_switchEthereumChain':
|
|
814
|
-
return this.handleSwitchChainRequest(request);
|
|
815
|
-
case 'eth_ecRecover':
|
|
816
|
-
case 'personal_sign':
|
|
817
|
-
case 'personal_ecRecover':
|
|
818
|
-
case 'eth_signTransaction':
|
|
819
|
-
case 'eth_sendTransaction':
|
|
820
|
-
case 'eth_signTypedData_v1':
|
|
821
|
-
case 'eth_signTypedData_v3':
|
|
822
|
-
case 'eth_signTypedData_v4':
|
|
823
|
-
case 'eth_signTypedData':
|
|
824
|
-
case 'wallet_addEthereumChain':
|
|
825
|
-
case 'wallet_watchAsset':
|
|
826
|
-
case 'wallet_sendCalls':
|
|
827
|
-
case 'wallet_showCallsStatus':
|
|
828
|
-
case 'wallet_grantPermissions':
|
|
829
|
-
return this.sendRequestToPopup(request);
|
|
830
|
-
default:
|
|
831
|
-
if (!this.chain.rpcUrl)
|
|
832
|
-
throw standardErrors.rpc.internal('No RPC URL set for chain');
|
|
833
|
-
return fetchRPCRequest(request, this.chain.rpcUrl);
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
async sendRequestToPopup(request) {
|
|
837
|
-
var _a, _b;
|
|
838
|
-
// Open the popup before constructing the request message.
|
|
839
|
-
// This is to ensure that the popup is not blocked by some browsers (i.e. Safari)
|
|
840
|
-
await ((_b = (_a = this.communicator).waitForPopupLoaded) === null || _b === void 0 ? void 0 : _b.call(_a));
|
|
841
|
-
const response = await this.sendEncryptedRequest(request);
|
|
842
|
-
const decrypted = await this.decryptResponseMessage(response);
|
|
843
|
-
const result = decrypted.result;
|
|
844
|
-
if ('error' in result)
|
|
845
|
-
throw result.error;
|
|
846
|
-
return result.value;
|
|
847
|
-
}
|
|
848
|
-
async cleanup() {
|
|
849
|
-
var _a, _b;
|
|
850
|
-
this.storage.clear();
|
|
851
|
-
await this.keyManager.clear();
|
|
852
|
-
this.accounts = [];
|
|
853
|
-
this.chain = {
|
|
854
|
-
id: (_b = (_a = this.metadata.appChainIds) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : 1,
|
|
855
|
-
};
|
|
856
|
-
}
|
|
857
|
-
/**
|
|
858
|
-
* @returns `null` if the request was successful.
|
|
859
|
-
* https://eips.ethereum.org/EIPS/eip-3326#wallet_switchethereumchain
|
|
860
|
-
*/
|
|
861
|
-
async handleSwitchChainRequest(request) {
|
|
862
|
-
var _a;
|
|
863
|
-
const params = request.params;
|
|
864
|
-
if (!params || !((_a = params[0]) === null || _a === void 0 ? void 0 : _a.chainId)) {
|
|
865
|
-
throw standardErrors.rpc.invalidParams();
|
|
866
|
-
}
|
|
867
|
-
const chainId = ensureIntNumber(params[0].chainId);
|
|
868
|
-
const localResult = this.updateChain(chainId);
|
|
869
|
-
if (localResult)
|
|
870
|
-
return null;
|
|
871
|
-
const popupResult = await this.sendRequestToPopup(request);
|
|
872
|
-
if (popupResult === null) {
|
|
873
|
-
this.updateChain(chainId);
|
|
874
|
-
}
|
|
875
|
-
return popupResult;
|
|
876
|
-
}
|
|
877
|
-
async sendEncryptedRequest(request) {
|
|
878
|
-
const sharedSecret = await this.keyManager.getSharedSecret();
|
|
879
|
-
if (!sharedSecret) {
|
|
880
|
-
throw standardErrors.provider.unauthorized('No valid session found, try requestAccounts before other methods');
|
|
881
|
-
}
|
|
882
|
-
const encrypted = await encryptContent({
|
|
883
|
-
action: request,
|
|
884
|
-
chainId: this.chain.id,
|
|
885
|
-
}, sharedSecret);
|
|
886
|
-
const message = await this.createRequestMessage({ encrypted });
|
|
887
|
-
return this.communicator.postRequestAndWaitForResponse(message);
|
|
888
|
-
}
|
|
889
|
-
async createRequestMessage(content) {
|
|
890
|
-
const publicKey = await exportKeyToHexString('public', await this.keyManager.getOwnPublicKey());
|
|
891
|
-
return {
|
|
892
|
-
id: crypto.randomUUID(),
|
|
893
|
-
sender: publicKey,
|
|
894
|
-
content,
|
|
895
|
-
timestamp: new Date(),
|
|
896
|
-
};
|
|
897
|
-
}
|
|
898
|
-
async decryptResponseMessage(message) {
|
|
899
|
-
var _a, _b;
|
|
900
|
-
const content = message.content;
|
|
901
|
-
// throw protocol level error
|
|
902
|
-
if ('failure' in content) {
|
|
903
|
-
throw content.failure;
|
|
904
|
-
}
|
|
905
|
-
const sharedSecret = await this.keyManager.getSharedSecret();
|
|
906
|
-
if (!sharedSecret) {
|
|
907
|
-
throw standardErrors.provider.unauthorized('Invalid session');
|
|
908
|
-
}
|
|
909
|
-
const response = await decryptContent(content.encrypted, sharedSecret);
|
|
910
|
-
const availableChains = (_a = response.data) === null || _a === void 0 ? void 0 : _a.chains;
|
|
911
|
-
if (availableChains) {
|
|
912
|
-
const chains = Object.entries(availableChains).map(([id, rpcUrl]) => ({
|
|
913
|
-
id: Number(id),
|
|
914
|
-
rpcUrl,
|
|
915
|
-
}));
|
|
916
|
-
this.storage.storeObject(AVAILABLE_CHAINS_STORAGE_KEY, chains);
|
|
917
|
-
this.updateChain(this.chain.id, chains);
|
|
918
|
-
}
|
|
919
|
-
const walletCapabilities = (_b = response.data) === null || _b === void 0 ? void 0 : _b.capabilities;
|
|
920
|
-
if (walletCapabilities) {
|
|
921
|
-
this.storage.storeObject(WALLET_CAPABILITIES_STORAGE_KEY, walletCapabilities);
|
|
922
|
-
}
|
|
923
|
-
return response;
|
|
924
|
-
}
|
|
925
|
-
updateChain(chainId, newAvailableChains) {
|
|
926
|
-
var _a;
|
|
927
|
-
const chains = newAvailableChains !== null && newAvailableChains !== void 0 ? newAvailableChains : this.storage.loadObject(AVAILABLE_CHAINS_STORAGE_KEY);
|
|
928
|
-
const chain = chains === null || chains === void 0 ? void 0 : chains.find((chain) => chain.id === chainId);
|
|
929
|
-
if (!chain)
|
|
930
|
-
return false;
|
|
931
|
-
if (chain !== this.chain) {
|
|
932
|
-
this.chain = chain;
|
|
933
|
-
this.storage.storeObject(ACTIVE_CHAIN_STORAGE_KEY, chain);
|
|
934
|
-
(_a = this.callback) === null || _a === void 0 ? void 0 : _a.call(this, 'chainChanged', hexStringFromNumber(chain.id));
|
|
935
|
-
}
|
|
936
|
-
return true;
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
var sha3 = {};
|
|
941
|
-
|
|
942
|
-
var _assert = {};
|
|
943
|
-
|
|
944
|
-
Object.defineProperty(_assert, "__esModule", { value: true });
|
|
945
|
-
_assert.isBytes = isBytes$1;
|
|
946
|
-
_assert.number = number;
|
|
947
|
-
_assert.bool = bool;
|
|
948
|
-
_assert.bytes = bytes$1;
|
|
949
|
-
_assert.hash = hash;
|
|
950
|
-
_assert.exists = exists$1;
|
|
951
|
-
_assert.output = output$1;
|
|
952
|
-
function number(n) {
|
|
953
|
-
if (!Number.isSafeInteger(n) || n < 0)
|
|
954
|
-
throw new Error(`positive integer expected, not ${n}`);
|
|
955
|
-
}
|
|
956
|
-
function bool(b) {
|
|
957
|
-
if (typeof b !== 'boolean')
|
|
958
|
-
throw new Error(`boolean expected, not ${b}`);
|
|
959
|
-
}
|
|
960
|
-
// copied from utils
|
|
961
|
-
function isBytes$1(a) {
|
|
962
|
-
return (a instanceof Uint8Array ||
|
|
963
|
-
(a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
|
|
964
|
-
}
|
|
965
|
-
function bytes$1(b, ...lengths) {
|
|
966
|
-
if (!isBytes$1(b))
|
|
967
|
-
throw new Error('Uint8Array expected');
|
|
968
|
-
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
969
|
-
throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`);
|
|
970
|
-
}
|
|
971
|
-
function hash(h) {
|
|
972
|
-
if (typeof h !== 'function' || typeof h.create !== 'function')
|
|
973
|
-
throw new Error('Hash should be wrapped by utils.wrapConstructor');
|
|
974
|
-
number(h.outputLen);
|
|
975
|
-
number(h.blockLen);
|
|
976
|
-
}
|
|
977
|
-
function exists$1(instance, checkFinished = true) {
|
|
978
|
-
if (instance.destroyed)
|
|
979
|
-
throw new Error('Hash instance has been destroyed');
|
|
980
|
-
if (checkFinished && instance.finished)
|
|
981
|
-
throw new Error('Hash#digest() has already been called');
|
|
982
|
-
}
|
|
983
|
-
function output$1(out, instance) {
|
|
984
|
-
bytes$1(out);
|
|
985
|
-
const min = instance.outputLen;
|
|
986
|
-
if (out.length < min) {
|
|
987
|
-
throw new Error(`digestInto() expects output buffer of length at least ${min}`);
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
const assert = { number, bool, bytes: bytes$1, hash, exists: exists$1, output: output$1 };
|
|
991
|
-
_assert.default = assert;
|
|
992
|
-
|
|
993
|
-
var _u64 = {};
|
|
994
|
-
|
|
995
|
-
Object.defineProperty(_u64, "__esModule", { value: true });
|
|
996
|
-
_u64.add5L = _u64.add5H = _u64.add4H = _u64.add4L = _u64.add3H = _u64.add3L = _u64.rotlBL = _u64.rotlBH = _u64.rotlSL = _u64.rotlSH = _u64.rotr32L = _u64.rotr32H = _u64.rotrBL = _u64.rotrBH = _u64.rotrSL = _u64.rotrSH = _u64.shrSL = _u64.shrSH = _u64.toBig = void 0;
|
|
997
|
-
_u64.fromBig = fromBig;
|
|
998
|
-
_u64.split = split;
|
|
999
|
-
_u64.add = add;
|
|
1000
|
-
const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
1001
|
-
const _32n = /* @__PURE__ */ BigInt(32);
|
|
1002
|
-
// We are not using BigUint64Array, because they are extremely slow as per 2022
|
|
1003
|
-
function fromBig(n, le = false) {
|
|
1004
|
-
if (le)
|
|
1005
|
-
return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
|
|
1006
|
-
return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
1007
|
-
}
|
|
1008
|
-
function split(lst, le = false) {
|
|
1009
|
-
let Ah = new Uint32Array(lst.length);
|
|
1010
|
-
let Al = new Uint32Array(lst.length);
|
|
1011
|
-
for (let i = 0; i < lst.length; i++) {
|
|
1012
|
-
const { h, l } = fromBig(lst[i], le);
|
|
1013
|
-
[Ah[i], Al[i]] = [h, l];
|
|
1014
|
-
}
|
|
1015
|
-
return [Ah, Al];
|
|
1016
|
-
}
|
|
1017
|
-
const toBig = (h, l) => (BigInt(h >>> 0) << _32n) | BigInt(l >>> 0);
|
|
1018
|
-
_u64.toBig = toBig;
|
|
1019
|
-
// for Shift in [0, 32)
|
|
1020
|
-
const shrSH = (h, _l, s) => h >>> s;
|
|
1021
|
-
_u64.shrSH = shrSH;
|
|
1022
|
-
const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
1023
|
-
_u64.shrSL = shrSL;
|
|
1024
|
-
// Right rotate for Shift in [1, 32)
|
|
1025
|
-
const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
|
|
1026
|
-
_u64.rotrSH = rotrSH;
|
|
1027
|
-
const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
|
|
1028
|
-
_u64.rotrSL = rotrSL;
|
|
1029
|
-
// Right rotate for Shift in (32, 64), NOTE: 32 is special case.
|
|
1030
|
-
const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
|
|
1031
|
-
_u64.rotrBH = rotrBH;
|
|
1032
|
-
const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
|
|
1033
|
-
_u64.rotrBL = rotrBL;
|
|
1034
|
-
// Right rotate for shift===32 (just swaps l&h)
|
|
1035
|
-
const rotr32H = (_h, l) => l;
|
|
1036
|
-
_u64.rotr32H = rotr32H;
|
|
1037
|
-
const rotr32L = (h, _l) => h;
|
|
1038
|
-
_u64.rotr32L = rotr32L;
|
|
1039
|
-
// Left rotate for Shift in [1, 32)
|
|
1040
|
-
const rotlSH = (h, l, s) => (h << s) | (l >>> (32 - s));
|
|
1041
|
-
_u64.rotlSH = rotlSH;
|
|
1042
|
-
const rotlSL = (h, l, s) => (l << s) | (h >>> (32 - s));
|
|
1043
|
-
_u64.rotlSL = rotlSL;
|
|
1044
|
-
// Left rotate for Shift in (32, 64), NOTE: 32 is special case.
|
|
1045
|
-
const rotlBH = (h, l, s) => (l << (s - 32)) | (h >>> (64 - s));
|
|
1046
|
-
_u64.rotlBH = rotlBH;
|
|
1047
|
-
const rotlBL = (h, l, s) => (h << (s - 32)) | (l >>> (64 - s));
|
|
1048
|
-
_u64.rotlBL = rotlBL;
|
|
1049
|
-
// JS uses 32-bit signed integers for bitwise operations which means we cannot
|
|
1050
|
-
// simple take carry out of low bit sum by shift, we need to use division.
|
|
1051
|
-
function add(Ah, Al, Bh, Bl) {
|
|
1052
|
-
const l = (Al >>> 0) + (Bl >>> 0);
|
|
1053
|
-
return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
|
|
1054
|
-
}
|
|
1055
|
-
// Addition with more than 2 elements
|
|
1056
|
-
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
1057
|
-
_u64.add3L = add3L;
|
|
1058
|
-
const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
|
|
1059
|
-
_u64.add3H = add3H;
|
|
1060
|
-
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
1061
|
-
_u64.add4L = add4L;
|
|
1062
|
-
const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
|
|
1063
|
-
_u64.add4H = add4H;
|
|
1064
|
-
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
1065
|
-
_u64.add5L = add5L;
|
|
1066
|
-
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
|
|
1067
|
-
_u64.add5H = add5H;
|
|
1068
|
-
// prettier-ignore
|
|
1069
|
-
const u64 = {
|
|
1070
|
-
fromBig, split, toBig,
|
|
1071
|
-
shrSH, shrSL,
|
|
1072
|
-
rotrSH, rotrSL, rotrBH, rotrBL,
|
|
1073
|
-
rotr32H, rotr32L,
|
|
1074
|
-
rotlSH, rotlSL, rotlBH, rotlBL,
|
|
1075
|
-
add, add3L, add3H, add4L, add4H, add5H, add5L,
|
|
1076
|
-
};
|
|
1077
|
-
_u64.default = u64;
|
|
1078
|
-
|
|
1079
|
-
var utils = {};
|
|
1080
|
-
|
|
1081
|
-
var crypto$1 = {};
|
|
1082
|
-
|
|
1083
|
-
Object.defineProperty(crypto$1, "__esModule", { value: true });
|
|
1084
|
-
crypto$1.crypto = void 0;
|
|
1085
|
-
crypto$1.crypto = typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;
|
|
1086
|
-
|
|
1087
|
-
(function (exports) {
|
|
1088
|
-
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
1089
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1090
|
-
exports.Hash = exports.nextTick = exports.byteSwapIfBE = exports.byteSwap = exports.isLE = exports.rotl = exports.rotr = exports.createView = exports.u32 = exports.u8 = void 0;
|
|
1091
|
-
exports.isBytes = isBytes;
|
|
1092
|
-
exports.byteSwap32 = byteSwap32;
|
|
1093
|
-
exports.bytesToHex = bytesToHex;
|
|
1094
|
-
exports.hexToBytes = hexToBytes;
|
|
1095
|
-
exports.asyncLoop = asyncLoop;
|
|
1096
|
-
exports.utf8ToBytes = utf8ToBytes;
|
|
1097
|
-
exports.toBytes = toBytes;
|
|
1098
|
-
exports.concatBytes = concatBytes;
|
|
1099
|
-
exports.checkOpts = checkOpts;
|
|
1100
|
-
exports.wrapConstructor = wrapConstructor;
|
|
1101
|
-
exports.wrapConstructorWithOpts = wrapConstructorWithOpts;
|
|
1102
|
-
exports.wrapXOFConstructorWithOpts = wrapXOFConstructorWithOpts;
|
|
1103
|
-
exports.randomBytes = randomBytes;
|
|
1104
|
-
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
|
1105
|
-
// node.js versions earlier than v19 don't declare it in global scope.
|
|
1106
|
-
// For node.js, package.json#exports field mapping rewrites import
|
|
1107
|
-
// from `crypto` to `cryptoNode`, which imports native module.
|
|
1108
|
-
// Makes the utils un-importable in browsers without a bundler.
|
|
1109
|
-
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
|
|
1110
|
-
const crypto_1 = crypto$1;
|
|
1111
|
-
const _assert_js_1 = _assert;
|
|
1112
|
-
// export { isBytes } from './_assert.js';
|
|
1113
|
-
// We can't reuse isBytes from _assert, because somehow this causes huge perf issues
|
|
1114
|
-
function isBytes(a) {
|
|
1115
|
-
return (a instanceof Uint8Array ||
|
|
1116
|
-
(a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
|
|
1117
|
-
}
|
|
1118
|
-
// Cast array to different type
|
|
1119
|
-
const u8 = (arr) => new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
1120
|
-
exports.u8 = u8;
|
|
1121
|
-
const u32 = (arr) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
1122
|
-
exports.u32 = u32;
|
|
1123
|
-
// Cast array to view
|
|
1124
|
-
const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
1125
|
-
exports.createView = createView;
|
|
1126
|
-
// The rotate right (circular right shift) operation for uint32
|
|
1127
|
-
const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
|
|
1128
|
-
exports.rotr = rotr;
|
|
1129
|
-
// The rotate left (circular left shift) operation for uint32
|
|
1130
|
-
const rotl = (word, shift) => (word << shift) | ((word >>> (32 - shift)) >>> 0);
|
|
1131
|
-
exports.rotl = rotl;
|
|
1132
|
-
exports.isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
|
|
1133
|
-
// The byte swap operation for uint32
|
|
1134
|
-
const byteSwap = (word) => ((word << 24) & 0xff000000) |
|
|
1135
|
-
((word << 8) & 0xff0000) |
|
|
1136
|
-
((word >>> 8) & 0xff00) |
|
|
1137
|
-
((word >>> 24) & 0xff);
|
|
1138
|
-
exports.byteSwap = byteSwap;
|
|
1139
|
-
// Conditionally byte swap if on a big-endian platform
|
|
1140
|
-
exports.byteSwapIfBE = exports.isLE ? (n) => n : (n) => (0, exports.byteSwap)(n);
|
|
1141
|
-
// In place byte swap for Uint32Array
|
|
1142
|
-
function byteSwap32(arr) {
|
|
1143
|
-
for (let i = 0; i < arr.length; i++) {
|
|
1144
|
-
arr[i] = (0, exports.byteSwap)(arr[i]);
|
|
1145
|
-
}
|
|
1146
|
-
}
|
|
1147
|
-
// Array where index 0xf0 (240) is mapped to string 'f0'
|
|
1148
|
-
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
|
|
1149
|
-
/**
|
|
1150
|
-
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
1151
|
-
*/
|
|
1152
|
-
function bytesToHex(bytes) {
|
|
1153
|
-
(0, _assert_js_1.bytes)(bytes);
|
|
1154
|
-
// pre-caching improves the speed 6x
|
|
1155
|
-
let hex = '';
|
|
1156
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
1157
|
-
hex += hexes[bytes[i]];
|
|
1158
|
-
}
|
|
1159
|
-
return hex;
|
|
1160
|
-
}
|
|
1161
|
-
// We use optimized technique to convert hex string to byte array
|
|
1162
|
-
const asciis = { _0: 48, _9: 57, _A: 65, _F: 70, _a: 97, _f: 102 };
|
|
1163
|
-
function asciiToBase16(char) {
|
|
1164
|
-
if (char >= asciis._0 && char <= asciis._9)
|
|
1165
|
-
return char - asciis._0;
|
|
1166
|
-
if (char >= asciis._A && char <= asciis._F)
|
|
1167
|
-
return char - (asciis._A - 10);
|
|
1168
|
-
if (char >= asciis._a && char <= asciis._f)
|
|
1169
|
-
return char - (asciis._a - 10);
|
|
1170
|
-
return;
|
|
1171
|
-
}
|
|
1172
|
-
/**
|
|
1173
|
-
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
1174
|
-
*/
|
|
1175
|
-
function hexToBytes(hex) {
|
|
1176
|
-
if (typeof hex !== 'string')
|
|
1177
|
-
throw new Error('hex string expected, got ' + typeof hex);
|
|
1178
|
-
const hl = hex.length;
|
|
1179
|
-
const al = hl / 2;
|
|
1180
|
-
if (hl % 2)
|
|
1181
|
-
throw new Error('padded hex string expected, got unpadded hex of length ' + hl);
|
|
1182
|
-
const array = new Uint8Array(al);
|
|
1183
|
-
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
1184
|
-
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
1185
|
-
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
1186
|
-
if (n1 === undefined || n2 === undefined) {
|
|
1187
|
-
const char = hex[hi] + hex[hi + 1];
|
|
1188
|
-
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
1189
|
-
}
|
|
1190
|
-
array[ai] = n1 * 16 + n2;
|
|
1191
|
-
}
|
|
1192
|
-
return array;
|
|
1193
|
-
}
|
|
1194
|
-
// There is no setImmediate in browser and setTimeout is slow.
|
|
1195
|
-
// call of async fn will return Promise, which will be fullfiled only on
|
|
1196
|
-
// next scheduler queue processing step and this is exactly what we need.
|
|
1197
|
-
const nextTick = async () => { };
|
|
1198
|
-
exports.nextTick = nextTick;
|
|
1199
|
-
// Returns control to thread each 'tick' ms to avoid blocking
|
|
1200
|
-
async function asyncLoop(iters, tick, cb) {
|
|
1201
|
-
let ts = Date.now();
|
|
1202
|
-
for (let i = 0; i < iters; i++) {
|
|
1203
|
-
cb(i);
|
|
1204
|
-
// Date.now() is not monotonic, so in case if clock goes backwards we return return control too
|
|
1205
|
-
const diff = Date.now() - ts;
|
|
1206
|
-
if (diff >= 0 && diff < tick)
|
|
1207
|
-
continue;
|
|
1208
|
-
await (0, exports.nextTick)();
|
|
1209
|
-
ts += diff;
|
|
1210
|
-
}
|
|
1211
|
-
}
|
|
1212
|
-
/**
|
|
1213
|
-
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
1214
|
-
*/
|
|
1215
|
-
function utf8ToBytes(str) {
|
|
1216
|
-
if (typeof str !== 'string')
|
|
1217
|
-
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
|
1218
|
-
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
1219
|
-
}
|
|
1220
|
-
/**
|
|
1221
|
-
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
1222
|
-
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
1223
|
-
* Keep in mind for future mutable operations.
|
|
1224
|
-
*/
|
|
1225
|
-
function toBytes(data) {
|
|
1226
|
-
if (typeof data === 'string')
|
|
1227
|
-
data = utf8ToBytes(data);
|
|
1228
|
-
(0, _assert_js_1.bytes)(data);
|
|
1229
|
-
return data;
|
|
1230
|
-
}
|
|
1231
|
-
/**
|
|
1232
|
-
* Copies several Uint8Arrays into one.
|
|
1233
|
-
*/
|
|
1234
|
-
function concatBytes(...arrays) {
|
|
1235
|
-
let sum = 0;
|
|
1236
|
-
for (let i = 0; i < arrays.length; i++) {
|
|
1237
|
-
const a = arrays[i];
|
|
1238
|
-
(0, _assert_js_1.bytes)(a);
|
|
1239
|
-
sum += a.length;
|
|
1240
|
-
}
|
|
1241
|
-
const res = new Uint8Array(sum);
|
|
1242
|
-
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
1243
|
-
const a = arrays[i];
|
|
1244
|
-
res.set(a, pad);
|
|
1245
|
-
pad += a.length;
|
|
1246
|
-
}
|
|
1247
|
-
return res;
|
|
1248
|
-
}
|
|
1249
|
-
// For runtime check if class implements interface
|
|
1250
|
-
class Hash {
|
|
1251
|
-
// Safe version that clones internal state
|
|
1252
|
-
clone() {
|
|
1253
|
-
return this._cloneInto();
|
|
1254
|
-
}
|
|
1255
|
-
}
|
|
1256
|
-
exports.Hash = Hash;
|
|
1257
|
-
const toStr = {}.toString;
|
|
1258
|
-
function checkOpts(defaults, opts) {
|
|
1259
|
-
if (opts !== undefined && toStr.call(opts) !== '[object Object]')
|
|
1260
|
-
throw new Error('Options should be object or undefined');
|
|
1261
|
-
const merged = Object.assign(defaults, opts);
|
|
1262
|
-
return merged;
|
|
1263
|
-
}
|
|
1264
|
-
function wrapConstructor(hashCons) {
|
|
1265
|
-
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
1266
|
-
const tmp = hashCons();
|
|
1267
|
-
hashC.outputLen = tmp.outputLen;
|
|
1268
|
-
hashC.blockLen = tmp.blockLen;
|
|
1269
|
-
hashC.create = () => hashCons();
|
|
1270
|
-
return hashC;
|
|
1271
|
-
}
|
|
1272
|
-
function wrapConstructorWithOpts(hashCons) {
|
|
1273
|
-
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
|
|
1274
|
-
const tmp = hashCons({});
|
|
1275
|
-
hashC.outputLen = tmp.outputLen;
|
|
1276
|
-
hashC.blockLen = tmp.blockLen;
|
|
1277
|
-
hashC.create = (opts) => hashCons(opts);
|
|
1278
|
-
return hashC;
|
|
1279
|
-
}
|
|
1280
|
-
function wrapXOFConstructorWithOpts(hashCons) {
|
|
1281
|
-
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
|
|
1282
|
-
const tmp = hashCons({});
|
|
1283
|
-
hashC.outputLen = tmp.outputLen;
|
|
1284
|
-
hashC.blockLen = tmp.blockLen;
|
|
1285
|
-
hashC.create = (opts) => hashCons(opts);
|
|
1286
|
-
return hashC;
|
|
1287
|
-
}
|
|
1288
|
-
/**
|
|
1289
|
-
* Secure PRNG. Uses `crypto.getRandomValues`, which defers to OS.
|
|
1290
|
-
*/
|
|
1291
|
-
function randomBytes(bytesLength = 32) {
|
|
1292
|
-
if (crypto_1.crypto && typeof crypto_1.crypto.getRandomValues === 'function') {
|
|
1293
|
-
return crypto_1.crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
1294
|
-
}
|
|
1295
|
-
// Legacy Node.js compatibility
|
|
1296
|
-
if (crypto_1.crypto && typeof crypto_1.crypto.randomBytes === 'function') {
|
|
1297
|
-
return crypto_1.crypto.randomBytes(bytesLength);
|
|
1298
|
-
}
|
|
1299
|
-
throw new Error('crypto.getRandomValues must be defined');
|
|
1300
|
-
}
|
|
1301
|
-
|
|
1302
|
-
} (utils));
|
|
1303
|
-
|
|
1304
|
-
Object.defineProperty(sha3, "__esModule", { value: true });
|
|
1305
|
-
sha3.shake256 = sha3.shake128 = sha3.keccak_512 = sha3.keccak_384 = sha3.keccak_256 = sha3.keccak_224 = sha3.sha3_512 = sha3.sha3_384 = sha3.sha3_256 = sha3.sha3_224 = sha3.Keccak = void 0;
|
|
1306
|
-
sha3.keccakP = keccakP;
|
|
1307
|
-
const _assert_js_1 = _assert;
|
|
1308
|
-
const _u64_js_1 = _u64;
|
|
1309
|
-
const utils_js_1 = utils;
|
|
1310
|
-
// SHA3 (keccak) is based on a new design: basically, the internal state is bigger than output size.
|
|
1311
|
-
// It's called a sponge function.
|
|
1312
|
-
// Various per round constants calculations
|
|
1313
|
-
const SHA3_PI = [];
|
|
1314
|
-
const SHA3_ROTL = [];
|
|
1315
|
-
const _SHA3_IOTA = [];
|
|
1316
|
-
const _0n = /* @__PURE__ */ BigInt(0);
|
|
1317
|
-
const _1n = /* @__PURE__ */ BigInt(1);
|
|
1318
|
-
const _2n = /* @__PURE__ */ BigInt(2);
|
|
1319
|
-
const _7n = /* @__PURE__ */ BigInt(7);
|
|
1320
|
-
const _256n = /* @__PURE__ */ BigInt(256);
|
|
1321
|
-
const _0x71n = /* @__PURE__ */ BigInt(0x71);
|
|
1322
|
-
for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
|
|
1323
|
-
// Pi
|
|
1324
|
-
[x, y] = [y, (2 * x + 3 * y) % 5];
|
|
1325
|
-
SHA3_PI.push(2 * (5 * y + x));
|
|
1326
|
-
// Rotational
|
|
1327
|
-
SHA3_ROTL.push((((round + 1) * (round + 2)) / 2) % 64);
|
|
1328
|
-
// Iota
|
|
1329
|
-
let t = _0n;
|
|
1330
|
-
for (let j = 0; j < 7; j++) {
|
|
1331
|
-
R = ((R << _1n) ^ ((R >> _7n) * _0x71n)) % _256n;
|
|
1332
|
-
if (R & _2n)
|
|
1333
|
-
t ^= _1n << ((_1n << /* @__PURE__ */ BigInt(j)) - _1n);
|
|
1334
|
-
}
|
|
1335
|
-
_SHA3_IOTA.push(t);
|
|
1336
|
-
}
|
|
1337
|
-
const [SHA3_IOTA_H, SHA3_IOTA_L] = /* @__PURE__ */ (0, _u64_js_1.split)(_SHA3_IOTA, true);
|
|
1338
|
-
// Left rotation (without 0, 32, 64)
|
|
1339
|
-
const rotlH = (h, l, s) => (s > 32 ? (0, _u64_js_1.rotlBH)(h, l, s) : (0, _u64_js_1.rotlSH)(h, l, s));
|
|
1340
|
-
const rotlL = (h, l, s) => (s > 32 ? (0, _u64_js_1.rotlBL)(h, l, s) : (0, _u64_js_1.rotlSL)(h, l, s));
|
|
1341
|
-
// Same as keccakf1600, but allows to skip some rounds
|
|
1342
|
-
function keccakP(s, rounds = 24) {
|
|
1343
|
-
const B = new Uint32Array(5 * 2);
|
|
1344
|
-
// NOTE: all indices are x2 since we store state as u32 instead of u64 (bigints to slow in js)
|
|
1345
|
-
for (let round = 24 - rounds; round < 24; round++) {
|
|
1346
|
-
// Theta θ
|
|
1347
|
-
for (let x = 0; x < 10; x++)
|
|
1348
|
-
B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
|
|
1349
|
-
for (let x = 0; x < 10; x += 2) {
|
|
1350
|
-
const idx1 = (x + 8) % 10;
|
|
1351
|
-
const idx0 = (x + 2) % 10;
|
|
1352
|
-
const B0 = B[idx0];
|
|
1353
|
-
const B1 = B[idx0 + 1];
|
|
1354
|
-
const Th = rotlH(B0, B1, 1) ^ B[idx1];
|
|
1355
|
-
const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
|
|
1356
|
-
for (let y = 0; y < 50; y += 10) {
|
|
1357
|
-
s[x + y] ^= Th;
|
|
1358
|
-
s[x + y + 1] ^= Tl;
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1361
|
-
// Rho (ρ) and Pi (π)
|
|
1362
|
-
let curH = s[2];
|
|
1363
|
-
let curL = s[3];
|
|
1364
|
-
for (let t = 0; t < 24; t++) {
|
|
1365
|
-
const shift = SHA3_ROTL[t];
|
|
1366
|
-
const Th = rotlH(curH, curL, shift);
|
|
1367
|
-
const Tl = rotlL(curH, curL, shift);
|
|
1368
|
-
const PI = SHA3_PI[t];
|
|
1369
|
-
curH = s[PI];
|
|
1370
|
-
curL = s[PI + 1];
|
|
1371
|
-
s[PI] = Th;
|
|
1372
|
-
s[PI + 1] = Tl;
|
|
1373
|
-
}
|
|
1374
|
-
// Chi (χ)
|
|
1375
|
-
for (let y = 0; y < 50; y += 10) {
|
|
1376
|
-
for (let x = 0; x < 10; x++)
|
|
1377
|
-
B[x] = s[y + x];
|
|
1378
|
-
for (let x = 0; x < 10; x++)
|
|
1379
|
-
s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
|
|
1380
|
-
}
|
|
1381
|
-
// Iota (ι)
|
|
1382
|
-
s[0] ^= SHA3_IOTA_H[round];
|
|
1383
|
-
s[1] ^= SHA3_IOTA_L[round];
|
|
1384
|
-
}
|
|
1385
|
-
B.fill(0);
|
|
1386
|
-
}
|
|
1387
|
-
class Keccak extends utils_js_1.Hash {
|
|
1388
|
-
// NOTE: we accept arguments in bytes instead of bits here.
|
|
1389
|
-
constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
|
|
1390
|
-
super();
|
|
1391
|
-
this.blockLen = blockLen;
|
|
1392
|
-
this.suffix = suffix;
|
|
1393
|
-
this.outputLen = outputLen;
|
|
1394
|
-
this.enableXOF = enableXOF;
|
|
1395
|
-
this.rounds = rounds;
|
|
1396
|
-
this.pos = 0;
|
|
1397
|
-
this.posOut = 0;
|
|
1398
|
-
this.finished = false;
|
|
1399
|
-
this.destroyed = false;
|
|
1400
|
-
// Can be passed from user as dkLen
|
|
1401
|
-
(0, _assert_js_1.number)(outputLen);
|
|
1402
|
-
// 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes
|
|
1403
|
-
if (0 >= this.blockLen || this.blockLen >= 200)
|
|
1404
|
-
throw new Error('Sha3 supports only keccak-f1600 function');
|
|
1405
|
-
this.state = new Uint8Array(200);
|
|
1406
|
-
this.state32 = (0, utils_js_1.u32)(this.state);
|
|
1407
|
-
}
|
|
1408
|
-
keccak() {
|
|
1409
|
-
if (!utils_js_1.isLE)
|
|
1410
|
-
(0, utils_js_1.byteSwap32)(this.state32);
|
|
1411
|
-
keccakP(this.state32, this.rounds);
|
|
1412
|
-
if (!utils_js_1.isLE)
|
|
1413
|
-
(0, utils_js_1.byteSwap32)(this.state32);
|
|
1414
|
-
this.posOut = 0;
|
|
1415
|
-
this.pos = 0;
|
|
1416
|
-
}
|
|
1417
|
-
update(data) {
|
|
1418
|
-
(0, _assert_js_1.exists)(this);
|
|
1419
|
-
const { blockLen, state } = this;
|
|
1420
|
-
data = (0, utils_js_1.toBytes)(data);
|
|
1421
|
-
const len = data.length;
|
|
1422
|
-
for (let pos = 0; pos < len;) {
|
|
1423
|
-
const take = Math.min(blockLen - this.pos, len - pos);
|
|
1424
|
-
for (let i = 0; i < take; i++)
|
|
1425
|
-
state[this.pos++] ^= data[pos++];
|
|
1426
|
-
if (this.pos === blockLen)
|
|
1427
|
-
this.keccak();
|
|
1428
|
-
}
|
|
1429
|
-
return this;
|
|
1430
|
-
}
|
|
1431
|
-
finish() {
|
|
1432
|
-
if (this.finished)
|
|
1433
|
-
return;
|
|
1434
|
-
this.finished = true;
|
|
1435
|
-
const { state, suffix, pos, blockLen } = this;
|
|
1436
|
-
// Do the padding
|
|
1437
|
-
state[pos] ^= suffix;
|
|
1438
|
-
if ((suffix & 0x80) !== 0 && pos === blockLen - 1)
|
|
1439
|
-
this.keccak();
|
|
1440
|
-
state[blockLen - 1] ^= 0x80;
|
|
1441
|
-
this.keccak();
|
|
1442
|
-
}
|
|
1443
|
-
writeInto(out) {
|
|
1444
|
-
(0, _assert_js_1.exists)(this, false);
|
|
1445
|
-
(0, _assert_js_1.bytes)(out);
|
|
1446
|
-
this.finish();
|
|
1447
|
-
const bufferOut = this.state;
|
|
1448
|
-
const { blockLen } = this;
|
|
1449
|
-
for (let pos = 0, len = out.length; pos < len;) {
|
|
1450
|
-
if (this.posOut >= blockLen)
|
|
1451
|
-
this.keccak();
|
|
1452
|
-
const take = Math.min(blockLen - this.posOut, len - pos);
|
|
1453
|
-
out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
|
|
1454
|
-
this.posOut += take;
|
|
1455
|
-
pos += take;
|
|
1456
|
-
}
|
|
1457
|
-
return out;
|
|
1458
|
-
}
|
|
1459
|
-
xofInto(out) {
|
|
1460
|
-
// Sha3/Keccak usage with XOF is probably mistake, only SHAKE instances can do XOF
|
|
1461
|
-
if (!this.enableXOF)
|
|
1462
|
-
throw new Error('XOF is not possible for this instance');
|
|
1463
|
-
return this.writeInto(out);
|
|
1464
|
-
}
|
|
1465
|
-
xof(bytes) {
|
|
1466
|
-
(0, _assert_js_1.number)(bytes);
|
|
1467
|
-
return this.xofInto(new Uint8Array(bytes));
|
|
1468
|
-
}
|
|
1469
|
-
digestInto(out) {
|
|
1470
|
-
(0, _assert_js_1.output)(out, this);
|
|
1471
|
-
if (this.finished)
|
|
1472
|
-
throw new Error('digest() was already called');
|
|
1473
|
-
this.writeInto(out);
|
|
1474
|
-
this.destroy();
|
|
1475
|
-
return out;
|
|
1476
|
-
}
|
|
1477
|
-
digest() {
|
|
1478
|
-
return this.digestInto(new Uint8Array(this.outputLen));
|
|
1479
|
-
}
|
|
1480
|
-
destroy() {
|
|
1481
|
-
this.destroyed = true;
|
|
1482
|
-
this.state.fill(0);
|
|
1483
|
-
}
|
|
1484
|
-
_cloneInto(to) {
|
|
1485
|
-
const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
|
|
1486
|
-
to || (to = new Keccak(blockLen, suffix, outputLen, enableXOF, rounds));
|
|
1487
|
-
to.state32.set(this.state32);
|
|
1488
|
-
to.pos = this.pos;
|
|
1489
|
-
to.posOut = this.posOut;
|
|
1490
|
-
to.finished = this.finished;
|
|
1491
|
-
to.rounds = rounds;
|
|
1492
|
-
// Suffix can change in cSHAKE
|
|
1493
|
-
to.suffix = suffix;
|
|
1494
|
-
to.outputLen = outputLen;
|
|
1495
|
-
to.enableXOF = enableXOF;
|
|
1496
|
-
to.destroyed = this.destroyed;
|
|
1497
|
-
return to;
|
|
1498
|
-
}
|
|
1499
|
-
}
|
|
1500
|
-
sha3.Keccak = Keccak;
|
|
1501
|
-
const gen = (suffix, blockLen, outputLen) => (0, utils_js_1.wrapConstructor)(() => new Keccak(blockLen, suffix, outputLen));
|
|
1502
|
-
sha3.sha3_224 = gen(0x06, 144, 224 / 8);
|
|
1503
|
-
/**
|
|
1504
|
-
* SHA3-256 hash function
|
|
1505
|
-
* @param message - that would be hashed
|
|
1506
|
-
*/
|
|
1507
|
-
sha3.sha3_256 = gen(0x06, 136, 256 / 8);
|
|
1508
|
-
sha3.sha3_384 = gen(0x06, 104, 384 / 8);
|
|
1509
|
-
sha3.sha3_512 = gen(0x06, 72, 512 / 8);
|
|
1510
|
-
sha3.keccak_224 = gen(0x01, 144, 224 / 8);
|
|
1511
|
-
/**
|
|
1512
|
-
* keccak-256 hash function. Different from SHA3-256.
|
|
1513
|
-
* @param message - that would be hashed
|
|
1514
|
-
*/
|
|
1515
|
-
sha3.keccak_256 = gen(0x01, 136, 256 / 8);
|
|
1516
|
-
sha3.keccak_384 = gen(0x01, 104, 384 / 8);
|
|
1517
|
-
sha3.keccak_512 = gen(0x01, 72, 512 / 8);
|
|
1518
|
-
const genShake = (suffix, blockLen, outputLen) => (0, utils_js_1.wrapXOFConstructorWithOpts)((opts = {}) => new Keccak(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true));
|
|
1519
|
-
sha3.shake128 = genShake(0x1f, 168, 128 / 8);
|
|
1520
|
-
sha3.shake256 = genShake(0x1f, 136, 256 / 8);
|
|
1521
|
-
|
|
1522
|
-
// Extracted from https://github.com/ethereumjs/ethereumjs-util and stripped out irrelevant code
|
|
1523
|
-
// Original code licensed under the Mozilla Public License Version 2.0
|
|
1524
|
-
|
|
1525
|
-
/* eslint-disable */
|
|
1526
|
-
//prettier-ignore
|
|
1527
|
-
const { keccak_256 } = sha3;
|
|
1528
|
-
|
|
1529
|
-
/**
|
|
1530
|
-
* Returns a buffer filled with 0s
|
|
1531
|
-
* @method zeros
|
|
1532
|
-
* @param {Number} bytes the number of bytes the buffer should be
|
|
1533
|
-
* @return {Buffer}
|
|
1534
|
-
*/
|
|
1535
|
-
function zeros (bytes) {
|
|
1536
|
-
return Buffer.allocUnsafe(bytes).fill(0)
|
|
1537
|
-
}
|
|
1538
|
-
|
|
1539
|
-
function bitLengthFromBigInt (num) {
|
|
1540
|
-
return num.toString(2).length
|
|
1541
|
-
}
|
|
1542
|
-
|
|
1543
|
-
function bufferBEFromBigInt(num, length) {
|
|
1544
|
-
let hex = num.toString(16);
|
|
1545
|
-
// Ensure the hex string length is even
|
|
1546
|
-
if (hex.length % 2 !== 0) hex = '0' + hex;
|
|
1547
|
-
// Convert hex string to a byte array
|
|
1548
|
-
const byteArray = hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16));
|
|
1549
|
-
// Ensure the byte array is of the specified length
|
|
1550
|
-
while (byteArray.length < length) {
|
|
1551
|
-
byteArray.unshift(0); // Prepend with zeroes if shorter than required length
|
|
1552
|
-
}
|
|
1553
|
-
|
|
1554
|
-
return Buffer.from(byteArray);
|
|
1555
|
-
}
|
|
1556
|
-
|
|
1557
|
-
function twosFromBigInt(value, width) {
|
|
1558
|
-
const isNegative = value < 0n;
|
|
1559
|
-
let result;
|
|
1560
|
-
if (isNegative) {
|
|
1561
|
-
// Prepare a mask for the specified width to perform NOT operation
|
|
1562
|
-
const mask = (1n << BigInt(width)) - 1n;
|
|
1563
|
-
// Invert bits (using NOT) and add one
|
|
1564
|
-
result = (~value & mask) + 1n;
|
|
1565
|
-
} else {
|
|
1566
|
-
result = value;
|
|
1567
|
-
}
|
|
1568
|
-
// Ensure the result fits in the specified width
|
|
1569
|
-
result &= (1n << BigInt(width)) - 1n;
|
|
1570
|
-
|
|
1571
|
-
return result;
|
|
1572
|
-
}
|
|
1573
|
-
|
|
1574
|
-
/**
|
|
1575
|
-
* Left Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes.
|
|
1576
|
-
* Or it truncates the beginning if it exceeds.
|
|
1577
|
-
* @method setLength
|
|
1578
|
-
* @param {Buffer|Array} msg the value to pad
|
|
1579
|
-
* @param {Number} length the number of bytes the output should be
|
|
1580
|
-
* @param {Boolean} [right=false] whether to start padding form the left or right
|
|
1581
|
-
* @return {Buffer|Array}
|
|
1582
|
-
*/
|
|
1583
|
-
function setLength (msg, length, right) {
|
|
1584
|
-
const buf = zeros(length);
|
|
1585
|
-
msg = toBuffer(msg);
|
|
1586
|
-
if (right) {
|
|
1587
|
-
if (msg.length < length) {
|
|
1588
|
-
msg.copy(buf);
|
|
1589
|
-
return buf
|
|
1590
|
-
}
|
|
1591
|
-
return msg.slice(0, length)
|
|
1592
|
-
} else {
|
|
1593
|
-
if (msg.length < length) {
|
|
1594
|
-
msg.copy(buf, length - msg.length);
|
|
1595
|
-
return buf
|
|
1596
|
-
}
|
|
1597
|
-
return msg.slice(-length)
|
|
1598
|
-
}
|
|
1599
|
-
}
|
|
1600
|
-
|
|
1601
|
-
/**
|
|
1602
|
-
* Right Pads an `Array` or `Buffer` with leading zeros till it has `length` bytes.
|
|
1603
|
-
* Or it truncates the beginning if it exceeds.
|
|
1604
|
-
* @param {Buffer|Array} msg the value to pad
|
|
1605
|
-
* @param {Number} length the number of bytes the output should be
|
|
1606
|
-
* @return {Buffer|Array}
|
|
1607
|
-
*/
|
|
1608
|
-
function setLengthRight (msg, length) {
|
|
1609
|
-
return setLength(msg, length, true)
|
|
1610
|
-
}
|
|
1611
|
-
|
|
1612
|
-
/**
|
|
1613
|
-
* Attempts to turn a value into a `Buffer`. As input it supports `Buffer`, `String`, `Number`, null/undefined, `BIgInt` and other objects with a `toArray()` method.
|
|
1614
|
-
* @param {*} v the value
|
|
1615
|
-
*/
|
|
1616
|
-
function toBuffer (v) {
|
|
1617
|
-
if (!Buffer.isBuffer(v)) {
|
|
1618
|
-
if (Array.isArray(v)) {
|
|
1619
|
-
v = Buffer.from(v);
|
|
1620
|
-
} else if (typeof v === 'string') {
|
|
1621
|
-
if (isHexString(v)) {
|
|
1622
|
-
v = Buffer.from(padToEven(stripHexPrefix(v)), 'hex');
|
|
1623
|
-
} else {
|
|
1624
|
-
v = Buffer.from(v);
|
|
1625
|
-
}
|
|
1626
|
-
} else if (typeof v === 'number') {
|
|
1627
|
-
v = intToBuffer(v);
|
|
1628
|
-
} else if (v === null || v === undefined) {
|
|
1629
|
-
v = Buffer.allocUnsafe(0);
|
|
1630
|
-
} else if (typeof v === 'bigint') {
|
|
1631
|
-
v = bufferBEFromBigInt(v);
|
|
1632
|
-
} else if (v.toArray) {
|
|
1633
|
-
// TODO: bigint should be handled above, may remove this duplicate
|
|
1634
|
-
// converts a BigInt to a Buffer
|
|
1635
|
-
v = Buffer.from(v.toArray());
|
|
1636
|
-
} else {
|
|
1637
|
-
throw new Error('invalid type')
|
|
1638
|
-
}
|
|
1639
|
-
}
|
|
1640
|
-
return v
|
|
1641
|
-
}
|
|
1642
|
-
|
|
1643
|
-
/**
|
|
1644
|
-
* Converts a `Buffer` into a hex `String`
|
|
1645
|
-
* @param {Buffer} buf
|
|
1646
|
-
* @return {String}
|
|
1647
|
-
*/
|
|
1648
|
-
function bufferToHex (buf) {
|
|
1649
|
-
buf = toBuffer(buf);
|
|
1650
|
-
return '0x' + buf.toString('hex')
|
|
1651
|
-
}
|
|
1652
|
-
|
|
1653
|
-
/**
|
|
1654
|
-
* Creates Keccak hash of the input
|
|
1655
|
-
* @param {Buffer|Array|String|Number} a the input data
|
|
1656
|
-
* @param {Number} [bits=256] the Keccak width
|
|
1657
|
-
* @return {Buffer}
|
|
1658
|
-
*/
|
|
1659
|
-
function keccak (a, bits) {
|
|
1660
|
-
a = toBuffer(a);
|
|
1661
|
-
if (!bits) bits = 256;
|
|
1662
|
-
if (bits !== 256) {
|
|
1663
|
-
throw new Error('unsupported')
|
|
1664
|
-
}
|
|
1665
|
-
return Buffer.from(keccak_256(new Uint8Array(a)))
|
|
1666
|
-
}
|
|
1667
|
-
|
|
1668
|
-
function padToEven (str) {
|
|
1669
|
-
return str.length % 2 ? '0' + str : str
|
|
1670
|
-
}
|
|
1671
|
-
|
|
1672
|
-
function isHexString (str) {
|
|
1673
|
-
return typeof str === 'string' && str.match(/^0x[0-9A-Fa-f]*$/)
|
|
1674
|
-
}
|
|
1675
|
-
|
|
1676
|
-
function stripHexPrefix (str) {
|
|
1677
|
-
if (typeof str === 'string' && str.startsWith('0x')) {
|
|
1678
|
-
return str.slice(2)
|
|
1679
|
-
}
|
|
1680
|
-
return str
|
|
1681
|
-
}
|
|
1682
|
-
|
|
1683
|
-
var util$2 = {
|
|
1684
|
-
zeros,
|
|
1685
|
-
setLength,
|
|
1686
|
-
setLengthRight,
|
|
1687
|
-
isHexString,
|
|
1688
|
-
stripHexPrefix,
|
|
1689
|
-
toBuffer,
|
|
1690
|
-
bufferToHex,
|
|
1691
|
-
keccak,
|
|
1692
|
-
bitLengthFromBigInt,
|
|
1693
|
-
bufferBEFromBigInt,
|
|
1694
|
-
twosFromBigInt
|
|
1695
|
-
};
|
|
1696
|
-
|
|
1697
|
-
// Extracted from https://github.com/ethereumjs/ethereumjs-abi and stripped out irrelevant code
|
|
1698
|
-
// Original code licensed under the MIT License - Copyright (c) 2015 Alex Beregszaszi
|
|
1699
|
-
|
|
1700
|
-
/* eslint-disable */
|
|
1701
|
-
//prettier-ignore
|
|
1702
|
-
const util$1 = util$2;
|
|
1703
|
-
|
|
1704
|
-
// Convert from short to canonical names
|
|
1705
|
-
// FIXME: optimise or make this nicer?
|
|
1706
|
-
function elementaryName (name) {
|
|
1707
|
-
if (name.startsWith('int[')) {
|
|
1708
|
-
return 'int256' + name.slice(3)
|
|
1709
|
-
} else if (name === 'int') {
|
|
1710
|
-
return 'int256'
|
|
1711
|
-
} else if (name.startsWith('uint[')) {
|
|
1712
|
-
return 'uint256' + name.slice(4)
|
|
1713
|
-
} else if (name === 'uint') {
|
|
1714
|
-
return 'uint256'
|
|
1715
|
-
} else if (name.startsWith('fixed[')) {
|
|
1716
|
-
return 'fixed128x128' + name.slice(5)
|
|
1717
|
-
} else if (name === 'fixed') {
|
|
1718
|
-
return 'fixed128x128'
|
|
1719
|
-
} else if (name.startsWith('ufixed[')) {
|
|
1720
|
-
return 'ufixed128x128' + name.slice(6)
|
|
1721
|
-
} else if (name === 'ufixed') {
|
|
1722
|
-
return 'ufixed128x128'
|
|
1723
|
-
}
|
|
1724
|
-
return name
|
|
1725
|
-
}
|
|
1726
|
-
|
|
1727
|
-
// Parse N from type<N>
|
|
1728
|
-
function parseTypeN (type) {
|
|
1729
|
-
return Number.parseInt(/^\D+(\d+)$/.exec(type)[1], 10)
|
|
1730
|
-
}
|
|
1731
|
-
|
|
1732
|
-
// Parse N,M from type<N>x<M>
|
|
1733
|
-
function parseTypeNxM (type) {
|
|
1734
|
-
var tmp = /^\D+(\d+)x(\d+)$/.exec(type);
|
|
1735
|
-
return [ Number.parseInt(tmp[1], 10), Number.parseInt(tmp[2], 10) ]
|
|
1736
|
-
}
|
|
1737
|
-
|
|
1738
|
-
// Parse N in type[<N>] where "type" can itself be an array type.
|
|
1739
|
-
function parseTypeArray (type) {
|
|
1740
|
-
var tmp = type.match(/(.*)\[(.*?)\]$/);
|
|
1741
|
-
if (tmp) {
|
|
1742
|
-
return tmp[2] === '' ? 'dynamic' : Number.parseInt(tmp[2], 10)
|
|
1743
|
-
}
|
|
1744
|
-
return null
|
|
1745
|
-
}
|
|
1746
|
-
|
|
1747
|
-
function parseNumber (arg) {
|
|
1748
|
-
var type = typeof arg;
|
|
1749
|
-
if (type === 'string' || type === 'number') {
|
|
1750
|
-
return BigInt(arg)
|
|
1751
|
-
} else if (type === 'bigint') {
|
|
1752
|
-
return arg
|
|
1753
|
-
} else {
|
|
1754
|
-
throw new Error('Argument is not a number')
|
|
1755
|
-
}
|
|
1756
|
-
}
|
|
1757
|
-
|
|
1758
|
-
// Encodes a single item (can be dynamic array)
|
|
1759
|
-
// @returns: Buffer
|
|
1760
|
-
function encodeSingle (type, arg) {
|
|
1761
|
-
var size, num, ret, i;
|
|
1762
|
-
|
|
1763
|
-
if (type === 'address') {
|
|
1764
|
-
return encodeSingle('uint160', parseNumber(arg))
|
|
1765
|
-
} else if (type === 'bool') {
|
|
1766
|
-
return encodeSingle('uint8', arg ? 1 : 0)
|
|
1767
|
-
} else if (type === 'string') {
|
|
1768
|
-
return encodeSingle('bytes', new Buffer(arg, 'utf8'))
|
|
1769
|
-
} else if (isArray(type)) {
|
|
1770
|
-
// this part handles fixed-length ([2]) and variable length ([]) arrays
|
|
1771
|
-
// NOTE: we catch here all calls to arrays, that simplifies the rest
|
|
1772
|
-
if (typeof arg.length === 'undefined') {
|
|
1773
|
-
throw new Error('Not an array?')
|
|
1774
|
-
}
|
|
1775
|
-
size = parseTypeArray(type);
|
|
1776
|
-
if (size !== 'dynamic' && size !== 0 && arg.length > size) {
|
|
1777
|
-
throw new Error('Elements exceed array size: ' + size)
|
|
1778
|
-
}
|
|
1779
|
-
ret = [];
|
|
1780
|
-
type = type.slice(0, type.lastIndexOf('['));
|
|
1781
|
-
if (typeof arg === 'string') {
|
|
1782
|
-
arg = JSON.parse(arg);
|
|
1783
|
-
}
|
|
1784
|
-
for (i in arg) {
|
|
1785
|
-
ret.push(encodeSingle(type, arg[i]));
|
|
1786
|
-
}
|
|
1787
|
-
if (size === 'dynamic') {
|
|
1788
|
-
var length = encodeSingle('uint256', arg.length);
|
|
1789
|
-
ret.unshift(length);
|
|
1790
|
-
}
|
|
1791
|
-
return Buffer.concat(ret)
|
|
1792
|
-
} else if (type === 'bytes') {
|
|
1793
|
-
arg = new Buffer(arg);
|
|
1794
|
-
|
|
1795
|
-
ret = Buffer.concat([ encodeSingle('uint256', arg.length), arg ]);
|
|
1796
|
-
|
|
1797
|
-
if ((arg.length % 32) !== 0) {
|
|
1798
|
-
ret = Buffer.concat([ ret, util$1.zeros(32 - (arg.length % 32)) ]);
|
|
1799
|
-
}
|
|
1800
|
-
|
|
1801
|
-
return ret
|
|
1802
|
-
} else if (type.startsWith('bytes')) {
|
|
1803
|
-
size = parseTypeN(type);
|
|
1804
|
-
if (size < 1 || size > 32) {
|
|
1805
|
-
throw new Error('Invalid bytes<N> width: ' + size)
|
|
1806
|
-
}
|
|
1807
|
-
|
|
1808
|
-
return util$1.setLengthRight(arg, 32)
|
|
1809
|
-
} else if (type.startsWith('uint')) {
|
|
1810
|
-
size = parseTypeN(type);
|
|
1811
|
-
if ((size % 8) || (size < 8) || (size > 256)) {
|
|
1812
|
-
throw new Error('Invalid uint<N> width: ' + size)
|
|
1813
|
-
}
|
|
1814
|
-
|
|
1815
|
-
num = parseNumber(arg);
|
|
1816
|
-
const bitLength = util$1.bitLengthFromBigInt(num);
|
|
1817
|
-
if (bitLength > size) {
|
|
1818
|
-
throw new Error('Supplied uint exceeds width: ' + size + ' vs ' + bitLength)
|
|
1819
|
-
}
|
|
1820
|
-
|
|
1821
|
-
if (num < 0) {
|
|
1822
|
-
throw new Error('Supplied uint is negative')
|
|
1823
|
-
}
|
|
1824
|
-
|
|
1825
|
-
return util$1.bufferBEFromBigInt(num, 32);
|
|
1826
|
-
} else if (type.startsWith('int')) {
|
|
1827
|
-
size = parseTypeN(type);
|
|
1828
|
-
if ((size % 8) || (size < 8) || (size > 256)) {
|
|
1829
|
-
throw new Error('Invalid int<N> width: ' + size)
|
|
1830
|
-
}
|
|
1831
|
-
|
|
1832
|
-
num = parseNumber(arg);
|
|
1833
|
-
const bitLength = util$1.bitLengthFromBigInt(num);
|
|
1834
|
-
if (bitLength > size) {
|
|
1835
|
-
throw new Error('Supplied int exceeds width: ' + size + ' vs ' + bitLength)
|
|
1836
|
-
}
|
|
1837
|
-
|
|
1838
|
-
const twos = util$1.twosFromBigInt(num, 256);
|
|
1839
|
-
|
|
1840
|
-
return util$1.bufferBEFromBigInt(twos, 32);
|
|
1841
|
-
} else if (type.startsWith('ufixed')) {
|
|
1842
|
-
size = parseTypeNxM(type);
|
|
1843
|
-
|
|
1844
|
-
num = parseNumber(arg);
|
|
1845
|
-
|
|
1846
|
-
if (num < 0) {
|
|
1847
|
-
throw new Error('Supplied ufixed is negative')
|
|
1848
|
-
}
|
|
1849
|
-
|
|
1850
|
-
return encodeSingle('uint256', num * BigInt(2) ** BigInt(size[1]))
|
|
1851
|
-
} else if (type.startsWith('fixed')) {
|
|
1852
|
-
size = parseTypeNxM(type);
|
|
1853
|
-
|
|
1854
|
-
return encodeSingle('int256', parseNumber(arg) * BigInt(2) ** BigInt(size[1]))
|
|
1855
|
-
}
|
|
1856
|
-
|
|
1857
|
-
throw new Error('Unsupported or invalid type: ' + type)
|
|
1858
|
-
}
|
|
1859
|
-
|
|
1860
|
-
// Is a type dynamic?
|
|
1861
|
-
function isDynamic (type) {
|
|
1862
|
-
// FIXME: handle all types? I don't think anything is missing now
|
|
1863
|
-
return (type === 'string') || (type === 'bytes') || (parseTypeArray(type) === 'dynamic')
|
|
1864
|
-
}
|
|
1865
|
-
|
|
1866
|
-
// Is a type an array?
|
|
1867
|
-
function isArray (type) {
|
|
1868
|
-
return type.lastIndexOf(']') === type.length - 1
|
|
1869
|
-
}
|
|
1870
|
-
|
|
1871
|
-
// Encode a method/event with arguments
|
|
1872
|
-
// @types an array of string type names
|
|
1873
|
-
// @args an array of the appropriate values
|
|
1874
|
-
function rawEncode (types, values) {
|
|
1875
|
-
var output = [];
|
|
1876
|
-
var data = [];
|
|
1877
|
-
|
|
1878
|
-
var headLength = 32 * types.length;
|
|
1879
|
-
|
|
1880
|
-
for (var i in types) {
|
|
1881
|
-
var type = elementaryName(types[i]);
|
|
1882
|
-
var value = values[i];
|
|
1883
|
-
var cur = encodeSingle(type, value);
|
|
1884
|
-
|
|
1885
|
-
// Use the head/tail method for storing dynamic data
|
|
1886
|
-
if (isDynamic(type)) {
|
|
1887
|
-
output.push(encodeSingle('uint256', headLength));
|
|
1888
|
-
data.push(cur);
|
|
1889
|
-
headLength += cur.length;
|
|
1890
|
-
} else {
|
|
1891
|
-
output.push(cur);
|
|
1892
|
-
}
|
|
1893
|
-
}
|
|
1894
|
-
|
|
1895
|
-
return Buffer.concat(output.concat(data))
|
|
1896
|
-
}
|
|
1897
|
-
|
|
1898
|
-
function solidityPack (types, values) {
|
|
1899
|
-
if (types.length !== values.length) {
|
|
1900
|
-
throw new Error('Number of types are not matching the values')
|
|
1901
|
-
}
|
|
1902
|
-
|
|
1903
|
-
var size, num;
|
|
1904
|
-
var ret = [];
|
|
1905
|
-
|
|
1906
|
-
for (var i = 0; i < types.length; i++) {
|
|
1907
|
-
var type = elementaryName(types[i]);
|
|
1908
|
-
var value = values[i];
|
|
1909
|
-
|
|
1910
|
-
if (type === 'bytes') {
|
|
1911
|
-
ret.push(value);
|
|
1912
|
-
} else if (type === 'string') {
|
|
1913
|
-
ret.push(new Buffer(value, 'utf8'));
|
|
1914
|
-
} else if (type === 'bool') {
|
|
1915
|
-
ret.push(new Buffer(value ? '01' : '00', 'hex'));
|
|
1916
|
-
} else if (type === 'address') {
|
|
1917
|
-
ret.push(util$1.setLength(value, 20));
|
|
1918
|
-
} else if (type.startsWith('bytes')) {
|
|
1919
|
-
size = parseTypeN(type);
|
|
1920
|
-
if (size < 1 || size > 32) {
|
|
1921
|
-
throw new Error('Invalid bytes<N> width: ' + size)
|
|
1922
|
-
}
|
|
1923
|
-
|
|
1924
|
-
ret.push(util$1.setLengthRight(value, size));
|
|
1925
|
-
} else if (type.startsWith('uint')) {
|
|
1926
|
-
size = parseTypeN(type);
|
|
1927
|
-
if ((size % 8) || (size < 8) || (size > 256)) {
|
|
1928
|
-
throw new Error('Invalid uint<N> width: ' + size)
|
|
1929
|
-
}
|
|
1930
|
-
|
|
1931
|
-
num = parseNumber(value);
|
|
1932
|
-
const bitLength = util$1.bitLengthFromBigInt(num);
|
|
1933
|
-
if (bitLength > size) {
|
|
1934
|
-
throw new Error('Supplied uint exceeds width: ' + size + ' vs ' + bitLength)
|
|
1935
|
-
}
|
|
1936
|
-
|
|
1937
|
-
ret.push(util$1.bufferBEFromBigInt(num, size / 8));
|
|
1938
|
-
} else if (type.startsWith('int')) {
|
|
1939
|
-
size = parseTypeN(type);
|
|
1940
|
-
if ((size % 8) || (size < 8) || (size > 256)) {
|
|
1941
|
-
throw new Error('Invalid int<N> width: ' + size)
|
|
1942
|
-
}
|
|
1943
|
-
|
|
1944
|
-
num = parseNumber(value);
|
|
1945
|
-
const bitLength = util$1.bitLengthFromBigInt(num);
|
|
1946
|
-
if (bitLength > size) {
|
|
1947
|
-
throw new Error('Supplied int exceeds width: ' + size + ' vs ' + bitLength)
|
|
1948
|
-
}
|
|
1949
|
-
|
|
1950
|
-
const twos = util$1.twosFromBigInt(num, size);
|
|
1951
|
-
ret.push(util$1.bufferBEFromBigInt(twos, size / 8));
|
|
1952
|
-
} else {
|
|
1953
|
-
// FIXME: support all other types
|
|
1954
|
-
throw new Error('Unsupported or invalid type: ' + type)
|
|
1955
|
-
}
|
|
1956
|
-
}
|
|
1957
|
-
|
|
1958
|
-
return Buffer.concat(ret)
|
|
1959
|
-
}
|
|
1960
|
-
|
|
1961
|
-
function soliditySHA3 (types, values) {
|
|
1962
|
-
return util$1.keccak(solidityPack(types, values))
|
|
1963
|
-
}
|
|
1964
|
-
|
|
1965
|
-
var abi$1 = {
|
|
1966
|
-
rawEncode,
|
|
1967
|
-
solidityPack,
|
|
1968
|
-
soliditySHA3
|
|
1969
|
-
};
|
|
1970
|
-
|
|
1971
|
-
/* eslint-disable */
|
|
1972
|
-
|
|
1973
|
-
//prettier-ignore
|
|
1974
|
-
|
|
1975
|
-
const util = util$2;
|
|
1976
|
-
const abi = abi$1;
|
|
1977
|
-
|
|
1978
|
-
const TYPED_MESSAGE_SCHEMA = {
|
|
1979
|
-
type: 'object',
|
|
1980
|
-
properties: {
|
|
1981
|
-
types: {
|
|
1982
|
-
type: 'object',
|
|
1983
|
-
additionalProperties: {
|
|
1984
|
-
type: 'array',
|
|
1985
|
-
items: {
|
|
1986
|
-
type: 'object',
|
|
1987
|
-
properties: {
|
|
1988
|
-
name: {type: 'string'},
|
|
1989
|
-
type: {type: 'string'},
|
|
1990
|
-
},
|
|
1991
|
-
required: ['name', 'type'],
|
|
1992
|
-
},
|
|
1993
|
-
},
|
|
1994
|
-
},
|
|
1995
|
-
primaryType: {type: 'string'},
|
|
1996
|
-
domain: {type: 'object'},
|
|
1997
|
-
message: {type: 'object'},
|
|
1998
|
-
},
|
|
1999
|
-
required: ['types', 'primaryType', 'domain', 'message'],
|
|
2000
|
-
};
|
|
2001
|
-
|
|
2002
|
-
/**
|
|
2003
|
-
* A collection of utility functions used for signing typed data
|
|
2004
|
-
*/
|
|
2005
|
-
const TypedDataUtils = {
|
|
2006
|
-
/**
|
|
2007
|
-
* Encodes an object by encoding and concatenating each of its members
|
|
2008
|
-
*
|
|
2009
|
-
* @param {string} primaryType - Root type
|
|
2010
|
-
* @param {Object} data - Object to encode
|
|
2011
|
-
* @param {Object} types - Type definitions
|
|
2012
|
-
* @returns {string} - Encoded representation of an object
|
|
2013
|
-
*/
|
|
2014
|
-
encodeData (primaryType, data, types, useV4 = true) {
|
|
2015
|
-
const encodedTypes = ['bytes32'];
|
|
2016
|
-
const encodedValues = [this.hashType(primaryType, types)];
|
|
2017
|
-
|
|
2018
|
-
if(useV4) {
|
|
2019
|
-
const encodeField = (name, type, value) => {
|
|
2020
|
-
if (types[type] !== undefined) {
|
|
2021
|
-
return ['bytes32', value == null ?
|
|
2022
|
-
'0x0000000000000000000000000000000000000000000000000000000000000000' :
|
|
2023
|
-
util.keccak(this.encodeData(type, value, types, useV4))]
|
|
2024
|
-
}
|
|
2025
|
-
|
|
2026
|
-
if(value === undefined)
|
|
2027
|
-
throw new Error(`missing value for field ${name} of type ${type}`)
|
|
2028
|
-
|
|
2029
|
-
if (type === 'bytes') {
|
|
2030
|
-
return ['bytes32', util.keccak(value)]
|
|
2031
|
-
}
|
|
2032
|
-
|
|
2033
|
-
if (type === 'string') {
|
|
2034
|
-
// convert string to buffer - prevents ethUtil from interpreting strings like '0xabcd' as hex
|
|
2035
|
-
if (typeof value === 'string') {
|
|
2036
|
-
value = Buffer.from(value, 'utf8');
|
|
2037
|
-
}
|
|
2038
|
-
return ['bytes32', util.keccak(value)]
|
|
2039
|
-
}
|
|
2040
|
-
|
|
2041
|
-
if (type.lastIndexOf(']') === type.length - 1) {
|
|
2042
|
-
const parsedType = type.slice(0, type.lastIndexOf('['));
|
|
2043
|
-
const typeValuePairs = value.map(item =>
|
|
2044
|
-
encodeField(name, parsedType, item));
|
|
2045
|
-
return ['bytes32', util.keccak(abi.rawEncode(
|
|
2046
|
-
typeValuePairs.map(([type]) => type),
|
|
2047
|
-
typeValuePairs.map(([, value]) => value),
|
|
2048
|
-
))]
|
|
2049
|
-
}
|
|
2050
|
-
|
|
2051
|
-
return [type, value]
|
|
2052
|
-
};
|
|
2053
|
-
|
|
2054
|
-
for (const field of types[primaryType]) {
|
|
2055
|
-
const [type, value] = encodeField(field.name, field.type, data[field.name]);
|
|
2056
|
-
encodedTypes.push(type);
|
|
2057
|
-
encodedValues.push(value);
|
|
2058
|
-
}
|
|
2059
|
-
} else {
|
|
2060
|
-
for (const field of types[primaryType]) {
|
|
2061
|
-
let value = data[field.name];
|
|
2062
|
-
if (value !== undefined) {
|
|
2063
|
-
if (field.type === 'bytes') {
|
|
2064
|
-
encodedTypes.push('bytes32');
|
|
2065
|
-
value = util.keccak(value);
|
|
2066
|
-
encodedValues.push(value);
|
|
2067
|
-
} else if (field.type === 'string') {
|
|
2068
|
-
encodedTypes.push('bytes32');
|
|
2069
|
-
// convert string to buffer - prevents ethUtil from interpreting strings like '0xabcd' as hex
|
|
2070
|
-
if (typeof value === 'string') {
|
|
2071
|
-
value = Buffer.from(value, 'utf8');
|
|
2072
|
-
}
|
|
2073
|
-
value = util.keccak(value);
|
|
2074
|
-
encodedValues.push(value);
|
|
2075
|
-
} else if (types[field.type] !== undefined) {
|
|
2076
|
-
encodedTypes.push('bytes32');
|
|
2077
|
-
value = util.keccak(this.encodeData(field.type, value, types, useV4));
|
|
2078
|
-
encodedValues.push(value);
|
|
2079
|
-
} else if (field.type.lastIndexOf(']') === field.type.length - 1) {
|
|
2080
|
-
throw new Error('Arrays currently unimplemented in encodeData')
|
|
2081
|
-
} else {
|
|
2082
|
-
encodedTypes.push(field.type);
|
|
2083
|
-
encodedValues.push(value);
|
|
2084
|
-
}
|
|
2085
|
-
}
|
|
2086
|
-
}
|
|
2087
|
-
}
|
|
2088
|
-
|
|
2089
|
-
return abi.rawEncode(encodedTypes, encodedValues)
|
|
2090
|
-
},
|
|
2091
|
-
|
|
2092
|
-
/**
|
|
2093
|
-
* Encodes the type of an object by encoding a comma delimited list of its members
|
|
2094
|
-
*
|
|
2095
|
-
* @param {string} primaryType - Root type to encode
|
|
2096
|
-
* @param {Object} types - Type definitions
|
|
2097
|
-
* @returns {string} - Encoded representation of the type of an object
|
|
2098
|
-
*/
|
|
2099
|
-
encodeType (primaryType, types) {
|
|
2100
|
-
let result = '';
|
|
2101
|
-
let deps = this.findTypeDependencies(primaryType, types).filter(dep => dep !== primaryType);
|
|
2102
|
-
deps = [primaryType].concat(deps.sort());
|
|
2103
|
-
for (const type of deps) {
|
|
2104
|
-
const children = types[type];
|
|
2105
|
-
if (!children) {
|
|
2106
|
-
throw new Error('No type definition specified: ' + type)
|
|
2107
|
-
}
|
|
2108
|
-
result += type + '(' + types[type].map(({ name, type }) => type + ' ' + name).join(',') + ')';
|
|
2109
|
-
}
|
|
2110
|
-
return result
|
|
2111
|
-
},
|
|
2112
|
-
|
|
2113
|
-
/**
|
|
2114
|
-
* Finds all types within a type definition object
|
|
2115
|
-
*
|
|
2116
|
-
* @param {string} primaryType - Root type
|
|
2117
|
-
* @param {Object} types - Type definitions
|
|
2118
|
-
* @param {Array} results - current set of accumulated types
|
|
2119
|
-
* @returns {Array} - Set of all types found in the type definition
|
|
2120
|
-
*/
|
|
2121
|
-
findTypeDependencies (primaryType, types, results = []) {
|
|
2122
|
-
primaryType = primaryType.match(/^\w*/)[0];
|
|
2123
|
-
if (results.includes(primaryType) || types[primaryType] === undefined) { return results }
|
|
2124
|
-
results.push(primaryType);
|
|
2125
|
-
for (const field of types[primaryType]) {
|
|
2126
|
-
for (const dep of this.findTypeDependencies(field.type, types, results)) {
|
|
2127
|
-
!results.includes(dep) && results.push(dep);
|
|
2128
|
-
}
|
|
2129
|
-
}
|
|
2130
|
-
return results
|
|
2131
|
-
},
|
|
2132
|
-
|
|
2133
|
-
/**
|
|
2134
|
-
* Hashes an object
|
|
2135
|
-
*
|
|
2136
|
-
* @param {string} primaryType - Root type
|
|
2137
|
-
* @param {Object} data - Object to hash
|
|
2138
|
-
* @param {Object} types - Type definitions
|
|
2139
|
-
* @returns {Buffer} - Hash of an object
|
|
2140
|
-
*/
|
|
2141
|
-
hashStruct (primaryType, data, types, useV4 = true) {
|
|
2142
|
-
return util.keccak(this.encodeData(primaryType, data, types, useV4))
|
|
2143
|
-
},
|
|
2144
|
-
|
|
2145
|
-
/**
|
|
2146
|
-
* Hashes the type of an object
|
|
2147
|
-
*
|
|
2148
|
-
* @param {string} primaryType - Root type to hash
|
|
2149
|
-
* @param {Object} types - Type definitions
|
|
2150
|
-
* @returns {string} - Hash of an object
|
|
2151
|
-
*/
|
|
2152
|
-
hashType (primaryType, types) {
|
|
2153
|
-
return util.keccak(this.encodeType(primaryType, types))
|
|
2154
|
-
},
|
|
2155
|
-
|
|
2156
|
-
/**
|
|
2157
|
-
* Removes properties from a message object that are not defined per EIP-712
|
|
2158
|
-
*
|
|
2159
|
-
* @param {Object} data - typed message object
|
|
2160
|
-
* @returns {Object} - typed message object with only allowed fields
|
|
2161
|
-
*/
|
|
2162
|
-
sanitizeData (data) {
|
|
2163
|
-
const sanitizedData = {};
|
|
2164
|
-
for (const key in TYPED_MESSAGE_SCHEMA.properties) {
|
|
2165
|
-
data[key] && (sanitizedData[key] = data[key]);
|
|
2166
|
-
}
|
|
2167
|
-
if (sanitizedData.types) {
|
|
2168
|
-
sanitizedData.types = Object.assign({ EIP712Domain: [] }, sanitizedData.types);
|
|
2169
|
-
}
|
|
2170
|
-
return sanitizedData
|
|
2171
|
-
},
|
|
2172
|
-
|
|
2173
|
-
/**
|
|
2174
|
-
* Returns the hash of a typed message as per EIP-712 for signing
|
|
2175
|
-
*
|
|
2176
|
-
* @param {Object} typedData - Types message data to sign
|
|
2177
|
-
* @returns {string} - sha3 hash for signing
|
|
2178
|
-
*/
|
|
2179
|
-
hash (typedData, useV4 = true) {
|
|
2180
|
-
const sanitizedData = this.sanitizeData(typedData);
|
|
2181
|
-
const parts = [Buffer.from('1901', 'hex')];
|
|
2182
|
-
parts.push(this.hashStruct('EIP712Domain', sanitizedData.domain, sanitizedData.types, useV4));
|
|
2183
|
-
if (sanitizedData.primaryType !== 'EIP712Domain') {
|
|
2184
|
-
parts.push(this.hashStruct(sanitizedData.primaryType, sanitizedData.message, sanitizedData.types, useV4));
|
|
2185
|
-
}
|
|
2186
|
-
return util.keccak(Buffer.concat(parts))
|
|
2187
|
-
},
|
|
2188
|
-
};
|
|
2189
|
-
|
|
2190
|
-
var ethEip712Util = {
|
|
2191
|
-
TYPED_MESSAGE_SCHEMA,
|
|
2192
|
-
TypedDataUtils,
|
|
2193
|
-
|
|
2194
|
-
hashForSignTypedDataLegacy: function (msgParams) {
|
|
2195
|
-
return typedSignatureHashLegacy(msgParams.data)
|
|
2196
|
-
},
|
|
2197
|
-
|
|
2198
|
-
hashForSignTypedData_v3: function (msgParams) {
|
|
2199
|
-
return TypedDataUtils.hash(msgParams.data, false)
|
|
2200
|
-
},
|
|
2201
|
-
|
|
2202
|
-
hashForSignTypedData_v4: function (msgParams) {
|
|
2203
|
-
return TypedDataUtils.hash(msgParams.data)
|
|
2204
|
-
},
|
|
2205
|
-
};
|
|
2206
|
-
|
|
2207
|
-
/**
|
|
2208
|
-
* @param typedData - Array of data along with types, as per EIP712.
|
|
2209
|
-
* @returns Buffer
|
|
2210
|
-
*/
|
|
2211
|
-
function typedSignatureHashLegacy(typedData) {
|
|
2212
|
-
const error = new Error('Expect argument to be non-empty array');
|
|
2213
|
-
if (typeof typedData !== 'object' || !typedData.length) throw error
|
|
2214
|
-
|
|
2215
|
-
const data = typedData.map(function (e) {
|
|
2216
|
-
return e.type === 'bytes' ? util.toBuffer(e.value) : e.value
|
|
2217
|
-
});
|
|
2218
|
-
const types = typedData.map(function (e) { return e.type });
|
|
2219
|
-
const schema = typedData.map(function (e) {
|
|
2220
|
-
if (!e.name) throw error
|
|
2221
|
-
return e.type + ' ' + e.name
|
|
2222
|
-
});
|
|
2223
|
-
|
|
2224
|
-
return abi.soliditySHA3(
|
|
2225
|
-
['bytes32', 'bytes32'],
|
|
2226
|
-
[
|
|
2227
|
-
abi.soliditySHA3(new Array(typedData.length).fill('string'), schema),
|
|
2228
|
-
abi.soliditySHA3(types, data)
|
|
2229
|
-
]
|
|
2230
|
-
)
|
|
2231
|
-
}
|
|
2232
|
-
|
|
2233
|
-
var eip712 = /*@__PURE__*/getDefaultExportFromCjs(ethEip712Util);
|
|
2234
|
-
|
|
2235
|
-
const WALLET_USER_NAME_KEY = 'walletUsername';
|
|
2236
|
-
const LOCAL_STORAGE_ADDRESSES_KEY = 'Addresses';
|
|
2237
|
-
const APP_VERSION_KEY = 'AppVersion';
|
|
2238
|
-
|
|
2239
|
-
// Copyright (c) 2018-2023 Coinbase, Inc. <https://www.coinbase.com/>
|
|
2240
|
-
function isErrorResponse(response) {
|
|
2241
|
-
return response.errorMessage !== undefined;
|
|
2242
|
-
}
|
|
2243
|
-
|
|
2244
|
-
// Copyright (c) 2018-2023 Coinbase, Inc. <https://www.coinbase.com/>
|
|
2245
|
-
class WalletLinkCipher {
|
|
2246
|
-
// @param secret hex representation of 32-byte secret
|
|
2247
|
-
constructor(secret) {
|
|
2248
|
-
this.secret = secret;
|
|
2249
|
-
}
|
|
2250
|
-
/**
|
|
2251
|
-
*
|
|
2252
|
-
* @param plainText string to be encrypted
|
|
2253
|
-
* returns hex string representation of bytes in the order: initialization vector (iv),
|
|
2254
|
-
* auth tag, encrypted plaintext. IV is 12 bytes. Auth tag is 16 bytes. Remaining bytes are the
|
|
2255
|
-
* encrypted plainText.
|
|
2256
|
-
*/
|
|
2257
|
-
async encrypt(plainText) {
|
|
2258
|
-
const secret = this.secret;
|
|
2259
|
-
if (secret.length !== 64)
|
|
2260
|
-
throw Error(`secret must be 256 bits`);
|
|
2261
|
-
const ivBytes = crypto.getRandomValues(new Uint8Array(12));
|
|
2262
|
-
const secretKey = await crypto.subtle.importKey('raw', hexStringToUint8Array(secret), { name: 'aes-gcm' }, false, ['encrypt', 'decrypt']);
|
|
2263
|
-
const enc = new TextEncoder();
|
|
2264
|
-
// Will return encrypted plainText with auth tag (ie MAC or checksum) appended at the end
|
|
2265
|
-
const encryptedResult = await window.crypto.subtle.encrypt({
|
|
2266
|
-
name: 'AES-GCM',
|
|
2267
|
-
iv: ivBytes,
|
|
2268
|
-
}, secretKey, enc.encode(plainText));
|
|
2269
|
-
const tagLength = 16;
|
|
2270
|
-
const authTag = encryptedResult.slice(encryptedResult.byteLength - tagLength);
|
|
2271
|
-
const encryptedPlaintext = encryptedResult.slice(0, encryptedResult.byteLength - tagLength);
|
|
2272
|
-
const authTagBytes = new Uint8Array(authTag);
|
|
2273
|
-
const encryptedPlaintextBytes = new Uint8Array(encryptedPlaintext);
|
|
2274
|
-
const concatted = new Uint8Array([...ivBytes, ...authTagBytes, ...encryptedPlaintextBytes]);
|
|
2275
|
-
return uint8ArrayToHex(concatted);
|
|
2276
|
-
}
|
|
2277
|
-
/**
|
|
2278
|
-
*
|
|
2279
|
-
* @param cipherText hex string representation of bytes in the order: initialization vector (iv),
|
|
2280
|
-
* auth tag, encrypted plaintext. IV is 12 bytes. Auth tag is 16 bytes.
|
|
2281
|
-
*/
|
|
2282
|
-
async decrypt(cipherText) {
|
|
2283
|
-
const secret = this.secret;
|
|
2284
|
-
if (secret.length !== 64)
|
|
2285
|
-
throw Error(`secret must be 256 bits`);
|
|
2286
|
-
return new Promise((resolve, reject) => {
|
|
2287
|
-
void (async function () {
|
|
2288
|
-
const secretKey = await crypto.subtle.importKey('raw', hexStringToUint8Array(secret), { name: 'aes-gcm' }, false, ['encrypt', 'decrypt']);
|
|
2289
|
-
const encrypted = hexStringToUint8Array(cipherText);
|
|
2290
|
-
const ivBytes = encrypted.slice(0, 12);
|
|
2291
|
-
const authTagBytes = encrypted.slice(12, 28);
|
|
2292
|
-
const encryptedPlaintextBytes = encrypted.slice(28);
|
|
2293
|
-
const concattedBytes = new Uint8Array([...encryptedPlaintextBytes, ...authTagBytes]);
|
|
2294
|
-
const algo = {
|
|
2295
|
-
name: 'AES-GCM',
|
|
2296
|
-
iv: new Uint8Array(ivBytes),
|
|
2297
|
-
};
|
|
2298
|
-
try {
|
|
2299
|
-
const decrypted = await window.crypto.subtle.decrypt(algo, secretKey, concattedBytes);
|
|
2300
|
-
const decoder = new TextDecoder();
|
|
2301
|
-
resolve(decoder.decode(decrypted));
|
|
2302
|
-
}
|
|
2303
|
-
catch (err) {
|
|
2304
|
-
reject(err);
|
|
2305
|
-
}
|
|
2306
|
-
})();
|
|
2307
|
-
});
|
|
2308
|
-
}
|
|
2309
|
-
}
|
|
2310
|
-
|
|
2311
|
-
class WalletLinkHTTP {
|
|
2312
|
-
constructor(linkAPIUrl, sessionId, sessionKey) {
|
|
2313
|
-
this.linkAPIUrl = linkAPIUrl;
|
|
2314
|
-
this.sessionId = sessionId;
|
|
2315
|
-
const credentials = `${sessionId}:${sessionKey}`;
|
|
2316
|
-
this.auth = `Basic ${btoa(credentials)}`;
|
|
2317
|
-
}
|
|
2318
|
-
// mark unseen events as seen
|
|
2319
|
-
async markUnseenEventsAsSeen(events) {
|
|
2320
|
-
return Promise.all(events.map((e) => fetch(`${this.linkAPIUrl}/events/${e.eventId}/seen`, {
|
|
2321
|
-
method: 'POST',
|
|
2322
|
-
headers: {
|
|
2323
|
-
Authorization: this.auth,
|
|
2324
|
-
},
|
|
2325
|
-
}))).catch((error) => console.error('Unabled to mark event as failed:', error));
|
|
2326
|
-
}
|
|
2327
|
-
async fetchUnseenEvents() {
|
|
2328
|
-
var _a;
|
|
2329
|
-
const response = await fetch(`${this.linkAPIUrl}/events?unseen=true`, {
|
|
2330
|
-
headers: {
|
|
2331
|
-
Authorization: this.auth,
|
|
2332
|
-
},
|
|
2333
|
-
});
|
|
2334
|
-
if (response.ok) {
|
|
2335
|
-
const { events, error } = (await response.json());
|
|
2336
|
-
if (error) {
|
|
2337
|
-
throw new Error(`Check unseen events failed: ${error}`);
|
|
2338
|
-
}
|
|
2339
|
-
const responseEvents = (_a = events === null || events === void 0 ? void 0 : events.filter((e) => e.event === 'Web3Response').map((e) => ({
|
|
2340
|
-
type: 'Event',
|
|
2341
|
-
sessionId: this.sessionId,
|
|
2342
|
-
eventId: e.id,
|
|
2343
|
-
event: e.event,
|
|
2344
|
-
data: e.data,
|
|
2345
|
-
}))) !== null && _a !== void 0 ? _a : [];
|
|
2346
|
-
this.markUnseenEventsAsSeen(responseEvents);
|
|
2347
|
-
return responseEvents;
|
|
2348
|
-
}
|
|
2349
|
-
throw new Error(`Check unseen events failed: ${response.status}`);
|
|
2350
|
-
}
|
|
2351
|
-
}
|
|
2352
|
-
|
|
2353
|
-
// Copyright (c) 2018-2023 Coinbase, Inc. <https://www.coinbase.com/>
|
|
2354
|
-
var ConnectionState;
|
|
2355
|
-
(function (ConnectionState) {
|
|
2356
|
-
ConnectionState[ConnectionState["DISCONNECTED"] = 0] = "DISCONNECTED";
|
|
2357
|
-
ConnectionState[ConnectionState["CONNECTING"] = 1] = "CONNECTING";
|
|
2358
|
-
ConnectionState[ConnectionState["CONNECTED"] = 2] = "CONNECTED";
|
|
2359
|
-
})(ConnectionState || (ConnectionState = {}));
|
|
2360
|
-
class WalletLinkWebSocket {
|
|
2361
|
-
setConnectionStateListener(listener) {
|
|
2362
|
-
this.connectionStateListener = listener;
|
|
2363
|
-
}
|
|
2364
|
-
setIncomingDataListener(listener) {
|
|
2365
|
-
this.incomingDataListener = listener;
|
|
2366
|
-
}
|
|
2367
|
-
/**
|
|
2368
|
-
* Constructor
|
|
2369
|
-
* @param url WebSocket server URL
|
|
2370
|
-
* @param [WebSocketClass] Custom WebSocket implementation
|
|
2371
|
-
*/
|
|
2372
|
-
constructor(url, WebSocketClass = WebSocket) {
|
|
2373
|
-
this.WebSocketClass = WebSocketClass;
|
|
2374
|
-
this.webSocket = null;
|
|
2375
|
-
this.pendingData = [];
|
|
2376
|
-
this.url = url.replace(/^http/, 'ws');
|
|
2377
|
-
}
|
|
2378
|
-
/**
|
|
2379
|
-
* Make a websocket connection
|
|
2380
|
-
* @returns a Promise that resolves when connected
|
|
2381
|
-
*/
|
|
2382
|
-
async connect() {
|
|
2383
|
-
if (this.webSocket) {
|
|
2384
|
-
throw new Error('webSocket object is not null');
|
|
2385
|
-
}
|
|
2386
|
-
return new Promise((resolve, reject) => {
|
|
2387
|
-
var _a;
|
|
2388
|
-
let webSocket;
|
|
2389
|
-
try {
|
|
2390
|
-
this.webSocket = webSocket = new this.WebSocketClass(this.url);
|
|
2391
|
-
}
|
|
2392
|
-
catch (err) {
|
|
2393
|
-
reject(err);
|
|
2394
|
-
return;
|
|
2395
|
-
}
|
|
2396
|
-
(_a = this.connectionStateListener) === null || _a === void 0 ? void 0 : _a.call(this, ConnectionState.CONNECTING);
|
|
2397
|
-
webSocket.onclose = (evt) => {
|
|
2398
|
-
var _a;
|
|
2399
|
-
this.clearWebSocket();
|
|
2400
|
-
reject(new Error(`websocket error ${evt.code}: ${evt.reason}`));
|
|
2401
|
-
(_a = this.connectionStateListener) === null || _a === void 0 ? void 0 : _a.call(this, ConnectionState.DISCONNECTED);
|
|
2402
|
-
};
|
|
2403
|
-
webSocket.onopen = (_) => {
|
|
2404
|
-
var _a;
|
|
2405
|
-
resolve();
|
|
2406
|
-
(_a = this.connectionStateListener) === null || _a === void 0 ? void 0 : _a.call(this, ConnectionState.CONNECTED);
|
|
2407
|
-
if (this.pendingData.length > 0) {
|
|
2408
|
-
const pending = [...this.pendingData];
|
|
2409
|
-
pending.forEach((data) => this.sendData(data));
|
|
2410
|
-
this.pendingData = [];
|
|
2411
|
-
}
|
|
2412
|
-
};
|
|
2413
|
-
webSocket.onmessage = (evt) => {
|
|
2414
|
-
var _a, _b;
|
|
2415
|
-
if (evt.data === 'h') {
|
|
2416
|
-
(_a = this.incomingDataListener) === null || _a === void 0 ? void 0 : _a.call(this, {
|
|
2417
|
-
type: 'Heartbeat',
|
|
2418
|
-
});
|
|
2419
|
-
}
|
|
2420
|
-
else {
|
|
2421
|
-
try {
|
|
2422
|
-
const message = JSON.parse(evt.data);
|
|
2423
|
-
(_b = this.incomingDataListener) === null || _b === void 0 ? void 0 : _b.call(this, message);
|
|
2424
|
-
}
|
|
2425
|
-
catch (_c) {
|
|
2426
|
-
/* empty */
|
|
2427
|
-
}
|
|
2428
|
-
}
|
|
2429
|
-
};
|
|
2430
|
-
});
|
|
2431
|
-
}
|
|
2432
|
-
/**
|
|
2433
|
-
* Disconnect from server
|
|
2434
|
-
*/
|
|
2435
|
-
disconnect() {
|
|
2436
|
-
var _a;
|
|
2437
|
-
const { webSocket } = this;
|
|
2438
|
-
if (!webSocket) {
|
|
2439
|
-
return;
|
|
2440
|
-
}
|
|
2441
|
-
this.clearWebSocket();
|
|
2442
|
-
(_a = this.connectionStateListener) === null || _a === void 0 ? void 0 : _a.call(this, ConnectionState.DISCONNECTED);
|
|
2443
|
-
this.connectionStateListener = undefined;
|
|
2444
|
-
this.incomingDataListener = undefined;
|
|
2445
|
-
try {
|
|
2446
|
-
webSocket.close();
|
|
2447
|
-
}
|
|
2448
|
-
catch (_b) {
|
|
2449
|
-
// noop
|
|
2450
|
-
}
|
|
2451
|
-
}
|
|
2452
|
-
/**
|
|
2453
|
-
* Send data to server
|
|
2454
|
-
* @param data text to send
|
|
2455
|
-
*/
|
|
2456
|
-
sendData(data) {
|
|
2457
|
-
const { webSocket } = this;
|
|
2458
|
-
if (!webSocket) {
|
|
2459
|
-
this.pendingData.push(data);
|
|
2460
|
-
this.connect();
|
|
2461
|
-
return;
|
|
2462
|
-
}
|
|
2463
|
-
webSocket.send(data);
|
|
2464
|
-
}
|
|
2465
|
-
clearWebSocket() {
|
|
2466
|
-
const { webSocket } = this;
|
|
2467
|
-
if (!webSocket) {
|
|
2468
|
-
return;
|
|
2469
|
-
}
|
|
2470
|
-
this.webSocket = null;
|
|
2471
|
-
webSocket.onclose = null;
|
|
2472
|
-
webSocket.onerror = null;
|
|
2473
|
-
webSocket.onmessage = null;
|
|
2474
|
-
webSocket.onopen = null;
|
|
2475
|
-
}
|
|
2476
|
-
}
|
|
2477
|
-
|
|
2478
|
-
// Copyright (c) 2018-2023 Coinbase, Inc. <https://www.coinbase.com/>
|
|
2479
|
-
const HEARTBEAT_INTERVAL = 10000;
|
|
2480
|
-
const REQUEST_TIMEOUT = 60000;
|
|
2481
|
-
/**
|
|
2482
|
-
* Coinbase Wallet Connection
|
|
2483
|
-
*/
|
|
2484
|
-
class WalletLinkConnection {
|
|
2485
|
-
/**
|
|
2486
|
-
* Constructor
|
|
2487
|
-
* @param session Session
|
|
2488
|
-
* @param linkAPIUrl Coinbase Wallet link server URL
|
|
2489
|
-
* @param listener WalletLinkConnectionUpdateListener
|
|
2490
|
-
* @param [WebSocketClass] Custom WebSocket implementation
|
|
2491
|
-
*/
|
|
2492
|
-
constructor({ session, linkAPIUrl, listener }) {
|
|
2493
|
-
this.destroyed = false;
|
|
2494
|
-
this.lastHeartbeatResponse = 0;
|
|
2495
|
-
this.nextReqId = IntNumber(1);
|
|
2496
|
-
/**
|
|
2497
|
-
* true if connected and authenticated, else false
|
|
2498
|
-
* runs listener when connected status changes
|
|
2499
|
-
*/
|
|
2500
|
-
this._connected = false;
|
|
2501
|
-
/**
|
|
2502
|
-
* true if linked (a guest has joined before)
|
|
2503
|
-
* runs listener when linked status changes
|
|
2504
|
-
*/
|
|
2505
|
-
this._linked = false;
|
|
2506
|
-
this.shouldFetchUnseenEventsOnConnect = false;
|
|
2507
|
-
this.requestResolutions = new Map();
|
|
2508
|
-
this.handleSessionMetadataUpdated = (metadata) => {
|
|
2509
|
-
if (!metadata)
|
|
2510
|
-
return;
|
|
2511
|
-
// Map of metadata key to handler function
|
|
2512
|
-
const handlers = new Map([
|
|
2513
|
-
['__destroyed', this.handleDestroyed],
|
|
2514
|
-
['EthereumAddress', this.handleAccountUpdated],
|
|
2515
|
-
['WalletUsername', this.handleWalletUsernameUpdated],
|
|
2516
|
-
['AppVersion', this.handleAppVersionUpdated],
|
|
2517
|
-
[
|
|
2518
|
-
'ChainId', // ChainId and JsonRpcUrl are always updated together
|
|
2519
|
-
(v) => metadata.JsonRpcUrl && this.handleChainUpdated(v, metadata.JsonRpcUrl),
|
|
2520
|
-
],
|
|
2521
|
-
]);
|
|
2522
|
-
// call handler for each metadata key if value is defined
|
|
2523
|
-
handlers.forEach((handler, key) => {
|
|
2524
|
-
const value = metadata[key];
|
|
2525
|
-
if (value === undefined)
|
|
2526
|
-
return;
|
|
2527
|
-
handler(value);
|
|
2528
|
-
});
|
|
2529
|
-
};
|
|
2530
|
-
this.handleDestroyed = (__destroyed) => {
|
|
2531
|
-
var _a;
|
|
2532
|
-
if (__destroyed !== '1')
|
|
2533
|
-
return;
|
|
2534
|
-
(_a = this.listener) === null || _a === void 0 ? void 0 : _a.resetAndReload();
|
|
2535
|
-
};
|
|
2536
|
-
this.handleAccountUpdated = async (encryptedEthereumAddress) => {
|
|
2537
|
-
var _a;
|
|
2538
|
-
const address = await this.cipher.decrypt(encryptedEthereumAddress);
|
|
2539
|
-
(_a = this.listener) === null || _a === void 0 ? void 0 : _a.accountUpdated(address);
|
|
2540
|
-
};
|
|
2541
|
-
this.handleMetadataUpdated = async (key, encryptedMetadataValue) => {
|
|
2542
|
-
var _a;
|
|
2543
|
-
const decryptedValue = await this.cipher.decrypt(encryptedMetadataValue);
|
|
2544
|
-
(_a = this.listener) === null || _a === void 0 ? void 0 : _a.metadataUpdated(key, decryptedValue);
|
|
2545
|
-
};
|
|
2546
|
-
this.handleWalletUsernameUpdated = async (walletUsername) => {
|
|
2547
|
-
this.handleMetadataUpdated(WALLET_USER_NAME_KEY, walletUsername);
|
|
2548
|
-
};
|
|
2549
|
-
this.handleAppVersionUpdated = async (appVersion) => {
|
|
2550
|
-
this.handleMetadataUpdated(APP_VERSION_KEY, appVersion);
|
|
2551
|
-
};
|
|
2552
|
-
this.handleChainUpdated = async (encryptedChainId, encryptedJsonRpcUrl) => {
|
|
2553
|
-
var _a;
|
|
2554
|
-
const chainId = await this.cipher.decrypt(encryptedChainId);
|
|
2555
|
-
const jsonRpcUrl = await this.cipher.decrypt(encryptedJsonRpcUrl);
|
|
2556
|
-
(_a = this.listener) === null || _a === void 0 ? void 0 : _a.chainUpdated(chainId, jsonRpcUrl);
|
|
2557
|
-
};
|
|
2558
|
-
this.session = session;
|
|
2559
|
-
this.cipher = new WalletLinkCipher(session.secret);
|
|
2560
|
-
this.listener = listener;
|
|
2561
|
-
const ws = new WalletLinkWebSocket(`${linkAPIUrl}/rpc`, WebSocket);
|
|
2562
|
-
ws.setConnectionStateListener(async (state) => {
|
|
2563
|
-
// attempt to reconnect every 5 seconds when disconnected
|
|
2564
|
-
let connected = false;
|
|
2565
|
-
switch (state) {
|
|
2566
|
-
case ConnectionState.DISCONNECTED:
|
|
2567
|
-
// if DISCONNECTED and not destroyed
|
|
2568
|
-
if (!this.destroyed) {
|
|
2569
|
-
const connect = async () => {
|
|
2570
|
-
// wait 5 seconds
|
|
2571
|
-
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
2572
|
-
// check whether it's destroyed again
|
|
2573
|
-
if (!this.destroyed) {
|
|
2574
|
-
// reconnect
|
|
2575
|
-
ws.connect().catch(() => {
|
|
2576
|
-
connect();
|
|
2577
|
-
});
|
|
2578
|
-
}
|
|
2579
|
-
};
|
|
2580
|
-
connect();
|
|
2581
|
-
}
|
|
2582
|
-
break;
|
|
2583
|
-
case ConnectionState.CONNECTED:
|
|
2584
|
-
// perform authentication upon connection
|
|
2585
|
-
// if CONNECTED, authenticate, and then check link status
|
|
2586
|
-
connected = await this.handleConnected();
|
|
2587
|
-
// send heartbeat every n seconds while connected
|
|
2588
|
-
// if CONNECTED, start the heartbeat timer
|
|
2589
|
-
// first timer event updates lastHeartbeat timestamp
|
|
2590
|
-
// subsequent calls send heartbeat message
|
|
2591
|
-
this.updateLastHeartbeat();
|
|
2592
|
-
setInterval(() => {
|
|
2593
|
-
this.heartbeat();
|
|
2594
|
-
}, HEARTBEAT_INTERVAL);
|
|
2595
|
-
// check for unseen events
|
|
2596
|
-
if (this.shouldFetchUnseenEventsOnConnect) {
|
|
2597
|
-
this.fetchUnseenEventsAPI();
|
|
2598
|
-
}
|
|
2599
|
-
break;
|
|
2600
|
-
case ConnectionState.CONNECTING:
|
|
2601
|
-
break;
|
|
2602
|
-
}
|
|
2603
|
-
// distinctUntilChanged
|
|
2604
|
-
if (this.connected !== connected) {
|
|
2605
|
-
this.connected = connected;
|
|
2606
|
-
}
|
|
2607
|
-
});
|
|
2608
|
-
ws.setIncomingDataListener((m) => {
|
|
2609
|
-
var _a;
|
|
2610
|
-
switch (m.type) {
|
|
2611
|
-
// handle server's heartbeat responses
|
|
2612
|
-
case 'Heartbeat':
|
|
2613
|
-
this.updateLastHeartbeat();
|
|
2614
|
-
return;
|
|
2615
|
-
// handle link status updates
|
|
2616
|
-
case 'IsLinkedOK':
|
|
2617
|
-
case 'Linked': {
|
|
2618
|
-
const linked = m.type === 'IsLinkedOK' ? m.linked : undefined;
|
|
2619
|
-
this.linked = linked || m.onlineGuests > 0;
|
|
2620
|
-
break;
|
|
2621
|
-
}
|
|
2622
|
-
// handle session config updates
|
|
2623
|
-
case 'GetSessionConfigOK':
|
|
2624
|
-
case 'SessionConfigUpdated': {
|
|
2625
|
-
this.handleSessionMetadataUpdated(m.metadata);
|
|
2626
|
-
break;
|
|
2627
|
-
}
|
|
2628
|
-
case 'Event': {
|
|
2629
|
-
this.handleIncomingEvent(m);
|
|
2630
|
-
break;
|
|
2631
|
-
}
|
|
2632
|
-
}
|
|
2633
|
-
// resolve request promises
|
|
2634
|
-
if (m.id !== undefined) {
|
|
2635
|
-
(_a = this.requestResolutions.get(m.id)) === null || _a === void 0 ? void 0 : _a(m);
|
|
2636
|
-
}
|
|
2637
|
-
});
|
|
2638
|
-
this.ws = ws;
|
|
2639
|
-
this.http = new WalletLinkHTTP(linkAPIUrl, session.id, session.key);
|
|
2640
|
-
}
|
|
2641
|
-
/**
|
|
2642
|
-
* Make a connection to the server
|
|
2643
|
-
*/
|
|
2644
|
-
connect() {
|
|
2645
|
-
if (this.destroyed) {
|
|
2646
|
-
throw new Error('instance is destroyed');
|
|
2647
|
-
}
|
|
2648
|
-
this.ws.connect();
|
|
2649
|
-
}
|
|
2650
|
-
/**
|
|
2651
|
-
* Terminate connection, and mark as destroyed. To reconnect, create a new
|
|
2652
|
-
* instance of WalletSDKConnection
|
|
2653
|
-
*/
|
|
2654
|
-
async destroy() {
|
|
2655
|
-
if (this.destroyed)
|
|
2656
|
-
return;
|
|
2657
|
-
await this.makeRequest({
|
|
2658
|
-
type: 'SetSessionConfig',
|
|
2659
|
-
id: IntNumber(this.nextReqId++),
|
|
2660
|
-
sessionId: this.session.id,
|
|
2661
|
-
metadata: { __destroyed: '1' },
|
|
2662
|
-
}, { timeout: 1000 });
|
|
2663
|
-
this.destroyed = true;
|
|
2664
|
-
this.ws.disconnect();
|
|
2665
|
-
this.listener = undefined;
|
|
2666
|
-
}
|
|
2667
|
-
get connected() {
|
|
2668
|
-
return this._connected;
|
|
2669
|
-
}
|
|
2670
|
-
set connected(connected) {
|
|
2671
|
-
this._connected = connected;
|
|
2672
|
-
}
|
|
2673
|
-
get linked() {
|
|
2674
|
-
return this._linked;
|
|
2675
|
-
}
|
|
2676
|
-
set linked(linked) {
|
|
2677
|
-
var _a, _b;
|
|
2678
|
-
this._linked = linked;
|
|
2679
|
-
if (linked)
|
|
2680
|
-
(_a = this.onceLinked) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
2681
|
-
(_b = this.listener) === null || _b === void 0 ? void 0 : _b.linkedUpdated(linked);
|
|
2682
|
-
}
|
|
2683
|
-
setOnceLinked(callback) {
|
|
2684
|
-
return new Promise((resolve) => {
|
|
2685
|
-
if (this.linked) {
|
|
2686
|
-
callback().then(resolve);
|
|
2687
|
-
}
|
|
2688
|
-
else {
|
|
2689
|
-
this.onceLinked = () => {
|
|
2690
|
-
callback().then(resolve);
|
|
2691
|
-
this.onceLinked = undefined;
|
|
2692
|
-
};
|
|
2693
|
-
}
|
|
2694
|
-
});
|
|
2695
|
-
}
|
|
2696
|
-
async handleIncomingEvent(m) {
|
|
2697
|
-
var _a;
|
|
2698
|
-
if (m.type !== 'Event' || m.event !== 'Web3Response') {
|
|
2699
|
-
return;
|
|
2700
|
-
}
|
|
2701
|
-
const decryptedData = await this.cipher.decrypt(m.data);
|
|
2702
|
-
const message = JSON.parse(decryptedData);
|
|
2703
|
-
if (message.type !== 'WEB3_RESPONSE')
|
|
2704
|
-
return;
|
|
2705
|
-
const { id, response } = message;
|
|
2706
|
-
(_a = this.listener) === null || _a === void 0 ? void 0 : _a.handleWeb3ResponseMessage(id, response);
|
|
2707
|
-
}
|
|
2708
|
-
async checkUnseenEvents() {
|
|
2709
|
-
if (!this.connected) {
|
|
2710
|
-
this.shouldFetchUnseenEventsOnConnect = true;
|
|
2711
|
-
return;
|
|
2712
|
-
}
|
|
2713
|
-
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
2714
|
-
try {
|
|
2715
|
-
await this.fetchUnseenEventsAPI();
|
|
2716
|
-
}
|
|
2717
|
-
catch (e) {
|
|
2718
|
-
console.error('Unable to check for unseen events', e);
|
|
2719
|
-
}
|
|
2720
|
-
}
|
|
2721
|
-
async fetchUnseenEventsAPI() {
|
|
2722
|
-
this.shouldFetchUnseenEventsOnConnect = false;
|
|
2723
|
-
const responseEvents = await this.http.fetchUnseenEvents();
|
|
2724
|
-
responseEvents.forEach((e) => this.handleIncomingEvent(e));
|
|
2725
|
-
}
|
|
2726
|
-
/**
|
|
2727
|
-
* Publish an event and emit event ID when successful
|
|
2728
|
-
* @param event event name
|
|
2729
|
-
* @param unencryptedData unencrypted event data
|
|
2730
|
-
* @param callWebhook whether the webhook should be invoked
|
|
2731
|
-
* @returns a Promise that emits event ID when successful
|
|
2732
|
-
*/
|
|
2733
|
-
async publishEvent(event, unencryptedData, callWebhook = false) {
|
|
2734
|
-
const data = await this.cipher.encrypt(JSON.stringify(Object.assign(Object.assign({}, unencryptedData), { origin: location.origin, relaySource: 'coinbaseWalletExtension' in window && window.coinbaseWalletExtension
|
|
2735
|
-
? 'injected_sdk'
|
|
2736
|
-
: 'sdk' })));
|
|
2737
|
-
const message = {
|
|
2738
|
-
type: 'PublishEvent',
|
|
2739
|
-
id: IntNumber(this.nextReqId++),
|
|
2740
|
-
sessionId: this.session.id,
|
|
2741
|
-
event,
|
|
2742
|
-
data,
|
|
2743
|
-
callWebhook,
|
|
2744
|
-
};
|
|
2745
|
-
return this.setOnceLinked(async () => {
|
|
2746
|
-
const res = await this.makeRequest(message);
|
|
2747
|
-
if (res.type === 'Fail') {
|
|
2748
|
-
throw new Error(res.error || 'failed to publish event');
|
|
2749
|
-
}
|
|
2750
|
-
return res.eventId;
|
|
2751
|
-
});
|
|
2752
|
-
}
|
|
2753
|
-
sendData(message) {
|
|
2754
|
-
this.ws.sendData(JSON.stringify(message));
|
|
2755
|
-
}
|
|
2756
|
-
updateLastHeartbeat() {
|
|
2757
|
-
this.lastHeartbeatResponse = Date.now();
|
|
2758
|
-
}
|
|
2759
|
-
heartbeat() {
|
|
2760
|
-
if (Date.now() - this.lastHeartbeatResponse > HEARTBEAT_INTERVAL * 2) {
|
|
2761
|
-
this.ws.disconnect();
|
|
2762
|
-
return;
|
|
2763
|
-
}
|
|
2764
|
-
try {
|
|
2765
|
-
this.ws.sendData('h');
|
|
2766
|
-
}
|
|
2767
|
-
catch (_a) {
|
|
2768
|
-
// noop
|
|
2769
|
-
}
|
|
2770
|
-
}
|
|
2771
|
-
async makeRequest(message, options = { timeout: REQUEST_TIMEOUT }) {
|
|
2772
|
-
const reqId = message.id;
|
|
2773
|
-
this.sendData(message);
|
|
2774
|
-
// await server message with corresponding id
|
|
2775
|
-
let timeoutId;
|
|
2776
|
-
return Promise.race([
|
|
2777
|
-
new Promise((_, reject) => {
|
|
2778
|
-
timeoutId = window.setTimeout(() => {
|
|
2779
|
-
reject(new Error(`request ${reqId} timed out`));
|
|
2780
|
-
}, options.timeout);
|
|
2781
|
-
}),
|
|
2782
|
-
new Promise((resolve) => {
|
|
2783
|
-
this.requestResolutions.set(reqId, (m) => {
|
|
2784
|
-
clearTimeout(timeoutId); // clear the timeout
|
|
2785
|
-
resolve(m);
|
|
2786
|
-
this.requestResolutions.delete(reqId);
|
|
2787
|
-
});
|
|
2788
|
-
}),
|
|
2789
|
-
]);
|
|
2790
|
-
}
|
|
2791
|
-
async handleConnected() {
|
|
2792
|
-
const res = await this.makeRequest({
|
|
2793
|
-
type: 'HostSession',
|
|
2794
|
-
id: IntNumber(this.nextReqId++),
|
|
2795
|
-
sessionId: this.session.id,
|
|
2796
|
-
sessionKey: this.session.key,
|
|
2797
|
-
});
|
|
2798
|
-
if (res.type === 'Fail')
|
|
2799
|
-
return false;
|
|
2800
|
-
this.sendData({
|
|
2801
|
-
type: 'IsLinked',
|
|
2802
|
-
id: IntNumber(this.nextReqId++),
|
|
2803
|
-
sessionId: this.session.id,
|
|
2804
|
-
});
|
|
2805
|
-
this.sendData({
|
|
2806
|
-
type: 'GetSessionConfig',
|
|
2807
|
-
id: IntNumber(this.nextReqId++),
|
|
2808
|
-
sessionId: this.session.id,
|
|
2809
|
-
});
|
|
2810
|
-
return true;
|
|
2811
|
-
}
|
|
2812
|
-
}
|
|
2813
|
-
|
|
2814
|
-
class RelayEventManager {
|
|
2815
|
-
constructor() {
|
|
2816
|
-
this._nextRequestId = 0;
|
|
2817
|
-
this.callbacks = new Map();
|
|
2818
|
-
}
|
|
2819
|
-
makeRequestId() {
|
|
2820
|
-
// max nextId == max int32 for compatibility with mobile
|
|
2821
|
-
this._nextRequestId = (this._nextRequestId + 1) % 0x7fffffff;
|
|
2822
|
-
const id = this._nextRequestId;
|
|
2823
|
-
const idStr = prepend0x(id.toString(16));
|
|
2824
|
-
// unlikely that this will ever be an issue, but just to be safe
|
|
2825
|
-
const callback = this.callbacks.get(idStr);
|
|
2826
|
-
if (callback) {
|
|
2827
|
-
this.callbacks.delete(idStr);
|
|
2828
|
-
}
|
|
2829
|
-
return id;
|
|
2830
|
-
}
|
|
2831
|
-
}
|
|
2832
|
-
|
|
2833
|
-
// copied from utils
|
|
2834
|
-
function isBytes(a) {
|
|
2835
|
-
return (a instanceof Uint8Array ||
|
|
2836
|
-
(a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array'));
|
|
2837
|
-
}
|
|
2838
|
-
function bytes(b, ...lengths) {
|
|
2839
|
-
if (!isBytes(b))
|
|
2840
|
-
throw new Error('Uint8Array expected');
|
|
2841
|
-
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
2842
|
-
throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`);
|
|
2843
|
-
}
|
|
2844
|
-
function exists(instance, checkFinished = true) {
|
|
2845
|
-
if (instance.destroyed)
|
|
2846
|
-
throw new Error('Hash instance has been destroyed');
|
|
2847
|
-
if (checkFinished && instance.finished)
|
|
2848
|
-
throw new Error('Hash#digest() has already been called');
|
|
2849
|
-
}
|
|
2850
|
-
function output(out, instance) {
|
|
2851
|
-
bytes(out);
|
|
2852
|
-
const min = instance.outputLen;
|
|
2853
|
-
if (out.length < min) {
|
|
2854
|
-
throw new Error(`digestInto() expects output buffer of length at least ${min}`);
|
|
2855
|
-
}
|
|
2856
|
-
}
|
|
2857
|
-
|
|
2858
|
-
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
2859
|
-
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
|
2860
|
-
// node.js versions earlier than v19 don't declare it in global scope.
|
|
2861
|
-
// For node.js, package.json#exports field mapping rewrites import
|
|
2862
|
-
// from `crypto` to `cryptoNode`, which imports native module.
|
|
2863
|
-
// Makes the utils un-importable in browsers without a bundler.
|
|
2864
|
-
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
|
|
2865
|
-
// Cast array to view
|
|
2866
|
-
const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
2867
|
-
// The rotate right (circular right shift) operation for uint32
|
|
2868
|
-
const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
|
|
2869
|
-
new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
|
|
2870
|
-
// Array where index 0xf0 (240) is mapped to string 'f0'
|
|
2871
|
-
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
|
|
2872
|
-
/**
|
|
2873
|
-
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
2874
|
-
*/
|
|
2875
|
-
function bytesToHex(bytes$1) {
|
|
2876
|
-
bytes(bytes$1);
|
|
2877
|
-
// pre-caching improves the speed 6x
|
|
2878
|
-
let hex = '';
|
|
2879
|
-
for (let i = 0; i < bytes$1.length; i++) {
|
|
2880
|
-
hex += hexes[bytes$1[i]];
|
|
2881
|
-
}
|
|
2882
|
-
return hex;
|
|
2883
|
-
}
|
|
2884
|
-
/**
|
|
2885
|
-
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
2886
|
-
*/
|
|
2887
|
-
function utf8ToBytes(str) {
|
|
2888
|
-
if (typeof str !== 'string')
|
|
2889
|
-
throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
|
|
2890
|
-
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
2891
|
-
}
|
|
2892
|
-
/**
|
|
2893
|
-
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
2894
|
-
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
2895
|
-
* Keep in mind for future mutable operations.
|
|
2896
|
-
*/
|
|
2897
|
-
function toBytes(data) {
|
|
2898
|
-
if (typeof data === 'string')
|
|
2899
|
-
data = utf8ToBytes(data);
|
|
2900
|
-
bytes(data);
|
|
2901
|
-
return data;
|
|
2902
|
-
}
|
|
2903
|
-
// For runtime check if class implements interface
|
|
2904
|
-
class Hash {
|
|
2905
|
-
// Safe version that clones internal state
|
|
2906
|
-
clone() {
|
|
2907
|
-
return this._cloneInto();
|
|
2908
|
-
}
|
|
2909
|
-
}
|
|
2910
|
-
function wrapConstructor(hashCons) {
|
|
2911
|
-
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
2912
|
-
const tmp = hashCons();
|
|
2913
|
-
hashC.outputLen = tmp.outputLen;
|
|
2914
|
-
hashC.blockLen = tmp.blockLen;
|
|
2915
|
-
hashC.create = () => hashCons();
|
|
2916
|
-
return hashC;
|
|
2917
|
-
}
|
|
2918
|
-
|
|
2919
|
-
/**
|
|
2920
|
-
* Polyfill for Safari 14
|
|
2921
|
-
*/
|
|
2922
|
-
function setBigUint64(view, byteOffset, value, isLE) {
|
|
2923
|
-
if (typeof view.setBigUint64 === 'function')
|
|
2924
|
-
return view.setBigUint64(byteOffset, value, isLE);
|
|
2925
|
-
const _32n = BigInt(32);
|
|
2926
|
-
const _u32_max = BigInt(0xffffffff);
|
|
2927
|
-
const wh = Number((value >> _32n) & _u32_max);
|
|
2928
|
-
const wl = Number(value & _u32_max);
|
|
2929
|
-
const h = isLE ? 4 : 0;
|
|
2930
|
-
const l = isLE ? 0 : 4;
|
|
2931
|
-
view.setUint32(byteOffset + h, wh, isLE);
|
|
2932
|
-
view.setUint32(byteOffset + l, wl, isLE);
|
|
2933
|
-
}
|
|
2934
|
-
/**
|
|
2935
|
-
* Choice: a ? b : c
|
|
2936
|
-
*/
|
|
2937
|
-
const Chi = (a, b, c) => (a & b) ^ (~a & c);
|
|
2938
|
-
/**
|
|
2939
|
-
* Majority function, true if any two inputs is true
|
|
2940
|
-
*/
|
|
2941
|
-
const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
|
|
2942
|
-
/**
|
|
2943
|
-
* Merkle-Damgard hash construction base class.
|
|
2944
|
-
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
2945
|
-
*/
|
|
2946
|
-
class HashMD extends Hash {
|
|
2947
|
-
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
2948
|
-
super();
|
|
2949
|
-
this.blockLen = blockLen;
|
|
2950
|
-
this.outputLen = outputLen;
|
|
2951
|
-
this.padOffset = padOffset;
|
|
2952
|
-
this.isLE = isLE;
|
|
2953
|
-
this.finished = false;
|
|
2954
|
-
this.length = 0;
|
|
2955
|
-
this.pos = 0;
|
|
2956
|
-
this.destroyed = false;
|
|
2957
|
-
this.buffer = new Uint8Array(blockLen);
|
|
2958
|
-
this.view = createView(this.buffer);
|
|
2959
|
-
}
|
|
2960
|
-
update(data) {
|
|
2961
|
-
exists(this);
|
|
2962
|
-
const { view, buffer, blockLen } = this;
|
|
2963
|
-
data = toBytes(data);
|
|
2964
|
-
const len = data.length;
|
|
2965
|
-
for (let pos = 0; pos < len;) {
|
|
2966
|
-
const take = Math.min(blockLen - this.pos, len - pos);
|
|
2967
|
-
// Fast path: we have at least one block in input, cast it to view and process
|
|
2968
|
-
if (take === blockLen) {
|
|
2969
|
-
const dataView = createView(data);
|
|
2970
|
-
for (; blockLen <= len - pos; pos += blockLen)
|
|
2971
|
-
this.process(dataView, pos);
|
|
2972
|
-
continue;
|
|
2973
|
-
}
|
|
2974
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
2975
|
-
this.pos += take;
|
|
2976
|
-
pos += take;
|
|
2977
|
-
if (this.pos === blockLen) {
|
|
2978
|
-
this.process(view, 0);
|
|
2979
|
-
this.pos = 0;
|
|
2980
|
-
}
|
|
2981
|
-
}
|
|
2982
|
-
this.length += data.length;
|
|
2983
|
-
this.roundClean();
|
|
2984
|
-
return this;
|
|
2985
|
-
}
|
|
2986
|
-
digestInto(out) {
|
|
2987
|
-
exists(this);
|
|
2988
|
-
output(out, this);
|
|
2989
|
-
this.finished = true;
|
|
2990
|
-
// Padding
|
|
2991
|
-
// We can avoid allocation of buffer for padding completely if it
|
|
2992
|
-
// was previously not allocated here. But it won't change performance.
|
|
2993
|
-
const { buffer, view, blockLen, isLE } = this;
|
|
2994
|
-
let { pos } = this;
|
|
2995
|
-
// append the bit '1' to the message
|
|
2996
|
-
buffer[pos++] = 0b10000000;
|
|
2997
|
-
this.buffer.subarray(pos).fill(0);
|
|
2998
|
-
// we have less than padOffset left in buffer, so we cannot put length in
|
|
2999
|
-
// current block, need process it and pad again
|
|
3000
|
-
if (this.padOffset > blockLen - pos) {
|
|
3001
|
-
this.process(view, 0);
|
|
3002
|
-
pos = 0;
|
|
3003
|
-
}
|
|
3004
|
-
// Pad until full block byte with zeros
|
|
3005
|
-
for (let i = pos; i < blockLen; i++)
|
|
3006
|
-
buffer[i] = 0;
|
|
3007
|
-
// Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
|
|
3008
|
-
// You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
|
|
3009
|
-
// So we just write lowest 64 bits of that value.
|
|
3010
|
-
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
3011
|
-
this.process(view, 0);
|
|
3012
|
-
const oview = createView(out);
|
|
3013
|
-
const len = this.outputLen;
|
|
3014
|
-
// NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
|
|
3015
|
-
if (len % 4)
|
|
3016
|
-
throw new Error('_sha2: outputLen should be aligned to 32bit');
|
|
3017
|
-
const outLen = len / 4;
|
|
3018
|
-
const state = this.get();
|
|
3019
|
-
if (outLen > state.length)
|
|
3020
|
-
throw new Error('_sha2: outputLen bigger than state');
|
|
3021
|
-
for (let i = 0; i < outLen; i++)
|
|
3022
|
-
oview.setUint32(4 * i, state[i], isLE);
|
|
3023
|
-
}
|
|
3024
|
-
digest() {
|
|
3025
|
-
const { buffer, outputLen } = this;
|
|
3026
|
-
this.digestInto(buffer);
|
|
3027
|
-
const res = buffer.slice(0, outputLen);
|
|
3028
|
-
this.destroy();
|
|
3029
|
-
return res;
|
|
3030
|
-
}
|
|
3031
|
-
_cloneInto(to) {
|
|
3032
|
-
to || (to = new this.constructor());
|
|
3033
|
-
to.set(...this.get());
|
|
3034
|
-
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
3035
|
-
to.length = length;
|
|
3036
|
-
to.pos = pos;
|
|
3037
|
-
to.finished = finished;
|
|
3038
|
-
to.destroyed = destroyed;
|
|
3039
|
-
if (length % blockLen)
|
|
3040
|
-
to.buffer.set(buffer);
|
|
3041
|
-
return to;
|
|
3042
|
-
}
|
|
3043
|
-
}
|
|
3044
|
-
|
|
3045
|
-
// SHA2-256 need to try 2^128 hashes to execute birthday attack.
|
|
3046
|
-
// BTC network is doing 2^67 hashes/sec as per early 2023.
|
|
3047
|
-
// Round constants:
|
|
3048
|
-
// first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
|
|
3049
|
-
// prettier-ignore
|
|
3050
|
-
const SHA256_K = /* @__PURE__ */ new Uint32Array([
|
|
3051
|
-
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
3052
|
-
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
3053
|
-
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
3054
|
-
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
3055
|
-
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
3056
|
-
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
3057
|
-
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
3058
|
-
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
3059
|
-
]);
|
|
3060
|
-
// Initial state:
|
|
3061
|
-
// first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19
|
|
3062
|
-
// prettier-ignore
|
|
3063
|
-
const SHA256_IV = /* @__PURE__ */ new Uint32Array([
|
|
3064
|
-
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
|
3065
|
-
]);
|
|
3066
|
-
// Temporary buffer, not used to store anything between runs
|
|
3067
|
-
// Named this way because it matches specification.
|
|
3068
|
-
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
3069
|
-
class SHA256 extends HashMD {
|
|
3070
|
-
constructor() {
|
|
3071
|
-
super(64, 32, 8, false);
|
|
3072
|
-
// We cannot use array here since array allows indexing by variable
|
|
3073
|
-
// which means optimizer/compiler cannot use registers.
|
|
3074
|
-
this.A = SHA256_IV[0] | 0;
|
|
3075
|
-
this.B = SHA256_IV[1] | 0;
|
|
3076
|
-
this.C = SHA256_IV[2] | 0;
|
|
3077
|
-
this.D = SHA256_IV[3] | 0;
|
|
3078
|
-
this.E = SHA256_IV[4] | 0;
|
|
3079
|
-
this.F = SHA256_IV[5] | 0;
|
|
3080
|
-
this.G = SHA256_IV[6] | 0;
|
|
3081
|
-
this.H = SHA256_IV[7] | 0;
|
|
3082
|
-
}
|
|
3083
|
-
get() {
|
|
3084
|
-
const { A, B, C, D, E, F, G, H } = this;
|
|
3085
|
-
return [A, B, C, D, E, F, G, H];
|
|
3086
|
-
}
|
|
3087
|
-
// prettier-ignore
|
|
3088
|
-
set(A, B, C, D, E, F, G, H) {
|
|
3089
|
-
this.A = A | 0;
|
|
3090
|
-
this.B = B | 0;
|
|
3091
|
-
this.C = C | 0;
|
|
3092
|
-
this.D = D | 0;
|
|
3093
|
-
this.E = E | 0;
|
|
3094
|
-
this.F = F | 0;
|
|
3095
|
-
this.G = G | 0;
|
|
3096
|
-
this.H = H | 0;
|
|
3097
|
-
}
|
|
3098
|
-
process(view, offset) {
|
|
3099
|
-
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
|
3100
|
-
for (let i = 0; i < 16; i++, offset += 4)
|
|
3101
|
-
SHA256_W[i] = view.getUint32(offset, false);
|
|
3102
|
-
for (let i = 16; i < 64; i++) {
|
|
3103
|
-
const W15 = SHA256_W[i - 15];
|
|
3104
|
-
const W2 = SHA256_W[i - 2];
|
|
3105
|
-
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
|
|
3106
|
-
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
|
|
3107
|
-
SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
|
|
3108
|
-
}
|
|
3109
|
-
// Compression function main loop, 64 rounds
|
|
3110
|
-
let { A, B, C, D, E, F, G, H } = this;
|
|
3111
|
-
for (let i = 0; i < 64; i++) {
|
|
3112
|
-
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
3113
|
-
const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
|
3114
|
-
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
3115
|
-
const T2 = (sigma0 + Maj(A, B, C)) | 0;
|
|
3116
|
-
H = G;
|
|
3117
|
-
G = F;
|
|
3118
|
-
F = E;
|
|
3119
|
-
E = (D + T1) | 0;
|
|
3120
|
-
D = C;
|
|
3121
|
-
C = B;
|
|
3122
|
-
B = A;
|
|
3123
|
-
A = (T1 + T2) | 0;
|
|
3124
|
-
}
|
|
3125
|
-
// Add the compressed chunk to the current hash value
|
|
3126
|
-
A = (A + this.A) | 0;
|
|
3127
|
-
B = (B + this.B) | 0;
|
|
3128
|
-
C = (C + this.C) | 0;
|
|
3129
|
-
D = (D + this.D) | 0;
|
|
3130
|
-
E = (E + this.E) | 0;
|
|
3131
|
-
F = (F + this.F) | 0;
|
|
3132
|
-
G = (G + this.G) | 0;
|
|
3133
|
-
H = (H + this.H) | 0;
|
|
3134
|
-
this.set(A, B, C, D, E, F, G, H);
|
|
3135
|
-
}
|
|
3136
|
-
roundClean() {
|
|
3137
|
-
SHA256_W.fill(0);
|
|
3138
|
-
}
|
|
3139
|
-
destroy() {
|
|
3140
|
-
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
3141
|
-
this.buffer.fill(0);
|
|
3142
|
-
}
|
|
3143
|
-
}
|
|
3144
|
-
/**
|
|
3145
|
-
* SHA2-256 hash function
|
|
3146
|
-
* @param message - data that would be hashed
|
|
3147
|
-
*/
|
|
3148
|
-
const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
|
|
3149
|
-
|
|
3150
|
-
// Copyright (c) 2018-2023 Coinbase, Inc. <https://www.coinbase.com/>
|
|
3151
|
-
const STORAGE_KEY_SESSION_ID = 'session:id';
|
|
3152
|
-
const STORAGE_KEY_SESSION_SECRET = 'session:secret';
|
|
3153
|
-
const STORAGE_KEY_SESSION_LINKED = 'session:linked';
|
|
3154
|
-
class WalletLinkSession {
|
|
3155
|
-
constructor(storage, id, secret, linked = false) {
|
|
3156
|
-
this.storage = storage;
|
|
3157
|
-
this.id = id;
|
|
3158
|
-
this.secret = secret;
|
|
3159
|
-
this.key = bytesToHex(sha256(`${id}, ${secret} WalletLink`));
|
|
3160
|
-
this._linked = !!linked;
|
|
3161
|
-
}
|
|
3162
|
-
static create(storage) {
|
|
3163
|
-
const id = randomBytesHex(16);
|
|
3164
|
-
const secret = randomBytesHex(32);
|
|
3165
|
-
return new WalletLinkSession(storage, id, secret).save();
|
|
3166
|
-
}
|
|
3167
|
-
static load(storage) {
|
|
3168
|
-
const id = storage.getItem(STORAGE_KEY_SESSION_ID);
|
|
3169
|
-
const linked = storage.getItem(STORAGE_KEY_SESSION_LINKED);
|
|
3170
|
-
const secret = storage.getItem(STORAGE_KEY_SESSION_SECRET);
|
|
3171
|
-
if (id && secret) {
|
|
3172
|
-
return new WalletLinkSession(storage, id, secret, linked === '1');
|
|
3173
|
-
}
|
|
3174
|
-
return null;
|
|
3175
|
-
}
|
|
3176
|
-
get linked() {
|
|
3177
|
-
return this._linked;
|
|
3178
|
-
}
|
|
3179
|
-
set linked(val) {
|
|
3180
|
-
this._linked = val;
|
|
3181
|
-
this.persistLinked();
|
|
3182
|
-
}
|
|
3183
|
-
save() {
|
|
3184
|
-
this.storage.setItem(STORAGE_KEY_SESSION_ID, this.id);
|
|
3185
|
-
this.storage.setItem(STORAGE_KEY_SESSION_SECRET, this.secret);
|
|
3186
|
-
this.persistLinked();
|
|
3187
|
-
return this;
|
|
3188
|
-
}
|
|
3189
|
-
persistLinked() {
|
|
3190
|
-
this.storage.setItem(STORAGE_KEY_SESSION_LINKED, this._linked ? '1' : '0');
|
|
3191
|
-
}
|
|
3192
|
-
}
|
|
3193
|
-
|
|
3194
|
-
function isInIFrame() {
|
|
3195
|
-
try {
|
|
3196
|
-
return window.frameElement !== null;
|
|
3197
|
-
}
|
|
3198
|
-
catch (e) {
|
|
3199
|
-
return false;
|
|
3200
|
-
}
|
|
3201
|
-
}
|
|
3202
|
-
function getLocation() {
|
|
3203
|
-
try {
|
|
3204
|
-
if (isInIFrame() && window.top) {
|
|
3205
|
-
return window.top.location;
|
|
3206
|
-
}
|
|
3207
|
-
return window.location;
|
|
3208
|
-
}
|
|
3209
|
-
catch (e) {
|
|
3210
|
-
return window.location;
|
|
3211
|
-
}
|
|
3212
|
-
}
|
|
3213
|
-
function isMobileWeb() {
|
|
3214
|
-
var _a;
|
|
3215
|
-
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test((_a = window === null || window === void 0 ? void 0 : window.navigator) === null || _a === void 0 ? void 0 : _a.userAgent);
|
|
3216
|
-
}
|
|
3217
|
-
function isDarkMode() {
|
|
3218
|
-
var _a, _b;
|
|
3219
|
-
return (_b = (_a = window === null || window === void 0 ? void 0 : window.matchMedia) === null || _a === void 0 ? void 0 : _a.call(window, '(prefers-color-scheme: dark)').matches) !== null && _b !== void 0 ? _b : false;
|
|
3220
|
-
}
|
|
3221
|
-
|
|
3222
|
-
var css$2 = (() => `@namespace svg "http://www.w3.org/2000/svg";.-cbwsdk-css-reset,.-cbwsdk-css-reset *{animation:none;animation-delay:0;animation-direction:normal;animation-duration:0;animation-fill-mode:none;animation-iteration-count:1;animation-name:none;animation-play-state:running;animation-timing-function:ease;backface-visibility:visible;background:0;background-attachment:scroll;background-clip:border-box;background-color:rgba(0,0,0,0);background-image:none;background-origin:padding-box;background-position:0 0;background-position-x:0;background-position-y:0;background-repeat:repeat;background-size:auto auto;border:0;border-style:none;border-width:medium;border-color:inherit;border-bottom:0;border-bottom-color:inherit;border-bottom-left-radius:0;border-bottom-right-radius:0;border-bottom-style:none;border-bottom-width:medium;border-collapse:separate;border-image:none;border-left:0;border-left-color:inherit;border-left-style:none;border-left-width:medium;border-radius:0;border-right:0;border-right-color:inherit;border-right-style:none;border-right-width:medium;border-spacing:0;border-top:0;border-top-color:inherit;border-top-left-radius:0;border-top-right-radius:0;border-top-style:none;border-top-width:medium;box-shadow:none;box-sizing:border-box;caption-side:top;clear:none;clip:auto;color:inherit;columns:auto;column-count:auto;column-fill:balance;column-gap:normal;column-rule:medium none currentColor;column-rule-color:currentColor;column-rule-style:none;column-rule-width:none;column-span:1;column-width:auto;counter-increment:none;counter-reset:none;direction:ltr;empty-cells:show;float:none;font:normal;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Helvetica Neue",Arial,sans-serif;font-size:medium;font-style:normal;font-variant:normal;font-weight:normal;height:auto;hyphens:none;letter-spacing:normal;line-height:normal;list-style:none;list-style-image:none;list-style-position:outside;list-style-type:disc;margin:0;margin-bottom:0;margin-left:0;margin-right:0;margin-top:0;opacity:1;orphans:0;outline:0;outline-color:invert;outline-style:none;outline-width:medium;overflow:visible;overflow-x:visible;overflow-y:visible;padding:0;padding-bottom:0;padding-left:0;padding-right:0;padding-top:0;page-break-after:auto;page-break-before:auto;page-break-inside:auto;perspective:none;perspective-origin:50% 50%;pointer-events:auto;position:static;quotes:"\\201C" "\\201D" "\\2018" "\\2019";tab-size:8;table-layout:auto;text-align:inherit;text-align-last:auto;text-decoration:none;text-decoration-color:inherit;text-decoration-line:none;text-decoration-style:solid;text-indent:0;text-shadow:none;text-transform:none;transform:none;transform-style:flat;transition:none;transition-delay:0s;transition-duration:0s;transition-property:none;transition-timing-function:ease;unicode-bidi:normal;vertical-align:baseline;visibility:visible;white-space:normal;widows:0;word-spacing:normal;z-index:auto}.-cbwsdk-css-reset strong{font-weight:bold}.-cbwsdk-css-reset *{box-sizing:border-box;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Helvetica Neue",Arial,sans-serif;line-height:1}.-cbwsdk-css-reset [class*=container]{margin:0;padding:0}.-cbwsdk-css-reset style{display:none}`)();
|
|
3223
|
-
|
|
3224
|
-
// Copyright (c) 2018-2023 Coinbase, Inc. <https://www.coinbase.com/>
|
|
3225
|
-
function injectCssReset() {
|
|
3226
|
-
const styleEl = document.createElement('style');
|
|
3227
|
-
styleEl.type = 'text/css';
|
|
3228
|
-
styleEl.appendChild(document.createTextNode(css$2));
|
|
3229
|
-
document.documentElement.appendChild(styleEl);
|
|
3230
|
-
}
|
|
3231
|
-
|
|
3232
|
-
var n,l$1,u$1,i$1,o$1,r$1,f$1,e$1,c$1,s$1,h$1={},v$1=[],p$1=/acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i,y$1=Array.isArray;function d$1(n,l){for(var u in l)n[u]=l[u];return n}function w$1(n){n&&n.parentNode&&n.parentNode.removeChild(n);}function _(l,u,t){var i,o,r,f={};for(r in u)"key"==r?i=u[r]:"ref"==r?o=u[r]:f[r]=u[r];if(arguments.length>2&&(f.children=arguments.length>3?n.call(arguments,2):t),"function"==typeof l&&null!=l.defaultProps)for(r in l.defaultProps) void 0===f[r]&&(f[r]=l.defaultProps[r]);return g(l,f,i,o,null)}function g(n,t,i,o,r){var f={type:n,props:t,key:i,ref:o,__k:null,__:null,__b:0,__e:null,__d:void 0,__c:null,constructor:void 0,__v:null==r?++u$1:r,__i:-1,__u:0};return null==r&&null!=l$1.vnode&&l$1.vnode(f),f}function b(n){return n.children}function k$1(n,l){this.props=n,this.context=l;}function x(n,l){if(null==l)return n.__?x(n.__,n.__i+1):null;for(var u;l<n.__k.length;l++)if(null!=(u=n.__k[l])&&null!=u.__e)return u.__e;return "function"==typeof n.type?x(n):null}function C$1(n){var l,u;if(null!=(n=n.__)&&null!=n.__c){for(n.__e=n.__c.base=null,l=0;l<n.__k.length;l++)if(null!=(u=n.__k[l])&&null!=u.__e){n.__e=n.__c.base=u.__e;break}return C$1(n)}}function S(n){(!n.__d&&(n.__d=true)&&i$1.push(n)&&!M.__r++||o$1!==l$1.debounceRendering)&&((o$1=l$1.debounceRendering)||r$1)(M);}function M(){var n,u,t,o,r,e,c,s;for(i$1.sort(f$1);n=i$1.shift();)n.__d&&(u=i$1.length,o=void 0,e=(r=(t=n).__v).__e,c=[],s=[],t.__P&&((o=d$1({},r)).__v=r.__v+1,l$1.vnode&&l$1.vnode(o),O(t.__P,o,r,t.__n,t.__P.namespaceURI,32&r.__u?[e]:null,c,null==e?x(r):e,!!(32&r.__u),s),o.__v=r.__v,o.__.__k[o.__i]=o,j$1(c,o,s),o.__e!=e&&C$1(o)),i$1.length>u&&i$1.sort(f$1));M.__r=0;}function P(n,l,u,t,i,o,r,f,e,c,s){var a,p,y,d,w,_=t&&t.__k||v$1,g=l.length;for(u.__d=e,$(u,l,_),e=u.__d,a=0;a<g;a++)null!=(y=u.__k[a])&&(p=-1===y.__i?h$1:_[y.__i]||h$1,y.__i=a,O(n,y,p,i,o,r,f,e,c,s),d=y.__e,y.ref&&p.ref!=y.ref&&(p.ref&&E(p.ref,null,y),s.push(y.ref,y.__c||d,y)),null==w&&null!=d&&(w=d),65536&y.__u||p.__k===y.__k?e=I(y,e,n):"function"==typeof y.type&&void 0!==y.__d?e=y.__d:d&&(e=d.nextSibling),y.__d=void 0,y.__u&=-196609);u.__d=e,u.__e=w;}function $(n,l,u){var t,i,o,r,f,e=l.length,c=u.length,s=c,a=0;for(n.__k=[],t=0;t<e;t++)null!=(i=l[t])&&"boolean"!=typeof i&&"function"!=typeof i?(r=t+a,(i=n.__k[t]="string"==typeof i||"number"==typeof i||"bigint"==typeof i||i.constructor==String?g(null,i,null,null,null):y$1(i)?g(b,{children:i},null,null,null):void 0===i.constructor&&i.__b>0?g(i.type,i.props,i.key,i.ref?i.ref:null,i.__v):i).__=n,i.__b=n.__b+1,o=null,-1!==(f=i.__i=L(i,u,r,s))&&(s--,(o=u[f])&&(o.__u|=131072)),null==o||null===o.__v?(-1==f&&a--,"function"!=typeof i.type&&(i.__u|=65536)):f!==r&&(f==r-1?a--:f==r+1?a++:(f>r?a--:a++,i.__u|=65536))):i=n.__k[t]=null;if(s)for(t=0;t<c;t++)null!=(o=u[t])&&0==(131072&o.__u)&&(o.__e==n.__d&&(n.__d=x(o)),N(o,o));}function I(n,l,u){var t,i;if("function"==typeof n.type){for(t=n.__k,i=0;t&&i<t.length;i++)t[i]&&(t[i].__=n,l=I(t[i],l,u));return l}n.__e!=l&&(l&&n.type&&!u.contains(l)&&(l=x(n)),u.insertBefore(n.__e,l||null),l=n.__e);do{l=l&&l.nextSibling;}while(null!=l&&8===l.nodeType);return l}function L(n,l,u,t){var i=n.key,o=n.type,r=u-1,f=u+1,e=l[u];if(null===e||e&&i==e.key&&o===e.type&&0==(131072&e.__u))return u;if(("function"!=typeof o||o===b||i)&&t>(null!=e&&0==(131072&e.__u)?1:0))for(;r>=0||f<l.length;){if(r>=0){if((e=l[r])&&0==(131072&e.__u)&&i==e.key&&o===e.type)return r;r--;}if(f<l.length){if((e=l[f])&&0==(131072&e.__u)&&i==e.key&&o===e.type)return f;f++;}}return -1}function T(n,l,u){"-"===l[0]?n.setProperty(l,null==u?"":u):n[l]=null==u?"":"number"!=typeof u||p$1.test(l)?u:u+"px";}function A(n,l,u,t,i){var o;n:if("style"===l)if("string"==typeof u)n.style.cssText=u;else {if("string"==typeof t&&(n.style.cssText=t=""),t)for(l in t)u&&l in u||T(n.style,l,"");if(u)for(l in u)t&&u[l]===t[l]||T(n.style,l,u[l]);}else if("o"===l[0]&&"n"===l[1])o=l!==(l=l.replace(/(PointerCapture)$|Capture$/i,"$1")),l=l.toLowerCase()in n||"onFocusOut"===l||"onFocusIn"===l?l.toLowerCase().slice(2):l.slice(2),n.l||(n.l={}),n.l[l+o]=u,u?t?u.u=t.u:(u.u=e$1,n.addEventListener(l,o?s$1:c$1,o)):n.removeEventListener(l,o?s$1:c$1,o);else {if("http://www.w3.org/2000/svg"==i)l=l.replace(/xlink(H|:h)/,"h").replace(/sName$/,"s");else if("width"!=l&&"height"!=l&&"href"!=l&&"list"!=l&&"form"!=l&&"tabIndex"!=l&&"download"!=l&&"rowSpan"!=l&&"colSpan"!=l&&"role"!=l&&"popover"!=l&&l in n)try{n[l]=null==u?"":u;break n}catch(n){}"function"==typeof u||(null==u||false===u&&"-"!==l[4]?n.removeAttribute(l):n.setAttribute(l,"popover"==l&&1==u?"":u));}}function F(n){return function(u){if(this.l){var t=this.l[u.type+n];if(null==u.t)u.t=e$1++;else if(u.t<t.u)return;return l$1.event&&(u=l$1.event(u)),"handleEvent"in t?t.handleEvent(u):t(u)}}}function O(n,u,t,i,o,r,f,e,c,s){var a,h,v,p,w,_,g,m,x,C,S,M,$,I,H,L,T=u.type;if(void 0!==u.constructor)return null;128&t.__u&&(c=!!(32&t.__u),r=[e=u.__e=t.__e]),(a=l$1.__b)&&a(u);n:if("function"==typeof T)try{if(m=u.props,x="prototype"in T&&T.prototype.render,C=(a=T.contextType)&&i[a.__c],S=a?C?C.props.value:a.__:i,t.__c?g=(h=u.__c=t.__c).__=h.__E:(x?u.__c=h=new T(m,S):(u.__c=h=new k$1(m,S),h.constructor=T,h.render=V),C&&C.sub(h),h.props=m,h.state||(h.state={}),h.context=S,h.__n=i,v=h.__d=!0,h.__h=[],h._sb=[]),x&&null==h.__s&&(h.__s=h.state),x&&null!=T.getDerivedStateFromProps&&(h.__s==h.state&&(h.__s=d$1({},h.__s)),d$1(h.__s,T.getDerivedStateFromProps(m,h.__s))),p=h.props,w=h.state,h.__v=u,v)x&&null==T.getDerivedStateFromProps&&null!=h.componentWillMount&&h.componentWillMount(),x&&null!=h.componentDidMount&&h.__h.push(h.componentDidMount);else {if(x&&null==T.getDerivedStateFromProps&&m!==p&&null!=h.componentWillReceiveProps&&h.componentWillReceiveProps(m,S),!h.__e&&(null!=h.shouldComponentUpdate&&!1===h.shouldComponentUpdate(m,h.__s,S)||u.__v===t.__v)){for(u.__v!==t.__v&&(h.props=m,h.state=h.__s,h.__d=!1),u.__e=t.__e,u.__k=t.__k,u.__k.some(function(n){n&&(n.__=u);}),M=0;M<h._sb.length;M++)h.__h.push(h._sb[M]);h._sb=[],h.__h.length&&f.push(h);break n}null!=h.componentWillUpdate&&h.componentWillUpdate(m,h.__s,S),x&&null!=h.componentDidUpdate&&h.__h.push(function(){h.componentDidUpdate(p,w,_);});}if(h.context=S,h.props=m,h.__P=n,h.__e=!1,$=l$1.__r,I=0,x){for(h.state=h.__s,h.__d=!1,$&&$(u),a=h.render(h.props,h.state,h.context),H=0;H<h._sb.length;H++)h.__h.push(h._sb[H]);h._sb=[];}else do{h.__d=!1,$&&$(u),a=h.render(h.props,h.state,h.context),h.state=h.__s;}while(h.__d&&++I<25);h.state=h.__s,null!=h.getChildContext&&(i=d$1(d$1({},i),h.getChildContext())),x&&!v&&null!=h.getSnapshotBeforeUpdate&&(_=h.getSnapshotBeforeUpdate(p,w)),P(n,y$1(L=null!=a&&a.type===b&&null==a.key?a.props.children:a)?L:[L],u,t,i,o,r,f,e,c,s),h.base=u.__e,u.__u&=-161,h.__h.length&&f.push(h),g&&(h.__E=h.__=null);}catch(n){if(u.__v=null,c||null!=r){for(u.__u|=c?160:128;e&&8===e.nodeType&&e.nextSibling;)e=e.nextSibling;r[r.indexOf(e)]=null,u.__e=e;}else u.__e=t.__e,u.__k=t.__k;l$1.__e(n,u,t);}else null==r&&u.__v===t.__v?(u.__k=t.__k,u.__e=t.__e):u.__e=z$1(t.__e,u,t,i,o,r,f,c,s);(a=l$1.diffed)&&a(u);}function j$1(n,u,t){u.__d=void 0;for(var i=0;i<t.length;i++)E(t[i],t[++i],t[++i]);l$1.__c&&l$1.__c(u,n),n.some(function(u){try{n=u.__h,u.__h=[],n.some(function(n){n.call(u);});}catch(n){l$1.__e(n,u.__v);}});}function z$1(u,t,i,o,r,f,e,c,s){var a,v,p,d,_,g,m,b=i.props,k=t.props,C=t.type;if("svg"===C?r="http://www.w3.org/2000/svg":"math"===C?r="http://www.w3.org/1998/Math/MathML":r||(r="http://www.w3.org/1999/xhtml"),null!=f)for(a=0;a<f.length;a++)if((_=f[a])&&"setAttribute"in _==!!C&&(C?_.localName===C:3===_.nodeType)){u=_,f[a]=null;break}if(null==u){if(null===C)return document.createTextNode(k);u=document.createElementNS(r,C,k.is&&k),c&&(l$1.__m&&l$1.__m(t,f),c=false),f=null;}if(null===C)b===k||c&&u.data===k||(u.data=k);else {if(f=f&&n.call(u.childNodes),b=i.props||h$1,!c&&null!=f)for(b={},a=0;a<u.attributes.length;a++)b[(_=u.attributes[a]).name]=_.value;for(a in b)if(_=b[a],"children"==a);else if("dangerouslySetInnerHTML"==a)p=_;else if(!(a in k)){if("value"==a&&"defaultValue"in k||"checked"==a&&"defaultChecked"in k)continue;A(u,a,null,_,r);}for(a in k)_=k[a],"children"==a?d=_:"dangerouslySetInnerHTML"==a?v=_:"value"==a?g=_:"checked"==a?m=_:c&&"function"!=typeof _||b[a]===_||A(u,a,_,b[a],r);if(v)c||p&&(v.__html===p.__html||v.__html===u.innerHTML)||(u.innerHTML=v.__html),t.__k=[];else if(p&&(u.innerHTML=""),P(u,y$1(d)?d:[d],t,i,o,"foreignObject"===C?"http://www.w3.org/1999/xhtml":r,f,e,f?f[0]:i.__k&&x(i,0),c,s),null!=f)for(a=f.length;a--;)w$1(f[a]);c||(a="value","progress"===C&&null==g?u.removeAttribute("value"):void 0!==g&&(g!==u[a]||"progress"===C&&!g||"option"===C&&g!==b[a])&&A(u,a,g,b[a],r),a="checked",void 0!==m&&m!==u[a]&&A(u,a,m,b[a],r));}return u}function E(n,u,t){try{if("function"==typeof n){var i="function"==typeof n.__u;i&&n.__u(),i&&null==u||(n.__u=n(u));}else n.current=u;}catch(n){l$1.__e(n,t);}}function N(n,u,t){var i,o;if(l$1.unmount&&l$1.unmount(n),(i=n.ref)&&(i.current&&i.current!==n.__e||E(i,null,u)),null!=(i=n.__c)){if(i.componentWillUnmount)try{i.componentWillUnmount();}catch(n){l$1.__e(n,u);}i.base=i.__P=null;}if(i=n.__k)for(o=0;o<i.length;o++)i[o]&&N(i[o],u,t||"function"!=typeof n.type);t||w$1(n.__e),n.__c=n.__=n.__e=n.__d=void 0;}function V(n,l,u){return this.constructor(n,u)}function q(u,t,i){var o,r,f,e;l$1.__&&l$1.__(u,t),r=(o="function"=="undefined")?null:t.__k,f=[],e=[],O(t,u=(t).__k=_(b,null,[u]),r||h$1,h$1,t.namespaceURI,r?null:t.firstChild?n.call(t.childNodes):null,f,r?r.__e:t.firstChild,o,e),j$1(f,u,e);}n=v$1.slice,l$1={__e:function(n,l,u,t){for(var i,o,r;l=l.__;)if((i=l.__c)&&!i.__)try{if((o=i.constructor)&&null!=o.getDerivedStateFromError&&(i.setState(o.getDerivedStateFromError(n)),r=i.__d),null!=i.componentDidCatch&&(i.componentDidCatch(n,t||{}),r=i.__d),r)return i.__E=i}catch(l){n=l;}throw n}},u$1=0,k$1.prototype.setState=function(n,l){var u;u=null!=this.__s&&this.__s!==this.state?this.__s:this.__s=d$1({},this.state),"function"==typeof n&&(n=n(d$1({},u),this.props)),n&&d$1(u,n),null!=n&&this.__v&&(l&&this._sb.push(l),S(this));},k$1.prototype.forceUpdate=function(n){this.__v&&(this.__e=true,n&&this.__h.push(n),S(this));},k$1.prototype.render=b,i$1=[],r$1="function"==typeof Promise?Promise.prototype.then.bind(Promise.resolve()):setTimeout,f$1=function(n,l){return n.__v.__b-l.__v.__b},M.__r=0,e$1=0,c$1=F(false),s$1=F(true);
|
|
3233
|
-
|
|
3234
|
-
var t,r,u,i,o=0,f=[],c=l$1,e=c.__b,a=c.__r,v=c.diffed,l=c.__c,m=c.unmount,s=c.__;function d(n,t){c.__h&&c.__h(r,n,o||t),o=0;var u=r.__H||(r.__H={__:[],__h:[]});return n>=u.__.length&&u.__.push({}),u.__[n]}function h(n){return o=1,p(D,n)}function p(n,u,i){var o=d(t++,2);if(o.t=n,!o.__c&&(o.__=[D(void 0,u),function(n){var t=o.__N?o.__N[0]:o.__[0],r=o.t(t,n);t!==r&&(o.__N=[r,o.__[1]],o.__c.setState({}));}],o.__c=r,!r.u)){var f=function(n,t,r){if(!o.__c.__H)return true;var u=o.__c.__H.__.filter(function(n){return !!n.__c});if(u.every(function(n){return !n.__N}))return !c||c.call(this,n,t,r);var i=o.__c.props!==n;return u.forEach(function(n){if(n.__N){var t=n.__[0];n.__=n.__N,n.__N=void 0,t!==n.__[0]&&(i=true);}}),c&&c.call(this,n,t,r)||i};r.u=true;var c=r.shouldComponentUpdate,e=r.componentWillUpdate;r.componentWillUpdate=function(n,t,r){if(this.__e){var u=c;c=void 0,f(n,t,r),c=u;}e&&e.call(this,n,t,r);},r.shouldComponentUpdate=f;}return o.__N||o.__}function y(n,u){var i=d(t++,3);!c.__s&&C(i.__H,u)&&(i.__=n,i.i=u,r.__H.__h.push(i));}function j(){for(var n;n=f.shift();)if(n.__P&&n.__H)try{n.__H.__h.forEach(z),n.__H.__h.forEach(B),n.__H.__h=[];}catch(t){n.__H.__h=[],c.__e(t,n.__v);}}c.__b=function(n){r=null,e&&e(n);},c.__=function(n,t){n&&t.__k&&t.__k.__m&&(n.__m=t.__k.__m),s&&s(n,t);},c.__r=function(n){a&&a(n),t=0;var i=(r=n.__c).__H;i&&(u===r?(i.__h=[],r.__h=[],i.__.forEach(function(n){n.__N&&(n.__=n.__N),n.i=n.__N=void 0;})):(i.__h.forEach(z),i.__h.forEach(B),i.__h=[],t=0)),u=r;},c.diffed=function(n){v&&v(n);var t=n.__c;t&&t.__H&&(t.__H.__h.length&&(1!==f.push(t)&&i===c.requestAnimationFrame||((i=c.requestAnimationFrame)||w)(j)),t.__H.__.forEach(function(n){n.i&&(n.__H=n.i),n.i=void 0;})),u=r=null;},c.__c=function(n,t){t.some(function(n){try{n.__h.forEach(z),n.__h=n.__h.filter(function(n){return !n.__||B(n)});}catch(r){t.some(function(n){n.__h&&(n.__h=[]);}),t=[],c.__e(r,n.__v);}}),l&&l(n,t);},c.unmount=function(n){m&&m(n);var t,r=n.__c;r&&r.__H&&(r.__H.__.forEach(function(n){try{z(n);}catch(n){t=n;}}),r.__H=void 0,t&&c.__e(t,r.__v));};var k="function"==typeof requestAnimationFrame;function w(n){var t,r=function(){clearTimeout(u),k&&cancelAnimationFrame(t),setTimeout(n);},u=setTimeout(r,100);k&&(t=requestAnimationFrame(r));}function z(n){var t=r,u=n.__c;"function"==typeof u&&(n.__c=void 0,u()),r=t;}function B(n){var t=r;n.__c=n.__(),r=t;}function C(n,t){return !n||n.length!==t.length||t.some(function(t,r){return t!==n[r]})}function D(n,t){return "function"==typeof t?t(n):t}
|
|
3235
|
-
|
|
3236
|
-
var css$1 = (() => `.-cbwsdk-css-reset .-gear-container{margin-left:16px !important;margin-right:9px !important;display:flex;align-items:center;justify-content:center;width:24px;height:24px;transition:opacity .25s}.-cbwsdk-css-reset .-gear-container *{user-select:none}.-cbwsdk-css-reset .-gear-container svg{opacity:0;position:absolute}.-cbwsdk-css-reset .-gear-icon{height:12px;width:12px;z-index:10000}.-cbwsdk-css-reset .-cbwsdk-snackbar{align-items:flex-end;display:flex;flex-direction:column;position:fixed;right:0;top:0;z-index:2147483647}.-cbwsdk-css-reset .-cbwsdk-snackbar *{user-select:none}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance{display:flex;flex-direction:column;margin:8px 16px 0 16px;overflow:visible;text-align:left;transform:translateX(0);transition:opacity .25s,transform .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-header:hover .-gear-container svg{opacity:1}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-header{display:flex;align-items:center;background:#fff;overflow:hidden;border:1px solid #e7ebee;box-sizing:border-box;border-radius:8px;cursor:pointer}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-header-cblogo{margin:8px 8px 8px 8px}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-header *{cursor:pointer}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-header-message{color:#000;font-size:13px;line-height:1.5;user-select:none}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu{background:#fff;transition:opacity .25s ease-in-out,transform .25s linear,visibility 0s;visibility:hidden;border:1px solid #e7ebee;box-sizing:border-box;border-radius:8px;opacity:0;flex-direction:column;padding-left:8px;padding-right:8px}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item:last-child{margin-bottom:8px !important}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item:hover{background:#f5f7f8;border-radius:6px;transition:background .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item:hover span{color:#050f19;transition:color .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item:hover svg path{fill:#000;transition:fill .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item{visibility:inherit;height:35px;margin-top:8px;margin-bottom:0;display:flex;flex-direction:row;align-items:center;padding:8px;cursor:pointer}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item *{visibility:inherit;cursor:pointer}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item-is-red:hover{background:rgba(223,95,103,.2);transition:background .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item-is-red:hover *{cursor:pointer}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item-is-red:hover svg path{fill:#df5f67;transition:fill .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item-is-red:hover span{color:#df5f67;transition:color .25s}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-menu-item-info{color:#aaa;font-size:13px;margin:0 8px 0 32px;position:absolute}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-hidden{opacity:0;text-align:left;transform:translateX(25%);transition:opacity .5s linear}.-cbwsdk-css-reset .-cbwsdk-snackbar-instance-expanded .-cbwsdk-snackbar-instance-menu{opacity:1;display:flex;transform:translateY(8px);visibility:visible}`)();
|
|
3237
|
-
|
|
3238
|
-
// Copyright (c) 2018-2023 Coinbase, Inc. <https://www.coinbase.com/>
|
|
3239
|
-
const cblogo = `data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHBhdGggZD0iTTEuNDkyIDEwLjQxOWE4LjkzIDguOTMgMCAwMTguOTMtOC45M2gxMS4xNjNhOC45MyA4LjkzIDAgMDE4LjkzIDguOTN2MTEuMTYzYTguOTMgOC45MyAwIDAxLTguOTMgOC45M0gxMC40MjJhOC45MyA4LjkzIDAgMDEtOC45My04LjkzVjEwLjQxOXoiIGZpbGw9IiMxNjUyRjAiLz48cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZD0iTTEwLjQxOSAwSDIxLjU4QzI3LjMzNSAwIDMyIDQuNjY1IDMyIDEwLjQxOVYyMS41OEMzMiAyNy4zMzUgMjcuMzM1IDMyIDIxLjU4MSAzMkgxMC40MkM0LjY2NSAzMiAwIDI3LjMzNSAwIDIxLjU4MVYxMC40MkMwIDQuNjY1IDQuNjY1IDAgMTAuNDE5IDB6bTAgMS40ODhhOC45MyA4LjkzIDAgMDAtOC45MyA4LjkzdjExLjE2M2E4LjkzIDguOTMgMCAwMDguOTMgOC45M0gyMS41OGE4LjkzIDguOTMgMCAwMDguOTMtOC45M1YxMC40MmE4LjkzIDguOTMgMCAwMC04LjkzLTguOTNIMTAuNDJ6IiBmaWxsPSIjZmZmIi8+PHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik0xNS45OTggMjYuMDQ5Yy01LjU0OSAwLTEwLjA0Ny00LjQ5OC0xMC4wNDctMTAuMDQ3IDAtNS41NDggNC40OTgtMTAuMDQ2IDEwLjA0Ny0xMC4wNDYgNS41NDggMCAxMC4wNDYgNC40OTggMTAuMDQ2IDEwLjA0NiAwIDUuNTQ5LTQuNDk4IDEwLjA0Ny0xMC4wNDYgMTAuMDQ3eiIgZmlsbD0iI2ZmZiIvPjxwYXRoIGQ9Ik0xMi43NjIgMTQuMjU0YzAtLjgyMi42NjctMS40ODkgMS40ODktMS40ODloMy40OTdjLjgyMiAwIDEuNDg4LjY2NiAxLjQ4OCAxLjQ4OXYzLjQ5N2MwIC44MjItLjY2NiAxLjQ4OC0xLjQ4OCAxLjQ4OGgtMy40OTdhMS40ODggMS40ODggMCAwMS0xLjQ4OS0xLjQ4OHYtMy40OTh6IiBmaWxsPSIjMTY1MkYwIi8+PC9zdmc+`;
|
|
3240
|
-
const gearIcon = `data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTIiIGhlaWdodD0iMTIiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHBhdGggZD0iTTEyIDYuNzV2LTEuNWwtMS43Mi0uNTdjLS4wOC0uMjctLjE5LS41Mi0uMzItLjc3bC44MS0xLjYyLTEuMDYtMS4wNi0xLjYyLjgxYy0uMjQtLjEzLS41LS4yNC0uNzctLjMyTDYuNzUgMGgtMS41bC0uNTcgMS43MmMtLjI3LjA4LS41My4xOS0uNzcuMzJsLTEuNjItLjgxLTEuMDYgMS4wNi44MSAxLjYyYy0uMTMuMjQtLjI0LjUtLjMyLjc3TDAgNS4yNXYxLjVsMS43Mi41N2MuMDguMjcuMTkuNTMuMzIuNzdsLS44MSAxLjYyIDEuMDYgMS4wNiAxLjYyLS44MWMuMjQuMTMuNS4yMy43Ny4zMkw1LjI1IDEyaDEuNWwuNTctMS43MmMuMjctLjA4LjUyLS4xOS43Ny0uMzJsMS42Mi44MSAxLjA2LTEuMDYtLjgxLTEuNjJjLjEzLS4yNC4yMy0uNS4zMi0uNzdMMTIgNi43NXpNNiA4LjVhMi41IDIuNSAwIDAxMC01IDIuNSAyLjUgMCAwMTAgNXoiIGZpbGw9IiMwNTBGMTkiLz48L3N2Zz4=`;
|
|
3241
|
-
class Snackbar {
|
|
3242
|
-
constructor() {
|
|
3243
|
-
this.items = new Map();
|
|
3244
|
-
this.nextItemKey = 0;
|
|
3245
|
-
this.root = null;
|
|
3246
|
-
this.darkMode = isDarkMode();
|
|
3247
|
-
}
|
|
3248
|
-
attach(el) {
|
|
3249
|
-
this.root = document.createElement('div');
|
|
3250
|
-
this.root.className = '-cbwsdk-snackbar-root';
|
|
3251
|
-
el.appendChild(this.root);
|
|
3252
|
-
this.render();
|
|
3253
|
-
}
|
|
3254
|
-
presentItem(itemProps) {
|
|
3255
|
-
const key = this.nextItemKey++;
|
|
3256
|
-
this.items.set(key, itemProps);
|
|
3257
|
-
this.render();
|
|
3258
|
-
return () => {
|
|
3259
|
-
this.items.delete(key);
|
|
3260
|
-
this.render();
|
|
3261
|
-
};
|
|
3262
|
-
}
|
|
3263
|
-
clear() {
|
|
3264
|
-
this.items.clear();
|
|
3265
|
-
this.render();
|
|
3266
|
-
}
|
|
3267
|
-
render() {
|
|
3268
|
-
if (!this.root) {
|
|
3269
|
-
return;
|
|
3270
|
-
}
|
|
3271
|
-
q(_("div", null,
|
|
3272
|
-
_(SnackbarContainer, { darkMode: this.darkMode }, Array.from(this.items.entries()).map(([key, itemProps]) => (_(SnackbarInstance, Object.assign({}, itemProps, { key: key })))))), this.root);
|
|
3273
|
-
}
|
|
3274
|
-
}
|
|
3275
|
-
const SnackbarContainer = (props) => (_("div", { class: clsx('-cbwsdk-snackbar-container') },
|
|
3276
|
-
_("style", null, css$1),
|
|
3277
|
-
_("div", { class: "-cbwsdk-snackbar" }, props.children)));
|
|
3278
|
-
const SnackbarInstance = ({ autoExpand, message, menuItems, }) => {
|
|
3279
|
-
const [hidden, setHidden] = h(true);
|
|
3280
|
-
const [expanded, setExpanded] = h(autoExpand !== null && autoExpand !== void 0 ? autoExpand : false);
|
|
3281
|
-
y(() => {
|
|
3282
|
-
const timers = [
|
|
3283
|
-
window.setTimeout(() => {
|
|
3284
|
-
setHidden(false);
|
|
3285
|
-
}, 1),
|
|
3286
|
-
window.setTimeout(() => {
|
|
3287
|
-
setExpanded(true);
|
|
3288
|
-
}, 10000),
|
|
3289
|
-
];
|
|
3290
|
-
return () => {
|
|
3291
|
-
timers.forEach(window.clearTimeout);
|
|
3292
|
-
};
|
|
3293
|
-
});
|
|
3294
|
-
const toggleExpanded = () => {
|
|
3295
|
-
setExpanded(!expanded);
|
|
3296
|
-
};
|
|
3297
|
-
return (_("div", { class: clsx('-cbwsdk-snackbar-instance', hidden && '-cbwsdk-snackbar-instance-hidden', expanded && '-cbwsdk-snackbar-instance-expanded') },
|
|
3298
|
-
_("div", { class: "-cbwsdk-snackbar-instance-header", onClick: toggleExpanded },
|
|
3299
|
-
_("img", { src: cblogo, class: "-cbwsdk-snackbar-instance-header-cblogo" }),
|
|
3300
|
-
' ',
|
|
3301
|
-
_("div", { class: "-cbwsdk-snackbar-instance-header-message" }, message),
|
|
3302
|
-
_("div", { class: "-gear-container" },
|
|
3303
|
-
!expanded && (_("svg", { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/svg" },
|
|
3304
|
-
_("circle", { cx: "12", cy: "12", r: "12", fill: "#F5F7F8" }))),
|
|
3305
|
-
_("img", { src: gearIcon, class: "-gear-icon", title: "Expand" }))),
|
|
3306
|
-
menuItems && menuItems.length > 0 && (_("div", { class: "-cbwsdk-snackbar-instance-menu" }, menuItems.map((action, i) => (_("div", { class: clsx('-cbwsdk-snackbar-instance-menu-item', action.isRed && '-cbwsdk-snackbar-instance-menu-item-is-red'), onClick: action.onClick, key: i },
|
|
3307
|
-
_("svg", { width: action.svgWidth, height: action.svgHeight, viewBox: "0 0 10 11", fill: "none", xmlns: "http://www.w3.org/2000/svg" },
|
|
3308
|
-
_("path", { "fill-rule": action.defaultFillRule, "clip-rule": action.defaultClipRule, d: action.path, fill: "#AAAAAA" })),
|
|
3309
|
-
_("span", { class: clsx('-cbwsdk-snackbar-instance-menu-item-info', action.isRed && '-cbwsdk-snackbar-instance-menu-item-info-is-red') }, action.info))))))));
|
|
3310
|
-
};
|
|
3311
|
-
|
|
3312
|
-
class WalletLinkRelayUI {
|
|
3313
|
-
constructor() {
|
|
3314
|
-
this.attached = false;
|
|
3315
|
-
this.snackbar = new Snackbar();
|
|
3316
|
-
}
|
|
3317
|
-
attach() {
|
|
3318
|
-
if (this.attached) {
|
|
3319
|
-
throw new Error('Coinbase Wallet SDK UI is already attached');
|
|
3320
|
-
}
|
|
3321
|
-
const el = document.documentElement;
|
|
3322
|
-
const container = document.createElement('div');
|
|
3323
|
-
container.className = '-cbwsdk-css-reset';
|
|
3324
|
-
el.appendChild(container);
|
|
3325
|
-
this.snackbar.attach(container);
|
|
3326
|
-
this.attached = true;
|
|
3327
|
-
injectCssReset();
|
|
3328
|
-
}
|
|
3329
|
-
showConnecting(options) {
|
|
3330
|
-
let snackbarProps;
|
|
3331
|
-
if (options.isUnlinkedErrorState) {
|
|
3332
|
-
snackbarProps = {
|
|
3333
|
-
autoExpand: true,
|
|
3334
|
-
message: 'Connection lost',
|
|
3335
|
-
menuItems: [
|
|
3336
|
-
{
|
|
3337
|
-
isRed: false,
|
|
3338
|
-
info: 'Reset connection',
|
|
3339
|
-
svgWidth: '10',
|
|
3340
|
-
svgHeight: '11',
|
|
3341
|
-
path: 'M5.00008 0.96875C6.73133 0.96875 8.23758 1.94375 9.00008 3.375L10.0001 2.375V5.5H9.53133H7.96883H6.87508L7.80633 4.56875C7.41258 3.3875 6.31258 2.53125 5.00008 2.53125C3.76258 2.53125 2.70633 3.2875 2.25633 4.36875L0.812576 3.76875C1.50008 2.125 3.11258 0.96875 5.00008 0.96875ZM2.19375 6.43125C2.5875 7.6125 3.6875 8.46875 5 8.46875C6.2375 8.46875 7.29375 7.7125 7.74375 6.63125L9.1875 7.23125C8.5 8.875 6.8875 10.0312 5 10.0312C3.26875 10.0312 1.7625 9.05625 1 7.625L0 8.625V5.5H0.46875H2.03125H3.125L2.19375 6.43125Z',
|
|
3342
|
-
defaultFillRule: 'evenodd',
|
|
3343
|
-
defaultClipRule: 'evenodd',
|
|
3344
|
-
onClick: options.onResetConnection,
|
|
3345
|
-
},
|
|
3346
|
-
],
|
|
3347
|
-
};
|
|
3348
|
-
}
|
|
3349
|
-
else {
|
|
3350
|
-
snackbarProps = {
|
|
3351
|
-
message: 'Confirm on phone',
|
|
3352
|
-
menuItems: [
|
|
3353
|
-
{
|
|
3354
|
-
isRed: true,
|
|
3355
|
-
info: 'Cancel transaction',
|
|
3356
|
-
svgWidth: '11',
|
|
3357
|
-
svgHeight: '11',
|
|
3358
|
-
path: 'M10.3711 1.52346L9.21775 0.370117L5.37109 4.21022L1.52444 0.370117L0.371094 1.52346L4.2112 5.37012L0.371094 9.21677L1.52444 10.3701L5.37109 6.53001L9.21775 10.3701L10.3711 9.21677L6.53099 5.37012L10.3711 1.52346Z',
|
|
3359
|
-
defaultFillRule: 'inherit',
|
|
3360
|
-
defaultClipRule: 'inherit',
|
|
3361
|
-
onClick: options.onCancel,
|
|
3362
|
-
},
|
|
3363
|
-
{
|
|
3364
|
-
isRed: false,
|
|
3365
|
-
info: 'Reset connection',
|
|
3366
|
-
svgWidth: '10',
|
|
3367
|
-
svgHeight: '11',
|
|
3368
|
-
path: 'M5.00008 0.96875C6.73133 0.96875 8.23758 1.94375 9.00008 3.375L10.0001 2.375V5.5H9.53133H7.96883H6.87508L7.80633 4.56875C7.41258 3.3875 6.31258 2.53125 5.00008 2.53125C3.76258 2.53125 2.70633 3.2875 2.25633 4.36875L0.812576 3.76875C1.50008 2.125 3.11258 0.96875 5.00008 0.96875ZM2.19375 6.43125C2.5875 7.6125 3.6875 8.46875 5 8.46875C6.2375 8.46875 7.29375 7.7125 7.74375 6.63125L9.1875 7.23125C8.5 8.875 6.8875 10.0312 5 10.0312C3.26875 10.0312 1.7625 9.05625 1 7.625L0 8.625V5.5H0.46875H2.03125H3.125L2.19375 6.43125Z',
|
|
3369
|
-
defaultFillRule: 'evenodd',
|
|
3370
|
-
defaultClipRule: 'evenodd',
|
|
3371
|
-
onClick: options.onResetConnection,
|
|
3372
|
-
},
|
|
3373
|
-
],
|
|
3374
|
-
};
|
|
3375
|
-
}
|
|
3376
|
-
return this.snackbar.presentItem(snackbarProps);
|
|
3377
|
-
}
|
|
3378
|
-
}
|
|
3379
|
-
|
|
3380
|
-
var css = (() => `.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-backdrop{position:fixed;top:0;left:0;right:0;bottom:0;transition:opacity .25s;background-color:rgba(10,11,13,.5)}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-backdrop-hidden{opacity:0}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box{display:block;position:fixed;top:50%;left:50%;transform:translate(-50%, -50%);padding:20px;border-radius:8px;background-color:#fff;color:#0a0b0d}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box p{display:block;font-weight:400;font-size:14px;line-height:20px;padding-bottom:12px;color:#5b636e}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box button{appearance:none;border:none;background:none;color:#0052ff;padding:0;text-decoration:none;display:block;font-weight:600;font-size:16px;line-height:24px}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box.dark{background-color:#0a0b0d;color:#fff}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box.dark button{color:#0052ff}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box.light{background-color:#fff;color:#0a0b0d}.-cbwsdk-css-reset .-cbwsdk-redirect-dialog-box.light button{color:#0052ff}`)();
|
|
3381
|
-
|
|
3382
|
-
class RedirectDialog {
|
|
3383
|
-
constructor() {
|
|
3384
|
-
this.root = null;
|
|
3385
|
-
this.darkMode = isDarkMode();
|
|
3386
|
-
}
|
|
3387
|
-
attach() {
|
|
3388
|
-
const el = document.documentElement;
|
|
3389
|
-
this.root = document.createElement('div');
|
|
3390
|
-
this.root.className = '-cbwsdk-css-reset';
|
|
3391
|
-
el.appendChild(this.root);
|
|
3392
|
-
injectCssReset();
|
|
3393
|
-
}
|
|
3394
|
-
present(props) {
|
|
3395
|
-
this.render(props);
|
|
3396
|
-
}
|
|
3397
|
-
clear() {
|
|
3398
|
-
this.render(null);
|
|
3399
|
-
}
|
|
3400
|
-
render(props) {
|
|
3401
|
-
if (!this.root)
|
|
3402
|
-
return;
|
|
3403
|
-
q(null, this.root);
|
|
3404
|
-
if (!props)
|
|
3405
|
-
return;
|
|
3406
|
-
q(_(RedirectDialogContent, Object.assign({}, props, { onDismiss: () => {
|
|
3407
|
-
this.clear();
|
|
3408
|
-
}, darkMode: this.darkMode })), this.root);
|
|
3409
|
-
}
|
|
3410
|
-
}
|
|
3411
|
-
const RedirectDialogContent = ({ title, buttonText, darkMode, onButtonClick, onDismiss }) => {
|
|
3412
|
-
const theme = darkMode ? 'dark' : 'light';
|
|
3413
|
-
return (_(SnackbarContainer, { darkMode: darkMode },
|
|
3414
|
-
_("div", { class: "-cbwsdk-redirect-dialog" },
|
|
3415
|
-
_("style", null, css),
|
|
3416
|
-
_("div", { class: "-cbwsdk-redirect-dialog-backdrop", onClick: onDismiss }),
|
|
3417
|
-
_("div", { class: clsx('-cbwsdk-redirect-dialog-box', theme) },
|
|
3418
|
-
_("p", null, title),
|
|
3419
|
-
_("button", { onClick: onButtonClick }, buttonText)))));
|
|
3420
|
-
};
|
|
3421
|
-
|
|
3422
|
-
const CB_KEYS_URL = 'https://keys.coinbase.com/connect';
|
|
3423
|
-
const WALLETLINK_URL = 'https://www.walletlink.org';
|
|
3424
|
-
const CBW_MOBILE_DEEPLINK_URL = 'https://go.cb-w.com/walletlink';
|
|
3425
|
-
|
|
3426
|
-
class WLMobileRelayUI {
|
|
3427
|
-
constructor() {
|
|
3428
|
-
this.attached = false;
|
|
3429
|
-
this.redirectDialog = new RedirectDialog();
|
|
3430
|
-
}
|
|
3431
|
-
attach() {
|
|
3432
|
-
if (this.attached) {
|
|
3433
|
-
throw new Error('Coinbase Wallet SDK UI is already attached');
|
|
3434
|
-
}
|
|
3435
|
-
this.redirectDialog.attach();
|
|
3436
|
-
this.attached = true;
|
|
3437
|
-
}
|
|
3438
|
-
redirectToCoinbaseWallet(walletLinkUrl) {
|
|
3439
|
-
const url = new URL(CBW_MOBILE_DEEPLINK_URL);
|
|
3440
|
-
url.searchParams.append('redirect_url', getLocation().href);
|
|
3441
|
-
if (walletLinkUrl) {
|
|
3442
|
-
url.searchParams.append('wl_url', walletLinkUrl);
|
|
3443
|
-
}
|
|
3444
|
-
const anchorTag = document.createElement('a');
|
|
3445
|
-
anchorTag.target = 'cbw-opener';
|
|
3446
|
-
anchorTag.href = url.href;
|
|
3447
|
-
anchorTag.rel = 'noreferrer noopener';
|
|
3448
|
-
anchorTag.click();
|
|
3449
|
-
}
|
|
3450
|
-
openCoinbaseWalletDeeplink(walletLinkUrl) {
|
|
3451
|
-
this.redirectDialog.present({
|
|
3452
|
-
title: 'Redirecting to Coinbase Wallet...',
|
|
3453
|
-
buttonText: 'Open',
|
|
3454
|
-
onButtonClick: () => {
|
|
3455
|
-
this.redirectToCoinbaseWallet(walletLinkUrl);
|
|
3456
|
-
},
|
|
3457
|
-
});
|
|
3458
|
-
setTimeout(() => {
|
|
3459
|
-
this.redirectToCoinbaseWallet(walletLinkUrl);
|
|
3460
|
-
}, 99);
|
|
3461
|
-
}
|
|
3462
|
-
showConnecting(_options) {
|
|
3463
|
-
// it uses the return callback to clear the dialog
|
|
3464
|
-
return () => {
|
|
3465
|
-
this.redirectDialog.clear();
|
|
3466
|
-
};
|
|
3467
|
-
}
|
|
3468
|
-
}
|
|
3469
|
-
|
|
3470
|
-
// Copyright (c) 2018-2023 Coinbase, Inc. <https://www.coinbase.com/>
|
|
3471
|
-
class WalletLinkRelay {
|
|
3472
|
-
constructor(options) {
|
|
3473
|
-
this.chainCallbackParams = { chainId: '', jsonRpcUrl: '' }; // to implement distinctUntilChanged
|
|
3474
|
-
this.isMobileWeb = isMobileWeb();
|
|
3475
|
-
this.linkedUpdated = (linked) => {
|
|
3476
|
-
this.isLinked = linked;
|
|
3477
|
-
const cachedAddresses = this.storage.getItem(LOCAL_STORAGE_ADDRESSES_KEY);
|
|
3478
|
-
if (linked) {
|
|
3479
|
-
// Only set linked session variable one way
|
|
3480
|
-
this._session.linked = linked;
|
|
3481
|
-
}
|
|
3482
|
-
this.isUnlinkedErrorState = false;
|
|
3483
|
-
if (cachedAddresses) {
|
|
3484
|
-
const addresses = cachedAddresses.split(' ');
|
|
3485
|
-
const wasConnectedViaStandalone = this.storage.getItem('IsStandaloneSigning') === 'true';
|
|
3486
|
-
if (addresses[0] !== '' && !linked && this._session.linked && !wasConnectedViaStandalone) {
|
|
3487
|
-
this.isUnlinkedErrorState = true;
|
|
3488
|
-
}
|
|
3489
|
-
}
|
|
3490
|
-
};
|
|
3491
|
-
this.metadataUpdated = (key, value) => {
|
|
3492
|
-
this.storage.setItem(key, value);
|
|
3493
|
-
};
|
|
3494
|
-
this.chainUpdated = (chainId, jsonRpcUrl) => {
|
|
3495
|
-
if (this.chainCallbackParams.chainId === chainId &&
|
|
3496
|
-
this.chainCallbackParams.jsonRpcUrl === jsonRpcUrl) {
|
|
3497
|
-
return;
|
|
3498
|
-
}
|
|
3499
|
-
this.chainCallbackParams = {
|
|
3500
|
-
chainId,
|
|
3501
|
-
jsonRpcUrl,
|
|
3502
|
-
};
|
|
3503
|
-
if (this.chainCallback) {
|
|
3504
|
-
this.chainCallback(jsonRpcUrl, Number.parseInt(chainId, 10));
|
|
3505
|
-
}
|
|
3506
|
-
};
|
|
3507
|
-
this.accountUpdated = (selectedAddress) => {
|
|
3508
|
-
if (this.accountsCallback) {
|
|
3509
|
-
this.accountsCallback([selectedAddress]);
|
|
3510
|
-
}
|
|
3511
|
-
if (WalletLinkRelay.accountRequestCallbackIds.size > 0) {
|
|
3512
|
-
// We get the ethereum address from the metadata. If for whatever
|
|
3513
|
-
// reason we don't get a response via an explicit web3 message
|
|
3514
|
-
// we can still fulfill the eip1102 request.
|
|
3515
|
-
Array.from(WalletLinkRelay.accountRequestCallbackIds.values()).forEach((id) => {
|
|
3516
|
-
this.invokeCallback(id, {
|
|
3517
|
-
method: 'requestEthereumAccounts',
|
|
3518
|
-
result: [selectedAddress],
|
|
3519
|
-
});
|
|
3520
|
-
});
|
|
3521
|
-
WalletLinkRelay.accountRequestCallbackIds.clear();
|
|
3522
|
-
}
|
|
3523
|
-
};
|
|
3524
|
-
this.resetAndReload = this.resetAndReload.bind(this);
|
|
3525
|
-
this.linkAPIUrl = options.linkAPIUrl;
|
|
3526
|
-
this.storage = options.storage;
|
|
3527
|
-
this.metadata = options.metadata;
|
|
3528
|
-
this.accountsCallback = options.accountsCallback;
|
|
3529
|
-
this.chainCallback = options.chainCallback;
|
|
3530
|
-
const { session, ui, connection } = this.subscribe();
|
|
3531
|
-
this._session = session;
|
|
3532
|
-
this.connection = connection;
|
|
3533
|
-
this.relayEventManager = new RelayEventManager();
|
|
3534
|
-
this.ui = ui;
|
|
3535
|
-
this.ui.attach();
|
|
3536
|
-
}
|
|
3537
|
-
subscribe() {
|
|
3538
|
-
const session = WalletLinkSession.load(this.storage) || WalletLinkSession.create(this.storage);
|
|
3539
|
-
const { linkAPIUrl } = this;
|
|
3540
|
-
const connection = new WalletLinkConnection({
|
|
3541
|
-
session,
|
|
3542
|
-
linkAPIUrl,
|
|
3543
|
-
listener: this,
|
|
3544
|
-
});
|
|
3545
|
-
const ui = this.isMobileWeb ? new WLMobileRelayUI() : new WalletLinkRelayUI();
|
|
3546
|
-
connection.connect();
|
|
3547
|
-
return { session, ui, connection };
|
|
3548
|
-
}
|
|
3549
|
-
resetAndReload() {
|
|
3550
|
-
this.connection
|
|
3551
|
-
.destroy()
|
|
3552
|
-
.then(() => {
|
|
3553
|
-
/**
|
|
3554
|
-
* Only clear storage if the session id we have in memory matches the one on disk
|
|
3555
|
-
* Otherwise, in the case where we have 2 tabs, another tab might have cleared
|
|
3556
|
-
* storage already. In that case if we clear storage again, the user will be in
|
|
3557
|
-
* a state where the first tab allows the user to connect but the session that
|
|
3558
|
-
* was used isn't persisted. This leaves the user in a state where they aren't
|
|
3559
|
-
* connected to the mobile app.
|
|
3560
|
-
*/
|
|
3561
|
-
const storedSession = WalletLinkSession.load(this.storage);
|
|
3562
|
-
if ((storedSession === null || storedSession === void 0 ? void 0 : storedSession.id) === this._session.id) {
|
|
3563
|
-
ScopedLocalStorage.clearAll();
|
|
3564
|
-
}
|
|
3565
|
-
document.location.reload();
|
|
3566
|
-
})
|
|
3567
|
-
.catch((_) => { });
|
|
3568
|
-
}
|
|
3569
|
-
signEthereumTransaction(params) {
|
|
3570
|
-
return this.sendRequest({
|
|
3571
|
-
method: 'signEthereumTransaction',
|
|
3572
|
-
params: {
|
|
3573
|
-
fromAddress: params.fromAddress,
|
|
3574
|
-
toAddress: params.toAddress,
|
|
3575
|
-
weiValue: bigIntStringFromBigInt(params.weiValue),
|
|
3576
|
-
data: hexStringFromBuffer(params.data, true),
|
|
3577
|
-
nonce: params.nonce,
|
|
3578
|
-
gasPriceInWei: params.gasPriceInWei ? bigIntStringFromBigInt(params.gasPriceInWei) : null,
|
|
3579
|
-
maxFeePerGas: params.gasPriceInWei ? bigIntStringFromBigInt(params.gasPriceInWei) : null,
|
|
3580
|
-
maxPriorityFeePerGas: params.gasPriceInWei
|
|
3581
|
-
? bigIntStringFromBigInt(params.gasPriceInWei)
|
|
3582
|
-
: null,
|
|
3583
|
-
gasLimit: params.gasLimit ? bigIntStringFromBigInt(params.gasLimit) : null,
|
|
3584
|
-
chainId: params.chainId,
|
|
3585
|
-
shouldSubmit: false,
|
|
3586
|
-
},
|
|
3587
|
-
});
|
|
3588
|
-
}
|
|
3589
|
-
signAndSubmitEthereumTransaction(params) {
|
|
3590
|
-
return this.sendRequest({
|
|
3591
|
-
method: 'signEthereumTransaction',
|
|
3592
|
-
params: {
|
|
3593
|
-
fromAddress: params.fromAddress,
|
|
3594
|
-
toAddress: params.toAddress,
|
|
3595
|
-
weiValue: bigIntStringFromBigInt(params.weiValue),
|
|
3596
|
-
data: hexStringFromBuffer(params.data, true),
|
|
3597
|
-
nonce: params.nonce,
|
|
3598
|
-
gasPriceInWei: params.gasPriceInWei ? bigIntStringFromBigInt(params.gasPriceInWei) : null,
|
|
3599
|
-
maxFeePerGas: params.maxFeePerGas ? bigIntStringFromBigInt(params.maxFeePerGas) : null,
|
|
3600
|
-
maxPriorityFeePerGas: params.maxPriorityFeePerGas
|
|
3601
|
-
? bigIntStringFromBigInt(params.maxPriorityFeePerGas)
|
|
3602
|
-
: null,
|
|
3603
|
-
gasLimit: params.gasLimit ? bigIntStringFromBigInt(params.gasLimit) : null,
|
|
3604
|
-
chainId: params.chainId,
|
|
3605
|
-
shouldSubmit: true,
|
|
3606
|
-
},
|
|
3607
|
-
});
|
|
3608
|
-
}
|
|
3609
|
-
submitEthereumTransaction(signedTransaction, chainId) {
|
|
3610
|
-
return this.sendRequest({
|
|
3611
|
-
method: 'submitEthereumTransaction',
|
|
3612
|
-
params: {
|
|
3613
|
-
signedTransaction: hexStringFromBuffer(signedTransaction, true),
|
|
3614
|
-
chainId,
|
|
3615
|
-
},
|
|
3616
|
-
});
|
|
3617
|
-
}
|
|
3618
|
-
getWalletLinkSession() {
|
|
3619
|
-
return this._session;
|
|
3620
|
-
}
|
|
3621
|
-
sendRequest(request) {
|
|
3622
|
-
let hideSnackbarItem = null;
|
|
3623
|
-
const id = randomBytesHex(8);
|
|
3624
|
-
const cancel = (error) => {
|
|
3625
|
-
this.publishWeb3RequestCanceledEvent(id);
|
|
3626
|
-
this.handleErrorResponse(id, request.method, error);
|
|
3627
|
-
hideSnackbarItem === null || hideSnackbarItem === void 0 ? void 0 : hideSnackbarItem();
|
|
3628
|
-
};
|
|
3629
|
-
return new Promise((resolve, reject) => {
|
|
3630
|
-
{
|
|
3631
|
-
hideSnackbarItem = this.ui.showConnecting({
|
|
3632
|
-
isUnlinkedErrorState: this.isUnlinkedErrorState,
|
|
3633
|
-
onCancel: cancel,
|
|
3634
|
-
onResetConnection: this.resetAndReload, // eslint-disable-line @typescript-eslint/unbound-method
|
|
3635
|
-
});
|
|
3636
|
-
}
|
|
3637
|
-
this.relayEventManager.callbacks.set(id, (response) => {
|
|
3638
|
-
hideSnackbarItem === null || hideSnackbarItem === void 0 ? void 0 : hideSnackbarItem();
|
|
3639
|
-
if (isErrorResponse(response)) {
|
|
3640
|
-
return reject(new Error(response.errorMessage));
|
|
3641
|
-
}
|
|
3642
|
-
resolve(response);
|
|
3643
|
-
});
|
|
3644
|
-
this.publishWeb3RequestEvent(id, request);
|
|
3645
|
-
});
|
|
3646
|
-
}
|
|
3647
|
-
publishWeb3RequestEvent(id, request) {
|
|
3648
|
-
const message = { type: 'WEB3_REQUEST', id, request };
|
|
3649
|
-
this.publishEvent('Web3Request', message, true)
|
|
3650
|
-
.then((_) => { })
|
|
3651
|
-
.catch((err) => {
|
|
3652
|
-
this.handleWeb3ResponseMessage(message.id, {
|
|
3653
|
-
method: request.method,
|
|
3654
|
-
errorMessage: err.message,
|
|
3655
|
-
});
|
|
3656
|
-
});
|
|
3657
|
-
if (this.isMobileWeb) {
|
|
3658
|
-
this.openCoinbaseWalletDeeplink(request.method);
|
|
3659
|
-
}
|
|
3660
|
-
}
|
|
3661
|
-
// copied from MobileRelay
|
|
3662
|
-
openCoinbaseWalletDeeplink(method) {
|
|
3663
|
-
if (!(this.ui instanceof WLMobileRelayUI))
|
|
3664
|
-
return;
|
|
3665
|
-
// For mobile relay requests, open the Coinbase Wallet app
|
|
3666
|
-
switch (method) {
|
|
3667
|
-
case 'requestEthereumAccounts': // requestEthereumAccounts is handled via popup
|
|
3668
|
-
case 'switchEthereumChain': // switchEthereumChain doesn't need to open the app
|
|
3669
|
-
return;
|
|
3670
|
-
default:
|
|
3671
|
-
window.addEventListener('blur', () => {
|
|
3672
|
-
window.addEventListener('focus', () => {
|
|
3673
|
-
this.connection.checkUnseenEvents();
|
|
3674
|
-
}, { once: true });
|
|
3675
|
-
}, { once: true });
|
|
3676
|
-
this.ui.openCoinbaseWalletDeeplink();
|
|
3677
|
-
break;
|
|
3678
|
-
}
|
|
3679
|
-
}
|
|
3680
|
-
publishWeb3RequestCanceledEvent(id) {
|
|
3681
|
-
const message = {
|
|
3682
|
-
type: 'WEB3_REQUEST_CANCELED',
|
|
3683
|
-
id,
|
|
3684
|
-
};
|
|
3685
|
-
this.publishEvent('Web3RequestCanceled', message, false).then();
|
|
3686
|
-
}
|
|
3687
|
-
publishEvent(event, message, callWebhook) {
|
|
3688
|
-
return this.connection.publishEvent(event, message, callWebhook);
|
|
3689
|
-
}
|
|
3690
|
-
handleWeb3ResponseMessage(id, response) {
|
|
3691
|
-
if (response.method === 'requestEthereumAccounts') {
|
|
3692
|
-
WalletLinkRelay.accountRequestCallbackIds.forEach((id) => this.invokeCallback(id, response));
|
|
3693
|
-
WalletLinkRelay.accountRequestCallbackIds.clear();
|
|
3694
|
-
return;
|
|
3695
|
-
}
|
|
3696
|
-
this.invokeCallback(id, response);
|
|
3697
|
-
}
|
|
3698
|
-
handleErrorResponse(id, method, error) {
|
|
3699
|
-
var _a;
|
|
3700
|
-
const errorMessage = (_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : 'Unspecified error message.';
|
|
3701
|
-
this.handleWeb3ResponseMessage(id, {
|
|
3702
|
-
method,
|
|
3703
|
-
errorMessage,
|
|
3704
|
-
});
|
|
3705
|
-
}
|
|
3706
|
-
invokeCallback(id, response) {
|
|
3707
|
-
const callback = this.relayEventManager.callbacks.get(id);
|
|
3708
|
-
if (callback) {
|
|
3709
|
-
callback(response);
|
|
3710
|
-
this.relayEventManager.callbacks.delete(id);
|
|
3711
|
-
}
|
|
3712
|
-
}
|
|
3713
|
-
requestEthereumAccounts() {
|
|
3714
|
-
const { appName, appLogoUrl } = this.metadata;
|
|
3715
|
-
const request = {
|
|
3716
|
-
method: 'requestEthereumAccounts',
|
|
3717
|
-
params: {
|
|
3718
|
-
appName,
|
|
3719
|
-
appLogoUrl,
|
|
3720
|
-
},
|
|
3721
|
-
};
|
|
3722
|
-
const id = randomBytesHex(8);
|
|
3723
|
-
return new Promise((resolve, reject) => {
|
|
3724
|
-
this.relayEventManager.callbacks.set(id, (response) => {
|
|
3725
|
-
if (isErrorResponse(response)) {
|
|
3726
|
-
return reject(new Error(response.errorMessage));
|
|
3727
|
-
}
|
|
3728
|
-
resolve(response);
|
|
3729
|
-
});
|
|
3730
|
-
WalletLinkRelay.accountRequestCallbackIds.add(id);
|
|
3731
|
-
this.publishWeb3RequestEvent(id, request);
|
|
3732
|
-
});
|
|
3733
|
-
}
|
|
3734
|
-
watchAsset(type, address, symbol, decimals, image, chainId) {
|
|
3735
|
-
const request = {
|
|
3736
|
-
method: 'watchAsset',
|
|
3737
|
-
params: {
|
|
3738
|
-
type,
|
|
3739
|
-
options: {
|
|
3740
|
-
address,
|
|
3741
|
-
symbol,
|
|
3742
|
-
decimals,
|
|
3743
|
-
image,
|
|
3744
|
-
},
|
|
3745
|
-
chainId,
|
|
3746
|
-
},
|
|
3747
|
-
};
|
|
3748
|
-
let hideSnackbarItem = null;
|
|
3749
|
-
const id = randomBytesHex(8);
|
|
3750
|
-
const cancel = (error) => {
|
|
3751
|
-
this.publishWeb3RequestCanceledEvent(id);
|
|
3752
|
-
this.handleErrorResponse(id, request.method, error);
|
|
3753
|
-
hideSnackbarItem === null || hideSnackbarItem === void 0 ? void 0 : hideSnackbarItem();
|
|
3754
|
-
};
|
|
3755
|
-
{
|
|
3756
|
-
hideSnackbarItem = this.ui.showConnecting({
|
|
3757
|
-
isUnlinkedErrorState: this.isUnlinkedErrorState,
|
|
3758
|
-
onCancel: cancel,
|
|
3759
|
-
onResetConnection: this.resetAndReload, // eslint-disable-line @typescript-eslint/unbound-method
|
|
3760
|
-
});
|
|
3761
|
-
}
|
|
3762
|
-
return new Promise((resolve, reject) => {
|
|
3763
|
-
this.relayEventManager.callbacks.set(id, (response) => {
|
|
3764
|
-
hideSnackbarItem === null || hideSnackbarItem === void 0 ? void 0 : hideSnackbarItem();
|
|
3765
|
-
if (isErrorResponse(response)) {
|
|
3766
|
-
return reject(new Error(response.errorMessage));
|
|
3767
|
-
}
|
|
3768
|
-
resolve(response);
|
|
3769
|
-
});
|
|
3770
|
-
this.publishWeb3RequestEvent(id, request);
|
|
3771
|
-
});
|
|
3772
|
-
}
|
|
3773
|
-
addEthereumChain(chainId, rpcUrls, iconUrls, blockExplorerUrls, chainName, nativeCurrency) {
|
|
3774
|
-
const request = {
|
|
3775
|
-
method: 'addEthereumChain',
|
|
3776
|
-
params: {
|
|
3777
|
-
chainId,
|
|
3778
|
-
rpcUrls,
|
|
3779
|
-
blockExplorerUrls,
|
|
3780
|
-
chainName,
|
|
3781
|
-
iconUrls,
|
|
3782
|
-
nativeCurrency,
|
|
3783
|
-
},
|
|
3784
|
-
};
|
|
3785
|
-
let hideSnackbarItem = null;
|
|
3786
|
-
const id = randomBytesHex(8);
|
|
3787
|
-
const cancel = (error) => {
|
|
3788
|
-
this.publishWeb3RequestCanceledEvent(id);
|
|
3789
|
-
this.handleErrorResponse(id, request.method, error);
|
|
3790
|
-
hideSnackbarItem === null || hideSnackbarItem === void 0 ? void 0 : hideSnackbarItem();
|
|
3791
|
-
};
|
|
3792
|
-
{
|
|
3793
|
-
hideSnackbarItem = this.ui.showConnecting({
|
|
3794
|
-
isUnlinkedErrorState: this.isUnlinkedErrorState,
|
|
3795
|
-
onCancel: cancel,
|
|
3796
|
-
onResetConnection: this.resetAndReload, // eslint-disable-line @typescript-eslint/unbound-method
|
|
3797
|
-
});
|
|
3798
|
-
}
|
|
3799
|
-
return new Promise((resolve, reject) => {
|
|
3800
|
-
this.relayEventManager.callbacks.set(id, (response) => {
|
|
3801
|
-
hideSnackbarItem === null || hideSnackbarItem === void 0 ? void 0 : hideSnackbarItem();
|
|
3802
|
-
if (isErrorResponse(response)) {
|
|
3803
|
-
return reject(new Error(response.errorMessage));
|
|
3804
|
-
}
|
|
3805
|
-
resolve(response);
|
|
3806
|
-
});
|
|
3807
|
-
this.publishWeb3RequestEvent(id, request);
|
|
3808
|
-
});
|
|
3809
|
-
}
|
|
3810
|
-
switchEthereumChain(chainId, address) {
|
|
3811
|
-
const request = {
|
|
3812
|
-
method: 'switchEthereumChain',
|
|
3813
|
-
params: Object.assign({ chainId }, { address }),
|
|
3814
|
-
};
|
|
3815
|
-
let hideSnackbarItem = null;
|
|
3816
|
-
const id = randomBytesHex(8);
|
|
3817
|
-
const cancel = (error) => {
|
|
3818
|
-
this.publishWeb3RequestCanceledEvent(id);
|
|
3819
|
-
this.handleErrorResponse(id, request.method, error);
|
|
3820
|
-
hideSnackbarItem === null || hideSnackbarItem === void 0 ? void 0 : hideSnackbarItem();
|
|
3821
|
-
};
|
|
3822
|
-
{
|
|
3823
|
-
hideSnackbarItem = this.ui.showConnecting({
|
|
3824
|
-
isUnlinkedErrorState: this.isUnlinkedErrorState,
|
|
3825
|
-
onCancel: cancel,
|
|
3826
|
-
onResetConnection: this.resetAndReload, // eslint-disable-line @typescript-eslint/unbound-method
|
|
3827
|
-
});
|
|
3828
|
-
}
|
|
3829
|
-
return new Promise((resolve, reject) => {
|
|
3830
|
-
this.relayEventManager.callbacks.set(id, (response) => {
|
|
3831
|
-
hideSnackbarItem === null || hideSnackbarItem === void 0 ? void 0 : hideSnackbarItem();
|
|
3832
|
-
if (isErrorResponse(response) && response.errorCode) {
|
|
3833
|
-
return reject(standardErrors.provider.custom({
|
|
3834
|
-
code: response.errorCode,
|
|
3835
|
-
message: `Unrecognized chain ID. Try adding the chain using addEthereumChain first.`,
|
|
3836
|
-
}));
|
|
3837
|
-
}
|
|
3838
|
-
else if (isErrorResponse(response)) {
|
|
3839
|
-
return reject(new Error(response.errorMessage));
|
|
3840
|
-
}
|
|
3841
|
-
resolve(response);
|
|
3842
|
-
});
|
|
3843
|
-
this.publishWeb3RequestEvent(id, request);
|
|
3844
|
-
});
|
|
3845
|
-
}
|
|
3846
|
-
}
|
|
3847
|
-
WalletLinkRelay.accountRequestCallbackIds = new Set();
|
|
3848
|
-
|
|
3849
|
-
// Copyright (c) 2018-2024 Coinbase, Inc. <https://www.coinbase.com/>
|
|
3850
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
3851
|
-
// @ts-nocheck
|
|
3852
|
-
const DEFAULT_CHAIN_ID_KEY = 'DefaultChainId';
|
|
3853
|
-
const DEFAULT_JSON_RPC_URL = 'DefaultJsonRpcUrl';
|
|
3854
|
-
// original source: https://github.com/coinbase/coinbase-wallet-sdk/blob/v3.7.1/packages/wallet-sdk/src/provider/CoinbaseWalletProvider.ts
|
|
3855
|
-
class WalletLinkSigner {
|
|
3856
|
-
constructor(options) {
|
|
3857
|
-
this._relay = null;
|
|
3858
|
-
this._addresses = [];
|
|
3859
|
-
this.metadata = options.metadata;
|
|
3860
|
-
this._storage = new ScopedLocalStorage('walletlink', WALLETLINK_URL);
|
|
3861
|
-
this.callback = options.callback || null;
|
|
3862
|
-
const cachedAddresses = this._storage.getItem(LOCAL_STORAGE_ADDRESSES_KEY);
|
|
3863
|
-
if (cachedAddresses) {
|
|
3864
|
-
const addresses = cachedAddresses.split(' ');
|
|
3865
|
-
if (addresses[0] !== '') {
|
|
3866
|
-
this._addresses = addresses.map((address) => ensureAddressString(address));
|
|
3867
|
-
}
|
|
3868
|
-
}
|
|
3869
|
-
this.initializeRelay();
|
|
3870
|
-
}
|
|
3871
|
-
getSession() {
|
|
3872
|
-
const relay = this.initializeRelay();
|
|
3873
|
-
const { id, secret } = relay.getWalletLinkSession();
|
|
3874
|
-
return { id, secret };
|
|
3875
|
-
}
|
|
3876
|
-
async handshake() {
|
|
3877
|
-
await this._eth_requestAccounts();
|
|
3878
|
-
}
|
|
3879
|
-
get selectedAddress() {
|
|
3880
|
-
return this._addresses[0] || undefined;
|
|
3881
|
-
}
|
|
3882
|
-
get jsonRpcUrl() {
|
|
3883
|
-
var _a;
|
|
3884
|
-
return (_a = this._storage.getItem(DEFAULT_JSON_RPC_URL)) !== null && _a !== void 0 ? _a : undefined;
|
|
3885
|
-
}
|
|
3886
|
-
set jsonRpcUrl(value) {
|
|
3887
|
-
this._storage.setItem(DEFAULT_JSON_RPC_URL, value);
|
|
3888
|
-
}
|
|
3889
|
-
updateProviderInfo(jsonRpcUrl, chainId) {
|
|
3890
|
-
var _a;
|
|
3891
|
-
this.jsonRpcUrl = jsonRpcUrl;
|
|
3892
|
-
// emit chainChanged event if necessary
|
|
3893
|
-
const originalChainId = this.getChainId();
|
|
3894
|
-
this._storage.setItem(DEFAULT_CHAIN_ID_KEY, chainId.toString(10));
|
|
3895
|
-
const chainChanged = ensureIntNumber(chainId) !== originalChainId;
|
|
3896
|
-
if (chainChanged) {
|
|
3897
|
-
(_a = this.callback) === null || _a === void 0 ? void 0 : _a.call(this, 'chainChanged', hexStringFromNumber(chainId));
|
|
3898
|
-
}
|
|
3899
|
-
}
|
|
3900
|
-
async watchAsset(params) {
|
|
3901
|
-
const request = (Array.isArray(params) ? params[0] : params);
|
|
3902
|
-
if (!request.type) {
|
|
3903
|
-
throw standardErrors.rpc.invalidParams('Type is required');
|
|
3904
|
-
}
|
|
3905
|
-
if ((request === null || request === void 0 ? void 0 : request.type) !== 'ERC20') {
|
|
3906
|
-
throw standardErrors.rpc.invalidParams(`Asset of type '${request.type}' is not supported`);
|
|
3907
|
-
}
|
|
3908
|
-
if (!(request === null || request === void 0 ? void 0 : request.options)) {
|
|
3909
|
-
throw standardErrors.rpc.invalidParams('Options are required');
|
|
3910
|
-
}
|
|
3911
|
-
if (!(request === null || request === void 0 ? void 0 : request.options.address)) {
|
|
3912
|
-
throw standardErrors.rpc.invalidParams('Address is required');
|
|
3913
|
-
}
|
|
3914
|
-
const chainId = this.getChainId();
|
|
3915
|
-
const { address, symbol, image, decimals } = request.options;
|
|
3916
|
-
const relay = this.initializeRelay();
|
|
3917
|
-
const result = await relay.watchAsset(request.type, address, symbol, decimals, image, chainId === null || chainId === void 0 ? void 0 : chainId.toString());
|
|
3918
|
-
if (isErrorResponse(result))
|
|
3919
|
-
return false;
|
|
3920
|
-
return !!result.result;
|
|
3921
|
-
}
|
|
3922
|
-
async addEthereumChain(params) {
|
|
3923
|
-
var _a, _b;
|
|
3924
|
-
const request = params[0];
|
|
3925
|
-
if (((_a = request.rpcUrls) === null || _a === void 0 ? void 0 : _a.length) === 0) {
|
|
3926
|
-
throw standardErrors.rpc.invalidParams('please pass in at least 1 rpcUrl');
|
|
3927
|
-
}
|
|
3928
|
-
if (!request.chainName || request.chainName.trim() === '') {
|
|
3929
|
-
throw standardErrors.rpc.invalidParams('chainName is a required field');
|
|
3930
|
-
}
|
|
3931
|
-
if (!request.nativeCurrency) {
|
|
3932
|
-
throw standardErrors.rpc.invalidParams('nativeCurrency is a required field');
|
|
3933
|
-
}
|
|
3934
|
-
const chainIdNumber = Number.parseInt(request.chainId, 16);
|
|
3935
|
-
if (chainIdNumber === this.getChainId()) {
|
|
3936
|
-
return false;
|
|
3937
|
-
}
|
|
3938
|
-
const relay = this.initializeRelay();
|
|
3939
|
-
const { rpcUrls = [], blockExplorerUrls = [], chainName, iconUrls = [], nativeCurrency, } = request;
|
|
3940
|
-
const res = await relay.addEthereumChain(chainIdNumber.toString(), rpcUrls, iconUrls, blockExplorerUrls, chainName, nativeCurrency);
|
|
3941
|
-
if (isErrorResponse(res))
|
|
3942
|
-
return false;
|
|
3943
|
-
if (((_b = res.result) === null || _b === void 0 ? void 0 : _b.isApproved) === true) {
|
|
3944
|
-
this.updateProviderInfo(rpcUrls[0], chainIdNumber);
|
|
3945
|
-
return null;
|
|
3946
|
-
}
|
|
3947
|
-
throw standardErrors.rpc.internal('unable to add ethereum chain');
|
|
3948
|
-
}
|
|
3949
|
-
async switchEthereumChain(params) {
|
|
3950
|
-
const request = params[0];
|
|
3951
|
-
const chainId = Number.parseInt(request.chainId, 16);
|
|
3952
|
-
const relay = this.initializeRelay();
|
|
3953
|
-
const res = await relay.switchEthereumChain(chainId.toString(10), this.selectedAddress || undefined);
|
|
3954
|
-
if (isErrorResponse(res))
|
|
3955
|
-
throw res;
|
|
3956
|
-
const switchResponse = res.result;
|
|
3957
|
-
if (switchResponse.isApproved && switchResponse.rpcUrl.length > 0) {
|
|
3958
|
-
this.updateProviderInfo(switchResponse.rpcUrl, chainId);
|
|
3959
|
-
}
|
|
3960
|
-
return null;
|
|
3961
|
-
}
|
|
3962
|
-
async cleanup() {
|
|
3963
|
-
this.callback = null;
|
|
3964
|
-
if (this._relay) {
|
|
3965
|
-
this._relay.resetAndReload();
|
|
3966
|
-
}
|
|
3967
|
-
this._storage.clear();
|
|
3968
|
-
}
|
|
3969
|
-
_setAddresses(addresses, _) {
|
|
3970
|
-
var _a;
|
|
3971
|
-
if (!Array.isArray(addresses)) {
|
|
3972
|
-
throw new Error('addresses is not an array');
|
|
3973
|
-
}
|
|
3974
|
-
const newAddresses = addresses.map((address) => ensureAddressString(address));
|
|
3975
|
-
if (JSON.stringify(newAddresses) === JSON.stringify(this._addresses)) {
|
|
3976
|
-
return;
|
|
3977
|
-
}
|
|
3978
|
-
this._addresses = newAddresses;
|
|
3979
|
-
(_a = this.callback) === null || _a === void 0 ? void 0 : _a.call(this, 'accountsChanged', newAddresses);
|
|
3980
|
-
this._storage.setItem(LOCAL_STORAGE_ADDRESSES_KEY, newAddresses.join(' '));
|
|
3981
|
-
}
|
|
3982
|
-
async request(request) {
|
|
3983
|
-
const params = request.params || [];
|
|
3984
|
-
switch (request.method) {
|
|
3985
|
-
case 'eth_accounts':
|
|
3986
|
-
return [...this._addresses];
|
|
3987
|
-
case 'eth_coinbase':
|
|
3988
|
-
return this.selectedAddress || null;
|
|
3989
|
-
case 'net_version':
|
|
3990
|
-
return this.getChainId().toString(10);
|
|
3991
|
-
case 'eth_chainId':
|
|
3992
|
-
return hexStringFromNumber(this.getChainId());
|
|
3993
|
-
case 'eth_requestAccounts':
|
|
3994
|
-
return this._eth_requestAccounts();
|
|
3995
|
-
case 'eth_ecRecover':
|
|
3996
|
-
case 'personal_ecRecover':
|
|
3997
|
-
return this.ecRecover(request);
|
|
3998
|
-
case 'personal_sign':
|
|
3999
|
-
return this.personalSign(request);
|
|
4000
|
-
case 'eth_signTransaction':
|
|
4001
|
-
return this._eth_signTransaction(params);
|
|
4002
|
-
case 'eth_sendRawTransaction':
|
|
4003
|
-
return this._eth_sendRawTransaction(params);
|
|
4004
|
-
case 'eth_sendTransaction':
|
|
4005
|
-
return this._eth_sendTransaction(params);
|
|
4006
|
-
case 'eth_signTypedData_v1':
|
|
4007
|
-
case 'eth_signTypedData_v3':
|
|
4008
|
-
case 'eth_signTypedData_v4':
|
|
4009
|
-
case 'eth_signTypedData':
|
|
4010
|
-
return this.signTypedData(request);
|
|
4011
|
-
case 'wallet_addEthereumChain':
|
|
4012
|
-
return this.addEthereumChain(params);
|
|
4013
|
-
case 'wallet_switchEthereumChain':
|
|
4014
|
-
return this.switchEthereumChain(params);
|
|
4015
|
-
case 'wallet_watchAsset':
|
|
4016
|
-
return this.watchAsset(params);
|
|
4017
|
-
default:
|
|
4018
|
-
if (!this.jsonRpcUrl)
|
|
4019
|
-
throw standardErrors.rpc.internal('No RPC URL set for chain');
|
|
4020
|
-
return fetchRPCRequest(request, this.jsonRpcUrl);
|
|
4021
|
-
}
|
|
4022
|
-
}
|
|
4023
|
-
_ensureKnownAddress(addressString) {
|
|
4024
|
-
const addressStr = ensureAddressString(addressString);
|
|
4025
|
-
const lowercaseAddresses = this._addresses.map((address) => ensureAddressString(address));
|
|
4026
|
-
if (!lowercaseAddresses.includes(addressStr)) {
|
|
4027
|
-
throw new Error('Unknown Ethereum address');
|
|
4028
|
-
}
|
|
4029
|
-
}
|
|
4030
|
-
_prepareTransactionParams(tx) {
|
|
4031
|
-
const fromAddress = tx.from ? ensureAddressString(tx.from) : this.selectedAddress;
|
|
4032
|
-
if (!fromAddress) {
|
|
4033
|
-
throw new Error('Ethereum address is unavailable');
|
|
4034
|
-
}
|
|
4035
|
-
this._ensureKnownAddress(fromAddress);
|
|
4036
|
-
const toAddress = tx.to ? ensureAddressString(tx.to) : null;
|
|
4037
|
-
const weiValue = tx.value != null ? ensureBigInt(tx.value) : BigInt(0);
|
|
4038
|
-
const data = tx.data ? ensureBuffer(tx.data) : Buffer.alloc(0);
|
|
4039
|
-
const nonce = tx.nonce != null ? ensureIntNumber(tx.nonce) : null;
|
|
4040
|
-
const gasPriceInWei = tx.gasPrice != null ? ensureBigInt(tx.gasPrice) : null;
|
|
4041
|
-
const maxFeePerGas = tx.maxFeePerGas != null ? ensureBigInt(tx.maxFeePerGas) : null;
|
|
4042
|
-
const maxPriorityFeePerGas = tx.maxPriorityFeePerGas != null ? ensureBigInt(tx.maxPriorityFeePerGas) : null;
|
|
4043
|
-
const gasLimit = tx.gas != null ? ensureBigInt(tx.gas) : null;
|
|
4044
|
-
const chainId = tx.chainId ? ensureIntNumber(tx.chainId) : this.getChainId();
|
|
4045
|
-
return {
|
|
4046
|
-
fromAddress,
|
|
4047
|
-
toAddress,
|
|
4048
|
-
weiValue,
|
|
4049
|
-
data,
|
|
4050
|
-
nonce,
|
|
4051
|
-
gasPriceInWei,
|
|
4052
|
-
maxFeePerGas,
|
|
4053
|
-
maxPriorityFeePerGas,
|
|
4054
|
-
gasLimit,
|
|
4055
|
-
chainId,
|
|
4056
|
-
};
|
|
4057
|
-
}
|
|
4058
|
-
async ecRecover(request) {
|
|
4059
|
-
const { method, params } = request;
|
|
4060
|
-
if (!Array.isArray(params))
|
|
4061
|
-
throw standardErrors.rpc.invalidParams();
|
|
4062
|
-
const relay = this.initializeRelay();
|
|
4063
|
-
const res = await relay.sendRequest({
|
|
4064
|
-
method: 'ethereumAddressFromSignedMessage',
|
|
4065
|
-
params: {
|
|
4066
|
-
message: encodeToHexString(params[0]),
|
|
4067
|
-
signature: encodeToHexString(params[1]),
|
|
4068
|
-
addPrefix: method === 'personal_ecRecover',
|
|
4069
|
-
},
|
|
4070
|
-
});
|
|
4071
|
-
if (isErrorResponse(res))
|
|
4072
|
-
throw res;
|
|
4073
|
-
return res.result;
|
|
4074
|
-
}
|
|
4075
|
-
getChainId() {
|
|
4076
|
-
var _a;
|
|
4077
|
-
return Number.parseInt((_a = this._storage.getItem(DEFAULT_CHAIN_ID_KEY)) !== null && _a !== void 0 ? _a : '1', 10);
|
|
4078
|
-
}
|
|
4079
|
-
async _eth_requestAccounts() {
|
|
4080
|
-
var _a, _b;
|
|
4081
|
-
if (this._addresses.length > 0) {
|
|
4082
|
-
(_a = this.callback) === null || _a === void 0 ? void 0 : _a.call(this, 'connect', { chainId: hexStringFromNumber(this.getChainId()) });
|
|
4083
|
-
return this._addresses;
|
|
4084
|
-
}
|
|
4085
|
-
const relay = this.initializeRelay();
|
|
4086
|
-
const res = await relay.requestEthereumAccounts();
|
|
4087
|
-
if (isErrorResponse(res))
|
|
4088
|
-
throw res;
|
|
4089
|
-
if (!res.result) {
|
|
4090
|
-
throw new Error('accounts received is empty');
|
|
4091
|
-
}
|
|
4092
|
-
this._setAddresses(res.result);
|
|
4093
|
-
(_b = this.callback) === null || _b === void 0 ? void 0 : _b.call(this, 'connect', { chainId: hexStringFromNumber(this.getChainId()) });
|
|
4094
|
-
return this._addresses;
|
|
4095
|
-
}
|
|
4096
|
-
async personalSign({ params }) {
|
|
4097
|
-
if (!Array.isArray(params))
|
|
4098
|
-
throw standardErrors.rpc.invalidParams();
|
|
4099
|
-
const address = params[1];
|
|
4100
|
-
const rawData = params[0];
|
|
4101
|
-
this._ensureKnownAddress(address);
|
|
4102
|
-
const relay = this.initializeRelay();
|
|
4103
|
-
const res = await relay.sendRequest({
|
|
4104
|
-
method: 'signEthereumMessage',
|
|
4105
|
-
params: {
|
|
4106
|
-
address: ensureAddressString(address),
|
|
4107
|
-
message: encodeToHexString(rawData),
|
|
4108
|
-
addPrefix: true,
|
|
4109
|
-
typedDataJson: null,
|
|
4110
|
-
},
|
|
4111
|
-
});
|
|
4112
|
-
if (isErrorResponse(res))
|
|
4113
|
-
throw res;
|
|
4114
|
-
return res.result;
|
|
4115
|
-
}
|
|
4116
|
-
async _eth_signTransaction(params) {
|
|
4117
|
-
const tx = this._prepareTransactionParams(params[0] || {});
|
|
4118
|
-
const relay = this.initializeRelay();
|
|
4119
|
-
const res = await relay.signEthereumTransaction(tx);
|
|
4120
|
-
if (isErrorResponse(res))
|
|
4121
|
-
throw res;
|
|
4122
|
-
return res.result;
|
|
4123
|
-
}
|
|
4124
|
-
async _eth_sendRawTransaction(params) {
|
|
4125
|
-
const signedTransaction = ensureBuffer(params[0]);
|
|
4126
|
-
const relay = this.initializeRelay();
|
|
4127
|
-
const res = await relay.submitEthereumTransaction(signedTransaction, this.getChainId());
|
|
4128
|
-
if (isErrorResponse(res))
|
|
4129
|
-
throw res;
|
|
4130
|
-
return res.result;
|
|
4131
|
-
}
|
|
4132
|
-
async _eth_sendTransaction(params) {
|
|
4133
|
-
const tx = this._prepareTransactionParams(params[0] || {});
|
|
4134
|
-
const relay = this.initializeRelay();
|
|
4135
|
-
const res = await relay.signAndSubmitEthereumTransaction(tx);
|
|
4136
|
-
if (isErrorResponse(res))
|
|
4137
|
-
throw res;
|
|
4138
|
-
return res.result;
|
|
4139
|
-
}
|
|
4140
|
-
async signTypedData(request) {
|
|
4141
|
-
const { method, params } = request;
|
|
4142
|
-
if (!Array.isArray(params))
|
|
4143
|
-
throw standardErrors.rpc.invalidParams();
|
|
4144
|
-
const encode = (input) => {
|
|
4145
|
-
const hashFuncMap = {
|
|
4146
|
-
eth_signTypedData_v1: eip712.hashForSignTypedDataLegacy,
|
|
4147
|
-
eth_signTypedData_v3: eip712.hashForSignTypedData_v3,
|
|
4148
|
-
eth_signTypedData_v4: eip712.hashForSignTypedData_v4,
|
|
4149
|
-
eth_signTypedData: eip712.hashForSignTypedData_v4,
|
|
4150
|
-
};
|
|
4151
|
-
return hexStringFromBuffer(hashFuncMap[method]({
|
|
4152
|
-
data: ensureParsedJSONObject(input),
|
|
4153
|
-
}), true);
|
|
4154
|
-
};
|
|
4155
|
-
const address = params[method === 'eth_signTypedData_v1' ? 1 : 0];
|
|
4156
|
-
const rawData = params[method === 'eth_signTypedData_v1' ? 0 : 1];
|
|
4157
|
-
this._ensureKnownAddress(address);
|
|
4158
|
-
const relay = this.initializeRelay();
|
|
4159
|
-
const res = await relay.sendRequest({
|
|
4160
|
-
method: 'signEthereumMessage',
|
|
4161
|
-
params: {
|
|
4162
|
-
address: ensureAddressString(address),
|
|
4163
|
-
message: encode(rawData),
|
|
4164
|
-
typedDataJson: JSON.stringify(rawData, null, 2),
|
|
4165
|
-
addPrefix: false,
|
|
4166
|
-
},
|
|
4167
|
-
});
|
|
4168
|
-
if (isErrorResponse(res))
|
|
4169
|
-
throw res;
|
|
4170
|
-
return res.result;
|
|
4171
|
-
}
|
|
4172
|
-
initializeRelay() {
|
|
4173
|
-
if (!this._relay) {
|
|
4174
|
-
this._relay = new WalletLinkRelay({
|
|
4175
|
-
linkAPIUrl: WALLETLINK_URL,
|
|
4176
|
-
storage: this._storage,
|
|
4177
|
-
metadata: this.metadata,
|
|
4178
|
-
accountsCallback: this._setAddresses.bind(this),
|
|
4179
|
-
chainCallback: this.updateProviderInfo.bind(this),
|
|
4180
|
-
});
|
|
4181
|
-
}
|
|
4182
|
-
return this._relay;
|
|
4183
|
-
}
|
|
4184
|
-
}
|
|
4185
|
-
|
|
4186
|
-
const SIGNER_TYPE_KEY = 'SignerType';
|
|
4187
|
-
const storage = new ScopedLocalStorage('CBWSDK', 'SignerConfigurator');
|
|
4188
|
-
function loadSignerType() {
|
|
4189
|
-
return storage.getItem(SIGNER_TYPE_KEY);
|
|
4190
|
-
}
|
|
4191
|
-
function storeSignerType(signerType) {
|
|
4192
|
-
storage.setItem(SIGNER_TYPE_KEY, signerType);
|
|
4193
|
-
}
|
|
4194
|
-
async function fetchSignerType(params) {
|
|
4195
|
-
const { communicator, metadata, handshakeRequest, callback } = params;
|
|
4196
|
-
listenForWalletLinkSessionRequest(communicator, metadata, callback).catch(() => { });
|
|
4197
|
-
const request = {
|
|
4198
|
-
id: crypto.randomUUID(),
|
|
4199
|
-
event: 'selectSignerType',
|
|
4200
|
-
data: Object.assign(Object.assign({}, params.preference), { handshakeRequest }),
|
|
4201
|
-
};
|
|
4202
|
-
const { data } = await communicator.postRequestAndWaitForResponse(request);
|
|
4203
|
-
return data;
|
|
4204
|
-
}
|
|
4205
|
-
function createSigner(params) {
|
|
4206
|
-
const { signerType, metadata, communicator, callback } = params;
|
|
4207
|
-
switch (signerType) {
|
|
4208
|
-
case 'scw': {
|
|
4209
|
-
return new SCWSigner({
|
|
4210
|
-
metadata,
|
|
4211
|
-
callback,
|
|
4212
|
-
communicator,
|
|
4213
|
-
});
|
|
4214
|
-
}
|
|
4215
|
-
case 'walletlink': {
|
|
4216
|
-
return new WalletLinkSigner({
|
|
4217
|
-
metadata,
|
|
4218
|
-
callback,
|
|
4219
|
-
});
|
|
4220
|
-
}
|
|
4221
|
-
}
|
|
4222
|
-
}
|
|
4223
|
-
async function listenForWalletLinkSessionRequest(communicator, metadata, callback) {
|
|
4224
|
-
await communicator.onMessage(({ event }) => event === 'WalletLinkSessionRequest');
|
|
4225
|
-
// temporary walletlink signer instance to handle WalletLinkSessionRequest
|
|
4226
|
-
// will revisit this when refactoring the walletlink signer
|
|
4227
|
-
const walletlink = new WalletLinkSigner({
|
|
4228
|
-
metadata,
|
|
4229
|
-
callback,
|
|
4230
|
-
});
|
|
4231
|
-
// send wallet link session to popup
|
|
4232
|
-
communicator.postMessage({
|
|
4233
|
-
event: 'WalletLinkUpdate',
|
|
4234
|
-
data: { session: walletlink.getSession() },
|
|
4235
|
-
});
|
|
4236
|
-
// wait for handshake to complete
|
|
4237
|
-
await walletlink.handshake();
|
|
4238
|
-
// send connected status to popup
|
|
4239
|
-
communicator.postMessage({
|
|
4240
|
-
event: 'WalletLinkUpdate',
|
|
4241
|
-
data: { connected: true },
|
|
4242
|
-
});
|
|
4243
|
-
}
|
|
4244
|
-
|
|
4245
|
-
const COOP_ERROR_MESSAGE = `Coinbase Wallet SDK requires the Cross-Origin-Opener-Policy header to not be set to 'same-origin'. This is to ensure that the SDK can communicate with the Coinbase Smart Wallet app.
|
|
4246
|
-
|
|
4247
|
-
Please see https://www.smartwallet.dev/guides/tips/popup-tips#cross-origin-opener-policy for more information.`;
|
|
4248
|
-
/**
|
|
4249
|
-
* Creates a checker for the Cross-Origin-Opener-Policy (COOP).
|
|
4250
|
-
*
|
|
4251
|
-
* @returns An object with methods to get and check the Cross-Origin-Opener-Policy.
|
|
4252
|
-
*
|
|
4253
|
-
* @method getCrossOriginOpenerPolicy
|
|
4254
|
-
* Retrieves current Cross-Origin-Opener-Policy.
|
|
4255
|
-
* @throws Will throw an error if the policy has not been checked yet.
|
|
4256
|
-
*
|
|
4257
|
-
* @method checkCrossOriginOpenerPolicy
|
|
4258
|
-
* Checks the Cross-Origin-Opener-Policy of the current environment.
|
|
4259
|
-
* If in a non-browser environment, sets the policy to 'non-browser-env'.
|
|
4260
|
-
* If in a browser environment, fetches the policy from the current origin.
|
|
4261
|
-
* Logs an error if the policy is 'same-origin'.
|
|
4262
|
-
*/
|
|
4263
|
-
const createCoopChecker = () => {
|
|
4264
|
-
let crossOriginOpenerPolicy;
|
|
4265
|
-
return {
|
|
4266
|
-
getCrossOriginOpenerPolicy: () => {
|
|
4267
|
-
if (crossOriginOpenerPolicy === undefined) {
|
|
4268
|
-
return 'undefined';
|
|
4269
|
-
}
|
|
4270
|
-
return crossOriginOpenerPolicy;
|
|
4271
|
-
},
|
|
4272
|
-
checkCrossOriginOpenerPolicy: async () => {
|
|
4273
|
-
if (typeof window === 'undefined') {
|
|
4274
|
-
// Non-browser environment
|
|
4275
|
-
crossOriginOpenerPolicy = 'non-browser-env';
|
|
4276
|
-
return;
|
|
4277
|
-
}
|
|
4278
|
-
try {
|
|
4279
|
-
const url = `${window.location.origin}${window.location.pathname}`;
|
|
4280
|
-
const response = await fetch(url, {
|
|
4281
|
-
method: 'HEAD',
|
|
4282
|
-
});
|
|
4283
|
-
if (!response.ok) {
|
|
4284
|
-
throw new Error(`HTTP error! status: ${response.status}`);
|
|
4285
|
-
}
|
|
4286
|
-
const result = response.headers.get('Cross-Origin-Opener-Policy');
|
|
4287
|
-
crossOriginOpenerPolicy = result !== null && result !== void 0 ? result : 'null';
|
|
4288
|
-
if (crossOriginOpenerPolicy === 'same-origin') {
|
|
4289
|
-
console.error(COOP_ERROR_MESSAGE);
|
|
4290
|
-
}
|
|
4291
|
-
}
|
|
4292
|
-
catch (error) {
|
|
4293
|
-
console.error('Error checking Cross-Origin-Opener-Policy:', error.message);
|
|
4294
|
-
crossOriginOpenerPolicy = 'error';
|
|
4295
|
-
}
|
|
4296
|
-
},
|
|
4297
|
-
};
|
|
4298
|
-
};
|
|
4299
|
-
const { checkCrossOriginOpenerPolicy, getCrossOriginOpenerPolicy } = createCoopChecker();
|
|
4300
|
-
|
|
4301
|
-
const POPUP_WIDTH = 420;
|
|
4302
|
-
const POPUP_HEIGHT = 540;
|
|
4303
|
-
// Window Management
|
|
4304
|
-
function openPopup(url) {
|
|
4305
|
-
const left = (window.innerWidth - POPUP_WIDTH) / 2 + window.screenX;
|
|
4306
|
-
const top = (window.innerHeight - POPUP_HEIGHT) / 2 + window.screenY;
|
|
4307
|
-
appendAppInfoQueryParams(url);
|
|
4308
|
-
const popup = window.open(url, 'Smart Wallet', `width=${POPUP_WIDTH}, height=${POPUP_HEIGHT}, left=${left}, top=${top}`);
|
|
4309
|
-
popup === null || popup === void 0 ? void 0 : popup.focus();
|
|
4310
|
-
if (!popup) {
|
|
4311
|
-
throw standardErrors.rpc.internal('Pop up window failed to open');
|
|
4312
|
-
}
|
|
4313
|
-
return popup;
|
|
4314
|
-
}
|
|
4315
|
-
function closePopup(popup) {
|
|
4316
|
-
if (popup && !popup.closed) {
|
|
4317
|
-
popup.close();
|
|
4318
|
-
}
|
|
4319
|
-
}
|
|
4320
|
-
function appendAppInfoQueryParams(url) {
|
|
4321
|
-
const params = {
|
|
4322
|
-
sdkName: NAME,
|
|
4323
|
-
sdkVersion: VERSION,
|
|
4324
|
-
origin: window.location.origin,
|
|
4325
|
-
coop: getCrossOriginOpenerPolicy(),
|
|
4326
|
-
};
|
|
4327
|
-
for (const [key, value] of Object.entries(params)) {
|
|
4328
|
-
url.searchParams.append(key, value.toString());
|
|
4329
|
-
}
|
|
4330
|
-
}
|
|
4331
|
-
|
|
4332
|
-
/**
|
|
4333
|
-
* Communicates with a popup window for Coinbase keys.coinbase.com (or another url)
|
|
4334
|
-
* to send and receive messages.
|
|
4335
|
-
*
|
|
4336
|
-
* This class is responsible for opening a popup window, posting messages to it,
|
|
4337
|
-
* and listening for responses.
|
|
4338
|
-
*
|
|
4339
|
-
* It also handles cleanup of event listeners and the popup window itself when necessary.
|
|
4340
|
-
*/
|
|
4341
|
-
class Communicator {
|
|
4342
|
-
constructor({ url = CB_KEYS_URL, metadata, preference }) {
|
|
4343
|
-
this.popup = null;
|
|
4344
|
-
this.listeners = new Map();
|
|
4345
|
-
/**
|
|
4346
|
-
* Posts a message to the popup window
|
|
4347
|
-
*/
|
|
4348
|
-
this.postMessage = async (message) => {
|
|
4349
|
-
const popup = await this.waitForPopupLoaded();
|
|
4350
|
-
popup.postMessage(message, this.url.origin);
|
|
4351
|
-
};
|
|
4352
|
-
/**
|
|
4353
|
-
* Posts a request to the popup window and waits for a response
|
|
4354
|
-
*/
|
|
4355
|
-
this.postRequestAndWaitForResponse = async (request) => {
|
|
4356
|
-
const responsePromise = this.onMessage(({ requestId }) => requestId === request.id);
|
|
4357
|
-
this.postMessage(request);
|
|
4358
|
-
return await responsePromise;
|
|
4359
|
-
};
|
|
4360
|
-
/**
|
|
4361
|
-
* Listens for messages from the popup window that match a given predicate.
|
|
4362
|
-
*/
|
|
4363
|
-
this.onMessage = async (predicate) => {
|
|
4364
|
-
return new Promise((resolve, reject) => {
|
|
4365
|
-
const listener = (event) => {
|
|
4366
|
-
if (event.origin !== this.url.origin)
|
|
4367
|
-
return; // origin validation
|
|
4368
|
-
const message = event.data;
|
|
4369
|
-
if (predicate(message)) {
|
|
4370
|
-
resolve(message);
|
|
4371
|
-
window.removeEventListener('message', listener);
|
|
4372
|
-
this.listeners.delete(listener);
|
|
4373
|
-
}
|
|
4374
|
-
};
|
|
4375
|
-
window.addEventListener('message', listener);
|
|
4376
|
-
this.listeners.set(listener, { reject });
|
|
4377
|
-
});
|
|
4378
|
-
};
|
|
4379
|
-
/**
|
|
4380
|
-
* Closes the popup, rejects all requests and clears the listeners
|
|
4381
|
-
*/
|
|
4382
|
-
this.disconnect = () => {
|
|
4383
|
-
// Note: keys popup handles closing itself. this is a fallback.
|
|
4384
|
-
closePopup(this.popup);
|
|
4385
|
-
this.popup = null;
|
|
4386
|
-
this.listeners.forEach(({ reject }, listener) => {
|
|
4387
|
-
reject(standardErrors.provider.userRejectedRequest('Request rejected'));
|
|
4388
|
-
window.removeEventListener('message', listener);
|
|
4389
|
-
});
|
|
4390
|
-
this.listeners.clear();
|
|
4391
|
-
};
|
|
4392
|
-
/**
|
|
4393
|
-
* Waits for the popup window to fully load and then sends a version message.
|
|
4394
|
-
*/
|
|
4395
|
-
this.waitForPopupLoaded = async () => {
|
|
4396
|
-
if (this.popup && !this.popup.closed) {
|
|
4397
|
-
// In case the user un-focused the popup between requests, focus it again
|
|
4398
|
-
this.popup.focus();
|
|
4399
|
-
return this.popup;
|
|
4400
|
-
}
|
|
4401
|
-
this.popup = openPopup(this.url);
|
|
4402
|
-
this.onMessage(({ event }) => event === 'PopupUnload')
|
|
4403
|
-
.then(this.disconnect)
|
|
4404
|
-
.catch(() => { });
|
|
4405
|
-
return this.onMessage(({ event }) => event === 'PopupLoaded')
|
|
4406
|
-
.then((message) => {
|
|
4407
|
-
this.postMessage({
|
|
4408
|
-
requestId: message.id,
|
|
4409
|
-
data: {
|
|
4410
|
-
version: VERSION,
|
|
4411
|
-
metadata: this.metadata,
|
|
4412
|
-
preference: this.preference,
|
|
4413
|
-
location: window.location.toString(),
|
|
4414
|
-
},
|
|
4415
|
-
});
|
|
4416
|
-
})
|
|
4417
|
-
.then(() => {
|
|
4418
|
-
if (!this.popup)
|
|
4419
|
-
throw standardErrors.rpc.internal();
|
|
4420
|
-
return this.popup;
|
|
4421
|
-
});
|
|
4422
|
-
};
|
|
4423
|
-
this.url = new URL(url);
|
|
4424
|
-
this.metadata = metadata;
|
|
4425
|
-
this.preference = preference;
|
|
4426
|
-
}
|
|
4427
|
-
}
|
|
4428
|
-
|
|
4429
|
-
// TODO: error should not depend on walletlink. revisit this.
|
|
4430
|
-
/**
|
|
4431
|
-
* Serializes an error to a format that is compatible with the Ethereum JSON RPC error format.
|
|
4432
|
-
* See https://docs.cloud.coinbase.com/wallet-sdk/docs/errors
|
|
4433
|
-
* for more information.
|
|
4434
|
-
*/
|
|
4435
|
-
function serializeError(error) {
|
|
4436
|
-
const serialized = serialize(getErrorObject(error), {
|
|
4437
|
-
shouldIncludeStack: true,
|
|
4438
|
-
});
|
|
4439
|
-
const docUrl = new URL('https://docs.cloud.coinbase.com/wallet-sdk/docs/errors');
|
|
4440
|
-
docUrl.searchParams.set('version', VERSION);
|
|
4441
|
-
docUrl.searchParams.set('code', serialized.code.toString());
|
|
4442
|
-
docUrl.searchParams.set('message', serialized.message);
|
|
4443
|
-
return Object.assign(Object.assign({}, serialized), { docUrl: docUrl.href });
|
|
4444
|
-
}
|
|
4445
|
-
/**
|
|
4446
|
-
* Converts an error to a serializable object.
|
|
4447
|
-
*/
|
|
4448
|
-
function getErrorObject(error) {
|
|
4449
|
-
var _a;
|
|
4450
|
-
if (typeof error === 'string') {
|
|
4451
|
-
return {
|
|
4452
|
-
message: error,
|
|
4453
|
-
code: standardErrorCodes.rpc.internal,
|
|
4454
|
-
};
|
|
4455
|
-
}
|
|
4456
|
-
else if (isErrorResponse(error)) {
|
|
4457
|
-
const message = error.errorMessage;
|
|
4458
|
-
const code = (_a = error.errorCode) !== null && _a !== void 0 ? _a : (message.match(/(denied|rejected)/i)
|
|
4459
|
-
? standardErrorCodes.provider.userRejectedRequest
|
|
4460
|
-
: undefined);
|
|
4461
|
-
return Object.assign(Object.assign({}, error), { message,
|
|
4462
|
-
code, data: { method: error.method } });
|
|
4463
|
-
}
|
|
4464
|
-
return error;
|
|
4465
|
-
}
|
|
4466
|
-
|
|
4467
|
-
class ProviderEventEmitter extends EventEmitter {
|
|
4468
|
-
}
|
|
4469
|
-
|
|
4470
|
-
var __rest = (undefined && undefined.__rest) || function (s, e) {
|
|
4471
|
-
var t = {};
|
|
4472
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
4473
|
-
t[p] = s[p];
|
|
4474
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
4475
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
4476
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
4477
|
-
t[p[i]] = s[p[i]];
|
|
4478
|
-
}
|
|
4479
|
-
return t;
|
|
4480
|
-
};
|
|
4481
|
-
class CoinbaseWalletProvider extends ProviderEventEmitter {
|
|
4482
|
-
constructor(_a) {
|
|
4483
|
-
var { metadata } = _a, _b = _a.preference, { keysUrl } = _b, preference = __rest(_b, ["keysUrl"]);
|
|
4484
|
-
super();
|
|
4485
|
-
this.signer = null;
|
|
4486
|
-
this.isCoinbaseWallet = true;
|
|
4487
|
-
this.metadata = metadata;
|
|
4488
|
-
this.preference = preference;
|
|
4489
|
-
this.communicator = new Communicator({
|
|
4490
|
-
url: keysUrl,
|
|
4491
|
-
metadata,
|
|
4492
|
-
preference,
|
|
4493
|
-
});
|
|
4494
|
-
const signerType = loadSignerType();
|
|
4495
|
-
if (signerType) {
|
|
4496
|
-
this.signer = this.initSigner(signerType);
|
|
4497
|
-
}
|
|
4498
|
-
}
|
|
4499
|
-
async request(args) {
|
|
4500
|
-
try {
|
|
4501
|
-
checkErrorForInvalidRequestArgs(args);
|
|
4502
|
-
if (!this.signer) {
|
|
4503
|
-
switch (args.method) {
|
|
4504
|
-
case 'eth_requestAccounts': {
|
|
4505
|
-
const signerType = await this.requestSignerSelection(args);
|
|
4506
|
-
const signer = this.initSigner(signerType);
|
|
4507
|
-
await signer.handshake(args);
|
|
4508
|
-
this.signer = signer;
|
|
4509
|
-
storeSignerType(signerType);
|
|
4510
|
-
break;
|
|
4511
|
-
}
|
|
4512
|
-
case 'net_version':
|
|
4513
|
-
return 1; // default value
|
|
4514
|
-
case 'eth_chainId':
|
|
4515
|
-
return hexStringFromNumber(1); // default value
|
|
4516
|
-
default: {
|
|
4517
|
-
throw standardErrors.provider.unauthorized("Must call 'eth_requestAccounts' before other methods");
|
|
4518
|
-
}
|
|
4519
|
-
}
|
|
4520
|
-
}
|
|
4521
|
-
return this.signer.request(args);
|
|
4522
|
-
}
|
|
4523
|
-
catch (error) {
|
|
4524
|
-
const { code } = error;
|
|
4525
|
-
if (code === standardErrorCodes.provider.unauthorized)
|
|
4526
|
-
this.disconnect();
|
|
4527
|
-
return Promise.reject(serializeError(error));
|
|
4528
|
-
}
|
|
4529
|
-
}
|
|
4530
|
-
/** @deprecated Use `.request({ method: 'eth_requestAccounts' })` instead. */
|
|
4531
|
-
async enable() {
|
|
4532
|
-
console.warn(`.enable() has been deprecated. Please use .request({ method: "eth_requestAccounts" }) instead.`);
|
|
4533
|
-
return await this.request({
|
|
4534
|
-
method: 'eth_requestAccounts',
|
|
4535
|
-
});
|
|
4536
|
-
}
|
|
4537
|
-
async disconnect() {
|
|
4538
|
-
var _a;
|
|
4539
|
-
await ((_a = this.signer) === null || _a === void 0 ? void 0 : _a.cleanup());
|
|
4540
|
-
this.signer = null;
|
|
4541
|
-
ScopedLocalStorage.clearAll();
|
|
4542
|
-
this.emit('disconnect', standardErrors.provider.disconnected('User initiated disconnection'));
|
|
4543
|
-
}
|
|
4544
|
-
requestSignerSelection(handshakeRequest) {
|
|
4545
|
-
return fetchSignerType({
|
|
4546
|
-
communicator: this.communicator,
|
|
4547
|
-
preference: this.preference,
|
|
4548
|
-
metadata: this.metadata,
|
|
4549
|
-
handshakeRequest,
|
|
4550
|
-
callback: this.emit.bind(this),
|
|
4551
|
-
});
|
|
4552
|
-
}
|
|
4553
|
-
initSigner(signerType) {
|
|
4554
|
-
return createSigner({
|
|
4555
|
-
signerType,
|
|
4556
|
-
metadata: this.metadata,
|
|
4557
|
-
communicator: this.communicator,
|
|
4558
|
-
callback: this.emit.bind(this),
|
|
4559
|
-
});
|
|
4560
|
-
}
|
|
4561
|
-
}
|
|
4562
|
-
|
|
4563
|
-
/**
|
|
4564
|
-
* Validates user supplied preferences. Throws if keys are not valid.
|
|
4565
|
-
* @param preference
|
|
4566
|
-
*/
|
|
4567
|
-
function validatePreferences(preference) {
|
|
4568
|
-
if (!preference) {
|
|
4569
|
-
return;
|
|
4570
|
-
}
|
|
4571
|
-
if (!['all', 'smartWalletOnly', 'eoaOnly'].includes(preference.options)) {
|
|
4572
|
-
throw new Error(`Invalid options: ${preference.options}`);
|
|
4573
|
-
}
|
|
4574
|
-
if (preference.attribution) {
|
|
4575
|
-
if (preference.attribution.auto !== undefined &&
|
|
4576
|
-
preference.attribution.dataSuffix !== undefined) {
|
|
4577
|
-
throw new Error(`Attribution cannot contain both auto and dataSuffix properties`);
|
|
4578
|
-
}
|
|
4579
|
-
}
|
|
4580
|
-
}
|
|
4581
|
-
|
|
4582
|
-
function createCoinbaseWalletProvider(options) {
|
|
4583
|
-
var _a;
|
|
4584
|
-
const params = {
|
|
4585
|
-
metadata: options.metadata,
|
|
4586
|
-
preference: options.preference,
|
|
4587
|
-
};
|
|
4588
|
-
return (_a = getCoinbaseInjectedProvider(params)) !== null && _a !== void 0 ? _a : new CoinbaseWalletProvider(params);
|
|
4589
|
-
}
|
|
4590
|
-
|
|
4591
|
-
const DEFAULT_PREFERENCE = {
|
|
4592
|
-
options: 'all',
|
|
4593
|
-
};
|
|
4594
|
-
/**
|
|
4595
|
-
* Create a Coinbase Wallet SDK instance.
|
|
4596
|
-
* @param params - Options to create a Coinbase Wallet SDK instance.
|
|
4597
|
-
* @returns A Coinbase Wallet SDK object.
|
|
4598
|
-
*/
|
|
4599
|
-
function createCoinbaseWalletSDK(params) {
|
|
4600
|
-
var _a;
|
|
4601
|
-
const versionStorage = new ScopedLocalStorage('CBWSDK');
|
|
4602
|
-
versionStorage.setItem('VERSION', VERSION);
|
|
4603
|
-
void checkCrossOriginOpenerPolicy();
|
|
4604
|
-
const options = {
|
|
4605
|
-
metadata: {
|
|
4606
|
-
appName: params.appName || 'Dapp',
|
|
4607
|
-
appLogoUrl: params.appLogoUrl || '',
|
|
4608
|
-
appChainIds: params.appChainIds || [],
|
|
4609
|
-
},
|
|
4610
|
-
preference: Object.assign(DEFAULT_PREFERENCE, (_a = params.preference) !== null && _a !== void 0 ? _a : {}),
|
|
4611
|
-
};
|
|
4612
|
-
/**
|
|
4613
|
-
* Validate user supplied preferences. Throws if key/values are not valid.
|
|
4614
|
-
*/
|
|
4615
|
-
validatePreferences(options.preference);
|
|
4616
|
-
let provider = null;
|
|
4617
|
-
return {
|
|
4618
|
-
getProvider: () => {
|
|
4619
|
-
if (!provider) {
|
|
4620
|
-
provider = createCoinbaseWalletProvider(options);
|
|
4621
|
-
}
|
|
4622
|
-
return provider;
|
|
4623
|
-
},
|
|
4624
|
-
};
|
|
4625
|
-
}
|
|
4626
|
-
|
|
4627
|
-
export { createCoinbaseWalletSDK };
|
|
4628
|
-
//# sourceMappingURL=index-Dehij_6h.js.map
|