@pooflabs/web 0.0.84 → 0.0.85-rc2

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.
Files changed (67) hide show
  1. package/dist/auth/providers/solana-mobile-wallet-provider.d.ts +47 -20
  2. package/dist/{index-BM6hgCdH.js → index-B1_Q49qQ.js} +2 -2
  3. package/dist/{index-BM6hgCdH.js.map → index-B1_Q49qQ.js.map} +1 -1
  4. package/dist/{index-CH2G5Y9i.esm.js → index-B7tJ8TAn.esm.js} +2 -2
  5. package/dist/{index-CH2G5Y9i.esm.js.map → index-B7tJ8TAn.esm.js.map} +1 -1
  6. package/dist/{index-BPlF6-PQ.js → index-BeFR3-cf.js} +522 -289
  7. package/dist/{index-BPlF6-PQ.js.map → index-BeFR3-cf.js.map} +1 -1
  8. package/dist/index-DbJCCvVN.esm.js +6 -0
  9. package/dist/index-DbJCCvVN.esm.js.map +1 -0
  10. package/dist/{index-CSeN3jbG.js → index-DflG_JAB.js} +3 -3
  11. package/dist/{index-CSeN3jbG.js.map → index-DflG_JAB.js.map} +1 -1
  12. package/dist/{index-Bp4b5LXs.js → index-DyRIXw-l.js} +2 -2
  13. package/dist/index-DyRIXw-l.js.map +1 -0
  14. package/dist/{index-XHbmzFFO.esm.js → index-QukWgaIi.esm.js} +523 -290
  15. package/dist/{index-XHbmzFFO.esm.js.map → index-QukWgaIi.esm.js.map} +1 -1
  16. package/dist/{index-ep10Sw82.esm.js → index-agGmWBn4.esm.js} +3 -3
  17. package/dist/{index-ep10Sw82.esm.js.map → index-agGmWBn4.esm.js.map} +1 -1
  18. package/dist/{index.browser-C1QL04xM.js → index.browser-BJmFjY2f.js} +1006 -15
  19. package/dist/index.browser-BJmFjY2f.js.map +1 -0
  20. package/dist/index.browser-DZtZWdul.js +4994 -0
  21. package/dist/index.browser-DZtZWdul.js.map +1 -0
  22. package/dist/index.browser-lHP9glAW.esm.js +6060 -0
  23. package/dist/index.browser-lHP9glAW.esm.js.map +1 -0
  24. package/dist/{index.browser-Cgj7Hs6n.esm.js → index.browser-p-pgsxxW.esm.js} +836 -916
  25. package/dist/index.browser-p-pgsxxW.esm.js.map +1 -0
  26. package/dist/index.esm.js +1 -1
  27. package/dist/index.js +1 -1
  28. package/dist/{index.native-BmtuyGXs.js → index.native-ANhb4jEB.js} +4 -4
  29. package/dist/{index.native-BmtuyGXs.js.map → index.native-ANhb4jEB.js.map} +1 -1
  30. package/dist/{index.native-X01vlukI.esm.js → index.native-a3mhu9HP.esm.js} +4 -4
  31. package/dist/{index.native-X01vlukI.esm.js.map → index.native-a3mhu9HP.esm.js.map} +1 -1
  32. package/dist/index.native.esm.js +1 -1
  33. package/dist/index.native.js +1 -1
  34. package/dist/{phantom-wallet-provider-C_LPU6Ns.js → phantom-wallet-provider-BOdn0D1Q.js} +4 -4
  35. package/dist/{phantom-wallet-provider-C_LPU6Ns.js.map → phantom-wallet-provider-BOdn0D1Q.js.map} +1 -1
  36. package/dist/{phantom-wallet-provider-DRvhBbbR.esm.js → phantom-wallet-provider-Dsza6TBv.esm.js} +4 -4
  37. package/dist/{phantom-wallet-provider-DRvhBbbR.esm.js.map → phantom-wallet-provider-Dsza6TBv.esm.js.map} +1 -1
  38. package/dist/{privy-wallet-provider-DQM8xO38.js → privy-wallet-provider-BlPh3Gn_.js} +3 -3
  39. package/dist/{privy-wallet-provider-DQM8xO38.js.map → privy-wallet-provider-BlPh3Gn_.js.map} +1 -1
  40. package/dist/{privy-wallet-provider-ZGu8q5T5.esm.js → privy-wallet-provider-bwwhkFjL.esm.js} +3 -3
  41. package/dist/{privy-wallet-provider-ZGu8q5T5.esm.js.map → privy-wallet-provider-bwwhkFjL.esm.js.map} +1 -1
  42. package/dist/solana-mobile-wallet-provider-S8yWHG6i.js +841 -0
  43. package/dist/solana-mobile-wallet-provider-S8yWHG6i.js.map +1 -0
  44. package/dist/solana-mobile-wallet-provider-fzLIgJBf.esm.js +820 -0
  45. package/dist/solana-mobile-wallet-provider-fzLIgJBf.esm.js.map +1 -0
  46. package/package.json +2 -1
  47. package/dist/index-Bp4b5LXs.js.map +0 -1
  48. package/dist/index-C80kRxWk.esm.js +0 -6
  49. package/dist/index-C80kRxWk.esm.js.map +0 -1
  50. package/dist/index.browser-BvNsUWjt.js +0 -105
  51. package/dist/index.browser-BvNsUWjt.js.map +0 -1
  52. package/dist/index.browser-BwIqqM4U.js +0 -1099
  53. package/dist/index.browser-BwIqqM4U.js.map +0 -1
  54. package/dist/index.browser-C1QL04xM.js.map +0 -1
  55. package/dist/index.browser-Cgj7Hs6n.esm.js.map +0 -1
  56. package/dist/index.browser-CifFI7Ju.esm.js +0 -1096
  57. package/dist/index.browser-CifFI7Ju.esm.js.map +0 -1
  58. package/dist/index.browser-D8VWPXTZ.esm.js +0 -102
  59. package/dist/index.browser-D8VWPXTZ.esm.js.map +0 -1
  60. package/dist/index.browser-OvGNsMPu.esm.js +0 -1002
  61. package/dist/index.browser-OvGNsMPu.esm.js.map +0 -1
  62. package/dist/index.browser-vuTr40so.js +0 -1008
  63. package/dist/index.browser-vuTr40so.js.map +0 -1
  64. package/dist/solana-mobile-wallet-provider-6gNw2_bX.js +0 -608
  65. package/dist/solana-mobile-wallet-provider-6gNw2_bX.js.map +0 -1
  66. package/dist/solana-mobile-wallet-provider-B65A7abd.esm.js +0 -587
  67. package/dist/solana-mobile-wallet-provider-B65A7abd.esm.js.map +0 -1
