@cavos/kit 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2777 @@
1
+ import { p256 } from '@noble/curves/p256';
2
+ import { sha256 } from '@noble/hashes/sha256';
3
+ import { hash, num, Signer, RpcProvider, PaymasterRpc, Account as Account$1 } from 'starknet';
4
+ import { PublicKey, TransactionInstruction, SystemProgram, SYSVAR_INSTRUCTIONS_PUBKEY, Transaction, Connection, sendAndConfirmTransaction } from '@solana/web3.js';
5
+ import { hkdf } from '@noble/hashes/hkdf';
6
+ import { pbkdf2 } from '@noble/hashes/pbkdf2';
7
+ import { randomBytes } from '@noble/hashes/utils';
8
+ import { rpc, hash as hash$1, xdr, Address, StrKey, nativeToScVal, Operation, scValToNative, Account, TransactionBuilder, BASE_FEE } from '@stellar/stellar-sdk';
9
+
10
+ // src/crypto/encoding.ts
11
+ var U128_MASK = (1n << 128n) - 1n;
12
+ function u256ToFelts(value) {
13
+ return [value & U128_MASK, value >> 128n];
14
+ }
15
+ function bytesToBigInt(bytes) {
16
+ let out = 0n;
17
+ for (const b of bytes) out = out << 8n | BigInt(b);
18
+ return out;
19
+ }
20
+ function bytesToHex(bytes) {
21
+ let s = "0x";
22
+ for (const b of bytes) s += b.toString(16).padStart(2, "0");
23
+ return s;
24
+ }
25
+ function hexToBytes(hex) {
26
+ const clean = hex.startsWith("0x") ? hex.slice(2) : hex;
27
+ const padded = clean.length % 2 ? "0" + clean : clean;
28
+ const out = new Uint8Array(padded.length / 2);
29
+ for (let i = 0; i < out.length; i++) out[i] = parseInt(padded.slice(i * 2, i * 2 + 2), 16);
30
+ return out;
31
+ }
32
+ function bytesToByteArrayCalldata(bytes) {
33
+ const CHUNK = 31;
34
+ const fullCount = Math.floor(bytes.length / CHUNK);
35
+ const out = [String(fullCount)];
36
+ for (let i = 0; i < fullCount; i++) {
37
+ out.push("0x" + bytesToBigInt(bytes.subarray(i * CHUNK, i * CHUNK + CHUNK)).toString(16));
38
+ }
39
+ const rem = bytes.subarray(fullCount * CHUNK);
40
+ out.push("0x" + (rem.length ? bytesToBigInt(rem).toString(16) : "0"));
41
+ out.push(String(rem.length));
42
+ return out;
43
+ }
44
+ function bigIntTo32Bytes(value) {
45
+ const out = new Uint8Array(32);
46
+ let v = value;
47
+ for (let i = 31; i >= 0; i--) {
48
+ out[i] = Number(v & 0xffn);
49
+ v >>= 8n;
50
+ }
51
+ return out;
52
+ }
53
+ function signatureToFelts(sig) {
54
+ const [rLow, rHigh] = u256ToFelts(sig.r);
55
+ const [sLow, sHigh] = u256ToFelts(sig.s);
56
+ return [rLow, rHigh, sLow, sHigh, sig.yParity ? 1n : 0n];
57
+ }
58
+ function recoverYParity(r, s, digest, pubkey) {
59
+ for (const bit of [0, 1]) {
60
+ try {
61
+ const candidate = new p256.Signature(r, s).addRecoveryBit(bit);
62
+ const point = candidate.recoverPublicKey(digest).toAffine();
63
+ if (point.x === pubkey.x && point.y === pubkey.y) {
64
+ return bit === 1;
65
+ }
66
+ } catch {
67
+ }
68
+ }
69
+ throw new Error("kit/signature: could not recover parity for the given pubkey");
70
+ }
71
+ var IDB_NAME = "cavos-kit";
72
+ var IDB_STORE = "device-keys";
73
+ var WebCryptoSigner = class _WebCryptoSigner {
74
+ constructor(privateKey, publicKey, keyId) {
75
+ this.privateKey = privateKey;
76
+ this.publicKey = publicKey;
77
+ this.keyId = keyId;
78
+ }
79
+ /** Create a fresh device key (first run on this device) and persist it. */
80
+ static async create(opts) {
81
+ assertSecureContext();
82
+ const pair = await crypto.subtle.generateKey(
83
+ { name: "ECDSA", namedCurve: "P-256" },
84
+ false,
85
+ // private key is NON-extractable
86
+ ["sign", "verify"]
87
+ );
88
+ const publicKey = await exportPublicKey(pair.publicKey);
89
+ await idbPut(opts.keyId, { privateKey: pair.privateKey, x: publicKey.x, y: publicKey.y });
90
+ return new _WebCryptoSigner(pair.privateKey, publicKey, opts.keyId);
91
+ }
92
+ /** Load an existing device key from storage, or null if none exists yet. */
93
+ static async load(opts) {
94
+ const rec = await idbGet(opts.keyId);
95
+ if (!rec) return null;
96
+ return new _WebCryptoSigner(rec.privateKey, { x: rec.x, y: rec.y }, opts.keyId);
97
+ }
98
+ /** Load the device key, creating one on first use. */
99
+ static async loadOrCreate(opts) {
100
+ return await _WebCryptoSigner.load(opts) ?? await _WebCryptoSigner.create(opts);
101
+ }
102
+ async getPublicKey() {
103
+ return this.publicKey;
104
+ }
105
+ async sign(txHash) {
106
+ const raw = new Uint8Array(
107
+ await crypto.subtle.sign(
108
+ { name: "ECDSA", hash: "SHA-256" },
109
+ this.privateKey,
110
+ txHash
111
+ )
112
+ );
113
+ const r = bytesToBigInt(raw.subarray(0, 32));
114
+ const s = bytesToBigInt(raw.subarray(32, 64));
115
+ const digest = sha256(txHash);
116
+ const yParity = recoverYParity(r, s, digest, this.publicKey);
117
+ return { r, s, yParity };
118
+ }
119
+ };
120
+ async function exportPublicKey(publicKey) {
121
+ const raw = new Uint8Array(await crypto.subtle.exportKey("raw", publicKey));
122
+ return { x: bytesToBigInt(raw.subarray(1, 33)), y: bytesToBigInt(raw.subarray(33, 65)) };
123
+ }
124
+ function assertSecureContext() {
125
+ const ok = typeof crypto !== "undefined" && typeof crypto.subtle !== "undefined" && (typeof window === "undefined" || window.isSecureContext);
126
+ if (!ok) {
127
+ throw new Error(
128
+ "Cavos: WebCrypto is unavailable. Device keys require a secure context \u2014 use HTTPS, or http://localhost. (For LAN/mobile dev testing, run `next dev --experimental-https`.)"
129
+ );
130
+ }
131
+ }
132
+ function openDb() {
133
+ return new Promise((resolve, reject) => {
134
+ const req = indexedDB.open(IDB_NAME, 1);
135
+ req.onupgradeneeded = () => req.result.createObjectStore(IDB_STORE);
136
+ req.onsuccess = () => resolve(req.result);
137
+ req.onerror = () => reject(req.error);
138
+ });
139
+ }
140
+ async function idbPut(keyId, value) {
141
+ const db = await openDb();
142
+ await tx(db, "readwrite", (store) => store.put(value, keyId));
143
+ db.close();
144
+ }
145
+ async function idbGet(keyId) {
146
+ const db = await openDb();
147
+ const result = await tx(db, "readonly", (store) => store.get(keyId));
148
+ db.close();
149
+ return result ?? null;
150
+ }
151
+ function tx(db, mode, run) {
152
+ return new Promise((resolve, reject) => {
153
+ const store = db.transaction(IDB_STORE, mode).objectStore(IDB_STORE);
154
+ const req = run(store);
155
+ req.onsuccess = () => resolve(req.result);
156
+ req.onerror = () => reject(req.error);
157
+ });
158
+ }
159
+
160
+ // src/chains/starknet/constants.ts
161
+ var STARKNET_NETWORKS = {
162
+ sepolia: {
163
+ chainId: "0x534e5f5345504f4c4941",
164
+ // SN_SEPOLIA
165
+ rpcUrl: "https://api.cartridge.gg/x/starknet/sepolia"
166
+ },
167
+ mainnet: {
168
+ chainId: "0x534e5f4d41494e",
169
+ // SN_MAIN
170
+ rpcUrl: "https://api.cartridge.gg/x/starknet/mainnet"
171
+ }
172
+ };
173
+ var UDC_ADDRESS = "0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf";
174
+ var CAVOS_PAYMASTER_URL = {
175
+ sepolia: "https://sepolia-paymaster.cavos.xyz",
176
+ mainnet: "https://paymaster.cavos.xyz"
177
+ };
178
+ var DEVICE_ACCOUNT_CLASS_HASH = {
179
+ sepolia: "0x25cbc5423e8ee895febb0ef2c3945b408da44d0039d915fbdd681fe6b6ba66b",
180
+ mainnet: "0x1840aded59e8a0d2b440a134cb9079a7fc11b06c77f58ed189ab436a034ca6a"
181
+ };
182
+ var StarknetAdapter = class {
183
+ constructor(opts) {
184
+ this.opts = opts;
185
+ this.chain = "starknet";
186
+ }
187
+ computeAddress({ addressSeed, initialSigner, salt }) {
188
+ return hash.calculateContractAddressFromHash(
189
+ num.toHex(salt ?? addressSeed),
190
+ this.opts.classHash,
191
+ this.constructorCalldata(addressSeed, initialSigner),
192
+ 0
193
+ // deployerAddress 0 => deterministic counterfactual address
194
+ );
195
+ }
196
+ /** Single UDC deploy; the constructor registers the first device signer, so
197
+ * the account is ready the moment it is deployed (fits the paymaster's
198
+ * deploy + execute_from_outside bundle). */
199
+ buildDeploy(params) {
200
+ const salt = params.salt ?? params.addressSeed;
201
+ const calldata = this.constructorCalldata(params.addressSeed, params.initialSigner);
202
+ return [
203
+ {
204
+ contractAddress: UDC_ADDRESS,
205
+ entrypoint: "deployContract",
206
+ calldata: [
207
+ this.opts.classHash,
208
+ num.toHex(salt),
209
+ "0x0",
210
+ // unique = false -> deployer-independent address
211
+ num.toHex(calldata.length),
212
+ ...calldata
213
+ ]
214
+ }
215
+ ];
216
+ }
217
+ /** Constructor calldata: [address_seed, pub_x_low, pub_x_high, pub_y_low, pub_y_high]. */
218
+ constructorCalldata(addressSeed, initialSigner) {
219
+ return [num.toHex(addressSeed), ...pubkeyCalldata(initialSigner)];
220
+ }
221
+ buildAddSigner(accountAddress, signer) {
222
+ return { contractAddress: accountAddress, entrypoint: "add_signer", calldata: pubkeyCalldata(signer) };
223
+ }
224
+ buildRemoveSigner(accountAddress, signer) {
225
+ return { contractAddress: accountAddress, entrypoint: "remove_signer", calldata: pubkeyCalldata(signer) };
226
+ }
227
+ async isAuthorizedSigner(accountAddress, signer) {
228
+ if (!this.opts.provider) throw new Error("kit/starknet: provider required for reads");
229
+ const res = await this.opts.provider.callContract({
230
+ contractAddress: accountAddress,
231
+ entrypoint: "is_authorized_signer",
232
+ calldata: pubkeyCalldata(signer)
233
+ });
234
+ return BigInt(res[0] ?? 0) !== 0n;
235
+ }
236
+ async buildSignature(txHash) {
237
+ if (!this.opts.signer) throw new Error("kit/starknet: signer required to sign");
238
+ const sig = await this.opts.signer.sign(bigIntTo32Bytes(txHash));
239
+ return signatureToFelts(sig).map((f) => num.toHex(f));
240
+ }
241
+ // --- passkey approvers ---
242
+ buildAddApprover(accountAddress, passkey) {
243
+ return { contractAddress: accountAddress, entrypoint: "add_approver", calldata: pubkeyCalldata(passkey) };
244
+ }
245
+ buildRemoveApprover(accountAddress, passkey) {
246
+ return { contractAddress: accountAddress, entrypoint: "remove_approver", calldata: pubkeyCalldata(passkey) };
247
+ }
248
+ async isApprover(accountAddress, passkey) {
249
+ if (!this.opts.provider) throw new Error("kit/starknet: provider required for reads");
250
+ const res = await this.opts.provider.callContract({
251
+ contractAddress: accountAddress,
252
+ entrypoint: "is_approver",
253
+ calldata: pubkeyCalldata(passkey)
254
+ });
255
+ return BigInt(res[0] ?? 0) !== 0n;
256
+ }
257
+ async getPasskeyNonce(accountAddress) {
258
+ if (!this.opts.provider) throw new Error("kit/starknet: provider required for reads");
259
+ const res = await this.opts.provider.callContract({
260
+ contractAddress: accountAddress,
261
+ entrypoint: "get_passkey_nonce",
262
+ calldata: []
263
+ });
264
+ return BigInt(res[0] ?? 0);
265
+ }
266
+ /** This chain's leaf for approving `add_signer(newSigner)` at `nonce`:
267
+ * `sha256(new_x || new_y || nonce)` (coords 32B BE, nonce 16B BE). The batch
268
+ * challenge the passkey signs is `sha256(concat(leaves))` across chains. */
269
+ passkeyLeaf(newSigner, nonce) {
270
+ const msg = new Uint8Array(32 + 32 + 16);
271
+ msg.set(bigIntTo32Bytes(newSigner.x), 0);
272
+ msg.set(bigIntTo32Bytes(newSigner.y), 32);
273
+ msg.set(bigIntTo32Bytes(nonce).subarray(16), 64);
274
+ return sha256(msg);
275
+ }
276
+ /** Passkey-authorized `add_signer` call. `leaves`/`leafIndex` place this chain's
277
+ * leaf in the multi-chain batch (single chain → `[leaf]`, index 0). `yParity`
278
+ * matches the raw `(r, s)` — the contract normalizes high-S internally. */
279
+ buildAddSignerViaPasskey(accountAddress, newSigner, nonce, leaves, leafIndex, assertion, yParity) {
280
+ const [rl, rh] = u256ToFelts(assertion.r);
281
+ const [sl, sh] = u256ToFelts(assertion.s);
282
+ const leavesCalldata = [String(leaves.length)];
283
+ for (const leaf of leaves) {
284
+ const [lo, hi] = u256ToFelts(bytesToBigInt(leaf));
285
+ leavesCalldata.push(num.toHex(lo), num.toHex(hi));
286
+ }
287
+ return {
288
+ contractAddress: accountAddress,
289
+ entrypoint: "add_signer_via_passkey",
290
+ calldata: [
291
+ ...pubkeyCalldata(newSigner),
292
+ // new_x, new_y (u256 pairs)
293
+ num.toHex(nonce),
294
+ ...leavesCalldata,
295
+ // Array<u256> leaves
296
+ String(leafIndex),
297
+ ...bytesToByteArrayCalldata(assertion.authenticatorData),
298
+ ...bytesToByteArrayCalldata(assertion.clientDataJSON),
299
+ String(assertion.challengeOffset),
300
+ num.toHex(rl),
301
+ num.toHex(rh),
302
+ num.toHex(sl),
303
+ num.toHex(sh),
304
+ yParity ? "0x1" : "0x0"
305
+ ]
306
+ };
307
+ }
308
+ };
309
+ function pubkeyCalldata(pk) {
310
+ const [xl, xh] = u256ToFelts(pk.x);
311
+ const [yl, yh] = u256ToFelts(pk.y);
312
+ return [num.toHex(xl), num.toHex(xh), num.toHex(yl), num.toHex(yh)];
313
+ }
314
+ var StarknetDeviceSigner = class extends Signer {
315
+ constructor(device) {
316
+ super("0x1");
317
+ this.device = device;
318
+ }
319
+ /** Device accounts are not controlled by a single Stark pubkey. */
320
+ async getPubKey() {
321
+ return "0x0";
322
+ }
323
+ /** Sign the computed tx hash silently with the device signer. */
324
+ async signRaw(msgHash) {
325
+ const sig = await this.device.sign(bigIntTo32Bytes(BigInt(msgHash)));
326
+ return signatureToFelts(sig).map((f) => num.toHex(f));
327
+ }
328
+ };
329
+
330
+ // src/registry/WalletRegistry.ts
331
+ var InMemoryWalletRegistry = class {
332
+ constructor() {
333
+ this.wallets = /* @__PURE__ */ new Map();
334
+ }
335
+ async lookup(userId) {
336
+ return this.wallets.get(userId) ?? null;
337
+ }
338
+ async register(params) {
339
+ this.wallets.set(params.userId, { address: params.address, devices: [params.initialSigner] });
340
+ }
341
+ async addDevice(params) {
342
+ const w = this.wallets.get(params.userId);
343
+ if (w) w.devices = [...w.devices ?? [], params.signer];
344
+ }
345
+ };
346
+
347
+ // src/registry/HttpWalletRegistry.ts
348
+ function toHex(n) {
349
+ return "0x" + n.toString(16);
350
+ }
351
+ function fromHex(s) {
352
+ return BigInt(s);
353
+ }
354
+ var HttpWalletRegistry = class {
355
+ constructor(opts) {
356
+ this.opts = opts;
357
+ }
358
+ async lookup(userId) {
359
+ const url = new URL("/api/wallets", this.opts.baseUrl);
360
+ url.searchParams.set("app_id", this.opts.appId);
361
+ url.searchParams.set("user_social_id", userId);
362
+ url.searchParams.set("network", this.opts.network);
363
+ const res = await fetch(url, { headers: { "Content-Type": "application/json" } });
364
+ if (!res.ok) throw new Error(`registry lookup failed: ${res.status}`);
365
+ const data = await res.json();
366
+ if (!data.found || !data.address) return null;
367
+ const devices = Array.isArray(data.devices) ? data.devices.map((d) => ({
368
+ x: fromHex(d.pub_x),
369
+ y: fromHex(d.pub_y)
370
+ })) : void 0;
371
+ return { address: data.address, devices };
372
+ }
373
+ async register(params) {
374
+ const res = await fetch(new URL("/api/wallets", this.opts.baseUrl), {
375
+ method: "POST",
376
+ headers: { "Content-Type": "application/json" },
377
+ body: JSON.stringify({
378
+ app_id: this.opts.appId,
379
+ user_social_id: params.userId,
380
+ network: this.opts.network,
381
+ address: params.address,
382
+ // Device-signer wallets send their initial signer (no encrypted blob).
383
+ devices: [{ x: toHex(params.initialSigner.x), y: toHex(params.initialSigner.y) }]
384
+ })
385
+ });
386
+ if (!res.ok) {
387
+ const t = await res.text().catch(() => "");
388
+ throw new Error(`registry register failed: ${res.status} ${t}`);
389
+ }
390
+ }
391
+ async addDevice(params) {
392
+ }
393
+ };
394
+ function deriveAddressSeed({ userId, appSalt }) {
395
+ const h = hash.computePoseidonHashOnElements([feltFromString(userId), feltFromString(appSalt)]);
396
+ return BigInt(h);
397
+ }
398
+ function deriveAddressSeedSolana({ userId, appSalt }) {
399
+ return sha256(new TextEncoder().encode(`cavos:solana:v1:${userId}:${appSalt}`));
400
+ }
401
+ function deriveAddressSeedStellar({ userId, appSalt }) {
402
+ return sha256(new TextEncoder().encode(`cavos:stellar:v1:${userId}:${appSalt}`));
403
+ }
404
+ function feltFromString(s) {
405
+ const bytes = new TextEncoder().encode(s);
406
+ const chunks = [];
407
+ for (let i = 0; i < bytes.length; i += 31) {
408
+ let w = 0n;
409
+ for (const b of bytes.subarray(i, i + 31)) w = w << 8n | BigInt(b);
410
+ chunks.push(w);
411
+ }
412
+ if (chunks.length === 0) return 0n;
413
+ if (chunks.length === 1) return chunks[0];
414
+ return BigInt(hash.computePoseidonHashOnElements(chunks));
415
+ }
416
+
417
+ // src/chains/solana/constants.ts
418
+ var DEVICE_ACCOUNT_PROGRAM_ID = "FHnoYNfYAmFrwt18gcBGG7G1S5q3RAbCBvrV2D29izNJ";
419
+ var SECP256R1_PROGRAM_ID = "Secp256r1SigVerify1111111111111111111111111";
420
+ var ACCOUNT_SEED = "cavos-account";
421
+ var DOMAIN_ADD = "cavos:add_signer:v1";
422
+ var DOMAIN_REMOVE = "cavos:remove_signer:v1";
423
+ var DOMAIN_TRANSFER = "cavos:transfer:v1";
424
+ var DOMAIN_EXECUTE = "cavos:execute:v1";
425
+ var DOMAIN_ADD_APPROVER = "cavos:add_approver:v1";
426
+ var DOMAIN_REMOVE_APPROVER = "cavos:remove_approver:v1";
427
+ var SECP256R1_N = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n;
428
+ var SOLANA_NETWORKS = {
429
+ "solana-devnet": "https://api.devnet.solana.com",
430
+ "solana-mainnet": "https://api.mainnet-beta.solana.com",
431
+ "solana-localnet": "http://127.0.0.1:8899"
432
+ };
433
+ var COMPRESSED_PUBKEY_SIZE = 33;
434
+ var SIGNATURE_SIZE = 64;
435
+ var CURRENT_IX = 65535;
436
+ var SolanaAdapter = class {
437
+ constructor(opts = {}) {
438
+ this.opts = opts;
439
+ this.chain = "solana";
440
+ this.programId = new PublicKey(opts.programId ?? DEVICE_ACCOUNT_PROGRAM_ID);
441
+ }
442
+ /** Deterministic account address: PDA of [seed, address_seed, initial_signer_x]. */
443
+ computeAddress(addressSeed, initialSigner) {
444
+ return this.pda(addressSeed, compressedPubkey(initialSigner)).toBase58();
445
+ }
446
+ pda(addressSeed, initialCompressed) {
447
+ const [pda] = PublicKey.findProgramAddressSync(
448
+ [
449
+ Buffer.from(ACCOUNT_SEED),
450
+ Buffer.from(addressSeed),
451
+ Buffer.from(initialCompressed.slice(1, 33))
452
+ // x-coordinate
453
+ ],
454
+ this.programId
455
+ );
456
+ return pda;
457
+ }
458
+ /** `initialize` instruction creating the account with its first device signer. */
459
+ buildInitialize(addressSeed, payer, initialSigner) {
460
+ const initialCompressed = compressedPubkey(initialSigner);
461
+ const account = this.pda(addressSeed, initialCompressed);
462
+ const data = Buffer.concat([
463
+ anchorDiscriminator("initialize"),
464
+ Buffer.from(addressSeed),
465
+ // [u8;32]
466
+ Buffer.from(initialCompressed)
467
+ // [u8;33]
468
+ ]);
469
+ return new TransactionInstruction({
470
+ programId: this.programId,
471
+ keys: [
472
+ { pubkey: account, isSigner: false, isWritable: true },
473
+ { pubkey: new PublicKey(payer), isSigner: true, isWritable: true },
474
+ { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }
475
+ ],
476
+ data
477
+ });
478
+ }
479
+ /** `[precompile, add_signer]` bundle, authorized by an existing device signer. */
480
+ async buildAddSigner(account, newSigner) {
481
+ const accountPk = new PublicKey(account);
482
+ const newCompressed = compressedPubkey(newSigner);
483
+ const nonce = await this.fetchNonce(accountPk);
484
+ const message = concatBytes(
485
+ Buffer.from(DOMAIN_ADD),
486
+ accountPk.toBuffer(),
487
+ newCompressed,
488
+ u64le(nonce)
489
+ );
490
+ const { precompileIx } = await this.signToPrecompile(message);
491
+ const ix = new TransactionInstruction({
492
+ programId: this.programId,
493
+ keys: this.guardedKeys(accountPk),
494
+ data: Buffer.concat([anchorDiscriminator("add_signer"), Buffer.from(newCompressed)])
495
+ });
496
+ return [precompileIx, ix];
497
+ }
498
+ /** `[precompile, remove_signer]` bundle, authorized by an existing device signer. */
499
+ async buildRemoveSigner(account, signer) {
500
+ const accountPk = new PublicKey(account);
501
+ const compressed = compressedPubkey(signer);
502
+ const nonce = await this.fetchNonce(accountPk);
503
+ const message = concatBytes(
504
+ Buffer.from(DOMAIN_REMOVE),
505
+ accountPk.toBuffer(),
506
+ compressed,
507
+ u64le(nonce)
508
+ );
509
+ const { precompileIx } = await this.signToPrecompile(message);
510
+ const ix = new TransactionInstruction({
511
+ programId: this.programId,
512
+ keys: this.guardedKeys(accountPk),
513
+ data: Buffer.concat([anchorDiscriminator("remove_signer"), Buffer.from(compressed)])
514
+ });
515
+ return [precompileIx, ix];
516
+ }
517
+ /** `[precompile, add_approver]` bundle enrolling a passkey approver (device-signed). */
518
+ async buildAddApprover(account, passkey) {
519
+ const accountPk = new PublicKey(account);
520
+ const compressed = compressedPubkey(passkey);
521
+ const nonce = await this.fetchNonce(accountPk);
522
+ const message = concatBytes(
523
+ Buffer.from(DOMAIN_ADD_APPROVER),
524
+ accountPk.toBuffer(),
525
+ compressed,
526
+ u64le(nonce)
527
+ );
528
+ const { precompileIx } = await this.signToPrecompile(message);
529
+ const ix = new TransactionInstruction({
530
+ programId: this.programId,
531
+ keys: this.guardedKeys(accountPk),
532
+ data: Buffer.concat([anchorDiscriminator("add_approver"), Buffer.from(compressed)])
533
+ });
534
+ return [precompileIx, ix];
535
+ }
536
+ /** `[precompile, remove_approver]` bundle (device-signed). */
537
+ async buildRemoveApprover(account, passkey) {
538
+ const accountPk = new PublicKey(account);
539
+ const compressed = compressedPubkey(passkey);
540
+ const nonce = await this.fetchNonce(accountPk);
541
+ const message = concatBytes(
542
+ Buffer.from(DOMAIN_REMOVE_APPROVER),
543
+ accountPk.toBuffer(),
544
+ compressed,
545
+ u64le(nonce)
546
+ );
547
+ const { precompileIx } = await this.signToPrecompile(message);
548
+ const ix = new TransactionInstruction({
549
+ programId: this.programId,
550
+ keys: this.guardedKeys(accountPk),
551
+ data: Buffer.concat([anchorDiscriminator("remove_approver"), Buffer.from(compressed)])
552
+ });
553
+ return [precompileIx, ix];
554
+ }
555
+ /** This chain's leaf for approving `add_signer(newSigner)` at `nonce`:
556
+ * `sha256(compressed(new_signer) || passkey_nonce_le8)`. The batch challenge the
557
+ * passkey signs is `sha256(concat(leaves))` across chains. */
558
+ passkeyLeaf(newSigner, nonce) {
559
+ return sha256(concatBytes(compressedPubkey(newSigner), u64le(nonce)));
560
+ }
561
+ /**
562
+ * `[precompile(passkey), add_signer_via_passkey]` bundle. The precompile ix
563
+ * verifies the PASSKEY's WebAuthn assertion over `authData || sha256(clientDataJSON)`;
564
+ * the program ix binds the challenge to `newSigner` + the passkey nonce and adds
565
+ * the signer. No device signature — a gasless relayer can submit it.
566
+ */
567
+ buildAddSignerViaPasskey(account, newSigner, passkey, leaves, leafIndex, assertion) {
568
+ const accountPk = new PublicKey(account);
569
+ const newCompressed = compressedPubkey(newSigner);
570
+ const passkeyCompressed = compressedPubkey(passkey);
571
+ const clientHash = sha256(assertion.clientDataJSON);
572
+ const message = concatBytes(assertion.authenticatorData, clientHash);
573
+ const signature = encodeLowSSignature(assertion.r, assertion.s);
574
+ const precompileIx = buildSecp256r1Instruction(passkeyCompressed, signature, message);
575
+ const leavesBlob = Buffer.concat([u32le(leaves.length), ...leaves.map((l) => Buffer.from(l))]);
576
+ const data = Buffer.concat([
577
+ anchorDiscriminator("add_signer_via_passkey"),
578
+ Buffer.from(newCompressed),
579
+ leavesBlob,
580
+ u32le(leafIndex),
581
+ serializeVecU8(assertion.authenticatorData),
582
+ serializeVecU8(assertion.clientDataJSON),
583
+ u32le(assertion.challengeOffset)
584
+ ]);
585
+ const ix = new TransactionInstruction({
586
+ programId: this.programId,
587
+ keys: this.guardedKeys(accountPk),
588
+ data
589
+ });
590
+ return [precompileIx, ix];
591
+ }
592
+ /** Read whether `passkey` is a registered approver. */
593
+ async isApprover(account, passkey) {
594
+ const approvers = await this.fetchApprovers(new PublicKey(account));
595
+ const target = Buffer.from(compressedPubkey(passkey)).toString("hex");
596
+ return approvers.some((a) => Buffer.from(a).toString("hex") === target);
597
+ }
598
+ /** Read the current passkey-approval nonce. */
599
+ async passkeyNonce(account) {
600
+ const info = await this.requireConnection().getAccountInfo(new PublicKey(account));
601
+ if (!info) return 0n;
602
+ const d = info.data;
603
+ const signersLenOff = 8 + 32 + 1 + 8 + COMPRESSED_PUBKEY_SIZE;
604
+ const signerCount = d.readUInt32LE(signersLenOff);
605
+ const approversLenOff = signersLenOff + 4 + signerCount * COMPRESSED_PUBKEY_SIZE;
606
+ const approverCount = d.readUInt32LE(approversLenOff);
607
+ const passkeyNonceOff = approversLenOff + 4 + approverCount * COMPRESSED_PUBKEY_SIZE;
608
+ return readU64le(d, passkeyNonceOff);
609
+ }
610
+ /** `[precompile, execute_transfer]` bundle moving lamports out of the account. */
611
+ async buildExecuteTransfer(account, destination, amount) {
612
+ const accountPk = new PublicKey(account);
613
+ const destPk = new PublicKey(destination);
614
+ const nonce = await this.fetchNonce(accountPk);
615
+ const message = concatBytes(
616
+ Buffer.from(DOMAIN_TRANSFER),
617
+ accountPk.toBuffer(),
618
+ destPk.toBuffer(),
619
+ u64le(amount),
620
+ u64le(nonce)
621
+ );
622
+ const { precompileIx } = await this.signToPrecompile(message);
623
+ const ix = new TransactionInstruction({
624
+ programId: this.programId,
625
+ keys: [
626
+ { pubkey: accountPk, isSigner: false, isWritable: true },
627
+ { pubkey: destPk, isSigner: false, isWritable: true },
628
+ { pubkey: SYSVAR_INSTRUCTIONS_PUBKEY, isSigner: false, isWritable: false }
629
+ ],
630
+ data: Buffer.concat([anchorDiscriminator("execute_transfer"), u64le(amount)])
631
+ });
632
+ return [precompileIx, ix];
633
+ }
634
+ /**
635
+ * `[precompile, execute]` bundle running arbitrary CPI instructions with the
636
+ * account PDA as signer. The device key signs over
637
+ * `DOMAIN_EXECUTE || account || sha256(canonical(instructions)) || nonce`, so
638
+ * the signature commits to the EXACT instruction set the program will invoke —
639
+ * no account/data substitution is possible after signing.
640
+ *
641
+ * The instructions' accounts are passed to the program via `remaining_accounts`
642
+ * (flattened, in order); the program enforces an exact, ordered mapping.
643
+ */
644
+ async buildExecute(account, instructions) {
645
+ if (instructions.length === 0) throw new Error("kit/solana: execute requires at least one instruction");
646
+ const accountPk = new PublicKey(account);
647
+ const nonce = await this.fetchNonce(accountPk);
648
+ const blob = serializeInstructions(instructions);
649
+ const ixsHash = sha256(blob);
650
+ const message = concatBytes(
651
+ Buffer.from(DOMAIN_EXECUTE),
652
+ accountPk.toBuffer(),
653
+ Buffer.from(ixsHash),
654
+ u64le(nonce)
655
+ );
656
+ const { precompileIx } = await this.signToPrecompile(message);
657
+ const blobLen = Buffer.alloc(4);
658
+ new DataView(blobLen.buffer).setUint32(0, blob.length, true);
659
+ const data = Buffer.concat([anchorDiscriminator("execute"), blobLen, blob]);
660
+ const remainingAccounts = [];
661
+ for (const ix2 of instructions) {
662
+ for (const acc of ix2.accounts) {
663
+ remainingAccounts.push({
664
+ pubkey: new PublicKey(acc.pubkey),
665
+ isSigner: false,
666
+ // signer flags are part of the signed InstructionData
667
+ isWritable: acc.isWritable
668
+ });
669
+ }
670
+ remainingAccounts.push({
671
+ pubkey: new PublicKey(ix2.programId),
672
+ isSigner: false,
673
+ isWritable: false
674
+ });
675
+ }
676
+ const ix = new TransactionInstruction({
677
+ programId: this.programId,
678
+ keys: [
679
+ { pubkey: accountPk, isSigner: false, isWritable: true },
680
+ { pubkey: SYSVAR_INSTRUCTIONS_PUBKEY, isSigner: false, isWritable: false },
681
+ ...remainingAccounts
682
+ ],
683
+ data
684
+ });
685
+ return [precompileIx, ix];
686
+ }
687
+ /** Read whether `signer` is currently an authorized signer of `account`. */
688
+ async isAuthorizedSigner(account, signer) {
689
+ const signers = await this.fetchSigners(new PublicKey(account));
690
+ const target = Buffer.from(compressedPubkey(signer)).toString("hex");
691
+ return signers.some((s) => Buffer.from(s).toString("hex") === target);
692
+ }
693
+ guardedKeys(account) {
694
+ return [
695
+ { pubkey: account, isSigner: false, isWritable: true },
696
+ { pubkey: SYSVAR_INSTRUCTIONS_PUBKEY, isSigner: false, isWritable: false }
697
+ ];
698
+ }
699
+ /** Sign `message` with the device key and build the matching precompile ix. */
700
+ async signToPrecompile(message) {
701
+ if (!this.opts.signer) throw new Error("kit/solana: signer required to authorize");
702
+ const pubkey = await this.opts.signer.getPublicKey();
703
+ const sig = await this.opts.signer.sign(message);
704
+ const signature = encodeLowSSignature(sig.r, sig.s);
705
+ const precompileIx = buildSecp256r1Instruction(
706
+ compressedPubkey(pubkey),
707
+ signature,
708
+ message
709
+ );
710
+ return { precompileIx };
711
+ }
712
+ async fetchNonce(account) {
713
+ const info = await this.requireConnection().getAccountInfo(account);
714
+ if (!info) return 0n;
715
+ return readU64le(info.data, 41);
716
+ }
717
+ async fetchSigners(account) {
718
+ const info = await this.requireConnection().getAccountInfo(account);
719
+ if (!info) return [];
720
+ const d = info.data;
721
+ const lenOffset = 8 + 32 + 1 + 8 + COMPRESSED_PUBKEY_SIZE;
722
+ const count = d.readUInt32LE(lenOffset);
723
+ const out = [];
724
+ let off = lenOffset + 4;
725
+ for (let i = 0; i < count; i++) {
726
+ out.push(Uint8Array.from(d.subarray(off, off + COMPRESSED_PUBKEY_SIZE)));
727
+ off += COMPRESSED_PUBKEY_SIZE;
728
+ }
729
+ return out;
730
+ }
731
+ async fetchApprovers(account) {
732
+ const info = await this.requireConnection().getAccountInfo(account);
733
+ if (!info) return [];
734
+ const d = info.data;
735
+ const signersLenOff = 8 + 32 + 1 + 8 + COMPRESSED_PUBKEY_SIZE;
736
+ const signerCount = d.readUInt32LE(signersLenOff);
737
+ const approversLenOff = signersLenOff + 4 + signerCount * COMPRESSED_PUBKEY_SIZE;
738
+ const count = d.readUInt32LE(approversLenOff);
739
+ const out = [];
740
+ let off = approversLenOff + 4;
741
+ for (let i = 0; i < count; i++) {
742
+ out.push(Uint8Array.from(d.subarray(off, off + COMPRESSED_PUBKEY_SIZE)));
743
+ off += COMPRESSED_PUBKEY_SIZE;
744
+ }
745
+ return out;
746
+ }
747
+ requireConnection() {
748
+ if (!this.opts.connection) throw new Error("kit/solana: connection required for reads");
749
+ return this.opts.connection;
750
+ }
751
+ };
752
+ function compressedPubkey(pk) {
753
+ const out = new Uint8Array(COMPRESSED_PUBKEY_SIZE);
754
+ out[0] = pk.y % 2n === 0n ? 2 : 3;
755
+ out.set(bigIntTo32Bytes(pk.x), 1);
756
+ return out;
757
+ }
758
+ function encodeLowSSignature(r, s) {
759
+ const lowS2 = s > SECP256R1_N / 2n ? SECP256R1_N - s : s;
760
+ const out = new Uint8Array(SIGNATURE_SIZE);
761
+ out.set(bigIntTo32Bytes(r), 0);
762
+ out.set(bigIntTo32Bytes(lowS2), 32);
763
+ return out;
764
+ }
765
+ function buildSecp256r1Instruction(compressed, signature, message) {
766
+ const headerLen = 2;
767
+ const offsetsLen = 14;
768
+ const pubkeyOffset = headerLen + offsetsLen;
769
+ const sigOffset = pubkeyOffset + COMPRESSED_PUBKEY_SIZE;
770
+ const msgOffset = sigOffset + SIGNATURE_SIZE;
771
+ const data = Buffer.alloc(msgOffset + message.length);
772
+ data.writeUInt8(1, 0);
773
+ data.writeUInt8(0, 1);
774
+ let o = headerLen;
775
+ data.writeUInt16LE(sigOffset, o);
776
+ o += 2;
777
+ data.writeUInt16LE(CURRENT_IX, o);
778
+ o += 2;
779
+ data.writeUInt16LE(pubkeyOffset, o);
780
+ o += 2;
781
+ data.writeUInt16LE(CURRENT_IX, o);
782
+ o += 2;
783
+ data.writeUInt16LE(msgOffset, o);
784
+ o += 2;
785
+ data.writeUInt16LE(message.length, o);
786
+ o += 2;
787
+ data.writeUInt16LE(CURRENT_IX, o);
788
+ o += 2;
789
+ Buffer.from(compressed).copy(data, pubkeyOffset);
790
+ Buffer.from(signature).copy(data, sigOffset);
791
+ Buffer.from(message).copy(data, msgOffset);
792
+ return new TransactionInstruction({
793
+ keys: [],
794
+ programId: new PublicKey(SECP256R1_PROGRAM_ID),
795
+ data
796
+ });
797
+ }
798
+ function anchorDiscriminator(name) {
799
+ return Buffer.from(sha256(`global:${name}`).slice(0, 8));
800
+ }
801
+ function u32le(n) {
802
+ const b = Buffer.alloc(4);
803
+ b.writeUInt32LE(n);
804
+ return b;
805
+ }
806
+ function u64le(n) {
807
+ const b = Buffer.alloc(8);
808
+ new DataView(b.buffer, b.byteOffset, 8).setBigUint64(0, BigInt(n), true);
809
+ return b;
810
+ }
811
+ function readU64le(buf, offset) {
812
+ return new DataView(buf.buffer, buf.byteOffset, buf.length).getBigUint64(
813
+ offset,
814
+ true
815
+ );
816
+ }
817
+ function concatBytes(...parts) {
818
+ const total = parts.reduce((n, p) => n + p.length, 0);
819
+ const out = new Uint8Array(total);
820
+ let off = 0;
821
+ for (const p of parts) {
822
+ out.set(p, off);
823
+ off += p.length;
824
+ }
825
+ return out;
826
+ }
827
+ function serializeInstruction(ix) {
828
+ const programId = new PublicKey(ix.programId).toBuffer();
829
+ const accounts = serializeAccounts(ix.accounts);
830
+ const data = serializeVecU8(ix.data);
831
+ return Buffer.concat([programId, accounts, data]);
832
+ }
833
+ function serializeAccounts(metas) {
834
+ const len = Buffer.alloc(4);
835
+ new DataView(len.buffer).setUint32(0, metas.length, true);
836
+ const parts = metas.map(serializeAccountMeta);
837
+ return Buffer.concat([len, ...parts]);
838
+ }
839
+ function serializeAccountMeta(meta) {
840
+ const pubkey = new PublicKey(meta.pubkey).toBuffer();
841
+ return Buffer.concat([pubkey, Buffer.from([meta.isSigner ? 1 : 0, meta.isWritable ? 1 : 0])]);
842
+ }
843
+ function serializeVecU8(data) {
844
+ const len = Buffer.alloc(4);
845
+ new DataView(len.buffer).setUint32(0, data.length, true);
846
+ return Buffer.concat([len, Buffer.from(data)]);
847
+ }
848
+ function serializeInstructions(instructions) {
849
+ return Buffer.concat(instructions.map(serializeInstruction));
850
+ }
851
+ var SolanaRelayer = class {
852
+ constructor(opts) {
853
+ this.opts = opts;
854
+ }
855
+ /** The relayer's fee-payer pubkey (fetched + cached from the backend). */
856
+ async getFeePayer() {
857
+ if (this.feePayer) return this.feePayer;
858
+ const res = await fetch(`${this.opts.baseUrl}/api/solana/relay?network=${this.opts.network}`);
859
+ if (!res.ok) throw new Error(`kit/solana: relayer fee-payer lookup failed (${res.status})`);
860
+ const { fee_payer } = await res.json();
861
+ this.feePayer = new PublicKey(fee_payer);
862
+ return this.feePayer;
863
+ }
864
+ /**
865
+ * Build a tx with the relayer as fee payer, serialize it unsigned, and POST it
866
+ * to the relayer to co-sign + submit. Returns the confirmed signature.
867
+ */
868
+ async send(instructions) {
869
+ const feePayer = await this.getFeePayer();
870
+ const { blockhash } = await this.opts.connection.getLatestBlockhash("confirmed");
871
+ const tx2 = new Transaction();
872
+ tx2.feePayer = feePayer;
873
+ tx2.recentBlockhash = blockhash;
874
+ tx2.add(...instructions);
875
+ const serialized = tx2.serialize({ requireAllSignatures: false, verifySignatures: false }).toString("base64");
876
+ const res = await fetch(`${this.opts.baseUrl}/api/solana/relay`, {
877
+ method: "POST",
878
+ headers: { "Content-Type": "application/json" },
879
+ body: JSON.stringify({
880
+ app_id: this.opts.appId,
881
+ network: this.opts.network,
882
+ transaction: serialized
883
+ })
884
+ });
885
+ if (!res.ok) {
886
+ const detail = await res.text().catch(() => "");
887
+ throw new Error(`kit/solana: relay failed (${res.status}) ${detail}`);
888
+ }
889
+ const { signature } = await res.json();
890
+ return signature;
891
+ }
892
+ };
893
+ var BACKUP_KDF_SALT = "cavos-recovery-v1";
894
+ var BACKUP_PBKDF2_ITERATIONS = 21e4;
895
+ var BACKUP_HKDF_INFO = "cavos-backup-signer";
896
+ var CODE_WORDS = 16;
897
+ function generateRecoveryCode() {
898
+ const bytes = randomBytes(CODE_WORDS);
899
+ const words = [];
900
+ for (const b of bytes) words.push(WORDLIST[b]);
901
+ return words.join(" ");
902
+ }
903
+ function deriveBackupKey(code) {
904
+ const normalised = code.trim().replace(/\s+/g, " ").toLowerCase();
905
+ if (!normalised) throw new Error("kit: recovery code is empty");
906
+ const stretched = pbkdf2(
907
+ sha256,
908
+ new TextEncoder().encode(normalised),
909
+ new TextEncoder().encode(BACKUP_KDF_SALT),
910
+ { c: BACKUP_PBKDF2_ITERATIONS, dkLen: 32 }
911
+ );
912
+ const seed = hkdf(sha256, stretched, void 0, BACKUP_HKDF_INFO, 32);
913
+ const d = bytesToBigInt(seed) % p256.CURVE.n;
914
+ if (d === 0n) throw new Error("kit: derived backup key is zero (retry with a new code)");
915
+ const priv = bigIntTo32Bytes(d);
916
+ const pub = p256.getPublicKey(priv, false);
917
+ return {
918
+ privateKey: priv,
919
+ publicKey: { x: bytesToBigInt(pub.subarray(1, 33)), y: bytesToBigInt(pub.subarray(33, 65)) }
920
+ };
921
+ }
922
+ var BackupSigner = class _BackupSigner {
923
+ constructor(privateKey, publicKey) {
924
+ this.privateKey = privateKey;
925
+ this.publicKeyValue = publicKey;
926
+ }
927
+ /** Build a signer from a recovery code (derive + wrap in one step). */
928
+ static fromCode(code) {
929
+ const { privateKey, publicKey } = deriveBackupKey(code);
930
+ return new _BackupSigner(privateKey, publicKey);
931
+ }
932
+ async getPublicKey() {
933
+ return this.publicKeyValue;
934
+ }
935
+ async sign(txHash) {
936
+ const digest = sha256(txHash);
937
+ const sig = p256.sign(digest, this.privateKey);
938
+ const yParity = recoverYParity(sig.r, sig.s, digest, this.publicKeyValue);
939
+ return { r: sig.r, s: sig.s, yParity };
940
+ }
941
+ };
942
+ var WORDLIST = [
943
+ "able",
944
+ "acid",
945
+ "amber",
946
+ "apple",
947
+ "arch",
948
+ "arrow",
949
+ "ashen",
950
+ "atlas",
951
+ "axis",
952
+ "badge",
953
+ "baker",
954
+ "balm",
955
+ "banner",
956
+ "basin",
957
+ "beacon",
958
+ "bench",
959
+ "beryl",
960
+ "birch",
961
+ "blade",
962
+ "bloom",
963
+ "bluer",
964
+ "border",
965
+ "brave",
966
+ "brick",
967
+ "brook",
968
+ "cabin",
969
+ "candle",
970
+ "carbon",
971
+ "cargo",
972
+ "cedar",
973
+ "chalk",
974
+ "charm",
975
+ "chrome",
976
+ "cipher",
977
+ "clam",
978
+ "clasp",
979
+ "cliff",
980
+ "clock",
981
+ "cobia",
982
+ "comet",
983
+ "coral",
984
+ "cotton",
985
+ "coves",
986
+ "crane",
987
+ "crest",
988
+ "crow",
989
+ "crystal",
990
+ "curio",
991
+ "dawn",
992
+ "delta",
993
+ "denim",
994
+ "depth",
995
+ "dewy",
996
+ "digger",
997
+ "docks",
998
+ "dover",
999
+ "drift",
1000
+ "dunes",
1001
+ "eagle",
1002
+ "ember",
1003
+ "echo",
1004
+ "eden",
1005
+ "elite",
1006
+ "ethic",
1007
+ "fable",
1008
+ "falcon",
1009
+ "fawn",
1010
+ "feather",
1011
+ "fern",
1012
+ "fjord",
1013
+ "flame",
1014
+ "flint",
1015
+ "forest",
1016
+ "forge",
1017
+ "frost",
1018
+ "garnet",
1019
+ "gemini",
1020
+ "glade",
1021
+ "glider",
1022
+ "glow",
1023
+ "granite",
1024
+ "grove",
1025
+ "guppy",
1026
+ "harbor",
1027
+ "haven",
1028
+ "hazel",
1029
+ "helio",
1030
+ "heron",
1031
+ "hickory",
1032
+ "honey",
1033
+ "horizon",
1034
+ "ivory",
1035
+ "jade",
1036
+ "jasper",
1037
+ "kestrel",
1038
+ "knot",
1039
+ "lagoon",
1040
+ "lattice",
1041
+ "laurel",
1042
+ "lavender",
1043
+ "lemon",
1044
+ "linden",
1045
+ "loon",
1046
+ "luger",
1047
+ "lumen",
1048
+ "lunar",
1049
+ "mango",
1050
+ "maple",
1051
+ "marble",
1052
+ "marsh",
1053
+ "meadow",
1054
+ "mercy",
1055
+ "mistle",
1056
+ "monsoon",
1057
+ "morning",
1058
+ "moss",
1059
+ "nacre",
1060
+ "nectar",
1061
+ "needle",
1062
+ "nimbus",
1063
+ "nova",
1064
+ "ocean",
1065
+ "onyx",
1066
+ "orbit",
1067
+ "otter",
1068
+ "palm",
1069
+ "panda",
1070
+ "pansy",
1071
+ "papaya",
1072
+ "passage",
1073
+ "pebble",
1074
+ "pelican",
1075
+ "pepper",
1076
+ "petal",
1077
+ "piano",
1078
+ "pierce",
1079
+ "pilot",
1080
+ "pioneer",
1081
+ "platinum",
1082
+ "plume",
1083
+ "poplar",
1084
+ "porpoise",
1085
+ "prairie",
1086
+ "prism",
1087
+ "pulsar",
1088
+ "quartz",
1089
+ "quasar",
1090
+ "quill",
1091
+ "quiver",
1092
+ "raven",
1093
+ "reef",
1094
+ "relic",
1095
+ "ridge",
1096
+ "ripple",
1097
+ "robin",
1098
+ "rocket",
1099
+ "rouge",
1100
+ "ruby",
1101
+ "saffron",
1102
+ "sage",
1103
+ "sail",
1104
+ "salmon",
1105
+ "sapphire",
1106
+ "scarab",
1107
+ "shadow",
1108
+ "shale",
1109
+ "sienna",
1110
+ "silica",
1111
+ "silver",
1112
+ "skyline",
1113
+ "slate",
1114
+ "sonar",
1115
+ "spruce",
1116
+ "starling",
1117
+ "stone",
1118
+ "sugar",
1119
+ "summit",
1120
+ "sunset",
1121
+ "swan",
1122
+ "tangent",
1123
+ "tarragon",
1124
+ "temple",
1125
+ "thistle",
1126
+ "thrush",
1127
+ "tiger",
1128
+ "topaz",
1129
+ "tundra",
1130
+ "turtle",
1131
+ "umber",
1132
+ "union",
1133
+ "valley",
1134
+ "vapor",
1135
+ "vector",
1136
+ "velvet",
1137
+ "violet",
1138
+ "vortex",
1139
+ "walnut",
1140
+ "whale",
1141
+ "winter",
1142
+ "wisp",
1143
+ "wisteria",
1144
+ "xenon",
1145
+ "yarrow",
1146
+ "zephyr",
1147
+ "zinc",
1148
+ "zodiac",
1149
+ "anchor",
1150
+ "basil",
1151
+ "cider",
1152
+ "daisy",
1153
+ "elfin",
1154
+ "ferry",
1155
+ "gimlet",
1156
+ "halcyon",
1157
+ "indigo",
1158
+ "juniper",
1159
+ "kindle",
1160
+ "lilac",
1161
+ "mantis",
1162
+ "nylon",
1163
+ "oracle",
1164
+ "parch",
1165
+ "quokka",
1166
+ "ramble",
1167
+ "thatch",
1168
+ "ultra",
1169
+ "vivid",
1170
+ "xylo",
1171
+ "yodel",
1172
+ "zesty",
1173
+ "arbor",
1174
+ "bliss",
1175
+ "calyx",
1176
+ "dwindle",
1177
+ "folio",
1178
+ "globe",
1179
+ "hymn",
1180
+ "ionic",
1181
+ "jolly",
1182
+ "knack",
1183
+ "lyric",
1184
+ "myrtle",
1185
+ "noble",
1186
+ "plumb",
1187
+ "quaint",
1188
+ "rustic",
1189
+ "satin",
1190
+ "timber",
1191
+ "urge",
1192
+ "vault",
1193
+ "whimsy",
1194
+ "yearn",
1195
+ "zenith",
1196
+ "ash",
1197
+ "beach",
1198
+ "dusk"
1199
+ ];
1200
+ function base64urlEncode(bytes) {
1201
+ let bin = "";
1202
+ for (const b of bytes) bin += String.fromCharCode(b);
1203
+ const b64 = typeof btoa !== "undefined" ? btoa(bin) : Buffer.from(bytes).toString("base64");
1204
+ return b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
1205
+ }
1206
+ function derToRs(der) {
1207
+ let i = 0;
1208
+ if (der[i++] !== 48) throw new Error("kit/webauthn: bad DER (no SEQUENCE)");
1209
+ if (der[i] & 128) i += 1 + (der[i] & 127);
1210
+ else i += 1;
1211
+ if (der[i++] !== 2) throw new Error("kit/webauthn: bad DER (no r INTEGER)");
1212
+ const rlen = der[i++];
1213
+ const r = bytesToBigInt(der.subarray(i, i + rlen));
1214
+ i += rlen;
1215
+ if (der[i++] !== 2) throw new Error("kit/webauthn: bad DER (no s INTEGER)");
1216
+ const slen = der[i++];
1217
+ const s = bytesToBigInt(der.subarray(i, i + slen));
1218
+ return { r, s };
1219
+ }
1220
+ function spkiToPublicKey(spki) {
1221
+ const idx = spki.lastIndexOf(4, spki.length - 65);
1222
+ const start = spki.length - 65;
1223
+ const prefix = spki[start];
1224
+ if (prefix !== 4) {
1225
+ if (idx < 0) throw new Error("kit/webauthn: no uncompressed EC point in SPKI");
1226
+ return { x: bytesToBigInt(spki.subarray(idx + 1, idx + 33)), y: bytesToBigInt(spki.subarray(idx + 33, idx + 65)) };
1227
+ }
1228
+ return {
1229
+ x: bytesToBigInt(spki.subarray(start + 1, start + 33)),
1230
+ y: bytesToBigInt(spki.subarray(start + 33, start + 65))
1231
+ };
1232
+ }
1233
+ function batchChallenge(leaves) {
1234
+ const total = leaves.reduce((n, l) => n + l.length, 0);
1235
+ const cat = new Uint8Array(total);
1236
+ let o = 0;
1237
+ for (const l of leaves) {
1238
+ cat.set(l, o);
1239
+ o += l.length;
1240
+ }
1241
+ return sha256(cat);
1242
+ }
1243
+ function webauthnDigest(authenticatorData, clientDataJSON) {
1244
+ const clientHash = sha256(clientDataJSON);
1245
+ const msg = new Uint8Array(authenticatorData.length + clientHash.length);
1246
+ msg.set(authenticatorData, 0);
1247
+ msg.set(clientHash, authenticatorData.length);
1248
+ return sha256(msg);
1249
+ }
1250
+ function recoverCandidatePublicKeys(r, s, digest) {
1251
+ const out = [];
1252
+ for (const bit of [0, 1]) {
1253
+ try {
1254
+ const point = new p256.Signature(r, s).addRecoveryBit(bit).recoverPublicKey(digest).toAffine();
1255
+ out.push({ publicKey: { x: point.x, y: point.y }, yParity: bit === 1 });
1256
+ } catch {
1257
+ }
1258
+ }
1259
+ return out;
1260
+ }
1261
+ function lowS(s) {
1262
+ const n = p256.CURVE.n;
1263
+ return s > n / 2n ? n - s : s;
1264
+ }
1265
+ function challengeOffsetOf(clientDataJSON, challengeB64) {
1266
+ const text = new TextDecoder().decode(clientDataJSON);
1267
+ const idx = text.indexOf(challengeB64);
1268
+ if (idx < 0) throw new Error("kit/webauthn: challenge not found in clientDataJSON");
1269
+ return idx;
1270
+ }
1271
+ var CavosSolana = class _CavosSolana {
1272
+ constructor(identity, address, status, connection, adapter, devicePubkey, relayer, feePayer) {
1273
+ this.identity = identity;
1274
+ this.address = address;
1275
+ this.status = status;
1276
+ this.connection = connection;
1277
+ this.adapter = adapter;
1278
+ this.devicePubkey = devicePubkey;
1279
+ this.relayer = relayer;
1280
+ this.feePayer = feePayer;
1281
+ /** Discriminant for the `CavosWallet` union — narrows `execute()` per chain. */
1282
+ this.chain = "solana";
1283
+ }
1284
+ get publicKey() {
1285
+ return this.devicePubkey;
1286
+ }
1287
+ static async connect(opts) {
1288
+ const identity = opts.identity ?? await opts.auth?.authenticate();
1289
+ if (!identity) throw new Error("kit/solana: connect requires `identity` or `auth`");
1290
+ if (opts.network === "solana-mainnet" && !opts.rpcUrl) {
1291
+ console.warn(
1292
+ "[cavos] Using the public mainnet-beta RPC. Pass `rpcUrl` with your own provider (Helius/Triton/QuickNode) for production \u2014 the public endpoint is rate-limited."
1293
+ );
1294
+ }
1295
+ const connection = new Connection(opts.rpcUrl ?? SOLANA_NETWORKS[opts.network], "confirmed");
1296
+ const signer = opts.createSigner ? await opts.createSigner(`${identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${identity.userId}:${opts.appSalt}` });
1297
+ const devicePubkey = await signer.getPublicKey();
1298
+ const adapter = new SolanaAdapter({ programId: opts.programId, connection, signer });
1299
+ const addressSeed = deriveAddressSeedSolana({ userId: identity.userId, appSalt: opts.appSalt });
1300
+ const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
1301
+ const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry);
1302
+ const relayer = opts.relayer ?? (opts.appId ? new SolanaRelayer({ baseUrl: backendUrl, appId: opts.appId, network: opts.network, connection }) : void 0);
1303
+ const existing = await registry.lookup(identity.userId);
1304
+ if (existing) {
1305
+ const isSigner2 = await adapter.isAuthorizedSigner(existing.address, devicePubkey);
1306
+ return new _CavosSolana(
1307
+ identity,
1308
+ existing.address,
1309
+ isSigner2 ? "ready" : "needs-device-approval",
1310
+ connection,
1311
+ adapter,
1312
+ devicePubkey,
1313
+ relayer,
1314
+ opts.feePayer
1315
+ );
1316
+ }
1317
+ const address = adapter.computeAddress(addressSeed, devicePubkey);
1318
+ const deployed = await connection.getAccountInfo(new PublicKey(address)) !== null;
1319
+ if (!deployed) {
1320
+ if (relayer) {
1321
+ const payer = await relayer.getFeePayer();
1322
+ const ix = adapter.buildInitialize(addressSeed, payer.toBase58(), devicePubkey);
1323
+ await relayer.send([ix]);
1324
+ } else if (opts.feePayer) {
1325
+ const ix = adapter.buildInitialize(addressSeed, opts.feePayer.publicKey.toBase58(), devicePubkey);
1326
+ await sendAndConfirmTransaction(connection, new Transaction().add(ix), [opts.feePayer]);
1327
+ } else {
1328
+ throw new Error("kit/solana: a relayer (appId) or feePayer is required to initialize a new account");
1329
+ }
1330
+ }
1331
+ await registry.register({ userId: identity.userId, address, initialSigner: devicePubkey });
1332
+ const isSigner = await adapter.isAuthorizedSigner(address, devicePubkey);
1333
+ return new _CavosSolana(
1334
+ identity,
1335
+ address,
1336
+ isSigner ? "ready" : "needs-device-approval",
1337
+ connection,
1338
+ adapter,
1339
+ devicePubkey,
1340
+ relayer,
1341
+ opts.feePayer
1342
+ );
1343
+ }
1344
+ /** Authorize an additional device signer (device-signed via precompile). */
1345
+ async addSigner(pubkey) {
1346
+ const ixs = await this.adapter.buildAddSigner(this.address, pubkey);
1347
+ return this.send(ixs);
1348
+ }
1349
+ /**
1350
+ * Enroll a passkey as an approver (2FA-style step-up). Device-signed + gasless;
1351
+ * requires a ready device. Idempotent. Returns the passkey pubkey + tx hash.
1352
+ */
1353
+ async enrollPasskey(passkey, params) {
1354
+ const enrolled = await passkey.enroll(params);
1355
+ const { transactionHash } = await this.addApprover(enrolled.publicKey);
1356
+ return { publicKey: enrolled.publicKey, transactionHash };
1357
+ }
1358
+ /** Register an already-enrolled passkey pubkey as an approver (gasless).
1359
+ * Idempotent. Lets one passkey be registered across chains without re-prompting. */
1360
+ async addApprover(pubkey) {
1361
+ if (this.status !== "ready") {
1362
+ throw new Error("kit/solana: addApprover requires a ready, authorized device");
1363
+ }
1364
+ if (await this.adapter.isApprover(this.address, pubkey)) return {};
1365
+ const ixs = await this.adapter.buildAddApprover(this.address, pubkey);
1366
+ const transactionHash = await this.send(ixs);
1367
+ return { transactionHash };
1368
+ }
1369
+ /**
1370
+ * From a fresh browser (status `needs-device-approval`), approve adding THIS
1371
+ * device with the user's synced passkey. Gasless via the relayer — the bundle
1372
+ * carries the passkey's WebAuthn assertion, so no device signature is needed.
1373
+ */
1374
+ async approveThisDeviceWithPasskey(passkey) {
1375
+ if (this.status === "ready") {
1376
+ throw new Error("kit/solana: this device is already an authorized signer");
1377
+ }
1378
+ const { leaf, nonce } = await this.passkeyLeafForThisDevice();
1379
+ const leaves = [leaf];
1380
+ const assertion = await passkey.assert(batchChallenge(leaves));
1381
+ const { transactionHash } = await this.submitPasskeyApproval(assertion, leaves, 0, nonce);
1382
+ return transactionHash;
1383
+ }
1384
+ /** This device's leaf + passkey nonce for a (possibly multi-chain) batch. */
1385
+ async passkeyLeafForThisDevice() {
1386
+ const nonce = await this.adapter.passkeyNonce(this.address);
1387
+ return { leaf: this.adapter.passkeyLeaf(this.devicePubkey, nonce), nonce };
1388
+ }
1389
+ /** Submit `add_signer_via_passkey` given a shared assertion + batch position.
1390
+ * Used by `approveThisDeviceWithPasskey` and `approveDeviceEverywhere`. */
1391
+ async submitPasskeyApproval(assertion, leaves, leafIndex, _nonce) {
1392
+ const digest = webauthnDigest(assertion.authenticatorData, assertion.clientDataJSON);
1393
+ const candidates = recoverCandidatePublicKeys(assertion.r, assertion.s, digest);
1394
+ let approver = null;
1395
+ for (const cand of candidates) {
1396
+ if (await this.adapter.isApprover(this.address, cand.publicKey)) {
1397
+ approver = cand.publicKey;
1398
+ break;
1399
+ }
1400
+ }
1401
+ if (!approver) throw new Error("kit/solana: this passkey is not a registered approver");
1402
+ const ixs = this.adapter.buildAddSignerViaPasskey(
1403
+ this.address,
1404
+ this.devicePubkey,
1405
+ approver,
1406
+ leaves,
1407
+ leafIndex,
1408
+ assertion
1409
+ );
1410
+ return { transactionHash: await this.send(ixs) };
1411
+ }
1412
+ /** Move `amount` lamports out of the account to `destination` (device-signed). */
1413
+ async execute(amount, destination) {
1414
+ if (this.status !== "ready") {
1415
+ throw new Error("kit/solana: this device is not yet an authorized signer of the wallet");
1416
+ }
1417
+ const ixs = await this.adapter.buildExecuteTransfer(this.address, destination, amount);
1418
+ return this.send(ixs);
1419
+ }
1420
+ /**
1421
+ * Run arbitrary CPI `instructions` with the account PDA as signer (device-
1422
+ * signed). The signature commits to sha256 of the canonical Borsh
1423
+ * serialization of the instructions, so it binds exactly the operations the
1424
+ * program will invoke. Unlocks SPL transfers, swaps, staking, etc.
1425
+ *
1426
+ * What the relayer will sponsor is constrained by the app's Solana program
1427
+ * allowlist (configured in the dashboard) — programs outside the allowlist are
1428
+ * rejected before co-signing.
1429
+ */
1430
+ async executeInstructions(instructions) {
1431
+ if (this.status !== "ready") {
1432
+ throw new Error("kit/solana: this device is not yet an authorized signer of the wallet");
1433
+ }
1434
+ const ixs = await this.adapter.buildExecute(this.address, instructions);
1435
+ return this.send(ixs);
1436
+ }
1437
+ /**
1438
+ * Register the backup signer derived from `code` as an authorized signer of this
1439
+ * account (device-signed via precompile). Idempotent: returns without a tx if
1440
+ * the backup signer is already registered. The code never leaves the device —
1441
+ * only the derived public key travels on-chain.
1442
+ *
1443
+ * Self-custodial: anyone who can re-derive the backup key from the code (i.e.
1444
+ * the rightful owner) can later recover the account with `CavosSolana.recover`.
1445
+ * Run this once, on a registered device, and have the user store the code.
1446
+ */
1447
+ async setupRecovery(code) {
1448
+ if (this.status !== "ready") {
1449
+ throw new Error("kit/solana: setupRecovery requires a ready, registered device");
1450
+ }
1451
+ const { publicKey: backupPubkey } = deriveBackupKey(code);
1452
+ const already = await this.adapter.isAuthorizedSigner(this.address, backupPubkey);
1453
+ if (already) return void 0;
1454
+ return this.addSigner(backupPubkey);
1455
+ }
1456
+ /**
1457
+ * Recover an account after losing every device signer. Derives the backup key
1458
+ * from `code`, uses it (not the new device key) to sign an `add_signer` for the
1459
+ * new device, and returns a ready CavosSolana bound to the new device. The
1460
+ * account address is unchanged.
1461
+ *
1462
+ * Self-custodial: only someone holding the code (i.e. the rightful owner) can
1463
+ * re-derive the backup key. The backend never sees the code.
1464
+ *
1465
+ * This mirrors `Cavos.recover` (Starknet): the backup key is just another
1466
+ * authorized signer, so recovery is an `add_signer(newDevice)` bundle signed by
1467
+ * the backup key. The on-chain program needs no recovery-specific entrypoint.
1468
+ */
1469
+ static async recover(opts) {
1470
+ if (opts.network === "solana-mainnet" && !opts.rpcUrl) {
1471
+ console.warn(
1472
+ "[cavos] Using the public mainnet-beta RPC. Pass `rpcUrl` with your own provider (Helius/Triton/QuickNode) for production \u2014 the public endpoint is rate-limited."
1473
+ );
1474
+ }
1475
+ const connection = new Connection(opts.rpcUrl ?? SOLANA_NETWORKS[opts.network], "confirmed");
1476
+ const signer = opts.createSigner ? await opts.createSigner(`${opts.identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${opts.identity.userId}:${opts.appSalt}` });
1477
+ const devicePubkey = await signer.getPublicKey();
1478
+ const backup = BackupSigner.fromCode(opts.code);
1479
+ const backupAdapter = new SolanaAdapter({
1480
+ programId: opts.programId,
1481
+ connection,
1482
+ signer: backup
1483
+ });
1484
+ const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
1485
+ const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry);
1486
+ const existing = await registry.lookup(opts.identity.userId);
1487
+ if (!existing) {
1488
+ throw new Error("kit/solana: no account found for this identity \u2014 nothing to recover");
1489
+ }
1490
+ const relayer = opts.relayer ?? (opts.appId ? new SolanaRelayer({ baseUrl: backendUrl, appId: opts.appId, network: opts.network, connection }) : void 0);
1491
+ const alreadyAuthed = await backupAdapter.isAuthorizedSigner(existing.address, devicePubkey);
1492
+ if (!alreadyAuthed) {
1493
+ const ixs = await backupAdapter.buildAddSigner(existing.address, devicePubkey);
1494
+ if (relayer) {
1495
+ await relayer.send(ixs);
1496
+ } else if (opts.feePayer) {
1497
+ await sendAndConfirmTransaction(connection, new Transaction().add(...ixs), [opts.feePayer]);
1498
+ } else {
1499
+ throw new Error("kit/solana: a relayer (appId) or feePayer is required to recover");
1500
+ }
1501
+ }
1502
+ const adapter = new SolanaAdapter({ programId: opts.programId, connection, signer });
1503
+ return new _CavosSolana(
1504
+ opts.identity,
1505
+ existing.address,
1506
+ "ready",
1507
+ connection,
1508
+ adapter,
1509
+ devicePubkey,
1510
+ relayer,
1511
+ opts.feePayer
1512
+ );
1513
+ }
1514
+ async send(ixs) {
1515
+ if (this.relayer) return this.relayer.send(ixs);
1516
+ if (this.feePayer) {
1517
+ return sendAndConfirmTransaction(this.connection, new Transaction().add(...ixs), [this.feePayer]);
1518
+ }
1519
+ throw new Error("kit/solana: no relayer or feePayer configured to submit transactions");
1520
+ }
1521
+ };
1522
+ var defaultRegistry = new InMemoryWalletRegistry();
1523
+
1524
+ // src/chains/stellar/constants.ts
1525
+ var FACTORY_CONTRACT_ID = {
1526
+ // Re-deployed 2026-07-01 with the passkey-approval device-account wasm (batched
1527
+ // multi-chain challenge). The factory pins the wasm hash immutably, so a new
1528
+ // wasm needs a new factory → new account addresses; testnet has no prod wallets.
1529
+ "stellar-testnet": "CBCJIODXIEBOXXD66KCUCF7ZDYJARKI4ZIVQOVWPULOBH5XGNCDP6W3I",
1530
+ // Set once the factory is deployed to mainnet (its address differs — network id
1531
+ // is part of contract-address derivation).
1532
+ "stellar-mainnet": ""
1533
+ };
1534
+ var DEVICE_ACCOUNT_WASM_HASH = {
1535
+ "stellar-testnet": "2671b085578e59a385ef5a5664e42f0450322fe3249539f588e1263ed5a31dce",
1536
+ "stellar-mainnet": ""
1537
+ };
1538
+ var STELLAR_NETWORKS = {
1539
+ "stellar-testnet": {
1540
+ rpcUrl: "https://soroban-testnet.stellar.org",
1541
+ passphrase: "Test SDF Network ; September 2015"
1542
+ },
1543
+ "stellar-mainnet": {
1544
+ rpcUrl: "https://soroban-rpc.mainnet.stellar.gateway.fm",
1545
+ passphrase: "Public Global Stellar Network ; September 2015"
1546
+ }
1547
+ };
1548
+ var NATIVE_SAC_ID = {
1549
+ "stellar-testnet": "CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC",
1550
+ "stellar-mainnet": "CAS3J7GYLGXMF6TDJBBYYSE3HQ6BBSMLNUQ34T6TZMYMW2EVH34XOWMA"
1551
+ };
1552
+ var SECP256R1_N2 = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n;
1553
+ var StellarAdapter = class {
1554
+ constructor(opts) {
1555
+ this.chain = "stellar";
1556
+ this.network = opts.network;
1557
+ this.passphrase = STELLAR_NETWORKS[opts.network].passphrase;
1558
+ this.rpcUrl = opts.rpcUrl ?? STELLAR_NETWORKS[opts.network].rpcUrl;
1559
+ this.factoryId = opts.factoryId ?? FACTORY_CONTRACT_ID[opts.network];
1560
+ if (!this.factoryId) {
1561
+ throw new Error(`kit/stellar: no factory contract id configured for ${opts.network}`);
1562
+ }
1563
+ this.signer = opts.signer;
1564
+ }
1565
+ server() {
1566
+ if (!this._server) {
1567
+ this._server = new rpc.Server(this.rpcUrl, {
1568
+ allowHttp: this.rpcUrl.startsWith("http://")
1569
+ });
1570
+ }
1571
+ return this._server;
1572
+ }
1573
+ networkId() {
1574
+ return hash$1(Buffer.from(this.passphrase));
1575
+ }
1576
+ /**
1577
+ * Deterministic account address for `(addressSeed, initialSigner)` — computed
1578
+ * off-chain, byte-identical to the factory's on-chain `account_address`.
1579
+ * `contractId = sha256(HashIdPreimage(networkId, factory, salt))` with
1580
+ * `salt = sha256(addressSeed || sec1(initialSigner))`.
1581
+ */
1582
+ computeAddress(addressSeed, initialSigner) {
1583
+ const salt = this.accountSalt(addressSeed, initialSigner);
1584
+ const preimage = xdr.HashIdPreimage.envelopeTypeContractId(
1585
+ new xdr.HashIdPreimageContractId({
1586
+ networkId: this.networkId(),
1587
+ contractIdPreimage: xdr.ContractIdPreimage.contractIdPreimageFromAddress(
1588
+ new xdr.ContractIdPreimageFromAddress({
1589
+ address: new Address(this.factoryId).toScAddress(),
1590
+ salt
1591
+ })
1592
+ )
1593
+ })
1594
+ );
1595
+ return StrKey.encodeContract(hash$1(preimage.toXDR()));
1596
+ }
1597
+ /** `salt = sha256(addressSeed(32) || sec1(initialSigner)(65))` — matches the factory. */
1598
+ accountSalt(addressSeed, initialSigner) {
1599
+ return hash$1(Buffer.concat([Buffer.from(addressSeed), Buffer.from(sec1Pubkey(initialSigner))]));
1600
+ }
1601
+ /** Host function: `factory.deploy(address_seed, initial_signer)`. */
1602
+ buildDeploy(addressSeed, initialSigner) {
1603
+ return invokeFunc(this.factoryId, "deploy", [
1604
+ bytesScVal(addressSeed),
1605
+ bytesScVal(sec1Pubkey(initialSigner))
1606
+ ]);
1607
+ }
1608
+ /** Host function: `account.add_signer(new_signer)` (requires device auth). */
1609
+ buildAddSigner(accountAddress, signer) {
1610
+ return invokeFunc(accountAddress, "add_signer", [bytesScVal(sec1Pubkey(signer))]);
1611
+ }
1612
+ /** Host function: `account.remove_signer(signer)` (requires device auth). */
1613
+ buildRemoveSigner(accountAddress, signer) {
1614
+ return invokeFunc(accountAddress, "remove_signer", [bytesScVal(sec1Pubkey(signer))]);
1615
+ }
1616
+ /** Host function: `account.add_approver(passkey)` (requires device auth). */
1617
+ buildAddApprover(accountAddress, passkey) {
1618
+ return invokeFunc(accountAddress, "add_approver", [bytesScVal(sec1Pubkey(passkey))]);
1619
+ }
1620
+ /** Host function: `account.remove_approver(passkey)` (requires device auth). */
1621
+ buildRemoveApprover(accountAddress, passkey) {
1622
+ return invokeFunc(accountAddress, "remove_approver", [bytesScVal(sec1Pubkey(passkey))]);
1623
+ }
1624
+ /** This chain's leaf for approving `add_signer(newSigner)` at `nonce`:
1625
+ * `sha256(sec1(new_signer) || nonce_be8)`. The batch challenge the passkey signs
1626
+ * is `sha256(concat(leaves))` across chains. */
1627
+ passkeyLeaf(newSigner, nonce) {
1628
+ const msg = new Uint8Array(65 + 8);
1629
+ msg.set(sec1Pubkey(newSigner), 0);
1630
+ const n = new Uint8Array(8);
1631
+ let v = nonce;
1632
+ for (let i = 7; i >= 0; i--) {
1633
+ n[i] = Number(v & 0xffn);
1634
+ v >>= 8n;
1635
+ }
1636
+ msg.set(n, 65);
1637
+ return sha256(msg);
1638
+ }
1639
+ /** Host function: passkey-authorized `add_signer_via_passkey` (no device auth —
1640
+ * authorized by the embedded WebAuthn assertion, so any relayer can submit).
1641
+ * `leaves`/`leafIndex` place this chain's leaf in the multi-chain batch. */
1642
+ buildAddSignerViaPasskey(accountAddress, newSigner, passkey, nonce, leaves, leafIndex, assertion) {
1643
+ const sig = encodeLowSSignature2({ r: assertion.r, s: assertion.s});
1644
+ const leavesScVal = xdr.ScVal.scvVec(leaves.map((l) => bytesScVal(l)));
1645
+ return invokeFunc(accountAddress, "add_signer_via_passkey", [
1646
+ bytesScVal(sec1Pubkey(newSigner)),
1647
+ bytesScVal(sec1Pubkey(passkey)),
1648
+ nativeToScVal(nonce, { type: "u64" }),
1649
+ leavesScVal,
1650
+ nativeToScVal(leafIndex, { type: "u32" }),
1651
+ bytesScVal(assertion.authenticatorData),
1652
+ bytesScVal(assertion.clientDataJSON),
1653
+ nativeToScVal(assertion.challengeOffset, { type: "u32" }),
1654
+ bytesScVal(sig)
1655
+ ]);
1656
+ }
1657
+ /** Read whether `passkey` is a registered approver (read-only simulation). */
1658
+ async isApprover(accountAddress, passkey, readSource) {
1659
+ if (!await this.isDeployed(accountAddress)) return false;
1660
+ const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
1661
+ const src = new Account3(readSource, "0");
1662
+ const op = Operation.invokeHostFunction({
1663
+ func: invokeFunc(accountAddress, "is_approver", [bytesScVal(sec1Pubkey(passkey))]),
1664
+ auth: []
1665
+ });
1666
+ const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
1667
+ const sim = await this.server().simulateTransaction(tx2);
1668
+ if (rpc.Api.isSimulationError(sim)) {
1669
+ throw new Error(`kit/stellar: is_approver simulation failed: ${sim.error}`);
1670
+ }
1671
+ if (!sim.result?.retval) return false;
1672
+ return scValToNative(sim.result.retval) === true;
1673
+ }
1674
+ /** Read the current passkey-approval nonce (read-only simulation). */
1675
+ async passkeyNonce(accountAddress, readSource) {
1676
+ if (!await this.isDeployed(accountAddress)) return 0n;
1677
+ const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
1678
+ const src = new Account3(readSource, "0");
1679
+ const op = Operation.invokeHostFunction({
1680
+ func: invokeFunc(accountAddress, "passkey_nonce", []),
1681
+ auth: []
1682
+ });
1683
+ const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
1684
+ const sim = await this.server().simulateTransaction(tx2);
1685
+ if (rpc.Api.isSimulationError(sim)) {
1686
+ throw new Error(`kit/stellar: passkey_nonce simulation failed: ${sim.error}`);
1687
+ }
1688
+ if (!sim.result?.retval) return 0n;
1689
+ return BigInt(scValToNative(sim.result.retval));
1690
+ }
1691
+ /** Host function: SEP-41 `token.transfer(from=account, to, amount)` (device auth). */
1692
+ buildTransfer(tokenId, accountAddress, destination, amount) {
1693
+ return invokeFunc(tokenId, "transfer", [
1694
+ new Address(accountAddress).toScVal(),
1695
+ new Address(destination).toScVal(),
1696
+ nativeToScVal(amount, { type: "i128" })
1697
+ ]);
1698
+ }
1699
+ /**
1700
+ * Sign a Soroban authorization entry with the silent device key, producing the
1701
+ * `Vec<DeviceSignature>` the account's `__check_auth` verifies. The device
1702
+ * signs `sha256(preimage)` (WebCrypto hashes once more internally), which is
1703
+ * exactly what the contract recomputes. Mutates + returns the entry.
1704
+ */
1705
+ async signAuthEntry(entry, validUntilLedger) {
1706
+ const addrCreds = entry.credentials().address();
1707
+ addrCreds.signatureExpirationLedger(validUntilLedger);
1708
+ const preimage = xdr.HashIdPreimage.envelopeTypeSorobanAuthorization(
1709
+ new xdr.HashIdPreimageSorobanAuthorization({
1710
+ networkId: this.networkId(),
1711
+ nonce: addrCreds.nonce(),
1712
+ signatureExpirationLedger: validUntilLedger,
1713
+ invocation: entry.rootInvocation()
1714
+ })
1715
+ );
1716
+ const payload = hash$1(preimage.toXDR());
1717
+ const sig = await this.signer.sign(new Uint8Array(payload));
1718
+ const pubkey = await this.signer.getPublicKey();
1719
+ addrCreds.signature(deviceSignatureScVal(pubkey, sig));
1720
+ return entry;
1721
+ }
1722
+ /**
1723
+ * Read a SEP-41 token balance of `account` via a read-only simulation of
1724
+ * `token.balance(account)`. Returns 0 when the account isn't deployed or holds
1725
+ * none. `readSource` is any funded G-account (used only for the simulation).
1726
+ */
1727
+ async readBalance(tokenId, account, readSource) {
1728
+ if (!await this.isDeployed(account)) return 0n;
1729
+ const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
1730
+ const src = new Account3(readSource, "0");
1731
+ const op = Operation.invokeHostFunction({
1732
+ func: invokeFunc(tokenId, "balance", [new Address(account).toScVal()]),
1733
+ auth: []
1734
+ });
1735
+ const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
1736
+ const sim = await this.server().simulateTransaction(tx2);
1737
+ if (rpc.Api.isSimulationError(sim) || !sim.result?.retval) return 0n;
1738
+ return BigInt(scValToNative(sim.result.retval));
1739
+ }
1740
+ /** Whether the account contract instance exists on-chain (is deployed). */
1741
+ async isDeployed(accountAddress) {
1742
+ try {
1743
+ const res = await this.server().getContractData(
1744
+ accountAddress,
1745
+ xdr.ScVal.scvLedgerKeyContractInstance(),
1746
+ rpc.Durability.Persistent
1747
+ );
1748
+ return !!res;
1749
+ } catch {
1750
+ return false;
1751
+ }
1752
+ }
1753
+ /**
1754
+ * Read whether `signer` is a currently-authorized signer of the account, via a
1755
+ * read-only simulation of `account.is_authorized(signer)`. `readSource` is any
1756
+ * funded G-account (used only for the simulation's source/sequence).
1757
+ */
1758
+ async isAuthorizedSigner(accountAddress, signer, readSource) {
1759
+ if (!await this.isDeployed(accountAddress)) return false;
1760
+ const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
1761
+ const src = new Account3(readSource, "0");
1762
+ const op = Operation.invokeHostFunction({
1763
+ func: invokeFunc(accountAddress, "is_authorized", [bytesScVal(sec1Pubkey(signer))]),
1764
+ auth: []
1765
+ });
1766
+ const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
1767
+ const sim = await this.server().simulateTransaction(tx2);
1768
+ if (rpc.Api.isSimulationError(sim)) {
1769
+ throw new Error(`kit/stellar: is_authorized simulation failed: ${sim.error}`);
1770
+ }
1771
+ if (!sim.result?.retval) return false;
1772
+ return scValToNative(sim.result.retval) === true;
1773
+ }
1774
+ };
1775
+ function sec1Pubkey(pk) {
1776
+ const out = new Uint8Array(65);
1777
+ out[0] = 4;
1778
+ out.set(bigIntTo32Bytes(pk.x), 1);
1779
+ out.set(bigIntTo32Bytes(pk.y), 33);
1780
+ return out;
1781
+ }
1782
+ function encodeLowSSignature2(sig) {
1783
+ const lowS2 = sig.s > SECP256R1_N2 / 2n ? SECP256R1_N2 - sig.s : sig.s;
1784
+ const out = new Uint8Array(64);
1785
+ out.set(bigIntTo32Bytes(sig.r), 0);
1786
+ out.set(bigIntTo32Bytes(lowS2), 32);
1787
+ return out;
1788
+ }
1789
+ function deviceSignatureScVal(pubkey, sig) {
1790
+ const element = nativeToScVal(
1791
+ {
1792
+ public_key: Buffer.from(sec1Pubkey(pubkey)),
1793
+ signature: Buffer.from(encodeLowSSignature2(sig))
1794
+ },
1795
+ { type: { public_key: ["symbol", "bytes"], signature: ["symbol", "bytes"] } }
1796
+ );
1797
+ return xdr.ScVal.scvVec([element]);
1798
+ }
1799
+ function invokeFunc(contractId, method, args) {
1800
+ return xdr.HostFunction.hostFunctionTypeInvokeContract(
1801
+ new xdr.InvokeContractArgs({
1802
+ contractAddress: new Address(contractId).toScAddress(),
1803
+ functionName: method,
1804
+ args
1805
+ })
1806
+ );
1807
+ }
1808
+ function bytesScVal(bytes) {
1809
+ return xdr.ScVal.scvBytes(Buffer.from(bytes));
1810
+ }
1811
+
1812
+ // src/chains/stellar/StellarRelayer.ts
1813
+ var StellarRelayer = class {
1814
+ constructor(opts) {
1815
+ this.opts = opts;
1816
+ }
1817
+ /** The relayer's source/fee-payer G-account (fetched + cached from the backend). */
1818
+ async getSource() {
1819
+ if (this.source) return this.source;
1820
+ const res = await fetch(`${this.opts.baseUrl}/api/stellar/relay?network=${this.opts.network}`);
1821
+ if (!res.ok) throw new Error(`kit/stellar: relayer source lookup failed (${res.status})`);
1822
+ const { fee_payer } = await res.json();
1823
+ this.source = fee_payer;
1824
+ return this.source;
1825
+ }
1826
+ /**
1827
+ * POST the assembled, device-authorized transaction XDR to the relayer to sign
1828
+ * the envelope + submit. Returns the confirmed transaction hash.
1829
+ */
1830
+ async submit(transactionXdr) {
1831
+ const res = await fetch(`${this.opts.baseUrl}/api/stellar/relay`, {
1832
+ method: "POST",
1833
+ headers: { "Content-Type": "application/json" },
1834
+ body: JSON.stringify({
1835
+ app_id: this.opts.appId,
1836
+ network: this.opts.network,
1837
+ transaction: transactionXdr
1838
+ })
1839
+ });
1840
+ if (!res.ok) {
1841
+ const detail = await res.text().catch(() => "");
1842
+ throw new Error(`kit/stellar: relay failed (${res.status}) ${detail}`);
1843
+ }
1844
+ const { hash: hash6 } = await res.json();
1845
+ return hash6;
1846
+ }
1847
+ };
1848
+ var CavosStellar = class _CavosStellar {
1849
+ constructor(identity, address, status, network, adapter, devicePubkey, relayer, sourceKeypair) {
1850
+ this.identity = identity;
1851
+ this.address = address;
1852
+ this.status = status;
1853
+ this.network = network;
1854
+ this.adapter = adapter;
1855
+ this.devicePubkey = devicePubkey;
1856
+ this.relayer = relayer;
1857
+ this.sourceKeypair = sourceKeypair;
1858
+ /** Discriminant for the `CavosWallet` union — narrows `execute()` per chain. */
1859
+ this.chain = "stellar";
1860
+ }
1861
+ get publicKey() {
1862
+ return this.devicePubkey;
1863
+ }
1864
+ static async connect(opts) {
1865
+ const identity = opts.identity ?? await opts.auth?.authenticate();
1866
+ if (!identity) throw new Error("kit/stellar: connect requires `identity` or `auth`");
1867
+ const signer = opts.createSigner ? await opts.createSigner(`${identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${identity.userId}:${opts.appSalt}` });
1868
+ const devicePubkey = await signer.getPublicKey();
1869
+ const adapter = new StellarAdapter({
1870
+ network: opts.network,
1871
+ rpcUrl: opts.rpcUrl,
1872
+ factoryId: opts.factoryId,
1873
+ signer
1874
+ });
1875
+ const addressSeed = deriveAddressSeedStellar({ userId: identity.userId, appSalt: opts.appSalt });
1876
+ const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
1877
+ const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry2);
1878
+ const relayer = opts.relayer ?? (opts.appId ? new StellarRelayer({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : void 0);
1879
+ const build = (address2, status) => new _CavosStellar(identity, address2, status, opts.network, adapter, devicePubkey, relayer, opts.sourceKeypair);
1880
+ const self = build("", "needs-device-approval");
1881
+ const readSource = await self.resolveSource();
1882
+ const existing = await registry.lookup(identity.userId);
1883
+ if (existing) {
1884
+ const isSigner2 = await adapter.isAuthorizedSigner(existing.address, devicePubkey, readSource);
1885
+ return build(existing.address, isSigner2 ? "ready" : "needs-device-approval");
1886
+ }
1887
+ const address = adapter.computeAddress(addressSeed, devicePubkey);
1888
+ if (!await adapter.isDeployed(address)) {
1889
+ const func = adapter.buildDeploy(addressSeed, devicePubkey);
1890
+ await self.submitHostFunction(func, void 0);
1891
+ }
1892
+ await registry.register({ userId: identity.userId, address, initialSigner: devicePubkey });
1893
+ const isSigner = await adapter.isAuthorizedSigner(address, devicePubkey, readSource);
1894
+ return build(address, isSigner ? "ready" : "needs-device-approval");
1895
+ }
1896
+ /** Authorize an additional device signer (device-signed via `__check_auth`). */
1897
+ async addSigner(pubkey) {
1898
+ const func = this.adapter.buildAddSigner(this.address, pubkey);
1899
+ return this.submitHostFunction(func, this.address);
1900
+ }
1901
+ /**
1902
+ * Enroll a passkey as an approver (2FA-style step-up). Device-signed + gasless;
1903
+ * requires a ready device. Idempotent. Returns the passkey pubkey + tx hash.
1904
+ */
1905
+ async enrollPasskey(passkey, params) {
1906
+ const enrolled = await passkey.enroll(params);
1907
+ const { transactionHash } = await this.addApprover(enrolled.publicKey);
1908
+ return { publicKey: enrolled.publicKey, transactionHash };
1909
+ }
1910
+ /** Register an already-enrolled passkey pubkey as an approver (gasless).
1911
+ * Idempotent. Lets one passkey be registered across chains without re-prompting. */
1912
+ async addApprover(pubkey) {
1913
+ if (this.status !== "ready") {
1914
+ throw new Error("kit/stellar: addApprover requires a ready, authorized device");
1915
+ }
1916
+ const readSource = await this.resolveSource();
1917
+ if (await this.adapter.isApprover(this.address, pubkey, readSource)) return {};
1918
+ const func = this.adapter.buildAddApprover(this.address, pubkey);
1919
+ const transactionHash = await this.submitHostFunction(func, this.address);
1920
+ return { transactionHash };
1921
+ }
1922
+ /**
1923
+ * From a fresh browser (status `needs-device-approval`), approve adding THIS
1924
+ * device using the user's synced passkey. Gasless via the relayer — the call
1925
+ * carries the WebAuthn assertion, so no device signature is needed. Returns the
1926
+ * tx hash. No trip back to an already-authorized device.
1927
+ */
1928
+ async approveThisDeviceWithPasskey(passkey) {
1929
+ if (this.status === "ready") {
1930
+ throw new Error("kit/stellar: this device is already an authorized signer");
1931
+ }
1932
+ const { leaf, nonce } = await this.passkeyLeafForThisDevice();
1933
+ const leaves = [leaf];
1934
+ const assertion = await passkey.assert(batchChallenge(leaves));
1935
+ const { transactionHash } = await this.submitPasskeyApproval(assertion, leaves, 0, nonce);
1936
+ return transactionHash;
1937
+ }
1938
+ /** This device's leaf + passkey nonce for a (possibly multi-chain) batch. */
1939
+ async passkeyLeafForThisDevice() {
1940
+ const readSource = await this.resolveSource();
1941
+ const nonce = await this.adapter.passkeyNonce(this.address, readSource);
1942
+ return { leaf: this.adapter.passkeyLeaf(this.devicePubkey, nonce), nonce };
1943
+ }
1944
+ /** Submit `add_signer_via_passkey` given a shared assertion + batch position.
1945
+ * No device auth entry — authorized purely by the passkey assertion. */
1946
+ async submitPasskeyApproval(assertion, leaves, leafIndex, nonce) {
1947
+ const readSource = await this.resolveSource();
1948
+ const digest = webauthnDigest(assertion.authenticatorData, assertion.clientDataJSON);
1949
+ const candidates = recoverCandidatePublicKeys(assertion.r, assertion.s, digest);
1950
+ let approver = null;
1951
+ for (const cand of candidates) {
1952
+ if (await this.adapter.isApprover(this.address, cand.publicKey, readSource)) {
1953
+ approver = cand.publicKey;
1954
+ break;
1955
+ }
1956
+ }
1957
+ if (!approver) throw new Error("kit/stellar: this passkey is not a registered approver");
1958
+ const func = this.adapter.buildAddSignerViaPasskey(
1959
+ this.address,
1960
+ this.devicePubkey,
1961
+ approver,
1962
+ nonce,
1963
+ leaves,
1964
+ leafIndex,
1965
+ assertion
1966
+ );
1967
+ return { transactionHash: await this.submitHostFunction(func, void 0) };
1968
+ }
1969
+ /** Move `amount` stroops of native XLM to `destination` (device-signed). */
1970
+ async execute(amount, destination) {
1971
+ return this.executeTransfer(NATIVE_SAC_ID[this.network], amount, destination);
1972
+ }
1973
+ /** Read this account's balance of `tokenId` (defaults to native XLM), in stroops. */
1974
+ async balance(tokenId = NATIVE_SAC_ID[this.network]) {
1975
+ const readSource = await this.resolveSource();
1976
+ return this.adapter.readBalance(tokenId, this.address, readSource);
1977
+ }
1978
+ /** Transfer `amount` of any SEP-41 token out of the account (device-signed). */
1979
+ async executeTransfer(tokenId, amount, destination) {
1980
+ if (this.status !== "ready") {
1981
+ throw new Error("kit/stellar: this device is not yet an authorized signer of the wallet");
1982
+ }
1983
+ const func = this.adapter.buildTransfer(tokenId, this.address, destination, amount);
1984
+ return this.submitHostFunction(func, this.address);
1985
+ }
1986
+ /**
1987
+ * Register the backup signer derived from `code` as an authorized signer of
1988
+ * this account (device-signed). Idempotent. The code never leaves the device —
1989
+ * only the derived public key travels on-chain. Mirrors the other chains.
1990
+ */
1991
+ async setupRecovery(code) {
1992
+ if (this.status !== "ready") {
1993
+ throw new Error("kit/stellar: setupRecovery requires a ready, registered device");
1994
+ }
1995
+ const { publicKey: backupPubkey } = deriveBackupKey(code);
1996
+ const readSource = await this.resolveSource();
1997
+ if (await this.adapter.isAuthorizedSigner(this.address, backupPubkey, readSource)) return void 0;
1998
+ return this.addSigner(backupPubkey);
1999
+ }
2000
+ /**
2001
+ * Recover an account after losing every device signer: derive the backup key
2002
+ * from `code`, use it (not the new device) to authorize `add_signer(newDevice)`,
2003
+ * and return a ready handle bound to the new device. The address is unchanged.
2004
+ */
2005
+ static async recover(opts) {
2006
+ const signer = opts.createSigner ? await opts.createSigner(`${opts.identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${opts.identity.userId}:${opts.appSalt}` });
2007
+ const devicePubkey = await signer.getPublicKey();
2008
+ const backup = BackupSigner.fromCode(opts.code);
2009
+ const backupAdapter = new StellarAdapter({
2010
+ network: opts.network,
2011
+ rpcUrl: opts.rpcUrl,
2012
+ factoryId: opts.factoryId,
2013
+ signer: backup
2014
+ });
2015
+ const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
2016
+ const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry2);
2017
+ const existing = await registry.lookup(opts.identity.userId);
2018
+ if (!existing) {
2019
+ throw new Error("kit/stellar: no account found for this identity \u2014 nothing to recover");
2020
+ }
2021
+ const relayer = opts.relayer ?? (opts.appId ? new StellarRelayer({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : void 0);
2022
+ const backupHandle = new _CavosStellar(
2023
+ opts.identity,
2024
+ existing.address,
2025
+ "ready",
2026
+ opts.network,
2027
+ backupAdapter,
2028
+ devicePubkey,
2029
+ relayer,
2030
+ opts.sourceKeypair
2031
+ );
2032
+ const readSource = await backupHandle.resolveSource();
2033
+ if (!await backupAdapter.isAuthorizedSigner(existing.address, devicePubkey, readSource)) {
2034
+ await backupHandle.addSigner(devicePubkey);
2035
+ }
2036
+ const adapter = new StellarAdapter({
2037
+ network: opts.network,
2038
+ rpcUrl: opts.rpcUrl,
2039
+ factoryId: opts.factoryId,
2040
+ signer
2041
+ });
2042
+ return new _CavosStellar(
2043
+ opts.identity,
2044
+ existing.address,
2045
+ "ready",
2046
+ opts.network,
2047
+ adapter,
2048
+ devicePubkey,
2049
+ relayer,
2050
+ opts.sourceKeypair
2051
+ );
2052
+ }
2053
+ /** The transaction source/fee-payer G-address (relayer or self-funded). */
2054
+ async resolveSource() {
2055
+ if (this.relayer) return this.relayer.getSource();
2056
+ if (this.sourceKeypair) return this.sourceKeypair.publicKey();
2057
+ throw new Error("kit/stellar: a relayer (appId) or sourceKeypair is required");
2058
+ }
2059
+ /**
2060
+ * Build → simulate → device-sign auth → assemble → submit an invoke-contract
2061
+ * host function. `authAccount` is the account whose `__check_auth` must sign the
2062
+ * operation's Soroban auth entry (undefined for a plain factory deploy).
2063
+ */
2064
+ async submitHostFunction(func, authAccount) {
2065
+ const server = this.adapter.server();
2066
+ const sourceAddr = await this.resolveSource();
2067
+ const simSource = new Account(sourceAddr, "0");
2068
+ const unsignedOp = Operation.invokeHostFunction({ func, auth: [] });
2069
+ const simTx = new TransactionBuilder(simSource, {
2070
+ fee: BASE_FEE,
2071
+ networkPassphrase: this.adapter.passphrase
2072
+ }).addOperation(unsignedOp).setTimeout(180).build();
2073
+ const sim = await server.simulateTransaction(simTx);
2074
+ if (rpc.Api.isSimulationError(sim)) {
2075
+ throw new Error(`kit/stellar: simulation failed: ${sim.error}`);
2076
+ }
2077
+ const validUntil = (await server.getLatestLedger()).sequence + 100;
2078
+ const entries = sim.result?.auth ?? [];
2079
+ const signedAuth = [];
2080
+ for (const entry of entries) {
2081
+ if (authAccount && isAddressCredentialFor(entry, authAccount)) {
2082
+ signedAuth.push(await this.adapter.signAuthEntry(entry, validUntil));
2083
+ } else {
2084
+ signedAuth.push(entry);
2085
+ }
2086
+ }
2087
+ const account = await server.getAccount(sourceAddr);
2088
+ const finalOp = Operation.invokeHostFunction({ func, auth: signedAuth });
2089
+ const built = new TransactionBuilder(account, {
2090
+ fee: BASE_FEE,
2091
+ networkPassphrase: this.adapter.passphrase
2092
+ }).addOperation(finalOp).setTimeout(180).build();
2093
+ const authSim = await server.simulateTransaction(built);
2094
+ if (rpc.Api.isSimulationError(authSim)) {
2095
+ throw new Error(`kit/stellar: auth simulation failed: ${authSim.error}`);
2096
+ }
2097
+ const assembled = rpc.assembleTransaction(built, authSim).build();
2098
+ if (this.relayer) {
2099
+ return this.relayer.submit(assembled.toXDR());
2100
+ }
2101
+ if (this.sourceKeypair) {
2102
+ assembled.sign(this.sourceKeypair);
2103
+ return this.sendAndConfirm(assembled);
2104
+ }
2105
+ throw new Error("kit/stellar: no relayer or sourceKeypair configured to submit");
2106
+ }
2107
+ /** Submit a signed tx via RPC and poll to confirmation. Returns the hash. */
2108
+ async sendAndConfirm(tx2) {
2109
+ const server = this.adapter.server();
2110
+ const sent = await server.sendTransaction(tx2);
2111
+ if (sent.status === "ERROR") {
2112
+ throw new Error(`kit/stellar: submit rejected: ${JSON.stringify(sent.errorResult)}`);
2113
+ }
2114
+ const hash6 = sent.hash;
2115
+ for (let i = 0; i < 30; i++) {
2116
+ const got = await server.getTransaction(hash6);
2117
+ if (got.status === rpc.Api.GetTransactionStatus.SUCCESS) return hash6;
2118
+ if (got.status === rpc.Api.GetTransactionStatus.FAILED) {
2119
+ throw new Error(`kit/stellar: tx ${hash6} failed`);
2120
+ }
2121
+ await new Promise((r) => setTimeout(r, 1e3));
2122
+ }
2123
+ throw new Error(`kit/stellar: tx ${hash6} not confirmed in time`);
2124
+ }
2125
+ };
2126
+ function isAddressCredentialFor(entry, accountAddress) {
2127
+ const creds = entry.credentials();
2128
+ if (creds.switch() !== xdr.SorobanCredentialsType.sorobanCredentialsAddress()) return false;
2129
+ return Address.fromScAddress(creds.address().address()).toString() === accountAddress;
2130
+ }
2131
+ var defaultRegistry2 = new InMemoryWalletRegistry();
2132
+
2133
+ // src/recovery/HttpRecoveryClient.ts
2134
+ function toHex2(n) {
2135
+ return "0x" + n.toString(16);
2136
+ }
2137
+ function fromHex2(s) {
2138
+ return BigInt(s);
2139
+ }
2140
+ function deviceLabel() {
2141
+ if (typeof navigator !== "undefined") {
2142
+ return navigator.userAgent || "a new device";
2143
+ }
2144
+ return "a new device";
2145
+ }
2146
+ var HttpRecoveryClient = class {
2147
+ constructor(opts) {
2148
+ this.opts = opts;
2149
+ }
2150
+ async requestDeviceAddition(params) {
2151
+ const res = await fetch(new URL("/api/devices/request", this.opts.baseUrl), {
2152
+ method: "POST",
2153
+ headers: { "Content-Type": "application/json" },
2154
+ body: JSON.stringify({
2155
+ app_id: this.opts.appId,
2156
+ wallet_address: params.accountAddress,
2157
+ new_pub_x: toHex2(params.newSigner.x),
2158
+ new_pub_y: toHex2(params.newSigner.y),
2159
+ device_label: params.deviceLabel ?? deviceLabel(),
2160
+ ...params.email ? { email: params.email } : {}
2161
+ })
2162
+ });
2163
+ if (!res.ok) {
2164
+ const t = await res.text().catch(() => "");
2165
+ throw new Error(`requestDeviceAddition failed: ${res.status} ${t}`);
2166
+ }
2167
+ const data = await res.json();
2168
+ return { requestId: data.request_id };
2169
+ }
2170
+ async getPendingRequest(requestId) {
2171
+ const url = new URL("/api/devices/request", this.opts.baseUrl);
2172
+ url.searchParams.set("id", requestId);
2173
+ const res = await fetch(url, { headers: { "Content-Type": "application/json" } });
2174
+ if (!res.ok) throw new Error(`getPendingRequest failed: ${res.status}`);
2175
+ const data = await res.json();
2176
+ if (!data.found) return null;
2177
+ const status = data.status;
2178
+ return {
2179
+ requestId: data.request_id,
2180
+ appId: data.app_id,
2181
+ userId: "",
2182
+ // the approving device already knows its own identity
2183
+ accountAddress: data.wallet_address,
2184
+ newSigner: { x: fromHex2(data.new_pub_x), y: fromHex2(data.new_pub_y) },
2185
+ createdAt: data.created_at,
2186
+ status
2187
+ };
2188
+ }
2189
+ async confirmDeviceAddition(params) {
2190
+ const res = await fetch(
2191
+ new URL(`/api/devices/request/${params.requestId}/confirm`, this.opts.baseUrl),
2192
+ {
2193
+ method: "POST",
2194
+ headers: { "Content-Type": "application/json" },
2195
+ body: JSON.stringify({ tx_hash: params.txHash })
2196
+ }
2197
+ );
2198
+ if (!res.ok) {
2199
+ const t = await res.text().catch(() => "");
2200
+ throw new Error(`confirmDeviceAddition failed: ${res.status} ${t}`);
2201
+ }
2202
+ }
2203
+ };
2204
+ var STARKNET_ENV = {
2205
+ mainnet: "mainnet",
2206
+ testnet: "sepolia"
2207
+ };
2208
+ var SOLANA_ENV = {
2209
+ mainnet: "solana-mainnet",
2210
+ testnet: "solana-devnet"
2211
+ };
2212
+ var STELLAR_ENV = {
2213
+ mainnet: "stellar-mainnet",
2214
+ testnet: "stellar-testnet"
2215
+ };
2216
+ var Cavos = class _Cavos {
2217
+ constructor(identity, address, status, account, adapter, devicePubkey, paymaster) {
2218
+ this.identity = identity;
2219
+ this.address = address;
2220
+ this.status = status;
2221
+ this.account = account;
2222
+ this.adapter = adapter;
2223
+ this.devicePubkey = devicePubkey;
2224
+ this.paymaster = paymaster;
2225
+ /** Discriminant for the `CavosWallet` union — narrows `execute()` per chain. */
2226
+ this.chain = "starknet";
2227
+ /** Request id of the pending device-addition, when status is needs-device-approval. */
2228
+ this.pendingRequestId = null;
2229
+ }
2230
+ /**
2231
+ * Unified entry point. Pick a `chain` and an `network` environment; the kit
2232
+ * resolves the concrete network (sepolia/devnet for testnet, mainnet for
2233
+ * mainnet) and returns a chain-native wallet. The result is a discriminated
2234
+ * union (`wallet.chain`), so `execute()` keeps each chain's native signature:
2235
+ *
2236
+ * const wallet = await Cavos.connect({ chain: "solana", network: "testnet", identity, appSalt, appId });
2237
+ * if (wallet.chain === "starknet") await wallet.execute(calls);
2238
+ * else await wallet.execute(amount, dest);
2239
+ */
2240
+ static async connect(opts) {
2241
+ if (opts.chain === "solana") {
2242
+ return CavosSolana.connect({
2243
+ network: SOLANA_ENV[opts.network],
2244
+ ...opts.auth ? { auth: opts.auth } : {},
2245
+ ...opts.identity ? { identity: opts.identity } : {},
2246
+ appSalt: opts.appSalt,
2247
+ ...opts.appId ? { appId: opts.appId } : {},
2248
+ ...opts.backendUrl ? { backendUrl: opts.backendUrl } : {},
2249
+ ...opts.registry ? { registry: opts.registry } : {},
2250
+ ...opts.rpcUrl ? { rpcUrl: opts.rpcUrl } : {},
2251
+ ...opts.programId ? { programId: opts.programId } : {},
2252
+ ...opts.createSigner ? { createSigner: opts.createSigner } : {},
2253
+ ...opts.relayer ? { relayer: opts.relayer } : {},
2254
+ ...opts.feePayer ? { feePayer: opts.feePayer } : {}
2255
+ });
2256
+ }
2257
+ if (opts.chain === "stellar") {
2258
+ return CavosStellar.connect({
2259
+ network: STELLAR_ENV[opts.network],
2260
+ ...opts.auth ? { auth: opts.auth } : {},
2261
+ ...opts.identity ? { identity: opts.identity } : {},
2262
+ appSalt: opts.appSalt,
2263
+ ...opts.appId ? { appId: opts.appId } : {},
2264
+ ...opts.backendUrl ? { backendUrl: opts.backendUrl } : {},
2265
+ ...opts.registry ? { registry: opts.registry } : {},
2266
+ ...opts.rpcUrl ? { rpcUrl: opts.rpcUrl } : {},
2267
+ ...opts.factoryId ? { factoryId: opts.factoryId } : {},
2268
+ ...opts.createSigner ? { createSigner: opts.createSigner } : {},
2269
+ ...opts.stellarRelayer ? { relayer: opts.stellarRelayer } : {},
2270
+ ...opts.stellarSourceKeypair ? { sourceKeypair: opts.stellarSourceKeypair } : {}
2271
+ });
2272
+ }
2273
+ if (!opts.paymasterApiKey) {
2274
+ throw new Error("kit: `paymasterApiKey` is required for Starknet connections");
2275
+ }
2276
+ return _Cavos.connectStarknet({
2277
+ network: STARKNET_ENV[opts.network],
2278
+ auth: opts.auth,
2279
+ identity: opts.identity,
2280
+ appSalt: opts.appSalt,
2281
+ appId: opts.appId,
2282
+ backendUrl: opts.backendUrl,
2283
+ registry: opts.registry,
2284
+ recovery: opts.recovery,
2285
+ paymasterApiKey: opts.paymasterApiKey,
2286
+ paymasterUrl: opts.paymasterUrl,
2287
+ rpcUrl: opts.rpcUrl,
2288
+ classHash: opts.classHash,
2289
+ createSigner: opts.createSigner
2290
+ });
2291
+ }
2292
+ static async connectStarknet(opts) {
2293
+ const identity = opts.identity ?? await opts.auth?.authenticate();
2294
+ if (!identity) throw new Error("kit: connect requires `identity` or `auth`");
2295
+ const classHash = opts.classHash ?? DEVICE_ACCOUNT_CLASS_HASH[opts.network];
2296
+ if (!classHash) throw new Error(`kit: no DeviceAccount class hash for ${opts.network}`);
2297
+ const provider = new RpcProvider({
2298
+ nodeUrl: opts.rpcUrl ?? STARKNET_NETWORKS[opts.network].rpcUrl
2299
+ });
2300
+ const paymasterUrl = opts.paymasterUrl ?? CAVOS_PAYMASTER_URL[opts.network];
2301
+ const paymasterConfig = { url: paymasterUrl, apiKey: opts.paymasterApiKey };
2302
+ const paymaster = new PaymasterRpc({
2303
+ nodeUrl: paymasterUrl,
2304
+ headers: { "x-paymaster-api-key": opts.paymasterApiKey }
2305
+ });
2306
+ const addressSeed = deriveAddressSeed({ userId: identity.userId, appSalt: opts.appSalt });
2307
+ const signer = opts.createSigner ? await opts.createSigner(`${identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${identity.userId}:${opts.appSalt}` });
2308
+ const devicePubkey = await signer.getPublicKey();
2309
+ const adapter = new StarknetAdapter({ classHash, signer, provider });
2310
+ const makeAccount = (address2) => new Account$1({
2311
+ provider,
2312
+ address: address2,
2313
+ signer: new StarknetDeviceSigner(signer),
2314
+ paymaster,
2315
+ cairoVersion: "1"
2316
+ });
2317
+ const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
2318
+ const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry3);
2319
+ const recovery = opts.recovery ?? (opts.appId ? new HttpRecoveryClient({ baseUrl: backendUrl, appId: opts.appId }) : null);
2320
+ const existing = await registry.lookup(identity.userId);
2321
+ if (existing) {
2322
+ const account2 = makeAccount(existing.address);
2323
+ const isSigner2 = await adapter.isAuthorizedSigner(existing.address, devicePubkey);
2324
+ const cavos = new _Cavos(
2325
+ identity,
2326
+ existing.address,
2327
+ isSigner2 ? "ready" : "needs-device-approval",
2328
+ account2,
2329
+ adapter,
2330
+ devicePubkey,
2331
+ paymasterConfig
2332
+ );
2333
+ if (!isSigner2 && recovery) {
2334
+ const dedup = lastDeviceRequest.get(identity.userId);
2335
+ const fresh = dedup && Date.now() - dedup.requestedAt < DEVICE_REQUEST_DEDUP_MS;
2336
+ try {
2337
+ if (fresh) {
2338
+ cavos.pendingRequestId = dedup.requestId;
2339
+ } else {
2340
+ const { requestId } = await recovery.requestDeviceAddition({
2341
+ userId: identity.userId,
2342
+ accountAddress: existing.address,
2343
+ newSigner: devicePubkey,
2344
+ ...identity.email ? { email: identity.email } : {}
2345
+ });
2346
+ cavos.pendingRequestId = requestId;
2347
+ lastDeviceRequest.set(identity.userId, { requestId, requestedAt: Date.now() });
2348
+ }
2349
+ } catch (e) {
2350
+ console.warn("[Cavos] requestDeviceAddition failed:", e);
2351
+ }
2352
+ }
2353
+ return cavos;
2354
+ }
2355
+ const address = adapter.computeAddress({ addressSeed, initialSigner: devicePubkey });
2356
+ const account = makeAccount(address);
2357
+ const alreadyDeployed = await isDeployed(provider, address);
2358
+ if (!alreadyDeployed) {
2359
+ const deploymentData = {
2360
+ address,
2361
+ class_hash: classHash,
2362
+ salt: num.toHex(addressSeed),
2363
+ calldata: adapter.constructorCalldata(addressSeed, devicePubkey),
2364
+ version: 1
2365
+ };
2366
+ const deployRes = await account.executePaymasterTransaction([], {
2367
+ feeMode: { mode: "sponsored" },
2368
+ deploymentData
2369
+ });
2370
+ try {
2371
+ await provider.waitForTransaction(deployRes.transaction_hash);
2372
+ } catch (e) {
2373
+ console.warn("[Cavos] deploy receipt wait failed:", e);
2374
+ }
2375
+ }
2376
+ await registry.register({ userId: identity.userId, address, initialSigner: devicePubkey });
2377
+ let isSigner;
2378
+ try {
2379
+ isSigner = await adapter.isAuthorizedSigner(address, devicePubkey);
2380
+ } catch (e) {
2381
+ console.warn("[Cavos] isAuthorizedSigner read failed:", e);
2382
+ isSigner = !alreadyDeployed;
2383
+ }
2384
+ return new _Cavos(
2385
+ identity,
2386
+ address,
2387
+ isSigner ? "ready" : "needs-device-approval",
2388
+ account,
2389
+ adapter,
2390
+ devicePubkey,
2391
+ paymasterConfig
2392
+ );
2393
+ }
2394
+ /** This device's public key (e.g. to request addition to an existing wallet). */
2395
+ get publicKey() {
2396
+ return this.devicePubkey;
2397
+ }
2398
+ /** Execute a sponsored (gasless) multicall, signed silently by the device. */
2399
+ async execute(calls) {
2400
+ if (this.status !== "ready") {
2401
+ throw new Error("kit: this device is not yet an authorized signer of the wallet");
2402
+ }
2403
+ const res = await this.account.executePaymasterTransaction(calls, {
2404
+ feeMode: { mode: "sponsored" }
2405
+ });
2406
+ return { transactionHash: res.transaction_hash };
2407
+ }
2408
+ /** Authorize an additional device signer (sponsored). Self-submitted. */
2409
+ async addSigner(pubkey) {
2410
+ return this.execute([this.adapter.buildAddSigner(this.address, pubkey)]);
2411
+ }
2412
+ /**
2413
+ * Enroll a passkey as an APPROVER so the user can later add devices from any
2414
+ * browser (2FA-style step-up). Requires a ready device (the enrollment call is
2415
+ * device-signed and gasless). Idempotent: a no-op if the passkey is already an
2416
+ * approver. Call this whenever the app decides to prompt "turn on device
2417
+ * approvals". Returns the passkey's public key + the enrollment tx hash.
2418
+ */
2419
+ async enrollPasskey(passkey, params) {
2420
+ const enrolled = await passkey.enroll(params);
2421
+ const { transactionHash } = await this.addApprover(enrolled.publicKey);
2422
+ return { publicKey: enrolled.publicKey, transactionHash };
2423
+ }
2424
+ /**
2425
+ * Register an ALREADY-enrolled passkey public key as an approver (gasless,
2426
+ * device-signed). Idempotent. Use this to register ONE passkey across multiple
2427
+ * chains without re-prompting `passkey.enroll()` on each: enroll once, then
2428
+ * call `addApprover(pubkey)` on each chain's wallet.
2429
+ */
2430
+ async addApprover(pubkey) {
2431
+ if (this.status !== "ready") {
2432
+ throw new Error("kit: addApprover requires a ready, authorized device");
2433
+ }
2434
+ if (await this.adapter.isApprover(this.address, pubkey)) return {};
2435
+ const { transactionHash } = await this.execute([
2436
+ this.adapter.buildAddApprover(this.address, pubkey)
2437
+ ]);
2438
+ return { transactionHash };
2439
+ }
2440
+ /**
2441
+ * From a brand-new browser (status `needs-device-approval`), use the user's
2442
+ * synced passkey to authorize adding THIS device — no trip back to an already-
2443
+ * authorized device.
2444
+ *
2445
+ * `add_signer_via_passkey` is a public external authorized by the embedded
2446
+ * WebAuthn assertion (no device signature), so by default we sponsor it through
2447
+ * the Cavos paymaster's `paymaster_executeDirectTransaction` (the forwarder's
2448
+ * `execute_sponsored` runs a generic call — it does NOT require SNIP-9). Pass a
2449
+ * custom `submit` to route it through your own relayer instead. Returns the tx.
2450
+ */
2451
+ async approveThisDeviceWithPasskey(opts) {
2452
+ if (this.status === "ready") {
2453
+ throw new Error("kit: this device is already an authorized signer");
2454
+ }
2455
+ const { leaf, nonce } = await this.passkeyLeafForThisDevice();
2456
+ const leaves = [leaf];
2457
+ const assertion = await opts.passkey.assert(batchChallenge(leaves));
2458
+ return this.submitPasskeyApproval(assertion, leaves, 0, nonce, opts.submit);
2459
+ }
2460
+ /** This device's leaf + the current passkey nonce, for a (possibly multi-chain)
2461
+ * passkey approval batch. See `approveDeviceEverywhere`. */
2462
+ async passkeyLeafForThisDevice() {
2463
+ const nonce = await this.adapter.getPasskeyNonce(this.address);
2464
+ return { leaf: this.adapter.passkeyLeaf(this.devicePubkey, nonce), nonce };
2465
+ }
2466
+ /** Submit `add_signer_via_passkey` given a (shared) assertion + this chain's
2467
+ * position in the batch. The assertion doesn't carry the passkey pubkey, so we
2468
+ * recover both candidates and pick the enrolled approver via the on-chain view
2469
+ * (no backend). Defaults to sponsoring through the paymaster. */
2470
+ async submitPasskeyApproval(assertion, leaves, leafIndex, nonce, submit) {
2471
+ const digest = webauthnDigest(assertion.authenticatorData, assertion.clientDataJSON);
2472
+ const candidates = recoverCandidatePublicKeys(assertion.r, assertion.s, digest);
2473
+ let yParity = null;
2474
+ for (const cand of candidates) {
2475
+ if (await this.adapter.isApprover(this.address, cand.publicKey)) {
2476
+ yParity = cand.yParity;
2477
+ break;
2478
+ }
2479
+ }
2480
+ if (yParity === null) {
2481
+ throw new Error("kit: this passkey is not a registered approver of the wallet");
2482
+ }
2483
+ const call = this.adapter.buildAddSignerViaPasskey(
2484
+ this.address,
2485
+ this.devicePubkey,
2486
+ nonce,
2487
+ leaves,
2488
+ leafIndex,
2489
+ assertion,
2490
+ yParity
2491
+ );
2492
+ if (submit) return submit(call);
2493
+ if (!this.paymaster) {
2494
+ throw new Error("kit: no paymaster configured \u2014 pass a `submit` relayer to approveThisDeviceWithPasskey");
2495
+ }
2496
+ return paymasterExecuteDirect(this.paymaster, this.address, call);
2497
+ }
2498
+ /**
2499
+ * Register a self-custodial backup signer derived from `code`, so the account
2500
+ * can be recovered after the user loses every device. Idempotent: if the
2501
+ * derived backup key is already an authorised signer, this is a no-op.
2502
+ *
2503
+ * The code never leaves the device — only its deterministic public key is
2504
+ * added on-chain as an ordinary signer. Sponsor this like any other
2505
+ * add_signer (gasless). Returns the transaction hash (or undefined when the
2506
+ * backup was already set up).
2507
+ */
2508
+ async setupRecovery(code) {
2509
+ const { publicKey: backupPubkey } = deriveBackupKey(code);
2510
+ const already = await this.adapter.isAuthorizedSigner(this.address, backupPubkey);
2511
+ if (already) return void 0;
2512
+ return this.addSigner(backupPubkey);
2513
+ }
2514
+ /**
2515
+ * Recover an account after losing every device signer. Derives the backup key
2516
+ * from `code`, uses it (not the new device key) to sign an `add_signer` for
2517
+ * the new device, and returns a ready Cavos bound to the new device. The
2518
+ * account address is unchanged.
2519
+ *
2520
+ * Self-custodial: only someone holding the code (i.e. the rightful owner) can
2521
+ * re-derive the backup key. The backend never sees the code.
2522
+ */
2523
+ static async recover(opts) {
2524
+ const network = STARKNET_ENV[opts.network];
2525
+ const classHash = opts.classHash ?? DEVICE_ACCOUNT_CLASS_HASH[network];
2526
+ if (!classHash) throw new Error(`kit: no DeviceAccount class hash for ${network}`);
2527
+ const provider = new RpcProvider({
2528
+ nodeUrl: opts.rpcUrl ?? STARKNET_NETWORKS[network].rpcUrl
2529
+ });
2530
+ const paymaster = new PaymasterRpc({
2531
+ nodeUrl: opts.paymasterUrl ?? CAVOS_PAYMASTER_URL[network],
2532
+ headers: { "x-paymaster-api-key": opts.paymasterApiKey }
2533
+ });
2534
+ const signer = opts.createSigner ? await opts.createSigner(`${opts.identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${opts.identity.userId}:${opts.appSalt}` });
2535
+ const devicePubkey = await signer.getPublicKey();
2536
+ const backup = BackupSigner.fromCode(opts.code);
2537
+ const backupAdapter = new StarknetAdapter({ classHash, signer: backup, provider });
2538
+ const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
2539
+ const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network }) : defaultRegistry3);
2540
+ const existing = await registry.lookup(opts.identity.userId);
2541
+ if (!existing) {
2542
+ throw new Error("kit: no account found for this identity \u2014 nothing to recover");
2543
+ }
2544
+ const backupAccount = new Account$1({
2545
+ provider,
2546
+ address: existing.address,
2547
+ signer: new StarknetDeviceSigner(backup),
2548
+ paymaster,
2549
+ cairoVersion: "1"
2550
+ });
2551
+ const alreadyAuthed = await backupAdapter.isAuthorizedSigner(existing.address, devicePubkey);
2552
+ if (!alreadyAuthed) {
2553
+ const res = await backupAccount.executePaymasterTransaction(
2554
+ [backupAdapter.buildAddSigner(existing.address, devicePubkey)],
2555
+ { feeMode: { mode: "sponsored" } }
2556
+ );
2557
+ try {
2558
+ await provider.waitForTransaction(res.transaction_hash);
2559
+ } catch (e) {
2560
+ console.warn("[Cavos] recovery add_signer receipt wait failed:", e);
2561
+ }
2562
+ }
2563
+ const adapter = new StarknetAdapter({ classHash, signer, provider });
2564
+ const account = new Account$1({
2565
+ provider,
2566
+ address: existing.address,
2567
+ signer: new StarknetDeviceSigner(signer),
2568
+ paymaster,
2569
+ cairoVersion: "1"
2570
+ });
2571
+ return new _Cavos(opts.identity, existing.address, "ready", account, adapter, devicePubkey);
2572
+ }
2573
+ };
2574
+ var defaultRegistry3 = new InMemoryWalletRegistry();
2575
+ var DEVICE_REQUEST_DEDUP_MS = 5 * 60 * 1e3;
2576
+ var lastDeviceRequest = /* @__PURE__ */ new Map();
2577
+ async function isDeployed(provider, address) {
2578
+ try {
2579
+ const classHash = await provider.getClassHashAt(address);
2580
+ return !!classHash && classHash !== "0x0";
2581
+ } catch {
2582
+ return false;
2583
+ }
2584
+ }
2585
+ async function approveDeviceEverywhere(wallets, passkey) {
2586
+ const targets = wallets.filter((w) => w.status === "needs-device-approval");
2587
+ if (targets.length === 0) return [];
2588
+ const infos = await Promise.all(targets.map((w) => w.passkeyLeafForThisDevice()));
2589
+ const leaves = infos.map((i) => i.leaf);
2590
+ const assertion = await passkey.assert(batchChallenge(leaves));
2591
+ const out = [];
2592
+ for (let i = 0; i < targets.length; i++) {
2593
+ const { transactionHash } = await targets[i].submitPasskeyApproval(
2594
+ assertion,
2595
+ leaves,
2596
+ i,
2597
+ infos[i].nonce
2598
+ );
2599
+ out.push({ chain: targets[i].chain, transactionHash });
2600
+ }
2601
+ return out;
2602
+ }
2603
+ async function paymasterExecuteDirect(paymaster, userAddress, call) {
2604
+ const body = {
2605
+ jsonrpc: "2.0",
2606
+ id: 1,
2607
+ method: "paymaster_executeDirectTransaction",
2608
+ params: {
2609
+ transaction: {
2610
+ type: "invoke",
2611
+ invoke: {
2612
+ user_address: userAddress,
2613
+ execute_from_outside_call: {
2614
+ to: call.contractAddress,
2615
+ selector: hash.getSelectorFromName(call.entrypoint),
2616
+ calldata: call.calldata.map((c) => num.toHex(c))
2617
+ }
2618
+ }
2619
+ },
2620
+ parameters: { version: "0x1", fee_mode: { mode: "sponsored" } }
2621
+ }
2622
+ };
2623
+ const res = await fetch(paymaster.url, {
2624
+ method: "POST",
2625
+ headers: {
2626
+ "Content-Type": "application/json",
2627
+ ...paymaster.apiKey ? { "x-paymaster-api-key": paymaster.apiKey } : {}
2628
+ },
2629
+ body: JSON.stringify(body)
2630
+ });
2631
+ const json = await res.json();
2632
+ if (json.error) {
2633
+ throw new Error(`kit: paymaster passkey approval failed: ${JSON.stringify(json.error)}`);
2634
+ }
2635
+ return { transactionHash: json.result?.transaction_hash ?? json.result?.tracking_id };
2636
+ }
2637
+ var CavosAuth = class {
2638
+ constructor(opts = {}) {
2639
+ this.opts = opts;
2640
+ /** Most recent nonce sent to the backend (for the pending OAuth/OTP request). */
2641
+ this.pendingNonce = null;
2642
+ this.last = null;
2643
+ this.backendUrl = opts.backendUrl ?? "https://cavos.xyz";
2644
+ }
2645
+ /** Redirect URL for Google login (open it; user returns to your redirectUri). */
2646
+ async getGoogleOAuthUrl(redirectUri) {
2647
+ return this.oauthUrl("google", redirectUri);
2648
+ }
2649
+ /** Redirect URL for Apple login. */
2650
+ async getAppleOAuthUrl(redirectUri) {
2651
+ return this.oauthUrl("apple", redirectUri);
2652
+ }
2653
+ async oauthUrl(provider, redirectUri) {
2654
+ if (typeof window === "undefined") throw new Error("kit/auth: OAuth requires a browser");
2655
+ const params = new URLSearchParams({
2656
+ nonce: this.freshNonce(),
2657
+ redirect_uri: redirectUri ?? window.location.href,
2658
+ ...this.opts.appId ? { app_id: this.opts.appId } : {}
2659
+ });
2660
+ const { url } = await this.get(`/api/oauth/${provider}?${params}`);
2661
+ return url;
2662
+ }
2663
+ /**
2664
+ * Resolve the identity from an OAuth callback. The auth data is carried in the
2665
+ * `auth_data` (or `zk_auth_data`) query param on return. We only extract `sub`.
2666
+ */
2667
+ async handleCallback(authDataOrSearch) {
2668
+ const authData = extractAuthData(authDataOrSearch);
2669
+ return this.identityFromAuthData(authData, "oauth");
2670
+ }
2671
+ /** Send a one-time code to an email (Firebase OTP). */
2672
+ async sendOtp(email) {
2673
+ await this.post("/api/oauth/firebase/otp/request", {
2674
+ email,
2675
+ nonce: this.freshNonce(),
2676
+ ...this.opts.appId ? { app_id: this.opts.appId } : {}
2677
+ });
2678
+ }
2679
+ /** Send a passwordless magic-link sign-in email (Firebase). */
2680
+ async sendMagicLink(email) {
2681
+ await this.post("/api/oauth/firebase/magic-link", {
2682
+ email,
2683
+ nonce: this.freshNonce(),
2684
+ ...this.opts.appId ? { app_id: this.opts.appId } : {},
2685
+ ...typeof window !== "undefined" ? { redirect_uri: window.location.href } : {}
2686
+ });
2687
+ }
2688
+ /** Verify the OTP and resolve the identity. */
2689
+ async verifyOtp(email, code) {
2690
+ const res = await this.post("/api/oauth/firebase/otp/verify", {
2691
+ email,
2692
+ code,
2693
+ nonce: this.consumeNonce(),
2694
+ ...this.opts.appId ? { app_id: this.opts.appId } : {}
2695
+ });
2696
+ return this.identityFromAuthData(res.id_token ?? res.jwt ?? res.token ?? JSON.stringify(res), "otp", email);
2697
+ }
2698
+ /** AuthProvider: returns the identity resolved by the last login step. */
2699
+ async authenticate() {
2700
+ if (!this.last) throw new Error("kit/auth: no identity yet \u2014 complete a login first");
2701
+ return this.last;
2702
+ }
2703
+ // ── internals ──────────────────────────────────────────────────────────────
2704
+ /**
2705
+ * Build an `Identity` from whatever the backend returned. The Cavos backend
2706
+ * wraps the user id in a JWT (its `sub` claim); for the device model we only
2707
+ * need that stable id — the signature is never checked on-chain.
2708
+ */
2709
+ async identityFromAuthData(authData, provider, emailOverride) {
2710
+ let token = authData;
2711
+ try {
2712
+ const parsed = JSON.parse(authData);
2713
+ token = parsed.id_token ?? parsed.jwt ?? parsed.token ?? authData;
2714
+ } catch {
2715
+ }
2716
+ const claims = parseJwt(token);
2717
+ return this.remember({
2718
+ userId: String(claims.sub ?? claims.user_id ?? claims.uid),
2719
+ email: claims.email ?? emailOverride,
2720
+ provider: claims.firebase?.sign_in_provider ?? claims.provider ?? provider
2721
+ });
2722
+ }
2723
+ /** Generate (and remember) the nonce the Cavos backend expects on requests. */
2724
+ freshNonce() {
2725
+ const bytes = crypto.getRandomValues(new Uint8Array(31));
2726
+ const h = hash.computePoseidonHashOnElements([bytesToChunks(bytes)]);
2727
+ this.pendingNonce = num.toHex(h);
2728
+ return this.pendingNonce;
2729
+ }
2730
+ /** Return the pending nonce (for the verify step), clearing it. */
2731
+ consumeNonce() {
2732
+ if (!this.pendingNonce) return this.freshNonce();
2733
+ const n = this.pendingNonce;
2734
+ this.pendingNonce = null;
2735
+ return n;
2736
+ }
2737
+ remember(id) {
2738
+ this.last = id;
2739
+ return id;
2740
+ }
2741
+ async get(path) {
2742
+ const r = await fetch(`${this.backendUrl}${path}`);
2743
+ if (!r.ok) throw new Error(`kit/auth: ${path} -> ${r.status} ${await r.text()}`);
2744
+ return r.json();
2745
+ }
2746
+ async post(path, body) {
2747
+ const r = await fetch(`${this.backendUrl}${path}`, {
2748
+ method: "POST",
2749
+ headers: { "Content-Type": "application/json" },
2750
+ body: JSON.stringify(body)
2751
+ });
2752
+ if (!r.ok) throw new Error(`kit/auth: ${path} -> ${r.status} ${await r.text()}`);
2753
+ return r.json();
2754
+ }
2755
+ };
2756
+ function extractAuthData(input) {
2757
+ if (input.includes("auth_data=") || input.includes("zk_auth_data=")) {
2758
+ const params = new URLSearchParams(input.startsWith("?") ? input : `?${input}`);
2759
+ return params.get("auth_data") ?? params.get("zk_auth_data") ?? input;
2760
+ }
2761
+ return input;
2762
+ }
2763
+ function parseJwt(jwt) {
2764
+ const part = jwt.split(".")[1];
2765
+ if (!part) throw new Error("kit/auth: malformed JWT");
2766
+ const json = atob(part.replace(/-/g, "+").replace(/_/g, "/"));
2767
+ return JSON.parse(json);
2768
+ }
2769
+ function bytesToChunks(bytes) {
2770
+ let w = 0n;
2771
+ for (const b of bytes.subarray(0, 31)) w = w << 8n | BigInt(b);
2772
+ return w;
2773
+ }
2774
+
2775
+ export { BackupSigner, Cavos, CavosAuth, CavosSolana, CavosStellar, DEVICE_ACCOUNT_CLASS_HASH, DEVICE_ACCOUNT_PROGRAM_ID, DEVICE_ACCOUNT_WASM_HASH, FACTORY_CONTRACT_ID, HttpRecoveryClient, HttpWalletRegistry, InMemoryWalletRegistry, NATIVE_SAC_ID, SECP256R1_PROGRAM_ID, SOLANA_NETWORKS, STARKNET_NETWORKS, STELLAR_NETWORKS, SolanaAdapter, SolanaRelayer, StarknetAdapter, StarknetDeviceSigner, StellarAdapter, StellarRelayer, UDC_ADDRESS, WebCryptoSigner, anchorDiscriminator, approveDeviceEverywhere, base64urlEncode, batchChallenge, bigIntTo32Bytes, buildSecp256r1Instruction, bytesToBigInt, bytesToHex, challengeOffsetOf, compressedPubkey, derToRs, deriveAddressSeed, deriveAddressSeedSolana, deriveAddressSeedStellar, deriveBackupKey, deviceSignatureScVal, encodeLowSSignature, encodeLowSSignature2, generateRecoveryCode, hexToBytes, lowS, recoverCandidatePublicKeys, recoverYParity, sec1Pubkey, serializeInstructions, signatureToFelts, spkiToPublicKey, u256ToFelts, webauthnDigest };
2776
+ //# sourceMappingURL=chunk-BNGLH3Q3.mjs.map
2777
+ //# sourceMappingURL=chunk-BNGLH3Q3.mjs.map