@veridex/sdk 1.0.0-beta.22 → 1.0.0-beta.23

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 (112) hide show
  1. package/dist/EVMClient-CoOR_ywD.d.mts +332 -0
  2. package/dist/auth/prepareAuth.d.mts +1 -1
  3. package/dist/auth/prepareAuth.js +749 -3
  4. package/dist/auth/prepareAuth.js.map +1 -1
  5. package/dist/auth/prepareAuth.mjs +25 -1404
  6. package/dist/auth/prepareAuth.mjs.map +1 -1
  7. package/dist/chains/aptos/index.d.mts +1 -1
  8. package/dist/chains/aptos/index.mjs +5 -574
  9. package/dist/chains/aptos/index.mjs.map +1 -1
  10. package/dist/chains/avalanche/index.d.mts +137 -0
  11. package/dist/chains/avalanche/index.js +1407 -0
  12. package/dist/chains/avalanche/index.js.map +1 -0
  13. package/dist/chains/avalanche/index.mjs +10 -0
  14. package/dist/chains/avalanche/index.mjs.map +1 -0
  15. package/dist/chains/evm/index.d.mts +4 -2
  16. package/dist/chains/evm/index.mjs +8 -1200
  17. package/dist/chains/evm/index.mjs.map +1 -1
  18. package/dist/chains/solana/index.d.mts +1 -1
  19. package/dist/chains/solana/index.mjs +4 -486
  20. package/dist/chains/solana/index.mjs.map +1 -1
  21. package/dist/chains/stacks/index.d.mts +1 -1
  22. package/dist/chains/stacks/index.mjs +36 -1114
  23. package/dist/chains/stacks/index.mjs.map +1 -1
  24. package/dist/chains/starknet/index.d.mts +1 -1
  25. package/dist/chains/starknet/index.mjs +5 -503
  26. package/dist/chains/starknet/index.mjs.map +1 -1
  27. package/dist/chains/sui/index.d.mts +1 -1
  28. package/dist/chains/sui/index.mjs +5 -529
  29. package/dist/chains/sui/index.mjs.map +1 -1
  30. package/dist/chunk-3Q34PMHU.mjs +840 -0
  31. package/dist/chunk-3Q34PMHU.mjs.map +1 -0
  32. package/dist/chunk-72ZA3OYQ.mjs +20 -0
  33. package/dist/chunk-72ZA3OYQ.mjs.map +1 -0
  34. package/dist/chunk-7IEUU6LD.mjs +549 -0
  35. package/dist/chunk-7IEUU6LD.mjs.map +1 -0
  36. package/dist/chunk-ABLEEC5N.mjs +38 -0
  37. package/dist/chunk-ABLEEC5N.mjs.map +1 -0
  38. package/dist/chunk-AORSW75A.mjs +1101 -0
  39. package/dist/chunk-AORSW75A.mjs.map +1 -0
  40. package/dist/chunk-BKTCPT7P.mjs +269 -0
  41. package/dist/chunk-BKTCPT7P.mjs.map +1 -0
  42. package/dist/chunk-CCHASDAI.mjs +330 -0
  43. package/dist/chunk-CCHASDAI.mjs.map +1 -0
  44. package/dist/chunk-FQ2WU7PX.mjs +509 -0
  45. package/dist/chunk-FQ2WU7PX.mjs.map +1 -0
  46. package/dist/chunk-JO74ENTF.mjs +417 -0
  47. package/dist/chunk-JO74ENTF.mjs.map +1 -0
  48. package/dist/chunk-K73Y36KS.mjs +407 -0
  49. package/dist/chunk-K73Y36KS.mjs.map +1 -0
  50. package/dist/chunk-KHV3EP3I.mjs +179 -0
  51. package/dist/chunk-KHV3EP3I.mjs.map +1 -0
  52. package/dist/chunk-MMSTOW4F.mjs +722 -0
  53. package/dist/chunk-MMSTOW4F.mjs.map +1 -0
  54. package/dist/chunk-MWZ5UHCF.mjs +1082 -0
  55. package/dist/chunk-MWZ5UHCF.mjs.map +1 -0
  56. package/dist/chunk-NU6JGI7L.mjs +565 -0
  57. package/dist/chunk-NU6JGI7L.mjs.map +1 -0
  58. package/dist/chunk-PIC2Y5DF.mjs +422 -0
  59. package/dist/chunk-PIC2Y5DF.mjs.map +1 -0
  60. package/dist/chunk-PPN7BG3A.mjs +464 -0
  61. package/dist/chunk-PPN7BG3A.mjs.map +1 -0
  62. package/dist/chunk-RSV7J43V.mjs +438 -0
  63. package/dist/chunk-RSV7J43V.mjs.map +1 -0
  64. package/dist/chunk-UTT6KI7N.mjs +216 -0
  65. package/dist/chunk-UTT6KI7N.mjs.map +1 -0
  66. package/dist/constants.mjs +16 -389
  67. package/dist/constants.mjs.map +1 -1
  68. package/dist/index-Du1PoZqu.d.mts +216 -0
  69. package/dist/index.d.mts +131 -9
  70. package/dist/index.js +12064 -11594
  71. package/dist/index.js.map +1 -1
  72. package/dist/index.mjs +4407 -12273
  73. package/dist/index.mjs.map +1 -1
  74. package/dist/passkey.d.mts +142 -0
  75. package/dist/passkey.js +807 -0
  76. package/dist/passkey.js.map +1 -0
  77. package/dist/passkey.mjs +15 -0
  78. package/dist/passkey.mjs.map +1 -0
  79. package/dist/payload.mjs +25 -244
  80. package/dist/payload.mjs.map +1 -1
  81. package/dist/portfolio-UV3LLWKI.mjs +13 -0
  82. package/dist/portfolio-UV3LLWKI.mjs.map +1 -0
  83. package/dist/queries/index.js +39 -1
  84. package/dist/queries/index.js.map +1 -1
  85. package/dist/queries/index.mjs +14 -1591
  86. package/dist/queries/index.mjs.map +1 -1
  87. package/dist/{types-DakHNZIP.d.ts → types-Bmk689Lw.d.mts} +1 -1
  88. package/dist/types.d.mts +10 -0
  89. package/dist/types.js.map +1 -1
  90. package/dist/utils.mjs +19 -385
  91. package/dist/utils.mjs.map +1 -1
  92. package/dist/wormhole.mjs +25 -397
  93. package/dist/wormhole.mjs.map +1 -1
  94. package/package.json +11 -1
  95. package/dist/auth/prepareAuth.d.ts +0 -25
  96. package/dist/chains/aptos/index.d.ts +0 -146
  97. package/dist/chains/evm/index.d.ts +0 -5
  98. package/dist/chains/solana/index.d.ts +0 -116
  99. package/dist/chains/stacks/index.d.ts +0 -559
  100. package/dist/chains/starknet/index.d.ts +0 -172
  101. package/dist/chains/sui/index.d.ts +0 -182
  102. package/dist/constants.d.ts +0 -150
  103. package/dist/index-Dy29mvBf.d.mts +0 -683
  104. package/dist/index-eXXqodd0.d.ts +0 -683
  105. package/dist/index.d.ts +0 -3442
  106. package/dist/payload.d.ts +0 -125
  107. package/dist/queries/index.d.ts +0 -148
  108. package/dist/types-DakHNZIP.d.mts +0 -571
  109. package/dist/types-DvFRnIBd.d.ts +0 -172
  110. package/dist/types.d.ts +0 -413
  111. package/dist/utils.d.ts +0 -81
  112. package/dist/wormhole.d.ts +0 -167