@@ -0,0 +1,4994 @@
1
+ 'use strict';
2
+
3
+ var index_native = require('./index.native-ANhb4jEB.js');
4
+ require('axios');
5
+ require('@solana/web3.js');
6
+ require('@coral-xyz/anchor');
7
+ require('react');
8
+
9
+ /** Name of the feature. */
10
+ const SolanaSignAndSendTransaction = 'solana:signAndSendTransaction';
11
+
12
+ /** Name of the feature. */
13
+ const SolanaSignIn = 'solana:signIn';
14
+
15
+ /** Name of the feature. */
16
+ const SolanaSignMessage = 'solana:signMessage';
17
+
18
+ /** Name of the feature. */
19
+ const SolanaSignTransaction = 'solana:signTransaction';
20
+
21
+ function createDecoder(decoder) {
22
+ return Object.freeze({
23
+ ...decoder,
24
+ decode: (bytes, offset = 0) => decoder.read(bytes, offset)[0]
25
+ });
26
+ }
27
+
28
+ var getBaseXDecoder = (alphabet4) => {
29
+ return createDecoder({
30
+ read(rawBytes, offset) {
31
+ const bytes = offset === 0 || offset <= -rawBytes.byteLength ? rawBytes : rawBytes.slice(offset);
32
+ if (bytes.length === 0) return ["", 0];
33
+ let trailIndex = bytes.findIndex((n) => n !== 0);
34
+ trailIndex = trailIndex === -1 ? bytes.length : trailIndex;
35
+ const leadingZeroes = alphabet4[0].repeat(trailIndex);
36
+ if (trailIndex === bytes.length) return [leadingZeroes, rawBytes.length];
37
+ const base10Number = bytes.slice(trailIndex).reduce((sum, byte) => sum * 256n + BigInt(byte), 0n);
38
+ const tailChars = getBaseXFromBigInt(base10Number, alphabet4);
39
+ return [leadingZeroes + tailChars, rawBytes.length];
40
+ }
41
+ });
42
+ };
43
+ function getBaseXFromBigInt(value, alphabet4) {
44
+ const base = BigInt(alphabet4.length);
45
+ const tailChars = [];
46
+ while (value > 0n) {
47
+ tailChars.unshift(alphabet4[Number(value % base)]);
48
+ value /= base;
49
+ }
50
+ return tailChars.join("");
51
+ }
52
+
53
+ // src/base58.ts
54
+ var alphabet2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
55
+ var getBase58Decoder = () => getBaseXDecoder(alphabet2);
56
+
57
+ /** Solana Mainnet (beta) cluster, e.g. https://api.mainnet-beta.solana.com */
58
+ const SOLANA_MAINNET_CHAIN = 'solana:mainnet';
59
+
60
+ /**
61
+ * TODO: docs
62
+ */
63
+ function createSignInMessageText(input) {
64
+ // ${domain} wants you to sign in with your Solana account:
65
+ // ${address}
66
+ //
67
+ // ${statement}
68
+ //
69
+ // URI: ${uri}
70
+ // Version: ${version}
71
+ // Chain ID: ${chain}
72
+ // Nonce: ${nonce}
73
+ // Issued At: ${issued-at}
74
+ // Expiration Time: ${expiration-time}
75
+ // Not Before: ${not-before}
76
+ // Request ID: ${request-id}
77
+ // Resources:
78
+ // - ${resources[0]}
79
+ // - ${resources[1]}
80
+ // ...
81
+ // - ${resources[n]}
82
+ let message = `${input.domain} wants you to sign in with your Solana account:\n`;
83
+ message += `${input.address}`;
84
+ if (input.statement) {
85
+ message += `\n\n${input.statement}`;
86
+ }
87
+ const fields = [];
88
+ if (input.uri) {
89
+ fields.push(`URI: ${input.uri}`);
90
+ }
91
+ if (input.version) {
92
+ fields.push(`Version: ${input.version}`);
93
+ }
94
+ if (input.chainId) {
95
+ fields.push(`Chain ID: ${input.chainId}`);
96
+ }
97
+ if (input.nonce) {
98
+ fields.push(`Nonce: ${input.nonce}`);
99
+ }
100
+ if (input.issuedAt) {
101
+ fields.push(`Issued At: ${input.issuedAt}`);
102
+ }
103
+ if (input.expirationTime) {
104
+ fields.push(`Expiration Time: ${input.expirationTime}`);
105
+ }
106
+ if (input.notBefore) {
107
+ fields.push(`Not Before: ${input.notBefore}`);
108
+ }
109
+ if (input.requestId) {
110
+ fields.push(`Request ID: ${input.requestId}`);
111
+ }
112
+ if (input.resources) {
113
+ fields.push(`Resources:`);
114
+ for (const resource of input.resources) {
115
+ fields.push(`- ${resource}`);
116
+ }
117
+ }
118
+ if (fields.length) {
119
+ message += `\n\n${fields.join('\n')}`;
120
+ }
121
+ return message;
122
+ }
123
+
124
+ //#region src/errors.ts
125
+ const SolanaMobileWalletAdapterErrorCode = {
126
+ ERROR_ASSOCIATION_PORT_OUT_OF_RANGE: "ERROR_ASSOCIATION_PORT_OUT_OF_RANGE",
127
+ ERROR_REFLECTOR_ID_OUT_OF_RANGE: "ERROR_REFLECTOR_ID_OUT_OF_RANGE",
128
+ ERROR_FORBIDDEN_WALLET_BASE_URL: "ERROR_FORBIDDEN_WALLET_BASE_URL",
129
+ ERROR_SECURE_CONTEXT_REQUIRED: "ERROR_SECURE_CONTEXT_REQUIRED",
130
+ ERROR_SESSION_CLOSED: "ERROR_SESSION_CLOSED",
131
+ ERROR_SESSION_TIMEOUT: "ERROR_SESSION_TIMEOUT",
132
+ ERROR_WALLET_NOT_FOUND: "ERROR_WALLET_NOT_FOUND",
133
+ ERROR_INVALID_PROTOCOL_VERSION: "ERROR_INVALID_PROTOCOL_VERSION",
134
+ ERROR_BROWSER_NOT_SUPPORTED: "ERROR_BROWSER_NOT_SUPPORTED",
135
+ ERROR_LOOPBACK_ACCESS_BLOCKED: "ERROR_LOOPBACK_ACCESS_BLOCKED",
136
+ ERROR_ASSOCIATION_CANCELLED: "ERROR_ASSOCIATION_CANCELLED"
137
+ };
138
+ var SolanaMobileWalletAdapterError = class extends Error {
139
+ data;
140
+ code;
141
+ constructor(...args) {
142
+ const [code, message, data] = args;
143
+ super(message);
144
+ this.code = code;
145
+ this.data = data;
146
+ this.name = "SolanaMobileWalletAdapterError";
147
+ }
148
+ };
149
+ var SolanaMobileWalletAdapterProtocolError = class extends Error {
150
+ data;
151
+ code;
152
+ jsonRpcMessageId;
153
+ constructor(...args) {
154
+ const [jsonRpcMessageId, code, message, data] = args;
155
+ super(message);
156
+ this.code = code;
157
+ this.data = data;
158
+ this.jsonRpcMessageId = jsonRpcMessageId;
159
+ this.name = "SolanaMobileWalletAdapterProtocolError";
160
+ }
161
+ };
162
+ //#endregion
163
+ //#region src/base64Utils.ts
164
+ function encode(input) {
165
+ return window.btoa(input);
166
+ }
167
+ function fromUint8Array$1(byteArray, urlsafe) {
168
+ const base64 = window.btoa(String.fromCharCode.call(null, ...byteArray));
169
+ return base64;
170
+ }
171
+ function toUint8Array$1(base64EncodedByteArray) {
172
+ return new Uint8Array(window.atob(base64EncodedByteArray).split("").map((c) => c.charCodeAt(0)));
173
+ }
174
+ //#endregion
175
+ //#region src/createHelloReq.ts
176
+ async function createHelloReq(ecdhPublicKey, associationKeypairPrivateKey) {
177
+ const publicKeyBuffer = await crypto.subtle.exportKey("raw", ecdhPublicKey);
178
+ const signatureBuffer = await crypto.subtle.sign({
179
+ hash: "SHA-256",
180
+ name: "ECDSA"
181
+ }, 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
+ //#endregion
188
+ //#region src/base58Utils.ts
189
+ function fromUint8Array$2(byteArray) {
190
+ return getBase58Decoder().decode(byteArray);
191
+ }
192
+ function base64ToBase58(base64EncodedString) {
193
+ return fromUint8Array$2(toUint8Array$1(base64EncodedString));
194
+ }
195
+ //#endregion
196
+ //#region src/createSIWSMessage.ts
197
+ function createSIWSMessage(payload) {
198
+ return createSignInMessageText(payload);
199
+ }
200
+ function createSIWSMessageBase64Url(payload) {
201
+ return encode(createSIWSMessage(payload)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
202
+ }
203
+ //#endregion
204
+ //#region src/types.ts
205
+ const SolanaSignTransactions = "solana:signTransactions";
206
+ const SolanaCloneAuthorization = "solana:cloneAuthorization";
207
+ //#endregion
208
+ //#region src/createMobileWalletProxy.ts
209
+ /**
210
+ * Creates a {@link MobileWallet} proxy that handles backwards compatibility and API to RPC conversion.
211
+ *
212
+ * @param protocolVersion the protocol version in use for this session/request
213
+ * @param protocolRequestHandler callback function that handles sending the RPC request to the wallet endpoint.
214
+ * @returns a {@link MobileWallet} proxy
215
+ */
216
+ function createMobileWalletProxy(protocolVersion, protocolRequestHandler) {
217
+ return new Proxy({}, {
218
+ get(target, p) {
219
+ if (p === "then") return null;
220
+ if (target[p] == null) target[p] = async function(inputParams) {
221
+ const { method, params } = handleMobileWalletRequest(p, inputParams, protocolVersion);
222
+ const result = await protocolRequestHandler(method, params);
223
+ if (method === "authorize" && params.sign_in_payload && !result.sign_in_result) result.sign_in_result = await signInFallback(params.sign_in_payload, result, protocolRequestHandler);
224
+ return handleMobileWalletResponse(p, result, protocolVersion);
225
+ };
226
+ return target[p];
227
+ },
228
+ defineProperty() {
229
+ return false;
230
+ },
231
+ deleteProperty() {
232
+ return false;
233
+ }
234
+ });
235
+ }
236
+ /**
237
+ * Handles all {@link MobileWallet} API requests and determines the correct MWA RPC method and params to call.
238
+ * This handles backwards compatibility, based on the provided @protocolVersion.
239
+ *
240
+ * @param methodName the name of {@link MobileWallet} method that was called
241
+ * @param methodParams the parameters that were passed to the method
242
+ * @param protocolVersion the protocol version in use for this session/request
243
+ * @returns the RPC request method and params that should be sent to the wallet endpoint
244
+ */
245
+ function handleMobileWalletRequest(methodName, methodParams, protocolVersion) {
246
+ let params = methodParams;
247
+ let method = methodName.toString().replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`).toLowerCase();
248
+ switch (methodName) {
249
+ case "authorize": {
250
+ const authorizeParams = params;
251
+ let { chain } = authorizeParams;
252
+ if (protocolVersion === "legacy") {
253
+ switch (chain) {
254
+ case "solana:testnet":
255
+ chain = "testnet";
256
+ break;
257
+ case "solana:devnet":
258
+ chain = "devnet";
259
+ break;
260
+ case "solana:mainnet":
261
+ chain = "mainnet-beta";
262
+ break;
263
+ default: chain = authorizeParams.cluster;
264
+ }
265
+ authorizeParams.cluster = chain;
266
+ params = authorizeParams;
267
+ } else {
268
+ switch (chain) {
269
+ case "testnet":
270
+ case "devnet":
271
+ chain = `solana:${chain}`;
272
+ break;
273
+ case "mainnet-beta":
274
+ chain = "solana:mainnet";
275
+ break;
276
+ }
277
+ authorizeParams.chain = chain;
278
+ params = authorizeParams;
279
+ }
280
+ }
281
+ case "reauthorize": {
282
+ const { auth_token, identity } = params;
283
+ if (auth_token) switch (protocolVersion) {
284
+ case "legacy":
285
+ method = "reauthorize";
286
+ params = {
287
+ auth_token,
288
+ identity
289
+ };
290
+ break;
291
+ default:
292
+ method = "authorize";
293
+ break;
294
+ }
295
+ break;
296
+ }
297
+ }
298
+ return {
299
+ method,
300
+ params
301
+ };
302
+ }
303
+ /**
304
+ * Handles all {@link MobileWallet} API responses and modifies the response for backwards compatibility, if needed
305
+ *
306
+ * @param method the {@link MobileWallet} method that was called
307
+ * @param response the original response that was returned by the method call
308
+ * @param protocolVersion the protocol version in use for this session/request
309
+ * @returns the possibly modified response
310
+ */
311
+ function handleMobileWalletResponse(method, response, protocolVersion) {
312
+ switch (method) {
313
+ case "getCapabilities": {
314
+ const capabilities = response;
315
+ switch (protocolVersion) {
316
+ case "legacy": {
317
+ const features = [SolanaSignTransactions];
318
+ if (capabilities.supports_clone_authorization === true) features.push(SolanaCloneAuthorization);
319
+ return {
320
+ ...capabilities,
321
+ features
322
+ };
323
+ }
324
+ case "v1": return {
325
+ ...capabilities,
326
+ supports_sign_and_send_transactions: true,
327
+ supports_clone_authorization: capabilities.features.includes(SolanaCloneAuthorization)
328
+ };
329
+ }
330
+ }
331
+ }
332
+ return response;
333
+ }
334
+ async function signInFallback(signInPayload, authorizationResult, protocolRequestHandler) {
335
+ const domain = signInPayload.domain ?? window.location.host;
336
+ const address = authorizationResult.accounts[0].address;
337
+ const siwsMessage = createSIWSMessageBase64Url({
338
+ ...signInPayload,
339
+ domain,
340
+ address: base64ToBase58(address)
341
+ });
342
+ const signedPayload = toUint8Array$1((await protocolRequestHandler("sign_messages", {
343
+ addresses: [address],
344
+ payloads: [siwsMessage]
345
+ })).signed_payloads[0]);
346
+ const signedMessage = fromUint8Array$1(signedPayload.slice(0, signedPayload.length - 64));
347
+ const signature = fromUint8Array$1(signedPayload.slice(signedPayload.length - 64));
348
+ return {
349
+ address,
350
+ signed_message: signedMessage.length == 0 ? siwsMessage : signedMessage,
351
+ signature
352
+ };
353
+ }
354
+ function createSequenceNumberVector(sequenceNumber) {
355
+ if (sequenceNumber >= 4294967296) throw new Error("Outbound sequence number overflow. The maximum sequence number is 32-bytes.");
356
+ const byteArray = /* @__PURE__ */ new ArrayBuffer(4);
357
+ new DataView(byteArray).setUint32(0, sequenceNumber, false);
358
+ return new Uint8Array(byteArray);
359
+ }
360
+ //#endregion
361
+ //#region src/encryptedMessage.ts
362
+ const INITIALIZATION_VECTOR_BYTES = 12;
363
+ async function encryptMessage(plaintext, sequenceNumber, sharedSecret) {
364
+ const sequenceNumberVector = createSequenceNumberVector(sequenceNumber);
365
+ const initializationVector = new Uint8Array(INITIALIZATION_VECTOR_BYTES);
366
+ crypto.getRandomValues(initializationVector);
367
+ const ciphertext = await crypto.subtle.encrypt(getAlgorithmParams(sequenceNumberVector, initializationVector), sharedSecret, new TextEncoder().encode(plaintext));
368
+ const response = new Uint8Array(sequenceNumberVector.byteLength + initializationVector.byteLength + ciphertext.byteLength);
369
+ response.set(new Uint8Array(sequenceNumberVector), 0);
370
+ response.set(new Uint8Array(initializationVector), sequenceNumberVector.byteLength);
371
+ response.set(new Uint8Array(ciphertext), sequenceNumberVector.byteLength + initializationVector.byteLength);
372
+ return response;
373
+ }
374
+ async function decryptMessage(message, sharedSecret) {
375
+ const sequenceNumberVector = message.slice(0, 4);
376
+ const initializationVector = message.slice(4, 4 + INITIALIZATION_VECTOR_BYTES);
377
+ const ciphertext = message.slice(4 + INITIALIZATION_VECTOR_BYTES);
378
+ const plaintextBuffer = await crypto.subtle.decrypt(getAlgorithmParams(sequenceNumberVector, initializationVector), sharedSecret, ciphertext);
379
+ return getUtf8Decoder().decode(plaintextBuffer);
380
+ }
381
+ function getAlgorithmParams(sequenceNumber, initializationVector) {
382
+ return {
383
+ additionalData: sequenceNumber,
384
+ iv: initializationVector,
385
+ name: "AES-GCM",
386
+ tagLength: 128
387
+ };
388
+ }
389
+ let _utf8Decoder;
390
+ function getUtf8Decoder() {
391
+ if (_utf8Decoder === void 0) _utf8Decoder = new TextDecoder("utf-8");
392
+ return _utf8Decoder;
393
+ }
394
+ //#endregion
395
+ //#region src/generateAssociationKeypair.ts
396
+ async function generateAssociationKeypair() {
397
+ return await crypto.subtle.generateKey({
398
+ name: "ECDSA",
399
+ namedCurve: "P-256"
400
+ }, false, ["sign"]);
401
+ }
402
+ //#endregion
403
+ //#region src/generateECDHKeypair.ts
404
+ async function generateECDHKeypair() {
405
+ return await crypto.subtle.generateKey({
406
+ name: "ECDH",
407
+ namedCurve: "P-256"
408
+ }, false, ["deriveKey", "deriveBits"]);
409
+ }
410
+ //#endregion
411
+ //#region src/arrayBufferToBase64String.ts
412
+ function arrayBufferToBase64String(buffer) {
413
+ let binary = "";
414
+ const bytes = new Uint8Array(buffer);
415
+ const len = bytes.byteLength;
416
+ for (let ii = 0; ii < len; ii++) binary += String.fromCharCode(bytes[ii]);
417
+ return window.btoa(binary);
418
+ }
419
+ //#endregion
420
+ //#region src/associationPort.ts
421
+ function getRandomAssociationPort() {
422
+ return assertAssociationPort(49152 + Math.floor(Math.random() * 16384));
423
+ }
424
+ function assertAssociationPort(port) {
425
+ if (port < 49152 || port > 65535) throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_ASSOCIATION_PORT_OUT_OF_RANGE, `Association port number must be between 49152 and 65535. ${port} given.`, { port });
426
+ return port;
427
+ }
428
+ //#endregion
429
+ //#region src/getStringWithURLUnsafeBase64CharactersReplaced.ts
430
+ function getStringWithURLUnsafeCharactersReplaced(unsafeBase64EncodedString) {
431
+ return unsafeBase64EncodedString.replace(/[/+=]/g, (m) => ({
432
+ "/": "_",
433
+ "+": "-",
434
+ "=": "."
435
+ })[m]);
436
+ }
437
+ //#endregion
438
+ //#region src/getAssociateAndroidIntentURL.ts
439
+ const INTENT_NAME = "solana-wallet";
440
+ function getPathParts(pathString) {
441
+ return pathString.replace(/(^\/+|\/+$)/g, "").split("/");
442
+ }
443
+ function getIntentURL(methodPathname, intentUrlBase) {
444
+ let baseUrl = null;
445
+ if (intentUrlBase) {
446
+ try {
447
+ baseUrl = new URL(intentUrlBase);
448
+ } catch {}
449
+ if (baseUrl?.protocol !== "https:") throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL, "Base URLs supplied by wallets must be valid `https` URLs");
450
+ }
451
+ baseUrl ||= new URL(`${INTENT_NAME}:/`);
452
+ const pathname = methodPathname.startsWith("/") ? methodPathname : [...getPathParts(baseUrl.pathname), ...getPathParts(methodPathname)].join("/");
453
+ return new URL(pathname, baseUrl);
454
+ }
455
+ async function getAssociateAndroidIntentURL(associationPublicKey, putativePort, associationURLBase, protocolVersions = ["v1"]) {
456
+ const associationPort = assertAssociationPort(putativePort);
457
+ const encodedKey = arrayBufferToBase64String(await crypto.subtle.exportKey("raw", associationPublicKey));
458
+ const url = getIntentURL("v1/associate/local", associationURLBase);
459
+ url.searchParams.set("association", getStringWithURLUnsafeCharactersReplaced(encodedKey));
460
+ url.searchParams.set("port", `${associationPort}`);
461
+ protocolVersions.forEach((version) => {
462
+ url.searchParams.set("v", version);
463
+ });
464
+ return url;
465
+ }
466
+ //#endregion
467
+ //#region src/jsonRpcMessage.ts
468
+ async function encryptJsonRpcMessage(jsonRpcMessage, sharedSecret) {
469
+ const plaintext = JSON.stringify(jsonRpcMessage);
470
+ const sequenceNumber = jsonRpcMessage.id;
471
+ return encryptMessage(plaintext, sequenceNumber, sharedSecret);
472
+ }
473
+ async function decryptJsonRpcMessage(message, sharedSecret) {
474
+ const plaintext = await decryptMessage(message, sharedSecret);
475
+ const jsonRpcMessage = JSON.parse(plaintext);
476
+ if (Object.hasOwnProperty.call(jsonRpcMessage, "error")) throw new SolanaMobileWalletAdapterProtocolError(jsonRpcMessage.id, jsonRpcMessage.error.code, jsonRpcMessage.error.message);
477
+ return jsonRpcMessage;
478
+ }
479
+ //#endregion
480
+ //#region src/parseHelloRsp.ts
481
+ async function parseHelloRsp(payloadBuffer, associationPublicKey, ecdhPrivateKey) {
482
+ const [associationPublicKeyBuffer, walletPublicKey] = await Promise.all([crypto.subtle.exportKey("raw", associationPublicKey), crypto.subtle.importKey("raw", payloadBuffer.slice(0, 65), {
483
+ name: "ECDH",
484
+ namedCurve: "P-256"
485
+ }, false, [])]);
486
+ const sharedSecret = await crypto.subtle.deriveBits({
487
+ name: "ECDH",
488
+ public: walletPublicKey
489
+ }, ecdhPrivateKey, 256);
490
+ const ecdhSecretKey = await crypto.subtle.importKey("raw", sharedSecret, "HKDF", false, ["deriveKey"]);
491
+ return await crypto.subtle.deriveKey({
492
+ name: "HKDF",
493
+ hash: "SHA-256",
494
+ salt: new Uint8Array(associationPublicKeyBuffer),
495
+ info: new Uint8Array()
496
+ }, ecdhSecretKey, {
497
+ name: "AES-GCM",
498
+ length: 128
499
+ }, false, ["encrypt", "decrypt"]);
500
+ }
501
+ //#endregion
502
+ //#region src/parseSessionProps.ts
503
+ async function parseSessionProps(message, sharedSecret) {
504
+ const plaintext = await decryptMessage(message, sharedSecret);
505
+ const jsonProperties = JSON.parse(plaintext);
506
+ let protocolVersion = "legacy";
507
+ if (Object.hasOwnProperty.call(jsonProperties, "v")) switch (jsonProperties.v) {
508
+ case 1:
509
+ case "1":
510
+ case "v1":
511
+ protocolVersion = "v1";
512
+ break;
513
+ case "legacy":
514
+ protocolVersion = "legacy";
515
+ break;
516
+ default: throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_INVALID_PROTOCOL_VERSION, `Unknown/unsupported protocol version: ${jsonProperties.v}`);
517
+ }
518
+ return { protocol_version: protocolVersion };
519
+ }
520
+ //#endregion
521
+ //#region src/startSession.ts
522
+ const Browser = {
523
+ Firefox: 0,
524
+ Other: 1
525
+ };
526
+ function assertUnreachable(x) {
527
+ return x;
528
+ }
529
+ function getBrowser() {
530
+ return navigator.userAgent.indexOf("Firefox/") !== -1 ? Browser.Firefox : Browser.Other;
531
+ }
532
+ function getDetectionPromise() {
533
+ return new Promise((resolve, reject) => {
534
+ function cleanup() {
535
+ clearTimeout(timeoutId);
536
+ window.removeEventListener("blur", handleBlur);
537
+ }
538
+ function handleBlur() {
539
+ cleanup();
540
+ resolve();
541
+ }
542
+ window.addEventListener("blur", handleBlur);
543
+ const timeoutId = setTimeout(() => {
544
+ cleanup();
545
+ reject();
546
+ }, 3e3);
547
+ });
548
+ }
549
+ let _frame = null;
550
+ function launchUrlThroughHiddenFrame(url) {
551
+ if (_frame == null) {
552
+ _frame = document.createElement("iframe");
553
+ _frame.style.display = "none";
554
+ document.body.appendChild(_frame);
555
+ }
556
+ _frame.contentWindow.location.href = url.toString();
557
+ }
558
+ async function launchAssociation(associationUrl) {
559
+ if (associationUrl.protocol === "https:") window.location.assign(associationUrl);
560
+ else try {
561
+ const browser = getBrowser();
562
+ switch (browser) {
563
+ case Browser.Firefox:
564
+ launchUrlThroughHiddenFrame(associationUrl);
565
+ break;
566
+ case Browser.Other: {
567
+ const detectionPromise = getDetectionPromise();
568
+ window.location.assign(associationUrl);
569
+ await detectionPromise;
570
+ break;
571
+ }
572
+ default: assertUnreachable(browser);
573
+ }
574
+ } catch {
575
+ throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_WALLET_NOT_FOUND, "Found no installed wallet that supports the mobile wallet protocol.");
576
+ }
577
+ }
578
+ async function startSession(associationPublicKey, associationURLBase) {
579
+ const randomAssociationPort = getRandomAssociationPort();
580
+ await launchAssociation(await getAssociateAndroidIntentURL(associationPublicKey, randomAssociationPort, associationURLBase));
581
+ return randomAssociationPort;
582
+ }
583
+ //#endregion
584
+ //#region src/transact.ts
585
+ const WEBSOCKET_CONNECTION_CONFIG = {
586
+ retryDelayScheduleMs: [
587
+ 150,
588
+ 150,
589
+ 200,
590
+ 500,
591
+ 500,
592
+ 750,
593
+ 750,
594
+ 1e3
595
+ ],
596
+ timeoutMs: 3e4
597
+ };
598
+ const WEBSOCKET_PROTOCOL_BINARY = "com.solana.mobilewalletadapter.v1";
599
+ function assertSecureContext() {
600
+ if (typeof window === "undefined" || window.isSecureContext !== true) throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SECURE_CONTEXT_REQUIRED, "The mobile wallet adapter protocol must be used in a secure context (`https`).");
601
+ }
602
+ function assertSecureEndpointSpecificURI(walletUriBase) {
603
+ let url;
604
+ try {
605
+ url = new URL(walletUriBase);
606
+ } catch {
607
+ throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL, "Invalid base URL supplied by wallet");
608
+ }
609
+ if (url.protocol !== "https:") throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_FORBIDDEN_WALLET_BASE_URL, "Base URLs supplied by wallets must be valid `https` URLs");
610
+ }
611
+ function getSequenceNumberFromByteArray(byteArray) {
612
+ return new DataView(byteArray).getUint32(0, false);
613
+ }
614
+ async function startScenario(config) {
615
+ assertSecureContext();
616
+ const associationKeypair = await generateAssociationKeypair();
617
+ const websocketURL = `ws://localhost:${await startSession(associationKeypair.publicKey, config?.baseUri)}/solana-wallet`;
618
+ let connectionStartTime;
619
+ const getNextRetryDelayMs = (() => {
620
+ const schedule = [...WEBSOCKET_CONNECTION_CONFIG.retryDelayScheduleMs];
621
+ return () => schedule.length > 1 ? schedule.shift() : schedule[0];
622
+ })();
623
+ let nextJsonRpcMessageId = 1;
624
+ let lastKnownInboundSequenceNumber = 0;
625
+ let state = { __type: "disconnected" };
626
+ let socket;
627
+ let sessionEstablished = false;
628
+ let handleForceClose;
629
+ return {
630
+ close: () => {
631
+ socket.close();
632
+ handleForceClose();
633
+ },
634
+ wallet: new Promise((resolve, reject) => {
635
+ const jsonRpcResponsePromises = {};
636
+ const handleOpen = async () => {
637
+ if (state.__type !== "connecting") {
638
+ console.warn(`Expected adapter state to be \`connecting\` at the moment the websocket opens. Got \`${state.__type}\`.`);
639
+ return;
640
+ }
641
+ socket.removeEventListener("open", handleOpen);
642
+ const { associationKeypair } = state;
643
+ const ecdhKeypair = await generateECDHKeypair();
644
+ socket.send(await createHelloReq(ecdhKeypair.publicKey, associationKeypair.privateKey));
645
+ state = {
646
+ __type: "hello_req_sent",
647
+ associationPublicKey: associationKeypair.publicKey,
648
+ ecdhPrivateKey: ecdhKeypair.privateKey
649
+ };
650
+ };
651
+ const handleClose = (evt) => {
652
+ if (evt.wasClean) state = { __type: "disconnected" };
653
+ else reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED, `The wallet session dropped unexpectedly (${evt.code}: ${evt.reason}).`, { closeEvent: evt }));
654
+ disposeSocket();
655
+ };
656
+ const handleError = async (_evt) => {
657
+ disposeSocket();
658
+ if (Date.now() - connectionStartTime >= WEBSOCKET_CONNECTION_CONFIG.timeoutMs) reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_TIMEOUT, `Failed to connect to the wallet websocket at ${websocketURL}.`));
659
+ else {
660
+ await new Promise((resolve) => {
661
+ const retryDelayMs = getNextRetryDelayMs();
662
+ retryWaitTimeoutId = window.setTimeout(resolve, retryDelayMs);
663
+ });
664
+ attemptSocketConnection();
665
+ }
666
+ };
667
+ const handleMessage = async (evt) => {
668
+ const responseBuffer = await evt.data.arrayBuffer();
669
+ switch (state.__type) {
670
+ case "connecting": {
671
+ if (responseBuffer.byteLength !== 0) throw new Error("Encountered unexpected message while connecting");
672
+ const ecdhKeypair = await generateECDHKeypair();
673
+ socket.send(await createHelloReq(ecdhKeypair.publicKey, associationKeypair.privateKey));
674
+ state = {
675
+ __type: "hello_req_sent",
676
+ associationPublicKey: associationKeypair.publicKey,
677
+ ecdhPrivateKey: ecdhKeypair.privateKey
678
+ };
679
+ break;
680
+ }
681
+ case "connected":
682
+ try {
683
+ const sequenceNumber = getSequenceNumberFromByteArray(responseBuffer.slice(0, 4));
684
+ if (sequenceNumber !== lastKnownInboundSequenceNumber + 1) throw new Error("Encrypted message has invalid sequence number");
685
+ lastKnownInboundSequenceNumber = sequenceNumber;
686
+ const jsonRpcMessage = await decryptJsonRpcMessage(responseBuffer, state.sharedSecret);
687
+ const responsePromise = jsonRpcResponsePromises[jsonRpcMessage.id];
688
+ delete jsonRpcResponsePromises[jsonRpcMessage.id];
689
+ responsePromise.resolve(jsonRpcMessage.result);
690
+ } catch (e) {
691
+ if (e instanceof SolanaMobileWalletAdapterProtocolError) {
692
+ const responsePromise = jsonRpcResponsePromises[e.jsonRpcMessageId];
693
+ delete jsonRpcResponsePromises[e.jsonRpcMessageId];
694
+ responsePromise.reject(e);
695
+ } else throw e;
696
+ }
697
+ break;
698
+ case "hello_req_sent": {
699
+ if (responseBuffer.byteLength === 0) {
700
+ const ecdhKeypair = await generateECDHKeypair();
701
+ socket.send(await createHelloReq(ecdhKeypair.publicKey, associationKeypair.privateKey));
702
+ state = {
703
+ __type: "hello_req_sent",
704
+ associationPublicKey: associationKeypair.publicKey,
705
+ ecdhPrivateKey: ecdhKeypair.privateKey
706
+ };
707
+ break;
708
+ }
709
+ const sharedSecret = await parseHelloRsp(responseBuffer, state.associationPublicKey, state.ecdhPrivateKey);
710
+ const sessionPropertiesBuffer = responseBuffer.slice(65);
711
+ const sessionProperties = sessionPropertiesBuffer.byteLength !== 0 ? await (async () => {
712
+ const sequenceNumber = getSequenceNumberFromByteArray(sessionPropertiesBuffer.slice(0, 4));
713
+ if (sequenceNumber !== lastKnownInboundSequenceNumber + 1) throw new Error("Encrypted message has invalid sequence number");
714
+ lastKnownInboundSequenceNumber = sequenceNumber;
715
+ return parseSessionProps(sessionPropertiesBuffer, sharedSecret);
716
+ })() : { protocol_version: "legacy" };
717
+ state = {
718
+ __type: "connected",
719
+ sharedSecret,
720
+ sessionProperties
721
+ };
722
+ const wallet = createMobileWalletProxy(sessionProperties.protocol_version, async (method, params) => {
723
+ const id = nextJsonRpcMessageId++;
724
+ socket.send(await encryptJsonRpcMessage({
725
+ id,
726
+ jsonrpc: "2.0",
727
+ method,
728
+ params: params ?? {}
729
+ }, sharedSecret));
730
+ return new Promise((resolve, reject) => {
731
+ jsonRpcResponsePromises[id] = {
732
+ resolve(result) {
733
+ switch (method) {
734
+ case "authorize":
735
+ case "reauthorize": {
736
+ const { wallet_uri_base } = result;
737
+ if (wallet_uri_base != null) try {
738
+ assertSecureEndpointSpecificURI(wallet_uri_base);
739
+ } catch (e) {
740
+ reject(e);
741
+ return;
742
+ }
743
+ break;
744
+ }
745
+ }
746
+ resolve(result);
747
+ },
748
+ reject
749
+ };
750
+ });
751
+ });
752
+ sessionEstablished = true;
753
+ try {
754
+ resolve(wallet);
755
+ } catch (e) {
756
+ reject(e);
757
+ }
758
+ break;
759
+ }
760
+ }
761
+ };
762
+ handleForceClose = () => {
763
+ socket.removeEventListener("message", handleMessage);
764
+ disposeSocket();
765
+ if (!sessionEstablished) reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_SESSION_CLOSED, `The wallet session was closed before connection.`, { closeEvent: new CloseEvent("socket was closed before connection") }));
766
+ };
767
+ let disposeSocket;
768
+ let retryWaitTimeoutId;
769
+ const attemptSocketConnection = () => {
770
+ if (disposeSocket) disposeSocket();
771
+ state = {
772
+ __type: "connecting",
773
+ associationKeypair
774
+ };
775
+ if (connectionStartTime === void 0) connectionStartTime = Date.now();
776
+ socket = new WebSocket(websocketURL, [WEBSOCKET_PROTOCOL_BINARY]);
777
+ socket.addEventListener("open", handleOpen);
778
+ socket.addEventListener("close", handleClose);
779
+ socket.addEventListener("error", handleError);
780
+ socket.addEventListener("message", handleMessage);
781
+ disposeSocket = () => {
782
+ window.clearTimeout(retryWaitTimeoutId);
783
+ socket.removeEventListener("open", handleOpen);
784
+ socket.removeEventListener("close", handleClose);
785
+ socket.removeEventListener("error", handleError);
786
+ socket.removeEventListener("message", handleMessage);
787
+ };
788
+ };
789
+ attemptSocketConnection();
790
+ })
791
+ };
792
+ }
793
+
794
+ /** Name of the feature. */
795
+ const StandardConnect = 'standard:connect';
796
+
797
+ /** Name of the feature. */
798
+ const StandardDisconnect = 'standard:disconnect';
799
+
800
+ /** Name of the feature. */
801
+ const StandardEvents = 'standard:events';
802
+
803
+ var browser = {};
804
+
805
+ var canPromise;
806
+ var hasRequiredCanPromise;
807
+
808
+ function requireCanPromise () {
809
+ if (hasRequiredCanPromise) return canPromise;
810
+ hasRequiredCanPromise = 1;
811
+ // can-promise has a crash in some versions of react native that dont have
812
+ // standard global objects
813
+ // https://github.com/soldair/node-qrcode/issues/157
814
+
815
+ canPromise = function () {
816
+ return typeof Promise === 'function' && Promise.prototype && Promise.prototype.then
817
+ };
818
+ return canPromise;
819
+ }
820
+
821
+ var qrcode = {};
822
+
823
+ var utils$1 = {};
824
+
825
+ var hasRequiredUtils$1;
826
+
827
+ function requireUtils$1 () {
828
+ if (hasRequiredUtils$1) return utils$1;
829
+ hasRequiredUtils$1 = 1;
830
+ let toSJISFunction;
831
+ const CODEWORDS_COUNT = [
832
+ 0, // Not used
833
+ 26, 44, 70, 100, 134, 172, 196, 242, 292, 346,
834
+ 404, 466, 532, 581, 655, 733, 815, 901, 991, 1085,
835
+ 1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185,
836
+ 2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706
837
+ ];
838
+
839
+ /**
840
+ * Returns the QR Code size for the specified version
841
+ *
842
+ * @param {Number} version QR Code version
843
+ * @return {Number} size of QR code
844
+ */
845
+ utils$1.getSymbolSize = function getSymbolSize (version) {
846
+ if (!version) throw new Error('"version" cannot be null or undefined')
847
+ if (version < 1 || version > 40) throw new Error('"version" should be in range from 1 to 40')
848
+ return version * 4 + 17
849
+ };
850
+
851
+ /**
852
+ * Returns the total number of codewords used to store data and EC information.
853
+ *
854
+ * @param {Number} version QR Code version
855
+ * @return {Number} Data length in bits
856
+ */
857
+ utils$1.getSymbolTotalCodewords = function getSymbolTotalCodewords (version) {
858
+ return CODEWORDS_COUNT[version]
859
+ };
860
+
861
+ /**
862
+ * Encode data with Bose-Chaudhuri-Hocquenghem
863
+ *
864
+ * @param {Number} data Value to encode
865
+ * @return {Number} Encoded value
866
+ */
867
+ utils$1.getBCHDigit = function (data) {
868
+ let digit = 0;
869
+
870
+ while (data !== 0) {
871
+ digit++;
872
+ data >>>= 1;
873
+ }
874
+
875
+ return digit
876
+ };
877
+
878
+ utils$1.setToSJISFunction = function setToSJISFunction (f) {
879
+ if (typeof f !== 'function') {
880
+ throw new Error('"toSJISFunc" is not a valid function.')
881
+ }
882
+
883
+ toSJISFunction = f;
884
+ };
885
+
886
+ utils$1.isKanjiModeEnabled = function () {
887
+ return typeof toSJISFunction !== 'undefined'
888
+ };
889
+
890
+ utils$1.toSJIS = function toSJIS (kanji) {
891
+ return toSJISFunction(kanji)
892
+ };
893
+ return utils$1;
894
+ }
895
+
896
+ var errorCorrectionLevel = {};
897
+
898
+ var hasRequiredErrorCorrectionLevel;
899
+
900
+ function requireErrorCorrectionLevel () {
901
+ if (hasRequiredErrorCorrectionLevel) return errorCorrectionLevel;
902
+ hasRequiredErrorCorrectionLevel = 1;
903
+ (function (exports$1) {
904
+ exports$1.L = { bit: 1 };
905
+ exports$1.M = { bit: 0 };
906
+ exports$1.Q = { bit: 3 };
907
+ exports$1.H = { bit: 2 };
908
+
909
+ function fromString (string) {
910
+ if (typeof string !== 'string') {
911
+ throw new Error('Param is not a string')
912
+ }
913
+
914
+ const lcStr = string.toLowerCase();
915
+
916
+ switch (lcStr) {
917
+ case 'l':
918
+ case 'low':
919
+ return exports$1.L
920
+
921
+ case 'm':
922
+ case 'medium':
923
+ return exports$1.M
924
+
925
+ case 'q':
926
+ case 'quartile':
927
+ return exports$1.Q
928
+
929
+ case 'h':
930
+ case 'high':
931
+ return exports$1.H
932
+
933
+ default:
934
+ throw new Error('Unknown EC Level: ' + string)
935
+ }
936
+ }
937
+
938
+ exports$1.isValid = function isValid (level) {
939
+ return level && typeof level.bit !== 'undefined' &&
940
+ level.bit >= 0 && level.bit < 4
941
+ };
942
+
943
+ exports$1.from = function from (value, defaultValue) {
944
+ if (exports$1.isValid(value)) {
945
+ return value
946
+ }
947
+
948
+ try {
949
+ return fromString(value)
950
+ } catch (e) {
951
+ return defaultValue
952
+ }
953
+ };
954
+ } (errorCorrectionLevel));
955
+ return errorCorrectionLevel;
956
+ }
957
+
958
+ var bitBuffer;
959
+ var hasRequiredBitBuffer;
960
+
961
+ function requireBitBuffer () {
962
+ if (hasRequiredBitBuffer) return bitBuffer;
963
+ hasRequiredBitBuffer = 1;
964
+ function BitBuffer () {
965
+ this.buffer = [];
966
+ this.length = 0;
967
+ }
968
+
969
+ BitBuffer.prototype = {
970
+
971
+ get: function (index) {
972
+ const bufIndex = Math.floor(index / 8);
973
+ return ((this.buffer[bufIndex] >>> (7 - index % 8)) & 1) === 1
974
+ },
975
+
976
+ put: function (num, length) {
977
+ for (let i = 0; i < length; i++) {
978
+ this.putBit(((num >>> (length - i - 1)) & 1) === 1);
979
+ }
980
+ },
981
+
982
+ getLengthInBits: function () {
983
+ return this.length
984
+ },
985
+
986
+ putBit: function (bit) {
987
+ const bufIndex = Math.floor(this.length / 8);
988
+ if (this.buffer.length <= bufIndex) {
989
+ this.buffer.push(0);
990
+ }
991
+
992
+ if (bit) {
993
+ this.buffer[bufIndex] |= (0x80 >>> (this.length % 8));
994
+ }
995
+
996
+ this.length++;
997
+ }
998
+ };
999
+
1000
+ bitBuffer = BitBuffer;
1001
+ return bitBuffer;
1002
+ }
1003
+
1004
+ /**
1005
+ * Helper class to handle QR Code symbol modules
1006
+ *
1007
+ * @param {Number} size Symbol size
1008
+ */
1009
+
1010
+ var bitMatrix;
1011
+ var hasRequiredBitMatrix;
1012
+
1013
+ function requireBitMatrix () {
1014
+ if (hasRequiredBitMatrix) return bitMatrix;
1015
+ hasRequiredBitMatrix = 1;
1016
+ function BitMatrix (size) {
1017
+ if (!size || size < 1) {
1018
+ throw new Error('BitMatrix size must be defined and greater than 0')
1019
+ }
1020
+
1021
+ this.size = size;
1022
+ this.data = new Uint8Array(size * size);
1023
+ this.reservedBit = new Uint8Array(size * size);
1024
+ }
1025
+
1026
+ /**
1027
+ * Set bit value at specified location
1028
+ * If reserved flag is set, this bit will be ignored during masking process
1029
+ *
1030
+ * @param {Number} row
1031
+ * @param {Number} col
1032
+ * @param {Boolean} value
1033
+ * @param {Boolean} reserved
1034
+ */
1035
+ BitMatrix.prototype.set = function (row, col, value, reserved) {
1036
+ const index = row * this.size + col;
1037
+ this.data[index] = value;
1038
+ if (reserved) this.reservedBit[index] = true;
1039
+ };
1040
+
1041
+ /**
1042
+ * Returns bit value at specified location
1043
+ *
1044
+ * @param {Number} row
1045
+ * @param {Number} col
1046
+ * @return {Boolean}
1047
+ */
1048
+ BitMatrix.prototype.get = function (row, col) {
1049
+ return this.data[row * this.size + col]
1050
+ };
1051
+
1052
+ /**
1053
+ * Applies xor operator at specified location
1054
+ * (used during masking process)
1055
+ *
1056
+ * @param {Number} row
1057
+ * @param {Number} col
1058
+ * @param {Boolean} value
1059
+ */
1060
+ BitMatrix.prototype.xor = function (row, col, value) {
1061
+ this.data[row * this.size + col] ^= value;
1062
+ };
1063
+
1064
+ /**
1065
+ * Check if bit at specified location is reserved
1066
+ *
1067
+ * @param {Number} row
1068
+ * @param {Number} col
1069
+ * @return {Boolean}
1070
+ */
1071
+ BitMatrix.prototype.isReserved = function (row, col) {
1072
+ return this.reservedBit[row * this.size + col]
1073
+ };
1074
+
1075
+ bitMatrix = BitMatrix;
1076
+ return bitMatrix;
1077
+ }
1078
+
1079
+ var alignmentPattern = {};
1080
+
1081
+ /**
1082
+ * Alignment pattern are fixed reference pattern in defined positions
1083
+ * in a matrix symbology, which enables the decode software to re-synchronise
1084
+ * the coordinate mapping of the image modules in the event of moderate amounts
1085
+ * of distortion of the image.
1086
+ *
1087
+ * Alignment patterns are present only in QR Code symbols of version 2 or larger
1088
+ * and their number depends on the symbol version.
1089
+ */
1090
+
1091
+ var hasRequiredAlignmentPattern;
1092
+
1093
+ function requireAlignmentPattern () {
1094
+ if (hasRequiredAlignmentPattern) return alignmentPattern;
1095
+ hasRequiredAlignmentPattern = 1;
1096
+ (function (exports$1) {
1097
+ const getSymbolSize = requireUtils$1().getSymbolSize;
1098
+
1099
+ /**
1100
+ * Calculate the row/column coordinates of the center module of each alignment pattern
1101
+ * for the specified QR Code version.
1102
+ *
1103
+ * The alignment patterns are positioned symmetrically on either side of the diagonal
1104
+ * running from the top left corner of the symbol to the bottom right corner.
1105
+ *
1106
+ * Since positions are simmetrical only half of the coordinates are returned.
1107
+ * Each item of the array will represent in turn the x and y coordinate.
1108
+ * @see {@link getPositions}
1109
+ *
1110
+ * @param {Number} version QR Code version
1111
+ * @return {Array} Array of coordinate
1112
+ */
1113
+ exports$1.getRowColCoords = function getRowColCoords (version) {
1114
+ if (version === 1) return []
1115
+
1116
+ const posCount = Math.floor(version / 7) + 2;
1117
+ const size = getSymbolSize(version);
1118
+ const intervals = size === 145 ? 26 : Math.ceil((size - 13) / (2 * posCount - 2)) * 2;
1119
+ const positions = [size - 7]; // Last coord is always (size - 7)
1120
+
1121
+ for (let i = 1; i < posCount - 1; i++) {
1122
+ positions[i] = positions[i - 1] - intervals;
1123
+ }
1124
+
1125
+ positions.push(6); // First coord is always 6
1126
+
1127
+ return positions.reverse()
1128
+ };
1129
+
1130
+ /**
1131
+ * Returns an array containing the positions of each alignment pattern.
1132
+ * Each array's element represent the center point of the pattern as (x, y) coordinates
1133
+ *
1134
+ * Coordinates are calculated expanding the row/column coordinates returned by {@link getRowColCoords}
1135
+ * and filtering out the items that overlaps with finder pattern
1136
+ *
1137
+ * @example
1138
+ * For a Version 7 symbol {@link getRowColCoords} returns values 6, 22 and 38.
1139
+ * The alignment patterns, therefore, are to be centered on (row, column)
1140
+ * positions (6,22), (22,6), (22,22), (22,38), (38,22), (38,38).
1141
+ * Note that the coordinates (6,6), (6,38), (38,6) are occupied by finder patterns
1142
+ * and are not therefore used for alignment patterns.
1143
+ *
1144
+ * let pos = getPositions(7)
1145
+ * // [[6,22], [22,6], [22,22], [22,38], [38,22], [38,38]]
1146
+ *
1147
+ * @param {Number} version QR Code version
1148
+ * @return {Array} Array of coordinates
1149
+ */
1150
+ exports$1.getPositions = function getPositions (version) {
1151
+ const coords = [];
1152
+ const pos = exports$1.getRowColCoords(version);
1153
+ const posLength = pos.length;
1154
+
1155
+ for (let i = 0; i < posLength; i++) {
1156
+ for (let j = 0; j < posLength; j++) {
1157
+ // Skip if position is occupied by finder patterns
1158
+ if ((i === 0 && j === 0) || // top-left
1159
+ (i === 0 && j === posLength - 1) || // bottom-left
1160
+ (i === posLength - 1 && j === 0)) { // top-right
1161
+ continue
1162
+ }
1163
+
1164
+ coords.push([pos[i], pos[j]]);
1165
+ }
1166
+ }
1167
+
1168
+ return coords
1169
+ };
1170
+ } (alignmentPattern));
1171
+ return alignmentPattern;
1172
+ }
1173
+
1174
+ var finderPattern = {};
1175
+
1176
+ var hasRequiredFinderPattern;
1177
+
1178
+ function requireFinderPattern () {
1179
+ if (hasRequiredFinderPattern) return finderPattern;
1180
+ hasRequiredFinderPattern = 1;
1181
+ const getSymbolSize = requireUtils$1().getSymbolSize;
1182
+ const FINDER_PATTERN_SIZE = 7;
1183
+
1184
+ /**
1185
+ * Returns an array containing the positions of each finder pattern.
1186
+ * Each array's element represent the top-left point of the pattern as (x, y) coordinates
1187
+ *
1188
+ * @param {Number} version QR Code version
1189
+ * @return {Array} Array of coordinates
1190
+ */
1191
+ finderPattern.getPositions = function getPositions (version) {
1192
+ const size = getSymbolSize(version);
1193
+
1194
+ return [
1195
+ // top-left
1196
+ [0, 0],
1197
+ // top-right
1198
+ [size - FINDER_PATTERN_SIZE, 0],
1199
+ // bottom-left
1200
+ [0, size - FINDER_PATTERN_SIZE]
1201
+ ]
1202
+ };
1203
+ return finderPattern;
1204
+ }
1205
+
1206
+ var maskPattern = {};
1207
+
1208
+ /**
1209
+ * Data mask pattern reference
1210
+ * @type {Object}
1211
+ */
1212
+
1213
+ var hasRequiredMaskPattern;
1214
+
1215
+ function requireMaskPattern () {
1216
+ if (hasRequiredMaskPattern) return maskPattern;
1217
+ hasRequiredMaskPattern = 1;
1218
+ (function (exports$1) {
1219
+ exports$1.Patterns = {
1220
+ PATTERN000: 0,
1221
+ PATTERN001: 1,
1222
+ PATTERN010: 2,
1223
+ PATTERN011: 3,
1224
+ PATTERN100: 4,
1225
+ PATTERN101: 5,
1226
+ PATTERN110: 6,
1227
+ PATTERN111: 7
1228
+ };
1229
+
1230
+ /**
1231
+ * Weighted penalty scores for the undesirable features
1232
+ * @type {Object}
1233
+ */
1234
+ const PenaltyScores = {
1235
+ N1: 3,
1236
+ N2: 3,
1237
+ N3: 40,
1238
+ N4: 10
1239
+ };
1240
+
1241
+ /**
1242
+ * Check if mask pattern value is valid
1243
+ *
1244
+ * @param {Number} mask Mask pattern
1245
+ * @return {Boolean} true if valid, false otherwise
1246
+ */
1247
+ exports$1.isValid = function isValid (mask) {
1248
+ return mask != null && mask !== '' && !isNaN(mask) && mask >= 0 && mask <= 7
1249
+ };
1250
+
1251
+ /**
1252
+ * Returns mask pattern from a value.
1253
+ * If value is not valid, returns undefined
1254
+ *
1255
+ * @param {Number|String} value Mask pattern value
1256
+ * @return {Number} Valid mask pattern or undefined
1257
+ */
1258
+ exports$1.from = function from (value) {
1259
+ return exports$1.isValid(value) ? parseInt(value, 10) : undefined
1260
+ };
1261
+
1262
+ /**
1263
+ * Find adjacent modules in row/column with the same color
1264
+ * and assign a penalty value.
1265
+ *
1266
+ * Points: N1 + i
1267
+ * i is the amount by which the number of adjacent modules of the same color exceeds 5
1268
+ */
1269
+ exports$1.getPenaltyN1 = function getPenaltyN1 (data) {
1270
+ const size = data.size;
1271
+ let points = 0;
1272
+ let sameCountCol = 0;
1273
+ let sameCountRow = 0;
1274
+ let lastCol = null;
1275
+ let lastRow = null;
1276
+
1277
+ for (let row = 0; row < size; row++) {
1278
+ sameCountCol = sameCountRow = 0;
1279
+ lastCol = lastRow = null;
1280
+
1281
+ for (let col = 0; col < size; col++) {
1282
+ let module = data.get(row, col);
1283
+ if (module === lastCol) {
1284
+ sameCountCol++;
1285
+ } else {
1286
+ if (sameCountCol >= 5) points += PenaltyScores.N1 + (sameCountCol - 5);
1287
+ lastCol = module;
1288
+ sameCountCol = 1;
1289
+ }
1290
+
1291
+ module = data.get(col, row);
1292
+ if (module === lastRow) {
1293
+ sameCountRow++;
1294
+ } else {
1295
+ if (sameCountRow >= 5) points += PenaltyScores.N1 + (sameCountRow - 5);
1296
+ lastRow = module;
1297
+ sameCountRow = 1;
1298
+ }
1299
+ }
1300
+
1301
+ if (sameCountCol >= 5) points += PenaltyScores.N1 + (sameCountCol - 5);
1302
+ if (sameCountRow >= 5) points += PenaltyScores.N1 + (sameCountRow - 5);
1303
+ }
1304
+
1305
+ return points
1306
+ };
1307
+
1308
+ /**
1309
+ * Find 2x2 blocks with the same color and assign a penalty value
1310
+ *
1311
+ * Points: N2 * (m - 1) * (n - 1)
1312
+ */
1313
+ exports$1.getPenaltyN2 = function getPenaltyN2 (data) {
1314
+ const size = data.size;
1315
+ let points = 0;
1316
+
1317
+ for (let row = 0; row < size - 1; row++) {
1318
+ for (let col = 0; col < size - 1; col++) {
1319
+ const last = data.get(row, col) +
1320
+ data.get(row, col + 1) +
1321
+ data.get(row + 1, col) +
1322
+ data.get(row + 1, col + 1);
1323
+
1324
+ if (last === 4 || last === 0) points++;
1325
+ }
1326
+ }
1327
+
1328
+ return points * PenaltyScores.N2
1329
+ };
1330
+
1331
+ /**
1332
+ * Find 1:1:3:1:1 ratio (dark:light:dark:light:dark) pattern in row/column,
1333
+ * preceded or followed by light area 4 modules wide
1334
+ *
1335
+ * Points: N3 * number of pattern found
1336
+ */
1337
+ exports$1.getPenaltyN3 = function getPenaltyN3 (data) {
1338
+ const size = data.size;
1339
+ let points = 0;
1340
+ let bitsCol = 0;
1341
+ let bitsRow = 0;
1342
+
1343
+ for (let row = 0; row < size; row++) {
1344
+ bitsCol = bitsRow = 0;
1345
+ for (let col = 0; col < size; col++) {
1346
+ bitsCol = ((bitsCol << 1) & 0x7FF) | data.get(row, col);
1347
+ if (col >= 10 && (bitsCol === 0x5D0 || bitsCol === 0x05D)) points++;
1348
+
1349
+ bitsRow = ((bitsRow << 1) & 0x7FF) | data.get(col, row);
1350
+ if (col >= 10 && (bitsRow === 0x5D0 || bitsRow === 0x05D)) points++;
1351
+ }
1352
+ }
1353
+
1354
+ return points * PenaltyScores.N3
1355
+ };
1356
+
1357
+ /**
1358
+ * Calculate proportion of dark modules in entire symbol
1359
+ *
1360
+ * Points: N4 * k
1361
+ *
1362
+ * k is the rating of the deviation of the proportion of dark modules
1363
+ * in the symbol from 50% in steps of 5%
1364
+ */
1365
+ exports$1.getPenaltyN4 = function getPenaltyN4 (data) {
1366
+ let darkCount = 0;
1367
+ const modulesCount = data.data.length;
1368
+
1369
+ for (let i = 0; i < modulesCount; i++) darkCount += data.data[i];
1370
+
1371
+ const k = Math.abs(Math.ceil((darkCount * 100 / modulesCount) / 5) - 10);
1372
+
1373
+ return k * PenaltyScores.N4
1374
+ };
1375
+
1376
+ /**
1377
+ * Return mask value at given position
1378
+ *
1379
+ * @param {Number} maskPattern Pattern reference value
1380
+ * @param {Number} i Row
1381
+ * @param {Number} j Column
1382
+ * @return {Boolean} Mask value
1383
+ */
1384
+ function getMaskAt (maskPattern, i, j) {
1385
+ switch (maskPattern) {
1386
+ case exports$1.Patterns.PATTERN000: return (i + j) % 2 === 0
1387
+ case exports$1.Patterns.PATTERN001: return i % 2 === 0
1388
+ case exports$1.Patterns.PATTERN010: return j % 3 === 0
1389
+ case exports$1.Patterns.PATTERN011: return (i + j) % 3 === 0
1390
+ case exports$1.Patterns.PATTERN100: return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 === 0
1391
+ case exports$1.Patterns.PATTERN101: return (i * j) % 2 + (i * j) % 3 === 0
1392
+ case exports$1.Patterns.PATTERN110: return ((i * j) % 2 + (i * j) % 3) % 2 === 0
1393
+ case exports$1.Patterns.PATTERN111: return ((i * j) % 3 + (i + j) % 2) % 2 === 0
1394
+
1395
+ default: throw new Error('bad maskPattern:' + maskPattern)
1396
+ }
1397
+ }
1398
+
1399
+ /**
1400
+ * Apply a mask pattern to a BitMatrix
1401
+ *
1402
+ * @param {Number} pattern Pattern reference number
1403
+ * @param {BitMatrix} data BitMatrix data
1404
+ */
1405
+ exports$1.applyMask = function applyMask (pattern, data) {
1406
+ const size = data.size;
1407
+
1408
+ for (let col = 0; col < size; col++) {
1409
+ for (let row = 0; row < size; row++) {
1410
+ if (data.isReserved(row, col)) continue
1411
+ data.xor(row, col, getMaskAt(pattern, row, col));
1412
+ }
1413
+ }
1414
+ };
1415
+
1416
+ /**
1417
+ * Returns the best mask pattern for data
1418
+ *
1419
+ * @param {BitMatrix} data
1420
+ * @return {Number} Mask pattern reference number
1421
+ */
1422
+ exports$1.getBestMask = function getBestMask (data, setupFormatFunc) {
1423
+ const numPatterns = Object.keys(exports$1.Patterns).length;
1424
+ let bestPattern = 0;
1425
+ let lowerPenalty = Infinity;
1426
+
1427
+ for (let p = 0; p < numPatterns; p++) {
1428
+ setupFormatFunc(p);
1429
+ exports$1.applyMask(p, data);
1430
+
1431
+ // Calculate penalty
1432
+ const penalty =
1433
+ exports$1.getPenaltyN1(data) +
1434
+ exports$1.getPenaltyN2(data) +
1435
+ exports$1.getPenaltyN3(data) +
1436
+ exports$1.getPenaltyN4(data);
1437
+
1438
+ // Undo previously applied mask
1439
+ exports$1.applyMask(p, data);
1440
+
1441
+ if (penalty < lowerPenalty) {
1442
+ lowerPenalty = penalty;
1443
+ bestPattern = p;
1444
+ }
1445
+ }
1446
+
1447
+ return bestPattern
1448
+ };
1449
+ } (maskPattern));
1450
+ return maskPattern;
1451
+ }
1452
+
1453
+ var errorCorrectionCode = {};
1454
+
1455
+ var hasRequiredErrorCorrectionCode;
1456
+
1457
+ function requireErrorCorrectionCode () {
1458
+ if (hasRequiredErrorCorrectionCode) return errorCorrectionCode;
1459
+ hasRequiredErrorCorrectionCode = 1;
1460
+ const ECLevel = requireErrorCorrectionLevel();
1461
+
1462
+ const EC_BLOCKS_TABLE = [
1463
+ // L M Q H
1464
+ 1, 1, 1, 1,
1465
+ 1, 1, 1, 1,
1466
+ 1, 1, 2, 2,
1467
+ 1, 2, 2, 4,
1468
+ 1, 2, 4, 4,
1469
+ 2, 4, 4, 4,
1470
+ 2, 4, 6, 5,
1471
+ 2, 4, 6, 6,
1472
+ 2, 5, 8, 8,
1473
+ 4, 5, 8, 8,
1474
+ 4, 5, 8, 11,
1475
+ 4, 8, 10, 11,
1476
+ 4, 9, 12, 16,
1477
+ 4, 9, 16, 16,
1478
+ 6, 10, 12, 18,
1479
+ 6, 10, 17, 16,
1480
+ 6, 11, 16, 19,
1481
+ 6, 13, 18, 21,
1482
+ 7, 14, 21, 25,
1483
+ 8, 16, 20, 25,
1484
+ 8, 17, 23, 25,
1485
+ 9, 17, 23, 34,
1486
+ 9, 18, 25, 30,
1487
+ 10, 20, 27, 32,
1488
+ 12, 21, 29, 35,
1489
+ 12, 23, 34, 37,
1490
+ 12, 25, 34, 40,
1491
+ 13, 26, 35, 42,
1492
+ 14, 28, 38, 45,
1493
+ 15, 29, 40, 48,
1494
+ 16, 31, 43, 51,
1495
+ 17, 33, 45, 54,
1496
+ 18, 35, 48, 57,
1497
+ 19, 37, 51, 60,
1498
+ 19, 38, 53, 63,
1499
+ 20, 40, 56, 66,
1500
+ 21, 43, 59, 70,
1501
+ 22, 45, 62, 74,
1502
+ 24, 47, 65, 77,
1503
+ 25, 49, 68, 81
1504
+ ];
1505
+
1506
+ const EC_CODEWORDS_TABLE = [
1507
+ // L M Q H
1508
+ 7, 10, 13, 17,
1509
+ 10, 16, 22, 28,
1510
+ 15, 26, 36, 44,
1511
+ 20, 36, 52, 64,
1512
+ 26, 48, 72, 88,
1513
+ 36, 64, 96, 112,
1514
+ 40, 72, 108, 130,
1515
+ 48, 88, 132, 156,
1516
+ 60, 110, 160, 192,
1517
+ 72, 130, 192, 224,
1518
+ 80, 150, 224, 264,
1519
+ 96, 176, 260, 308,
1520
+ 104, 198, 288, 352,
1521
+ 120, 216, 320, 384,
1522
+ 132, 240, 360, 432,
1523
+ 144, 280, 408, 480,
1524
+ 168, 308, 448, 532,
1525
+ 180, 338, 504, 588,
1526
+ 196, 364, 546, 650,
1527
+ 224, 416, 600, 700,
1528
+ 224, 442, 644, 750,
1529
+ 252, 476, 690, 816,
1530
+ 270, 504, 750, 900,
1531
+ 300, 560, 810, 960,
1532
+ 312, 588, 870, 1050,
1533
+ 336, 644, 952, 1110,
1534
+ 360, 700, 1020, 1200,
1535
+ 390, 728, 1050, 1260,
1536
+ 420, 784, 1140, 1350,
1537
+ 450, 812, 1200, 1440,
1538
+ 480, 868, 1290, 1530,
1539
+ 510, 924, 1350, 1620,
1540
+ 540, 980, 1440, 1710,
1541
+ 570, 1036, 1530, 1800,
1542
+ 570, 1064, 1590, 1890,
1543
+ 600, 1120, 1680, 1980,
1544
+ 630, 1204, 1770, 2100,
1545
+ 660, 1260, 1860, 2220,
1546
+ 720, 1316, 1950, 2310,
1547
+ 750, 1372, 2040, 2430
1548
+ ];
1549
+
1550
+ /**
1551
+ * Returns the number of error correction block that the QR Code should contain
1552
+ * for the specified version and error correction level.
1553
+ *
1554
+ * @param {Number} version QR Code version
1555
+ * @param {Number} errorCorrectionLevel Error correction level
1556
+ * @return {Number} Number of error correction blocks
1557
+ */
1558
+ errorCorrectionCode.getBlocksCount = function getBlocksCount (version, errorCorrectionLevel) {
1559
+ switch (errorCorrectionLevel) {
1560
+ case ECLevel.L:
1561
+ return EC_BLOCKS_TABLE[(version - 1) * 4 + 0]
1562
+ case ECLevel.M:
1563
+ return EC_BLOCKS_TABLE[(version - 1) * 4 + 1]
1564
+ case ECLevel.Q:
1565
+ return EC_BLOCKS_TABLE[(version - 1) * 4 + 2]
1566
+ case ECLevel.H:
1567
+ return EC_BLOCKS_TABLE[(version - 1) * 4 + 3]
1568
+ default:
1569
+ return undefined
1570
+ }
1571
+ };
1572
+
1573
+ /**
1574
+ * Returns the number of error correction codewords to use for the specified
1575
+ * version and error correction level.
1576
+ *
1577
+ * @param {Number} version QR Code version
1578
+ * @param {Number} errorCorrectionLevel Error correction level
1579
+ * @return {Number} Number of error correction codewords
1580
+ */
1581
+ errorCorrectionCode.getTotalCodewordsCount = function getTotalCodewordsCount (version, errorCorrectionLevel) {
1582
+ switch (errorCorrectionLevel) {
1583
+ case ECLevel.L:
1584
+ return EC_CODEWORDS_TABLE[(version - 1) * 4 + 0]
1585
+ case ECLevel.M:
1586
+ return EC_CODEWORDS_TABLE[(version - 1) * 4 + 1]
1587
+ case ECLevel.Q:
1588
+ return EC_CODEWORDS_TABLE[(version - 1) * 4 + 2]
1589
+ case ECLevel.H:
1590
+ return EC_CODEWORDS_TABLE[(version - 1) * 4 + 3]
1591
+ default:
1592
+ return undefined
1593
+ }
1594
+ };
1595
+ return errorCorrectionCode;
1596
+ }
1597
+
1598
+ var polynomial = {};
1599
+
1600
+ var galoisField = {};
1601
+
1602
+ var hasRequiredGaloisField;
1603
+
1604
+ function requireGaloisField () {
1605
+ if (hasRequiredGaloisField) return galoisField;
1606
+ hasRequiredGaloisField = 1;
1607
+ const EXP_TABLE = new Uint8Array(512);
1608
+ const LOG_TABLE = new Uint8Array(256)
1609
+ /**
1610
+ * Precompute the log and anti-log tables for faster computation later
1611
+ *
1612
+ * For each possible value in the galois field 2^8, we will pre-compute
1613
+ * the logarithm and anti-logarithm (exponential) of this value
1614
+ *
1615
+ * ref {@link https://en.wikiversity.org/wiki/Reed%E2%80%93Solomon_codes_for_coders#Introduction_to_mathematical_fields}
1616
+ */
1617
+ ;(function initTables () {
1618
+ let x = 1;
1619
+ for (let i = 0; i < 255; i++) {
1620
+ EXP_TABLE[i] = x;
1621
+ LOG_TABLE[x] = i;
1622
+
1623
+ x <<= 1; // multiply by 2
1624
+
1625
+ // The QR code specification says to use byte-wise modulo 100011101 arithmetic.
1626
+ // This means that when a number is 256 or larger, it should be XORed with 0x11D.
1627
+ if (x & 0x100) { // similar to x >= 256, but a lot faster (because 0x100 == 256)
1628
+ x ^= 0x11D;
1629
+ }
1630
+ }
1631
+
1632
+ // Optimization: double the size of the anti-log table so that we don't need to mod 255 to
1633
+ // stay inside the bounds (because we will mainly use this table for the multiplication of
1634
+ // two GF numbers, no more).
1635
+ // @see {@link mul}
1636
+ for (let i = 255; i < 512; i++) {
1637
+ EXP_TABLE[i] = EXP_TABLE[i - 255];
1638
+ }
1639
+ }());
1640
+
1641
+ /**
1642
+ * Returns log value of n inside Galois Field
1643
+ *
1644
+ * @param {Number} n
1645
+ * @return {Number}
1646
+ */
1647
+ galoisField.log = function log (n) {
1648
+ if (n < 1) throw new Error('log(' + n + ')')
1649
+ return LOG_TABLE[n]
1650
+ };
1651
+
1652
+ /**
1653
+ * Returns anti-log value of n inside Galois Field
1654
+ *
1655
+ * @param {Number} n
1656
+ * @return {Number}
1657
+ */
1658
+ galoisField.exp = function exp (n) {
1659
+ return EXP_TABLE[n]
1660
+ };
1661
+
1662
+ /**
1663
+ * Multiplies two number inside Galois Field
1664
+ *
1665
+ * @param {Number} x
1666
+ * @param {Number} y
1667
+ * @return {Number}
1668
+ */
1669
+ galoisField.mul = function mul (x, y) {
1670
+ if (x === 0 || y === 0) return 0
1671
+
1672
+ // should be EXP_TABLE[(LOG_TABLE[x] + LOG_TABLE[y]) % 255] if EXP_TABLE wasn't oversized
1673
+ // @see {@link initTables}
1674
+ return EXP_TABLE[LOG_TABLE[x] + LOG_TABLE[y]]
1675
+ };
1676
+ return galoisField;
1677
+ }
1678
+
1679
+ var hasRequiredPolynomial;
1680
+
1681
+ function requirePolynomial () {
1682
+ if (hasRequiredPolynomial) return polynomial;
1683
+ hasRequiredPolynomial = 1;
1684
+ (function (exports$1) {
1685
+ const GF = requireGaloisField();
1686
+
1687
+ /**
1688
+ * Multiplies two polynomials inside Galois Field
1689
+ *
1690
+ * @param {Uint8Array} p1 Polynomial
1691
+ * @param {Uint8Array} p2 Polynomial
1692
+ * @return {Uint8Array} Product of p1 and p2
1693
+ */
1694
+ exports$1.mul = function mul (p1, p2) {
1695
+ const coeff = new Uint8Array(p1.length + p2.length - 1);
1696
+
1697
+ for (let i = 0; i < p1.length; i++) {
1698
+ for (let j = 0; j < p2.length; j++) {
1699
+ coeff[i + j] ^= GF.mul(p1[i], p2[j]);
1700
+ }
1701
+ }
1702
+
1703
+ return coeff
1704
+ };
1705
+
1706
+ /**
1707
+ * Calculate the remainder of polynomials division
1708
+ *
1709
+ * @param {Uint8Array} divident Polynomial
1710
+ * @param {Uint8Array} divisor Polynomial
1711
+ * @return {Uint8Array} Remainder
1712
+ */
1713
+ exports$1.mod = function mod (divident, divisor) {
1714
+ let result = new Uint8Array(divident);
1715
+
1716
+ while ((result.length - divisor.length) >= 0) {
1717
+ const coeff = result[0];
1718
+
1719
+ for (let i = 0; i < divisor.length; i++) {
1720
+ result[i] ^= GF.mul(divisor[i], coeff);
1721
+ }
1722
+
1723
+ // remove all zeros from buffer head
1724
+ let offset = 0;
1725
+ while (offset < result.length && result[offset] === 0) offset++;
1726
+ result = result.slice(offset);
1727
+ }
1728
+
1729
+ return result
1730
+ };
1731
+
1732
+ /**
1733
+ * Generate an irreducible generator polynomial of specified degree
1734
+ * (used by Reed-Solomon encoder)
1735
+ *
1736
+ * @param {Number} degree Degree of the generator polynomial
1737
+ * @return {Uint8Array} Buffer containing polynomial coefficients
1738
+ */
1739
+ exports$1.generateECPolynomial = function generateECPolynomial (degree) {
1740
+ let poly = new Uint8Array([1]);
1741
+ for (let i = 0; i < degree; i++) {
1742
+ poly = exports$1.mul(poly, new Uint8Array([1, GF.exp(i)]));
1743
+ }
1744
+
1745
+ return poly
1746
+ };
1747
+ } (polynomial));
1748
+ return polynomial;
1749
+ }
1750
+
1751
+ var reedSolomonEncoder;
1752
+ var hasRequiredReedSolomonEncoder;
1753
+
1754
+ function requireReedSolomonEncoder () {
1755
+ if (hasRequiredReedSolomonEncoder) return reedSolomonEncoder;
1756
+ hasRequiredReedSolomonEncoder = 1;
1757
+ const Polynomial = requirePolynomial();
1758
+
1759
+ function ReedSolomonEncoder (degree) {
1760
+ this.genPoly = undefined;
1761
+ this.degree = degree;
1762
+
1763
+ if (this.degree) this.initialize(this.degree);
1764
+ }
1765
+
1766
+ /**
1767
+ * Initialize the encoder.
1768
+ * The input param should correspond to the number of error correction codewords.
1769
+ *
1770
+ * @param {Number} degree
1771
+ */
1772
+ ReedSolomonEncoder.prototype.initialize = function initialize (degree) {
1773
+ // create an irreducible generator polynomial
1774
+ this.degree = degree;
1775
+ this.genPoly = Polynomial.generateECPolynomial(this.degree);
1776
+ };
1777
+
1778
+ /**
1779
+ * Encodes a chunk of data
1780
+ *
1781
+ * @param {Uint8Array} data Buffer containing input data
1782
+ * @return {Uint8Array} Buffer containing encoded data
1783
+ */
1784
+ ReedSolomonEncoder.prototype.encode = function encode (data) {
1785
+ if (!this.genPoly) {
1786
+ throw new Error('Encoder not initialized')
1787
+ }
1788
+
1789
+ // Calculate EC for this data block
1790
+ // extends data size to data+genPoly size
1791
+ const paddedData = new Uint8Array(data.length + this.degree);
1792
+ paddedData.set(data);
1793
+
1794
+ // The error correction codewords are the remainder after dividing the data codewords
1795
+ // by a generator polynomial
1796
+ const remainder = Polynomial.mod(paddedData, this.genPoly);
1797
+
1798
+ // return EC data blocks (last n byte, where n is the degree of genPoly)
1799
+ // If coefficients number in remainder are less than genPoly degree,
1800
+ // pad with 0s to the left to reach the needed number of coefficients
1801
+ const start = this.degree - remainder.length;
1802
+ if (start > 0) {
1803
+ const buff = new Uint8Array(this.degree);
1804
+ buff.set(remainder, start);
1805
+
1806
+ return buff
1807
+ }
1808
+
1809
+ return remainder
1810
+ };
1811
+
1812
+ reedSolomonEncoder = ReedSolomonEncoder;
1813
+ return reedSolomonEncoder;
1814
+ }
1815
+
1816
+ var version = {};
1817
+
1818
+ var mode = {};
1819
+
1820
+ var versionCheck = {};
1821
+
1822
+ /**
1823
+ * Check if QR Code version is valid
1824
+ *
1825
+ * @param {Number} version QR Code version
1826
+ * @return {Boolean} true if valid version, false otherwise
1827
+ */
1828
+
1829
+ var hasRequiredVersionCheck;
1830
+
1831
+ function requireVersionCheck () {
1832
+ if (hasRequiredVersionCheck) return versionCheck;
1833
+ hasRequiredVersionCheck = 1;
1834
+ versionCheck.isValid = function isValid (version) {
1835
+ return !isNaN(version) && version >= 1 && version <= 40
1836
+ };
1837
+ return versionCheck;
1838
+ }
1839
+
1840
+ var regex = {};
1841
+
1842
+ var hasRequiredRegex;
1843
+
1844
+ function requireRegex () {
1845
+ if (hasRequiredRegex) return regex;
1846
+ hasRequiredRegex = 1;
1847
+ const numeric = '[0-9]+';
1848
+ const alphanumeric = '[A-Z $%*+\\-./:]+';
1849
+ let kanji = '(?:[u3000-u303F]|[u3040-u309F]|[u30A0-u30FF]|' +
1850
+ '[uFF00-uFFEF]|[u4E00-u9FAF]|[u2605-u2606]|[u2190-u2195]|u203B|' +
1851
+ '[u2010u2015u2018u2019u2025u2026u201Cu201Du2225u2260]|' +
1852
+ '[u0391-u0451]|[u00A7u00A8u00B1u00B4u00D7u00F7])+';
1853
+ kanji = kanji.replace(/u/g, '\\u');
1854
+
1855
+ const byte = '(?:(?![A-Z0-9 $%*+\\-./:]|' + kanji + ')(?:.|[\r\n]))+';
1856
+
1857
+ regex.KANJI = new RegExp(kanji, 'g');
1858
+ regex.BYTE_KANJI = new RegExp('[^A-Z0-9 $%*+\\-./:]+', 'g');
1859
+ regex.BYTE = new RegExp(byte, 'g');
1860
+ regex.NUMERIC = new RegExp(numeric, 'g');
1861
+ regex.ALPHANUMERIC = new RegExp(alphanumeric, 'g');
1862
+
1863
+ const TEST_KANJI = new RegExp('^' + kanji + '$');
1864
+ const TEST_NUMERIC = new RegExp('^' + numeric + '$');
1865
+ const TEST_ALPHANUMERIC = new RegExp('^[A-Z0-9 $%*+\\-./:]+$');
1866
+
1867
+ regex.testKanji = function testKanji (str) {
1868
+ return TEST_KANJI.test(str)
1869
+ };
1870
+
1871
+ regex.testNumeric = function testNumeric (str) {
1872
+ return TEST_NUMERIC.test(str)
1873
+ };
1874
+
1875
+ regex.testAlphanumeric = function testAlphanumeric (str) {
1876
+ return TEST_ALPHANUMERIC.test(str)
1877
+ };
1878
+ return regex;
1879
+ }
1880
+
1881
+ var hasRequiredMode;
1882
+
1883
+ function requireMode () {
1884
+ if (hasRequiredMode) return mode;
1885
+ hasRequiredMode = 1;
1886
+ (function (exports$1) {
1887
+ const VersionCheck = requireVersionCheck();
1888
+ const Regex = requireRegex();
1889
+
1890
+ /**
1891
+ * Numeric mode encodes data from the decimal digit set (0 - 9)
1892
+ * (byte values 30HEX to 39HEX).
1893
+ * Normally, 3 data characters are represented by 10 bits.
1894
+ *
1895
+ * @type {Object}
1896
+ */
1897
+ exports$1.NUMERIC = {
1898
+ id: 'Numeric',
1899
+ bit: 1 << 0,
1900
+ ccBits: [10, 12, 14]
1901
+ };
1902
+
1903
+ /**
1904
+ * Alphanumeric mode encodes data from a set of 45 characters,
1905
+ * i.e. 10 numeric digits (0 - 9),
1906
+ * 26 alphabetic characters (A - Z),
1907
+ * and 9 symbols (SP, $, %, *, +, -, ., /, :).
1908
+ * Normally, two input characters are represented by 11 bits.
1909
+ *
1910
+ * @type {Object}
1911
+ */
1912
+ exports$1.ALPHANUMERIC = {
1913
+ id: 'Alphanumeric',
1914
+ bit: 1 << 1,
1915
+ ccBits: [9, 11, 13]
1916
+ };
1917
+
1918
+ /**
1919
+ * In byte mode, data is encoded at 8 bits per character.
1920
+ *
1921
+ * @type {Object}
1922
+ */
1923
+ exports$1.BYTE = {
1924
+ id: 'Byte',
1925
+ bit: 1 << 2,
1926
+ ccBits: [8, 16, 16]
1927
+ };
1928
+
1929
+ /**
1930
+ * The Kanji mode efficiently encodes Kanji characters in accordance with
1931
+ * the Shift JIS system based on JIS X 0208.
1932
+ * The Shift JIS values are shifted from the JIS X 0208 values.
1933
+ * JIS X 0208 gives details of the shift coded representation.
1934
+ * Each two-byte character value is compacted to a 13-bit binary codeword.
1935
+ *
1936
+ * @type {Object}
1937
+ */
1938
+ exports$1.KANJI = {
1939
+ id: 'Kanji',
1940
+ bit: 1 << 3,
1941
+ ccBits: [8, 10, 12]
1942
+ };
1943
+
1944
+ /**
1945
+ * Mixed mode will contain a sequences of data in a combination of any of
1946
+ * the modes described above
1947
+ *
1948
+ * @type {Object}
1949
+ */
1950
+ exports$1.MIXED = {
1951
+ bit: -1
1952
+ };
1953
+
1954
+ /**
1955
+ * Returns the number of bits needed to store the data length
1956
+ * according to QR Code specifications.
1957
+ *
1958
+ * @param {Mode} mode Data mode
1959
+ * @param {Number} version QR Code version
1960
+ * @return {Number} Number of bits
1961
+ */
1962
+ exports$1.getCharCountIndicator = function getCharCountIndicator (mode, version) {
1963
+ if (!mode.ccBits) throw new Error('Invalid mode: ' + mode)
1964
+
1965
+ if (!VersionCheck.isValid(version)) {
1966
+ throw new Error('Invalid version: ' + version)
1967
+ }
1968
+
1969
+ if (version >= 1 && version < 10) return mode.ccBits[0]
1970
+ else if (version < 27) return mode.ccBits[1]
1971
+ return mode.ccBits[2]
1972
+ };
1973
+
1974
+ /**
1975
+ * Returns the most efficient mode to store the specified data
1976
+ *
1977
+ * @param {String} dataStr Input data string
1978
+ * @return {Mode} Best mode
1979
+ */
1980
+ exports$1.getBestModeForData = function getBestModeForData (dataStr) {
1981
+ if (Regex.testNumeric(dataStr)) return exports$1.NUMERIC
1982
+ else if (Regex.testAlphanumeric(dataStr)) return exports$1.ALPHANUMERIC
1983
+ else if (Regex.testKanji(dataStr)) return exports$1.KANJI
1984
+ else return exports$1.BYTE
1985
+ };
1986
+
1987
+ /**
1988
+ * Return mode name as string
1989
+ *
1990
+ * @param {Mode} mode Mode object
1991
+ * @returns {String} Mode name
1992
+ */
1993
+ exports$1.toString = function toString (mode) {
1994
+ if (mode && mode.id) return mode.id
1995
+ throw new Error('Invalid mode')
1996
+ };
1997
+
1998
+ /**
1999
+ * Check if input param is a valid mode object
2000
+ *
2001
+ * @param {Mode} mode Mode object
2002
+ * @returns {Boolean} True if valid mode, false otherwise
2003
+ */
2004
+ exports$1.isValid = function isValid (mode) {
2005
+ return mode && mode.bit && mode.ccBits
2006
+ };
2007
+
2008
+ /**
2009
+ * Get mode object from its name
2010
+ *
2011
+ * @param {String} string Mode name
2012
+ * @returns {Mode} Mode object
2013
+ */
2014
+ function fromString (string) {
2015
+ if (typeof string !== 'string') {
2016
+ throw new Error('Param is not a string')
2017
+ }
2018
+
2019
+ const lcStr = string.toLowerCase();
2020
+
2021
+ switch (lcStr) {
2022
+ case 'numeric':
2023
+ return exports$1.NUMERIC
2024
+ case 'alphanumeric':
2025
+ return exports$1.ALPHANUMERIC
2026
+ case 'kanji':
2027
+ return exports$1.KANJI
2028
+ case 'byte':
2029
+ return exports$1.BYTE
2030
+ default:
2031
+ throw new Error('Unknown mode: ' + string)
2032
+ }
2033
+ }
2034
+
2035
+ /**
2036
+ * Returns mode from a value.
2037
+ * If value is not a valid mode, returns defaultValue
2038
+ *
2039
+ * @param {Mode|String} value Encoding mode
2040
+ * @param {Mode} defaultValue Fallback value
2041
+ * @return {Mode} Encoding mode
2042
+ */
2043
+ exports$1.from = function from (value, defaultValue) {
2044
+ if (exports$1.isValid(value)) {
2045
+ return value
2046
+ }
2047
+
2048
+ try {
2049
+ return fromString(value)
2050
+ } catch (e) {
2051
+ return defaultValue
2052
+ }
2053
+ };
2054
+ } (mode));
2055
+ return mode;
2056
+ }
2057
+
2058
+ var hasRequiredVersion;
2059
+
2060
+ function requireVersion () {
2061
+ if (hasRequiredVersion) return version;
2062
+ hasRequiredVersion = 1;
2063
+ (function (exports$1) {
2064
+ const Utils = requireUtils$1();
2065
+ const ECCode = requireErrorCorrectionCode();
2066
+ const ECLevel = requireErrorCorrectionLevel();
2067
+ const Mode = requireMode();
2068
+ const VersionCheck = requireVersionCheck();
2069
+
2070
+ // Generator polynomial used to encode version information
2071
+ const G18 = (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0);
2072
+ const G18_BCH = Utils.getBCHDigit(G18);
2073
+
2074
+ function getBestVersionForDataLength (mode, length, errorCorrectionLevel) {
2075
+ for (let currentVersion = 1; currentVersion <= 40; currentVersion++) {
2076
+ if (length <= exports$1.getCapacity(currentVersion, errorCorrectionLevel, mode)) {
2077
+ return currentVersion
2078
+ }
2079
+ }
2080
+
2081
+ return undefined
2082
+ }
2083
+
2084
+ function getReservedBitsCount (mode, version) {
2085
+ // Character count indicator + mode indicator bits
2086
+ return Mode.getCharCountIndicator(mode, version) + 4
2087
+ }
2088
+
2089
+ function getTotalBitsFromDataArray (segments, version) {
2090
+ let totalBits = 0;
2091
+
2092
+ segments.forEach(function (data) {
2093
+ const reservedBits = getReservedBitsCount(data.mode, version);
2094
+ totalBits += reservedBits + data.getBitsLength();
2095
+ });
2096
+
2097
+ return totalBits
2098
+ }
2099
+
2100
+ function getBestVersionForMixedData (segments, errorCorrectionLevel) {
2101
+ for (let currentVersion = 1; currentVersion <= 40; currentVersion++) {
2102
+ const length = getTotalBitsFromDataArray(segments, currentVersion);
2103
+ if (length <= exports$1.getCapacity(currentVersion, errorCorrectionLevel, Mode.MIXED)) {
2104
+ return currentVersion
2105
+ }
2106
+ }
2107
+
2108
+ return undefined
2109
+ }
2110
+
2111
+ /**
2112
+ * Returns version number from a value.
2113
+ * If value is not a valid version, returns defaultValue
2114
+ *
2115
+ * @param {Number|String} value QR Code version
2116
+ * @param {Number} defaultValue Fallback value
2117
+ * @return {Number} QR Code version number
2118
+ */
2119
+ exports$1.from = function from (value, defaultValue) {
2120
+ if (VersionCheck.isValid(value)) {
2121
+ return parseInt(value, 10)
2122
+ }
2123
+
2124
+ return defaultValue
2125
+ };
2126
+
2127
+ /**
2128
+ * Returns how much data can be stored with the specified QR code version
2129
+ * and error correction level
2130
+ *
2131
+ * @param {Number} version QR Code version (1-40)
2132
+ * @param {Number} errorCorrectionLevel Error correction level
2133
+ * @param {Mode} mode Data mode
2134
+ * @return {Number} Quantity of storable data
2135
+ */
2136
+ exports$1.getCapacity = function getCapacity (version, errorCorrectionLevel, mode) {
2137
+ if (!VersionCheck.isValid(version)) {
2138
+ throw new Error('Invalid QR Code version')
2139
+ }
2140
+
2141
+ // Use Byte mode as default
2142
+ if (typeof mode === 'undefined') mode = Mode.BYTE;
2143
+
2144
+ // Total codewords for this QR code version (Data + Error correction)
2145
+ const totalCodewords = Utils.getSymbolTotalCodewords(version);
2146
+
2147
+ // Total number of error correction codewords
2148
+ const ecTotalCodewords = ECCode.getTotalCodewordsCount(version, errorCorrectionLevel);
2149
+
2150
+ // Total number of data codewords
2151
+ const dataTotalCodewordsBits = (totalCodewords - ecTotalCodewords) * 8;
2152
+
2153
+ if (mode === Mode.MIXED) return dataTotalCodewordsBits
2154
+
2155
+ const usableBits = dataTotalCodewordsBits - getReservedBitsCount(mode, version);
2156
+
2157
+ // Return max number of storable codewords
2158
+ switch (mode) {
2159
+ case Mode.NUMERIC:
2160
+ return Math.floor((usableBits / 10) * 3)
2161
+
2162
+ case Mode.ALPHANUMERIC:
2163
+ return Math.floor((usableBits / 11) * 2)
2164
+
2165
+ case Mode.KANJI:
2166
+ return Math.floor(usableBits / 13)
2167
+
2168
+ case Mode.BYTE:
2169
+ default:
2170
+ return Math.floor(usableBits / 8)
2171
+ }
2172
+ };
2173
+
2174
+ /**
2175
+ * Returns the minimum version needed to contain the amount of data
2176
+ *
2177
+ * @param {Segment} data Segment of data
2178
+ * @param {Number} [errorCorrectionLevel=H] Error correction level
2179
+ * @param {Mode} mode Data mode
2180
+ * @return {Number} QR Code version
2181
+ */
2182
+ exports$1.getBestVersionForData = function getBestVersionForData (data, errorCorrectionLevel) {
2183
+ let seg;
2184
+
2185
+ const ecl = ECLevel.from(errorCorrectionLevel, ECLevel.M);
2186
+
2187
+ if (Array.isArray(data)) {
2188
+ if (data.length > 1) {
2189
+ return getBestVersionForMixedData(data, ecl)
2190
+ }
2191
+
2192
+ if (data.length === 0) {
2193
+ return 1
2194
+ }
2195
+
2196
+ seg = data[0];
2197
+ } else {
2198
+ seg = data;
2199
+ }
2200
+
2201
+ return getBestVersionForDataLength(seg.mode, seg.getLength(), ecl)
2202
+ };
2203
+
2204
+ /**
2205
+ * Returns version information with relative error correction bits
2206
+ *
2207
+ * The version information is included in QR Code symbols of version 7 or larger.
2208
+ * It consists of an 18-bit sequence containing 6 data bits,
2209
+ * with 12 error correction bits calculated using the (18, 6) Golay code.
2210
+ *
2211
+ * @param {Number} version QR Code version
2212
+ * @return {Number} Encoded version info bits
2213
+ */
2214
+ exports$1.getEncodedBits = function getEncodedBits (version) {
2215
+ if (!VersionCheck.isValid(version) || version < 7) {
2216
+ throw new Error('Invalid QR Code version')
2217
+ }
2218
+
2219
+ let d = version << 12;
2220
+
2221
+ while (Utils.getBCHDigit(d) - G18_BCH >= 0) {
2222
+ d ^= (G18 << (Utils.getBCHDigit(d) - G18_BCH));
2223
+ }
2224
+
2225
+ return (version << 12) | d
2226
+ };
2227
+ } (version));
2228
+ return version;
2229
+ }
2230
+
2231
+ var formatInfo = {};
2232
+
2233
+ var hasRequiredFormatInfo;
2234
+
2235
+ function requireFormatInfo () {
2236
+ if (hasRequiredFormatInfo) return formatInfo;
2237
+ hasRequiredFormatInfo = 1;
2238
+ const Utils = requireUtils$1();
2239
+
2240
+ const G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0);
2241
+ const G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1);
2242
+ const G15_BCH = Utils.getBCHDigit(G15);
2243
+
2244
+ /**
2245
+ * Returns format information with relative error correction bits
2246
+ *
2247
+ * The format information is a 15-bit sequence containing 5 data bits,
2248
+ * with 10 error correction bits calculated using the (15, 5) BCH code.
2249
+ *
2250
+ * @param {Number} errorCorrectionLevel Error correction level
2251
+ * @param {Number} mask Mask pattern
2252
+ * @return {Number} Encoded format information bits
2253
+ */
2254
+ formatInfo.getEncodedBits = function getEncodedBits (errorCorrectionLevel, mask) {
2255
+ const data = ((errorCorrectionLevel.bit << 3) | mask);
2256
+ let d = data << 10;
2257
+
2258
+ while (Utils.getBCHDigit(d) - G15_BCH >= 0) {
2259
+ d ^= (G15 << (Utils.getBCHDigit(d) - G15_BCH));
2260
+ }
2261
+
2262
+ // xor final data with mask pattern in order to ensure that
2263
+ // no combination of Error Correction Level and data mask pattern
2264
+ // will result in an all-zero data string
2265
+ return ((data << 10) | d) ^ G15_MASK
2266
+ };
2267
+ return formatInfo;
2268
+ }
2269
+
2270
+ var segments = {};
2271
+
2272
+ var numericData;
2273
+ var hasRequiredNumericData;
2274
+
2275
+ function requireNumericData () {
2276
+ if (hasRequiredNumericData) return numericData;
2277
+ hasRequiredNumericData = 1;
2278
+ const Mode = requireMode();
2279
+
2280
+ function NumericData (data) {
2281
+ this.mode = Mode.NUMERIC;
2282
+ this.data = data.toString();
2283
+ }
2284
+
2285
+ NumericData.getBitsLength = function getBitsLength (length) {
2286
+ return 10 * Math.floor(length / 3) + ((length % 3) ? ((length % 3) * 3 + 1) : 0)
2287
+ };
2288
+
2289
+ NumericData.prototype.getLength = function getLength () {
2290
+ return this.data.length
2291
+ };
2292
+
2293
+ NumericData.prototype.getBitsLength = function getBitsLength () {
2294
+ return NumericData.getBitsLength(this.data.length)
2295
+ };
2296
+
2297
+ NumericData.prototype.write = function write (bitBuffer) {
2298
+ let i, group, value;
2299
+
2300
+ // The input data string is divided into groups of three digits,
2301
+ // and each group is converted to its 10-bit binary equivalent.
2302
+ for (i = 0; i + 3 <= this.data.length; i += 3) {
2303
+ group = this.data.substr(i, 3);
2304
+ value = parseInt(group, 10);
2305
+
2306
+ bitBuffer.put(value, 10);
2307
+ }
2308
+
2309
+ // If the number of input digits is not an exact multiple of three,
2310
+ // the final one or two digits are converted to 4 or 7 bits respectively.
2311
+ const remainingNum = this.data.length - i;
2312
+ if (remainingNum > 0) {
2313
+ group = this.data.substr(i);
2314
+ value = parseInt(group, 10);
2315
+
2316
+ bitBuffer.put(value, remainingNum * 3 + 1);
2317
+ }
2318
+ };
2319
+
2320
+ numericData = NumericData;
2321
+ return numericData;
2322
+ }
2323
+
2324
+ var alphanumericData;
2325
+ var hasRequiredAlphanumericData;
2326
+
2327
+ function requireAlphanumericData () {
2328
+ if (hasRequiredAlphanumericData) return alphanumericData;
2329
+ hasRequiredAlphanumericData = 1;
2330
+ const Mode = requireMode();
2331
+
2332
+ /**
2333
+ * Array of characters available in alphanumeric mode
2334
+ *
2335
+ * As per QR Code specification, to each character
2336
+ * is assigned a value from 0 to 44 which in this case coincides
2337
+ * with the array index
2338
+ *
2339
+ * @type {Array}
2340
+ */
2341
+ const ALPHA_NUM_CHARS = [
2342
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
2343
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
2344
+ 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
2345
+ ' ', '$', '%', '*', '+', '-', '.', '/', ':'
2346
+ ];
2347
+
2348
+ function AlphanumericData (data) {
2349
+ this.mode = Mode.ALPHANUMERIC;
2350
+ this.data = data;
2351
+ }
2352
+
2353
+ AlphanumericData.getBitsLength = function getBitsLength (length) {
2354
+ return 11 * Math.floor(length / 2) + 6 * (length % 2)
2355
+ };
2356
+
2357
+ AlphanumericData.prototype.getLength = function getLength () {
2358
+ return this.data.length
2359
+ };
2360
+
2361
+ AlphanumericData.prototype.getBitsLength = function getBitsLength () {
2362
+ return AlphanumericData.getBitsLength(this.data.length)
2363
+ };
2364
+
2365
+ AlphanumericData.prototype.write = function write (bitBuffer) {
2366
+ let i;
2367
+
2368
+ // Input data characters are divided into groups of two characters
2369
+ // and encoded as 11-bit binary codes.
2370
+ for (i = 0; i + 2 <= this.data.length; i += 2) {
2371
+ // The character value of the first character is multiplied by 45
2372
+ let value = ALPHA_NUM_CHARS.indexOf(this.data[i]) * 45;
2373
+
2374
+ // The character value of the second digit is added to the product
2375
+ value += ALPHA_NUM_CHARS.indexOf(this.data[i + 1]);
2376
+
2377
+ // The sum is then stored as 11-bit binary number
2378
+ bitBuffer.put(value, 11);
2379
+ }
2380
+
2381
+ // If the number of input data characters is not a multiple of two,
2382
+ // the character value of the final character is encoded as a 6-bit binary number.
2383
+ if (this.data.length % 2) {
2384
+ bitBuffer.put(ALPHA_NUM_CHARS.indexOf(this.data[i]), 6);
2385
+ }
2386
+ };
2387
+
2388
+ alphanumericData = AlphanumericData;
2389
+ return alphanumericData;
2390
+ }
2391
+
2392
+ var byteData;
2393
+ var hasRequiredByteData;
2394
+
2395
+ function requireByteData () {
2396
+ if (hasRequiredByteData) return byteData;
2397
+ hasRequiredByteData = 1;
2398
+ const Mode = requireMode();
2399
+
2400
+ function ByteData (data) {
2401
+ this.mode = Mode.BYTE;
2402
+ if (typeof (data) === 'string') {
2403
+ this.data = new TextEncoder().encode(data);
2404
+ } else {
2405
+ this.data = new Uint8Array(data);
2406
+ }
2407
+ }
2408
+
2409
+ ByteData.getBitsLength = function getBitsLength (length) {
2410
+ return length * 8
2411
+ };
2412
+
2413
+ ByteData.prototype.getLength = function getLength () {
2414
+ return this.data.length
2415
+ };
2416
+
2417
+ ByteData.prototype.getBitsLength = function getBitsLength () {
2418
+ return ByteData.getBitsLength(this.data.length)
2419
+ };
2420
+
2421
+ ByteData.prototype.write = function (bitBuffer) {
2422
+ for (let i = 0, l = this.data.length; i < l; i++) {
2423
+ bitBuffer.put(this.data[i], 8);
2424
+ }
2425
+ };
2426
+
2427
+ byteData = ByteData;
2428
+ return byteData;
2429
+ }
2430
+
2431
+ var kanjiData;
2432
+ var hasRequiredKanjiData;
2433
+
2434
+ function requireKanjiData () {
2435
+ if (hasRequiredKanjiData) return kanjiData;
2436
+ hasRequiredKanjiData = 1;
2437
+ const Mode = requireMode();
2438
+ const Utils = requireUtils$1();
2439
+
2440
+ function KanjiData (data) {
2441
+ this.mode = Mode.KANJI;
2442
+ this.data = data;
2443
+ }
2444
+
2445
+ KanjiData.getBitsLength = function getBitsLength (length) {
2446
+ return length * 13
2447
+ };
2448
+
2449
+ KanjiData.prototype.getLength = function getLength () {
2450
+ return this.data.length
2451
+ };
2452
+
2453
+ KanjiData.prototype.getBitsLength = function getBitsLength () {
2454
+ return KanjiData.getBitsLength(this.data.length)
2455
+ };
2456
+
2457
+ KanjiData.prototype.write = function (bitBuffer) {
2458
+ let i;
2459
+
2460
+ // In the Shift JIS system, Kanji characters are represented by a two byte combination.
2461
+ // These byte values are shifted from the JIS X 0208 values.
2462
+ // JIS X 0208 gives details of the shift coded representation.
2463
+ for (i = 0; i < this.data.length; i++) {
2464
+ let value = Utils.toSJIS(this.data[i]);
2465
+
2466
+ // For characters with Shift JIS values from 0x8140 to 0x9FFC:
2467
+ if (value >= 0x8140 && value <= 0x9FFC) {
2468
+ // Subtract 0x8140 from Shift JIS value
2469
+ value -= 0x8140;
2470
+
2471
+ // For characters with Shift JIS values from 0xE040 to 0xEBBF
2472
+ } else if (value >= 0xE040 && value <= 0xEBBF) {
2473
+ // Subtract 0xC140 from Shift JIS value
2474
+ value -= 0xC140;
2475
+ } else {
2476
+ throw new Error(
2477
+ 'Invalid SJIS character: ' + this.data[i] + '\n' +
2478
+ 'Make sure your charset is UTF-8')
2479
+ }
2480
+
2481
+ // Multiply most significant byte of result by 0xC0
2482
+ // and add least significant byte to product
2483
+ value = (((value >>> 8) & 0xff) * 0xC0) + (value & 0xff);
2484
+
2485
+ // Convert result to a 13-bit binary string
2486
+ bitBuffer.put(value, 13);
2487
+ }
2488
+ };
2489
+
2490
+ kanjiData = KanjiData;
2491
+ return kanjiData;
2492
+ }
2493
+
2494
+ var dijkstra = {exports: {}};
2495
+
2496
+ var hasRequiredDijkstra;
2497
+
2498
+ function requireDijkstra () {
2499
+ if (hasRequiredDijkstra) return dijkstra.exports;
2500
+ hasRequiredDijkstra = 1;
2501
+ (function (module) {
2502
+
2503
+ /******************************************************************************
2504
+ * Created 2008-08-19.
2505
+ *
2506
+ * Dijkstra path-finding functions. Adapted from the Dijkstar Python project.
2507
+ *
2508
+ * Copyright (C) 2008
2509
+ * Wyatt Baldwin <self@wyattbaldwin.com>
2510
+ * All rights reserved
2511
+ *
2512
+ * Licensed under the MIT license.
2513
+ *
2514
+ * http://www.opensource.org/licenses/mit-license.php
2515
+ *
2516
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2517
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2518
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2519
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2520
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2521
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2522
+ * THE SOFTWARE.
2523
+ *****************************************************************************/
2524
+ var dijkstra = {
2525
+ single_source_shortest_paths: function(graph, s, d) {
2526
+ // Predecessor map for each node that has been encountered.
2527
+ // node ID => predecessor node ID
2528
+ var predecessors = {};
2529
+
2530
+ // Costs of shortest paths from s to all nodes encountered.
2531
+ // node ID => cost
2532
+ var costs = {};
2533
+ costs[s] = 0;
2534
+
2535
+ // Costs of shortest paths from s to all nodes encountered; differs from
2536
+ // `costs` in that it provides easy access to the node that currently has
2537
+ // the known shortest path from s.
2538
+ // XXX: Do we actually need both `costs` and `open`?
2539
+ var open = dijkstra.PriorityQueue.make();
2540
+ open.push(s, 0);
2541
+
2542
+ var closest,
2543
+ u, v,
2544
+ cost_of_s_to_u,
2545
+ adjacent_nodes,
2546
+ cost_of_e,
2547
+ cost_of_s_to_u_plus_cost_of_e,
2548
+ cost_of_s_to_v,
2549
+ first_visit;
2550
+ while (!open.empty()) {
2551
+ // In the nodes remaining in graph that have a known cost from s,
2552
+ // find the node, u, that currently has the shortest path from s.
2553
+ closest = open.pop();
2554
+ u = closest.value;
2555
+ cost_of_s_to_u = closest.cost;
2556
+
2557
+ // Get nodes adjacent to u...
2558
+ adjacent_nodes = graph[u] || {};
2559
+
2560
+ // ...and explore the edges that connect u to those nodes, updating
2561
+ // the cost of the shortest paths to any or all of those nodes as
2562
+ // necessary. v is the node across the current edge from u.
2563
+ for (v in adjacent_nodes) {
2564
+ if (adjacent_nodes.hasOwnProperty(v)) {
2565
+ // Get the cost of the edge running from u to v.
2566
+ cost_of_e = adjacent_nodes[v];
2567
+
2568
+ // Cost of s to u plus the cost of u to v across e--this is *a*
2569
+ // cost from s to v that may or may not be less than the current
2570
+ // known cost to v.
2571
+ cost_of_s_to_u_plus_cost_of_e = cost_of_s_to_u + cost_of_e;
2572
+
2573
+ // If we haven't visited v yet OR if the current known cost from s to
2574
+ // v is greater than the new cost we just found (cost of s to u plus
2575
+ // cost of u to v across e), update v's cost in the cost list and
2576
+ // update v's predecessor in the predecessor list (it's now u).
2577
+ cost_of_s_to_v = costs[v];
2578
+ first_visit = (typeof costs[v] === 'undefined');
2579
+ if (first_visit || cost_of_s_to_v > cost_of_s_to_u_plus_cost_of_e) {
2580
+ costs[v] = cost_of_s_to_u_plus_cost_of_e;
2581
+ open.push(v, cost_of_s_to_u_plus_cost_of_e);
2582
+ predecessors[v] = u;
2583
+ }
2584
+ }
2585
+ }
2586
+ }
2587
+
2588
+ if (typeof d !== 'undefined' && typeof costs[d] === 'undefined') {
2589
+ var msg = ['Could not find a path from ', s, ' to ', d, '.'].join('');
2590
+ throw new Error(msg);
2591
+ }
2592
+
2593
+ return predecessors;
2594
+ },
2595
+
2596
+ extract_shortest_path_from_predecessor_list: function(predecessors, d) {
2597
+ var nodes = [];
2598
+ var u = d;
2599
+ while (u) {
2600
+ nodes.push(u);
2601
+ predecessors[u];
2602
+ u = predecessors[u];
2603
+ }
2604
+ nodes.reverse();
2605
+ return nodes;
2606
+ },
2607
+
2608
+ find_path: function(graph, s, d) {
2609
+ var predecessors = dijkstra.single_source_shortest_paths(graph, s, d);
2610
+ return dijkstra.extract_shortest_path_from_predecessor_list(
2611
+ predecessors, d);
2612
+ },
2613
+
2614
+ /**
2615
+ * A very naive priority queue implementation.
2616
+ */
2617
+ PriorityQueue: {
2618
+ make: function (opts) {
2619
+ var T = dijkstra.PriorityQueue,
2620
+ t = {},
2621
+ key;
2622
+ opts = opts || {};
2623
+ for (key in T) {
2624
+ if (T.hasOwnProperty(key)) {
2625
+ t[key] = T[key];
2626
+ }
2627
+ }
2628
+ t.queue = [];
2629
+ t.sorter = opts.sorter || T.default_sorter;
2630
+ return t;
2631
+ },
2632
+
2633
+ default_sorter: function (a, b) {
2634
+ return a.cost - b.cost;
2635
+ },
2636
+
2637
+ /**
2638
+ * Add a new item to the queue and ensure the highest priority element
2639
+ * is at the front of the queue.
2640
+ */
2641
+ push: function (value, cost) {
2642
+ var item = {value: value, cost: cost};
2643
+ this.queue.push(item);
2644
+ this.queue.sort(this.sorter);
2645
+ },
2646
+
2647
+ /**
2648
+ * Return the highest priority element in the queue.
2649
+ */
2650
+ pop: function () {
2651
+ return this.queue.shift();
2652
+ },
2653
+
2654
+ empty: function () {
2655
+ return this.queue.length === 0;
2656
+ }
2657
+ }
2658
+ };
2659
+
2660
+
2661
+ // node.js module exports
2662
+ {
2663
+ module.exports = dijkstra;
2664
+ }
2665
+ } (dijkstra));
2666
+ return dijkstra.exports;
2667
+ }
2668
+
2669
+ var hasRequiredSegments;
2670
+
2671
+ function requireSegments () {
2672
+ if (hasRequiredSegments) return segments;
2673
+ hasRequiredSegments = 1;
2674
+ (function (exports$1) {
2675
+ const Mode = requireMode();
2676
+ const NumericData = requireNumericData();
2677
+ const AlphanumericData = requireAlphanumericData();
2678
+ const ByteData = requireByteData();
2679
+ const KanjiData = requireKanjiData();
2680
+ const Regex = requireRegex();
2681
+ const Utils = requireUtils$1();
2682
+ const dijkstra = requireDijkstra();
2683
+
2684
+ /**
2685
+ * Returns UTF8 byte length
2686
+ *
2687
+ * @param {String} str Input string
2688
+ * @return {Number} Number of byte
2689
+ */
2690
+ function getStringByteLength (str) {
2691
+ return unescape(encodeURIComponent(str)).length
2692
+ }
2693
+
2694
+ /**
2695
+ * Get a list of segments of the specified mode
2696
+ * from a string
2697
+ *
2698
+ * @param {Mode} mode Segment mode
2699
+ * @param {String} str String to process
2700
+ * @return {Array} Array of object with segments data
2701
+ */
2702
+ function getSegments (regex, mode, str) {
2703
+ const segments = [];
2704
+ let result;
2705
+
2706
+ while ((result = regex.exec(str)) !== null) {
2707
+ segments.push({
2708
+ data: result[0],
2709
+ index: result.index,
2710
+ mode: mode,
2711
+ length: result[0].length
2712
+ });
2713
+ }
2714
+
2715
+ return segments
2716
+ }
2717
+
2718
+ /**
2719
+ * Extracts a series of segments with the appropriate
2720
+ * modes from a string
2721
+ *
2722
+ * @param {String} dataStr Input string
2723
+ * @return {Array} Array of object with segments data
2724
+ */
2725
+ function getSegmentsFromString (dataStr) {
2726
+ const numSegs = getSegments(Regex.NUMERIC, Mode.NUMERIC, dataStr);
2727
+ const alphaNumSegs = getSegments(Regex.ALPHANUMERIC, Mode.ALPHANUMERIC, dataStr);
2728
+ let byteSegs;
2729
+ let kanjiSegs;
2730
+
2731
+ if (Utils.isKanjiModeEnabled()) {
2732
+ byteSegs = getSegments(Regex.BYTE, Mode.BYTE, dataStr);
2733
+ kanjiSegs = getSegments(Regex.KANJI, Mode.KANJI, dataStr);
2734
+ } else {
2735
+ byteSegs = getSegments(Regex.BYTE_KANJI, Mode.BYTE, dataStr);
2736
+ kanjiSegs = [];
2737
+ }
2738
+
2739
+ const segs = numSegs.concat(alphaNumSegs, byteSegs, kanjiSegs);
2740
+
2741
+ return segs
2742
+ .sort(function (s1, s2) {
2743
+ return s1.index - s2.index
2744
+ })
2745
+ .map(function (obj) {
2746
+ return {
2747
+ data: obj.data,
2748
+ mode: obj.mode,
2749
+ length: obj.length
2750
+ }
2751
+ })
2752
+ }
2753
+
2754
+ /**
2755
+ * Returns how many bits are needed to encode a string of
2756
+ * specified length with the specified mode
2757
+ *
2758
+ * @param {Number} length String length
2759
+ * @param {Mode} mode Segment mode
2760
+ * @return {Number} Bit length
2761
+ */
2762
+ function getSegmentBitsLength (length, mode) {
2763
+ switch (mode) {
2764
+ case Mode.NUMERIC:
2765
+ return NumericData.getBitsLength(length)
2766
+ case Mode.ALPHANUMERIC:
2767
+ return AlphanumericData.getBitsLength(length)
2768
+ case Mode.KANJI:
2769
+ return KanjiData.getBitsLength(length)
2770
+ case Mode.BYTE:
2771
+ return ByteData.getBitsLength(length)
2772
+ }
2773
+ }
2774
+
2775
+ /**
2776
+ * Merges adjacent segments which have the same mode
2777
+ *
2778
+ * @param {Array} segs Array of object with segments data
2779
+ * @return {Array} Array of object with segments data
2780
+ */
2781
+ function mergeSegments (segs) {
2782
+ return segs.reduce(function (acc, curr) {
2783
+ const prevSeg = acc.length - 1 >= 0 ? acc[acc.length - 1] : null;
2784
+ if (prevSeg && prevSeg.mode === curr.mode) {
2785
+ acc[acc.length - 1].data += curr.data;
2786
+ return acc
2787
+ }
2788
+
2789
+ acc.push(curr);
2790
+ return acc
2791
+ }, [])
2792
+ }
2793
+
2794
+ /**
2795
+ * Generates a list of all possible nodes combination which
2796
+ * will be used to build a segments graph.
2797
+ *
2798
+ * Nodes are divided by groups. Each group will contain a list of all the modes
2799
+ * in which is possible to encode the given text.
2800
+ *
2801
+ * For example the text '12345' can be encoded as Numeric, Alphanumeric or Byte.
2802
+ * The group for '12345' will contain then 3 objects, one for each
2803
+ * possible encoding mode.
2804
+ *
2805
+ * Each node represents a possible segment.
2806
+ *
2807
+ * @param {Array} segs Array of object with segments data
2808
+ * @return {Array} Array of object with segments data
2809
+ */
2810
+ function buildNodes (segs) {
2811
+ const nodes = [];
2812
+ for (let i = 0; i < segs.length; i++) {
2813
+ const seg = segs[i];
2814
+
2815
+ switch (seg.mode) {
2816
+ case Mode.NUMERIC:
2817
+ nodes.push([seg,
2818
+ { data: seg.data, mode: Mode.ALPHANUMERIC, length: seg.length },
2819
+ { data: seg.data, mode: Mode.BYTE, length: seg.length }
2820
+ ]);
2821
+ break
2822
+ case Mode.ALPHANUMERIC:
2823
+ nodes.push([seg,
2824
+ { data: seg.data, mode: Mode.BYTE, length: seg.length }
2825
+ ]);
2826
+ break
2827
+ case Mode.KANJI:
2828
+ nodes.push([seg,
2829
+ { data: seg.data, mode: Mode.BYTE, length: getStringByteLength(seg.data) }
2830
+ ]);
2831
+ break
2832
+ case Mode.BYTE:
2833
+ nodes.push([
2834
+ { data: seg.data, mode: Mode.BYTE, length: getStringByteLength(seg.data) }
2835
+ ]);
2836
+ }
2837
+ }
2838
+
2839
+ return nodes
2840
+ }
2841
+
2842
+ /**
2843
+ * Builds a graph from a list of nodes.
2844
+ * All segments in each node group will be connected with all the segments of
2845
+ * the next group and so on.
2846
+ *
2847
+ * At each connection will be assigned a weight depending on the
2848
+ * segment's byte length.
2849
+ *
2850
+ * @param {Array} nodes Array of object with segments data
2851
+ * @param {Number} version QR Code version
2852
+ * @return {Object} Graph of all possible segments
2853
+ */
2854
+ function buildGraph (nodes, version) {
2855
+ const table = {};
2856
+ const graph = { start: {} };
2857
+ let prevNodeIds = ['start'];
2858
+
2859
+ for (let i = 0; i < nodes.length; i++) {
2860
+ const nodeGroup = nodes[i];
2861
+ const currentNodeIds = [];
2862
+
2863
+ for (let j = 0; j < nodeGroup.length; j++) {
2864
+ const node = nodeGroup[j];
2865
+ const key = '' + i + j;
2866
+
2867
+ currentNodeIds.push(key);
2868
+ table[key] = { node: node, lastCount: 0 };
2869
+ graph[key] = {};
2870
+
2871
+ for (let n = 0; n < prevNodeIds.length; n++) {
2872
+ const prevNodeId = prevNodeIds[n];
2873
+
2874
+ if (table[prevNodeId] && table[prevNodeId].node.mode === node.mode) {
2875
+ graph[prevNodeId][key] =
2876
+ getSegmentBitsLength(table[prevNodeId].lastCount + node.length, node.mode) -
2877
+ getSegmentBitsLength(table[prevNodeId].lastCount, node.mode);
2878
+
2879
+ table[prevNodeId].lastCount += node.length;
2880
+ } else {
2881
+ if (table[prevNodeId]) table[prevNodeId].lastCount = node.length;
2882
+
2883
+ graph[prevNodeId][key] = getSegmentBitsLength(node.length, node.mode) +
2884
+ 4 + Mode.getCharCountIndicator(node.mode, version); // switch cost
2885
+ }
2886
+ }
2887
+ }
2888
+
2889
+ prevNodeIds = currentNodeIds;
2890
+ }
2891
+
2892
+ for (let n = 0; n < prevNodeIds.length; n++) {
2893
+ graph[prevNodeIds[n]].end = 0;
2894
+ }
2895
+
2896
+ return { map: graph, table: table }
2897
+ }
2898
+
2899
+ /**
2900
+ * Builds a segment from a specified data and mode.
2901
+ * If a mode is not specified, the more suitable will be used.
2902
+ *
2903
+ * @param {String} data Input data
2904
+ * @param {Mode | String} modesHint Data mode
2905
+ * @return {Segment} Segment
2906
+ */
2907
+ function buildSingleSegment (data, modesHint) {
2908
+ let mode;
2909
+ const bestMode = Mode.getBestModeForData(data);
2910
+
2911
+ mode = Mode.from(modesHint, bestMode);
2912
+
2913
+ // Make sure data can be encoded
2914
+ if (mode !== Mode.BYTE && mode.bit < bestMode.bit) {
2915
+ throw new Error('"' + data + '"' +
2916
+ ' cannot be encoded with mode ' + Mode.toString(mode) +
2917
+ '.\n Suggested mode is: ' + Mode.toString(bestMode))
2918
+ }
2919
+
2920
+ // Use Mode.BYTE if Kanji support is disabled
2921
+ if (mode === Mode.KANJI && !Utils.isKanjiModeEnabled()) {
2922
+ mode = Mode.BYTE;
2923
+ }
2924
+
2925
+ switch (mode) {
2926
+ case Mode.NUMERIC:
2927
+ return new NumericData(data)
2928
+
2929
+ case Mode.ALPHANUMERIC:
2930
+ return new AlphanumericData(data)
2931
+
2932
+ case Mode.KANJI:
2933
+ return new KanjiData(data)
2934
+
2935
+ case Mode.BYTE:
2936
+ return new ByteData(data)
2937
+ }
2938
+ }
2939
+
2940
+ /**
2941
+ * Builds a list of segments from an array.
2942
+ * Array can contain Strings or Objects with segment's info.
2943
+ *
2944
+ * For each item which is a string, will be generated a segment with the given
2945
+ * string and the more appropriate encoding mode.
2946
+ *
2947
+ * For each item which is an object, will be generated a segment with the given
2948
+ * data and mode.
2949
+ * Objects must contain at least the property "data".
2950
+ * If property "mode" is not present, the more suitable mode will be used.
2951
+ *
2952
+ * @param {Array} array Array of objects with segments data
2953
+ * @return {Array} Array of Segments
2954
+ */
2955
+ exports$1.fromArray = function fromArray (array) {
2956
+ return array.reduce(function (acc, seg) {
2957
+ if (typeof seg === 'string') {
2958
+ acc.push(buildSingleSegment(seg, null));
2959
+ } else if (seg.data) {
2960
+ acc.push(buildSingleSegment(seg.data, seg.mode));
2961
+ }
2962
+
2963
+ return acc
2964
+ }, [])
2965
+ };
2966
+
2967
+ /**
2968
+ * Builds an optimized sequence of segments from a string,
2969
+ * which will produce the shortest possible bitstream.
2970
+ *
2971
+ * @param {String} data Input string
2972
+ * @param {Number} version QR Code version
2973
+ * @return {Array} Array of segments
2974
+ */
2975
+ exports$1.fromString = function fromString (data, version) {
2976
+ const segs = getSegmentsFromString(data, Utils.isKanjiModeEnabled());
2977
+
2978
+ const nodes = buildNodes(segs);
2979
+ const graph = buildGraph(nodes, version);
2980
+ const path = dijkstra.find_path(graph.map, 'start', 'end');
2981
+
2982
+ const optimizedSegs = [];
2983
+ for (let i = 1; i < path.length - 1; i++) {
2984
+ optimizedSegs.push(graph.table[path[i]].node);
2985
+ }
2986
+
2987
+ return exports$1.fromArray(mergeSegments(optimizedSegs))
2988
+ };
2989
+
2990
+ /**
2991
+ * Splits a string in various segments with the modes which
2992
+ * best represent their content.
2993
+ * The produced segments are far from being optimized.
2994
+ * The output of this function is only used to estimate a QR Code version
2995
+ * which may contain the data.
2996
+ *
2997
+ * @param {string} data Input string
2998
+ * @return {Array} Array of segments
2999
+ */
3000
+ exports$1.rawSplit = function rawSplit (data) {
3001
+ return exports$1.fromArray(
3002
+ getSegmentsFromString(data, Utils.isKanjiModeEnabled())
3003
+ )
3004
+ };
3005
+ } (segments));
3006
+ return segments;
3007
+ }
3008
+
3009
+ var hasRequiredQrcode;
3010
+
3011
+ function requireQrcode () {
3012
+ if (hasRequiredQrcode) return qrcode;
3013
+ hasRequiredQrcode = 1;
3014
+ const Utils = requireUtils$1();
3015
+ const ECLevel = requireErrorCorrectionLevel();
3016
+ const BitBuffer = requireBitBuffer();
3017
+ const BitMatrix = requireBitMatrix();
3018
+ const AlignmentPattern = requireAlignmentPattern();
3019
+ const FinderPattern = requireFinderPattern();
3020
+ const MaskPattern = requireMaskPattern();
3021
+ const ECCode = requireErrorCorrectionCode();
3022
+ const ReedSolomonEncoder = requireReedSolomonEncoder();
3023
+ const Version = requireVersion();
3024
+ const FormatInfo = requireFormatInfo();
3025
+ const Mode = requireMode();
3026
+ const Segments = requireSegments();
3027
+
3028
+ /**
3029
+ * QRCode for JavaScript
3030
+ *
3031
+ * modified by Ryan Day for nodejs support
3032
+ * Copyright (c) 2011 Ryan Day
3033
+ *
3034
+ * Licensed under the MIT license:
3035
+ * http://www.opensource.org/licenses/mit-license.php
3036
+ *
3037
+ //---------------------------------------------------------------------
3038
+ // QRCode for JavaScript
3039
+ //
3040
+ // Copyright (c) 2009 Kazuhiko Arase
3041
+ //
3042
+ // URL: http://www.d-project.com/
3043
+ //
3044
+ // Licensed under the MIT license:
3045
+ // http://www.opensource.org/licenses/mit-license.php
3046
+ //
3047
+ // The word "QR Code" is registered trademark of
3048
+ // DENSO WAVE INCORPORATED
3049
+ // http://www.denso-wave.com/qrcode/faqpatent-e.html
3050
+ //
3051
+ //---------------------------------------------------------------------
3052
+ */
3053
+
3054
+ /**
3055
+ * Add finder patterns bits to matrix
3056
+ *
3057
+ * @param {BitMatrix} matrix Modules matrix
3058
+ * @param {Number} version QR Code version
3059
+ */
3060
+ function setupFinderPattern (matrix, version) {
3061
+ const size = matrix.size;
3062
+ const pos = FinderPattern.getPositions(version);
3063
+
3064
+ for (let i = 0; i < pos.length; i++) {
3065
+ const row = pos[i][0];
3066
+ const col = pos[i][1];
3067
+
3068
+ for (let r = -1; r <= 7; r++) {
3069
+ if (row + r <= -1 || size <= row + r) continue
3070
+
3071
+ for (let c = -1; c <= 7; c++) {
3072
+ if (col + c <= -1 || size <= col + c) continue
3073
+
3074
+ if ((r >= 0 && r <= 6 && (c === 0 || c === 6)) ||
3075
+ (c >= 0 && c <= 6 && (r === 0 || r === 6)) ||
3076
+ (r >= 2 && r <= 4 && c >= 2 && c <= 4)) {
3077
+ matrix.set(row + r, col + c, true, true);
3078
+ } else {
3079
+ matrix.set(row + r, col + c, false, true);
3080
+ }
3081
+ }
3082
+ }
3083
+ }
3084
+ }
3085
+
3086
+ /**
3087
+ * Add timing pattern bits to matrix
3088
+ *
3089
+ * Note: this function must be called before {@link setupAlignmentPattern}
3090
+ *
3091
+ * @param {BitMatrix} matrix Modules matrix
3092
+ */
3093
+ function setupTimingPattern (matrix) {
3094
+ const size = matrix.size;
3095
+
3096
+ for (let r = 8; r < size - 8; r++) {
3097
+ const value = r % 2 === 0;
3098
+ matrix.set(r, 6, value, true);
3099
+ matrix.set(6, r, value, true);
3100
+ }
3101
+ }
3102
+
3103
+ /**
3104
+ * Add alignment patterns bits to matrix
3105
+ *
3106
+ * Note: this function must be called after {@link setupTimingPattern}
3107
+ *
3108
+ * @param {BitMatrix} matrix Modules matrix
3109
+ * @param {Number} version QR Code version
3110
+ */
3111
+ function setupAlignmentPattern (matrix, version) {
3112
+ const pos = AlignmentPattern.getPositions(version);
3113
+
3114
+ for (let i = 0; i < pos.length; i++) {
3115
+ const row = pos[i][0];
3116
+ const col = pos[i][1];
3117
+
3118
+ for (let r = -2; r <= 2; r++) {
3119
+ for (let c = -2; c <= 2; c++) {
3120
+ if (r === -2 || r === 2 || c === -2 || c === 2 ||
3121
+ (r === 0 && c === 0)) {
3122
+ matrix.set(row + r, col + c, true, true);
3123
+ } else {
3124
+ matrix.set(row + r, col + c, false, true);
3125
+ }
3126
+ }
3127
+ }
3128
+ }
3129
+ }
3130
+
3131
+ /**
3132
+ * Add version info bits to matrix
3133
+ *
3134
+ * @param {BitMatrix} matrix Modules matrix
3135
+ * @param {Number} version QR Code version
3136
+ */
3137
+ function setupVersionInfo (matrix, version) {
3138
+ const size = matrix.size;
3139
+ const bits = Version.getEncodedBits(version);
3140
+ let row, col, mod;
3141
+
3142
+ for (let i = 0; i < 18; i++) {
3143
+ row = Math.floor(i / 3);
3144
+ col = i % 3 + size - 8 - 3;
3145
+ mod = ((bits >> i) & 1) === 1;
3146
+
3147
+ matrix.set(row, col, mod, true);
3148
+ matrix.set(col, row, mod, true);
3149
+ }
3150
+ }
3151
+
3152
+ /**
3153
+ * Add format info bits to matrix
3154
+ *
3155
+ * @param {BitMatrix} matrix Modules matrix
3156
+ * @param {ErrorCorrectionLevel} errorCorrectionLevel Error correction level
3157
+ * @param {Number} maskPattern Mask pattern reference value
3158
+ */
3159
+ function setupFormatInfo (matrix, errorCorrectionLevel, maskPattern) {
3160
+ const size = matrix.size;
3161
+ const bits = FormatInfo.getEncodedBits(errorCorrectionLevel, maskPattern);
3162
+ let i, mod;
3163
+
3164
+ for (i = 0; i < 15; i++) {
3165
+ mod = ((bits >> i) & 1) === 1;
3166
+
3167
+ // vertical
3168
+ if (i < 6) {
3169
+ matrix.set(i, 8, mod, true);
3170
+ } else if (i < 8) {
3171
+ matrix.set(i + 1, 8, mod, true);
3172
+ } else {
3173
+ matrix.set(size - 15 + i, 8, mod, true);
3174
+ }
3175
+
3176
+ // horizontal
3177
+ if (i < 8) {
3178
+ matrix.set(8, size - i - 1, mod, true);
3179
+ } else if (i < 9) {
3180
+ matrix.set(8, 15 - i - 1 + 1, mod, true);
3181
+ } else {
3182
+ matrix.set(8, 15 - i - 1, mod, true);
3183
+ }
3184
+ }
3185
+
3186
+ // fixed module
3187
+ matrix.set(size - 8, 8, 1, true);
3188
+ }
3189
+
3190
+ /**
3191
+ * Add encoded data bits to matrix
3192
+ *
3193
+ * @param {BitMatrix} matrix Modules matrix
3194
+ * @param {Uint8Array} data Data codewords
3195
+ */
3196
+ function setupData (matrix, data) {
3197
+ const size = matrix.size;
3198
+ let inc = -1;
3199
+ let row = size - 1;
3200
+ let bitIndex = 7;
3201
+ let byteIndex = 0;
3202
+
3203
+ for (let col = size - 1; col > 0; col -= 2) {
3204
+ if (col === 6) col--;
3205
+
3206
+ while (true) {
3207
+ for (let c = 0; c < 2; c++) {
3208
+ if (!matrix.isReserved(row, col - c)) {
3209
+ let dark = false;
3210
+
3211
+ if (byteIndex < data.length) {
3212
+ dark = (((data[byteIndex] >>> bitIndex) & 1) === 1);
3213
+ }
3214
+
3215
+ matrix.set(row, col - c, dark);
3216
+ bitIndex--;
3217
+
3218
+ if (bitIndex === -1) {
3219
+ byteIndex++;
3220
+ bitIndex = 7;
3221
+ }
3222
+ }
3223
+ }
3224
+
3225
+ row += inc;
3226
+
3227
+ if (row < 0 || size <= row) {
3228
+ row -= inc;
3229
+ inc = -inc;
3230
+ break
3231
+ }
3232
+ }
3233
+ }
3234
+ }
3235
+
3236
+ /**
3237
+ * Create encoded codewords from data input
3238
+ *
3239
+ * @param {Number} version QR Code version
3240
+ * @param {ErrorCorrectionLevel} errorCorrectionLevel Error correction level
3241
+ * @param {ByteData} data Data input
3242
+ * @return {Uint8Array} Buffer containing encoded codewords
3243
+ */
3244
+ function createData (version, errorCorrectionLevel, segments) {
3245
+ // Prepare data buffer
3246
+ const buffer = new BitBuffer();
3247
+
3248
+ segments.forEach(function (data) {
3249
+ // prefix data with mode indicator (4 bits)
3250
+ buffer.put(data.mode.bit, 4);
3251
+
3252
+ // Prefix data with character count indicator.
3253
+ // The character count indicator is a string of bits that represents the
3254
+ // number of characters that are being encoded.
3255
+ // The character count indicator must be placed after the mode indicator
3256
+ // and must be a certain number of bits long, depending on the QR version
3257
+ // and data mode
3258
+ // @see {@link Mode.getCharCountIndicator}.
3259
+ buffer.put(data.getLength(), Mode.getCharCountIndicator(data.mode, version));
3260
+
3261
+ // add binary data sequence to buffer
3262
+ data.write(buffer);
3263
+ });
3264
+
3265
+ // Calculate required number of bits
3266
+ const totalCodewords = Utils.getSymbolTotalCodewords(version);
3267
+ const ecTotalCodewords = ECCode.getTotalCodewordsCount(version, errorCorrectionLevel);
3268
+ const dataTotalCodewordsBits = (totalCodewords - ecTotalCodewords) * 8;
3269
+
3270
+ // Add a terminator.
3271
+ // If the bit string is shorter than the total number of required bits,
3272
+ // a terminator of up to four 0s must be added to the right side of the string.
3273
+ // If the bit string is more than four bits shorter than the required number of bits,
3274
+ // add four 0s to the end.
3275
+ if (buffer.getLengthInBits() + 4 <= dataTotalCodewordsBits) {
3276
+ buffer.put(0, 4);
3277
+ }
3278
+
3279
+ // If the bit string is fewer than four bits shorter, add only the number of 0s that
3280
+ // are needed to reach the required number of bits.
3281
+
3282
+ // After adding the terminator, if the number of bits in the string is not a multiple of 8,
3283
+ // pad the string on the right with 0s to make the string's length a multiple of 8.
3284
+ while (buffer.getLengthInBits() % 8 !== 0) {
3285
+ buffer.putBit(0);
3286
+ }
3287
+
3288
+ // Add pad bytes if the string is still shorter than the total number of required bits.
3289
+ // Extend the buffer to fill the data capacity of the symbol corresponding to
3290
+ // the Version and Error Correction Level by adding the Pad Codewords 11101100 (0xEC)
3291
+ // and 00010001 (0x11) alternately.
3292
+ const remainingByte = (dataTotalCodewordsBits - buffer.getLengthInBits()) / 8;
3293
+ for (let i = 0; i < remainingByte; i++) {
3294
+ buffer.put(i % 2 ? 0x11 : 0xEC, 8);
3295
+ }
3296
+
3297
+ return createCodewords(buffer, version, errorCorrectionLevel)
3298
+ }
3299
+
3300
+ /**
3301
+ * Encode input data with Reed-Solomon and return codewords with
3302
+ * relative error correction bits
3303
+ *
3304
+ * @param {BitBuffer} bitBuffer Data to encode
3305
+ * @param {Number} version QR Code version
3306
+ * @param {ErrorCorrectionLevel} errorCorrectionLevel Error correction level
3307
+ * @return {Uint8Array} Buffer containing encoded codewords
3308
+ */
3309
+ function createCodewords (bitBuffer, version, errorCorrectionLevel) {
3310
+ // Total codewords for this QR code version (Data + Error correction)
3311
+ const totalCodewords = Utils.getSymbolTotalCodewords(version);
3312
+
3313
+ // Total number of error correction codewords
3314
+ const ecTotalCodewords = ECCode.getTotalCodewordsCount(version, errorCorrectionLevel);
3315
+
3316
+ // Total number of data codewords
3317
+ const dataTotalCodewords = totalCodewords - ecTotalCodewords;
3318
+
3319
+ // Total number of blocks
3320
+ const ecTotalBlocks = ECCode.getBlocksCount(version, errorCorrectionLevel);
3321
+
3322
+ // Calculate how many blocks each group should contain
3323
+ const blocksInGroup2 = totalCodewords % ecTotalBlocks;
3324
+ const blocksInGroup1 = ecTotalBlocks - blocksInGroup2;
3325
+
3326
+ const totalCodewordsInGroup1 = Math.floor(totalCodewords / ecTotalBlocks);
3327
+
3328
+ const dataCodewordsInGroup1 = Math.floor(dataTotalCodewords / ecTotalBlocks);
3329
+ const dataCodewordsInGroup2 = dataCodewordsInGroup1 + 1;
3330
+
3331
+ // Number of EC codewords is the same for both groups
3332
+ const ecCount = totalCodewordsInGroup1 - dataCodewordsInGroup1;
3333
+
3334
+ // Initialize a Reed-Solomon encoder with a generator polynomial of degree ecCount
3335
+ const rs = new ReedSolomonEncoder(ecCount);
3336
+
3337
+ let offset = 0;
3338
+ const dcData = new Array(ecTotalBlocks);
3339
+ const ecData = new Array(ecTotalBlocks);
3340
+ let maxDataSize = 0;
3341
+ const buffer = new Uint8Array(bitBuffer.buffer);
3342
+
3343
+ // Divide the buffer into the required number of blocks
3344
+ for (let b = 0; b < ecTotalBlocks; b++) {
3345
+ const dataSize = b < blocksInGroup1 ? dataCodewordsInGroup1 : dataCodewordsInGroup2;
3346
+
3347
+ // extract a block of data from buffer
3348
+ dcData[b] = buffer.slice(offset, offset + dataSize);
3349
+
3350
+ // Calculate EC codewords for this data block
3351
+ ecData[b] = rs.encode(dcData[b]);
3352
+
3353
+ offset += dataSize;
3354
+ maxDataSize = Math.max(maxDataSize, dataSize);
3355
+ }
3356
+
3357
+ // Create final data
3358
+ // Interleave the data and error correction codewords from each block
3359
+ const data = new Uint8Array(totalCodewords);
3360
+ let index = 0;
3361
+ let i, r;
3362
+
3363
+ // Add data codewords
3364
+ for (i = 0; i < maxDataSize; i++) {
3365
+ for (r = 0; r < ecTotalBlocks; r++) {
3366
+ if (i < dcData[r].length) {
3367
+ data[index++] = dcData[r][i];
3368
+ }
3369
+ }
3370
+ }
3371
+
3372
+ // Apped EC codewords
3373
+ for (i = 0; i < ecCount; i++) {
3374
+ for (r = 0; r < ecTotalBlocks; r++) {
3375
+ data[index++] = ecData[r][i];
3376
+ }
3377
+ }
3378
+
3379
+ return data
3380
+ }
3381
+
3382
+ /**
3383
+ * Build QR Code symbol
3384
+ *
3385
+ * @param {String} data Input string
3386
+ * @param {Number} version QR Code version
3387
+ * @param {ErrorCorretionLevel} errorCorrectionLevel Error level
3388
+ * @param {MaskPattern} maskPattern Mask pattern
3389
+ * @return {Object} Object containing symbol data
3390
+ */
3391
+ function createSymbol (data, version, errorCorrectionLevel, maskPattern) {
3392
+ let segments;
3393
+
3394
+ if (Array.isArray(data)) {
3395
+ segments = Segments.fromArray(data);
3396
+ } else if (typeof data === 'string') {
3397
+ let estimatedVersion = version;
3398
+
3399
+ if (!estimatedVersion) {
3400
+ const rawSegments = Segments.rawSplit(data);
3401
+
3402
+ // Estimate best version that can contain raw splitted segments
3403
+ estimatedVersion = Version.getBestVersionForData(rawSegments, errorCorrectionLevel);
3404
+ }
3405
+
3406
+ // Build optimized segments
3407
+ // If estimated version is undefined, try with the highest version
3408
+ segments = Segments.fromString(data, estimatedVersion || 40);
3409
+ } else {
3410
+ throw new Error('Invalid data')
3411
+ }
3412
+
3413
+ // Get the min version that can contain data
3414
+ const bestVersion = Version.getBestVersionForData(segments, errorCorrectionLevel);
3415
+
3416
+ // If no version is found, data cannot be stored
3417
+ if (!bestVersion) {
3418
+ throw new Error('The amount of data is too big to be stored in a QR Code')
3419
+ }
3420
+
3421
+ // If not specified, use min version as default
3422
+ if (!version) {
3423
+ version = bestVersion;
3424
+
3425
+ // Check if the specified version can contain the data
3426
+ } else if (version < bestVersion) {
3427
+ throw new Error('\n' +
3428
+ 'The chosen QR Code version cannot contain this amount of data.\n' +
3429
+ 'Minimum version required to store current data is: ' + bestVersion + '.\n'
3430
+ )
3431
+ }
3432
+
3433
+ const dataBits = createData(version, errorCorrectionLevel, segments);
3434
+
3435
+ // Allocate matrix buffer
3436
+ const moduleCount = Utils.getSymbolSize(version);
3437
+ const modules = new BitMatrix(moduleCount);
3438
+
3439
+ // Add function modules
3440
+ setupFinderPattern(modules, version);
3441
+ setupTimingPattern(modules);
3442
+ setupAlignmentPattern(modules, version);
3443
+
3444
+ // Add temporary dummy bits for format info just to set them as reserved.
3445
+ // This is needed to prevent these bits from being masked by {@link MaskPattern.applyMask}
3446
+ // since the masking operation must be performed only on the encoding region.
3447
+ // These blocks will be replaced with correct values later in code.
3448
+ setupFormatInfo(modules, errorCorrectionLevel, 0);
3449
+
3450
+ if (version >= 7) {
3451
+ setupVersionInfo(modules, version);
3452
+ }
3453
+
3454
+ // Add data codewords
3455
+ setupData(modules, dataBits);
3456
+
3457
+ if (isNaN(maskPattern)) {
3458
+ // Find best mask pattern
3459
+ maskPattern = MaskPattern.getBestMask(modules,
3460
+ setupFormatInfo.bind(null, modules, errorCorrectionLevel));
3461
+ }
3462
+
3463
+ // Apply mask pattern
3464
+ MaskPattern.applyMask(maskPattern, modules);
3465
+
3466
+ // Replace format info bits with correct values
3467
+ setupFormatInfo(modules, errorCorrectionLevel, maskPattern);
3468
+
3469
+ return {
3470
+ modules: modules,
3471
+ version: version,
3472
+ errorCorrectionLevel: errorCorrectionLevel,
3473
+ maskPattern: maskPattern,
3474
+ segments: segments
3475
+ }
3476
+ }
3477
+
3478
+ /**
3479
+ * QR Code
3480
+ *
3481
+ * @param {String | Array} data Input data
3482
+ * @param {Object} options Optional configurations
3483
+ * @param {Number} options.version QR Code version
3484
+ * @param {String} options.errorCorrectionLevel Error correction level
3485
+ * @param {Function} options.toSJISFunc Helper func to convert utf8 to sjis
3486
+ */
3487
+ qrcode.create = function create (data, options) {
3488
+ if (typeof data === 'undefined' || data === '') {
3489
+ throw new Error('No input text')
3490
+ }
3491
+
3492
+ let errorCorrectionLevel = ECLevel.M;
3493
+ let version;
3494
+ let mask;
3495
+
3496
+ if (typeof options !== 'undefined') {
3497
+ // Use higher error correction level as default
3498
+ errorCorrectionLevel = ECLevel.from(options.errorCorrectionLevel, ECLevel.M);
3499
+ version = Version.from(options.version);
3500
+ mask = MaskPattern.from(options.maskPattern);
3501
+
3502
+ if (options.toSJISFunc) {
3503
+ Utils.setToSJISFunction(options.toSJISFunc);
3504
+ }
3505
+ }
3506
+
3507
+ return createSymbol(data, version, errorCorrectionLevel, mask)
3508
+ };
3509
+ return qrcode;
3510
+ }
3511
+
3512
+ var canvas = {};
3513
+
3514
+ var utils = {};
3515
+
3516
+ var hasRequiredUtils;
3517
+
3518
+ function requireUtils () {
3519
+ if (hasRequiredUtils) return utils;
3520
+ hasRequiredUtils = 1;
3521
+ (function (exports$1) {
3522
+ function hex2rgba (hex) {
3523
+ if (typeof hex === 'number') {
3524
+ hex = hex.toString();
3525
+ }
3526
+
3527
+ if (typeof hex !== 'string') {
3528
+ throw new Error('Color should be defined as hex string')
3529
+ }
3530
+
3531
+ let hexCode = hex.slice().replace('#', '').split('');
3532
+ if (hexCode.length < 3 || hexCode.length === 5 || hexCode.length > 8) {
3533
+ throw new Error('Invalid hex color: ' + hex)
3534
+ }
3535
+
3536
+ // Convert from short to long form (fff -> ffffff)
3537
+ if (hexCode.length === 3 || hexCode.length === 4) {
3538
+ hexCode = Array.prototype.concat.apply([], hexCode.map(function (c) {
3539
+ return [c, c]
3540
+ }));
3541
+ }
3542
+
3543
+ // Add default alpha value
3544
+ if (hexCode.length === 6) hexCode.push('F', 'F');
3545
+
3546
+ const hexValue = parseInt(hexCode.join(''), 16);
3547
+
3548
+ return {
3549
+ r: (hexValue >> 24) & 255,
3550
+ g: (hexValue >> 16) & 255,
3551
+ b: (hexValue >> 8) & 255,
3552
+ a: hexValue & 255,
3553
+ hex: '#' + hexCode.slice(0, 6).join('')
3554
+ }
3555
+ }
3556
+
3557
+ exports$1.getOptions = function getOptions (options) {
3558
+ if (!options) options = {};
3559
+ if (!options.color) options.color = {};
3560
+
3561
+ const margin = typeof options.margin === 'undefined' ||
3562
+ options.margin === null ||
3563
+ options.margin < 0
3564
+ ? 4
3565
+ : options.margin;
3566
+
3567
+ const width = options.width && options.width >= 21 ? options.width : undefined;
3568
+ const scale = options.scale || 4;
3569
+
3570
+ return {
3571
+ width: width,
3572
+ scale: width ? 4 : scale,
3573
+ margin: margin,
3574
+ color: {
3575
+ dark: hex2rgba(options.color.dark || '#000000ff'),
3576
+ light: hex2rgba(options.color.light || '#ffffffff')
3577
+ },
3578
+ type: options.type,
3579
+ rendererOpts: options.rendererOpts || {}
3580
+ }
3581
+ };
3582
+
3583
+ exports$1.getScale = function getScale (qrSize, opts) {
3584
+ return opts.width && opts.width >= qrSize + opts.margin * 2
3585
+ ? opts.width / (qrSize + opts.margin * 2)
3586
+ : opts.scale
3587
+ };
3588
+
3589
+ exports$1.getImageWidth = function getImageWidth (qrSize, opts) {
3590
+ const scale = exports$1.getScale(qrSize, opts);
3591
+ return Math.floor((qrSize + opts.margin * 2) * scale)
3592
+ };
3593
+
3594
+ exports$1.qrToImageData = function qrToImageData (imgData, qr, opts) {
3595
+ const size = qr.modules.size;
3596
+ const data = qr.modules.data;
3597
+ const scale = exports$1.getScale(size, opts);
3598
+ const symbolSize = Math.floor((size + opts.margin * 2) * scale);
3599
+ const scaledMargin = opts.margin * scale;
3600
+ const palette = [opts.color.light, opts.color.dark];
3601
+
3602
+ for (let i = 0; i < symbolSize; i++) {
3603
+ for (let j = 0; j < symbolSize; j++) {
3604
+ let posDst = (i * symbolSize + j) * 4;
3605
+ let pxColor = opts.color.light;
3606
+
3607
+ if (i >= scaledMargin && j >= scaledMargin &&
3608
+ i < symbolSize - scaledMargin && j < symbolSize - scaledMargin) {
3609
+ const iSrc = Math.floor((i - scaledMargin) / scale);
3610
+ const jSrc = Math.floor((j - scaledMargin) / scale);
3611
+ pxColor = palette[data[iSrc * size + jSrc] ? 1 : 0];
3612
+ }
3613
+
3614
+ imgData[posDst++] = pxColor.r;
3615
+ imgData[posDst++] = pxColor.g;
3616
+ imgData[posDst++] = pxColor.b;
3617
+ imgData[posDst] = pxColor.a;
3618
+ }
3619
+ }
3620
+ };
3621
+ } (utils));
3622
+ return utils;
3623
+ }
3624
+
3625
+ var hasRequiredCanvas;
3626
+
3627
+ function requireCanvas () {
3628
+ if (hasRequiredCanvas) return canvas;
3629
+ hasRequiredCanvas = 1;
3630
+ (function (exports$1) {
3631
+ const Utils = requireUtils();
3632
+
3633
+ function clearCanvas (ctx, canvas, size) {
3634
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
3635
+
3636
+ if (!canvas.style) canvas.style = {};
3637
+ canvas.height = size;
3638
+ canvas.width = size;
3639
+ canvas.style.height = size + 'px';
3640
+ canvas.style.width = size + 'px';
3641
+ }
3642
+
3643
+ function getCanvasElement () {
3644
+ try {
3645
+ return document.createElement('canvas')
3646
+ } catch (e) {
3647
+ throw new Error('You need to specify a canvas element')
3648
+ }
3649
+ }
3650
+
3651
+ exports$1.render = function render (qrData, canvas, options) {
3652
+ let opts = options;
3653
+ let canvasEl = canvas;
3654
+
3655
+ if (typeof opts === 'undefined' && (!canvas || !canvas.getContext)) {
3656
+ opts = canvas;
3657
+ canvas = undefined;
3658
+ }
3659
+
3660
+ if (!canvas) {
3661
+ canvasEl = getCanvasElement();
3662
+ }
3663
+
3664
+ opts = Utils.getOptions(opts);
3665
+ const size = Utils.getImageWidth(qrData.modules.size, opts);
3666
+
3667
+ const ctx = canvasEl.getContext('2d');
3668
+ const image = ctx.createImageData(size, size);
3669
+ Utils.qrToImageData(image.data, qrData, opts);
3670
+
3671
+ clearCanvas(ctx, canvasEl, size);
3672
+ ctx.putImageData(image, 0, 0);
3673
+
3674
+ return canvasEl
3675
+ };
3676
+
3677
+ exports$1.renderToDataURL = function renderToDataURL (qrData, canvas, options) {
3678
+ let opts = options;
3679
+
3680
+ if (typeof opts === 'undefined' && (!canvas || !canvas.getContext)) {
3681
+ opts = canvas;
3682
+ canvas = undefined;
3683
+ }
3684
+
3685
+ if (!opts) opts = {};
3686
+
3687
+ const canvasEl = exports$1.render(qrData, canvas, opts);
3688
+
3689
+ const type = opts.type || 'image/png';
3690
+ const rendererOpts = opts.rendererOpts || {};
3691
+
3692
+ return canvasEl.toDataURL(type, rendererOpts.quality)
3693
+ };
3694
+ } (canvas));
3695
+ return canvas;
3696
+ }
3697
+
3698
+ var svgTag = {};
3699
+
3700
+ var hasRequiredSvgTag;
3701
+
3702
+ function requireSvgTag () {
3703
+ if (hasRequiredSvgTag) return svgTag;
3704
+ hasRequiredSvgTag = 1;
3705
+ const Utils = requireUtils();
3706
+
3707
+ function getColorAttrib (color, attrib) {
3708
+ const alpha = color.a / 255;
3709
+ const str = attrib + '="' + color.hex + '"';
3710
+
3711
+ return alpha < 1
3712
+ ? str + ' ' + attrib + '-opacity="' + alpha.toFixed(2).slice(1) + '"'
3713
+ : str
3714
+ }
3715
+
3716
+ function svgCmd (cmd, x, y) {
3717
+ let str = cmd + x;
3718
+ if (typeof y !== 'undefined') str += ' ' + y;
3719
+
3720
+ return str
3721
+ }
3722
+
3723
+ function qrToPath (data, size, margin) {
3724
+ let path = '';
3725
+ let moveBy = 0;
3726
+ let newRow = false;
3727
+ let lineLength = 0;
3728
+
3729
+ for (let i = 0; i < data.length; i++) {
3730
+ const col = Math.floor(i % size);
3731
+ const row = Math.floor(i / size);
3732
+
3733
+ if (!col && !newRow) newRow = true;
3734
+
3735
+ if (data[i]) {
3736
+ lineLength++;
3737
+
3738
+ if (!(i > 0 && col > 0 && data[i - 1])) {
3739
+ path += newRow
3740
+ ? svgCmd('M', col + margin, 0.5 + row + margin)
3741
+ : svgCmd('m', moveBy, 0);
3742
+
3743
+ moveBy = 0;
3744
+ newRow = false;
3745
+ }
3746
+
3747
+ if (!(col + 1 < size && data[i + 1])) {
3748
+ path += svgCmd('h', lineLength);
3749
+ lineLength = 0;
3750
+ }
3751
+ } else {
3752
+ moveBy++;
3753
+ }
3754
+ }
3755
+
3756
+ return path
3757
+ }
3758
+
3759
+ svgTag.render = function render (qrData, options, cb) {
3760
+ const opts = Utils.getOptions(options);
3761
+ const size = qrData.modules.size;
3762
+ const data = qrData.modules.data;
3763
+ const qrcodesize = size + opts.margin * 2;
3764
+
3765
+ const bg = !opts.color.light.a
3766
+ ? ''
3767
+ : '<path ' + getColorAttrib(opts.color.light, 'fill') +
3768
+ ' d="M0 0h' + qrcodesize + 'v' + qrcodesize + 'H0z"/>';
3769
+
3770
+ const path =
3771
+ '<path ' + getColorAttrib(opts.color.dark, 'stroke') +
3772
+ ' d="' + qrToPath(data, size, opts.margin) + '"/>';
3773
+
3774
+ const viewBox = 'viewBox="' + '0 0 ' + qrcodesize + ' ' + qrcodesize + '"';
3775
+
3776
+ const width = !opts.width ? '' : 'width="' + opts.width + '" height="' + opts.width + '" ';
3777
+
3778
+ const svgTag = '<svg xmlns="http://www.w3.org/2000/svg" ' + width + viewBox + ' shape-rendering="crispEdges">' + bg + path + '</svg>\n';
3779
+
3780
+ if (typeof cb === 'function') {
3781
+ cb(null, svgTag);
3782
+ }
3783
+
3784
+ return svgTag
3785
+ };
3786
+ return svgTag;
3787
+ }
3788
+
3789
+ var hasRequiredBrowser;
3790
+
3791
+ function requireBrowser () {
3792
+ if (hasRequiredBrowser) return browser;
3793
+ hasRequiredBrowser = 1;
3794
+ const canPromise = requireCanPromise();
3795
+
3796
+ const QRCode = requireQrcode();
3797
+ const CanvasRenderer = requireCanvas();
3798
+ const SvgRenderer = requireSvgTag();
3799
+
3800
+ function renderCanvas (renderFunc, canvas, text, opts, cb) {
3801
+ const args = [].slice.call(arguments, 1);
3802
+ const argsNum = args.length;
3803
+ const isLastArgCb = typeof args[argsNum - 1] === 'function';
3804
+
3805
+ if (!isLastArgCb && !canPromise()) {
3806
+ throw new Error('Callback required as last argument')
3807
+ }
3808
+
3809
+ if (isLastArgCb) {
3810
+ if (argsNum < 2) {
3811
+ throw new Error('Too few arguments provided')
3812
+ }
3813
+
3814
+ if (argsNum === 2) {
3815
+ cb = text;
3816
+ text = canvas;
3817
+ canvas = opts = undefined;
3818
+ } else if (argsNum === 3) {
3819
+ if (canvas.getContext && typeof cb === 'undefined') {
3820
+ cb = opts;
3821
+ opts = undefined;
3822
+ } else {
3823
+ cb = opts;
3824
+ opts = text;
3825
+ text = canvas;
3826
+ canvas = undefined;
3827
+ }
3828
+ }
3829
+ } else {
3830
+ if (argsNum < 1) {
3831
+ throw new Error('Too few arguments provided')
3832
+ }
3833
+
3834
+ if (argsNum === 1) {
3835
+ text = canvas;
3836
+ canvas = opts = undefined;
3837
+ } else if (argsNum === 2 && !canvas.getContext) {
3838
+ opts = text;
3839
+ text = canvas;
3840
+ canvas = undefined;
3841
+ }
3842
+
3843
+ return new Promise(function (resolve, reject) {
3844
+ try {
3845
+ const data = QRCode.create(text, opts);
3846
+ resolve(renderFunc(data, canvas, opts));
3847
+ } catch (e) {
3848
+ reject(e);
3849
+ }
3850
+ })
3851
+ }
3852
+
3853
+ try {
3854
+ const data = QRCode.create(text, opts);
3855
+ cb(null, renderFunc(data, canvas, opts));
3856
+ } catch (e) {
3857
+ cb(e);
3858
+ }
3859
+ }
3860
+
3861
+ browser.create = QRCode.create;
3862
+ browser.toCanvas = renderCanvas.bind(null, CanvasRenderer.render);
3863
+ browser.toDataURL = renderCanvas.bind(null, CanvasRenderer.renderToDataURL);
3864
+
3865
+ // only svg for now.
3866
+ browser.toString = renderCanvas.bind(null, function (data, _, opts) {
3867
+ return SvgRenderer.render(data, opts)
3868
+ });
3869
+ return browser;
3870
+ }
3871
+
3872
+ requireBrowser();
3873
+
3874
+ //#region src/base64Utils.ts
3875
+ function fromUint8Array(byteArray) {
3876
+ return window.btoa(String.fromCharCode.call(null, ...byteArray));
3877
+ }
3878
+ function toUint8Array(base64EncodedByteArray) {
3879
+ return new Uint8Array(window.atob(base64EncodedByteArray).split("").map((c) => c.charCodeAt(0)));
3880
+ }
3881
+ //#endregion
3882
+ //#region src/embedded-modal/loadingSpinner.ts
3883
+ const modalHtml$1 = `
3884
+ <div class="mobile-wallet-adapter-embedded-loading-indicator" role="dialog" aria-modal="true" aria-labelledby="modal-title">
3885
+ <div data-modal-close style="position: absolute; width: 100%; height: 100%;"></div>
3886
+ <div class="mobile-wallet-adapter-embedded-loading-container">
3887
+ <div class="mobile-wallet-adapter-embedded-loading-animation"></div>
3888
+ </div>
3889
+ </div>
3890
+ `;
3891
+ const css$6 = `
3892
+ .mobile-wallet-adapter-embedded-loading-indicator {
3893
+ display: flex; /* Use flexbox to center content */
3894
+ justify-content: center; /* Center horizontally */
3895
+ align-items: start; /* Center vertically */
3896
+ position: fixed; /* Stay in place */
3897
+ z-index: 1; /* Sit on top */
3898
+ left: 0;
3899
+ top: 0;
3900
+ width: 100%; /* Full width */
3901
+ height: 100%; /* Full height */
3902
+ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
3903
+ overflow-y: auto; /* enable scrolling */
3904
+ }
3905
+
3906
+ .mobile-wallet-adapter-embedded-loading-container {
3907
+ display: flex;
3908
+ margin: auto;
3909
+ }
3910
+
3911
+ .mobile-wallet-adapter-embedded-loading-animation {
3912
+ position: relative;
3913
+ left: -9999px;
3914
+ width: 10px;
3915
+ height: 10px;
3916
+ border-radius: 5px;
3917
+ background-color: var(--spinner-color);
3918
+ color: var(--spinner-color);
3919
+ box-shadow: 9984px 0 0 0 var(--spinner-color),
3920
+ 9999px 0 0 0 var(--spinner-color),
3921
+ 10014px 0 0 0 var(--spinner-color);
3922
+ animation: dot-typing 1.5s infinite linear;
3923
+ }
3924
+
3925
+ @keyframes dot-typing {
3926
+ 0% {
3927
+ box-shadow: 9984px 0 0 0 var(--spinner-color),
3928
+ 9999px 0 0 0 var(--spinner-color),
3929
+ 10014px 0 0 0 var(--spinner-color);
3930
+ }
3931
+ 16.667% {
3932
+ box-shadow: 9984px -10px 0 0 var(--spinner-color),
3933
+ 9999px 0 0 0 var(--spinner-color),
3934
+ 10014px 0 0 0 var(--spinner-color);
3935
+ }
3936
+ 33.333% {
3937
+ box-shadow: 9984px 0 0 0 var(--spinner-color),
3938
+ 9999px 0 0 0 var(--spinner-color),
3939
+ 10014px 0 0 0 var(--spinner-color);
3940
+ }
3941
+ 50% {
3942
+ box-shadow: 9984px 0 0 0 var(--spinner-color),
3943
+ 9999px -10px 0 0 var(--spinner-color),
3944
+ 10014px 0 0 0 var(--spinner-color);
3945
+ }
3946
+ 66.667% {
3947
+ box-shadow: 9984px 0 0 0 var(--spinner-color),
3948
+ 9999px 0 0 0 var(--spinner-color),
3949
+ 10014px 0 0 0 var(--spinner-color);
3950
+ }
3951
+ 83.333% {
3952
+ box-shadow: 9984px 0 0 0 var(--spinner-color),
3953
+ 9999px 0 0 0 var(--spinner-color),
3954
+ 10014px -10px 0 0 var(--spinner-color);
3955
+ }
3956
+ 100% {
3957
+ box-shadow: 9984px 0 0 0 var(--spinner-color),
3958
+ 9999px 0 0 0 var(--spinner-color),
3959
+ 10014px 0 0 0 var(--spinner-color);
3960
+ }
3961
+ }
3962
+ `;
3963
+ var EmbeddedLoadingSpinner = class {
3964
+ #root = null;
3965
+ #eventListeners = {};
3966
+ #listenersAttached = false;
3967
+ dom = null;
3968
+ constructor() {
3969
+ this.init = this.init.bind(this);
3970
+ this.#root = document.getElementById("mobile-wallet-adapter-embedded-root-ui");
3971
+ }
3972
+ async init() {
3973
+ console.log("Injecting modal");
3974
+ this.#injectHTML();
3975
+ }
3976
+ open = () => {
3977
+ console.debug("Modal open");
3978
+ this.#attachEventListeners();
3979
+ if (this.#root) this.#root.style.display = "flex";
3980
+ };
3981
+ close = (event = void 0) => {
3982
+ console.debug("Modal close");
3983
+ this.#removeEventListeners();
3984
+ if (this.#root) this.#root.style.display = "none";
3985
+ this.#eventListeners["close"]?.forEach((listener) => listener(event));
3986
+ };
3987
+ addEventListener(event, listener) {
3988
+ this.#eventListeners[event]?.push(listener) || (this.#eventListeners[event] = [listener]);
3989
+ return () => this.removeEventListener(event, listener);
3990
+ }
3991
+ removeEventListener(event, listener) {
3992
+ this.#eventListeners[event] = this.#eventListeners[event]?.filter((existingListener) => listener !== existingListener);
3993
+ }
3994
+ #injectHTML() {
3995
+ if (this.dom) return;
3996
+ this.#root = document.createElement("div");
3997
+ this.#root.id = "mobile-wallet-adapter-embedded-root-ui";
3998
+ this.#root.innerHTML = modalHtml$1;
3999
+ this.#root.style.display = "none";
4000
+ const styles = document.createElement("style");
4001
+ styles.id = "mobile-wallet-adapter-embedded-modal-styles";
4002
+ styles.textContent = css$6;
4003
+ const host = document.createElement("div");
4004
+ this.dom = host.attachShadow({ mode: "closed" });
4005
+ host.style.setProperty("--spinner-color", "#FFFFFF");
4006
+ this.dom.appendChild(styles);
4007
+ this.dom.appendChild(this.#root);
4008
+ document.body.appendChild(host);
4009
+ }
4010
+ #attachEventListeners() {
4011
+ if (!this.#root || this.#listenersAttached) return;
4012
+ [...this.#root.querySelectorAll("[data-modal-close]")].forEach((closer) => closer?.addEventListener("click", (event) => {
4013
+ this.close(event);
4014
+ }));
4015
+ window.addEventListener("load", this.close);
4016
+ document.addEventListener("keydown", this.#handleKeyDown);
4017
+ this.#listenersAttached = true;
4018
+ }
4019
+ #removeEventListeners() {
4020
+ if (!this.#listenersAttached) return;
4021
+ window.removeEventListener("load", this.close);
4022
+ document.removeEventListener("keydown", this.#handleKeyDown);
4023
+ if (!this.#root) return;
4024
+ [...this.#root.querySelectorAll("[data-modal-close]")].forEach((closer) => closer?.removeEventListener("click", this.close));
4025
+ this.#listenersAttached = false;
4026
+ }
4027
+ #handleKeyDown = (event) => {
4028
+ if (event.key === "Escape") this.close(event);
4029
+ };
4030
+ };
4031
+ //#endregion
4032
+ //#region src/embedded-modal/modal.ts
4033
+ const modalHtml = `
4034
+ <div class="mobile-wallet-adapter-embedded-modal-container" role="dialog" aria-modal="true" aria-labelledby="modal-title">
4035
+ <div data-modal-close style="position: absolute; width: 100%; height: 100%;"></div>
4036
+ <div class="mobile-wallet-adapter-embedded-modal-card">
4037
+ <div>
4038
+ <button data-modal-close class="mobile-wallet-adapter-embedded-modal-close">
4039
+ <svg width="14" height="14">
4040
+ <path d="M 6.7125,8.3036995 1.9082,13.108199 c -0.2113,0.2112 -0.4765,0.3168 -0.7957,0.3168 -0.3192,0 -0.5844,-0.1056 -0.7958,-0.3168 C 0.1056,12.896899 0,12.631699 0,12.312499 c 0,-0.3192 0.1056,-0.5844 0.3167,-0.7958 L 5.1212,6.7124995 0.3167,1.9082 C 0.1056,1.6969 0,1.4317 0,1.1125 0,0.7933 0.1056,0.5281 0.3167,0.3167 0.5281,0.1056 0.7933,0 1.1125,0 1.4317,0 1.6969,0.1056 1.9082,0.3167 L 6.7125,5.1212 11.5167,0.3167 C 11.7281,0.1056 11.9933,0 12.3125,0 c 0.3192,0 0.5844,0.1056 0.7957,0.3167 0.2112,0.2114 0.3168,0.4766 0.3168,0.7958 0,0.3192 -0.1056,0.5844 -0.3168,0.7957 L 8.3037001,6.7124995 13.1082,11.516699 c 0.2112,0.2114 0.3168,0.4766 0.3168,0.7958 0,0.3192 -0.1056,0.5844 -0.3168,0.7957 -0.2113,0.2112 -0.4765,0.3168 -0.7957,0.3168 -0.3192,0 -0.5844,-0.1056 -0.7958,-0.3168 z" />
4041
+ </svg>
4042
+ </button>
4043
+ </div>
4044
+ <div class="mobile-wallet-adapter-embedded-modal-content"></div>
4045
+ </div>
4046
+ </div>
4047
+ `;
4048
+ const css$5 = `
4049
+ .mobile-wallet-adapter-embedded-modal-container {
4050
+ display: flex; /* Use flexbox to center content */
4051
+ justify-content: center; /* Center horizontally */
4052
+ align-items: center; /* Center vertically */
4053
+ position: fixed; /* Stay in place */
4054
+ z-index: 2147483647; /* Sit on top */
4055
+ left: 0;
4056
+ top: 0;
4057
+ width: 100%; /* Full width */
4058
+ height: 100%; /* Full height */
4059
+ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
4060
+ overflow-y: auto; /* enable scrolling */
4061
+ }
4062
+
4063
+ .mobile-wallet-adapter-embedded-modal-card {
4064
+ display: flex;
4065
+ flex-direction: column;
4066
+ margin: auto 20px;
4067
+ max-width: 780px;
4068
+ padding: 20px;
4069
+ border-radius: 24px;
4070
+ background: #ffffff;
4071
+ font-family: "Inter Tight", "PT Sans", Calibri, sans-serif;
4072
+ transform: translateY(-200%);
4073
+ animation: slide-in 0.5s forwards;
4074
+ }
4075
+
4076
+ @keyframes slide-in {
4077
+ 100% { transform: translateY(0%); }
4078
+ }
4079
+
4080
+ .mobile-wallet-adapter-embedded-modal-close {
4081
+ display: flex;
4082
+ align-items: center;
4083
+ justify-content: center;
4084
+ width: 32px;
4085
+ height: 32px;
4086
+ cursor: pointer;
4087
+ background: #e4e9e9;
4088
+ border: none;
4089
+ border-radius: 50%;
4090
+ }
4091
+
4092
+ .mobile-wallet-adapter-embedded-modal-close:focus-visible {
4093
+ outline-color: red;
4094
+ }
4095
+
4096
+ .mobile-wallet-adapter-embedded-modal-close svg {
4097
+ fill: #546266;
4098
+ transition: fill 200ms ease 0s;
4099
+ }
4100
+
4101
+ .mobile-wallet-adapter-embedded-modal-close:hover svg {
4102
+ fill: #fff;
4103
+ }
4104
+ `;
4105
+ const fonts = `
4106
+ <link rel="preconnect" href="https://fonts.googleapis.com">
4107
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
4108
+ <link href="https://fonts.googleapis.com/css2?family=Inter+Tight:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
4109
+ `;
4110
+ var EmbeddedModal = class {
4111
+ #root = null;
4112
+ #eventListeners = {};
4113
+ #listenersAttached = false;
4114
+ dom = null;
4115
+ constructor() {
4116
+ this.init = this.init.bind(this);
4117
+ this.#root = document.getElementById("mobile-wallet-adapter-embedded-root-ui");
4118
+ }
4119
+ async init() {
4120
+ console.log("Injecting modal");
4121
+ this.#injectHTML();
4122
+ }
4123
+ open = () => {
4124
+ console.debug("Modal open");
4125
+ this.#attachEventListeners();
4126
+ if (this.#root) this.#root.style.display = "flex";
4127
+ };
4128
+ close = (event = void 0) => {
4129
+ console.debug("Modal close");
4130
+ this.#removeEventListeners();
4131
+ if (this.#root) this.#root.style.display = "none";
4132
+ this.#eventListeners["close"]?.forEach((listener) => listener(event));
4133
+ };
4134
+ addEventListener(event, listener) {
4135
+ this.#eventListeners[event]?.push(listener) || (this.#eventListeners[event] = [listener]);
4136
+ return () => this.removeEventListener(event, listener);
4137
+ }
4138
+ removeEventListener(event, listener) {
4139
+ this.#eventListeners[event] = this.#eventListeners[event]?.filter((existingListener) => listener !== existingListener);
4140
+ }
4141
+ #injectHTML() {
4142
+ if (document.getElementById("mobile-wallet-adapter-embedded-root-ui")) {
4143
+ if (!this.#root) this.#root = document.getElementById("mobile-wallet-adapter-embedded-root-ui");
4144
+ return;
4145
+ }
4146
+ this.#root = document.createElement("div");
4147
+ this.#root.id = "mobile-wallet-adapter-embedded-root-ui";
4148
+ this.#root.innerHTML = modalHtml;
4149
+ this.#root.style.display = "none";
4150
+ const content = this.#root.querySelector(".mobile-wallet-adapter-embedded-modal-content");
4151
+ if (content) content.innerHTML = this.contentHtml;
4152
+ const styles = document.createElement("style");
4153
+ styles.id = "mobile-wallet-adapter-embedded-modal-styles";
4154
+ styles.textContent = css$5 + this.contentStyles;
4155
+ const host = document.createElement("div");
4156
+ host.innerHTML = fonts;
4157
+ this.dom = host.attachShadow({ mode: "closed" });
4158
+ this.dom.appendChild(styles);
4159
+ this.dom.appendChild(this.#root);
4160
+ document.body.appendChild(host);
4161
+ }
4162
+ #attachEventListeners() {
4163
+ if (!this.#root || this.#listenersAttached) return;
4164
+ [...this.#root.querySelectorAll("[data-modal-close]")].forEach((closer) => closer?.addEventListener("click", this.close));
4165
+ window.addEventListener("load", this.close);
4166
+ document.addEventListener("keydown", this.#handleKeyDown);
4167
+ this.#listenersAttached = true;
4168
+ }
4169
+ #removeEventListeners() {
4170
+ if (!this.#listenersAttached) return;
4171
+ window.removeEventListener("load", this.close);
4172
+ document.removeEventListener("keydown", this.#handleKeyDown);
4173
+ if (!this.#root) return;
4174
+ [...this.#root.querySelectorAll("[data-modal-close]")].forEach((closer) => closer?.removeEventListener("click", this.close));
4175
+ this.#listenersAttached = false;
4176
+ }
4177
+ #handleKeyDown = (event) => {
4178
+ if (event.key === "Escape") this.close(event);
4179
+ };
4180
+ };
4181
+ //#endregion
4182
+ //#region src/embedded-modal/localConnectionModal.ts
4183
+ var LocalConnectionModal = class extends EmbeddedModal {
4184
+ contentStyles = css$3;
4185
+ contentHtml = ErrorDialogHtml$3;
4186
+ initWithCallback(callback) {
4187
+ super.init();
4188
+ this.#prepareLaunchAction(callback);
4189
+ }
4190
+ #prepareLaunchAction(callback) {
4191
+ const launchButton = this.dom?.getElementById("mobile-wallet-adapter-launch-action");
4192
+ const listener = async () => {
4193
+ launchButton?.removeEventListener("click", listener);
4194
+ this.close();
4195
+ callback();
4196
+ };
4197
+ launchButton?.addEventListener("click", listener);
4198
+ }
4199
+ };
4200
+ const ErrorDialogHtml$3 = `
4201
+ <svg class="mobile-wallet-adapter-embedded-modal-launch-icon" width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
4202
+ <path d="M21.6 48C7.2 48 0 40.8 0 26.4V21.6C0 7.2 7.2 0 21.6 0H26.4C40.8 0 48 7.2 48 21.6V26.4C48 40.8 40.8 48 26.4 48H21.6Z" fill="#15994E"/>
4203
+ <mask id="mask0_189_522" style="mask-type:alpha" maskUnits="userSpaceOnUse" x="8" y="8" width="32" height="32">
4204
+ <rect x="8" y="8" width="32" height="32" fill="#D9D9D9"/>
4205
+ </mask>
4206
+ <g mask="url(#mask0_189_522)">
4207
+ <mask id="mask1_189_522" style="mask-type:alpha" maskUnits="userSpaceOnUse" x="8" y="8" width="32" height="32">
4208
+ <rect x="8" y="8" width="32" height="32" fill="#D9D9D9"/>
4209
+ </mask>
4210
+ <g mask="url(#mask1_189_522)">
4211
+ <path d="M22.1092 26.1208L19.4498 23.4615C19.1736 23.1851 18.8253 23.0468 18.4048 23.0468C17.9846 23.0468 17.6363 23.1851 17.3598 23.4615C17.0836 23.7377 16.9468 24.0861 16.9495 24.5065C16.9522 24.9267 17.0916 25.275 17.3678 25.5512L21.0405 29.2238C21.3463 29.5276 21.7031 29.6795 22.1108 29.6795C22.5184 29.6795 22.8742 29.5276 23.1782 29.2238L30.5918 21.8098C30.8683 21.5336 31.0065 21.1867 31.0065 20.7692C31.0065 20.3514 30.8683 20.0044 30.5918 19.7282C30.3156 19.4517 29.9673 19.3135 29.5468 19.3135C29.1266 19.3135 28.7784 19.4517 28.5022 19.7282L22.1092 26.1208ZM23.9998 37.6042C22.113 37.6042 20.3425 37.2473 18.6885 36.5335C17.0343 35.8197 15.5954 34.8512 14.3718 33.6278C13.1485 32.4043 12.18 30.9654 11.4662 29.3112C10.7524 27.6572 10.3955 25.8867 10.3955 23.9998C10.3955 22.113 10.7524 20.3425 11.4662 18.6885C12.18 17.0343 13.1485 15.5954 14.3718 14.3718C15.5954 13.1485 17.0343 12.18 18.6885 11.4662C20.3425 10.7524 22.113 10.3955 23.9998 10.3955C25.8867 10.3955 27.6572 10.7524 29.3112 11.4662C30.9654 12.18 32.4043 13.1485 33.6278 14.3718C34.8512 15.5954 35.8197 17.0343 36.5335 18.6885C37.2473 20.3425 37.6042 22.113 37.6042 23.9998C37.6042 25.8867 37.2473 27.6572 36.5335 29.3112C35.8197 30.9654 34.8512 32.4043 33.6278 33.6278C32.4043 34.8512 30.9654 35.8197 29.3112 36.5335C27.6572 37.2473 25.8867 37.6042 23.9998 37.6042Z" fill="white"/>
4212
+ </g>
4213
+ </g>
4214
+ </svg>
4215
+ <div class="mobile-wallet-adapter-embedded-modal-title">Ready to connect!</div>
4216
+ <div>
4217
+ <button data-modal-action id="mobile-wallet-adapter-launch-action" class="mobile-wallet-adapter-embedded-modal-launch-action">
4218
+ Connect Wallet
4219
+ </button>
4220
+ </div>
4221
+ `;
4222
+ const css$3 = `
4223
+ .mobile-wallet-adapter-embedded-modal-close {
4224
+ display: none;
4225
+ }
4226
+ .mobile-wallet-adapter-embedded-modal-content {
4227
+ text-align: center;
4228
+ min-width: 300px;
4229
+ }
4230
+ .mobile-wallet-adapter-embedded-modal-launch-icon {
4231
+ margin-top: 24px;
4232
+ }
4233
+ .mobile-wallet-adapter-embedded-modal-title {
4234
+ margin: 18px 100px 30px 100px;
4235
+ color: #000000;
4236
+ font-size: 2.75em;
4237
+ font-weight: 600;
4238
+ }
4239
+ .mobile-wallet-adapter-embedded-modal-launch-action {
4240
+ display: block;
4241
+ width: 100%;
4242
+ height: 56px;
4243
+ font-size: 1.25em;
4244
+ background: #000000;
4245
+ color: #FFFFFF;
4246
+ border-radius: 18px;
4247
+ }
4248
+ /* Smaller screens */
4249
+ @media all and (max-width: 600px) {
4250
+ .mobile-wallet-adapter-embedded-modal-title {
4251
+ font-size: 1.5em;
4252
+ margin-right: 12px;
4253
+ margin-left: 12px;
4254
+ }
4255
+ }
4256
+ `;
4257
+ //#endregion
4258
+ //#region src/embedded-modal/loopbackBlockedModal.ts
4259
+ var LoopbackPermissionBlockedModal = class extends EmbeddedModal {
4260
+ contentStyles = css$2;
4261
+ get contentHtml() {
4262
+ const instructions = getIsPwaLaunchedAsApp() ? "Long press the app icon on your home screen to open site settings" : "Tap the lock or settings icon in the address bar to open site settings";
4263
+ return ErrorDialogHtml$2.replace("{{PERMISSION_INSTRUCTION_DETAIL}}", instructions);
4264
+ }
4265
+ async init() {
4266
+ super.init();
4267
+ this.#prepareLaunchAction();
4268
+ }
4269
+ #prepareLaunchAction() {
4270
+ const launchButton = this.dom?.getElementById("mobile-wallet-adapter-launch-action");
4271
+ const listener = async (event) => {
4272
+ launchButton?.removeEventListener("click", listener);
4273
+ this.close(event);
4274
+ };
4275
+ launchButton?.addEventListener("click", listener);
4276
+ }
4277
+ };
4278
+ const ErrorDialogHtml$2 = `
4279
+ <div class="mobile-wallet-adapter-embedded-modal-header">
4280
+ Local Wallet Connection
4281
+ </div>
4282
+ <svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
4283
+ <path d="M21.6 48C7.2 48 0 40.8 0 26.4V21.6C0 7.2 7.2 0 21.6 0H26.4C40.8 0 48 7.2 48 21.6V26.4C48 40.8 40.8 48 26.4 48H21.6Z" fill="#ED1515"/>
4284
+ <mask id="mask0_147_1364" style="mask-type:alpha" maskUnits="userSpaceOnUse" x="8" y="8" width="32" height="32">
4285
+ <rect x="8" y="8" width="32" height="32" fill="#D9D9D9"/>
4286
+ </mask>
4287
+ <g mask="url(#mask0_147_1364)">
4288
+ <path d="M20.1398 36.2705C19.7363 36.2705 19.3508 36.1945 18.9835 36.0425C18.6162 35.8907 18.2916 35.674 18.0098 35.3922L12.6072 29.9895C12.3254 29.7077 12.1086 29.3832 11.9568 29.0158C11.8048 28.6485 11.7288 28.2631 11.7288 27.8595V20.1395C11.7288 19.736 11.8048 19.3505 11.9568 18.9832C12.1086 18.6158 12.3254 18.2913 12.6072 18.0095L18.0098 12.6068C18.2916 12.3251 18.6162 12.1083 18.9835 11.9565C19.3508 11.8045 19.7363 11.7285 20.1398 11.7285H27.8598C28.2634 11.7285 28.6488 11.8045 29.0162 11.9565C29.3835 12.1083 29.708 12.3251 29.9898 12.6068L35.3925 18.0095C35.6743 18.2913 35.891 18.6158 36.0428 18.9832C36.1948 19.3505 36.2708 19.736 36.2708 20.1395V27.8595C36.2708 28.2631 36.1948 28.6485 36.0428 29.0158C35.891 29.3832 35.6743 29.7077 35.3925 29.9895L29.9898 35.3922C29.708 35.674 29.3835 35.8907 29.0162 36.0425C28.6488 36.1945 28.2634 36.2705 27.8598 36.2705H20.1398ZM20.1732 33.2372H27.8265L33.2375 27.8262V20.1728L27.8265 14.7618H20.1732L14.7622 20.1728V27.8262L20.1732 33.2372ZM23.9998 25.9538L26.7868 28.7408C27.0473 29.0013 27.3729 29.1302 27.7638 29.1275C28.1549 29.1248 28.4807 28.9933 28.7412 28.7328C29.0016 28.4724 29.1318 28.1466 29.1318 27.7555C29.1318 27.3646 29.0016 27.039 28.7412 26.7785L25.9542 23.9995L28.7412 21.2125C29.0016 20.9521 29.1318 20.6264 29.1318 20.2355C29.1318 19.8444 29.0016 19.5186 28.7412 19.2582C28.4807 18.9977 28.1549 18.8675 27.7638 18.8675C27.3729 18.8675 27.0473 18.9977 26.7868 19.2582L23.9998 22.0452L21.2128 19.2582C20.9524 18.9977 20.628 18.8675 20.2398 18.8675C19.8514 18.8675 19.5269 18.9977 19.2665 19.2582C19.006 19.5186 18.8758 19.8444 18.8758 20.2355C18.8758 20.6264 19.006 20.9521 19.2665 21.2125L22.0455 23.9995L19.2585 26.7865C18.998 27.047 18.8692 27.3713 18.8718 27.7595C18.8745 28.148 19.006 28.4724 19.2665 28.7328C19.5269 28.9933 19.8527 29.1235 20.2438 29.1235C20.6347 29.1235 20.9604 28.9933 21.2208 28.7328L23.9998 25.9538Z" fill="black"/>
4289
+ </g>
4290
+ </svg>
4291
+ <div class="mobile-wallet-adapter-embedded-modal-title">
4292
+ Your wallet connection is blocked
4293
+ </div>
4294
+ <div id="mobile-wallet-adapter-local-launch-message" class="mobile-wallet-adapter-embedded-modal-subtitle">
4295
+ Visit site settings in the address bar and allow "Apps on Device".
4296
+ </div>
4297
+
4298
+ <div class="mobile-wallet-adapter-embedded-modal-divider"><hr></div>
4299
+ <div class="mobile-wallet-adapter-embedded-modal-footer">
4300
+ <div class="mobile-wallet-adapter-embedded-modal-details">
4301
+ <!-- Clickable header (label associated with the checkbox) -->
4302
+ <label for="collapsible-1" class="mobile-wallet-adapter-embedded-modal-details-collapsible-header">
4303
+ <!-- Hidden checkbox to track state -->
4304
+ <input type="checkbox" id="collapsible-1" class="mobile-wallet-adapter-embedded-modal-details-collapsible-input">
4305
+ <span class="mobile-wallet-adapter-embedded-modal-details-collapsible-header-label">
4306
+ See details
4307
+ </span>
4308
+ <svg class="mobile-wallet-adapter-embedded-modal-details-collapsible-header-icon" width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
4309
+ <mask id="mask0_147_1382" style="mask-type:alpha" maskUnits="userSpaceOnUse" x="0" y="0" width="24" height="24">
4310
+ <rect width="24" height="24" fill="#D9D9D9"/>
4311
+ </mask>
4312
+ <g mask="url(#mask0_147_1382)">
4313
+ <path d="M11.9999 17.0811C11.8506 17.0811 11.7087 17.0563 11.5741 17.0067C11.4395 16.957 11.3162 16.8762 11.2042 16.7643L6.57924 12.1393C6.36801 11.9281 6.26656 11.667 6.27489 11.3561C6.28322 11.0453 6.39301 10.7842 6.60424 10.573C6.81547 10.3618 7.08069 10.2561 7.39989 10.2561C7.71909 10.2561 7.9843 10.3618 8.19554 10.573L11.9999 14.3773L15.8292 10.548C16.0405 10.3368 16.3015 10.2353 16.6124 10.2436C16.9233 10.252 17.1843 10.3618 17.3955 10.573C17.6068 10.7842 17.7124 11.0494 17.7124 11.3686C17.7124 11.6878 17.6068 11.9531 17.3955 12.1643L12.7955 16.7643C12.6836 16.8762 12.5603 16.957 12.4257 17.0067C12.2911 17.0563 12.1492 17.0811 11.9999 17.0811Z" fill="black"/>
4314
+ </g>
4315
+ </svg>
4316
+ </label>
4317
+
4318
+ <!-- Content to show/hide -->
4319
+ <ul class="mobile-wallet-adapter-embedded-modal-details-collapsible-content">
4320
+ <li>{{PERMISSION_INSTRUCTION_DETAIL}}</li>
4321
+ <li>Allow "Apps on Device"</li>
4322
+ </ul>
4323
+ </div>
4324
+ </div>
4325
+ <div>
4326
+ <button data-modal-action id="mobile-wallet-adapter-launch-action" class="mobile-wallet-adapter-embedded-modal-launch-action">
4327
+ Got it
4328
+ </button>
4329
+ </div>
4330
+ `;
4331
+ const css$2 = `
4332
+ .mobile-wallet-adapter-embedded-modal-close {
4333
+ display: none;
4334
+ }
4335
+ .mobile-wallet-adapter-embedded-modal-content {
4336
+ text-align: center;
4337
+ }
4338
+ .mobile-wallet-adapter-embedded-modal-header {
4339
+ margin: 18px auto 30px auto;
4340
+ color: #7D9093;
4341
+ font-size: 1.0em;
4342
+ font-weight: 500;
4343
+ }
4344
+ .mobile-wallet-adapter-embedded-modal-title {
4345
+ margin: 18px 100px auto 100px;
4346
+ color: #000000;
4347
+ font-size: 2.75em;
4348
+ font-weight: 600;
4349
+ }
4350
+ .mobile-wallet-adapter-embedded-modal-subtitle {
4351
+ margin: 12px 60px 30px 60px;
4352
+ color: #7D9093;
4353
+ font-size: 1.25em;
4354
+ font-weight: 400;
4355
+ }
4356
+ .mobile-wallet-adapter-embedded-modal-details-collapsible-header {
4357
+ display: flex;
4358
+ flex-direction: row;
4359
+ justify-content: space-between;
4360
+ margin: 10px auto 10px auto;
4361
+ color: #000000;
4362
+ font-size: 1.5em;
4363
+ font-weight: 600;
4364
+ cursor: pointer; /* Show pointer on hover */
4365
+ transition: background 0.2s ease; /* Smooth background change */
4366
+ }
4367
+ .mobile-wallet-adapter-embedded-modal-details-collapsible-header-icon {
4368
+ transition: rotate 0.3s ease;
4369
+ }
4370
+ .mobile-wallet-adapter-embedded-modal-details-collapsible-input {
4371
+ display: none; /* Hide the checkbox */
4372
+ }
4373
+ .mobile-wallet-adapter-embedded-modal-details-collapsible-content {
4374
+ margin: 0px auto 40px auto;
4375
+ max-height: 0px; /* Collapse content */
4376
+ overflow: hidden; /* Hide overflow when collapsed */
4377
+ transition: max-height 0.3s ease; /* Smooth transition */
4378
+ }
4379
+ .mobile-wallet-adapter-embedded-modal-details-collapsible-content li {
4380
+ margin: 20px auto;
4381
+ color: #000000;
4382
+ font-size: 1.25em;
4383
+ font-weight: 400;
4384
+ text-align: left;
4385
+ }
4386
+ /* When checkbox is checked, show content */
4387
+ .mobile-wallet-adapter-embedded-modal-details-collapsible-header:has(> input:checked) ~ .mobile-wallet-adapter-embedded-modal-details-collapsible-content {
4388
+ max-height: 300px;
4389
+ }
4390
+ .mobile-wallet-adapter-embedded-modal-details-collapsible-header:has(> input:checked) > .mobile-wallet-adapter-embedded-modal-details-collapsible-header-icon {
4391
+ rotate: 180deg;
4392
+ }
4393
+ .mobile-wallet-adapter-embedded-modal-launch-action {
4394
+ display: block;
4395
+ width: 100%;
4396
+ height: 56px;
4397
+ /*margin-top: 40px;*/
4398
+ font-size: 1.25em;
4399
+ /*line-height: 24px;*/
4400
+ /*letter-spacing: -1%;*/
4401
+ background: #000000;
4402
+ color: #FFFFFF;
4403
+ border-radius: 18px;
4404
+ }
4405
+ /* Smaller screens */
4406
+ @media all and (max-width: 600px) {
4407
+ .mobile-wallet-adapter-embedded-modal-title {
4408
+ font-size: 1.75em;
4409
+ margin-right: 12px;
4410
+ margin-left: 12px;
4411
+ }
4412
+ .mobile-wallet-adapter-embedded-modal-subtitle {
4413
+ margin-right: 12px;
4414
+ margin-left: 12px;
4415
+ }
4416
+ }
4417
+ `;
4418
+ //#endregion
4419
+ //#region src/embedded-modal/loopbackPermissionModal.ts
4420
+ var LoopbackPermissionModal = class extends EmbeddedModal {
4421
+ contentStyles = css$1;
4422
+ contentHtml = ErrorDialogHtml$1;
4423
+ async init() {
4424
+ super.init();
4425
+ this.#prepareLaunchAction();
4426
+ }
4427
+ #prepareLaunchAction() {
4428
+ const launchButton = this.dom?.getElementById("mobile-wallet-adapter-launch-action");
4429
+ const listener = async () => {
4430
+ launchButton?.removeEventListener("click", listener);
4431
+ try {
4432
+ await fetch("http://localhost");
4433
+ } catch {}
4434
+ this.close();
4435
+ };
4436
+ launchButton?.addEventListener("click", listener);
4437
+ }
4438
+ };
4439
+ const ErrorDialogHtml$1 = `
4440
+ <div class="mobile-wallet-adapter-embedded-modal-title">Allow connections to your wallet</div>
4441
+ <div id="mobile-wallet-adapter-local-launch-message" class="mobile-wallet-adapter-embedded-modal-subtitle">
4442
+ Tap "Allow" on the next screen
4443
+ </div>
4444
+ <svg class="mobile-wallet-adapter-embedded-modal-permission-prompt-mock" xmlns="http://www.w3.org/2000/svg" width="281" height="83" viewBox="0 0 281 83" fill="none">
4445
+ <rect width="281" height="83" rx="22" fill="#F0F3F5"/>
4446
+ <path d="M254.194 64L252.626 56.657H254.047L254.866 61.452L254.985 62.278H255.02L255.146 61.452L255.993 57.497H257.4L258.254 61.431L258.373 62.278H258.415L258.534 61.431L259.346 56.657H260.718L259.143 64H257.673L256.826 59.961L256.693 59.093H256.651L256.511 59.961L255.664 64H254.194Z" fill="black"/>
4447
+ <path d="M248.837 64.231C248.147 64.231 247.54 64.07 247.017 63.748C246.495 63.426 246.086 62.978 245.792 62.404C245.498 61.83 245.351 61.1673 245.351 60.416V60.241C245.351 59.4897 245.498 58.827 245.792 58.253C246.086 57.679 246.495 57.2333 247.017 56.916C247.54 56.594 248.147 56.433 248.837 56.433C249.528 56.433 250.135 56.594 250.657 56.916C251.18 57.2333 251.588 57.679 251.882 58.253C252.176 58.827 252.323 59.4897 252.323 60.241V60.416C252.323 61.1673 252.176 61.83 251.882 62.404C251.588 62.978 251.18 63.426 250.657 63.748C250.135 64.07 249.528 64.231 248.837 64.231ZM248.837 62.824C249.43 62.824 249.897 62.607 250.237 62.173C250.583 61.7343 250.755 61.1417 250.755 60.395V60.262C250.755 59.5107 250.583 58.918 250.237 58.484C249.897 58.05 249.43 57.833 248.837 57.833C248.249 57.833 247.783 58.05 247.437 58.484C247.092 58.918 246.919 59.5107 246.919 60.262V60.395C246.919 61.1417 247.092 61.7343 247.437 62.173C247.783 62.607 248.249 62.824 248.837 62.824Z" fill="black"/>
4448
+ <path d="M242.298 64.231C241.467 64.231 240.814 63.993 240.338 63.517C239.866 63.0364 239.631 62.3737 239.631 61.529V53.78H241.178V61.389C241.178 62.3317 241.591 62.803 242.417 62.803C242.65 62.803 242.865 62.7587 243.061 62.67C243.257 62.5814 243.464 62.4367 243.684 62.236L244.538 63.377C244.225 63.6664 243.884 63.881 243.516 64.021C243.152 64.161 242.746 64.231 242.298 64.231ZM237.51 55.061V53.78H240.611V55.061H237.51Z" fill="black"/>
4449
+ <path d="M234.463 64.231C233.633 64.231 232.979 63.993 232.503 63.517C232.032 63.0364 231.796 62.3737 231.796 61.529V53.78H233.343V61.389C233.343 62.3317 233.756 62.803 234.582 62.803C234.816 62.803 235.03 62.7587 235.226 62.67C235.422 62.5814 235.63 62.4367 235.849 62.236L236.703 63.377C236.391 63.6664 236.05 63.881 235.681 64.021C235.317 64.161 234.911 64.231 234.463 64.231ZM229.675 55.061V53.78H232.776V55.061H229.675Z" fill="black"/>
4450
+ <path d="M221.442 64L224.557 53.976H226.132L229.233 64H227.581L225.642 56.972L225.341 55.761H225.299L225.005 56.972L223.073 64H221.442ZM222.835 61.634L223.255 60.29H227.371L227.805 61.634H222.835Z" fill="black"/>
4451
+ <path d="M178.261 64L175.034 60.066V60.024L178.121 56.657H180.011L176.504 60.423V59.632L180.165 64H178.261ZM173.543 64V53.78H175.097V64H173.543Z" fill="#7D9093" fill-opacity="0.5"/>
4452
+ <path d="M169.306 64.224C168.588 64.224 167.958 64.0653 167.416 63.748C166.88 63.426 166.462 62.9803 166.163 62.411C165.865 61.837 165.715 61.1673 165.715 60.402V60.248C165.715 59.4873 165.862 58.8223 166.156 58.253C166.45 57.679 166.863 57.2333 167.395 56.916C167.927 56.594 168.546 56.433 169.25 56.433C169.978 56.433 170.59 56.6056 171.084 56.951C171.579 57.2917 171.955 57.777 172.211 58.407L170.874 58.995C170.72 58.6123 170.508 58.323 170.237 58.127C169.967 57.9263 169.633 57.826 169.236 57.826C168.63 57.826 168.149 58.0383 167.794 58.463C167.444 58.883 167.269 59.4616 167.269 60.199V60.465C167.269 61.1837 167.454 61.7577 167.822 62.187C168.196 62.6163 168.69 62.831 169.306 62.831C169.712 62.831 170.06 62.733 170.349 62.537C170.639 62.341 170.877 62.0423 171.063 61.641L172.379 62.285C172.188 62.6957 171.941 63.0457 171.637 63.335C171.334 63.6243 170.986 63.846 170.594 64C170.202 64.1493 169.773 64.224 169.306 64.224Z" fill="#7D9093" fill-opacity="0.5"/>
4453
+ <path d="M161.003 64.231C160.312 64.231 159.706 64.07 159.183 63.748C158.66 63.426 158.252 62.978 157.958 62.404C157.664 61.83 157.517 61.1673 157.517 60.416V60.241C157.517 59.4897 157.664 58.827 157.958 58.253C158.252 57.679 158.66 57.2333 159.183 56.916C159.706 56.594 160.312 56.433 161.003 56.433C161.694 56.433 162.3 56.594 162.823 56.916C163.346 57.2333 163.754 57.679 164.048 58.253C164.342 58.827 164.489 59.4897 164.489 60.241V60.416C164.489 61.1673 164.342 61.83 164.048 62.404C163.754 62.978 163.346 63.426 162.823 63.748C162.3 64.07 161.694 64.231 161.003 64.231ZM161.003 62.824C161.596 62.824 162.062 62.607 162.403 62.173C162.748 61.7343 162.921 61.1417 162.921 60.395V60.262C162.921 59.5107 162.748 58.918 162.403 58.484C162.062 58.05 161.596 57.833 161.003 57.833C160.415 57.833 159.948 58.05 159.603 58.484C159.258 58.918 159.085 59.5107 159.085 60.262V60.395C159.085 61.1417 159.258 61.7343 159.603 62.173C159.948 62.607 160.415 62.824 161.003 62.824Z" fill="#7D9093" fill-opacity="0.5"/>
4454
+ <path d="M154.463 64.231C153.633 64.231 152.979 63.993 152.503 63.517C152.032 63.0364 151.796 62.3737 151.796 61.529V53.78H153.343V61.389C153.343 62.3317 153.756 62.803 154.582 62.803C154.816 62.803 155.03 62.7587 155.226 62.67C155.422 62.5814 155.63 62.4367 155.849 62.236L156.703 63.377C156.391 63.6664 156.05 63.881 155.681 64.021C155.317 64.161 154.911 64.231 154.463 64.231ZM149.675 55.061V53.78H152.776V55.061H149.675Z" fill="#7D9093" fill-opacity="0.5"/>
4455
+ <path d="M142.24 64V53.976H145.544C146.421 53.976 147.112 54.1953 147.616 54.634C148.12 55.0726 148.372 55.6583 148.372 56.391V56.566C148.372 57.0886 148.246 57.5366 147.994 57.91C147.742 58.2833 147.38 58.5586 146.909 58.736V58.792C147.492 58.9226 147.947 59.2003 148.274 59.625C148.605 60.045 148.771 60.5606 148.771 61.172V61.361C148.771 61.893 148.645 62.3573 148.393 62.754C148.145 63.1506 147.795 63.4586 147.343 63.678C146.895 63.8926 146.365 64 145.754 64H142.24ZM143.794 62.656H145.572C146.085 62.656 146.482 62.5253 146.762 62.264C147.042 62.0026 147.182 61.6293 147.182 61.144V60.99C147.182 60.5046 147.037 60.1313 146.748 59.87C146.463 59.604 146.05 59.471 145.509 59.471H143.36V58.183H145.32C145.791 58.183 146.153 58.064 146.405 57.826C146.657 57.588 146.783 57.2496 146.783 56.811V56.685C146.783 56.2416 146.657 55.9033 146.405 55.67C146.157 55.4366 145.796 55.32 145.32 55.32H143.794V62.656Z" fill="#7D9093" fill-opacity="0.5"/>
4456
+ <rect x="18" y="17" width="246" height="7" rx="3.5" fill="#7D9093" fill-opacity="0.26"/>
4457
+ <rect x="18" y="33" width="82" height="7" rx="3.5" fill="#7D9093" fill-opacity="0.26"/>
4458
+ </svg>
4459
+ <div>
4460
+ <button data-modal-action id="mobile-wallet-adapter-launch-action" class="mobile-wallet-adapter-embedded-modal-launch-action">
4461
+ Continue to Allow
4462
+ </button>
4463
+ </div>
4464
+ `;
4465
+ const css$1 = `
4466
+ .mobile-wallet-adapter-embedded-modal-close {
4467
+ display: none;
4468
+ }
4469
+ .mobile-wallet-adapter-embedded-modal-content {
4470
+ text-align: center;
4471
+ }
4472
+ .mobile-wallet-adapter-embedded-modal-title {
4473
+ margin: 18px 100px auto 100px;
4474
+ color: #000000;
4475
+ font-size: 2.75em;
4476
+ font-weight: 600;
4477
+ }
4478
+ .mobile-wallet-adapter-embedded-modal-subtitle {
4479
+ margin: 20px 60px 40px 60px;
4480
+ color: #7D9093;
4481
+ font-size: 1.25em;
4482
+ font-weight: 400;
4483
+ }
4484
+ .mobile-wallet-adapter-embedded-modal-permission-prompt-mock {
4485
+ width: 90%;
4486
+ height: auto;
4487
+ margin: 0 auto 30px auto;
4488
+ display: block;
4489
+ }
4490
+ .mobile-wallet-adapter-embedded-modal-launch-action {
4491
+ display: block;
4492
+ width: 100%;
4493
+ height: 56px;
4494
+ font-size: 1.25em;
4495
+ background: #000000;
4496
+ color: #FFFFFF;
4497
+ border-radius: 18px;
4498
+ }
4499
+ /* Smaller screens */
4500
+ @media all and (max-width: 600px) {
4501
+ .mobile-wallet-adapter-embedded-modal-title {
4502
+ font-size: 1.5em;
4503
+ margin-right: 12px;
4504
+ margin-left: 12px;
4505
+ }
4506
+ .mobile-wallet-adapter-embedded-modal-subtitle {
4507
+ margin-right: 12px;
4508
+ margin-left: 12px;
4509
+ }
4510
+ }
4511
+ `;
4512
+ function isSolanaMobileWebShell(userAgentString) {
4513
+ return userAgentString.includes("Solana Mobile Web Shell");
4514
+ }
4515
+ function getIsPwaLaunchedAsApp() {
4516
+ const isAndroidTwa = typeof document !== "undefined" && document.referrer.startsWith("android-app://");
4517
+ if (typeof window == "undefined") return isAndroidTwa;
4518
+ const isStandalone = window.matchMedia("(display-mode: standalone)").matches;
4519
+ const isFullscreen = window.matchMedia("(display-mode: fullscreen)").matches;
4520
+ const isMinimalUI = window.matchMedia("(display-mode: minimal-ui)").matches;
4521
+ return isAndroidTwa || isStandalone || isFullscreen || isMinimalUI;
4522
+ }
4523
+ async function checkLocalNetworkAccessPermission() {
4524
+ if (typeof navigator !== "undefined" && isSolanaMobileWebShell(navigator.userAgent)) return;
4525
+ try {
4526
+ const lnaPermission = await navigator.permissions.query({ name: "loopback-network" });
4527
+ if (lnaPermission.state === "granted") return;
4528
+ else if (lnaPermission.state === "denied") {
4529
+ const modal = new LoopbackPermissionBlockedModal();
4530
+ modal.init();
4531
+ modal.open();
4532
+ throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_LOOPBACK_ACCESS_BLOCKED, "Local Network Access permission denied");
4533
+ } else if (lnaPermission.state === "prompt") {
4534
+ const modal = new LoopbackPermissionModal();
4535
+ if (await new Promise((resolve, reject) => {
4536
+ modal.addEventListener("close", (event) => {
4537
+ if (event) reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_ASSOCIATION_CANCELLED, "Wallet connection cancelled by user", { event }));
4538
+ });
4539
+ lnaPermission.onchange = () => {
4540
+ lnaPermission.onchange = null;
4541
+ resolve(lnaPermission.state);
4542
+ };
4543
+ modal.init();
4544
+ modal.open();
4545
+ }) === "granted") {
4546
+ const modal = new LocalConnectionModal();
4547
+ await new Promise((resolve, reject) => {
4548
+ modal.addEventListener("close", (event) => {
4549
+ if (event) reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_ASSOCIATION_CANCELLED, "Wallet connection cancelled by user", { event }));
4550
+ });
4551
+ modal.initWithCallback(async () => {
4552
+ resolve(true);
4553
+ });
4554
+ modal.open();
4555
+ });
4556
+ return;
4557
+ } else return await checkLocalNetworkAccessPermission();
4558
+ }
4559
+ throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_LOOPBACK_ACCESS_BLOCKED, "Local Network Access permission unknown");
4560
+ } catch (e) {
4561
+ if (e instanceof TypeError && (e.message.includes("loopback-network") || e.message.includes("local-network-access"))) return;
4562
+ if (e instanceof SolanaMobileWalletAdapterError) throw e;
4563
+ throw new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_LOOPBACK_ACCESS_BLOCKED, e instanceof Error ? e.message : "Local Network Access permission unknown");
4564
+ }
4565
+ }
4566
+ //#endregion
4567
+ //#region src/icon.ts
4568
+ const icon = "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik03IDIuNUgxN0MxNy44Mjg0IDIuNSAxOC41IDMuMTcxNTcgMTguNSA0VjIwQzE4LjUgMjAuODI4NCAxNy44Mjg0IDIxLjUgMTcgMjEuNUg3QzYuMTcxNTcgMjEuNSA1LjUgMjAuODI4NCA1LjUgMjBWNEM1LjUgMy4xNzE1NyA2LjE3MTU3IDIuNSA3IDIuNVpNMyA0QzMgMS43OTA4NiA0Ljc5MDg2IDAgNyAwSDE3QzE5LjIwOTEgMCAyMSAxLjc5MDg2IDIxIDRWMjBDMjEgMjIuMjA5MSAxOS4yMDkxIDI0IDE3IDI0SDdDNC43OTA4NiAyNCAzIDIyLjIwOTEgMyAyMFY0Wk0xMSA0LjYxNTM4QzEwLjQ0NzcgNC42MTUzOCAxMCA1LjA2MzEgMTAgNS42MTUzOFY2LjM4NDYyQzEwIDYuOTM2OSAxMC40NDc3IDcuMzg0NjIgMTEgNy4zODQ2MkgxM0MxMy41NTIzIDcuMzg0NjIgMTQgNi45MzY5IDE0IDYuMzg0NjJWNS42MTUzOEMxNCA1LjA2MzEgMTMuNTUyMyA0LjYxNTM4IDEzIDQuNjE1MzhIMTFaIiBmaWxsPSIjRENCOEZGIi8+Cjwvc3ZnPgo=";
4569
+ //#endregion
4570
+ //#region src/wallet.ts
4571
+ const SolanaMobileWalletAdapterWalletName = "Mobile Wallet Adapter";
4572
+ const SIGNATURE_LENGTH_IN_BYTES = 64;
4573
+ const DEFAULT_FEATURES = [
4574
+ SolanaSignAndSendTransaction,
4575
+ SolanaSignTransaction,
4576
+ SolanaSignMessage,
4577
+ SolanaSignIn
4578
+ ];
4579
+ const WALLET_ASSOCIATION_TIMEOUT = 3e4;
4580
+ function getErrorMessage(error) {
4581
+ return error instanceof Error ? error.message : "Unknown error";
4582
+ }
4583
+ var LocalSolanaMobileWalletAdapterWallet = class {
4584
+ #listeners = {};
4585
+ #version = "1.0.0";
4586
+ #name = SolanaMobileWalletAdapterWalletName;
4587
+ #url = "https://solanamobile.com/wallets";
4588
+ #icon = icon;
4589
+ #appIdentity;
4590
+ #authorization;
4591
+ #authorizationCache;
4592
+ #connecting = false;
4593
+ /**
4594
+ * Every time the connection is recycled in some way (eg. `disconnect()` is called)
4595
+ * increment this and use it to make sure that `transact` calls from the previous
4596
+ * 'generation' don't continue to do work and throw exceptions.
4597
+ */
4598
+ #connectionGeneration = 0;
4599
+ #chains = [];
4600
+ #chainSelector;
4601
+ #optionalFeatures;
4602
+ #onWalletNotFound;
4603
+ get version() {
4604
+ return this.#version;
4605
+ }
4606
+ get name() {
4607
+ return this.#name;
4608
+ }
4609
+ get url() {
4610
+ return this.#url;
4611
+ }
4612
+ get icon() {
4613
+ return this.#icon;
4614
+ }
4615
+ get chains() {
4616
+ return this.#chains;
4617
+ }
4618
+ get features() {
4619
+ return {
4620
+ [StandardConnect]: {
4621
+ version: "1.0.0",
4622
+ connect: this.#connect
4623
+ },
4624
+ [StandardDisconnect]: {
4625
+ version: "1.0.0",
4626
+ disconnect: this.#disconnect
4627
+ },
4628
+ [StandardEvents]: {
4629
+ version: "1.0.0",
4630
+ on: this.#on
4631
+ },
4632
+ [SolanaSignMessage]: {
4633
+ version: "1.0.0",
4634
+ signMessage: this.#signMessage
4635
+ },
4636
+ [SolanaSignIn]: {
4637
+ version: "1.0.0",
4638
+ signIn: this.#signIn
4639
+ },
4640
+ ...this.#optionalFeatures
4641
+ };
4642
+ }
4643
+ get accounts() {
4644
+ return this.#authorization?.accounts ?? [];
4645
+ }
4646
+ constructor(config) {
4647
+ this.#authorizationCache = config.authorizationCache;
4648
+ this.#appIdentity = config.appIdentity;
4649
+ this.#chains = config.chains;
4650
+ this.#chainSelector = config.chainSelector;
4651
+ this.#onWalletNotFound = config.onWalletNotFound;
4652
+ this.#optionalFeatures = {
4653
+ [SolanaSignAndSendTransaction]: {
4654
+ version: "1.0.0",
4655
+ supportedTransactionVersions: ["legacy", 0],
4656
+ signAndSendTransaction: this.#signAndSendTransaction
4657
+ },
4658
+ [SolanaSignTransaction]: {
4659
+ version: "1.0.0",
4660
+ supportedTransactionVersions: ["legacy", 0],
4661
+ signTransaction: this.#signTransaction
4662
+ }
4663
+ };
4664
+ }
4665
+ get connected() {
4666
+ return !!this.#authorization;
4667
+ }
4668
+ get isAuthorized() {
4669
+ return !!this.#authorization;
4670
+ }
4671
+ get currentAuthorization() {
4672
+ return this.#authorization;
4673
+ }
4674
+ get cachedAuthorizationResult() {
4675
+ return this.#authorizationCache.get();
4676
+ }
4677
+ #on = (event, listener) => {
4678
+ this.#listeners[event]?.push(listener) || (this.#listeners[event] = [listener]);
4679
+ return () => this.#off(event, listener);
4680
+ };
4681
+ #emit(event, ...args) {
4682
+ this.#listeners[event]?.forEach((listener) => listener.apply(null, args));
4683
+ }
4684
+ #off(event, listener) {
4685
+ this.#listeners[event] = this.#listeners[event]?.filter((existingListener) => listener !== existingListener);
4686
+ }
4687
+ #connect = async ({ silent } = {}) => {
4688
+ if (this.#connecting || this.connected) return { accounts: this.accounts };
4689
+ this.#connecting = true;
4690
+ try {
4691
+ if (silent) {
4692
+ const cachedAuthorization = await this.#authorizationCache.get();
4693
+ if (cachedAuthorization) {
4694
+ await this.#handleWalletCapabilitiesResult(cachedAuthorization.capabilities);
4695
+ await this.#handleAuthorizationResult(cachedAuthorization);
4696
+ } else return { accounts: this.accounts };
4697
+ } else await this.#performAuthorization();
4698
+ } catch (e) {
4699
+ throw new Error(getErrorMessage(e), { cause: e });
4700
+ } finally {
4701
+ this.#connecting = false;
4702
+ }
4703
+ return { accounts: this.accounts };
4704
+ };
4705
+ #performAuthorization = async (signInPayload) => {
4706
+ try {
4707
+ const cachedAuthorizationResult = await this.#authorizationCache.get();
4708
+ if (cachedAuthorizationResult) {
4709
+ this.#handleAuthorizationResult(cachedAuthorizationResult);
4710
+ return cachedAuthorizationResult;
4711
+ }
4712
+ const selectedChain = await this.#chainSelector.select(this.#chains);
4713
+ return await this.#transact(async (wallet) => {
4714
+ const [capabilities, mwaAuthorizationResult] = await Promise.all([wallet.getCapabilities(), wallet.authorize({
4715
+ chain: selectedChain,
4716
+ identity: this.#appIdentity,
4717
+ sign_in_payload: signInPayload
4718
+ })]);
4719
+ const accounts = this.#accountsToWalletStandardAccounts(mwaAuthorizationResult.accounts);
4720
+ const authorization = {
4721
+ ...mwaAuthorizationResult,
4722
+ accounts,
4723
+ chain: selectedChain,
4724
+ capabilities
4725
+ };
4726
+ Promise.all([
4727
+ this.#handleWalletCapabilitiesResult(capabilities),
4728
+ this.#authorizationCache.set(authorization),
4729
+ this.#handleAuthorizationResult(authorization)
4730
+ ]);
4731
+ return authorization;
4732
+ });
4733
+ } catch (e) {
4734
+ throw new Error(getErrorMessage(e), { cause: e });
4735
+ }
4736
+ };
4737
+ #handleAuthorizationResult = async (authorization) => {
4738
+ const didPublicKeysChange = this.#authorization == null || this.#authorization?.accounts.length !== authorization.accounts.length || this.#authorization.accounts.some((account, ii) => account.address !== authorization.accounts[ii].address);
4739
+ this.#authorization = authorization;
4740
+ if (didPublicKeysChange) this.#emit("change", { accounts: this.accounts });
4741
+ };
4742
+ #handleWalletCapabilitiesResult = async (capabilities) => {
4743
+ const supportsSignTransaction = capabilities.features.includes("solana:signTransactions");
4744
+ const supportsSignAndSendTransaction = capabilities.supports_sign_and_send_transactions;
4745
+ const didCapabilitiesChange = SolanaSignAndSendTransaction in this.features !== supportsSignAndSendTransaction || SolanaSignTransaction in this.features !== supportsSignTransaction;
4746
+ this.#optionalFeatures = {
4747
+ ...(supportsSignAndSendTransaction || !supportsSignAndSendTransaction && !supportsSignTransaction) && { [SolanaSignAndSendTransaction]: {
4748
+ version: "1.0.0",
4749
+ supportedTransactionVersions: ["legacy", 0],
4750
+ signAndSendTransaction: this.#signAndSendTransaction
4751
+ } },
4752
+ ...supportsSignTransaction && { [SolanaSignTransaction]: {
4753
+ version: "1.0.0",
4754
+ supportedTransactionVersions: ["legacy", 0],
4755
+ signTransaction: this.#signTransaction
4756
+ } }
4757
+ };
4758
+ if (didCapabilitiesChange) this.#emit("change", { features: this.features });
4759
+ };
4760
+ #performReauthorization = async (wallet, authToken, chain) => {
4761
+ try {
4762
+ const [capabilities, mwaAuthorizationResult] = await Promise.all([this.#authorization?.capabilities ?? await wallet.getCapabilities(), wallet.authorize({
4763
+ auth_token: authToken,
4764
+ identity: this.#appIdentity,
4765
+ chain
4766
+ })]);
4767
+ const accounts = this.#accountsToWalletStandardAccounts(mwaAuthorizationResult.accounts);
4768
+ const authorization = {
4769
+ ...mwaAuthorizationResult,
4770
+ accounts,
4771
+ chain,
4772
+ capabilities
4773
+ };
4774
+ Promise.all([this.#authorizationCache.set(authorization), this.#handleAuthorizationResult(authorization)]);
4775
+ } catch (e) {
4776
+ this.#disconnect();
4777
+ throw new Error(getErrorMessage(e), { cause: e });
4778
+ }
4779
+ };
4780
+ #disconnect = async () => {
4781
+ this.#authorizationCache.clear();
4782
+ this.#connecting = false;
4783
+ this.#connectionGeneration++;
4784
+ this.#authorization = void 0;
4785
+ this.#emit("change", { accounts: this.accounts });
4786
+ };
4787
+ #transact = async (callback) => {
4788
+ const walletUriBase = this.#authorization?.wallet_uri_base;
4789
+ const config = walletUriBase ? { baseUri: walletUriBase } : void 0;
4790
+ const currentConnectionGeneration = this.#connectionGeneration;
4791
+ const loadingSpinner = new EmbeddedLoadingSpinner();
4792
+ try {
4793
+ let associating = true;
4794
+ let timeout = void 0;
4795
+ const result = await Promise.race([checkLocalNetworkAccessPermission().then(async () => {
4796
+ loadingSpinner.init();
4797
+ const { wallet, close } = await startScenario(config);
4798
+ associating = false;
4799
+ loadingSpinner.addEventListener("close", (event) => {
4800
+ if (event) close();
4801
+ });
4802
+ loadingSpinner.open();
4803
+ const result = await callback(await wallet);
4804
+ loadingSpinner.close();
4805
+ close();
4806
+ return result;
4807
+ }), new Promise((_, reject) => {
4808
+ timeout = setTimeout(() => {
4809
+ if (associating) reject(new SolanaMobileWalletAdapterError(SolanaMobileWalletAdapterErrorCode.ERROR_ASSOCIATION_CANCELLED, "Wallet connection timed out", { event: void 0 }));
4810
+ }, WALLET_ASSOCIATION_TIMEOUT);
4811
+ })]);
4812
+ clearTimeout(timeout);
4813
+ return result;
4814
+ } catch (e) {
4815
+ loadingSpinner.close();
4816
+ if (this.#connectionGeneration !== currentConnectionGeneration) await new Promise(() => {});
4817
+ if (e instanceof Error && e.name === "SolanaMobileWalletAdapterError" && e.code === "ERROR_WALLET_NOT_FOUND") await this.#onWalletNotFound(this);
4818
+ throw e;
4819
+ }
4820
+ };
4821
+ #assertIsAuthorized = () => {
4822
+ if (!this.#authorization) throw new Error("Wallet not connected");
4823
+ return {
4824
+ authToken: this.#authorization.auth_token,
4825
+ chain: this.#authorization.chain
4826
+ };
4827
+ };
4828
+ #accountsToWalletStandardAccounts = (accounts) => {
4829
+ return accounts.map((account) => {
4830
+ const publicKey = toUint8Array(account.address);
4831
+ return {
4832
+ address: index_native.base58.encode(publicKey),
4833
+ publicKey,
4834
+ label: account.label,
4835
+ icon: account.icon,
4836
+ chains: account.chains ?? this.#chains,
4837
+ features: account.features ?? DEFAULT_FEATURES
4838
+ };
4839
+ });
4840
+ };
4841
+ #performSignTransactions = async (transactions) => {
4842
+ const { authToken, chain } = this.#assertIsAuthorized();
4843
+ try {
4844
+ const base64Transactions = transactions.map((tx) => {
4845
+ return fromUint8Array(tx);
4846
+ });
4847
+ return await this.#transact(async (wallet) => {
4848
+ await this.#performReauthorization(wallet, authToken, chain);
4849
+ return (await wallet.signTransactions({ payloads: base64Transactions })).signed_payloads.map(toUint8Array);
4850
+ });
4851
+ } catch (e) {
4852
+ throw new Error(getErrorMessage(e), { cause: e });
4853
+ }
4854
+ };
4855
+ #performSignAndSendTransaction = async (transaction, options) => {
4856
+ const { authToken, chain } = this.#assertIsAuthorized();
4857
+ try {
4858
+ return await this.#transact(async (wallet) => {
4859
+ const [capabilities] = await Promise.all([wallet.getCapabilities(), this.#performReauthorization(wallet, authToken, chain)]);
4860
+ if (capabilities.supports_sign_and_send_transactions) {
4861
+ const base64Transaction = fromUint8Array(transaction);
4862
+ return (await wallet.signAndSendTransactions({
4863
+ ...options,
4864
+ payloads: [base64Transaction]
4865
+ })).signatures.map(toUint8Array)[0];
4866
+ } else throw new Error("connected wallet does not support signAndSendTransaction");
4867
+ });
4868
+ } catch (e) {
4869
+ throw new Error(getErrorMessage(e), { cause: e });
4870
+ }
4871
+ };
4872
+ #signAndSendTransaction = async (...inputs) => {
4873
+ const outputs = [];
4874
+ for (const input of inputs) {
4875
+ const signature = await this.#performSignAndSendTransaction(input.transaction, input.options);
4876
+ outputs.push({ signature });
4877
+ }
4878
+ return outputs;
4879
+ };
4880
+ #signTransaction = async (...inputs) => {
4881
+ return (await this.#performSignTransactions(inputs.map(({ transaction }) => transaction))).map((signedTransaction) => {
4882
+ return { signedTransaction };
4883
+ });
4884
+ };
4885
+ #signMessage = async (...inputs) => {
4886
+ const { authToken, chain } = this.#assertIsAuthorized();
4887
+ const addresses = inputs.map(({ account }) => fromUint8Array(new Uint8Array(account.publicKey)));
4888
+ const messages = inputs.map(({ message }) => fromUint8Array(message));
4889
+ try {
4890
+ return await this.#transact(async (wallet) => {
4891
+ await this.#performReauthorization(wallet, authToken, chain);
4892
+ return (await wallet.signMessages({
4893
+ addresses,
4894
+ payloads: messages
4895
+ })).signed_payloads.map(toUint8Array).map((signedMessage) => {
4896
+ return {
4897
+ signedMessage,
4898
+ signature: signedMessage.slice(-SIGNATURE_LENGTH_IN_BYTES)
4899
+ };
4900
+ });
4901
+ });
4902
+ } catch (e) {
4903
+ throw new Error(getErrorMessage(e), { cause: e });
4904
+ }
4905
+ };
4906
+ #signIn = async (...inputs) => {
4907
+ const outputs = [];
4908
+ if (inputs.length > 1) for (const input of inputs) outputs.push(await this.#performSignIn(input));
4909
+ else return [await this.#performSignIn(inputs[0])];
4910
+ return outputs;
4911
+ };
4912
+ #performSignIn = async (input) => {
4913
+ this.#connecting = true;
4914
+ try {
4915
+ const authorizationResult = await this.#performAuthorization({
4916
+ ...input,
4917
+ domain: input?.domain ?? window.location.host
4918
+ });
4919
+ if (!authorizationResult.sign_in_result) throw new Error("Sign in failed, no sign in result returned by wallet");
4920
+ const signedInAddress = authorizationResult.sign_in_result.address;
4921
+ const signedInAccount = authorizationResult.accounts.find((acc) => acc.address == signedInAddress);
4922
+ return {
4923
+ account: {
4924
+ ...signedInAccount ?? { address: index_native.base58.encode(toUint8Array(signedInAddress)) },
4925
+ publicKey: toUint8Array(signedInAddress),
4926
+ chains: signedInAccount?.chains ?? this.#chains,
4927
+ features: signedInAccount?.features ?? authorizationResult.capabilities.features
4928
+ },
4929
+ signedMessage: toUint8Array(authorizationResult.sign_in_result.signed_message),
4930
+ signature: toUint8Array(authorizationResult.sign_in_result.signature)
4931
+ };
4932
+ } catch (e) {
4933
+ throw new Error(getErrorMessage(e), { cause: e });
4934
+ } finally {
4935
+ this.#connecting = false;
4936
+ }
4937
+ };
4938
+ };
4939
+ //#endregion
4940
+ //#region src/createDefaultAuthorizationCache.ts
4941
+ const CACHE_KEY = "SolanaMobileWalletAdapterDefaultAuthorizationCache";
4942
+ function createDefaultAuthorizationCache() {
4943
+ let storage;
4944
+ try {
4945
+ storage = window.localStorage;
4946
+ } catch {}
4947
+ return {
4948
+ async clear() {
4949
+ if (!storage) return;
4950
+ try {
4951
+ storage.removeItem(CACHE_KEY);
4952
+ } catch {}
4953
+ },
4954
+ async get() {
4955
+ if (!storage) return;
4956
+ try {
4957
+ const parsed = JSON.parse(storage.getItem(CACHE_KEY));
4958
+ if (parsed && parsed.accounts) {
4959
+ const parsedAccounts = parsed.accounts.map((account) => {
4960
+ return {
4961
+ ...account,
4962
+ publicKey: "publicKey" in account ? new Uint8Array(Object.values(account.publicKey)) : index_native.base58.decode(account.address)
4963
+ };
4964
+ });
4965
+ return {
4966
+ ...parsed,
4967
+ accounts: parsedAccounts
4968
+ };
4969
+ } else return parsed || void 0;
4970
+ } catch {}
4971
+ },
4972
+ async set(authorization) {
4973
+ if (!storage) return;
4974
+ try {
4975
+ storage.setItem(CACHE_KEY, JSON.stringify(authorization));
4976
+ } catch {}
4977
+ }
4978
+ };
4979
+ }
4980
+ //#endregion
4981
+ //#region src/createDefaultChainSelector.ts
4982
+ function createDefaultChainSelector() {
4983
+ return { async select(chains) {
4984
+ if (chains.length === 1) return chains[0];
4985
+ else if (chains.includes(SOLANA_MAINNET_CHAIN)) return SOLANA_MAINNET_CHAIN;
4986
+ else return chains[0];
4987
+ } };
4988
+ }
4989
+
4990
+ exports.LocalSolanaMobileWalletAdapterWallet = LocalSolanaMobileWalletAdapterWallet;
4991
+ exports.SolanaMobileWalletAdapterWalletName = SolanaMobileWalletAdapterWalletName;
4992
+ exports.createDefaultAuthorizationCache = createDefaultAuthorizationCache;
4993
+ exports.createDefaultChainSelector = createDefaultChainSelector;
4994
+ //# sourceMappingURL=index.browser-DZtZWdul.js.map