@pooflabs/web 0.0.70 → 0.0.72

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.
@@ -0,0 +1,1167 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * TODO: docs
5
+ */
6
+ function createSignInMessageText(input) {
7
+ // ${domain} wants you to sign in with your Solana account:
8
+ // ${address}
9
+ //
10
+ // ${statement}
11
+ //
12
+ // URI: ${uri}
13
+ // Version: ${version}
14
+ // Chain ID: ${chain}
15
+ // Nonce: ${nonce}
16
+ // Issued At: ${issued-at}
17
+ // Expiration Time: ${expiration-time}
18
+ // Not Before: ${not-before}
19
+ // Request ID: ${request-id}
20
+ // Resources:
21
+ // - ${resources[0]}
22
+ // - ${resources[1]}
23
+ // ...
24
+ // - ${resources[n]}
25
+ let message = `${input.domain} wants you to sign in with your Solana account:\n`;
26
+ message += `${input.address}`;
27
+ if (input.statement) {
28
+ message += `\n\n${input.statement}`;
29
+ }
30
+ const fields = [];
31
+ if (input.uri) {
32
+ fields.push(`URI: ${input.uri}`);
33
+ }
34
+ if (input.version) {
35
+ fields.push(`Version: ${input.version}`);
36
+ }
37
+ if (input.chainId) {
38
+ fields.push(`Chain ID: ${input.chainId}`);
39
+ }
40
+ if (input.nonce) {
41
+ fields.push(`Nonce: ${input.nonce}`);
42
+ }
43
+ if (input.issuedAt) {
44
+ fields.push(`Issued At: ${input.issuedAt}`);
45
+ }
46
+ if (input.expirationTime) {
47
+ fields.push(`Expiration Time: ${input.expirationTime}`);
48
+ }
49
+ if (input.notBefore) {
50
+ fields.push(`Not Before: ${input.notBefore}`);
51
+ }
52
+ if (input.requestId) {
53
+ fields.push(`Request ID: ${input.requestId}`);
54
+ }
55
+ if (input.resources) {
56
+ fields.push(`Resources:`);
57
+ for (const resource of input.resources) {
58
+ fields.push(`- ${resource}`);
59
+ }
60
+ }
61
+ if (fields.length) {
62
+ message += `\n\n${fields.join('\n')}`;
63
+ }
64
+ return message;
65
+ }
66
+
67
+ function createDecoder(decoder) {
68
+ return Object.freeze({
69
+ ...decoder,
70
+ decode: (bytes, offset = 0) => decoder.read(bytes, offset)[0]
71
+ });
72
+ }
73
+
74
+ var getBaseXDecoder = (alphabet4) => {
75
+ return createDecoder({
76
+ read(rawBytes, offset) {
77
+ const bytes = offset === 0 ? rawBytes : rawBytes.slice(offset);
78
+ if (bytes.length === 0) return ["", 0];
79
+ let trailIndex = bytes.findIndex((n) => n !== 0);
80
+ trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
81
+ const leadingZeroes = alphabet4[0].repeat(trailIndex);
82
+ if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];
83
+ const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
84
+ const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
85
+ return [leadingZeroes + tailChars, rawBytes.length];
86
+ }
87
+ });
88
+ };
89
+ function getBaseXFromBigInt(value, alphabet4) {
90
+ const base = BigInt(alphabet4.length);
91
+ const tailChars = [];
92
+ while (value > 0n) {
93
+ tailChars.unshift(alphabet4[Number(value % base)]);
94
+ value /= base;
95
+ }
96
+ return tailChars.join("");
97
+ }
98
+
99
+ // src/base58.ts
100
+ var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
101
+ var getBase58Decoder = () => getBaseXDecoder(alphabet2);
102
+
103
+ // Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
104
+ const SolanaMobileWalletAdapterErrorCode = {
105
+ ERROR_ASSOCIATION_PORT_OUT_OF_RANGE: 'ERROR_ASSOCIATION_PORT_OUT_OF_RANGE',
106
+ ERROR_FORBIDDEN_WALLET_BASE_URL: 'ERROR_FORBIDDEN_WALLET_BASE_URL',
107
+ ERROR_SECURE_CONTEXT_REQUIRED: 'ERROR_SECURE_CONTEXT_REQUIRED',
108
+ ERROR_SESSION_CLOSED: 'ERROR_SESSION_CLOSED',
109
+ ERROR_SESSION_TIMEOUT: 'ERROR_SESSION_TIMEOUT',
110
+ ERROR_WALLET_NOT_FOUND: 'ERROR_WALLET_NOT_FOUND',
111
+ ERROR_INVALID_PROTOCOL_VERSION: 'ERROR_INVALID_PROTOCOL_VERSION'};
112
+ class SolanaMobileWalletAdapterError extends Error {
113
+ constructor(...args) {
114
+ const [code, message, data] = args;
115
+ super(message);
116
+ this.code = code;
117
+ this.data = data;
118
+ this.name = 'SolanaMobileWalletAdapterError';
119
+ }
120
+ }
121
+ class SolanaMobileWalletAdapterProtocolError extends Error {
122
+ constructor(...args) {
123
+ const [jsonRpcMessageId, code, message, data] = args;
124
+ super(message);
125
+ this.code = code;
126
+ this.data = data;
127
+ this.jsonRpcMessageId = jsonRpcMessageId;
128
+ this.name = 'SolanaMobileWalletAdapterProtocolError';
129
+ }
130
+ }
131
+
132
+ /******************************************************************************
133
+ Copyright (c) Microsoft Corporation.
134
+
135
+ Permission to use, copy, modify, and/or distribute this software for any
136
+ purpose with or without fee is hereby granted.
137
+
138
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
139
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
140
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
141
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
142
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
143
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
144
+ PERFORMANCE OF THIS SOFTWARE.
145
+ ***************************************************************************** */
146
+
147
+ function __awaiter(thisArg, _arguments, P, generator) {
148
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
149
+ return new (P || (P = Promise))(function (resolve, reject) {
150
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
151
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
152
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
153
+ step((generator = generator.apply(thisArg, [])).next());
154
+ });
155
+ }
156
+
157
+ function encode(input) {
158
+ return window.btoa(input);
159
+ }
160
+ function fromUint8Array$1(byteArray, urlsafe) {
161
+ const base64 = window.btoa(String.fromCharCode.call(null, ...byteArray));
162
+ if (urlsafe) {
163
+ return base64
164
+ .replace(/\+/g, '-')
165
+ .replace(/\//g, '_')
166
+ .replace(/=+$/, '');
167
+ }
168
+ else
169
+ return base64;
170
+ }
171
+ function toUint8Array(base64EncodedByteArray) {
172
+ return new Uint8Array(window
173
+ .atob(base64EncodedByteArray)
174
+ .split('')
175
+ .map((c) => c.charCodeAt(0)));
176
+ }
177
+
178
+ function createHelloReq(ecdhPublicKey, associationKeypairPrivateKey) {
179
+ return __awaiter(this, void 0, void 0, function* () {
180
+ const publicKeyBuffer = yield crypto.subtle.exportKey('raw', ecdhPublicKey);
181
+ const signatureBuffer = yield crypto.subtle.sign({ hash: 'SHA-256', name: 'ECDSA' }, associationKeypairPrivateKey, publicKeyBuffer);
182
+ const response = new Uint8Array(publicKeyBuffer.byteLength + signatureBuffer.byteLength);
183
+ response.set(new Uint8Array(publicKeyBuffer), 0);
184
+ response.set(new Uint8Array(signatureBuffer), publicKeyBuffer.byteLength);
185
+ return response;
186
+ });
187
+ }
188
+
189
+ function createSIWSMessage(payload) {
190
+ return createSignInMessageText(payload);
191
+ }
192
+ function createSIWSMessageBase64Url(payload) {
193
+ return encode(createSIWSMessage(payload))
194
+ .replace(/\+/g, '-')
195
+ .replace(/\//g, '_')
196
+ .replace(/=+$/, ''); // convert to base64url encoding;
197
+ }
198
+
199
+ // optional features
200
+ const SolanaSignTransactions = 'solana:signTransactions';
201
+ const SolanaCloneAuthorization = 'solana:cloneAuthorization';
202
+
203
+ function fromUint8Array(byteArray) {
204
+ return getBase58Decoder().decode(byteArray);
205
+ }
206
+ function base64ToBase58(base64EncodedString) {
207
+ return fromUint8Array(toUint8Array(base64EncodedString));
208
+ }
209
+
210
+ /**
211
+ * Creates a {@link MobileWallet} proxy that handles backwards compatibility and API to RPC conversion.
212
+ *
213
+ * @param protocolVersion the protocol version in use for this session/request
214
+ * @param protocolRequestHandler callback function that handles sending the RPC request to the wallet endpoint.
215
+ * @returns a {@link MobileWallet} proxy
216
+ */
217
+ function createMobileWalletProxy(protocolVersion, protocolRequestHandler) {
218
+ return new Proxy({}, {
219
+ get(target, p) {
220
+ // Wrapping a Proxy in a promise results in the Proxy being asked for a 'then' property so must
221
+ // return null if 'then' is called on this proxy to let the 'resolve()' call know this is not a promise.
222
+ // see: https://stackoverflow.com/a/53890904
223
+ //@ts-ignore
224
+ if (p === 'then') {
225
+ return null;
226
+ }
227
+ if (target[p] == null) {
228
+ target[p] = function (inputParams) {
229
+ return __awaiter(this, void 0, void 0, function* () {
230
+ const { method, params } = handleMobileWalletRequest(p, inputParams, protocolVersion);
231
+ const result = yield protocolRequestHandler(method, params);
232
+ // if the request tried to sign in but the wallet did not return a sign in result, fallback on message signing
233
+ if (method === 'authorize' && params.sign_in_payload && !result.sign_in_result) {
234
+ result['sign_in_result'] = yield signInFallback(params.sign_in_payload, result, protocolRequestHandler);
235
+ }
236
+ return handleMobileWalletResponse(p, result, protocolVersion);
237
+ });
238
+ };
239
+ }
240
+ return target[p];
241
+ },
242
+ defineProperty() {
243
+ return false;
244
+ },
245
+ deleteProperty() {
246
+ return false;
247
+ },
248
+ });
249
+ }
250
+ /**
251
+ * Handles all {@link MobileWallet} API requests and determines the correct MWA RPC method and params to call.
252
+ * This handles backwards compatibility, based on the provided @protocolVersion.
253
+ *
254
+ * @param methodName the name of {@link MobileWallet} method that was called
255
+ * @param methodParams the parameters that were passed to the method
256
+ * @param protocolVersion the protocol version in use for this session/request
257
+ * @returns the RPC request method and params that should be sent to the wallet endpoint
258
+ */
259
+ function handleMobileWalletRequest(methodName, methodParams, protocolVersion) {
260
+ let params = methodParams;
261
+ let method = methodName
262
+ .toString()
263
+ .replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)
264
+ .toLowerCase();
265
+ switch (methodName) {
266
+ case 'authorize': {
267
+ let { chain } = params;
268
+ if (protocolVersion === 'legacy') {
269
+ switch (chain) {
270
+ case 'solana:testnet': {
271
+ chain = 'testnet';
272
+ break;
273
+ }
274
+ case 'solana:devnet': {
275
+ chain = 'devnet';
276
+ break;
277
+ }
278
+ case 'solana:mainnet': {
279
+ chain = 'mainnet-beta';
280
+ break;
281
+ }
282
+ default: {
283
+ chain = params.cluster;
284
+ }
285
+ }
286
+ params.cluster = chain;
287
+ }
288
+ else {
289
+ switch (chain) {
290
+ case 'testnet':
291
+ case 'devnet': {
292
+ chain = `solana:${chain}`;
293
+ break;
294
+ }
295
+ case 'mainnet-beta': {
296
+ chain = 'solana:mainnet';
297
+ break;
298
+ }
299
+ }
300
+ params.chain = chain;
301
+ }
302
+ }
303
+ case 'reauthorize': {
304
+ const { auth_token, identity } = params;
305
+ if (auth_token) {
306
+ switch (protocolVersion) {
307
+ case 'legacy': {
308
+ method = 'reauthorize';
309
+ params = { auth_token: auth_token, identity: identity };
310
+ break;
311
+ }
312
+ default: {
313
+ method = 'authorize';
314
+ break;
315
+ }
316
+ }
317
+ }
318
+ break;
319
+ }
320
+ }
321
+ return { method, params };
322
+ }
323
+ /**
324
+ * Handles all {@link MobileWallet} API responses and modifies the response for backwards compatibility, if needed
325
+ *
326
+ * @param method the {@link MobileWallet} method that was called
327
+ * @param response the original response that was returned by the method call
328
+ * @param protocolVersion the protocol version in use for this session/request
329
+ * @returns the possibly modified response
330
+ */
331
+ function handleMobileWalletResponse(method, response, protocolVersion) {
332
+ switch (method) {
333
+ case 'getCapabilities': {
334
+ const capabilities = response;
335
+ switch (protocolVersion) {
336
+ case 'legacy': {
337
+ const features = [SolanaSignTransactions];
338
+ if (capabilities.supports_clone_authorization === true) {
339
+ features.push(SolanaCloneAuthorization);
340
+ }
341
+ return Object.assign(Object.assign({}, capabilities), { features: features });
342
+ }
343
+ case 'v1': {
344
+ return Object.assign(Object.assign({}, capabilities), { supports_sign_and_send_transactions: true, supports_clone_authorization: capabilities.features.includes(SolanaCloneAuthorization) });
345
+ }
346
+ }
347
+ }
348
+ }
349
+ return response;
350
+ }
351
+ function signInFallback(signInPayload, authorizationResult, protocolRequestHandler) {
352
+ var _a;
353
+ return __awaiter(this, void 0, void 0, function* () {
354
+ const domain = (_a = signInPayload.domain) !== null && _a !== void 0 ? _a : window.location.host;
355
+ const address = authorizationResult.accounts[0].address;
356
+ const siwsMessage = createSIWSMessageBase64Url(Object.assign(Object.assign({}, signInPayload), { domain, address: base64ToBase58(address) }));
357
+ const signMessageResult = yield protocolRequestHandler('sign_messages', {
358
+ addresses: [address],
359
+ payloads: [siwsMessage]
360
+ });
361
+ const signedPayload = toUint8Array(signMessageResult.signed_payloads[0]);
362
+ const signedMessage = fromUint8Array$1(signedPayload.slice(0, signedPayload.length - 64));
363
+ const signature = fromUint8Array$1(signedPayload.slice(signedPayload.length - 64));
364
+ const signInResult = {
365
+ address: address,
366
+ // Workaround: some wallets have been observed to only reply with the message signature.
367
+ // This is non-compliant with the spec, but in the interest of maximizing compatibility,
368
+ // detect this case and reuse the original message.
369
+ signed_message: signedMessage.length == 0 ? siwsMessage : signedMessage,
370
+ signature
371
+ };
372
+ return signInResult;
373
+ });
374
+ }
375
+
376
+ const SEQUENCE_NUMBER_BYTES = 4;
377
+ function createSequenceNumberVector(sequenceNumber) {
378
+ if (sequenceNumber >= 4294967296) {
379
+ throw new Error('Outbound sequence number overflow. The maximum sequence number is 32-bytes.');
380
+ }
381
+ const byteArray = new ArrayBuffer(SEQUENCE_NUMBER_BYTES);
382
+ const view = new DataView(byteArray);
383
+ view.setUint32(0, sequenceNumber, /* littleEndian */ false);
384
+ return new Uint8Array(byteArray);
385
+ }
386
+
387
+ const INITIALIZATION_VECTOR_BYTES = 12;
388
+ const ENCODED_PUBLIC_KEY_LENGTH_BYTES = 65;
389
+ function encryptMessage(plaintext, sequenceNumber, sharedSecret) {
390
+ return __awaiter(this, void 0, void 0, function* () {
391
+ const sequenceNumberVector = createSequenceNumberVector(sequenceNumber);
392
+ const initializationVector = new Uint8Array(INITIALIZATION_VECTOR_BYTES);
393
+ crypto.getRandomValues(initializationVector);
394
+ const ciphertext = yield crypto.subtle.encrypt(getAlgorithmParams(sequenceNumberVector, initializationVector), sharedSecret, new TextEncoder().encode(plaintext));
395
+ const response = new Uint8Array(sequenceNumberVector.byteLength + initializationVector.byteLength + ciphertext.byteLength);
396
+ response.set(new Uint8Array(sequenceNumberVector), 0);
397
+ response.set(new Uint8Array(initializationVector), sequenceNumberVector.byteLength);
398
+ response.set(new Uint8Array(ciphertext), sequenceNumberVector.byteLength + initializationVector.byteLength);
399
+ return response;
400
+ });
401
+ }
402
+ function decryptMessage(message, sharedSecret) {
403
+ return __awaiter(this, void 0, void 0, function* () {
404
+ const sequenceNumberVector = message.slice(0, SEQUENCE_NUMBER_BYTES);
405
+ const initializationVector = message.slice(SEQUENCE_NUMBER_BYTES, SEQUENCE_NUMBER_BYTES + INITIALIZATION_VECTOR_BYTES);
406
+ const ciphertext = message.slice(SEQUENCE_NUMBER_BYTES + INITIALIZATION_VECTOR_BYTES);
407
+ const plaintextBuffer = yield crypto.subtle.decrypt(getAlgorithmParams(sequenceNumberVector, initializationVector), sharedSecret, ciphertext);
408
+ const plaintext = getUtf8Decoder().decode(plaintextBuffer);
409
+ return plaintext;
410
+ });
411
+ }
412
+ function getAlgorithmParams(sequenceNumber, initializationVector) {
413
+ return {
414
+ additionalData: sequenceNumber,
415
+ iv: initializationVector,
416
+ name: 'AES-GCM',
417
+ tagLength: 128, // 16 byte tag => 128 bits
418
+ };
419
+ }
420
+ let _utf8Decoder;
421
+ function getUtf8Decoder() {
422
+ if (_utf8Decoder === undefined) {
423
+ _utf8Decoder = new TextDecoder('utf-8');
424
+ }
425
+ return _utf8Decoder;
426
+ }
427
+
428
+ function generateAssociationKeypair() {
429
+ return __awaiter(this, void 0, void 0, function* () {
430
+ return yield crypto.subtle.generateKey({
431
+ name: 'ECDSA',
432
+ namedCurve: 'P-256',
433
+ }, false /* extractable */, ['sign'] /* keyUsages */);
434
+ });
435
+ }
436
+
437
+ function generateECDHKeypair() {
438
+ return __awaiter(this, void 0, void 0, function* () {
439
+ return yield crypto.subtle.generateKey({
440
+ name: 'ECDH',
441
+ namedCurve: 'P-256',
442
+ }, false /* extractable */, ['deriveKey', 'deriveBits'] /* keyUsages */);
443
+ });
444
+ }
445
+
446
+ // https://stackoverflow.com/a/9458996/802047
447
+ function arrayBufferToBase64String(buffer) {
448
+ let binary = '';
449
+ const bytes = new Uint8Array(buffer);
450
+ const len = bytes.byteLength;
451
+ for (let ii = 0; ii < len; ii++) {
452
+ binary += String.fromCharCode(bytes[ii]);
453
+ }
454
+ return window.btoa(binary);
455
+ }
456
+
457
+ function getRandomAssociationPort() {
458
+ return assertAssociationPort(49152 + Math.floor(Math.random() * (65535 - 49152 + 1)));
459
+ }
460
+ function assertAssociationPort(port) {
461
+ if (port < 49152 || port > 65535) {
462
+ throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_ASSOCIATION_PORT_OUT_OF_RANGE, `Association port number must be between 49152 and 65535. ${port} given.`, { port });
463
+ }
464
+ return port;
465
+ }
466
+
467
+ function getStringWithURLUnsafeCharactersReplaced(unsafeBase64EncodedString) {
468
+ return unsafeBase64EncodedString.replace(/[/+=]/g, (m) => ({
469
+ '/': '_',
470
+ '+': '-',
471
+ '=': '.',
472
+ }[m]));
473
+ }
474
+
475
+ const INTENT_NAME = 'solana-wallet';
476
+ function getPathParts(pathString) {
477
+ return (pathString
478
+ // Strip leading and trailing slashes
479
+ .replace(/(^\/+|\/+$)/g, '')
480
+ // Return an array of directories
481
+ .split('/'));
482
+ }
483
+ function getIntentURL(methodPathname, intentUrlBase) {
484
+ let baseUrl = null;
485
+ if (intentUrlBase) {
486
+ try {
487
+ baseUrl = new URL(intentUrlBase);
488
+ }
489
+ catch (_a) { } // eslint-disable-line no-empty
490
+ if ((baseUrl === null || baseUrl === void 0 ? void 0 : baseUrl.protocol) !== 'https:') {
491
+ throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL, 'Base URLs supplied by wallets must be valid `https` URLs');
492
+ }
493
+ }
494
+ baseUrl || (baseUrl = new URL(`${INTENT_NAME}:/`));
495
+ const pathname = methodPathname.startsWith('/')
496
+ ? // Method is an absolute path. Replace it wholesale.
497
+ methodPathname
498
+ : // Method is a relative path. Merge it with the existing one.
499
+ [...getPathParts(baseUrl.pathname), ...getPathParts(methodPathname)].join('/');
500
+ return new URL(pathname, baseUrl);
501
+ }
502
+ function getAssociateAndroidIntentURL(associationPublicKey, putativePort, associationURLBase, protocolVersions = ['v1']) {
503
+ return __awaiter(this, void 0, void 0, function* () {
504
+ const associationPort = assertAssociationPort(putativePort);
505
+ const exportedKey = yield crypto.subtle.exportKey('raw', associationPublicKey);
506
+ const encodedKey = arrayBufferToBase64String(exportedKey);
507
+ const url = getIntentURL('v1/associate/local', associationURLBase);
508
+ url.searchParams.set('association', getStringWithURLUnsafeCharactersReplaced(encodedKey));
509
+ url.searchParams.set('port', `${associationPort}`);
510
+ protocolVersions.forEach((version) => {
511
+ url.searchParams.set('v', version);
512
+ });
513
+ return url;
514
+ });
515
+ }
516
+ function getRemoteAssociateAndroidIntentURL(associationPublicKey, hostAuthority, reflectorId, associationURLBase, protocolVersions = ['v1']) {
517
+ return __awaiter(this, void 0, void 0, function* () {
518
+ const exportedKey = yield crypto.subtle.exportKey('raw', associationPublicKey);
519
+ const encodedKey = arrayBufferToBase64String(exportedKey);
520
+ const url = getIntentURL('v1/associate/remote', associationURLBase);
521
+ url.searchParams.set('association', getStringWithURLUnsafeCharactersReplaced(encodedKey));
522
+ url.searchParams.set('reflector', `${hostAuthority}`);
523
+ url.searchParams.set('id', `${fromUint8Array$1(reflectorId, true)}`);
524
+ protocolVersions.forEach((version) => {
525
+ url.searchParams.set('v', version);
526
+ });
527
+ return url;
528
+ });
529
+ }
530
+
531
+ function encryptJsonRpcMessage(jsonRpcMessage, sharedSecret) {
532
+ return __awaiter(this, void 0, void 0, function* () {
533
+ const plaintext = JSON.stringify(jsonRpcMessage);
534
+ const sequenceNumber = jsonRpcMessage.id;
535
+ return encryptMessage(plaintext, sequenceNumber, sharedSecret);
536
+ });
537
+ }
538
+ function decryptJsonRpcMessage(message, sharedSecret) {
539
+ return __awaiter(this, void 0, void 0, function* () {
540
+ const plaintext = yield decryptMessage(message, sharedSecret);
541
+ const jsonRpcMessage = JSON.parse(plaintext);
542
+ if (Object.hasOwnProperty.call(jsonRpcMessage, 'error')) {
543
+ throw new SolanaMobileWalletAdapterProtocolError(jsonRpcMessage.id, jsonRpcMessage.error.code, jsonRpcMessage.error.message);
544
+ }
545
+ return jsonRpcMessage;
546
+ });
547
+ }
548
+
549
+ function parseHelloRsp(payloadBuffer, // The X9.62-encoded wallet endpoint ephemeral ECDH public keypoint.
550
+ associationPublicKey, ecdhPrivateKey) {
551
+ return __awaiter(this, void 0, void 0, function* () {
552
+ const [associationPublicKeyBuffer, walletPublicKey] = yield Promise.all([
553
+ crypto.subtle.exportKey('raw', associationPublicKey),
554
+ crypto.subtle.importKey('raw', payloadBuffer.slice(0, ENCODED_PUBLIC_KEY_LENGTH_BYTES), { name: 'ECDH', namedCurve: 'P-256' }, false /* extractable */, [] /* keyUsages */),
555
+ ]);
556
+ const sharedSecret = yield crypto.subtle.deriveBits({ name: 'ECDH', public: walletPublicKey }, ecdhPrivateKey, 256);
557
+ const ecdhSecretKey = yield crypto.subtle.importKey('raw', sharedSecret, 'HKDF', false /* extractable */, ['deriveKey'] /* keyUsages */);
558
+ const aesKeyMaterialVal = yield crypto.subtle.deriveKey({
559
+ name: 'HKDF',
560
+ hash: 'SHA-256',
561
+ salt: new Uint8Array(associationPublicKeyBuffer),
562
+ info: new Uint8Array(),
563
+ }, ecdhSecretKey, { name: 'AES-GCM', length: 128 }, false /* extractable */, ['encrypt', 'decrypt']);
564
+ return aesKeyMaterialVal;
565
+ });
566
+ }
567
+
568
+ function parseSessionProps(message, sharedSecret) {
569
+ return __awaiter(this, void 0, void 0, function* () {
570
+ const plaintext = yield decryptMessage(message, sharedSecret);
571
+ const jsonProperties = JSON.parse(plaintext);
572
+ let protocolVersion = 'legacy';
573
+ if (Object.hasOwnProperty.call(jsonProperties, 'v')) {
574
+ switch (jsonProperties.v) {
575
+ case 1:
576
+ case '1':
577
+ case 'v1':
578
+ protocolVersion = 'v1';
579
+ break;
580
+ case 'legacy':
581
+ protocolVersion = 'legacy';
582
+ break;
583
+ default:
584
+ throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_INVALID_PROTOCOL_VERSION, `Unknown/unsupported protocol version: ${jsonProperties.v}`);
585
+ }
586
+ }
587
+ return ({
588
+ protocol_version: protocolVersion
589
+ });
590
+ });
591
+ }
592
+
593
+ // Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
594
+ const Browser = {
595
+ Firefox: 0,
596
+ Other: 1,
597
+ };
598
+ function assertUnreachable(x) {
599
+ return x;
600
+ }
601
+ function getBrowser() {
602
+ return navigator.userAgent.indexOf('Firefox/') !== -1 ? Browser.Firefox : Browser.Other;
603
+ }
604
+ function getDetectionPromise() {
605
+ // Chrome and others silently fail if a custom protocol is not supported.
606
+ // For these, we wait to see if the browser is navigated away from in
607
+ // a reasonable amount of time (ie. the native wallet opened).
608
+ return new Promise((resolve, reject) => {
609
+ function cleanup() {
610
+ clearTimeout(timeoutId);
611
+ window.removeEventListener('blur', handleBlur);
612
+ }
613
+ function handleBlur() {
614
+ cleanup();
615
+ resolve();
616
+ }
617
+ window.addEventListener('blur', handleBlur);
618
+ const timeoutId = setTimeout(() => {
619
+ cleanup();
620
+ reject();
621
+ }, 3000);
622
+ });
623
+ }
624
+ let _frame = null;
625
+ function launchUrlThroughHiddenFrame(url) {
626
+ if (_frame == null) {
627
+ _frame = document.createElement('iframe');
628
+ _frame.style.display = 'none';
629
+ document.body.appendChild(_frame);
630
+ }
631
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
632
+ _frame.contentWindow.location.href = url.toString();
633
+ }
634
+ function launchAssociation(associationUrl) {
635
+ return __awaiter(this, void 0, void 0, function* () {
636
+ if (associationUrl.protocol === 'https:') {
637
+ // The association URL is an Android 'App Link' or iOS 'Universal Link'.
638
+ // These are regular web URLs that are designed to launch an app if it
639
+ // is installed or load the actual target webpage if not.
640
+ window.location.assign(associationUrl);
641
+ }
642
+ else {
643
+ // The association URL has a custom protocol (eg. `solana-wallet:`)
644
+ try {
645
+ const browser = getBrowser();
646
+ switch (browser) {
647
+ case Browser.Firefox:
648
+ // If a custom protocol is not supported in Firefox, it throws.
649
+ launchUrlThroughHiddenFrame(associationUrl);
650
+ // If we reached this line, it's supported.
651
+ break;
652
+ case Browser.Other: {
653
+ const detectionPromise = getDetectionPromise();
654
+ window.location.assign(associationUrl);
655
+ yield detectionPromise;
656
+ break;
657
+ }
658
+ default:
659
+ assertUnreachable(browser);
660
+ }
661
+ }
662
+ catch (e) {
663
+ throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_WALLET_NOT_FOUND, 'Found no installed wallet that supports the mobile wallet protocol.');
664
+ }
665
+ }
666
+ });
667
+ }
668
+ function startSession(associationPublicKey, associationURLBase) {
669
+ return __awaiter(this, void 0, void 0, function* () {
670
+ const randomAssociationPort = getRandomAssociationPort();
671
+ const associationUrl = yield getAssociateAndroidIntentURL(associationPublicKey, randomAssociationPort, associationURLBase);
672
+ yield launchAssociation(associationUrl);
673
+ return randomAssociationPort;
674
+ });
675
+ }
676
+
677
+ const WEBSOCKET_CONNECTION_CONFIG = {
678
+ /**
679
+ * 300 milliseconds is a generally accepted threshold for what someone
680
+ * would consider an acceptable response time for a user interface
681
+ * after having performed a low-attention tapping task. We set the initial
682
+ * interval at which we wait for the wallet to set up the websocket at
683
+ * half this, as per the Nyquist frequency, with a progressive backoff
684
+ * sequence from there. The total wait time is 30s, which allows for the
685
+ * user to be presented with a disambiguation dialog, select a wallet, and
686
+ * for the wallet app to subsequently start.
687
+ */
688
+ retryDelayScheduleMs: [150, 150, 200, 500, 500, 750, 750, 1000],
689
+ timeoutMs: 30000,
690
+ };
691
+ const WEBSOCKET_PROTOCOL_BINARY = 'com.solana.mobilewalletadapter.v1';
692
+ const WEBSOCKET_PROTOCOL_BASE64 = 'com.solana.mobilewalletadapter.v1.base64';
693
+ function assertSecureContext() {
694
+ if (typeof window === 'undefined' || window.isSecureContext !== true) {
695
+ throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SECURE_CONTEXT_REQUIRED, 'The mobile wallet adapter protocol must be used in a secure context (`https`).');
696
+ }
697
+ }
698
+ function assertSecureEndpointSpecificURI(walletUriBase) {
699
+ let url;
700
+ try {
701
+ url = new URL(walletUriBase);
702
+ }
703
+ catch (_a) {
704
+ throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL, 'Invalid base URL supplied by wallet');
705
+ }
706
+ if (url.protocol !== 'https:') {
707
+ throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL, 'Base URLs supplied by wallets must be valid `https` URLs');
708
+ }
709
+ }
710
+ function getSequenceNumberFromByteArray(byteArray) {
711
+ const view = new DataView(byteArray);
712
+ return view.getUint32(0, /* littleEndian */ false);
713
+ }
714
+ function decodeVarLong(byteArray) {
715
+ var bytes = new Uint8Array(byteArray), l = byteArray.byteLength, limit = 10, value = 0, offset = 0, b;
716
+ do {
717
+ if (offset >= l || offset > limit)
718
+ throw new RangeError('Failed to decode varint');
719
+ b = bytes[offset++];
720
+ value |= (b & 0x7F) << (7 * offset);
721
+ } while (b >= 0x80);
722
+ return { value, offset };
723
+ }
724
+ function getReflectorIdFromByteArray(byteArray) {
725
+ let { value: length, offset } = decodeVarLong(byteArray);
726
+ return new Uint8Array(byteArray.slice(offset, offset + length));
727
+ }
728
+ function transact(callback, config) {
729
+ return __awaiter(this, void 0, void 0, function* () {
730
+ assertSecureContext();
731
+ const associationKeypair = yield generateAssociationKeypair();
732
+ const sessionPort = yield startSession(associationKeypair.publicKey, config === null || config === void 0 ? void 0 : config.baseUri);
733
+ const websocketURL = `ws://localhost:${sessionPort}/solana-wallet`;
734
+ let connectionStartTime;
735
+ const getNextRetryDelayMs = (() => {
736
+ const schedule = [...WEBSOCKET_CONNECTION_CONFIG.retryDelayScheduleMs];
737
+ return () => (schedule.length > 1 ? schedule.shift() : schedule[0]);
738
+ })();
739
+ let nextJsonRpcMessageId = 1;
740
+ let lastKnownInboundSequenceNumber = 0;
741
+ let state = { __type: 'disconnected' };
742
+ return new Promise((resolve, reject) => {
743
+ let socket;
744
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
745
+ const jsonRpcResponsePromises = {};
746
+ const handleOpen = () => __awaiter(this, void 0, void 0, function* () {
747
+ if (state.__type !== 'connecting') {
748
+ console.warn('Expected adapter state to be `connecting` at the moment the websocket opens. ' +
749
+ `Got \`${state.__type}\`.`);
750
+ return;
751
+ }
752
+ socket.removeEventListener('open', handleOpen);
753
+ // previous versions of this library and walletlib incorrectly implemented the MWA session
754
+ // establishment protocol for local connections. The dapp is supposed to wait for the
755
+ // APP_PING message before sending the HELLO_REQ. Instead, the dapp was sending the HELLO_REQ
756
+ // immediately upon connection to the websocket server regardless of wether or not an
757
+ // APP_PING was sent by the wallet/websocket server. We must continue to support this behavior
758
+ // in case the user is using a wallet that has not updated their walletlib implementation.
759
+ const { associationKeypair } = state;
760
+ const ecdhKeypair = yield generateECDHKeypair();
761
+ socket.send(yield createHelloReq(ecdhKeypair.publicKey, associationKeypair.privateKey));
762
+ state = {
763
+ __type: 'hello_req_sent',
764
+ associationPublicKey: associationKeypair.publicKey,
765
+ ecdhPrivateKey: ecdhKeypair.privateKey,
766
+ };
767
+ });
768
+ const handleClose = (evt) => {
769
+ if (evt.wasClean) {
770
+ state = { __type: 'disconnected' };
771
+ }
772
+ else {
773
+ reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED, `The wallet session dropped unexpectedly (${evt.code}: ${evt.reason}).`, { closeEvent: evt }));
774
+ }
775
+ disposeSocket();
776
+ };
777
+ const handleError = (_evt) => __awaiter(this, void 0, void 0, function* () {
778
+ disposeSocket();
779
+ if (Date.now() - connectionStartTime >= WEBSOCKET_CONNECTION_CONFIG.timeoutMs) {
780
+ reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_TIMEOUT, `Failed to connect to the wallet websocket at ${websocketURL}.`));
781
+ }
782
+ else {
783
+ yield new Promise((resolve) => {
784
+ const retryDelayMs = getNextRetryDelayMs();
785
+ retryWaitTimeoutId = window.setTimeout(resolve, retryDelayMs);
786
+ });
787
+ attemptSocketConnection();
788
+ }
789
+ });
790
+ const handleMessage = (evt) => __awaiter(this, void 0, void 0, function* () {
791
+ const responseBuffer = yield evt.data.arrayBuffer();
792
+ switch (state.__type) {
793
+ case 'connecting':
794
+ if (responseBuffer.byteLength !== 0) {
795
+ throw new Error('Encountered unexpected message while connecting');
796
+ }
797
+ const ecdhKeypair = yield generateECDHKeypair();
798
+ socket.send(yield createHelloReq(ecdhKeypair.publicKey, associationKeypair.privateKey));
799
+ state = {
800
+ __type: 'hello_req_sent',
801
+ associationPublicKey: associationKeypair.publicKey,
802
+ ecdhPrivateKey: ecdhKeypair.privateKey,
803
+ };
804
+ break;
805
+ case 'connected':
806
+ try {
807
+ const sequenceNumberVector = responseBuffer.slice(0, SEQUENCE_NUMBER_BYTES);
808
+ const sequenceNumber = getSequenceNumberFromByteArray(sequenceNumberVector);
809
+ if (sequenceNumber !== (lastKnownInboundSequenceNumber + 1)) {
810
+ throw new Error('Encrypted message has invalid sequence number');
811
+ }
812
+ lastKnownInboundSequenceNumber = sequenceNumber;
813
+ const jsonRpcMessage = yield decryptJsonRpcMessage(responseBuffer, state.sharedSecret);
814
+ const responsePromise = jsonRpcResponsePromises[jsonRpcMessage.id];
815
+ delete jsonRpcResponsePromises[jsonRpcMessage.id];
816
+ responsePromise.resolve(jsonRpcMessage.result);
817
+ }
818
+ catch (e) {
819
+ if (e instanceof SolanaMobileWalletAdapterProtocolError) {
820
+ const responsePromise = jsonRpcResponsePromises[e.jsonRpcMessageId];
821
+ delete jsonRpcResponsePromises[e.jsonRpcMessageId];
822
+ responsePromise.reject(e);
823
+ }
824
+ else {
825
+ throw e;
826
+ }
827
+ }
828
+ break;
829
+ case 'hello_req_sent': {
830
+ // if we receive an APP_PING message (empty message), resend the HELLO_REQ (see above)
831
+ if (responseBuffer.byteLength === 0) {
832
+ const ecdhKeypair = yield generateECDHKeypair();
833
+ socket.send(yield createHelloReq(ecdhKeypair.publicKey, associationKeypair.privateKey));
834
+ state = {
835
+ __type: 'hello_req_sent',
836
+ associationPublicKey: associationKeypair.publicKey,
837
+ ecdhPrivateKey: ecdhKeypair.privateKey,
838
+ };
839
+ break;
840
+ }
841
+ const sharedSecret = yield parseHelloRsp(responseBuffer, state.associationPublicKey, state.ecdhPrivateKey);
842
+ const sessionPropertiesBuffer = responseBuffer.slice(ENCODED_PUBLIC_KEY_LENGTH_BYTES);
843
+ const sessionProperties = sessionPropertiesBuffer.byteLength !== 0
844
+ ? yield (() => __awaiter(this, void 0, void 0, function* () {
845
+ const sequenceNumberVector = sessionPropertiesBuffer.slice(0, SEQUENCE_NUMBER_BYTES);
846
+ const sequenceNumber = getSequenceNumberFromByteArray(sequenceNumberVector);
847
+ if (sequenceNumber !== (lastKnownInboundSequenceNumber + 1)) {
848
+ throw new Error('Encrypted message has invalid sequence number');
849
+ }
850
+ lastKnownInboundSequenceNumber = sequenceNumber;
851
+ return parseSessionProps(sessionPropertiesBuffer, sharedSecret);
852
+ }))() : { protocol_version: 'legacy' };
853
+ state = { __type: 'connected', sharedSecret, sessionProperties };
854
+ const wallet = createMobileWalletProxy(sessionProperties.protocol_version, (method, params) => __awaiter(this, void 0, void 0, function* () {
855
+ const id = nextJsonRpcMessageId++;
856
+ socket.send(yield encryptJsonRpcMessage({
857
+ id,
858
+ jsonrpc: '2.0',
859
+ method,
860
+ params: params !== null && params !== void 0 ? params : {},
861
+ }, sharedSecret));
862
+ return new Promise((resolve, reject) => {
863
+ jsonRpcResponsePromises[id] = {
864
+ resolve(result) {
865
+ switch (method) {
866
+ case 'authorize':
867
+ case 'reauthorize': {
868
+ const { wallet_uri_base } = result;
869
+ if (wallet_uri_base != null) {
870
+ try {
871
+ assertSecureEndpointSpecificURI(wallet_uri_base);
872
+ }
873
+ catch (e) {
874
+ reject(e);
875
+ return;
876
+ }
877
+ }
878
+ break;
879
+ }
880
+ }
881
+ resolve(result);
882
+ },
883
+ reject,
884
+ };
885
+ });
886
+ }));
887
+ try {
888
+ resolve(yield callback(wallet));
889
+ }
890
+ catch (e) {
891
+ reject(e);
892
+ }
893
+ finally {
894
+ disposeSocket();
895
+ socket.close();
896
+ }
897
+ break;
898
+ }
899
+ }
900
+ });
901
+ let disposeSocket;
902
+ let retryWaitTimeoutId;
903
+ const attemptSocketConnection = () => {
904
+ if (disposeSocket) {
905
+ disposeSocket();
906
+ }
907
+ state = { __type: 'connecting', associationKeypair };
908
+ if (connectionStartTime === undefined) {
909
+ connectionStartTime = Date.now();
910
+ }
911
+ socket = new WebSocket(websocketURL, [WEBSOCKET_PROTOCOL_BINARY]);
912
+ socket.addEventListener('open', handleOpen);
913
+ socket.addEventListener('close', handleClose);
914
+ socket.addEventListener('error', handleError);
915
+ socket.addEventListener('message', handleMessage);
916
+ disposeSocket = () => {
917
+ window.clearTimeout(retryWaitTimeoutId);
918
+ socket.removeEventListener('open', handleOpen);
919
+ socket.removeEventListener('close', handleClose);
920
+ socket.removeEventListener('error', handleError);
921
+ socket.removeEventListener('message', handleMessage);
922
+ };
923
+ };
924
+ attemptSocketConnection();
925
+ });
926
+ });
927
+ }
928
+ function startRemoteScenario(config) {
929
+ return __awaiter(this, void 0, void 0, function* () {
930
+ assertSecureContext();
931
+ const associationKeypair = yield generateAssociationKeypair();
932
+ const websocketURL = `wss://${config === null || config === void 0 ? void 0 : config.remoteHostAuthority}/reflect`;
933
+ let connectionStartTime;
934
+ const getNextRetryDelayMs = (() => {
935
+ const schedule = [...WEBSOCKET_CONNECTION_CONFIG.retryDelayScheduleMs];
936
+ return () => (schedule.length > 1 ? schedule.shift() : schedule[0]);
937
+ })();
938
+ let nextJsonRpcMessageId = 1;
939
+ let lastKnownInboundSequenceNumber = 0;
940
+ let encoding;
941
+ let state = { __type: 'disconnected' };
942
+ let socket;
943
+ let disposeSocket;
944
+ let decodeBytes = (evt) => __awaiter(this, void 0, void 0, function* () {
945
+ if (encoding == 'base64') { // base64 encoding
946
+ const message = yield evt.data;
947
+ return toUint8Array(message).buffer;
948
+ }
949
+ else {
950
+ return yield evt.data.arrayBuffer();
951
+ }
952
+ });
953
+ // Reflector Connection Phase
954
+ // here we connect to the reflector and wait for the REFLECTOR_ID message
955
+ // so we build the association URL and return that back to the caller
956
+ const associationUrl = yield new Promise((resolve, reject) => {
957
+ const handleOpen = () => __awaiter(this, void 0, void 0, function* () {
958
+ if (state.__type !== 'connecting') {
959
+ console.warn('Expected adapter state to be `connecting` at the moment the websocket opens. ' +
960
+ `Got \`${state.__type}\`.`);
961
+ return;
962
+ }
963
+ if (socket.protocol.includes(WEBSOCKET_PROTOCOL_BASE64)) {
964
+ encoding = 'base64';
965
+ }
966
+ else {
967
+ encoding = 'binary';
968
+ }
969
+ socket.removeEventListener('open', handleOpen);
970
+ });
971
+ const handleClose = (evt) => {
972
+ if (evt.wasClean) {
973
+ state = { __type: 'disconnected' };
974
+ }
975
+ else {
976
+ reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED, `The wallet session dropped unexpectedly (${evt.code}: ${evt.reason}).`, { closeEvent: evt }));
977
+ }
978
+ disposeSocket();
979
+ };
980
+ const handleError = (_evt) => __awaiter(this, void 0, void 0, function* () {
981
+ disposeSocket();
982
+ if (Date.now() - connectionStartTime >= WEBSOCKET_CONNECTION_CONFIG.timeoutMs) {
983
+ reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_TIMEOUT, `Failed to connect to the wallet websocket at ${websocketURL}.`));
984
+ }
985
+ else {
986
+ yield new Promise((resolve) => {
987
+ const retryDelayMs = getNextRetryDelayMs();
988
+ retryWaitTimeoutId = window.setTimeout(resolve, retryDelayMs);
989
+ });
990
+ attemptSocketConnection();
991
+ }
992
+ });
993
+ const handleReflectorIdMessage = (evt) => __awaiter(this, void 0, void 0, function* () {
994
+ const responseBuffer = yield decodeBytes(evt);
995
+ if (state.__type === 'connecting') {
996
+ if (responseBuffer.byteLength == 0) {
997
+ throw new Error('Encountered unexpected message while connecting');
998
+ }
999
+ const reflectorId = getReflectorIdFromByteArray(responseBuffer);
1000
+ state = {
1001
+ __type: 'reflector_id_received',
1002
+ reflectorId: reflectorId
1003
+ };
1004
+ const associationUrl = yield getRemoteAssociateAndroidIntentURL(associationKeypair.publicKey, config.remoteHostAuthority, reflectorId, config === null || config === void 0 ? void 0 : config.baseUri);
1005
+ socket.removeEventListener('message', handleReflectorIdMessage);
1006
+ resolve(associationUrl);
1007
+ }
1008
+ });
1009
+ let retryWaitTimeoutId;
1010
+ const attemptSocketConnection = () => {
1011
+ if (disposeSocket) {
1012
+ disposeSocket();
1013
+ }
1014
+ state = { __type: 'connecting', associationKeypair };
1015
+ if (connectionStartTime === undefined) {
1016
+ connectionStartTime = Date.now();
1017
+ }
1018
+ socket = new WebSocket(websocketURL, [WEBSOCKET_PROTOCOL_BINARY, WEBSOCKET_PROTOCOL_BASE64]);
1019
+ socket.addEventListener('open', handleOpen);
1020
+ socket.addEventListener('close', handleClose);
1021
+ socket.addEventListener('error', handleError);
1022
+ socket.addEventListener('message', handleReflectorIdMessage);
1023
+ disposeSocket = () => {
1024
+ window.clearTimeout(retryWaitTimeoutId);
1025
+ socket.removeEventListener('open', handleOpen);
1026
+ socket.removeEventListener('close', handleClose);
1027
+ socket.removeEventListener('error', handleError);
1028
+ socket.removeEventListener('message', handleReflectorIdMessage);
1029
+ };
1030
+ };
1031
+ attemptSocketConnection();
1032
+ });
1033
+ // Wallet Connection Phase
1034
+ // here we return the association URL (containing the reflector ID) to the caller +
1035
+ // a promise that will resolve the MobileWallet object once the wallet connects.
1036
+ let sessionEstablished = false;
1037
+ let handleClose;
1038
+ return { associationUrl, close: () => {
1039
+ socket.close();
1040
+ handleClose();
1041
+ }, wallet: new Promise((resolve, reject) => {
1042
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1043
+ const jsonRpcResponsePromises = {};
1044
+ const handleMessage = (evt) => __awaiter(this, void 0, void 0, function* () {
1045
+ const responseBuffer = yield decodeBytes(evt);
1046
+ switch (state.__type) {
1047
+ case 'reflector_id_received':
1048
+ if (responseBuffer.byteLength !== 0) {
1049
+ throw new Error('Encountered unexpected message while awaiting reflection');
1050
+ }
1051
+ const ecdhKeypair = yield generateECDHKeypair();
1052
+ const binaryMsg = yield createHelloReq(ecdhKeypair.publicKey, associationKeypair.privateKey);
1053
+ if (encoding == 'base64') {
1054
+ socket.send(fromUint8Array$1(binaryMsg));
1055
+ }
1056
+ else {
1057
+ socket.send(binaryMsg);
1058
+ }
1059
+ state = {
1060
+ __type: 'hello_req_sent',
1061
+ associationPublicKey: associationKeypair.publicKey,
1062
+ ecdhPrivateKey: ecdhKeypair.privateKey,
1063
+ };
1064
+ break;
1065
+ case 'connected':
1066
+ try {
1067
+ const sequenceNumberVector = responseBuffer.slice(0, SEQUENCE_NUMBER_BYTES);
1068
+ const sequenceNumber = getSequenceNumberFromByteArray(sequenceNumberVector);
1069
+ if (sequenceNumber !== (lastKnownInboundSequenceNumber + 1)) {
1070
+ throw new Error('Encrypted message has invalid sequence number');
1071
+ }
1072
+ lastKnownInboundSequenceNumber = sequenceNumber;
1073
+ const jsonRpcMessage = yield decryptJsonRpcMessage(responseBuffer, state.sharedSecret);
1074
+ const responsePromise = jsonRpcResponsePromises[jsonRpcMessage.id];
1075
+ delete jsonRpcResponsePromises[jsonRpcMessage.id];
1076
+ responsePromise.resolve(jsonRpcMessage.result);
1077
+ }
1078
+ catch (e) {
1079
+ if (e instanceof SolanaMobileWalletAdapterProtocolError) {
1080
+ const responsePromise = jsonRpcResponsePromises[e.jsonRpcMessageId];
1081
+ delete jsonRpcResponsePromises[e.jsonRpcMessageId];
1082
+ responsePromise.reject(e);
1083
+ }
1084
+ else {
1085
+ throw e;
1086
+ }
1087
+ }
1088
+ break;
1089
+ case 'hello_req_sent': {
1090
+ const sharedSecret = yield parseHelloRsp(responseBuffer, state.associationPublicKey, state.ecdhPrivateKey);
1091
+ const sessionPropertiesBuffer = responseBuffer.slice(ENCODED_PUBLIC_KEY_LENGTH_BYTES);
1092
+ const sessionProperties = sessionPropertiesBuffer.byteLength !== 0
1093
+ ? yield (() => __awaiter(this, void 0, void 0, function* () {
1094
+ const sequenceNumberVector = sessionPropertiesBuffer.slice(0, SEQUENCE_NUMBER_BYTES);
1095
+ const sequenceNumber = getSequenceNumberFromByteArray(sequenceNumberVector);
1096
+ if (sequenceNumber !== (lastKnownInboundSequenceNumber + 1)) {
1097
+ throw new Error('Encrypted message has invalid sequence number');
1098
+ }
1099
+ lastKnownInboundSequenceNumber = sequenceNumber;
1100
+ return parseSessionProps(sessionPropertiesBuffer, sharedSecret);
1101
+ }))() : { protocol_version: 'legacy' };
1102
+ state = { __type: 'connected', sharedSecret, sessionProperties };
1103
+ const wallet = createMobileWalletProxy(sessionProperties.protocol_version, (method, params) => __awaiter(this, void 0, void 0, function* () {
1104
+ const id = nextJsonRpcMessageId++;
1105
+ const binaryMsg = yield encryptJsonRpcMessage({
1106
+ id,
1107
+ jsonrpc: '2.0',
1108
+ method,
1109
+ params: params !== null && params !== void 0 ? params : {},
1110
+ }, sharedSecret);
1111
+ if (encoding == 'base64') {
1112
+ socket.send(fromUint8Array$1(binaryMsg));
1113
+ }
1114
+ else {
1115
+ socket.send(binaryMsg);
1116
+ }
1117
+ return new Promise((resolve, reject) => {
1118
+ jsonRpcResponsePromises[id] = {
1119
+ resolve(result) {
1120
+ switch (method) {
1121
+ case 'authorize':
1122
+ case 'reauthorize': {
1123
+ const { wallet_uri_base } = result;
1124
+ if (wallet_uri_base != null) {
1125
+ try {
1126
+ assertSecureEndpointSpecificURI(wallet_uri_base);
1127
+ }
1128
+ catch (e) {
1129
+ reject(e);
1130
+ return;
1131
+ }
1132
+ }
1133
+ break;
1134
+ }
1135
+ }
1136
+ resolve(result);
1137
+ },
1138
+ reject,
1139
+ };
1140
+ });
1141
+ }));
1142
+ sessionEstablished = true;
1143
+ try {
1144
+ resolve(wallet);
1145
+ }
1146
+ catch (e) {
1147
+ reject(e);
1148
+ }
1149
+ break;
1150
+ }
1151
+ }
1152
+ });
1153
+ socket.addEventListener('message', handleMessage);
1154
+ handleClose = () => {
1155
+ socket.removeEventListener('message', handleMessage);
1156
+ disposeSocket();
1157
+ if (!sessionEstablished) {
1158
+ reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED, `The wallet session was closed before connection.`, { closeEvent: new CloseEvent('socket was closed before connection') }));
1159
+ }
1160
+ };
1161
+ }) };
1162
+ });
1163
+ }
1164
+
1165
+ exports.startRemoteScenario = startRemoteScenario;
1166
+ exports.transact = transact;
1167
+ //# sourceMappingURL=index.browser-BOJRGZWX.js.map