@@ -0,0 +1,722 @@
1
+ import {
2
+ base64URLDecode,
3
+ base64URLEncode,
4
+ computeKeyHash,
5
+ parseDERSignature
6
+ } from "./chunk-KHV3EP3I.mjs";
7
+
8
+ // src/core/PasskeyManager.ts
9
+ import {
10
+ startRegistration,
11
+ startAuthentication,
12
+ browserSupportsWebAuthn
13
+ } from "@simplewebauthn/browser";
14
+ import { ethers } from "ethers";
15
+ var VERIDEX_RP_ID = "veridex.network";
16
+ function detectRpId(forceLocal) {
17
+ if (typeof window === "undefined") return "localhost";
18
+ const hostname = window.location.hostname;
19
+ if (hostname === "localhost" || hostname === "127.0.0.1" || /^\d+\.\d+\.\d+\.\d+$/.test(hostname)) {
20
+ return hostname;
21
+ }
22
+ if (forceLocal) {
23
+ const parts = hostname.split(".");
24
+ if (parts.length <= 2) {
25
+ return hostname;
26
+ }
27
+ return parts.slice(-2).join(".");
28
+ }
29
+ return VERIDEX_RP_ID;
30
+ }
31
+ async function supportsRelatedOrigins() {
32
+ if (typeof window === "undefined" || !window.PublicKeyCredential) {
33
+ return false;
34
+ }
35
+ if ("getClientCapabilities" in PublicKeyCredential) {
36
+ try {
37
+ const getCapabilities = PublicKeyCredential.getClientCapabilities;
38
+ const capabilities = await getCapabilities();
39
+ return capabilities?.relatedOrigins === true;
40
+ } catch {
41
+ return false;
42
+ }
43
+ }
44
+ return false;
45
+ }
46
+ var PasskeyManager = class _PasskeyManager {
47
+ config;
48
+ credential = null;
49
+ constructor(config = {}) {
50
+ this.config = {
51
+ rpName: config.rpName ?? "Veridex Protocol",
52
+ rpId: config.rpId ?? detectRpId(),
53
+ timeout: config.timeout ?? 6e4,
54
+ userVerification: config.userVerification ?? "required",
55
+ authenticatorAttachment: config.authenticatorAttachment ?? "platform",
56
+ relayerUrl: config.relayerUrl ?? ""
57
+ };
58
+ }
59
+ static isSupported() {
60
+ return browserSupportsWebAuthn();
61
+ }
62
+ static async isPlatformAuthenticatorAvailable() {
63
+ if (typeof window === "undefined" || !window.PublicKeyCredential) {
64
+ return false;
65
+ }
66
+ return await window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable();
67
+ }
68
+ async register(username, displayName) {
69
+ if (!_PasskeyManager.isSupported()) {
70
+ throw new Error("WebAuthn is not supported in this browser");
71
+ }
72
+ const challenge = ethers.randomBytes(32);
73
+ const challengeBase64 = base64URLEncode(challenge);
74
+ const options = {
75
+ challenge: challengeBase64,
76
+ rp: {
77
+ name: this.config.rpName,
78
+ id: this.config.rpId
79
+ },
80
+ user: {
81
+ id: base64URLEncode(ethers.toUtf8Bytes(username)),
82
+ name: username,
83
+ displayName
84
+ },
85
+ pubKeyCredParams: [
86
+ { alg: -7, type: "public-key" },
87
+ // ES256 (P-256) - WebAuthn default
88
+ { alg: -257, type: "public-key" },
89
+ // RS256 - Widely supported
90
+ { alg: -8, type: "public-key" },
91
+ // EdDSA - Modern, efficient
92
+ { alg: -35, type: "public-key" },
93
+ // ES384 (P-384)
94
+ { alg: -36, type: "public-key" },
95
+ // ES512 (P-521)
96
+ { alg: -37, type: "public-key" }
97
+ // PS256 - RSA PSS
98
+ ],
99
+ authenticatorSelection: {
100
+ authenticatorAttachment: this.config.authenticatorAttachment,
101
+ userVerification: this.config.userVerification,
102
+ residentKey: "required",
103
+ requireResidentKey: true
104
+ },
105
+ timeout: this.config.timeout,
106
+ attestation: "none"
107
+ };
108
+ const response = await startRegistration(options);
109
+ const publicKey = this.extractPublicKeyFromAttestation(response);
110
+ const keyHash = computeKeyHash(publicKey.x, publicKey.y);
111
+ this.credential = {
112
+ credentialId: response.id,
113
+ publicKeyX: publicKey.x,
114
+ publicKeyY: publicKey.y,
115
+ keyHash
116
+ };
117
+ return this.credential;
118
+ }
119
+ async sign(challenge) {
120
+ if (!this.credential) {
121
+ throw new Error("No credential set. Call register() or setCredential() first.");
122
+ }
123
+ const challengeBase64 = base64URLEncode(challenge);
124
+ const options = {
125
+ challenge: challengeBase64,
126
+ rpId: this.config.rpId,
127
+ allowCredentials: [
128
+ {
129
+ id: this.credential.credentialId,
130
+ type: "public-key",
131
+ transports: ["internal"]
132
+ }
133
+ ],
134
+ userVerification: this.config.userVerification,
135
+ timeout: this.config.timeout
136
+ };
137
+ const response = await startAuthentication(options);
138
+ return this.parseAuthenticationResponse(response);
139
+ }
140
+ /**
141
+ * Authenticate using a discoverable credential (passkey)
142
+ * This allows sign-in without knowing the credential ID ahead of time.
143
+ * The authenticator will show all available passkeys for this RP.
144
+ *
145
+ * @param challenge - Optional challenge bytes. If not provided, a random challenge is used.
146
+ * @returns The credential that was used to authenticate, along with the signature
147
+ */
148
+ async authenticate(challenge) {
149
+ if (!_PasskeyManager.isSupported()) {
150
+ throw new Error("WebAuthn is not supported in this browser");
151
+ }
152
+ const actualChallenge = challenge ?? ethers.randomBytes(32);
153
+ const challengeBase64 = base64URLEncode(actualChallenge);
154
+ const options = {
155
+ challenge: challengeBase64,
156
+ rpId: this.config.rpId,
157
+ userVerification: this.config.userVerification,
158
+ timeout: this.config.timeout
159
+ };
160
+ const response = await startAuthentication(options);
161
+ const credentialId = response.id;
162
+ const signature = this.parseAuthenticationResponse(response);
163
+ let storedCredential = this.findCredentialById(credentialId);
164
+ if (storedCredential) {
165
+ this.credential = storedCredential;
166
+ return { credential: storedCredential, signature };
167
+ }
168
+ if (this.config.relayerUrl) {
169
+ storedCredential = await this.loadCredentialFromRelayer(credentialId);
170
+ if (storedCredential) {
171
+ this.credential = storedCredential;
172
+ this.addCredentialToStorage(storedCredential);
173
+ return { credential: storedCredential, signature };
174
+ }
175
+ }
176
+ const hasRelayer = !!this.config.relayerUrl;
177
+ throw new Error(
178
+ "Credential not found. This passkey was registered on a different device or the data was cleared. " + (hasRelayer ? "The credential was not found. Please register a new passkey." : "Please register a new passkey or ensure the relayer URL is configured.")
179
+ );
180
+ }
181
+ /**
182
+ * Find a credential by ID in the list of stored credentials
183
+ */
184
+ findCredentialById(credentialId) {
185
+ if (typeof window === "undefined") return null;
186
+ const stored = this.getAllStoredCredentials();
187
+ return stored.find((c) => c.credentialId === credentialId) || null;
188
+ }
189
+ /**
190
+ * Get all credentials stored in localStorage
191
+ */
192
+ getAllStoredCredentials(key = "veridex_credentials") {
193
+ if (typeof window === "undefined") return [];
194
+ const stored = localStorage.getItem(key);
195
+ if (!stored) {
196
+ const legacy = localStorage.getItem("veridex_credential");
197
+ if (legacy) {
198
+ try {
199
+ const data = JSON.parse(legacy);
200
+ const cred = this.parseStoredCredential(data);
201
+ if (cred) {
202
+ this.saveCredentials([cred], key);
203
+ localStorage.removeItem("veridex_credential");
204
+ return [cred];
205
+ }
206
+ } catch (e) {
207
+ }
208
+ }
209
+ return [];
210
+ }
211
+ try {
212
+ const data = JSON.parse(stored);
213
+ if (Array.isArray(data)) {
214
+ return data.map((item) => this.parseStoredCredential(item)).filter((c) => c !== null);
215
+ }
216
+ return [];
217
+ } catch (error) {
218
+ console.error("Failed to load credentials:", error);
219
+ return [];
220
+ }
221
+ }
222
+ parseStoredCredential(data) {
223
+ try {
224
+ return {
225
+ credentialId: data.credentialId,
226
+ publicKeyX: BigInt(data.publicKeyX),
227
+ publicKeyY: BigInt(data.publicKeyY),
228
+ keyHash: data.keyHash
229
+ };
230
+ } catch {
231
+ return null;
232
+ }
233
+ }
234
+ /**
235
+ * Save a list of credentials to localStorage
236
+ */
237
+ saveCredentials(credentials, key = "veridex_credentials") {
238
+ if (typeof window === "undefined") return;
239
+ const data = credentials.map((c) => ({
240
+ credentialId: c.credentialId,
241
+ publicKeyX: c.publicKeyX.toString(),
242
+ publicKeyY: c.publicKeyY.toString(),
243
+ keyHash: c.keyHash
244
+ }));
245
+ localStorage.setItem(key, JSON.stringify(data));
246
+ }
247
+ /**
248
+ * Add a single credential to storage (append or update)
249
+ */
250
+ addCredentialToStorage(credential, key = "veridex_credentials") {
251
+ const stored = this.getAllStoredCredentials(key);
252
+ const existingIndex = stored.findIndex((c) => c.credentialId === credential.credentialId);
253
+ if (existingIndex >= 0) {
254
+ stored[existingIndex] = credential;
255
+ } else {
256
+ stored.push(credential);
257
+ }
258
+ this.saveCredentials(stored, key);
259
+ }
260
+ /**
261
+ * Check if there's ANY stored credential for this RP
262
+ */
263
+ hasStoredCredential() {
264
+ return this.getAllStoredCredentials().length > 0;
265
+ }
266
+ getCredential() {
267
+ return this.credential;
268
+ }
269
+ setCredential(credential) {
270
+ this.credential = credential;
271
+ }
272
+ createCredentialFromPublicKey(credentialId, publicKeyX, publicKeyY) {
273
+ const keyHash = computeKeyHash(publicKeyX, publicKeyY);
274
+ this.credential = {
275
+ credentialId,
276
+ publicKeyX,
277
+ publicKeyY,
278
+ keyHash
279
+ };
280
+ return this.credential;
281
+ }
282
+ clearCredential() {
283
+ this.credential = null;
284
+ }
285
+ /**
286
+ * Save the current credential to localStorage (appends to list)
287
+ */
288
+ saveToLocalStorage(key = "veridex_credentials") {
289
+ if (!this.credential) {
290
+ throw new Error("No credential to save");
291
+ }
292
+ this.addCredentialToStorage(this.credential, key);
293
+ }
294
+ loadFromLocalStorage(key = "veridex_credentials") {
295
+ if (typeof window === "undefined") {
296
+ return null;
297
+ }
298
+ const stored = this.getAllStoredCredentials(key);
299
+ if (stored.length > 0) {
300
+ this.credential = stored[stored.length - 1];
301
+ return this.credential;
302
+ }
303
+ return null;
304
+ }
305
+ removeFromLocalStorage(key = "veridex_credentials") {
306
+ if (typeof window !== "undefined") {
307
+ localStorage.removeItem(key);
308
+ localStorage.removeItem("veridex_credential");
309
+ }
310
+ }
311
+ // =========================================================================
312
+ // Relayer-based Credential Storage (Cross-Device Recovery)
313
+ // =========================================================================
314
+ /**
315
+ * Save the current credential to the relayer for cross-device recovery.
316
+ * This should be called after registration.
317
+ */
318
+ async saveCredentialToRelayer() {
319
+ if (!this.credential) {
320
+ throw new Error("No credential to save");
321
+ }
322
+ if (!this.config.relayerUrl) {
323
+ console.warn("Relayer URL not configured; skipping remote credential storage");
324
+ return false;
325
+ }
326
+ try {
327
+ const response = await fetch(`${this.config.relayerUrl}/api/v1/credential`, {
328
+ method: "POST",
329
+ headers: { "Content-Type": "application/json" },
330
+ body: JSON.stringify({
331
+ keyHash: this.credential.keyHash,
332
+ credentialId: this.credential.credentialId,
333
+ publicKeyX: this.credential.publicKeyX.toString(),
334
+ publicKeyY: this.credential.publicKeyY.toString()
335
+ })
336
+ });
337
+ if (!response.ok) {
338
+ const errorData = await response.json().catch(() => ({}));
339
+ console.error("Failed to save credential to relayer:", errorData);
340
+ return false;
341
+ }
342
+ console.log("Credential saved to relayer for cross-device recovery");
343
+ return true;
344
+ } catch (error) {
345
+ console.error("Failed to save credential to relayer:", error);
346
+ return false;
347
+ }
348
+ }
349
+ /**
350
+ * Load a credential from the relayer by credential ID.
351
+ * Used during discoverable credential authentication when localStorage is empty.
352
+ */
353
+ async loadCredentialFromRelayer(credentialId) {
354
+ if (!this.config.relayerUrl) {
355
+ return null;
356
+ }
357
+ try {
358
+ const response = await fetch(
359
+ `${this.config.relayerUrl}/api/v1/credential/by-id/${encodeURIComponent(credentialId)}`
360
+ );
361
+ if (!response.ok) {
362
+ return null;
363
+ }
364
+ const data = await response.json();
365
+ if (!data.exists) {
366
+ return null;
367
+ }
368
+ if (!data.credentialId || !data.publicKeyX || !data.publicKeyY || !data.keyHash) {
369
+ console.warn("Relayer returned incomplete credential data:", {
370
+ hasCredentialId: !!data.credentialId,
371
+ hasPublicKeyX: !!data.publicKeyX,
372
+ hasPublicKeyY: !!data.publicKeyY,
373
+ hasKeyHash: !!data.keyHash
374
+ });
375
+ return null;
376
+ }
377
+ return {
378
+ credentialId: data.credentialId,
379
+ publicKeyX: BigInt(data.publicKeyX),
380
+ publicKeyY: BigInt(data.publicKeyY),
381
+ keyHash: data.keyHash
382
+ };
383
+ } catch (error) {
384
+ console.error("Failed to load credential from relayer:", error);
385
+ return null;
386
+ }
387
+ }
388
+ /**
389
+ * Load a credential from the relayer by keyHash.
390
+ * Useful when you know the user's keyHash but not their credential ID.
391
+ */
392
+ async loadCredentialFromRelayerByKeyHash(keyHash) {
393
+ if (!this.config.relayerUrl) {
394
+ return null;
395
+ }
396
+ try {
397
+ const response = await fetch(
398
+ `${this.config.relayerUrl}/api/v1/credential/${encodeURIComponent(keyHash)}`
399
+ );
400
+ if (!response.ok) {
401
+ return null;
402
+ }
403
+ const data = await response.json();
404
+ if (!data.exists) {
405
+ return null;
406
+ }
407
+ if (!data.credentialId || !data.publicKeyX || !data.publicKeyY || !data.keyHash) {
408
+ console.warn("Relayer returned incomplete credential data for keyHash:", {
409
+ hasCredentialId: !!data.credentialId,
410
+ hasPublicKeyX: !!data.publicKeyX,
411
+ hasPublicKeyY: !!data.publicKeyY,
412
+ hasKeyHash: !!data.keyHash
413
+ });
414
+ return null;
415
+ }
416
+ return {
417
+ credentialId: data.credentialId,
418
+ publicKeyX: BigInt(data.publicKeyX),
419
+ publicKeyY: BigInt(data.publicKeyY),
420
+ keyHash: data.keyHash
421
+ };
422
+ } catch (error) {
423
+ console.error("Failed to load credential from relayer:", error);
424
+ return null;
425
+ }
426
+ }
427
+ extractPublicKeyFromAttestation(response) {
428
+ const attestationObject = base64URLDecode(response.response.attestationObject);
429
+ let offset = 0;
430
+ if (attestationObject[offset] >= 160 && attestationObject[offset] <= 191) {
431
+ offset++;
432
+ }
433
+ while (offset < attestationObject.length - 37) {
434
+ if (attestationObject[offset] === 104 && // text string, 8 bytes
435
+ attestationObject[offset + 1] === 97 && // 'a'
436
+ attestationObject[offset + 2] === 117 && // 'u'
437
+ attestationObject[offset + 3] === 116 && // 't'
438
+ attestationObject[offset + 4] === 104 && // 'h'
439
+ attestationObject[offset + 5] === 68 && // 'D'
440
+ attestationObject[offset + 6] === 97 && // 'a'
441
+ attestationObject[offset + 7] === 116 && // 't'
442
+ attestationObject[offset + 8] === 97) {
443
+ offset += 9;
444
+ break;
445
+ }
446
+ offset++;
447
+ }
448
+ if (attestationObject[offset] === 88 || attestationObject[offset] === 89) {
449
+ const lengthBytes = attestationObject[offset] === 88 ? 1 : 2;
450
+ offset += 1 + lengthBytes;
451
+ }
452
+ offset += 32;
453
+ offset += 1;
454
+ offset += 4;
455
+ offset += 16;
456
+ const credIdLen = attestationObject[offset] << 8 | attestationObject[offset + 1];
457
+ offset += 2;
458
+ offset += credIdLen;
459
+ const coseKey = attestationObject.slice(offset);
460
+ console.log("COSE key length:", coseKey.length);
461
+ console.log("COSE key hex:", this.bytesToHex(coseKey.slice(0, Math.min(100, coseKey.length))));
462
+ const { x, y } = this.parseCOSEKey(coseKey);
463
+ return { x, y };
464
+ }
465
+ parseCOSEKey(coseKey) {
466
+ console.log("COSE key length:", coseKey.length);
467
+ console.log("COSE key hex:", this.bytesToHex(coseKey));
468
+ const parsed = this.tryParseCOSEKeyStrategies(coseKey);
469
+ if (parsed) {
470
+ return parsed;
471
+ }
472
+ return this.parseCOSEKeyWithCBORStructure(coseKey);
473
+ }
474
+ tryParseCOSEKeyStrategies(coseKey) {
475
+ const keyBytes = new Uint8Array(coseKey);
476
+ for (let i = 0; i < keyBytes.length - 40; i++) {
477
+ if (keyBytes[i] === 88 && keyBytes[i + 1] === 32) {
478
+ const potentialX = keyBytes.slice(i + 2, i + 34);
479
+ for (let j = i + 34; j < keyBytes.length - 34; j++) {
480
+ if (keyBytes[j] === 88 && keyBytes[j + 1] === 32) {
481
+ const potentialY = keyBytes.slice(j + 2, j + 34);
482
+ if (this.isValidCoordinate(potentialX) && this.isValidCoordinate(potentialY)) {
483
+ console.log("Found coordinates via pattern matching");
484
+ return {
485
+ x: this.bytesToBigInt(potentialX),
486
+ y: this.bytesToBigInt(potentialY)
487
+ };
488
+ }
489
+ }
490
+ }
491
+ }
492
+ }
493
+ return this.tryParseASN1Structure(keyBytes);
494
+ }
495
+ parseCOSEKeyWithCBORStructure(coseKey) {
496
+ const bytes = new Uint8Array(coseKey);
497
+ let xBytes = null;
498
+ let yBytes = null;
499
+ let i = 0;
500
+ while (i < bytes.length) {
501
+ if (bytes[i] === 88) {
502
+ const length = bytes[i + 1];
503
+ if (length === 32) {
504
+ const start = i + 2;
505
+ const end = start + 32;
506
+ if (end <= bytes.length) {
507
+ const coordinate = bytes.slice(start, end);
508
+ if (!xBytes) {
509
+ xBytes = coordinate;
510
+ console.log("Found x at offset", i);
511
+ } else if (!yBytes) {
512
+ yBytes = coordinate;
513
+ console.log("Found y at offset", i);
514
+ break;
515
+ }
516
+ }
517
+ i = end;
518
+ } else {
519
+ i += length + 2;
520
+ }
521
+ } else if (bytes[i] === 66) {
522
+ if (i + 3 < bytes.length) {
523
+ const length = bytes[i + 1] << 8 | bytes[i + 2];
524
+ if (length === 32) {
525
+ const start = i + 3;
526
+ const end = start + 32;
527
+ if (end <= bytes.length) {
528
+ const coordinate = bytes.slice(start, end);
529
+ if (!xBytes) {
530
+ xBytes = coordinate;
531
+ console.log("Found x at offset", i);
532
+ } else if (!yBytes) {
533
+ yBytes = coordinate;
534
+ console.log("Found y at offset", i);
535
+ break;
536
+ }
537
+ }
538
+ i = end;
539
+ } else {
540
+ i += length + 3;
541
+ }
542
+ } else {
543
+ i++;
544
+ }
545
+ } else if (bytes[i] === 64) {
546
+ i++;
547
+ while (i < bytes.length && bytes[i] !== 255) {
548
+ if (bytes[i] === 4 && i + 65 <= bytes.length) {
549
+ const x = bytes.slice(i + 1, i + 33);
550
+ const y = bytes.slice(i + 33, i + 65);
551
+ if (this.isValidCoordinate(x) && this.isValidCoordinate(y)) {
552
+ xBytes = x;
553
+ yBytes = y;
554
+ console.log("Found coordinates in uncompressed point format");
555
+ break;
556
+ }
557
+ }
558
+ i++;
559
+ }
560
+ if (xBytes && yBytes) break;
561
+ } else {
562
+ i++;
563
+ }
564
+ }
565
+ if (!xBytes || !yBytes) {
566
+ const potentialCoords = this.find32ByteSequences(bytes);
567
+ if (potentialCoords.length >= 2) {
568
+ xBytes = potentialCoords[0];
569
+ yBytes = potentialCoords[1];
570
+ console.log("Fallback: Using first two 32-byte sequences as coordinates");
571
+ }
572
+ }
573
+ if (!xBytes || !yBytes) {
574
+ console.error("Failed to find coordinates in COSE key. Full dump:");
575
+ console.error("Hex:", this.bytesToHex(bytes));
576
+ console.error("Structure analysis:");
577
+ this.analyzeCOSEStructure(bytes);
578
+ throw new Error("Failed to extract public key coordinates from COSE key. Check console for details.");
579
+ }
580
+ return {
581
+ x: this.bytesToBigInt(xBytes),
582
+ y: this.bytesToBigInt(yBytes)
583
+ };
584
+ }
585
+ tryParseASN1Structure(bytes) {
586
+ if (bytes[0] === 48) {
587
+ let offset = 2;
588
+ if (bytes[offset] === 3) {
589
+ offset += 2;
590
+ if (bytes[offset] === 48) {
591
+ offset += 2;
592
+ offset += 12;
593
+ if (bytes[offset] === 3 && bytes[offset + 2] === 4) {
594
+ offset += 3;
595
+ const x = bytes.slice(offset, offset + 32);
596
+ const y = bytes.slice(offset + 32, offset + 64);
597
+ if (x.length === 32 && y.length === 32) {
598
+ console.log("Found coordinates via ASN.1 parsing");
599
+ return {
600
+ x: this.bytesToBigInt(x),
601
+ y: this.bytesToBigInt(y)
602
+ };
603
+ }
604
+ }
605
+ }
606
+ }
607
+ }
608
+ return null;
609
+ }
610
+ find32ByteSequences(bytes) {
611
+ const sequences = [];
612
+ for (let i = 0; i <= bytes.length - 32; i++) {
613
+ const sequence = bytes.slice(i, i + 32);
614
+ if (this.isValidCoordinate(sequence)) {
615
+ sequences.push(sequence);
616
+ }
617
+ }
618
+ return sequences;
619
+ }
620
+ isValidCoordinate(bytes) {
621
+ if (bytes.length !== 32) return false;
622
+ let allZeros = true;
623
+ let allOnes = true;
624
+ for (const byte of bytes) {
625
+ if (byte !== 0) allZeros = false;
626
+ if (byte !== 255) allOnes = false;
627
+ }
628
+ return !allZeros && !allOnes;
629
+ }
630
+ bytesToBigInt(bytes) {
631
+ return BigInt("0x" + this.bytesToHex(bytes));
632
+ }
633
+ bytesToHex(bytes) {
634
+ return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
635
+ }
636
+ analyzeCOSEStructure(bytes) {
637
+ console.log("COSE Structure Analysis:");
638
+ console.log("First 20 bytes:", this.bytesToHex(bytes.slice(0, 20)));
639
+ const firstByte = bytes[0];
640
+ console.log("First byte (0x" + firstByte.toString(16) + "):");
641
+ if (firstByte >= 160 && firstByte <= 191) {
642
+ console.log("- Definite length map with", firstByte & 31, "pairs");
643
+ } else if (firstByte === 191) {
644
+ console.log("- Indefinite length map");
645
+ } else if (firstByte === 4) {
646
+ console.log("- Byte string");
647
+ } else if (firstByte === 2) {
648
+ console.log("- Negative integer");
649
+ }
650
+ let count32 = 0;
651
+ for (let i = 0; i <= bytes.length - 32; i++) {
652
+ const chunk = bytes.slice(i, i + 32);
653
+ if (this.isValidCoordinate(chunk)) {
654
+ console.log(
655
+ `Found valid 32-byte sequence at offset ${i}:`,
656
+ this.bytesToHex(chunk.slice(0, 8)) + "..."
657
+ );
658
+ count32++;
659
+ }
660
+ }
661
+ console.log(`Total valid 32-byte sequences: ${count32}`);
662
+ console.log("Looking for known markers:");
663
+ const markers = [
664
+ { byte: 4, name: "Uncompressed point marker" },
665
+ { byte: 3, name: "BIT STRING" },
666
+ { byte: 48, name: "SEQUENCE" },
667
+ { byte: 2, name: "INTEGER" },
668
+ { byte: 6, name: "OBJECT IDENTIFIER" },
669
+ { byte: 88, name: "Byte string with length byte" },
670
+ { byte: 66, name: "Byte string with 2-byte length" },
671
+ { byte: 64, name: "Byte string indefinite length" },
672
+ { byte: 160, name: "Map start" },
673
+ { byte: 191, name: "Indefinite map start" }
674
+ ];
675
+ for (const marker of markers) {
676
+ const indices = [];
677
+ for (let i = 0; i < bytes.length; i++) {
678
+ if (bytes[i] === marker.byte) {
679
+ indices.push(i);
680
+ }
681
+ }
682
+ if (indices.length > 0) {
683
+ console.log(` ${marker.name} (0x${marker.byte.toString(16)}) at positions:`, indices.slice(0, 5).join(", "));
684
+ }
685
+ }
686
+ }
687
+ parseAuthenticationResponse(response) {
688
+ const authenticatorData = base64URLDecode(response.response.authenticatorData);
689
+ const clientDataJSON = response.response.clientDataJSON;
690
+ const signature = base64URLDecode(response.response.signature);
691
+ const { r, s } = parseDERSignature(signature);
692
+ const P256_N = BigInt("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");
693
+ const P256_N_DIV_2 = BigInt(
694
+ "0x7FFFFFFF800000007FFFFFFFFFFFFFFFDE737D56D38BCF4279DCE5617E3192A8"
695
+ );
696
+ const clientDataStr = new TextDecoder().decode(base64URLDecode(clientDataJSON));
697
+ const challengeIndex = clientDataStr.indexOf('"challenge"');
698
+ const typeIndex = clientDataStr.indexOf('"type"');
699
+ if (challengeIndex === -1 || typeIndex === -1) {
700
+ throw new Error("Invalid clientDataJSON format");
701
+ }
702
+ return {
703
+ authenticatorData: ethers.hexlify(authenticatorData),
704
+ clientDataJSON: clientDataStr,
705
+ challengeIndex,
706
+ typeIndex,
707
+ r: this.bytesToBigInt(r),
708
+ s: (() => {
709
+ const sBig = this.bytesToBigInt(s);
710
+ return sBig > P256_N_DIV_2 ? P256_N - sBig : sBig;
711
+ })()
712
+ };
713
+ }
714
+ };
715
+
716
+ export {
717
+ VERIDEX_RP_ID,
718
+ detectRpId,
719
+ supportsRelatedOrigins,
720
+ PasskeyManager
721
+ };
722
+ //# sourceMappingURL=chunk-MMSTOW4F.mjs.map