@thru/passkey 0.2.15 → 0.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/server.js CHANGED
@@ -62,6 +62,14 @@ function maskForBits(bits) {
62
62
  }
63
63
 
64
64
  // src/server/utils.ts
65
+ var feePayerQueueSymbol = /* @__PURE__ */ Symbol.for("thru.sharedFeePayerQueues");
66
+ function getFeePayerQueues() {
67
+ const globalQueues = globalThis;
68
+ if (!globalQueues[feePayerQueueSymbol]) {
69
+ globalQueues[feePayerQueueSymbol] = /* @__PURE__ */ new Map();
70
+ }
71
+ return globalQueues[feePayerQueueSymbol];
72
+ }
65
73
  async function getStateProof(client, address, proofType = 1, targetSlot) {
66
74
  const proofRequest = {
67
75
  address,
@@ -77,23 +85,37 @@ async function getStateProof(client, address, proofType = 1, targetSlot) {
77
85
  return proof.proof;
78
86
  }
79
87
  async function trackTransaction(client, signature, timeoutMs = 5e3) {
88
+ let finalizedSeen = false;
80
89
  try {
81
90
  for await (const update of client.transactions.track(signature, { timeoutMs })) {
82
91
  if (update.executionResult) {
92
+ const vmError = update.executionResult.vmError !== void 0 && update.executionResult.vmError !== null ? BigInt(update.executionResult.vmError) : 0n;
93
+ const userErrorCode = update.executionResult.userErrorCode;
94
+ const executionError = update.executionResult.executionResult !== void 0 && update.executionResult.executionResult !== null ? BigInt(update.executionResult.executionResult) : 0n;
95
+ const success = vmError === 0n && executionError === 0n && userErrorCode === 0n;
83
96
  return {
84
97
  signature,
85
- status: update.executionResult.userErrorCode === 0n ? "finalized" : "failed",
86
- errorCode: update.executionResult.userErrorCode
98
+ status: success ? "finalized" : "failed",
99
+ errorCode: vmError !== 0n ? vmError : executionError !== 0n ? executionError : userErrorCode
87
100
  };
88
101
  }
89
102
  if (update.statusCode === 3) {
90
- return {
91
- signature,
92
- status: "finalized"
93
- };
103
+ finalizedSeen = true;
94
104
  }
95
105
  }
106
+ if (finalizedSeen) {
107
+ return {
108
+ signature,
109
+ status: "finalized_without_execution"
110
+ };
111
+ }
96
112
  } catch {
113
+ if (finalizedSeen) {
114
+ return {
115
+ signature,
116
+ status: "finalized_without_execution"
117
+ };
118
+ }
97
119
  return {
98
120
  signature,
99
121
  status: "timeout"
@@ -107,6 +129,26 @@ async function trackTransaction(client, signature, timeoutMs = 5e3) {
107
129
  function toThruAddress(bytes) {
108
130
  return encodeAddress(bytes);
109
131
  }
132
+ async function withSerializedFeePayer(feePayerPublicKey, work) {
133
+ const queueKey = toThruAddress(feePayerPublicKey);
134
+ const feePayerQueues = getFeePayerQueues();
135
+ const previous = feePayerQueues.get(queueKey) ?? Promise.resolve();
136
+ let release;
137
+ const current = new Promise((resolve) => {
138
+ release = resolve;
139
+ });
140
+ const tail = previous.then(() => current);
141
+ feePayerQueues.set(queueKey, tail);
142
+ await previous;
143
+ try {
144
+ return await work();
145
+ } finally {
146
+ release();
147
+ if (feePayerQueues.get(queueKey) === tail) {
148
+ feePayerQueues.delete(queueKey);
149
+ }
150
+ }
151
+ }
110
152
 
111
153
  // src/server/create-wallet.ts
112
154
  async function createPasskeyWallet(opts) {
@@ -114,14 +156,15 @@ async function createPasskeyWallet(opts) {
114
156
  const seed = await createWalletSeed(walletName, opts.pubkeyX, opts.pubkeyY);
115
157
  const walletBytes = await deriveWalletAddress(seed, PASSKEY_MANAGER_PROGRAM_ADDRESS);
116
158
  const walletAddress = toThruAddress(walletBytes);
117
- let walletExists = false;
118
- try {
119
- await opts.client.accounts.get(walletAddress);
120
- walletExists = true;
121
- } catch {
122
- walletExists = false;
123
- }
124
- if (!walletExists) {
159
+ await withSerializedFeePayer(opts.adminPublicKey, async () => {
160
+ let walletExists = false;
161
+ try {
162
+ await opts.client.accounts.get(walletAddress);
163
+ walletExists = true;
164
+ } catch {
165
+ walletExists = false;
166
+ }
167
+ if (walletExists) return;
125
168
  const stateProof = await getStateProof(opts.client, walletAddress);
126
169
  const accountCtx = buildAccountContext({
127
170
  walletAddress,
@@ -145,8 +188,8 @@ async function createPasskeyWallet(opts) {
145
188
  program: PASSKEY_MANAGER_PROGRAM_ADDRESS,
146
189
  instructionData: createIx,
147
190
  accounts: {
148
- readWrite: [walletAddress],
149
- readOnly: []
191
+ readWrite: accountCtx.readWriteAddresses,
192
+ readOnly: accountCtx.readOnlyAddresses
150
193
  },
151
194
  header: { fee: 0n }
152
195
  });
@@ -155,10 +198,10 @@ async function createPasskeyWallet(opts) {
155
198
  const result = await trackTransaction(opts.client, signature, 6e4);
156
199
  if (result.status !== "finalized") {
157
200
  throw new Error(
158
- `Wallet creation failed with error code: ${result.errorCode ?? "unknown"}`
201
+ `Wallet creation failed with status: ${result.status}${result.errorCode !== void 0 ? ` (error code: ${result.errorCode})` : ""}`
159
202
  );
160
203
  }
161
- }
204
+ });
162
205
  let credentialLookupAddress;
163
206
  if (opts.credentialId) {
164
207
  const credentialIdBytes = base64UrlToBytes(opts.credentialId);
@@ -167,18 +210,20 @@ async function createPasskeyWallet(opts) {
167
210
  walletName,
168
211
  PASSKEY_MANAGER_PROGRAM_ADDRESS
169
212
  );
170
- credentialLookupAddress = toThruAddress(lookupAddressBytes);
171
- let lookupExists = false;
213
+ const lookupAddress = toThruAddress(lookupAddressBytes);
214
+ credentialLookupAddress = lookupAddress;
172
215
  try {
173
- await opts.client.accounts.get(credentialLookupAddress);
174
- lookupExists = true;
175
- } catch {
176
- lookupExists = false;
177
- }
178
- if (!lookupExists) {
179
- try {
216
+ await withSerializedFeePayer(opts.adminPublicKey, async () => {
217
+ let lookupExists = false;
218
+ try {
219
+ await opts.client.accounts.get(lookupAddress);
220
+ lookupExists = true;
221
+ } catch {
222
+ lookupExists = false;
223
+ }
224
+ if (lookupExists) return;
180
225
  const credSeed = await createCredentialLookupSeed(credentialIdBytes, walletName);
181
- const stateProof = await getStateProof(opts.client, credentialLookupAddress);
226
+ const stateProof = await getStateProof(opts.client, lookupAddress);
182
227
  const accountCtx = buildAccountContext({
183
228
  walletAddress,
184
229
  readWriteAccounts: [lookupAddressBytes],
@@ -197,8 +242,8 @@ async function createPasskeyWallet(opts) {
197
242
  program: PASSKEY_MANAGER_PROGRAM_ADDRESS,
198
243
  instructionData: registerIx,
199
244
  accounts: {
200
- readWrite: [walletAddress, credentialLookupAddress],
201
- readOnly: []
245
+ readWrite: accountCtx.readWriteAddresses,
246
+ readOnly: accountCtx.readOnlyAddresses
202
247
  },
203
248
  header: { fee: 0n }
204
249
  });
@@ -210,9 +255,9 @@ async function createPasskeyWallet(opts) {
210
255
  `Credential registration failed with status: ${result.status}${result.errorCode !== void 0 ? ` (error code: ${result.errorCode})` : ""}`
211
256
  );
212
257
  }
213
- } catch (error) {
214
- console.warn("Credential registration failed (non-fatal):", error);
215
- }
258
+ });
259
+ } catch (error) {
260
+ console.warn("Credential registration failed (non-fatal):", error);
216
261
  }
217
262
  }
218
263
  return {
@@ -248,28 +293,30 @@ import {
248
293
  hexToBytes as hexToBytes2
249
294
  } from "@thru/passkey-manager";
250
295
  async function submitPasskeyTransaction(opts) {
251
- const validateIx = encodeValidateInstruction({
252
- walletAccountIdx: opts.accountCtx.walletAccountIdx,
253
- authIdx: 0,
254
- signatureR: hexToBytes2(opts.signatureR),
255
- signatureS: hexToBytes2(opts.signatureS),
256
- authenticatorData: Buffer.from(opts.authenticatorData, "base64"),
257
- clientDataJSON: Buffer.from(opts.clientDataJSON, "base64")
258
- });
259
- const instructionData = concatenateInstructions([validateIx, opts.invokeIx]);
260
- const transaction = await opts.client.transactions.build({
261
- feePayer: { publicKey: opts.adminPublicKey },
262
- program: PASSKEY_MANAGER_PROGRAM_ADDRESS2,
263
- instructionData,
264
- accounts: {
265
- readWrite: opts.accountCtx.readWriteAddresses,
266
- readOnly: opts.accountCtx.readOnlyAddresses
267
- },
268
- header: { fee: 0n }
296
+ return withSerializedFeePayer(opts.adminPublicKey, async () => {
297
+ const validateIx = encodeValidateInstruction({
298
+ walletAccountIdx: opts.accountCtx.walletAccountIdx,
299
+ authIdx: 0,
300
+ signatureR: hexToBytes2(opts.signatureR),
301
+ signatureS: hexToBytes2(opts.signatureS),
302
+ authenticatorData: Buffer.from(opts.authenticatorData, "base64"),
303
+ clientDataJSON: Buffer.from(opts.clientDataJSON, "base64")
304
+ });
305
+ const instructionData = concatenateInstructions([validateIx, opts.invokeIx]);
306
+ const transaction = await opts.client.transactions.build({
307
+ feePayer: { publicKey: opts.adminPublicKey },
308
+ program: PASSKEY_MANAGER_PROGRAM_ADDRESS2,
309
+ instructionData,
310
+ accounts: {
311
+ readWrite: opts.accountCtx.readWriteAddresses,
312
+ readOnly: opts.accountCtx.readOnlyAddresses
313
+ },
314
+ header: { fee: 0n }
315
+ });
316
+ await transaction.sign(opts.adminPrivateKey);
317
+ const signature = await opts.client.transactions.send(transaction.toWire());
318
+ return trackTransaction(opts.client, signature);
269
319
  });
270
- await transaction.sign(opts.adminPrivateKey);
271
- const signature = await opts.client.transactions.send(transaction.toWire());
272
- return trackTransaction(opts.client, signature);
273
320
  }
274
321
 
275
322
  // src/server/handlers.ts
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/server/create-wallet.ts","../../helpers/src/constants.ts","../../helpers/src/address.ts","../src/server/utils.ts","../src/server/challenge.ts","../src/server/submit.ts","../src/server/handlers.ts"],"sourcesContent":["import {\n PASSKEY_MANAGER_PROGRAM_ADDRESS,\n base64UrlToBytes,\n buildAccountContext,\n createCredentialLookupSeed,\n createWalletSeed,\n deriveCredentialLookupAddress,\n deriveWalletAddress,\n encodeCreateInstruction,\n encodeRegisterCredentialInstruction,\n} from '@thru/passkey-manager';\nimport { toThruAddress, getStateProof, trackTransaction } from './utils';\nimport type { ThruClient } from './types';\n\nexport async function createPasskeyWallet(opts: {\n client: ThruClient;\n adminPublicKey: Uint8Array;\n adminPrivateKey: string;\n adminAddress: string;\n pubkeyX: Uint8Array;\n pubkeyY: Uint8Array;\n credentialId?: string;\n walletName?: string;\n}): Promise<{ walletAddress: string; credentialLookupAddress?: string }> {\n const walletName = opts.walletName ?? 'default';\n const seed = await createWalletSeed(walletName, opts.pubkeyX, opts.pubkeyY);\n const walletBytes = await deriveWalletAddress(seed, PASSKEY_MANAGER_PROGRAM_ADDRESS);\n const walletAddress = toThruAddress(walletBytes);\n\n let walletExists = false;\n try {\n await opts.client.accounts.get(walletAddress);\n walletExists = true;\n } catch {\n walletExists = false;\n }\n\n if (!walletExists) {\n const stateProof = await getStateProof(opts.client, walletAddress);\n const accountCtx = buildAccountContext({\n walletAddress,\n readWriteAccounts: [],\n readOnlyAccounts: [],\n feePayerAddress: opts.adminAddress,\n programAddress: PASSKEY_MANAGER_PROGRAM_ADDRESS,\n });\n\n const createIx = encodeCreateInstruction({\n walletAccountIdx: accountCtx.walletAccountIdx,\n authority: {\n tag: 1,\n pubkeyX: opts.pubkeyX,\n pubkeyY: opts.pubkeyY,\n },\n seed,\n stateProof,\n });\n\n const transaction = await opts.client.transactions.build({\n feePayer: { publicKey: opts.adminPublicKey },\n program: PASSKEY_MANAGER_PROGRAM_ADDRESS,\n instructionData: createIx,\n accounts: {\n readWrite: [walletAddress],\n readOnly: [],\n },\n header: { fee: 0n },\n });\n\n await transaction.sign(opts.adminPrivateKey);\n const signature = await opts.client.transactions.send(transaction.toWire());\n const result = await trackTransaction(opts.client, signature, 60000);\n if (result.status !== 'finalized') {\n throw new Error(\n `Wallet creation failed with error code: ${result.errorCode ?? 'unknown'}`\n );\n }\n }\n\n let credentialLookupAddress: string | undefined;\n if (opts.credentialId) {\n const credentialIdBytes = base64UrlToBytes(opts.credentialId);\n const lookupAddressBytes = await deriveCredentialLookupAddress(\n credentialIdBytes,\n walletName,\n PASSKEY_MANAGER_PROGRAM_ADDRESS\n );\n\n credentialLookupAddress = toThruAddress(lookupAddressBytes);\n\n let lookupExists = false;\n try {\n await opts.client.accounts.get(credentialLookupAddress);\n lookupExists = true;\n } catch {\n lookupExists = false;\n }\n\n if (!lookupExists) {\n try {\n const credSeed = await createCredentialLookupSeed(credentialIdBytes, walletName);\n const stateProof = await getStateProof(opts.client, credentialLookupAddress);\n const accountCtx = buildAccountContext({\n walletAddress,\n readWriteAccounts: [lookupAddressBytes],\n readOnlyAccounts: [],\n feePayerAddress: opts.adminAddress,\n programAddress: PASSKEY_MANAGER_PROGRAM_ADDRESS,\n });\n\n const registerIx = encodeRegisterCredentialInstruction({\n walletAccountIdx: accountCtx.walletAccountIdx,\n lookupAccountIdx: accountCtx.getAccountIndex(lookupAddressBytes),\n seed: credSeed,\n stateProof,\n });\n\n const transaction = await opts.client.transactions.build({\n feePayer: { publicKey: opts.adminPublicKey },\n program: PASSKEY_MANAGER_PROGRAM_ADDRESS,\n instructionData: registerIx,\n accounts: {\n readWrite: [walletAddress, credentialLookupAddress],\n readOnly: [],\n },\n header: { fee: 0n },\n });\n\n await transaction.sign(opts.adminPrivateKey);\n const signature = await opts.client.transactions.send(transaction.toWire());\n const result = await trackTransaction(opts.client, signature, 60000);\n if (result.status !== 'finalized') {\n throw new Error(\n `Credential registration failed with status: ${result.status}${\n result.errorCode !== undefined ? ` (error code: ${result.errorCode})` : ''\n }`\n );\n }\n } catch (error) {\n console.warn('Credential registration failed (non-fatal):', error);\n }\n }\n }\n\n return {\n walletAddress,\n credentialLookupAddress,\n };\n}\n","export const BASE64_URL_ALPHABET = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_\";\n\nconst tempMap = new Int16Array(256).fill(-1);\nfor (let i = 0; i < BASE64_URL_ALPHABET.length; i++) {\n tempMap[BASE64_URL_ALPHABET.charCodeAt(i)] = i;\n}\nexport const BASE64_URL_MAP = tempMap\n\n","import { BASE64_URL_ALPHABET, BASE64_URL_MAP } from \"./constants\";\n\nexport function encodeAddress(bytes: Uint8Array): string {\n if (bytes.length !== 32) {\n throw new Error('Expected 32-byte address');\n }\n\n let checksum = 0;\n let accumulator = 0;\n let bitsCollected = 0;\n const output: string[] = ['t', 'a'];\n\n for (let i = 0; i < 30; i++) {\n const byte = bytes[i];\n checksum += byte;\n accumulator = ((accumulator << 8) | byte) >>> 0;\n bitsCollected += 8;\n while (bitsCollected >= 6) {\n const index = (accumulator >> (bitsCollected - 6)) & 0x3f;\n output.push(BASE64_URL_ALPHABET[index]);\n bitsCollected -= 6;\n accumulator &= maskForBits(bitsCollected);\n }\n }\n\n const secondLast = bytes[30];\n checksum += secondLast;\n accumulator = ((accumulator << 8) | secondLast) >>> 0;\n bitsCollected += 8;\n\n const last = bytes[31];\n checksum += last;\n accumulator = ((accumulator << 8) | last) >>> 0;\n bitsCollected += 8;\n\n accumulator = ((accumulator << 8) | (checksum & 0xff)) >>> 0;\n bitsCollected += 8;\n\n while (bitsCollected >= 6) {\n const index = (accumulator >> (bitsCollected - 6)) & 0x3f;\n output.push(BASE64_URL_ALPHABET[index]);\n bitsCollected -= 6;\n accumulator &= maskForBits(bitsCollected);\n }\n\n return output.join('');\n}\n\nexport function decodeAddress(value: string): Uint8Array {\n if (value.length !== 46) {\n throw new Error('Invalid address length');\n }\n if (!value.startsWith('ta')) {\n throw new Error('Address must start with \"ta\"');\n }\n\n const output = new Uint8Array(32);\n let checksum = 0;\n let inIdx = 2;\n let remaining = 40;\n let outIdx = 0;\n\n while (remaining >= 4) {\n const a = BASE64_URL_MAP[value.charCodeAt(inIdx)];\n const b = BASE64_URL_MAP[value.charCodeAt(inIdx + 1)];\n const c = BASE64_URL_MAP[value.charCodeAt(inIdx + 2)];\n const d = BASE64_URL_MAP[value.charCodeAt(inIdx + 3)];\n if (a < 0 || b < 0 || c < 0 || d < 0) {\n throw new Error('Invalid address encoding');\n }\n const triple = (a << 18) | (b << 12) | (c << 6) | d;\n const byte1 = (triple >> 16) & 0xff;\n const byte2 = (triple >> 8) & 0xff;\n const byte3 = triple & 0xff;\n checksum += byte1;\n checksum += byte2;\n checksum += byte3;\n output[outIdx++] = byte1;\n output[outIdx++] = byte2;\n output[outIdx++] = byte3;\n inIdx += 4;\n remaining -= 4;\n }\n\n const a = BASE64_URL_MAP[value.charCodeAt(inIdx)];\n const b = BASE64_URL_MAP[value.charCodeAt(inIdx + 1)];\n const c = BASE64_URL_MAP[value.charCodeAt(inIdx + 2)];\n const d = BASE64_URL_MAP[value.charCodeAt(inIdx + 3)];\n if (a < 0 || b < 0 || c < 0 || d < 0) {\n throw new Error('Invalid address encoding');\n }\n const triple = (a << 18) | (b << 12) | (c << 6) | d;\n const byte1 = (triple >> 16) & 0xff;\n const byte2 = (triple >> 8) & 0xff;\n const incomingChecksum = triple & 0xff;\n\n checksum += byte1;\n checksum += byte2;\n output[outIdx++] = byte1;\n output[outIdx++] = byte2;\n\n checksum &= 0xff;\n if (checksum !== incomingChecksum) {\n throw new Error('Address checksum mismatch');\n }\n\n return output;\n}\n\nfunction maskForBits(bits: number): number {\n return bits === 0 ? 0 : (1 << bits) - 1;\n}\n","import { encodeAddress } from '@thru/helpers';\nimport type { ThruClient, TransactionResult } from './types';\n\nexport async function getStateProof(\n client: ThruClient,\n address: string,\n proofType: number = 1,\n targetSlot?: bigint\n): Promise<Uint8Array> {\n const proofRequest: {\n address: string;\n proofType: number;\n targetSlot?: bigint;\n } = {\n address,\n proofType,\n };\n\n if (targetSlot !== undefined) {\n proofRequest.targetSlot = targetSlot;\n }\n\n const proof = await client.proofs.generate(proofRequest);\n\n if (!proof.proof || proof.proof.length === 0) {\n throw new Error(`No state proof returned for ${address}`);\n }\n\n return proof.proof;\n}\n\nexport async function trackTransaction(\n client: ThruClient,\n signature: string,\n timeoutMs: number = 5000\n): Promise<TransactionResult> {\n try {\n for await (const update of client.transactions.track(signature, { timeoutMs })) {\n if (update.executionResult) {\n return {\n signature,\n status: update.executionResult.userErrorCode === 0n ? 'finalized' : 'failed',\n errorCode: update.executionResult.userErrorCode,\n };\n }\n\n if (update.statusCode === 3) {\n return {\n signature,\n status: 'finalized',\n };\n }\n }\n } catch {\n return {\n signature,\n status: 'timeout',\n };\n }\n\n return {\n signature,\n status: 'timeout',\n };\n}\n\nexport function toThruAddress(bytes: Uint8Array): string {\n return encodeAddress(bytes);\n}\n","import {\n bytesToBase64Url,\n createValidateChallenge,\n fetchWalletNonce,\n} from '@thru/passkey-manager';\nimport type { AccountContext } from '@thru/passkey-manager';\nimport type { PasskeyChallengeResult, ThruClient } from './types';\n\nexport async function createPasskeyChallenge(opts: {\n client: ThruClient;\n walletAddress: string;\n accountCtx: AccountContext;\n invokeIx: Uint8Array;\n}): Promise<PasskeyChallengeResult> {\n const nonce = await fetchWalletNonce(opts.client, opts.walletAddress);\n const challenge = await createValidateChallenge(\n nonce,\n opts.accountCtx.accountAddresses,\n opts.invokeIx\n );\n\n return {\n challenge: bytesToBase64Url(challenge),\n nonce: nonce.toString(),\n };\n}\n","import {\n PASSKEY_MANAGER_PROGRAM_ADDRESS,\n concatenateInstructions,\n encodeValidateInstruction,\n hexToBytes,\n} from '@thru/passkey-manager';\nimport type { AccountContext } from '@thru/passkey-manager';\nimport { trackTransaction } from './utils';\nimport type {\n PasskeySignaturePayload,\n ThruClient,\n TransactionResult,\n} from './types';\n\nexport async function submitPasskeyTransaction(opts: {\n client: ThruClient;\n adminPublicKey: Uint8Array;\n adminPrivateKey: string;\n walletAddress: string;\n accountCtx: AccountContext;\n invokeIx: Uint8Array;\n} & PasskeySignaturePayload): Promise<TransactionResult> {\n const validateIx = encodeValidateInstruction({\n walletAccountIdx: opts.accountCtx.walletAccountIdx,\n authIdx: 0,\n signatureR: hexToBytes(opts.signatureR),\n signatureS: hexToBytes(opts.signatureS),\n authenticatorData: Buffer.from(opts.authenticatorData, 'base64'),\n clientDataJSON: Buffer.from(opts.clientDataJSON, 'base64'),\n });\n\n const instructionData = concatenateInstructions([validateIx, opts.invokeIx]);\n const transaction = await opts.client.transactions.build({\n feePayer: { publicKey: opts.adminPublicKey },\n program: PASSKEY_MANAGER_PROGRAM_ADDRESS,\n instructionData,\n accounts: {\n readWrite: opts.accountCtx.readWriteAddresses,\n readOnly: opts.accountCtx.readOnlyAddresses,\n },\n header: { fee: 0n },\n });\n\n await transaction.sign(opts.adminPrivateKey);\n const signature = await opts.client.transactions.send(transaction.toWire());\n return trackTransaction(opts.client, signature);\n}\n","import type { PasskeyContextResult } from './types';\nimport { createPasskeyChallenge } from './challenge';\nimport { submitPasskeyTransaction } from './submit';\nimport type {\n PasskeyChallengeSubmitPayload,\n ThruClient,\n TransactionResult,\n} from './types';\n\nexport function createPasskeyHandlers<P>(opts: {\n buildContext: (params: P) => Promise<PasskeyContextResult>;\n adminPublicKey: Uint8Array;\n adminPrivateKey: string;\n client: ThruClient;\n challengeTtlMs?: number;\n}) {\n const pendingContexts = new Map<\n string,\n { context: PasskeyContextResult; createdAt: number }\n >();\n const challengeTtlMs = opts.challengeTtlMs ?? 5 * 60_000;\n\n function createPendingContextKey(\n walletAddress: string,\n nonce: string,\n challenge: string\n ): string {\n return `${walletAddress}:${nonce}:${challenge}`;\n }\n\n function prunePendingContexts(now = Date.now()): void {\n for (const [nonce, entry] of pendingContexts.entries()) {\n if (now - entry.createdAt > challengeTtlMs) {\n pendingContexts.delete(nonce);\n }\n }\n }\n\n return {\n challenge: async (walletAddress: string, params: P) => {\n prunePendingContexts();\n\n const context = await opts.buildContext(params);\n const challenge = await createPasskeyChallenge({\n client: opts.client,\n walletAddress,\n accountCtx: context.accountCtx,\n invokeIx: context.invokeIx,\n });\n\n pendingContexts.set(\n createPendingContextKey(walletAddress, challenge.nonce, challenge.challenge),\n {\n context,\n createdAt: Date.now(),\n }\n );\n\n return challenge;\n },\n submit: async (\n walletAddress: string,\n params: P,\n payload: PasskeyChallengeSubmitPayload\n ): Promise<TransactionResult> => {\n void params;\n prunePendingContexts();\n\n const pendingKey = createPendingContextKey(\n walletAddress,\n payload.nonce,\n payload.challenge\n );\n const pending = pendingContexts.get(pendingKey);\n if (!pending) {\n throw new Error('Missing or expired challenge nonce');\n }\n\n pendingContexts.delete(pendingKey);\n const { nonce: _nonce, challenge: _challenge, ...signaturePayload } = payload;\n\n return submitPasskeyTransaction({\n client: opts.client,\n adminPublicKey: opts.adminPublicKey,\n adminPrivateKey: opts.adminPrivateKey,\n walletA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Q,WAAW,YAAY,GAAG,iBAAiB,IAAI;AAEtE,aAAO,yBAAyB;AAAA,QAC9B,QAAQ,KAAK;AAAA,QACb,gBAAgB,KAAK;AAAA,QACrB,iBAAiB,KAAK;AAAA,QACtB;AAAA,QACA,YAAY,QAAQ,QAAQ;AAAA,QAC5B,UAAU,QAAQ,QAAQ;AAAA,QAC1B,GAAG;AAAA,MACL,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["PASSKEY_MANAGER_PROGRAM_ADDRESS","hexToBytes","hexToBytes","PASSKEY_MANAGER_PROGRAM_ADDRESS"]}
1
+ {"version":3,"sources":["../src/server/create-wallet.ts","../../helpers/src/constants.ts","../../helpers/src/address.ts","../src/server/utils.ts","../src/server/challenge.ts","../src/server/submit.ts","../src/server/handlers.ts"],"sourcesContent":["import {\n PASSKEY_MANAGER_PROGRAM_ADDRESS,\n base64UrlToBytes,\n buildAccountContext,\n createCredentialLookupSeed,\n createWalletSeed,\n deriveCredentialLookupAddress,\n deriveWalletAddress,\n encodeCreateInstruction,\n encodeRegisterCredentialInstruction,\n} from '@thru/passkey-manager';\nimport {\n toThruAddress,\n getStateProof,\n trackTransaction,\n withSerializedFeePayer,\n} from './utils';\nimport type { ThruClient } from './types';\n\nexport async function createPasskeyWallet(opts: {\n client: ThruClient;\n adminPublicKey: Uint8Array;\n adminPrivateKey: Uint8Array;\n adminAddress: string;\n pubkeyX: Uint8Array;\n pubkeyY: Uint8Array;\n credentialId?: string;\n walletName?: string;\n}): Promise<{ walletAddress: string; credentialLookupAddress?: string }> {\n const walletName = opts.walletName ?? 'default';\n const seed = await createWalletSeed(walletName, opts.pubkeyX, opts.pubkeyY);\n const walletBytes = await deriveWalletAddress(seed, PASSKEY_MANAGER_PROGRAM_ADDRESS);\n const walletAddress = toThruAddress(walletBytes);\n\n await withSerializedFeePayer(opts.adminPublicKey, async () => {\n let walletExists = false;\n try {\n await opts.client.accounts.get(walletAddress);\n walletExists = true;\n } catch {\n walletExists = false;\n }\n\n if (walletExists) return;\n\n const stateProof = await getStateProof(opts.client, walletAddress);\n const accountCtx = buildAccountContext({\n walletAddress,\n readWriteAccounts: [],\n readOnlyAccounts: [],\n feePayerAddress: opts.adminAddress,\n programAddress: PASSKEY_MANAGER_PROGRAM_ADDRESS,\n });\n\n const createIx = encodeCreateInstruction({\n walletAccountIdx: accountCtx.walletAccountIdx,\n authority: {\n tag: 1,\n pubkeyX: opts.pubkeyX,\n pubkeyY: opts.pubkeyY,\n },\n seed,\n stateProof,\n });\n\n const transaction = await opts.client.transactions.build({\n feePayer: { publicKey: opts.adminPublicKey },\n program: PASSKEY_MANAGER_PROGRAM_ADDRESS,\n instructionData: createIx,\n accounts: {\n readWrite: accountCtx.readWriteAddresses,\n readOnly: accountCtx.readOnlyAddresses,\n },\n header: { fee: 0n },\n });\n\n await transaction.sign(opts.adminPrivateKey);\n const signature = await opts.client.transactions.send(transaction.toWire());\n const result = await trackTransaction(opts.client, signature, 60000);\n if (result.status !== 'finalized') {\n throw new Error(\n `Wallet creation failed with status: ${result.status}${\n result.errorCode !== undefined ? ` (error code: ${result.errorCode})` : ''\n }`\n );\n }\n });\n\n let credentialLookupAddress: string | undefined;\n if (opts.credentialId) {\n const credentialIdBytes = base64UrlToBytes(opts.credentialId);\n const lookupAddressBytes = await deriveCredentialLookupAddress(\n credentialIdBytes,\n walletName,\n PASSKEY_MANAGER_PROGRAM_ADDRESS\n );\n const lookupAddress = toThruAddress(lookupAddressBytes);\n\n credentialLookupAddress = lookupAddress;\n\n try {\n await withSerializedFeePayer(opts.adminPublicKey, async () => {\n let lookupExists = false;\n try {\n await opts.client.accounts.get(lookupAddress);\n lookupExists = true;\n } catch {\n lookupExists = false;\n }\n\n if (lookupExists) return;\n\n const credSeed = await createCredentialLookupSeed(credentialIdBytes, walletName);\n const stateProof = await getStateProof(opts.client, lookupAddress);\n const accountCtx = buildAccountContext({\n walletAddress,\n readWriteAccounts: [lookupAddressBytes],\n readOnlyAccounts: [],\n feePayerAddress: opts.adminAddress,\n programAddress: PASSKEY_MANAGER_PROGRAM_ADDRESS,\n });\n\n const registerIx = encodeRegisterCredentialInstruction({\n walletAccountIdx: accountCtx.walletAccountIdx,\n lookupAccountIdx: accountCtx.getAccountIndex(lookupAddressBytes),\n seed: credSeed,\n stateProof,\n });\n\n const transaction = await opts.client.transactions.build({\n feePayer: { publicKey: opts.adminPublicKey },\n program: PASSKEY_MANAGER_PROGRAM_ADDRESS,\n instructionData: registerIx,\n accounts: {\n readWrite: accountCtx.readWriteAddresses,\n readOnly: accountCtx.readOnlyAddresses,\n },\n header: { fee: 0n },\n });\n\n await transaction.sign(opts.adminPrivateKey);\n const signature = await opts.client.transactions.send(transaction.toWire());\n const result = await trackTransaction(opts.client, signature, 60000);\n if (result.status !== 'finalized') {\n throw new Error(\n `Credential registration failed with status: ${result.status}${\n result.errorCode !== undefined ? ` (error code: ${result.errorCode})` : ''\n }`\n );\n }\n });\n } catch (error) {\n console.warn('Credential registration failed (non-fatal):', error);\n }\n }\n\n return {\n walletAddress,\n credentialLookupAddress,\n };\n}\n","export const BASE64_URL_ALPHABET = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_\";\n\nconst tempMap = new Int16Array(256).fill(-1);\nfor (let i = 0; i < BASE64_URL_ALPHABET.length; i++) {\n tempMap[BASE64_URL_ALPHABET.charCodeAt(i)] = i;\n}\nexport const BASE64_URL_MAP = tempMap\n\n","import { BASE64_URL_ALPHABET, BASE64_URL_MAP } from \"./constants\";\n\nexport function encodeAddress(bytes: Uint8Array): string {\n if (bytes.length !== 32) {\n throw new Error('Expected 32-byte address');\n }\n\n let checksum = 0;\n let accumulator = 0;\n let bitsCollected = 0;\n const output: string[] = ['t', 'a'];\n\n for (let i = 0; i < 30; i++) {\n const byte = bytes[i];\n checksum += byte;\n accumulator = ((accumulator << 8) | byte) >>> 0;\n bitsCollected += 8;\n while (bitsCollected >= 6) {\n const index = (accumulator >> (bitsCollected - 6)) & 0x3f;\n output.push(BASE64_URL_ALPHABET[index]);\n bitsCollected -= 6;\n accumulator &= maskForBits(bitsCollected);\n }\n }\n\n const secondLast = bytes[30];\n checksum += secondLast;\n accumulator = ((accumulator << 8) | secondLast) >>> 0;\n bitsCollected += 8;\n\n const last = bytes[31];\n checksum += last;\n accumulator = ((accumulator << 8) | last) >>> 0;\n bitsCollected += 8;\n\n accumulator = ((accumulator << 8) | (checksum & 0xff)) >>> 0;\n bitsCollected += 8;\n\n while (bitsCollected >= 6) {\n const index = (accumulator >> (bitsCollected - 6)) & 0x3f;\n output.push(BASE64_URL_ALPHABET[index]);\n bitsCollected -= 6;\n accumulator &= maskForBits(bitsCollected);\n }\n\n return output.join('');\n}\n\nexport function decodeAddress(value: string): Uint8Array {\n if (value.length !== 46) {\n throw new Error('Invalid address length');\n }\n if (!value.startsWith('ta')) {\n throw new Error('Address must start with \"ta\"');\n }\n\n const output = new Uint8Array(32);\n let checksum = 0;\n let inIdx = 2;\n let remaining = 40;\n let outIdx = 0;\n\n while (remaining >= 4) {\n const a = BASE64_URL_MAP[value.charCodeAt(inIdx)];\n const b = BASE64_URL_MAP[value.charCodeAt(inIdx + 1)];\n const c = BASE64_URL_MAP[value.charCodeAt(inIdx + 2)];\n const d = BASE64_URL_MAP[value.charCodeAt(inIdx + 3)];\n if (a < 0 || b < 0 || c < 0 || d < 0) {\n throw new Error('Invalid address encoding');\n }\n const triple = (a << 18) | (b << 12) | (c << 6) | d;\n const byte1 = (triple >> 16) & 0xff;\n const byte2 = (triple >> 8) & 0xff;\n const byte3 = triple & 0xff;\n checksum += byte1;\n checksum += byte2;\n checksum += byte3;\n output[outIdx++] = byte1;\n output[outIdx++] = byte2;\n output[outIdx++] = byte3;\n inIdx += 4;\n remaining -= 4;\n }\n\n const a = BASE64_URL_MAP[value.charCodeAt(inIdx)];\n const b = BASE64_URL_MAP[value.charCodeAt(inIdx + 1)];\n const c = BASE64_URL_MAP[value.charCodeAt(inIdx + 2)];\n const d = BASE64_URL_MAP[value.charCodeAt(inIdx + 3)];\n if (a < 0 || b < 0 || c < 0 || d < 0) {\n throw new Error('Invalid address encoding');\n }\n const triple = (a << 18) | (b << 12) | (c << 6) | d;\n const byte1 = (triple >> 16) & 0xff;\n const byte2 = (triple >> 8) & 0xff;\n const incomingChecksum = triple & 0xff;\n\n checksum += byte1;\n checksum += byte2;\n output[outIdx++] = byte1;\n output[outIdx++] = byte2;\n\n checksum &= 0xff;\n if (checksum !== incomingChecksum) {\n throw new Error('Address checksum mismatch');\n }\n\n return output;\n}\n\nfunction maskForBits(bits: number): number {\n return bits === 0 ? 0 : (1 << bits) - 1;\n}\n","import { encodeAddress } from '@thru/helpers';\nimport type { ThruClient, TransactionResult } from './types';\n\nconst feePayerQueueSymbol = Symbol.for('thru.sharedFeePayerQueues');\n\nfunction getFeePayerQueues(): Map<string, Promise<void>> {\n const globalQueues = globalThis as typeof globalThis & {\n [feePayerQueueSymbol]?: Map<string, Promise<void>>;\n };\n\n if (!globalQueues[feePayerQueueSymbol]) {\n globalQueues[feePayerQueueSymbol] = new Map<string, Promise<void>>();\n }\n\n return globalQueues[feePayerQueueSymbol];\n}\n\nexport async function getStateProof(\n client: ThruClient,\n address: string,\n proofType: number = 1,\n targetSlot?: bigint\n): Promise<Uint8Array> {\n const proofRequest: {\n address: string;\n proofType: number;\n targetSlot?: bigint;\n } = {\n address,\n proofType,\n };\n\n if (targetSlot !== undefined) {\n proofRequest.targetSlot = targetSlot;\n }\n\n const proof = await client.proofs.generate(proofRequest);\n\n if (!proof.proof || proof.proof.length === 0) {\n throw new Error(`No state proof returned for ${address}`);\n }\n\n return proof.proof;\n}\n\nexport async function trackTransaction(\n client: ThruClient,\n signature: string,\n timeoutMs: number = 5000\n): Promise<TransactionResult> {\n let finalizedSeen = false;\n\n try {\n for await (const update of client.transactions.track(signature, { timeoutMs })) {\n if (update.executionResult) {\n const vmError =\n update.executionResult.vmError !== undefined && update.executionResult.vmError !== null\n ? BigInt(update.executionResult.vmError)\n : 0n;\n const userErrorCode = update.executionResult.userErrorCode;\n const executionError =\n update.executionResult.executionResult !== undefined &&\n update.executionResult.executionResult !== null\n ? BigInt(update.executionResult.executionResult)\n : 0n;\n const success = vmError === 0n && executionError === 0n && userErrorCode === 0n;\n\n return {\n signature,\n status: success ? 'finalized' : 'failed',\n errorCode: vmError !== 0n ? vmError : executionError !== 0n ? executionError : userErrorCode,\n };\n }\n\n if (update.statusCode === 3) {\n finalizedSeen = true;\n }\n }\n\n if (finalizedSeen) {\n return {\n signature,\n status: 'finalized_without_execution',\n };\n }\n } catch {\n if (finalizedSeen) {\n return {\n signature,\n status: 'finalized_without_execution',\n };\n }\n\n return {\n signature,\n status: 'timeout',\n };\n }\n\n return {\n signature,\n status: 'timeout',\n };\n}\n\nexport function toThruAddress(bytes: Uint8Array): string {\n return encodeAddress(bytes);\n}\n\nexport async function withSerializedFeePayer<T>(\n feePayerPublicKey: Uint8Array,\n work: () => Promise<T>\n): Promise<T> {\n const queueKey = toThruAddress(feePayerPublicKey);\n const feePayerQueues = getFeePayerQueues();\n const previous = feePayerQueues.get(queueKey) ?? Promise.resolve();\n let release!: () => void;\n const current = new Promise<void>((resolve) => {\n release = resolve;\n });\n const tail = previous.then(() => current);\n feePayerQueues.set(queueKey, tail);\n\n await previous;\n\n try {\n return await work();\n } finally {\n release();\n if (feePayerQueues.get(queueKey) === tail) {\n feePayerQueues.delete(queueKey);\n }\n }\n}\n","import {\n bytesToBase64Url,\n createValidateChallenge,\n fetchWalletNonce,\n} from '@thru/passkey-manager';\nimport type { AccountContext } from '@thru/passkey-manager';\nimport type { PasskeyChallengeResult, ThruClient } from './types';\n\nexport async function createPasskeyChallenge(opts: {\n client: ThruClient;\n walletAddress: string;\n accountCtx: AccountContext;\n invokeIx: Uint8Array;\n}): Promise<PasskeyChallengeResult> {\n const nonce = await fetchWalletNonce(opts.client, opts.walletAddress);\n const challenge = await createValidateChallenge(\n nonce,\n opts.accountCtx.accountAddresses,\n opts.invokeIx\n );\n\n return {\n challenge: bytesToBase64Url(challenge),\n nonce: nonce.toString(),\n };\n}\n","import {\n PASSKEY_MANAGER_PROGRAM_ADDRESS,\n concatenateInstructions,\n encodeValidateInstruction,\n hexToBytes,\n} from '@thru/passkey-manager';\nimport type { AccountContext } from '@thru/passkey-manager';\nimport { trackTransaction, withSerializedFeePayer } from './utils';\nimport type {\n PasskeySignaturePayload,\n ThruClient,\n TransactionResult,\n} from './types';\n\nexport async function submitPasskeyTransaction(opts: {\n client: ThruClient;\n adminPublicKey: Uint8Array;\n adminPrivateKey: Uint8Array;\n walletAddress: string;\n accountCtx: AccountContext;\n invokeIx: Uint8Array;\n} & PasskeySignaturePayload): Promise<TransactionResult> {\n return withSerializedFeePayer(opts.adminPublicKey, async () => {\n const validateIx = encodeValidateInstruction({\n walletAccountIdx: opts.accountCtx.walletAccountIdx,\n authIdx: 0,\n signatureR: hexToBytes(opts.signatureR),\n signatureS: hexToBytes(opts.signatureS),\n authenticatorData: Buffer.from(opts.authenticatorData, 'base64'),\n clientDataJSON: Buffer.from(opts.clientDataJSON, 'base64'),\n });\n\n const instructionData = concatenateInstructions([validateIx, opts.invokeIx]);\n const transaction = await opts.client.transactions.build({\n feePayer: { publicKey: opts.adminPublicKey },\n program: PASSKEY_MANAGER_PROGRAM_ADDRESS,\n instructionData,\n accounts: {\n readWrite: opts.accountCtx.readWriteAddresses,\n readOnly: opts.accountCtx.readOnlyAddresses,\n },\n header: { fee: 0n },\n });\n\n await transaction.sign(opts.adminPrivateKey);\n const signature = await opts.client.transactions.send(transaction.toWire());\n return trackTransaction(opts.client, signature);\n });\n}\n","import type { PasskeyContextResult } from './types';\nimport { createPasskeyChallenge } from './challenge';\nimport { submitPasskeyTransaction } from './submit';\nimport type {\n PasskeyChallengeSubmitPayload,\n ThruClient,\n TransactionResult,\n} from './types';\n\nexport function createPasskeyHandlers<P>(opts: {\n buildContext: (params: P) => Promise<PasskeyContextResult>;\n adminPublicKey: Uint8Array;\n adminPrivateKey: Uint8Array;\n client: ThruClient;\n challengeTtlMs?: number;\n}) {\n const pendingContexts = new Map<\n string,\n { context: PasskeyContextResult; createdAt: number }\n >();\n const challengeTtlMs = opts.challengeTtlMs ?? 5 * 60_000;\n\n function createPendingContextKey(\n walletAddress: string,\n nonce: string,\n challenge: string\n ): string {\n return `${walletAddress}:${nonce}:${challenge}`;\n }\n\n function prunePendingContexts(now = Date.now()): void {\n for (const [nonce, entry] of pendingContexts.entries()) {\n if (now - entry.createdAt > challengeTtlMs) {\n pendingContexts.delete(nonce);\n }\n }\n }\n\n return {\n challenge: async (walletAddress: string, params: P) => {\n prunePendingContexts();\n\n const context = await opts.buildContext(params);\n const challenge = await createPasskeyChallenge({\n client: opts.client,\n walletAddress,\n accountCtx: context.accountCtx,\n invokeIx: context.invokeIx,\n });\n\n pendingContexts.set(\n createPendingContextKey(walletAddress, challenge.nonce, challenge.challenge),\n {\n context,\n createdAt: Date.now(),\n }\n );\n\n return challenge;\n },\n submit: async (\n walletAddress: string,\n params: P,\n payload: PasskeyChallengeSubmitPayload\n ): Promise<TransactionResult> => {\n void params;\n prunePendingContexts();\n\n const pendingKey = createPendingContextKey(\n walletAddress,\n payload.nonce,\n payload.challenge\n );\n const pending = pendingContexts.get(pendingKey);\n if (!pending) {\n throw new Error('Missing or expired challenge nonce');\n }\n\n pendingContexts.delete(pendingKey);\n const { nonce: _nonce, challenge: _challenge, ...signaturePayload } = payload;\n\n return submitPasskeyTransaction({\n client: opts.client,\n adminPublicKey: opts.adminPublicKey,\n adminPrivateKey: opts.adminPrivateKey,\n walletAddress,\n accountCtx: pending.context.accountCtx,\n invokeIx: pending.context.invokeIx,\n ...signaturePayload,\n });\n },\n };\n}\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;;;ACVA,IAAM,sBAAsB;AAEnC,IAAM,UAAU,IAAI,WAAW,GAAG,EAA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AAQ,QAAQ;AAAA,QAC5B,UAAU,QAAQ,QAAQ;AAAA,QAC1B,GAAG;AAAA,MACL,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["PASSKEY_MANAGER_PROGRAM_ADDRESS","hexToBytes","hexToBytes","PASSKEY_MANAGER_PROGRAM_ADDRESS"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thru/passkey",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -38,7 +38,7 @@
38
38
  }
39
39
  },
40
40
  "dependencies": {
41
- "@thru/passkey-manager": "0.2.15"
41
+ "@thru/passkey-manager": "0.2.17"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "expo-secure-store": "*",
@@ -62,11 +62,15 @@
62
62
  },
63
63
  "devDependencies": {
64
64
  "tsup": "^8.5.0",
65
- "typescript": "^5.9.3"
65
+ "typescript": "^5.9.3",
66
+ "vitest": "^3.2.4"
66
67
  },
67
68
  "scripts": {
68
69
  "build": "tsup",
69
70
  "dev": "tsup --watch",
71
+ "test": "vitest run",
72
+ "test:run": "vitest run",
73
+ "test:watch": "vitest watch",
70
74
  "clean": "rm -rf dist"
71
75
  }
72
76
  }
@@ -0,0 +1,13 @@
1
+ declare module 'expo-secure-store' {
2
+ export const WHEN_UNLOCKED_THIS_DEVICE_ONLY: string;
3
+
4
+ export function setItemAsync(
5
+ key: string,
6
+ value: string,
7
+ options?: Record<string, unknown>
8
+ ): Promise<void>;
9
+
10
+ export function getItemAsync(key: string): Promise<string | null>;
11
+
12
+ export function deleteItemAsync(key: string): Promise<void>;
13
+ }
@@ -0,0 +1,25 @@
1
+ declare module 'react-native-passkeys' {
2
+ export interface PasskeyCreateResponse {
3
+ id: string;
4
+ response: {
5
+ getPublicKey?: () => string | undefined;
6
+ };
7
+ }
8
+
9
+ export interface PasskeyGetResponse {
10
+ id: string;
11
+ response: {
12
+ signature: string;
13
+ authenticatorData: string;
14
+ clientDataJSON: string;
15
+ };
16
+ }
17
+
18
+ export function create(
19
+ request: Record<string, unknown>
20
+ ): Promise<PasskeyCreateResponse | null>;
21
+
22
+ export function get(
23
+ request: Record<string, unknown>
24
+ ): Promise<PasskeyGetResponse | null>;
25
+ }
@@ -0,0 +1,186 @@
1
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import type { ThruClient } from './types';
3
+
4
+ vi.mock('@thru/helpers', () => ({
5
+ encodeAddress: (bytes: Uint8Array) => {
6
+ const first = bytes[0];
7
+ if (first === 11) return 'wallet-address';
8
+ if (first === 22) return 'lookup-address';
9
+ return `address-${Array.from(bytes).join('-')}`;
10
+ },
11
+ }));
12
+
13
+ vi.mock('@thru/passkey-manager', () => ({
14
+ PASSKEY_MANAGER_PROGRAM_ADDRESS: 'passkey-program',
15
+ base64UrlToBytes: () => new Uint8Array([7]),
16
+ buildAccountContext: (params: { readWriteAccounts: Uint8Array[] }) => ({
17
+ walletAccountIdx: params.readWriteAccounts.length === 0 ? 2 : 3,
18
+ readWriteAddresses:
19
+ params.readWriteAccounts.length === 0
20
+ ? ['wallet-address']
21
+ : ['lookup-address', 'wallet-address'],
22
+ readOnlyAddresses: [],
23
+ getAccountIndex: () => 2,
24
+ }),
25
+ createCredentialLookupSeed: async () => new Uint8Array([8]),
26
+ createWalletSeed: async () => new Uint8Array([1]),
27
+ deriveCredentialLookupAddress: async () => new Uint8Array([22]),
28
+ deriveWalletAddress: async () => new Uint8Array([11]),
29
+ encodeCreateInstruction: () => new Uint8Array([101]),
30
+ encodeRegisterCredentialInstruction: () => new Uint8Array([202]),
31
+ }));
32
+
33
+ import { createPasskeyWallet } from './create-wallet';
34
+
35
+ const feePayerQueueSymbol = Symbol.for('thru.sharedFeePayerQueues');
36
+
37
+ function createDeferred<T = void>() {
38
+ let resolve!: (value: T | PromiseLike<T>) => void;
39
+ const promise = new Promise<T>((res) => {
40
+ resolve = res;
41
+ });
42
+
43
+ return { promise, resolve };
44
+ }
45
+
46
+ function clearFeePayerQueues(): void {
47
+ const globalQueues = globalThis as typeof globalThis & {
48
+ [feePayerQueueSymbol]?: Map<string, Promise<void>>;
49
+ };
50
+
51
+ globalQueues[feePayerQueueSymbol]?.clear();
52
+ delete globalQueues[feePayerQueueSymbol];
53
+ }
54
+
55
+ describe('createPasskeyWallet', () => {
56
+ beforeEach(() => {
57
+ clearFeePayerQueues();
58
+ });
59
+
60
+ it('re-checks wallet and credential existence after acquiring the serialized fee-payer lock', async () => {
61
+ const walletTrackStarted = createDeferred<void>();
62
+ const walletTrackRelease = createDeferred<void>();
63
+ const lookupTrackStarted = createDeferred<void>();
64
+ const lookupTrackRelease = createDeferred<void>();
65
+
66
+ const state = {
67
+ walletExists: false,
68
+ lookupExists: false,
69
+ walletTrackCount: 0,
70
+ lookupTrackCount: 0,
71
+ };
72
+
73
+ const sentKinds: string[] = [];
74
+
75
+ const client = {
76
+ accounts: {
77
+ get: vi.fn(async (address: string) => {
78
+ if (address === 'wallet-address') {
79
+ if (!state.walletExists) throw new Error('missing wallet');
80
+ return { data: { data: new Uint8Array() } };
81
+ }
82
+
83
+ if (address === 'lookup-address') {
84
+ if (!state.lookupExists) throw new Error('missing lookup');
85
+ return { data: { data: new Uint8Array() } };
86
+ }
87
+
88
+ return { data: { data: new Uint8Array() } };
89
+ }),
90
+ },
91
+ proofs: {
92
+ generate: vi.fn(async () => ({ proof: new Uint8Array([9]) })),
93
+ },
94
+ transactions: {
95
+ build: vi.fn(async (params: { accounts: { readWrite: string[] } }) => {
96
+ const kind = params.accounts.readWrite.length === 1 ? 'wallet' : 'lookup';
97
+ return {
98
+ sign: vi.fn(async () => {}),
99
+ toWire: () => new TextEncoder().encode(kind),
100
+ };
101
+ }),
102
+ send: vi.fn(async (wire: Uint8Array) => {
103
+ const kind = new TextDecoder().decode(wire);
104
+ sentKinds.push(kind);
105
+ return `${kind}-sig-${sentKinds.length}`;
106
+ }),
107
+ track: vi.fn(async function* (signature: string) {
108
+ if (signature.startsWith('wallet-sig')) {
109
+ state.walletTrackCount += 1;
110
+ if (state.walletTrackCount === 1) {
111
+ walletTrackStarted.resolve();
112
+ await walletTrackRelease.promise;
113
+ state.walletExists = true;
114
+ }
115
+ } else {
116
+ state.lookupTrackCount += 1;
117
+ if (state.lookupTrackCount === 1) {
118
+ lookupTrackStarted.resolve();
119
+ await lookupTrackRelease.promise;
120
+ state.lookupExists = true;
121
+ }
122
+ }
123
+
124
+ yield {
125
+ executionResult: {
126
+ userErrorCode: 0n,
127
+ vmError: 0,
128
+ executionResult: 0n,
129
+ },
130
+ };
131
+ }),
132
+ },
133
+ } as unknown as ThruClient;
134
+
135
+ const opts = {
136
+ client,
137
+ adminPublicKey: new Uint8Array([1, 2, 3]),
138
+ adminPrivateKey: new Uint8Array([9, 9, 9]),
139
+ adminAddress: 'admin-address',
140
+ pubkeyX: new Uint8Array([4]),
141
+ pubkeyY: new Uint8Array([5]),
142
+ credentialId: 'credential-id',
143
+ walletName: 'default-wallet',
144
+ };
145
+
146
+ const first = createPasskeyWallet(opts);
147
+ await walletTrackStarted.promise;
148
+
149
+ const second = createPasskeyWallet(opts);
150
+ await Promise.resolve();
151
+
152
+ walletTrackRelease.resolve();
153
+ await lookupTrackStarted.promise;
154
+ await Promise.resolve();
155
+
156
+ lookupTrackRelease.resolve();
157
+
158
+ await expect(Promise.all([first, second])).resolves.toEqual([
159
+ {
160
+ walletAddress: 'wallet-address',
161
+ credentialLookupAddress: 'lookup-address',
162
+ },
163
+ {
164
+ walletAddress: 'wallet-address',
165
+ credentialLookupAddress: 'lookup-address',
166
+ },
167
+ ]);
168
+
169
+ expect(sentKinds).toEqual(['wallet', 'lookup']);
170
+ expect(state.walletTrackCount).toBe(1);
171
+ expect(state.lookupTrackCount).toBe(1);
172
+
173
+ expect(vi.mocked(client.transactions.build).mock.calls[0]?.[0].accounts).toEqual({
174
+ readWrite: ['wallet-address'],
175
+ readOnly: [],
176
+ });
177
+ expect(vi.mocked(client.transactions.build).mock.calls[1]?.[0].accounts).toEqual({
178
+ readWrite: ['lookup-address', 'wallet-address'],
179
+ readOnly: [],
180
+ });
181
+
182
+ const accountChecks = vi.mocked(client.accounts.get).mock.calls.map(([address]) => address);
183
+ expect(accountChecks.filter((address) => address === 'wallet-address')).toHaveLength(2);
184
+ expect(accountChecks.filter((address) => address === 'lookup-address')).toHaveLength(2);
185
+ });
186
+ });