@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.
- package/README.md +113 -42
- package/dist/Cavos-BH2_tOQ2.d.mts +994 -0
- package/dist/Cavos-BH2_tOQ2.d.ts +994 -0
- package/dist/chunk-BNGLH3Q3.mjs +2777 -0
- package/dist/chunk-BNGLH3Q3.mjs.map +1 -0
- package/dist/index.d.mts +156 -242
- package/dist/index.d.ts +156 -242
- package/dist/index.js +1989 -151
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +88 -2
- package/dist/index.mjs.map +1 -1
- package/dist/react/index.d.mts +42 -5
- package/dist/react/index.d.ts +42 -5
- package/dist/react/index.js +1786 -86
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +48 -7
- package/dist/react/index.mjs.map +1 -1
- package/package.json +4 -1
- package/dist/chunk-XWBX2ZIO.mjs +0 -1061
- package/dist/chunk-XWBX2ZIO.mjs.map +0 -1
- package/dist/constants-C530TZFF.d.mts +0 -89
- package/dist/constants-C530TZFF.d.ts +0 -89
package/dist/index.js
CHANGED
|
@@ -3,9 +3,11 @@
|
|
|
3
3
|
var starknet = require('starknet');
|
|
4
4
|
var sha256 = require('@noble/hashes/sha256');
|
|
5
5
|
var p256 = require('@noble/curves/p256');
|
|
6
|
+
var web3_js = require('@solana/web3.js');
|
|
6
7
|
var hkdf = require('@noble/hashes/hkdf');
|
|
7
8
|
var pbkdf2 = require('@noble/hashes/pbkdf2');
|
|
8
9
|
var utils = require('@noble/hashes/utils');
|
|
10
|
+
var stellarSdk = require('@stellar/stellar-sdk');
|
|
9
11
|
|
|
10
12
|
// src/Cavos.ts
|
|
11
13
|
|
|
@@ -31,6 +33,18 @@ function hexToBytes(hex) {
|
|
|
31
33
|
for (let i = 0; i < out.length; i++) out[i] = parseInt(padded.slice(i * 2, i * 2 + 2), 16);
|
|
32
34
|
return out;
|
|
33
35
|
}
|
|
36
|
+
function bytesToByteArrayCalldata(bytes) {
|
|
37
|
+
const CHUNK = 31;
|
|
38
|
+
const fullCount = Math.floor(bytes.length / CHUNK);
|
|
39
|
+
const out = [String(fullCount)];
|
|
40
|
+
for (let i = 0; i < fullCount; i++) {
|
|
41
|
+
out.push("0x" + bytesToBigInt(bytes.subarray(i * CHUNK, i * CHUNK + CHUNK)).toString(16));
|
|
42
|
+
}
|
|
43
|
+
const rem = bytes.subarray(fullCount * CHUNK);
|
|
44
|
+
out.push("0x" + (rem.length ? bytesToBigInt(rem).toString(16) : "0"));
|
|
45
|
+
out.push(String(rem.length));
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
34
48
|
function bigIntTo32Bytes(value) {
|
|
35
49
|
const out = new Uint8Array(32);
|
|
36
50
|
let v = value;
|
|
@@ -168,8 +182,8 @@ var CAVOS_PAYMASTER_URL = {
|
|
|
168
182
|
mainnet: "https://paymaster.cavos.xyz"
|
|
169
183
|
};
|
|
170
184
|
var DEVICE_ACCOUNT_CLASS_HASH = {
|
|
171
|
-
sepolia: "
|
|
172
|
-
mainnet: ""
|
|
185
|
+
sepolia: "0x25cbc5423e8ee895febb0ef2c3945b408da44d0039d915fbdd681fe6b6ba66b",
|
|
186
|
+
mainnet: "0x1840aded59e8a0d2b440a134cb9079a7fc11b06c77f58ed189ab436a034ca6a"
|
|
173
187
|
};
|
|
174
188
|
|
|
175
189
|
// src/chains/starknet/StarknetAdapter.ts
|
|
@@ -232,6 +246,73 @@ var StarknetAdapter = class {
|
|
|
232
246
|
const sig = await this.opts.signer.sign(bigIntTo32Bytes(txHash));
|
|
233
247
|
return signatureToFelts(sig).map((f) => starknet.num.toHex(f));
|
|
234
248
|
}
|
|
249
|
+
// --- passkey approvers ---
|
|
250
|
+
buildAddApprover(accountAddress, passkey) {
|
|
251
|
+
return { contractAddress: accountAddress, entrypoint: "add_approver", calldata: pubkeyCalldata(passkey) };
|
|
252
|
+
}
|
|
253
|
+
buildRemoveApprover(accountAddress, passkey) {
|
|
254
|
+
return { contractAddress: accountAddress, entrypoint: "remove_approver", calldata: pubkeyCalldata(passkey) };
|
|
255
|
+
}
|
|
256
|
+
async isApprover(accountAddress, passkey) {
|
|
257
|
+
if (!this.opts.provider) throw new Error("kit/starknet: provider required for reads");
|
|
258
|
+
const res = await this.opts.provider.callContract({
|
|
259
|
+
contractAddress: accountAddress,
|
|
260
|
+
entrypoint: "is_approver",
|
|
261
|
+
calldata: pubkeyCalldata(passkey)
|
|
262
|
+
});
|
|
263
|
+
return BigInt(res[0] ?? 0) !== 0n;
|
|
264
|
+
}
|
|
265
|
+
async getPasskeyNonce(accountAddress) {
|
|
266
|
+
if (!this.opts.provider) throw new Error("kit/starknet: provider required for reads");
|
|
267
|
+
const res = await this.opts.provider.callContract({
|
|
268
|
+
contractAddress: accountAddress,
|
|
269
|
+
entrypoint: "get_passkey_nonce",
|
|
270
|
+
calldata: []
|
|
271
|
+
});
|
|
272
|
+
return BigInt(res[0] ?? 0);
|
|
273
|
+
}
|
|
274
|
+
/** This chain's leaf for approving `add_signer(newSigner)` at `nonce`:
|
|
275
|
+
* `sha256(new_x || new_y || nonce)` (coords 32B BE, nonce 16B BE). The batch
|
|
276
|
+
* challenge the passkey signs is `sha256(concat(leaves))` across chains. */
|
|
277
|
+
passkeyLeaf(newSigner, nonce) {
|
|
278
|
+
const msg = new Uint8Array(32 + 32 + 16);
|
|
279
|
+
msg.set(bigIntTo32Bytes(newSigner.x), 0);
|
|
280
|
+
msg.set(bigIntTo32Bytes(newSigner.y), 32);
|
|
281
|
+
msg.set(bigIntTo32Bytes(nonce).subarray(16), 64);
|
|
282
|
+
return sha256.sha256(msg);
|
|
283
|
+
}
|
|
284
|
+
/** Passkey-authorized `add_signer` call. `leaves`/`leafIndex` place this chain's
|
|
285
|
+
* leaf in the multi-chain batch (single chain → `[leaf]`, index 0). `yParity`
|
|
286
|
+
* matches the raw `(r, s)` — the contract normalizes high-S internally. */
|
|
287
|
+
buildAddSignerViaPasskey(accountAddress, newSigner, nonce, leaves, leafIndex, assertion, yParity) {
|
|
288
|
+
const [rl, rh] = u256ToFelts(assertion.r);
|
|
289
|
+
const [sl, sh] = u256ToFelts(assertion.s);
|
|
290
|
+
const leavesCalldata = [String(leaves.length)];
|
|
291
|
+
for (const leaf of leaves) {
|
|
292
|
+
const [lo, hi] = u256ToFelts(bytesToBigInt(leaf));
|
|
293
|
+
leavesCalldata.push(starknet.num.toHex(lo), starknet.num.toHex(hi));
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
contractAddress: accountAddress,
|
|
297
|
+
entrypoint: "add_signer_via_passkey",
|
|
298
|
+
calldata: [
|
|
299
|
+
...pubkeyCalldata(newSigner),
|
|
300
|
+
// new_x, new_y (u256 pairs)
|
|
301
|
+
starknet.num.toHex(nonce),
|
|
302
|
+
...leavesCalldata,
|
|
303
|
+
// Array<u256> leaves
|
|
304
|
+
String(leafIndex),
|
|
305
|
+
...bytesToByteArrayCalldata(assertion.authenticatorData),
|
|
306
|
+
...bytesToByteArrayCalldata(assertion.clientDataJSON),
|
|
307
|
+
String(assertion.challengeOffset),
|
|
308
|
+
starknet.num.toHex(rl),
|
|
309
|
+
starknet.num.toHex(rh),
|
|
310
|
+
starknet.num.toHex(sl),
|
|
311
|
+
starknet.num.toHex(sh),
|
|
312
|
+
yParity ? "0x1" : "0x0"
|
|
313
|
+
]
|
|
314
|
+
};
|
|
315
|
+
}
|
|
235
316
|
};
|
|
236
317
|
function pubkeyCalldata(pk) {
|
|
237
318
|
const [xl, xh] = u256ToFelts(pk.x);
|
|
@@ -318,76 +399,505 @@ var HttpWalletRegistry = class {
|
|
|
318
399
|
async addDevice(params) {
|
|
319
400
|
}
|
|
320
401
|
};
|
|
402
|
+
function deriveAddressSeed({ userId, appSalt }) {
|
|
403
|
+
const h = starknet.hash.computePoseidonHashOnElements([feltFromString(userId), feltFromString(appSalt)]);
|
|
404
|
+
return BigInt(h);
|
|
405
|
+
}
|
|
406
|
+
function deriveAddressSeedSolana({ userId, appSalt }) {
|
|
407
|
+
return sha256.sha256(new TextEncoder().encode(`cavos:solana:v1:${userId}:${appSalt}`));
|
|
408
|
+
}
|
|
409
|
+
function deriveAddressSeedStellar({ userId, appSalt }) {
|
|
410
|
+
return sha256.sha256(new TextEncoder().encode(`cavos:stellar:v1:${userId}:${appSalt}`));
|
|
411
|
+
}
|
|
412
|
+
function feltFromString(s) {
|
|
413
|
+
const bytes = new TextEncoder().encode(s);
|
|
414
|
+
const chunks = [];
|
|
415
|
+
for (let i = 0; i < bytes.length; i += 31) {
|
|
416
|
+
let w = 0n;
|
|
417
|
+
for (const b of bytes.subarray(i, i + 31)) w = w << 8n | BigInt(b);
|
|
418
|
+
chunks.push(w);
|
|
419
|
+
}
|
|
420
|
+
if (chunks.length === 0) return 0n;
|
|
421
|
+
if (chunks.length === 1) return chunks[0];
|
|
422
|
+
return BigInt(starknet.hash.computePoseidonHashOnElements(chunks));
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// src/chains/solana/constants.ts
|
|
426
|
+
var DEVICE_ACCOUNT_PROGRAM_ID = "FHnoYNfYAmFrwt18gcBGG7G1S5q3RAbCBvrV2D29izNJ";
|
|
427
|
+
var SECP256R1_PROGRAM_ID = "Secp256r1SigVerify1111111111111111111111111";
|
|
428
|
+
var ACCOUNT_SEED = "cavos-account";
|
|
429
|
+
var DOMAIN_ADD = "cavos:add_signer:v1";
|
|
430
|
+
var DOMAIN_REMOVE = "cavos:remove_signer:v1";
|
|
431
|
+
var DOMAIN_TRANSFER = "cavos:transfer:v1";
|
|
432
|
+
var DOMAIN_EXECUTE = "cavos:execute:v1";
|
|
433
|
+
var DOMAIN_ADD_APPROVER = "cavos:add_approver:v1";
|
|
434
|
+
var DOMAIN_REMOVE_APPROVER = "cavos:remove_approver:v1";
|
|
435
|
+
var SECP256R1_N = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n;
|
|
436
|
+
var SOLANA_NETWORKS = {
|
|
437
|
+
"solana-devnet": "https://api.devnet.solana.com",
|
|
438
|
+
"solana-mainnet": "https://api.mainnet-beta.solana.com",
|
|
439
|
+
"solana-localnet": "http://127.0.0.1:8899"
|
|
440
|
+
};
|
|
321
441
|
|
|
322
|
-
// src/
|
|
323
|
-
|
|
324
|
-
|
|
442
|
+
// src/chains/solana/SolanaAdapter.ts
|
|
443
|
+
var COMPRESSED_PUBKEY_SIZE = 33;
|
|
444
|
+
var SIGNATURE_SIZE = 64;
|
|
445
|
+
var CURRENT_IX = 65535;
|
|
446
|
+
var SolanaAdapter = class {
|
|
447
|
+
constructor(opts = {}) {
|
|
448
|
+
this.opts = opts;
|
|
449
|
+
this.chain = "solana";
|
|
450
|
+
this.programId = new web3_js.PublicKey(opts.programId ?? DEVICE_ACCOUNT_PROGRAM_ID);
|
|
451
|
+
}
|
|
452
|
+
/** Deterministic account address: PDA of [seed, address_seed, initial_signer_x]. */
|
|
453
|
+
computeAddress(addressSeed, initialSigner) {
|
|
454
|
+
return this.pda(addressSeed, compressedPubkey(initialSigner)).toBase58();
|
|
455
|
+
}
|
|
456
|
+
pda(addressSeed, initialCompressed) {
|
|
457
|
+
const [pda] = web3_js.PublicKey.findProgramAddressSync(
|
|
458
|
+
[
|
|
459
|
+
Buffer.from(ACCOUNT_SEED),
|
|
460
|
+
Buffer.from(addressSeed),
|
|
461
|
+
Buffer.from(initialCompressed.slice(1, 33))
|
|
462
|
+
// x-coordinate
|
|
463
|
+
],
|
|
464
|
+
this.programId
|
|
465
|
+
);
|
|
466
|
+
return pda;
|
|
467
|
+
}
|
|
468
|
+
/** `initialize` instruction creating the account with its first device signer. */
|
|
469
|
+
buildInitialize(addressSeed, payer, initialSigner) {
|
|
470
|
+
const initialCompressed = compressedPubkey(initialSigner);
|
|
471
|
+
const account = this.pda(addressSeed, initialCompressed);
|
|
472
|
+
const data = Buffer.concat([
|
|
473
|
+
anchorDiscriminator("initialize"),
|
|
474
|
+
Buffer.from(addressSeed),
|
|
475
|
+
// [u8;32]
|
|
476
|
+
Buffer.from(initialCompressed)
|
|
477
|
+
// [u8;33]
|
|
478
|
+
]);
|
|
479
|
+
return new web3_js.TransactionInstruction({
|
|
480
|
+
programId: this.programId,
|
|
481
|
+
keys: [
|
|
482
|
+
{ pubkey: account, isSigner: false, isWritable: true },
|
|
483
|
+
{ pubkey: new web3_js.PublicKey(payer), isSigner: true, isWritable: true },
|
|
484
|
+
{ pubkey: web3_js.SystemProgram.programId, isSigner: false, isWritable: false }
|
|
485
|
+
],
|
|
486
|
+
data
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
/** `[precompile, add_signer]` bundle, authorized by an existing device signer. */
|
|
490
|
+
async buildAddSigner(account, newSigner) {
|
|
491
|
+
const accountPk = new web3_js.PublicKey(account);
|
|
492
|
+
const newCompressed = compressedPubkey(newSigner);
|
|
493
|
+
const nonce = await this.fetchNonce(accountPk);
|
|
494
|
+
const message = concatBytes(
|
|
495
|
+
Buffer.from(DOMAIN_ADD),
|
|
496
|
+
accountPk.toBuffer(),
|
|
497
|
+
newCompressed,
|
|
498
|
+
u64le(nonce)
|
|
499
|
+
);
|
|
500
|
+
const { precompileIx } = await this.signToPrecompile(message);
|
|
501
|
+
const ix = new web3_js.TransactionInstruction({
|
|
502
|
+
programId: this.programId,
|
|
503
|
+
keys: this.guardedKeys(accountPk),
|
|
504
|
+
data: Buffer.concat([anchorDiscriminator("add_signer"), Buffer.from(newCompressed)])
|
|
505
|
+
});
|
|
506
|
+
return [precompileIx, ix];
|
|
507
|
+
}
|
|
508
|
+
/** `[precompile, remove_signer]` bundle, authorized by an existing device signer. */
|
|
509
|
+
async buildRemoveSigner(account, signer) {
|
|
510
|
+
const accountPk = new web3_js.PublicKey(account);
|
|
511
|
+
const compressed = compressedPubkey(signer);
|
|
512
|
+
const nonce = await this.fetchNonce(accountPk);
|
|
513
|
+
const message = concatBytes(
|
|
514
|
+
Buffer.from(DOMAIN_REMOVE),
|
|
515
|
+
accountPk.toBuffer(),
|
|
516
|
+
compressed,
|
|
517
|
+
u64le(nonce)
|
|
518
|
+
);
|
|
519
|
+
const { precompileIx } = await this.signToPrecompile(message);
|
|
520
|
+
const ix = new web3_js.TransactionInstruction({
|
|
521
|
+
programId: this.programId,
|
|
522
|
+
keys: this.guardedKeys(accountPk),
|
|
523
|
+
data: Buffer.concat([anchorDiscriminator("remove_signer"), Buffer.from(compressed)])
|
|
524
|
+
});
|
|
525
|
+
return [precompileIx, ix];
|
|
526
|
+
}
|
|
527
|
+
/** `[precompile, add_approver]` bundle enrolling a passkey approver (device-signed). */
|
|
528
|
+
async buildAddApprover(account, passkey) {
|
|
529
|
+
const accountPk = new web3_js.PublicKey(account);
|
|
530
|
+
const compressed = compressedPubkey(passkey);
|
|
531
|
+
const nonce = await this.fetchNonce(accountPk);
|
|
532
|
+
const message = concatBytes(
|
|
533
|
+
Buffer.from(DOMAIN_ADD_APPROVER),
|
|
534
|
+
accountPk.toBuffer(),
|
|
535
|
+
compressed,
|
|
536
|
+
u64le(nonce)
|
|
537
|
+
);
|
|
538
|
+
const { precompileIx } = await this.signToPrecompile(message);
|
|
539
|
+
const ix = new web3_js.TransactionInstruction({
|
|
540
|
+
programId: this.programId,
|
|
541
|
+
keys: this.guardedKeys(accountPk),
|
|
542
|
+
data: Buffer.concat([anchorDiscriminator("add_approver"), Buffer.from(compressed)])
|
|
543
|
+
});
|
|
544
|
+
return [precompileIx, ix];
|
|
545
|
+
}
|
|
546
|
+
/** `[precompile, remove_approver]` bundle (device-signed). */
|
|
547
|
+
async buildRemoveApprover(account, passkey) {
|
|
548
|
+
const accountPk = new web3_js.PublicKey(account);
|
|
549
|
+
const compressed = compressedPubkey(passkey);
|
|
550
|
+
const nonce = await this.fetchNonce(accountPk);
|
|
551
|
+
const message = concatBytes(
|
|
552
|
+
Buffer.from(DOMAIN_REMOVE_APPROVER),
|
|
553
|
+
accountPk.toBuffer(),
|
|
554
|
+
compressed,
|
|
555
|
+
u64le(nonce)
|
|
556
|
+
);
|
|
557
|
+
const { precompileIx } = await this.signToPrecompile(message);
|
|
558
|
+
const ix = new web3_js.TransactionInstruction({
|
|
559
|
+
programId: this.programId,
|
|
560
|
+
keys: this.guardedKeys(accountPk),
|
|
561
|
+
data: Buffer.concat([anchorDiscriminator("remove_approver"), Buffer.from(compressed)])
|
|
562
|
+
});
|
|
563
|
+
return [precompileIx, ix];
|
|
564
|
+
}
|
|
565
|
+
/** This chain's leaf for approving `add_signer(newSigner)` at `nonce`:
|
|
566
|
+
* `sha256(compressed(new_signer) || passkey_nonce_le8)`. The batch challenge the
|
|
567
|
+
* passkey signs is `sha256(concat(leaves))` across chains. */
|
|
568
|
+
passkeyLeaf(newSigner, nonce) {
|
|
569
|
+
return sha256.sha256(concatBytes(compressedPubkey(newSigner), u64le(nonce)));
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* `[precompile(passkey), add_signer_via_passkey]` bundle. The precompile ix
|
|
573
|
+
* verifies the PASSKEY's WebAuthn assertion over `authData || sha256(clientDataJSON)`;
|
|
574
|
+
* the program ix binds the challenge to `newSigner` + the passkey nonce and adds
|
|
575
|
+
* the signer. No device signature — a gasless relayer can submit it.
|
|
576
|
+
*/
|
|
577
|
+
buildAddSignerViaPasskey(account, newSigner, passkey, leaves, leafIndex, assertion) {
|
|
578
|
+
const accountPk = new web3_js.PublicKey(account);
|
|
579
|
+
const newCompressed = compressedPubkey(newSigner);
|
|
580
|
+
const passkeyCompressed = compressedPubkey(passkey);
|
|
581
|
+
const clientHash = sha256.sha256(assertion.clientDataJSON);
|
|
582
|
+
const message = concatBytes(assertion.authenticatorData, clientHash);
|
|
583
|
+
const signature = encodeLowSSignature(assertion.r, assertion.s);
|
|
584
|
+
const precompileIx = buildSecp256r1Instruction(passkeyCompressed, signature, message);
|
|
585
|
+
const leavesBlob = Buffer.concat([u32le(leaves.length), ...leaves.map((l) => Buffer.from(l))]);
|
|
586
|
+
const data = Buffer.concat([
|
|
587
|
+
anchorDiscriminator("add_signer_via_passkey"),
|
|
588
|
+
Buffer.from(newCompressed),
|
|
589
|
+
leavesBlob,
|
|
590
|
+
u32le(leafIndex),
|
|
591
|
+
serializeVecU8(assertion.authenticatorData),
|
|
592
|
+
serializeVecU8(assertion.clientDataJSON),
|
|
593
|
+
u32le(assertion.challengeOffset)
|
|
594
|
+
]);
|
|
595
|
+
const ix = new web3_js.TransactionInstruction({
|
|
596
|
+
programId: this.programId,
|
|
597
|
+
keys: this.guardedKeys(accountPk),
|
|
598
|
+
data
|
|
599
|
+
});
|
|
600
|
+
return [precompileIx, ix];
|
|
601
|
+
}
|
|
602
|
+
/** Read whether `passkey` is a registered approver. */
|
|
603
|
+
async isApprover(account, passkey) {
|
|
604
|
+
const approvers = await this.fetchApprovers(new web3_js.PublicKey(account));
|
|
605
|
+
const target = Buffer.from(compressedPubkey(passkey)).toString("hex");
|
|
606
|
+
return approvers.some((a) => Buffer.from(a).toString("hex") === target);
|
|
607
|
+
}
|
|
608
|
+
/** Read the current passkey-approval nonce. */
|
|
609
|
+
async passkeyNonce(account) {
|
|
610
|
+
const info = await this.requireConnection().getAccountInfo(new web3_js.PublicKey(account));
|
|
611
|
+
if (!info) return 0n;
|
|
612
|
+
const d = info.data;
|
|
613
|
+
const signersLenOff = 8 + 32 + 1 + 8 + COMPRESSED_PUBKEY_SIZE;
|
|
614
|
+
const signerCount = d.readUInt32LE(signersLenOff);
|
|
615
|
+
const approversLenOff = signersLenOff + 4 + signerCount * COMPRESSED_PUBKEY_SIZE;
|
|
616
|
+
const approverCount = d.readUInt32LE(approversLenOff);
|
|
617
|
+
const passkeyNonceOff = approversLenOff + 4 + approverCount * COMPRESSED_PUBKEY_SIZE;
|
|
618
|
+
return readU64le(d, passkeyNonceOff);
|
|
619
|
+
}
|
|
620
|
+
/** `[precompile, execute_transfer]` bundle moving lamports out of the account. */
|
|
621
|
+
async buildExecuteTransfer(account, destination, amount) {
|
|
622
|
+
const accountPk = new web3_js.PublicKey(account);
|
|
623
|
+
const destPk = new web3_js.PublicKey(destination);
|
|
624
|
+
const nonce = await this.fetchNonce(accountPk);
|
|
625
|
+
const message = concatBytes(
|
|
626
|
+
Buffer.from(DOMAIN_TRANSFER),
|
|
627
|
+
accountPk.toBuffer(),
|
|
628
|
+
destPk.toBuffer(),
|
|
629
|
+
u64le(amount),
|
|
630
|
+
u64le(nonce)
|
|
631
|
+
);
|
|
632
|
+
const { precompileIx } = await this.signToPrecompile(message);
|
|
633
|
+
const ix = new web3_js.TransactionInstruction({
|
|
634
|
+
programId: this.programId,
|
|
635
|
+
keys: [
|
|
636
|
+
{ pubkey: accountPk, isSigner: false, isWritable: true },
|
|
637
|
+
{ pubkey: destPk, isSigner: false, isWritable: true },
|
|
638
|
+
{ pubkey: web3_js.SYSVAR_INSTRUCTIONS_PUBKEY, isSigner: false, isWritable: false }
|
|
639
|
+
],
|
|
640
|
+
data: Buffer.concat([anchorDiscriminator("execute_transfer"), u64le(amount)])
|
|
641
|
+
});
|
|
642
|
+
return [precompileIx, ix];
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* `[precompile, execute]` bundle running arbitrary CPI instructions with the
|
|
646
|
+
* account PDA as signer. The device key signs over
|
|
647
|
+
* `DOMAIN_EXECUTE || account || sha256(canonical(instructions)) || nonce`, so
|
|
648
|
+
* the signature commits to the EXACT instruction set the program will invoke —
|
|
649
|
+
* no account/data substitution is possible after signing.
|
|
650
|
+
*
|
|
651
|
+
* The instructions' accounts are passed to the program via `remaining_accounts`
|
|
652
|
+
* (flattened, in order); the program enforces an exact, ordered mapping.
|
|
653
|
+
*/
|
|
654
|
+
async buildExecute(account, instructions) {
|
|
655
|
+
if (instructions.length === 0) throw new Error("kit/solana: execute requires at least one instruction");
|
|
656
|
+
const accountPk = new web3_js.PublicKey(account);
|
|
657
|
+
const nonce = await this.fetchNonce(accountPk);
|
|
658
|
+
const blob = serializeInstructions(instructions);
|
|
659
|
+
const ixsHash = sha256.sha256(blob);
|
|
660
|
+
const message = concatBytes(
|
|
661
|
+
Buffer.from(DOMAIN_EXECUTE),
|
|
662
|
+
accountPk.toBuffer(),
|
|
663
|
+
Buffer.from(ixsHash),
|
|
664
|
+
u64le(nonce)
|
|
665
|
+
);
|
|
666
|
+
const { precompileIx } = await this.signToPrecompile(message);
|
|
667
|
+
const blobLen = Buffer.alloc(4);
|
|
668
|
+
new DataView(blobLen.buffer).setUint32(0, blob.length, true);
|
|
669
|
+
const data = Buffer.concat([anchorDiscriminator("execute"), blobLen, blob]);
|
|
670
|
+
const remainingAccounts = [];
|
|
671
|
+
for (const ix2 of instructions) {
|
|
672
|
+
for (const acc of ix2.accounts) {
|
|
673
|
+
remainingAccounts.push({
|
|
674
|
+
pubkey: new web3_js.PublicKey(acc.pubkey),
|
|
675
|
+
isSigner: false,
|
|
676
|
+
// signer flags are part of the signed InstructionData
|
|
677
|
+
isWritable: acc.isWritable
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
remainingAccounts.push({
|
|
681
|
+
pubkey: new web3_js.PublicKey(ix2.programId),
|
|
682
|
+
isSigner: false,
|
|
683
|
+
isWritable: false
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
const ix = new web3_js.TransactionInstruction({
|
|
687
|
+
programId: this.programId,
|
|
688
|
+
keys: [
|
|
689
|
+
{ pubkey: accountPk, isSigner: false, isWritable: true },
|
|
690
|
+
{ pubkey: web3_js.SYSVAR_INSTRUCTIONS_PUBKEY, isSigner: false, isWritable: false },
|
|
691
|
+
...remainingAccounts
|
|
692
|
+
],
|
|
693
|
+
data
|
|
694
|
+
});
|
|
695
|
+
return [precompileIx, ix];
|
|
696
|
+
}
|
|
697
|
+
/** Read whether `signer` is currently an authorized signer of `account`. */
|
|
698
|
+
async isAuthorizedSigner(account, signer) {
|
|
699
|
+
const signers = await this.fetchSigners(new web3_js.PublicKey(account));
|
|
700
|
+
const target = Buffer.from(compressedPubkey(signer)).toString("hex");
|
|
701
|
+
return signers.some((s) => Buffer.from(s).toString("hex") === target);
|
|
702
|
+
}
|
|
703
|
+
guardedKeys(account) {
|
|
704
|
+
return [
|
|
705
|
+
{ pubkey: account, isSigner: false, isWritable: true },
|
|
706
|
+
{ pubkey: web3_js.SYSVAR_INSTRUCTIONS_PUBKEY, isSigner: false, isWritable: false }
|
|
707
|
+
];
|
|
708
|
+
}
|
|
709
|
+
/** Sign `message` with the device key and build the matching precompile ix. */
|
|
710
|
+
async signToPrecompile(message) {
|
|
711
|
+
if (!this.opts.signer) throw new Error("kit/solana: signer required to authorize");
|
|
712
|
+
const pubkey = await this.opts.signer.getPublicKey();
|
|
713
|
+
const sig = await this.opts.signer.sign(message);
|
|
714
|
+
const signature = encodeLowSSignature(sig.r, sig.s);
|
|
715
|
+
const precompileIx = buildSecp256r1Instruction(
|
|
716
|
+
compressedPubkey(pubkey),
|
|
717
|
+
signature,
|
|
718
|
+
message
|
|
719
|
+
);
|
|
720
|
+
return { precompileIx };
|
|
721
|
+
}
|
|
722
|
+
async fetchNonce(account) {
|
|
723
|
+
const info = await this.requireConnection().getAccountInfo(account);
|
|
724
|
+
if (!info) return 0n;
|
|
725
|
+
return readU64le(info.data, 41);
|
|
726
|
+
}
|
|
727
|
+
async fetchSigners(account) {
|
|
728
|
+
const info = await this.requireConnection().getAccountInfo(account);
|
|
729
|
+
if (!info) return [];
|
|
730
|
+
const d = info.data;
|
|
731
|
+
const lenOffset = 8 + 32 + 1 + 8 + COMPRESSED_PUBKEY_SIZE;
|
|
732
|
+
const count = d.readUInt32LE(lenOffset);
|
|
733
|
+
const out = [];
|
|
734
|
+
let off = lenOffset + 4;
|
|
735
|
+
for (let i = 0; i < count; i++) {
|
|
736
|
+
out.push(Uint8Array.from(d.subarray(off, off + COMPRESSED_PUBKEY_SIZE)));
|
|
737
|
+
off += COMPRESSED_PUBKEY_SIZE;
|
|
738
|
+
}
|
|
739
|
+
return out;
|
|
740
|
+
}
|
|
741
|
+
async fetchApprovers(account) {
|
|
742
|
+
const info = await this.requireConnection().getAccountInfo(account);
|
|
743
|
+
if (!info) return [];
|
|
744
|
+
const d = info.data;
|
|
745
|
+
const signersLenOff = 8 + 32 + 1 + 8 + COMPRESSED_PUBKEY_SIZE;
|
|
746
|
+
const signerCount = d.readUInt32LE(signersLenOff);
|
|
747
|
+
const approversLenOff = signersLenOff + 4 + signerCount * COMPRESSED_PUBKEY_SIZE;
|
|
748
|
+
const count = d.readUInt32LE(approversLenOff);
|
|
749
|
+
const out = [];
|
|
750
|
+
let off = approversLenOff + 4;
|
|
751
|
+
for (let i = 0; i < count; i++) {
|
|
752
|
+
out.push(Uint8Array.from(d.subarray(off, off + COMPRESSED_PUBKEY_SIZE)));
|
|
753
|
+
off += COMPRESSED_PUBKEY_SIZE;
|
|
754
|
+
}
|
|
755
|
+
return out;
|
|
756
|
+
}
|
|
757
|
+
requireConnection() {
|
|
758
|
+
if (!this.opts.connection) throw new Error("kit/solana: connection required for reads");
|
|
759
|
+
return this.opts.connection;
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
function compressedPubkey(pk) {
|
|
763
|
+
const out = new Uint8Array(COMPRESSED_PUBKEY_SIZE);
|
|
764
|
+
out[0] = pk.y % 2n === 0n ? 2 : 3;
|
|
765
|
+
out.set(bigIntTo32Bytes(pk.x), 1);
|
|
766
|
+
return out;
|
|
325
767
|
}
|
|
326
|
-
function
|
|
327
|
-
|
|
768
|
+
function encodeLowSSignature(r, s) {
|
|
769
|
+
const lowS2 = s > SECP256R1_N / 2n ? SECP256R1_N - s : s;
|
|
770
|
+
const out = new Uint8Array(SIGNATURE_SIZE);
|
|
771
|
+
out.set(bigIntTo32Bytes(r), 0);
|
|
772
|
+
out.set(bigIntTo32Bytes(lowS2), 32);
|
|
773
|
+
return out;
|
|
328
774
|
}
|
|
329
|
-
function
|
|
330
|
-
|
|
331
|
-
|
|
775
|
+
function buildSecp256r1Instruction(compressed, signature, message) {
|
|
776
|
+
const headerLen = 2;
|
|
777
|
+
const offsetsLen = 14;
|
|
778
|
+
const pubkeyOffset = headerLen + offsetsLen;
|
|
779
|
+
const sigOffset = pubkeyOffset + COMPRESSED_PUBKEY_SIZE;
|
|
780
|
+
const msgOffset = sigOffset + SIGNATURE_SIZE;
|
|
781
|
+
const data = Buffer.alloc(msgOffset + message.length);
|
|
782
|
+
data.writeUInt8(1, 0);
|
|
783
|
+
data.writeUInt8(0, 1);
|
|
784
|
+
let o = headerLen;
|
|
785
|
+
data.writeUInt16LE(sigOffset, o);
|
|
786
|
+
o += 2;
|
|
787
|
+
data.writeUInt16LE(CURRENT_IX, o);
|
|
788
|
+
o += 2;
|
|
789
|
+
data.writeUInt16LE(pubkeyOffset, o);
|
|
790
|
+
o += 2;
|
|
791
|
+
data.writeUInt16LE(CURRENT_IX, o);
|
|
792
|
+
o += 2;
|
|
793
|
+
data.writeUInt16LE(msgOffset, o);
|
|
794
|
+
o += 2;
|
|
795
|
+
data.writeUInt16LE(message.length, o);
|
|
796
|
+
o += 2;
|
|
797
|
+
data.writeUInt16LE(CURRENT_IX, o);
|
|
798
|
+
o += 2;
|
|
799
|
+
Buffer.from(compressed).copy(data, pubkeyOffset);
|
|
800
|
+
Buffer.from(signature).copy(data, sigOffset);
|
|
801
|
+
Buffer.from(message).copy(data, msgOffset);
|
|
802
|
+
return new web3_js.TransactionInstruction({
|
|
803
|
+
keys: [],
|
|
804
|
+
programId: new web3_js.PublicKey(SECP256R1_PROGRAM_ID),
|
|
805
|
+
data
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
function anchorDiscriminator(name) {
|
|
809
|
+
return Buffer.from(sha256.sha256(`global:${name}`).slice(0, 8));
|
|
810
|
+
}
|
|
811
|
+
function u32le(n) {
|
|
812
|
+
const b = Buffer.alloc(4);
|
|
813
|
+
b.writeUInt32LE(n);
|
|
814
|
+
return b;
|
|
815
|
+
}
|
|
816
|
+
function u64le(n) {
|
|
817
|
+
const b = Buffer.alloc(8);
|
|
818
|
+
new DataView(b.buffer, b.byteOffset, 8).setBigUint64(0, BigInt(n), true);
|
|
819
|
+
return b;
|
|
820
|
+
}
|
|
821
|
+
function readU64le(buf2, offset) {
|
|
822
|
+
return new DataView(buf2.buffer, buf2.byteOffset, buf2.length).getBigUint64(
|
|
823
|
+
offset,
|
|
824
|
+
true
|
|
825
|
+
);
|
|
826
|
+
}
|
|
827
|
+
function concatBytes(...parts) {
|
|
828
|
+
const total = parts.reduce((n, p) => n + p.length, 0);
|
|
829
|
+
const out = new Uint8Array(total);
|
|
830
|
+
let off = 0;
|
|
831
|
+
for (const p of parts) {
|
|
832
|
+
out.set(p, off);
|
|
833
|
+
off += p.length;
|
|
332
834
|
}
|
|
333
|
-
return
|
|
835
|
+
return out;
|
|
334
836
|
}
|
|
335
|
-
|
|
837
|
+
function serializeInstruction(ix) {
|
|
838
|
+
const programId = new web3_js.PublicKey(ix.programId).toBuffer();
|
|
839
|
+
const accounts = serializeAccounts(ix.accounts);
|
|
840
|
+
const data = serializeVecU8(ix.data);
|
|
841
|
+
return Buffer.concat([programId, accounts, data]);
|
|
842
|
+
}
|
|
843
|
+
function serializeAccounts(metas) {
|
|
844
|
+
const len = Buffer.alloc(4);
|
|
845
|
+
new DataView(len.buffer).setUint32(0, metas.length, true);
|
|
846
|
+
const parts = metas.map(serializeAccountMeta);
|
|
847
|
+
return Buffer.concat([len, ...parts]);
|
|
848
|
+
}
|
|
849
|
+
function serializeAccountMeta(meta) {
|
|
850
|
+
const pubkey = new web3_js.PublicKey(meta.pubkey).toBuffer();
|
|
851
|
+
return Buffer.concat([pubkey, Buffer.from([meta.isSigner ? 1 : 0, meta.isWritable ? 1 : 0])]);
|
|
852
|
+
}
|
|
853
|
+
function serializeVecU8(data) {
|
|
854
|
+
const len = Buffer.alloc(4);
|
|
855
|
+
new DataView(len.buffer).setUint32(0, data.length, true);
|
|
856
|
+
return Buffer.concat([len, Buffer.from(data)]);
|
|
857
|
+
}
|
|
858
|
+
function serializeInstructions(instructions) {
|
|
859
|
+
return Buffer.concat(instructions.map(serializeInstruction));
|
|
860
|
+
}
|
|
861
|
+
var SolanaRelayer = class {
|
|
336
862
|
constructor(opts) {
|
|
337
863
|
this.opts = opts;
|
|
338
864
|
}
|
|
339
|
-
|
|
340
|
-
|
|
865
|
+
/** The relayer's fee-payer pubkey (fetched + cached from the backend). */
|
|
866
|
+
async getFeePayer() {
|
|
867
|
+
if (this.feePayer) return this.feePayer;
|
|
868
|
+
const res = await fetch(`${this.opts.baseUrl}/api/solana/relay?network=${this.opts.network}`);
|
|
869
|
+
if (!res.ok) throw new Error(`kit/solana: relayer fee-payer lookup failed (${res.status})`);
|
|
870
|
+
const { fee_payer } = await res.json();
|
|
871
|
+
this.feePayer = new web3_js.PublicKey(fee_payer);
|
|
872
|
+
return this.feePayer;
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Build a tx with the relayer as fee payer, serialize it unsigned, and POST it
|
|
876
|
+
* to the relayer to co-sign + submit. Returns the confirmed signature.
|
|
877
|
+
*/
|
|
878
|
+
async send(instructions) {
|
|
879
|
+
const feePayer = await this.getFeePayer();
|
|
880
|
+
const { blockhash } = await this.opts.connection.getLatestBlockhash("confirmed");
|
|
881
|
+
const tx2 = new web3_js.Transaction();
|
|
882
|
+
tx2.feePayer = feePayer;
|
|
883
|
+
tx2.recentBlockhash = blockhash;
|
|
884
|
+
tx2.add(...instructions);
|
|
885
|
+
const serialized = tx2.serialize({ requireAllSignatures: false, verifySignatures: false }).toString("base64");
|
|
886
|
+
const res = await fetch(`${this.opts.baseUrl}/api/solana/relay`, {
|
|
341
887
|
method: "POST",
|
|
342
888
|
headers: { "Content-Type": "application/json" },
|
|
343
889
|
body: JSON.stringify({
|
|
344
890
|
app_id: this.opts.appId,
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
new_pub_y: toHex2(params.newSigner.y),
|
|
348
|
-
device_label: params.deviceLabel ?? deviceLabel(),
|
|
349
|
-
...params.email ? { email: params.email } : {}
|
|
891
|
+
network: this.opts.network,
|
|
892
|
+
transaction: serialized
|
|
350
893
|
})
|
|
351
894
|
});
|
|
352
895
|
if (!res.ok) {
|
|
353
|
-
const
|
|
354
|
-
throw new Error(`
|
|
355
|
-
}
|
|
356
|
-
const data = await res.json();
|
|
357
|
-
return { requestId: data.request_id };
|
|
358
|
-
}
|
|
359
|
-
async getPendingRequest(requestId) {
|
|
360
|
-
const url = new URL("/api/devices/request", this.opts.baseUrl);
|
|
361
|
-
url.searchParams.set("id", requestId);
|
|
362
|
-
const res = await fetch(url, { headers: { "Content-Type": "application/json" } });
|
|
363
|
-
if (!res.ok) throw new Error(`getPendingRequest failed: ${res.status}`);
|
|
364
|
-
const data = await res.json();
|
|
365
|
-
if (!data.found) return null;
|
|
366
|
-
const status = data.status;
|
|
367
|
-
return {
|
|
368
|
-
requestId: data.request_id,
|
|
369
|
-
appId: data.app_id,
|
|
370
|
-
userId: "",
|
|
371
|
-
// the approving device already knows its own identity
|
|
372
|
-
accountAddress: data.wallet_address,
|
|
373
|
-
newSigner: { x: fromHex2(data.new_pub_x), y: fromHex2(data.new_pub_y) },
|
|
374
|
-
createdAt: data.created_at,
|
|
375
|
-
status
|
|
376
|
-
};
|
|
377
|
-
}
|
|
378
|
-
async confirmDeviceAddition(params) {
|
|
379
|
-
const res = await fetch(
|
|
380
|
-
new URL(`/api/devices/request/${params.requestId}/confirm`, this.opts.baseUrl),
|
|
381
|
-
{
|
|
382
|
-
method: "POST",
|
|
383
|
-
headers: { "Content-Type": "application/json" },
|
|
384
|
-
body: JSON.stringify({ tx_hash: params.txHash })
|
|
385
|
-
}
|
|
386
|
-
);
|
|
387
|
-
if (!res.ok) {
|
|
388
|
-
const t = await res.text().catch(() => "");
|
|
389
|
-
throw new Error(`confirmDeviceAddition failed: ${res.status} ${t}`);
|
|
896
|
+
const detail = await res.text().catch(() => "");
|
|
897
|
+
throw new Error(`kit/solana: relay failed (${res.status}) ${detail}`);
|
|
390
898
|
}
|
|
899
|
+
const { signature } = await res.json();
|
|
900
|
+
return signature;
|
|
391
901
|
}
|
|
392
902
|
};
|
|
393
903
|
var BACKUP_KDF_SALT = "cavos-recovery-v1";
|
|
@@ -697,122 +1207,1196 @@ var WORDLIST = [
|
|
|
697
1207
|
"beach",
|
|
698
1208
|
"dusk"
|
|
699
1209
|
];
|
|
700
|
-
function
|
|
701
|
-
|
|
702
|
-
|
|
1210
|
+
function base64urlEncode(bytes) {
|
|
1211
|
+
let bin = "";
|
|
1212
|
+
for (const b of bytes) bin += String.fromCharCode(b);
|
|
1213
|
+
const b64 = typeof btoa !== "undefined" ? btoa(bin) : Buffer.from(bytes).toString("base64");
|
|
1214
|
+
return b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
703
1215
|
}
|
|
704
|
-
function
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
1216
|
+
function derToRs(der) {
|
|
1217
|
+
let i = 0;
|
|
1218
|
+
if (der[i++] !== 48) throw new Error("kit/webauthn: bad DER (no SEQUENCE)");
|
|
1219
|
+
if (der[i] & 128) i += 1 + (der[i] & 127);
|
|
1220
|
+
else i += 1;
|
|
1221
|
+
if (der[i++] !== 2) throw new Error("kit/webauthn: bad DER (no r INTEGER)");
|
|
1222
|
+
const rlen = der[i++];
|
|
1223
|
+
const r = bytesToBigInt(der.subarray(i, i + rlen));
|
|
1224
|
+
i += rlen;
|
|
1225
|
+
if (der[i++] !== 2) throw new Error("kit/webauthn: bad DER (no s INTEGER)");
|
|
1226
|
+
const slen = der[i++];
|
|
1227
|
+
const s = bytesToBigInt(der.subarray(i, i + slen));
|
|
1228
|
+
return { r, s };
|
|
1229
|
+
}
|
|
1230
|
+
function spkiToPublicKey(spki) {
|
|
1231
|
+
const idx = spki.lastIndexOf(4, spki.length - 65);
|
|
1232
|
+
const start = spki.length - 65;
|
|
1233
|
+
const prefix = spki[start];
|
|
1234
|
+
if (prefix !== 4) {
|
|
1235
|
+
if (idx < 0) throw new Error("kit/webauthn: no uncompressed EC point in SPKI");
|
|
1236
|
+
return { x: bytesToBigInt(spki.subarray(idx + 1, idx + 33)), y: bytesToBigInt(spki.subarray(idx + 33, idx + 65)) };
|
|
711
1237
|
}
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
1238
|
+
return {
|
|
1239
|
+
x: bytesToBigInt(spki.subarray(start + 1, start + 33)),
|
|
1240
|
+
y: bytesToBigInt(spki.subarray(start + 33, start + 65))
|
|
1241
|
+
};
|
|
1242
|
+
}
|
|
1243
|
+
function batchChallenge(leaves) {
|
|
1244
|
+
const total = leaves.reduce((n, l) => n + l.length, 0);
|
|
1245
|
+
const cat = new Uint8Array(total);
|
|
1246
|
+
let o = 0;
|
|
1247
|
+
for (const l of leaves) {
|
|
1248
|
+
cat.set(l, o);
|
|
1249
|
+
o += l.length;
|
|
1250
|
+
}
|
|
1251
|
+
return sha256.sha256(cat);
|
|
1252
|
+
}
|
|
1253
|
+
function webauthnDigest(authenticatorData, clientDataJSON) {
|
|
1254
|
+
const clientHash = sha256.sha256(clientDataJSON);
|
|
1255
|
+
const msg = new Uint8Array(authenticatorData.length + clientHash.length);
|
|
1256
|
+
msg.set(authenticatorData, 0);
|
|
1257
|
+
msg.set(clientHash, authenticatorData.length);
|
|
1258
|
+
return sha256.sha256(msg);
|
|
1259
|
+
}
|
|
1260
|
+
function recoverCandidatePublicKeys(r, s, digest) {
|
|
1261
|
+
const out = [];
|
|
1262
|
+
for (const bit of [0, 1]) {
|
|
1263
|
+
try {
|
|
1264
|
+
const point = new p256.p256.Signature(r, s).addRecoveryBit(bit).recoverPublicKey(digest).toAffine();
|
|
1265
|
+
out.push({ publicKey: { x: point.x, y: point.y }, yParity: bit === 1 });
|
|
1266
|
+
} catch {
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
return out;
|
|
1270
|
+
}
|
|
1271
|
+
function lowS(s) {
|
|
1272
|
+
const n = p256.p256.CURVE.n;
|
|
1273
|
+
return s > n / 2n ? n - s : s;
|
|
1274
|
+
}
|
|
1275
|
+
function challengeOffsetOf(clientDataJSON, challengeB64) {
|
|
1276
|
+
const text = new TextDecoder().decode(clientDataJSON);
|
|
1277
|
+
const idx = text.indexOf(challengeB64);
|
|
1278
|
+
if (idx < 0) throw new Error("kit/webauthn: challenge not found in clientDataJSON");
|
|
1279
|
+
return idx;
|
|
715
1280
|
}
|
|
716
1281
|
|
|
717
|
-
// src/
|
|
718
|
-
var
|
|
719
|
-
constructor(identity, address, status,
|
|
1282
|
+
// src/chains/solana/CavosSolana.ts
|
|
1283
|
+
var CavosSolana = class _CavosSolana {
|
|
1284
|
+
constructor(identity, address, status, connection, adapter, devicePubkey, relayer, feePayer) {
|
|
720
1285
|
this.identity = identity;
|
|
721
1286
|
this.address = address;
|
|
722
1287
|
this.status = status;
|
|
723
|
-
this.
|
|
1288
|
+
this.connection = connection;
|
|
724
1289
|
this.adapter = adapter;
|
|
725
1290
|
this.devicePubkey = devicePubkey;
|
|
726
|
-
|
|
727
|
-
this.
|
|
1291
|
+
this.relayer = relayer;
|
|
1292
|
+
this.feePayer = feePayer;
|
|
1293
|
+
/** Discriminant for the `CavosWallet` union — narrows `execute()` per chain. */
|
|
1294
|
+
this.chain = "solana";
|
|
1295
|
+
}
|
|
1296
|
+
get publicKey() {
|
|
1297
|
+
return this.devicePubkey;
|
|
728
1298
|
}
|
|
729
1299
|
static async connect(opts) {
|
|
730
1300
|
const identity = opts.identity ?? await opts.auth?.authenticate();
|
|
731
|
-
if (!identity) throw new Error("kit: connect requires `identity` or `auth`");
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
}
|
|
737
|
-
const
|
|
738
|
-
nodeUrl: opts.paymasterUrl ?? CAVOS_PAYMASTER_URL[opts.network],
|
|
739
|
-
headers: { "x-paymaster-api-key": opts.paymasterApiKey }
|
|
740
|
-
});
|
|
741
|
-
const addressSeed = deriveAddressSeed({ userId: identity.userId, appSalt: opts.appSalt });
|
|
1301
|
+
if (!identity) throw new Error("kit/solana: connect requires `identity` or `auth`");
|
|
1302
|
+
if (opts.network === "solana-mainnet" && !opts.rpcUrl) {
|
|
1303
|
+
console.warn(
|
|
1304
|
+
"[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."
|
|
1305
|
+
);
|
|
1306
|
+
}
|
|
1307
|
+
const connection = new web3_js.Connection(opts.rpcUrl ?? SOLANA_NETWORKS[opts.network], "confirmed");
|
|
742
1308
|
const signer = opts.createSigner ? await opts.createSigner(`${identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${identity.userId}:${opts.appSalt}` });
|
|
743
1309
|
const devicePubkey = await signer.getPublicKey();
|
|
744
|
-
const adapter = new
|
|
745
|
-
const
|
|
746
|
-
provider,
|
|
747
|
-
address: address2,
|
|
748
|
-
signer: new StarknetDeviceSigner(signer),
|
|
749
|
-
paymaster,
|
|
750
|
-
cairoVersion: "1"
|
|
751
|
-
});
|
|
1310
|
+
const adapter = new SolanaAdapter({ programId: opts.programId, connection, signer });
|
|
1311
|
+
const addressSeed = deriveAddressSeedSolana({ userId: identity.userId, appSalt: opts.appSalt });
|
|
752
1312
|
const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
|
|
753
1313
|
const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry);
|
|
754
|
-
const
|
|
1314
|
+
const relayer = opts.relayer ?? (opts.appId ? new SolanaRelayer({ baseUrl: backendUrl, appId: opts.appId, network: opts.network, connection }) : void 0);
|
|
755
1315
|
const existing = await registry.lookup(identity.userId);
|
|
756
1316
|
if (existing) {
|
|
757
|
-
const account2 = makeAccount(existing.address);
|
|
758
1317
|
const isSigner2 = await adapter.isAuthorizedSigner(existing.address, devicePubkey);
|
|
759
|
-
|
|
1318
|
+
return new _CavosSolana(
|
|
760
1319
|
identity,
|
|
761
1320
|
existing.address,
|
|
762
1321
|
isSigner2 ? "ready" : "needs-device-approval",
|
|
763
|
-
|
|
1322
|
+
connection,
|
|
764
1323
|
adapter,
|
|
765
|
-
devicePubkey
|
|
1324
|
+
devicePubkey,
|
|
1325
|
+
relayer,
|
|
1326
|
+
opts.feePayer
|
|
766
1327
|
);
|
|
767
|
-
if (!isSigner2 && recovery) {
|
|
768
|
-
const dedup = lastDeviceRequest.get(identity.userId);
|
|
769
|
-
const fresh = dedup && Date.now() - dedup.requestedAt < DEVICE_REQUEST_DEDUP_MS;
|
|
770
|
-
try {
|
|
771
|
-
if (fresh) {
|
|
772
|
-
cavos.pendingRequestId = dedup.requestId;
|
|
773
|
-
} else {
|
|
774
|
-
const { requestId } = await recovery.requestDeviceAddition({
|
|
775
|
-
userId: identity.userId,
|
|
776
|
-
accountAddress: existing.address,
|
|
777
|
-
newSigner: devicePubkey,
|
|
778
|
-
...identity.email ? { email: identity.email } : {}
|
|
779
|
-
});
|
|
780
|
-
cavos.pendingRequestId = requestId;
|
|
781
|
-
lastDeviceRequest.set(identity.userId, { requestId, requestedAt: Date.now() });
|
|
782
|
-
}
|
|
783
|
-
} catch (e) {
|
|
784
|
-
console.warn("[Cavos] requestDeviceAddition failed:", e);
|
|
785
|
-
}
|
|
786
|
-
}
|
|
787
|
-
return cavos;
|
|
788
1328
|
}
|
|
789
|
-
const address = adapter.computeAddress(
|
|
790
|
-
const
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
}
|
|
800
|
-
|
|
801
|
-
feeMode: { mode: "sponsored" },
|
|
802
|
-
deploymentData
|
|
803
|
-
});
|
|
804
|
-
try {
|
|
805
|
-
await provider.waitForTransaction(deployRes.transaction_hash);
|
|
806
|
-
} catch (e) {
|
|
807
|
-
console.warn("[Cavos] deploy receipt wait failed:", e);
|
|
1329
|
+
const address = adapter.computeAddress(addressSeed, devicePubkey);
|
|
1330
|
+
const deployed = await connection.getAccountInfo(new web3_js.PublicKey(address)) !== null;
|
|
1331
|
+
if (!deployed) {
|
|
1332
|
+
if (relayer) {
|
|
1333
|
+
const payer = await relayer.getFeePayer();
|
|
1334
|
+
const ix = adapter.buildInitialize(addressSeed, payer.toBase58(), devicePubkey);
|
|
1335
|
+
await relayer.send([ix]);
|
|
1336
|
+
} else if (opts.feePayer) {
|
|
1337
|
+
const ix = adapter.buildInitialize(addressSeed, opts.feePayer.publicKey.toBase58(), devicePubkey);
|
|
1338
|
+
await web3_js.sendAndConfirmTransaction(connection, new web3_js.Transaction().add(ix), [opts.feePayer]);
|
|
1339
|
+
} else {
|
|
1340
|
+
throw new Error("kit/solana: a relayer (appId) or feePayer is required to initialize a new account");
|
|
808
1341
|
}
|
|
809
1342
|
}
|
|
810
1343
|
await registry.register({ userId: identity.userId, address, initialSigner: devicePubkey });
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
1344
|
+
const isSigner = await adapter.isAuthorizedSigner(address, devicePubkey);
|
|
1345
|
+
return new _CavosSolana(
|
|
1346
|
+
identity,
|
|
1347
|
+
address,
|
|
1348
|
+
isSigner ? "ready" : "needs-device-approval",
|
|
1349
|
+
connection,
|
|
1350
|
+
adapter,
|
|
1351
|
+
devicePubkey,
|
|
1352
|
+
relayer,
|
|
1353
|
+
opts.feePayer
|
|
1354
|
+
);
|
|
1355
|
+
}
|
|
1356
|
+
/** Authorize an additional device signer (device-signed via precompile). */
|
|
1357
|
+
async addSigner(pubkey) {
|
|
1358
|
+
const ixs = await this.adapter.buildAddSigner(this.address, pubkey);
|
|
1359
|
+
return this.send(ixs);
|
|
1360
|
+
}
|
|
1361
|
+
/**
|
|
1362
|
+
* Enroll a passkey as an approver (2FA-style step-up). Device-signed + gasless;
|
|
1363
|
+
* requires a ready device. Idempotent. Returns the passkey pubkey + tx hash.
|
|
1364
|
+
*/
|
|
1365
|
+
async enrollPasskey(passkey, params) {
|
|
1366
|
+
const enrolled = await passkey.enroll(params);
|
|
1367
|
+
const { transactionHash } = await this.addApprover(enrolled.publicKey);
|
|
1368
|
+
return { publicKey: enrolled.publicKey, transactionHash };
|
|
1369
|
+
}
|
|
1370
|
+
/** Register an already-enrolled passkey pubkey as an approver (gasless).
|
|
1371
|
+
* Idempotent. Lets one passkey be registered across chains without re-prompting. */
|
|
1372
|
+
async addApprover(pubkey) {
|
|
1373
|
+
if (this.status !== "ready") {
|
|
1374
|
+
throw new Error("kit/solana: addApprover requires a ready, authorized device");
|
|
1375
|
+
}
|
|
1376
|
+
if (await this.adapter.isApprover(this.address, pubkey)) return {};
|
|
1377
|
+
const ixs = await this.adapter.buildAddApprover(this.address, pubkey);
|
|
1378
|
+
const transactionHash = await this.send(ixs);
|
|
1379
|
+
return { transactionHash };
|
|
1380
|
+
}
|
|
1381
|
+
/**
|
|
1382
|
+
* From a fresh browser (status `needs-device-approval`), approve adding THIS
|
|
1383
|
+
* device with the user's synced passkey. Gasless via the relayer — the bundle
|
|
1384
|
+
* carries the passkey's WebAuthn assertion, so no device signature is needed.
|
|
1385
|
+
*/
|
|
1386
|
+
async approveThisDeviceWithPasskey(passkey) {
|
|
1387
|
+
if (this.status === "ready") {
|
|
1388
|
+
throw new Error("kit/solana: this device is already an authorized signer");
|
|
1389
|
+
}
|
|
1390
|
+
const { leaf, nonce } = await this.passkeyLeafForThisDevice();
|
|
1391
|
+
const leaves = [leaf];
|
|
1392
|
+
const assertion = await passkey.assert(batchChallenge(leaves));
|
|
1393
|
+
const { transactionHash } = await this.submitPasskeyApproval(assertion, leaves, 0, nonce);
|
|
1394
|
+
return transactionHash;
|
|
1395
|
+
}
|
|
1396
|
+
/** This device's leaf + passkey nonce for a (possibly multi-chain) batch. */
|
|
1397
|
+
async passkeyLeafForThisDevice() {
|
|
1398
|
+
const nonce = await this.adapter.passkeyNonce(this.address);
|
|
1399
|
+
return { leaf: this.adapter.passkeyLeaf(this.devicePubkey, nonce), nonce };
|
|
1400
|
+
}
|
|
1401
|
+
/** Submit `add_signer_via_passkey` given a shared assertion + batch position.
|
|
1402
|
+
* Used by `approveThisDeviceWithPasskey` and `approveDeviceEverywhere`. */
|
|
1403
|
+
async submitPasskeyApproval(assertion, leaves, leafIndex, _nonce) {
|
|
1404
|
+
const digest = webauthnDigest(assertion.authenticatorData, assertion.clientDataJSON);
|
|
1405
|
+
const candidates = recoverCandidatePublicKeys(assertion.r, assertion.s, digest);
|
|
1406
|
+
let approver = null;
|
|
1407
|
+
for (const cand of candidates) {
|
|
1408
|
+
if (await this.adapter.isApprover(this.address, cand.publicKey)) {
|
|
1409
|
+
approver = cand.publicKey;
|
|
1410
|
+
break;
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
if (!approver) throw new Error("kit/solana: this passkey is not a registered approver");
|
|
1414
|
+
const ixs = this.adapter.buildAddSignerViaPasskey(
|
|
1415
|
+
this.address,
|
|
1416
|
+
this.devicePubkey,
|
|
1417
|
+
approver,
|
|
1418
|
+
leaves,
|
|
1419
|
+
leafIndex,
|
|
1420
|
+
assertion
|
|
1421
|
+
);
|
|
1422
|
+
return { transactionHash: await this.send(ixs) };
|
|
1423
|
+
}
|
|
1424
|
+
/** Move `amount` lamports out of the account to `destination` (device-signed). */
|
|
1425
|
+
async execute(amount, destination) {
|
|
1426
|
+
if (this.status !== "ready") {
|
|
1427
|
+
throw new Error("kit/solana: this device is not yet an authorized signer of the wallet");
|
|
1428
|
+
}
|
|
1429
|
+
const ixs = await this.adapter.buildExecuteTransfer(this.address, destination, amount);
|
|
1430
|
+
return this.send(ixs);
|
|
1431
|
+
}
|
|
1432
|
+
/**
|
|
1433
|
+
* Run arbitrary CPI `instructions` with the account PDA as signer (device-
|
|
1434
|
+
* signed). The signature commits to sha256 of the canonical Borsh
|
|
1435
|
+
* serialization of the instructions, so it binds exactly the operations the
|
|
1436
|
+
* program will invoke. Unlocks SPL transfers, swaps, staking, etc.
|
|
1437
|
+
*
|
|
1438
|
+
* What the relayer will sponsor is constrained by the app's Solana program
|
|
1439
|
+
* allowlist (configured in the dashboard) — programs outside the allowlist are
|
|
1440
|
+
* rejected before co-signing.
|
|
1441
|
+
*/
|
|
1442
|
+
async executeInstructions(instructions) {
|
|
1443
|
+
if (this.status !== "ready") {
|
|
1444
|
+
throw new Error("kit/solana: this device is not yet an authorized signer of the wallet");
|
|
1445
|
+
}
|
|
1446
|
+
const ixs = await this.adapter.buildExecute(this.address, instructions);
|
|
1447
|
+
return this.send(ixs);
|
|
1448
|
+
}
|
|
1449
|
+
/**
|
|
1450
|
+
* Register the backup signer derived from `code` as an authorized signer of this
|
|
1451
|
+
* account (device-signed via precompile). Idempotent: returns without a tx if
|
|
1452
|
+
* the backup signer is already registered. The code never leaves the device —
|
|
1453
|
+
* only the derived public key travels on-chain.
|
|
1454
|
+
*
|
|
1455
|
+
* Self-custodial: anyone who can re-derive the backup key from the code (i.e.
|
|
1456
|
+
* the rightful owner) can later recover the account with `CavosSolana.recover`.
|
|
1457
|
+
* Run this once, on a registered device, and have the user store the code.
|
|
1458
|
+
*/
|
|
1459
|
+
async setupRecovery(code) {
|
|
1460
|
+
if (this.status !== "ready") {
|
|
1461
|
+
throw new Error("kit/solana: setupRecovery requires a ready, registered device");
|
|
1462
|
+
}
|
|
1463
|
+
const { publicKey: backupPubkey } = deriveBackupKey(code);
|
|
1464
|
+
const already = await this.adapter.isAuthorizedSigner(this.address, backupPubkey);
|
|
1465
|
+
if (already) return void 0;
|
|
1466
|
+
return this.addSigner(backupPubkey);
|
|
1467
|
+
}
|
|
1468
|
+
/**
|
|
1469
|
+
* Recover an account after losing every device signer. Derives the backup key
|
|
1470
|
+
* from `code`, uses it (not the new device key) to sign an `add_signer` for the
|
|
1471
|
+
* new device, and returns a ready CavosSolana bound to the new device. The
|
|
1472
|
+
* account address is unchanged.
|
|
1473
|
+
*
|
|
1474
|
+
* Self-custodial: only someone holding the code (i.e. the rightful owner) can
|
|
1475
|
+
* re-derive the backup key. The backend never sees the code.
|
|
1476
|
+
*
|
|
1477
|
+
* This mirrors `Cavos.recover` (Starknet): the backup key is just another
|
|
1478
|
+
* authorized signer, so recovery is an `add_signer(newDevice)` bundle signed by
|
|
1479
|
+
* the backup key. The on-chain program needs no recovery-specific entrypoint.
|
|
1480
|
+
*/
|
|
1481
|
+
static async recover(opts) {
|
|
1482
|
+
if (opts.network === "solana-mainnet" && !opts.rpcUrl) {
|
|
1483
|
+
console.warn(
|
|
1484
|
+
"[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."
|
|
1485
|
+
);
|
|
1486
|
+
}
|
|
1487
|
+
const connection = new web3_js.Connection(opts.rpcUrl ?? SOLANA_NETWORKS[opts.network], "confirmed");
|
|
1488
|
+
const signer = opts.createSigner ? await opts.createSigner(`${opts.identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${opts.identity.userId}:${opts.appSalt}` });
|
|
1489
|
+
const devicePubkey = await signer.getPublicKey();
|
|
1490
|
+
const backup = BackupSigner.fromCode(opts.code);
|
|
1491
|
+
const backupAdapter = new SolanaAdapter({
|
|
1492
|
+
programId: opts.programId,
|
|
1493
|
+
connection,
|
|
1494
|
+
signer: backup
|
|
1495
|
+
});
|
|
1496
|
+
const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
|
|
1497
|
+
const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry);
|
|
1498
|
+
const existing = await registry.lookup(opts.identity.userId);
|
|
1499
|
+
if (!existing) {
|
|
1500
|
+
throw new Error("kit/solana: no account found for this identity \u2014 nothing to recover");
|
|
1501
|
+
}
|
|
1502
|
+
const relayer = opts.relayer ?? (opts.appId ? new SolanaRelayer({ baseUrl: backendUrl, appId: opts.appId, network: opts.network, connection }) : void 0);
|
|
1503
|
+
const alreadyAuthed = await backupAdapter.isAuthorizedSigner(existing.address, devicePubkey);
|
|
1504
|
+
if (!alreadyAuthed) {
|
|
1505
|
+
const ixs = await backupAdapter.buildAddSigner(existing.address, devicePubkey);
|
|
1506
|
+
if (relayer) {
|
|
1507
|
+
await relayer.send(ixs);
|
|
1508
|
+
} else if (opts.feePayer) {
|
|
1509
|
+
await web3_js.sendAndConfirmTransaction(connection, new web3_js.Transaction().add(...ixs), [opts.feePayer]);
|
|
1510
|
+
} else {
|
|
1511
|
+
throw new Error("kit/solana: a relayer (appId) or feePayer is required to recover");
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
const adapter = new SolanaAdapter({ programId: opts.programId, connection, signer });
|
|
1515
|
+
return new _CavosSolana(
|
|
1516
|
+
opts.identity,
|
|
1517
|
+
existing.address,
|
|
1518
|
+
"ready",
|
|
1519
|
+
connection,
|
|
1520
|
+
adapter,
|
|
1521
|
+
devicePubkey,
|
|
1522
|
+
relayer,
|
|
1523
|
+
opts.feePayer
|
|
1524
|
+
);
|
|
1525
|
+
}
|
|
1526
|
+
async send(ixs) {
|
|
1527
|
+
if (this.relayer) return this.relayer.send(ixs);
|
|
1528
|
+
if (this.feePayer) {
|
|
1529
|
+
return web3_js.sendAndConfirmTransaction(this.connection, new web3_js.Transaction().add(...ixs), [this.feePayer]);
|
|
1530
|
+
}
|
|
1531
|
+
throw new Error("kit/solana: no relayer or feePayer configured to submit transactions");
|
|
1532
|
+
}
|
|
1533
|
+
};
|
|
1534
|
+
var defaultRegistry = new InMemoryWalletRegistry();
|
|
1535
|
+
|
|
1536
|
+
// src/chains/stellar/constants.ts
|
|
1537
|
+
var FACTORY_CONTRACT_ID = {
|
|
1538
|
+
// Re-deployed 2026-07-01 with the passkey-approval device-account wasm (batched
|
|
1539
|
+
// multi-chain challenge). The factory pins the wasm hash immutably, so a new
|
|
1540
|
+
// wasm needs a new factory → new account addresses; testnet has no prod wallets.
|
|
1541
|
+
"stellar-testnet": "CBCJIODXIEBOXXD66KCUCF7ZDYJARKI4ZIVQOVWPULOBH5XGNCDP6W3I",
|
|
1542
|
+
// Set once the factory is deployed to mainnet (its address differs — network id
|
|
1543
|
+
// is part of contract-address derivation).
|
|
1544
|
+
"stellar-mainnet": ""
|
|
1545
|
+
};
|
|
1546
|
+
var DEVICE_ACCOUNT_WASM_HASH = {
|
|
1547
|
+
"stellar-testnet": "2671b085578e59a385ef5a5664e42f0450322fe3249539f588e1263ed5a31dce",
|
|
1548
|
+
"stellar-mainnet": ""
|
|
1549
|
+
};
|
|
1550
|
+
var STELLAR_NETWORKS = {
|
|
1551
|
+
"stellar-testnet": {
|
|
1552
|
+
rpcUrl: "https://soroban-testnet.stellar.org",
|
|
1553
|
+
passphrase: "Test SDF Network ; September 2015"
|
|
1554
|
+
},
|
|
1555
|
+
"stellar-mainnet": {
|
|
1556
|
+
rpcUrl: "https://soroban-rpc.mainnet.stellar.gateway.fm",
|
|
1557
|
+
passphrase: "Public Global Stellar Network ; September 2015"
|
|
1558
|
+
}
|
|
1559
|
+
};
|
|
1560
|
+
var NATIVE_SAC_ID = {
|
|
1561
|
+
"stellar-testnet": "CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC",
|
|
1562
|
+
"stellar-mainnet": "CAS3J7GYLGXMF6TDJBBYYSE3HQ6BBSMLNUQ34T6TZMYMW2EVH34XOWMA"
|
|
1563
|
+
};
|
|
1564
|
+
|
|
1565
|
+
// src/chains/stellar/StellarAdapter.ts
|
|
1566
|
+
var SECP256R1_N2 = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n;
|
|
1567
|
+
var StellarAdapter = class {
|
|
1568
|
+
constructor(opts) {
|
|
1569
|
+
this.chain = "stellar";
|
|
1570
|
+
this.network = opts.network;
|
|
1571
|
+
this.passphrase = STELLAR_NETWORKS[opts.network].passphrase;
|
|
1572
|
+
this.rpcUrl = opts.rpcUrl ?? STELLAR_NETWORKS[opts.network].rpcUrl;
|
|
1573
|
+
this.factoryId = opts.factoryId ?? FACTORY_CONTRACT_ID[opts.network];
|
|
1574
|
+
if (!this.factoryId) {
|
|
1575
|
+
throw new Error(`kit/stellar: no factory contract id configured for ${opts.network}`);
|
|
1576
|
+
}
|
|
1577
|
+
this.signer = opts.signer;
|
|
1578
|
+
}
|
|
1579
|
+
server() {
|
|
1580
|
+
if (!this._server) {
|
|
1581
|
+
this._server = new stellarSdk.rpc.Server(this.rpcUrl, {
|
|
1582
|
+
allowHttp: this.rpcUrl.startsWith("http://")
|
|
1583
|
+
});
|
|
1584
|
+
}
|
|
1585
|
+
return this._server;
|
|
1586
|
+
}
|
|
1587
|
+
networkId() {
|
|
1588
|
+
return stellarSdk.hash(Buffer.from(this.passphrase));
|
|
1589
|
+
}
|
|
1590
|
+
/**
|
|
1591
|
+
* Deterministic account address for `(addressSeed, initialSigner)` — computed
|
|
1592
|
+
* off-chain, byte-identical to the factory's on-chain `account_address`.
|
|
1593
|
+
* `contractId = sha256(HashIdPreimage(networkId, factory, salt))` with
|
|
1594
|
+
* `salt = sha256(addressSeed || sec1(initialSigner))`.
|
|
1595
|
+
*/
|
|
1596
|
+
computeAddress(addressSeed, initialSigner) {
|
|
1597
|
+
const salt = this.accountSalt(addressSeed, initialSigner);
|
|
1598
|
+
const preimage = stellarSdk.xdr.HashIdPreimage.envelopeTypeContractId(
|
|
1599
|
+
new stellarSdk.xdr.HashIdPreimageContractId({
|
|
1600
|
+
networkId: this.networkId(),
|
|
1601
|
+
contractIdPreimage: stellarSdk.xdr.ContractIdPreimage.contractIdPreimageFromAddress(
|
|
1602
|
+
new stellarSdk.xdr.ContractIdPreimageFromAddress({
|
|
1603
|
+
address: new stellarSdk.Address(this.factoryId).toScAddress(),
|
|
1604
|
+
salt
|
|
1605
|
+
})
|
|
1606
|
+
)
|
|
1607
|
+
})
|
|
1608
|
+
);
|
|
1609
|
+
return stellarSdk.StrKey.encodeContract(stellarSdk.hash(preimage.toXDR()));
|
|
1610
|
+
}
|
|
1611
|
+
/** `salt = sha256(addressSeed(32) || sec1(initialSigner)(65))` — matches the factory. */
|
|
1612
|
+
accountSalt(addressSeed, initialSigner) {
|
|
1613
|
+
return stellarSdk.hash(Buffer.concat([Buffer.from(addressSeed), Buffer.from(sec1Pubkey(initialSigner))]));
|
|
1614
|
+
}
|
|
1615
|
+
/** Host function: `factory.deploy(address_seed, initial_signer)`. */
|
|
1616
|
+
buildDeploy(addressSeed, initialSigner) {
|
|
1617
|
+
return invokeFunc(this.factoryId, "deploy", [
|
|
1618
|
+
bytesScVal(addressSeed),
|
|
1619
|
+
bytesScVal(sec1Pubkey(initialSigner))
|
|
1620
|
+
]);
|
|
1621
|
+
}
|
|
1622
|
+
/** Host function: `account.add_signer(new_signer)` (requires device auth). */
|
|
1623
|
+
buildAddSigner(accountAddress, signer) {
|
|
1624
|
+
return invokeFunc(accountAddress, "add_signer", [bytesScVal(sec1Pubkey(signer))]);
|
|
1625
|
+
}
|
|
1626
|
+
/** Host function: `account.remove_signer(signer)` (requires device auth). */
|
|
1627
|
+
buildRemoveSigner(accountAddress, signer) {
|
|
1628
|
+
return invokeFunc(accountAddress, "remove_signer", [bytesScVal(sec1Pubkey(signer))]);
|
|
1629
|
+
}
|
|
1630
|
+
/** Host function: `account.add_approver(passkey)` (requires device auth). */
|
|
1631
|
+
buildAddApprover(accountAddress, passkey) {
|
|
1632
|
+
return invokeFunc(accountAddress, "add_approver", [bytesScVal(sec1Pubkey(passkey))]);
|
|
1633
|
+
}
|
|
1634
|
+
/** Host function: `account.remove_approver(passkey)` (requires device auth). */
|
|
1635
|
+
buildRemoveApprover(accountAddress, passkey) {
|
|
1636
|
+
return invokeFunc(accountAddress, "remove_approver", [bytesScVal(sec1Pubkey(passkey))]);
|
|
1637
|
+
}
|
|
1638
|
+
/** This chain's leaf for approving `add_signer(newSigner)` at `nonce`:
|
|
1639
|
+
* `sha256(sec1(new_signer) || nonce_be8)`. The batch challenge the passkey signs
|
|
1640
|
+
* is `sha256(concat(leaves))` across chains. */
|
|
1641
|
+
passkeyLeaf(newSigner, nonce) {
|
|
1642
|
+
const msg = new Uint8Array(65 + 8);
|
|
1643
|
+
msg.set(sec1Pubkey(newSigner), 0);
|
|
1644
|
+
const n = new Uint8Array(8);
|
|
1645
|
+
let v = nonce;
|
|
1646
|
+
for (let i = 7; i >= 0; i--) {
|
|
1647
|
+
n[i] = Number(v & 0xffn);
|
|
1648
|
+
v >>= 8n;
|
|
1649
|
+
}
|
|
1650
|
+
msg.set(n, 65);
|
|
1651
|
+
return sha256.sha256(msg);
|
|
1652
|
+
}
|
|
1653
|
+
/** Host function: passkey-authorized `add_signer_via_passkey` (no device auth —
|
|
1654
|
+
* authorized by the embedded WebAuthn assertion, so any relayer can submit).
|
|
1655
|
+
* `leaves`/`leafIndex` place this chain's leaf in the multi-chain batch. */
|
|
1656
|
+
buildAddSignerViaPasskey(accountAddress, newSigner, passkey, nonce, leaves, leafIndex, assertion) {
|
|
1657
|
+
const sig = encodeLowSSignature2({ r: assertion.r, s: assertion.s});
|
|
1658
|
+
const leavesScVal = stellarSdk.xdr.ScVal.scvVec(leaves.map((l) => bytesScVal(l)));
|
|
1659
|
+
return invokeFunc(accountAddress, "add_signer_via_passkey", [
|
|
1660
|
+
bytesScVal(sec1Pubkey(newSigner)),
|
|
1661
|
+
bytesScVal(sec1Pubkey(passkey)),
|
|
1662
|
+
stellarSdk.nativeToScVal(nonce, { type: "u64" }),
|
|
1663
|
+
leavesScVal,
|
|
1664
|
+
stellarSdk.nativeToScVal(leafIndex, { type: "u32" }),
|
|
1665
|
+
bytesScVal(assertion.authenticatorData),
|
|
1666
|
+
bytesScVal(assertion.clientDataJSON),
|
|
1667
|
+
stellarSdk.nativeToScVal(assertion.challengeOffset, { type: "u32" }),
|
|
1668
|
+
bytesScVal(sig)
|
|
1669
|
+
]);
|
|
1670
|
+
}
|
|
1671
|
+
/** Read whether `passkey` is a registered approver (read-only simulation). */
|
|
1672
|
+
async isApprover(accountAddress, passkey, readSource) {
|
|
1673
|
+
if (!await this.isDeployed(accountAddress)) return false;
|
|
1674
|
+
const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
|
|
1675
|
+
const src = new Account3(readSource, "0");
|
|
1676
|
+
const op = stellarSdk.Operation.invokeHostFunction({
|
|
1677
|
+
func: invokeFunc(accountAddress, "is_approver", [bytesScVal(sec1Pubkey(passkey))]),
|
|
1678
|
+
auth: []
|
|
1679
|
+
});
|
|
1680
|
+
const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
|
|
1681
|
+
const sim = await this.server().simulateTransaction(tx2);
|
|
1682
|
+
if (stellarSdk.rpc.Api.isSimulationError(sim)) {
|
|
1683
|
+
throw new Error(`kit/stellar: is_approver simulation failed: ${sim.error}`);
|
|
1684
|
+
}
|
|
1685
|
+
if (!sim.result?.retval) return false;
|
|
1686
|
+
return stellarSdk.scValToNative(sim.result.retval) === true;
|
|
1687
|
+
}
|
|
1688
|
+
/** Read the current passkey-approval nonce (read-only simulation). */
|
|
1689
|
+
async passkeyNonce(accountAddress, readSource) {
|
|
1690
|
+
if (!await this.isDeployed(accountAddress)) return 0n;
|
|
1691
|
+
const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
|
|
1692
|
+
const src = new Account3(readSource, "0");
|
|
1693
|
+
const op = stellarSdk.Operation.invokeHostFunction({
|
|
1694
|
+
func: invokeFunc(accountAddress, "passkey_nonce", []),
|
|
1695
|
+
auth: []
|
|
1696
|
+
});
|
|
1697
|
+
const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
|
|
1698
|
+
const sim = await this.server().simulateTransaction(tx2);
|
|
1699
|
+
if (stellarSdk.rpc.Api.isSimulationError(sim)) {
|
|
1700
|
+
throw new Error(`kit/stellar: passkey_nonce simulation failed: ${sim.error}`);
|
|
1701
|
+
}
|
|
1702
|
+
if (!sim.result?.retval) return 0n;
|
|
1703
|
+
return BigInt(stellarSdk.scValToNative(sim.result.retval));
|
|
1704
|
+
}
|
|
1705
|
+
/** Host function: SEP-41 `token.transfer(from=account, to, amount)` (device auth). */
|
|
1706
|
+
buildTransfer(tokenId, accountAddress, destination, amount) {
|
|
1707
|
+
return invokeFunc(tokenId, "transfer", [
|
|
1708
|
+
new stellarSdk.Address(accountAddress).toScVal(),
|
|
1709
|
+
new stellarSdk.Address(destination).toScVal(),
|
|
1710
|
+
stellarSdk.nativeToScVal(amount, { type: "i128" })
|
|
1711
|
+
]);
|
|
1712
|
+
}
|
|
1713
|
+
/**
|
|
1714
|
+
* Sign a Soroban authorization entry with the silent device key, producing the
|
|
1715
|
+
* `Vec<DeviceSignature>` the account's `__check_auth` verifies. The device
|
|
1716
|
+
* signs `sha256(preimage)` (WebCrypto hashes once more internally), which is
|
|
1717
|
+
* exactly what the contract recomputes. Mutates + returns the entry.
|
|
1718
|
+
*/
|
|
1719
|
+
async signAuthEntry(entry, validUntilLedger) {
|
|
1720
|
+
const addrCreds = entry.credentials().address();
|
|
1721
|
+
addrCreds.signatureExpirationLedger(validUntilLedger);
|
|
1722
|
+
const preimage = stellarSdk.xdr.HashIdPreimage.envelopeTypeSorobanAuthorization(
|
|
1723
|
+
new stellarSdk.xdr.HashIdPreimageSorobanAuthorization({
|
|
1724
|
+
networkId: this.networkId(),
|
|
1725
|
+
nonce: addrCreds.nonce(),
|
|
1726
|
+
signatureExpirationLedger: validUntilLedger,
|
|
1727
|
+
invocation: entry.rootInvocation()
|
|
1728
|
+
})
|
|
1729
|
+
);
|
|
1730
|
+
const payload = stellarSdk.hash(preimage.toXDR());
|
|
1731
|
+
const sig = await this.signer.sign(new Uint8Array(payload));
|
|
1732
|
+
const pubkey = await this.signer.getPublicKey();
|
|
1733
|
+
addrCreds.signature(deviceSignatureScVal(pubkey, sig));
|
|
1734
|
+
return entry;
|
|
1735
|
+
}
|
|
1736
|
+
/**
|
|
1737
|
+
* Read a SEP-41 token balance of `account` via a read-only simulation of
|
|
1738
|
+
* `token.balance(account)`. Returns 0 when the account isn't deployed or holds
|
|
1739
|
+
* none. `readSource` is any funded G-account (used only for the simulation).
|
|
1740
|
+
*/
|
|
1741
|
+
async readBalance(tokenId, account, readSource) {
|
|
1742
|
+
if (!await this.isDeployed(account)) return 0n;
|
|
1743
|
+
const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
|
|
1744
|
+
const src = new Account3(readSource, "0");
|
|
1745
|
+
const op = stellarSdk.Operation.invokeHostFunction({
|
|
1746
|
+
func: invokeFunc(tokenId, "balance", [new stellarSdk.Address(account).toScVal()]),
|
|
1747
|
+
auth: []
|
|
1748
|
+
});
|
|
1749
|
+
const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
|
|
1750
|
+
const sim = await this.server().simulateTransaction(tx2);
|
|
1751
|
+
if (stellarSdk.rpc.Api.isSimulationError(sim) || !sim.result?.retval) return 0n;
|
|
1752
|
+
return BigInt(stellarSdk.scValToNative(sim.result.retval));
|
|
1753
|
+
}
|
|
1754
|
+
/** Whether the account contract instance exists on-chain (is deployed). */
|
|
1755
|
+
async isDeployed(accountAddress) {
|
|
1756
|
+
try {
|
|
1757
|
+
const res = await this.server().getContractData(
|
|
1758
|
+
accountAddress,
|
|
1759
|
+
stellarSdk.xdr.ScVal.scvLedgerKeyContractInstance(),
|
|
1760
|
+
stellarSdk.rpc.Durability.Persistent
|
|
1761
|
+
);
|
|
1762
|
+
return !!res;
|
|
1763
|
+
} catch {
|
|
1764
|
+
return false;
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
/**
|
|
1768
|
+
* Read whether `signer` is a currently-authorized signer of the account, via a
|
|
1769
|
+
* read-only simulation of `account.is_authorized(signer)`. `readSource` is any
|
|
1770
|
+
* funded G-account (used only for the simulation's source/sequence).
|
|
1771
|
+
*/
|
|
1772
|
+
async isAuthorizedSigner(accountAddress, signer, readSource) {
|
|
1773
|
+
if (!await this.isDeployed(accountAddress)) return false;
|
|
1774
|
+
const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
|
|
1775
|
+
const src = new Account3(readSource, "0");
|
|
1776
|
+
const op = stellarSdk.Operation.invokeHostFunction({
|
|
1777
|
+
func: invokeFunc(accountAddress, "is_authorized", [bytesScVal(sec1Pubkey(signer))]),
|
|
1778
|
+
auth: []
|
|
1779
|
+
});
|
|
1780
|
+
const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
|
|
1781
|
+
const sim = await this.server().simulateTransaction(tx2);
|
|
1782
|
+
if (stellarSdk.rpc.Api.isSimulationError(sim)) {
|
|
1783
|
+
throw new Error(`kit/stellar: is_authorized simulation failed: ${sim.error}`);
|
|
1784
|
+
}
|
|
1785
|
+
if (!sim.result?.retval) return false;
|
|
1786
|
+
return stellarSdk.scValToNative(sim.result.retval) === true;
|
|
1787
|
+
}
|
|
1788
|
+
};
|
|
1789
|
+
function sec1Pubkey(pk) {
|
|
1790
|
+
const out = new Uint8Array(65);
|
|
1791
|
+
out[0] = 4;
|
|
1792
|
+
out.set(bigIntTo32Bytes(pk.x), 1);
|
|
1793
|
+
out.set(bigIntTo32Bytes(pk.y), 33);
|
|
1794
|
+
return out;
|
|
1795
|
+
}
|
|
1796
|
+
function encodeLowSSignature2(sig) {
|
|
1797
|
+
const lowS2 = sig.s > SECP256R1_N2 / 2n ? SECP256R1_N2 - sig.s : sig.s;
|
|
1798
|
+
const out = new Uint8Array(64);
|
|
1799
|
+
out.set(bigIntTo32Bytes(sig.r), 0);
|
|
1800
|
+
out.set(bigIntTo32Bytes(lowS2), 32);
|
|
1801
|
+
return out;
|
|
1802
|
+
}
|
|
1803
|
+
function deviceSignatureScVal(pubkey, sig) {
|
|
1804
|
+
const element = stellarSdk.nativeToScVal(
|
|
1805
|
+
{
|
|
1806
|
+
public_key: Buffer.from(sec1Pubkey(pubkey)),
|
|
1807
|
+
signature: Buffer.from(encodeLowSSignature2(sig))
|
|
1808
|
+
},
|
|
1809
|
+
{ type: { public_key: ["symbol", "bytes"], signature: ["symbol", "bytes"] } }
|
|
1810
|
+
);
|
|
1811
|
+
return stellarSdk.xdr.ScVal.scvVec([element]);
|
|
1812
|
+
}
|
|
1813
|
+
function invokeFunc(contractId, method, args) {
|
|
1814
|
+
return stellarSdk.xdr.HostFunction.hostFunctionTypeInvokeContract(
|
|
1815
|
+
new stellarSdk.xdr.InvokeContractArgs({
|
|
1816
|
+
contractAddress: new stellarSdk.Address(contractId).toScAddress(),
|
|
1817
|
+
functionName: method,
|
|
1818
|
+
args
|
|
1819
|
+
})
|
|
1820
|
+
);
|
|
1821
|
+
}
|
|
1822
|
+
function bytesScVal(bytes) {
|
|
1823
|
+
return stellarSdk.xdr.ScVal.scvBytes(Buffer.from(bytes));
|
|
1824
|
+
}
|
|
1825
|
+
|
|
1826
|
+
// src/chains/stellar/StellarRelayer.ts
|
|
1827
|
+
var StellarRelayer = class {
|
|
1828
|
+
constructor(opts) {
|
|
1829
|
+
this.opts = opts;
|
|
1830
|
+
}
|
|
1831
|
+
/** The relayer's source/fee-payer G-account (fetched + cached from the backend). */
|
|
1832
|
+
async getSource() {
|
|
1833
|
+
if (this.source) return this.source;
|
|
1834
|
+
const res = await fetch(`${this.opts.baseUrl}/api/stellar/relay?network=${this.opts.network}`);
|
|
1835
|
+
if (!res.ok) throw new Error(`kit/stellar: relayer source lookup failed (${res.status})`);
|
|
1836
|
+
const { fee_payer } = await res.json();
|
|
1837
|
+
this.source = fee_payer;
|
|
1838
|
+
return this.source;
|
|
1839
|
+
}
|
|
1840
|
+
/**
|
|
1841
|
+
* POST the assembled, device-authorized transaction XDR to the relayer to sign
|
|
1842
|
+
* the envelope + submit. Returns the confirmed transaction hash.
|
|
1843
|
+
*/
|
|
1844
|
+
async submit(transactionXdr) {
|
|
1845
|
+
const res = await fetch(`${this.opts.baseUrl}/api/stellar/relay`, {
|
|
1846
|
+
method: "POST",
|
|
1847
|
+
headers: { "Content-Type": "application/json" },
|
|
1848
|
+
body: JSON.stringify({
|
|
1849
|
+
app_id: this.opts.appId,
|
|
1850
|
+
network: this.opts.network,
|
|
1851
|
+
transaction: transactionXdr
|
|
1852
|
+
})
|
|
1853
|
+
});
|
|
1854
|
+
if (!res.ok) {
|
|
1855
|
+
const detail = await res.text().catch(() => "");
|
|
1856
|
+
throw new Error(`kit/stellar: relay failed (${res.status}) ${detail}`);
|
|
1857
|
+
}
|
|
1858
|
+
const { hash: hash6 } = await res.json();
|
|
1859
|
+
return hash6;
|
|
1860
|
+
}
|
|
1861
|
+
};
|
|
1862
|
+
|
|
1863
|
+
// src/chains/stellar/CavosStellar.ts
|
|
1864
|
+
var CavosStellar = class _CavosStellar {
|
|
1865
|
+
constructor(identity, address, status, network, adapter, devicePubkey, relayer, sourceKeypair) {
|
|
1866
|
+
this.identity = identity;
|
|
1867
|
+
this.address = address;
|
|
1868
|
+
this.status = status;
|
|
1869
|
+
this.network = network;
|
|
1870
|
+
this.adapter = adapter;
|
|
1871
|
+
this.devicePubkey = devicePubkey;
|
|
1872
|
+
this.relayer = relayer;
|
|
1873
|
+
this.sourceKeypair = sourceKeypair;
|
|
1874
|
+
/** Discriminant for the `CavosWallet` union — narrows `execute()` per chain. */
|
|
1875
|
+
this.chain = "stellar";
|
|
1876
|
+
}
|
|
1877
|
+
get publicKey() {
|
|
1878
|
+
return this.devicePubkey;
|
|
1879
|
+
}
|
|
1880
|
+
static async connect(opts) {
|
|
1881
|
+
const identity = opts.identity ?? await opts.auth?.authenticate();
|
|
1882
|
+
if (!identity) throw new Error("kit/stellar: connect requires `identity` or `auth`");
|
|
1883
|
+
const signer = opts.createSigner ? await opts.createSigner(`${identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${identity.userId}:${opts.appSalt}` });
|
|
1884
|
+
const devicePubkey = await signer.getPublicKey();
|
|
1885
|
+
const adapter = new StellarAdapter({
|
|
1886
|
+
network: opts.network,
|
|
1887
|
+
rpcUrl: opts.rpcUrl,
|
|
1888
|
+
factoryId: opts.factoryId,
|
|
1889
|
+
signer
|
|
1890
|
+
});
|
|
1891
|
+
const addressSeed = deriveAddressSeedStellar({ userId: identity.userId, appSalt: opts.appSalt });
|
|
1892
|
+
const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
|
|
1893
|
+
const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry2);
|
|
1894
|
+
const relayer = opts.relayer ?? (opts.appId ? new StellarRelayer({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : void 0);
|
|
1895
|
+
const build = (address2, status) => new _CavosStellar(identity, address2, status, opts.network, adapter, devicePubkey, relayer, opts.sourceKeypair);
|
|
1896
|
+
const self = build("", "needs-device-approval");
|
|
1897
|
+
const readSource = await self.resolveSource();
|
|
1898
|
+
const existing = await registry.lookup(identity.userId);
|
|
1899
|
+
if (existing) {
|
|
1900
|
+
const isSigner2 = await adapter.isAuthorizedSigner(existing.address, devicePubkey, readSource);
|
|
1901
|
+
return build(existing.address, isSigner2 ? "ready" : "needs-device-approval");
|
|
1902
|
+
}
|
|
1903
|
+
const address = adapter.computeAddress(addressSeed, devicePubkey);
|
|
1904
|
+
if (!await adapter.isDeployed(address)) {
|
|
1905
|
+
const func = adapter.buildDeploy(addressSeed, devicePubkey);
|
|
1906
|
+
await self.submitHostFunction(func, void 0);
|
|
1907
|
+
}
|
|
1908
|
+
await registry.register({ userId: identity.userId, address, initialSigner: devicePubkey });
|
|
1909
|
+
const isSigner = await adapter.isAuthorizedSigner(address, devicePubkey, readSource);
|
|
1910
|
+
return build(address, isSigner ? "ready" : "needs-device-approval");
|
|
1911
|
+
}
|
|
1912
|
+
/** Authorize an additional device signer (device-signed via `__check_auth`). */
|
|
1913
|
+
async addSigner(pubkey) {
|
|
1914
|
+
const func = this.adapter.buildAddSigner(this.address, pubkey);
|
|
1915
|
+
return this.submitHostFunction(func, this.address);
|
|
1916
|
+
}
|
|
1917
|
+
/**
|
|
1918
|
+
* Enroll a passkey as an approver (2FA-style step-up). Device-signed + gasless;
|
|
1919
|
+
* requires a ready device. Idempotent. Returns the passkey pubkey + tx hash.
|
|
1920
|
+
*/
|
|
1921
|
+
async enrollPasskey(passkey, params) {
|
|
1922
|
+
const enrolled = await passkey.enroll(params);
|
|
1923
|
+
const { transactionHash } = await this.addApprover(enrolled.publicKey);
|
|
1924
|
+
return { publicKey: enrolled.publicKey, transactionHash };
|
|
1925
|
+
}
|
|
1926
|
+
/** Register an already-enrolled passkey pubkey as an approver (gasless).
|
|
1927
|
+
* Idempotent. Lets one passkey be registered across chains without re-prompting. */
|
|
1928
|
+
async addApprover(pubkey) {
|
|
1929
|
+
if (this.status !== "ready") {
|
|
1930
|
+
throw new Error("kit/stellar: addApprover requires a ready, authorized device");
|
|
1931
|
+
}
|
|
1932
|
+
const readSource = await this.resolveSource();
|
|
1933
|
+
if (await this.adapter.isApprover(this.address, pubkey, readSource)) return {};
|
|
1934
|
+
const func = this.adapter.buildAddApprover(this.address, pubkey);
|
|
1935
|
+
const transactionHash = await this.submitHostFunction(func, this.address);
|
|
1936
|
+
return { transactionHash };
|
|
1937
|
+
}
|
|
1938
|
+
/**
|
|
1939
|
+
* From a fresh browser (status `needs-device-approval`), approve adding THIS
|
|
1940
|
+
* device using the user's synced passkey. Gasless via the relayer — the call
|
|
1941
|
+
* carries the WebAuthn assertion, so no device signature is needed. Returns the
|
|
1942
|
+
* tx hash. No trip back to an already-authorized device.
|
|
1943
|
+
*/
|
|
1944
|
+
async approveThisDeviceWithPasskey(passkey) {
|
|
1945
|
+
if (this.status === "ready") {
|
|
1946
|
+
throw new Error("kit/stellar: this device is already an authorized signer");
|
|
1947
|
+
}
|
|
1948
|
+
const { leaf, nonce } = await this.passkeyLeafForThisDevice();
|
|
1949
|
+
const leaves = [leaf];
|
|
1950
|
+
const assertion = await passkey.assert(batchChallenge(leaves));
|
|
1951
|
+
const { transactionHash } = await this.submitPasskeyApproval(assertion, leaves, 0, nonce);
|
|
1952
|
+
return transactionHash;
|
|
1953
|
+
}
|
|
1954
|
+
/** This device's leaf + passkey nonce for a (possibly multi-chain) batch. */
|
|
1955
|
+
async passkeyLeafForThisDevice() {
|
|
1956
|
+
const readSource = await this.resolveSource();
|
|
1957
|
+
const nonce = await this.adapter.passkeyNonce(this.address, readSource);
|
|
1958
|
+
return { leaf: this.adapter.passkeyLeaf(this.devicePubkey, nonce), nonce };
|
|
1959
|
+
}
|
|
1960
|
+
/** Submit `add_signer_via_passkey` given a shared assertion + batch position.
|
|
1961
|
+
* No device auth entry — authorized purely by the passkey assertion. */
|
|
1962
|
+
async submitPasskeyApproval(assertion, leaves, leafIndex, nonce) {
|
|
1963
|
+
const readSource = await this.resolveSource();
|
|
1964
|
+
const digest = webauthnDigest(assertion.authenticatorData, assertion.clientDataJSON);
|
|
1965
|
+
const candidates = recoverCandidatePublicKeys(assertion.r, assertion.s, digest);
|
|
1966
|
+
let approver = null;
|
|
1967
|
+
for (const cand of candidates) {
|
|
1968
|
+
if (await this.adapter.isApprover(this.address, cand.publicKey, readSource)) {
|
|
1969
|
+
approver = cand.publicKey;
|
|
1970
|
+
break;
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
if (!approver) throw new Error("kit/stellar: this passkey is not a registered approver");
|
|
1974
|
+
const func = this.adapter.buildAddSignerViaPasskey(
|
|
1975
|
+
this.address,
|
|
1976
|
+
this.devicePubkey,
|
|
1977
|
+
approver,
|
|
1978
|
+
nonce,
|
|
1979
|
+
leaves,
|
|
1980
|
+
leafIndex,
|
|
1981
|
+
assertion
|
|
1982
|
+
);
|
|
1983
|
+
return { transactionHash: await this.submitHostFunction(func, void 0) };
|
|
1984
|
+
}
|
|
1985
|
+
/** Move `amount` stroops of native XLM to `destination` (device-signed). */
|
|
1986
|
+
async execute(amount, destination) {
|
|
1987
|
+
return this.executeTransfer(NATIVE_SAC_ID[this.network], amount, destination);
|
|
1988
|
+
}
|
|
1989
|
+
/** Read this account's balance of `tokenId` (defaults to native XLM), in stroops. */
|
|
1990
|
+
async balance(tokenId = NATIVE_SAC_ID[this.network]) {
|
|
1991
|
+
const readSource = await this.resolveSource();
|
|
1992
|
+
return this.adapter.readBalance(tokenId, this.address, readSource);
|
|
1993
|
+
}
|
|
1994
|
+
/** Transfer `amount` of any SEP-41 token out of the account (device-signed). */
|
|
1995
|
+
async executeTransfer(tokenId, amount, destination) {
|
|
1996
|
+
if (this.status !== "ready") {
|
|
1997
|
+
throw new Error("kit/stellar: this device is not yet an authorized signer of the wallet");
|
|
1998
|
+
}
|
|
1999
|
+
const func = this.adapter.buildTransfer(tokenId, this.address, destination, amount);
|
|
2000
|
+
return this.submitHostFunction(func, this.address);
|
|
2001
|
+
}
|
|
2002
|
+
/**
|
|
2003
|
+
* Register the backup signer derived from `code` as an authorized signer of
|
|
2004
|
+
* this account (device-signed). Idempotent. The code never leaves the device —
|
|
2005
|
+
* only the derived public key travels on-chain. Mirrors the other chains.
|
|
2006
|
+
*/
|
|
2007
|
+
async setupRecovery(code) {
|
|
2008
|
+
if (this.status !== "ready") {
|
|
2009
|
+
throw new Error("kit/stellar: setupRecovery requires a ready, registered device");
|
|
2010
|
+
}
|
|
2011
|
+
const { publicKey: backupPubkey } = deriveBackupKey(code);
|
|
2012
|
+
const readSource = await this.resolveSource();
|
|
2013
|
+
if (await this.adapter.isAuthorizedSigner(this.address, backupPubkey, readSource)) return void 0;
|
|
2014
|
+
return this.addSigner(backupPubkey);
|
|
2015
|
+
}
|
|
2016
|
+
/**
|
|
2017
|
+
* Recover an account after losing every device signer: derive the backup key
|
|
2018
|
+
* from `code`, use it (not the new device) to authorize `add_signer(newDevice)`,
|
|
2019
|
+
* and return a ready handle bound to the new device. The address is unchanged.
|
|
2020
|
+
*/
|
|
2021
|
+
static async recover(opts) {
|
|
2022
|
+
const signer = opts.createSigner ? await opts.createSigner(`${opts.identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${opts.identity.userId}:${opts.appSalt}` });
|
|
2023
|
+
const devicePubkey = await signer.getPublicKey();
|
|
2024
|
+
const backup = BackupSigner.fromCode(opts.code);
|
|
2025
|
+
const backupAdapter = new StellarAdapter({
|
|
2026
|
+
network: opts.network,
|
|
2027
|
+
rpcUrl: opts.rpcUrl,
|
|
2028
|
+
factoryId: opts.factoryId,
|
|
2029
|
+
signer: backup
|
|
2030
|
+
});
|
|
2031
|
+
const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
|
|
2032
|
+
const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry2);
|
|
2033
|
+
const existing = await registry.lookup(opts.identity.userId);
|
|
2034
|
+
if (!existing) {
|
|
2035
|
+
throw new Error("kit/stellar: no account found for this identity \u2014 nothing to recover");
|
|
2036
|
+
}
|
|
2037
|
+
const relayer = opts.relayer ?? (opts.appId ? new StellarRelayer({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : void 0);
|
|
2038
|
+
const backupHandle = new _CavosStellar(
|
|
2039
|
+
opts.identity,
|
|
2040
|
+
existing.address,
|
|
2041
|
+
"ready",
|
|
2042
|
+
opts.network,
|
|
2043
|
+
backupAdapter,
|
|
2044
|
+
devicePubkey,
|
|
2045
|
+
relayer,
|
|
2046
|
+
opts.sourceKeypair
|
|
2047
|
+
);
|
|
2048
|
+
const readSource = await backupHandle.resolveSource();
|
|
2049
|
+
if (!await backupAdapter.isAuthorizedSigner(existing.address, devicePubkey, readSource)) {
|
|
2050
|
+
await backupHandle.addSigner(devicePubkey);
|
|
2051
|
+
}
|
|
2052
|
+
const adapter = new StellarAdapter({
|
|
2053
|
+
network: opts.network,
|
|
2054
|
+
rpcUrl: opts.rpcUrl,
|
|
2055
|
+
factoryId: opts.factoryId,
|
|
2056
|
+
signer
|
|
2057
|
+
});
|
|
2058
|
+
return new _CavosStellar(
|
|
2059
|
+
opts.identity,
|
|
2060
|
+
existing.address,
|
|
2061
|
+
"ready",
|
|
2062
|
+
opts.network,
|
|
2063
|
+
adapter,
|
|
2064
|
+
devicePubkey,
|
|
2065
|
+
relayer,
|
|
2066
|
+
opts.sourceKeypair
|
|
2067
|
+
);
|
|
2068
|
+
}
|
|
2069
|
+
/** The transaction source/fee-payer G-address (relayer or self-funded). */
|
|
2070
|
+
async resolveSource() {
|
|
2071
|
+
if (this.relayer) return this.relayer.getSource();
|
|
2072
|
+
if (this.sourceKeypair) return this.sourceKeypair.publicKey();
|
|
2073
|
+
throw new Error("kit/stellar: a relayer (appId) or sourceKeypair is required");
|
|
2074
|
+
}
|
|
2075
|
+
/**
|
|
2076
|
+
* Build → simulate → device-sign auth → assemble → submit an invoke-contract
|
|
2077
|
+
* host function. `authAccount` is the account whose `__check_auth` must sign the
|
|
2078
|
+
* operation's Soroban auth entry (undefined for a plain factory deploy).
|
|
2079
|
+
*/
|
|
2080
|
+
async submitHostFunction(func, authAccount) {
|
|
2081
|
+
const server = this.adapter.server();
|
|
2082
|
+
const sourceAddr = await this.resolveSource();
|
|
2083
|
+
const simSource = new stellarSdk.Account(sourceAddr, "0");
|
|
2084
|
+
const unsignedOp = stellarSdk.Operation.invokeHostFunction({ func, auth: [] });
|
|
2085
|
+
const simTx = new stellarSdk.TransactionBuilder(simSource, {
|
|
2086
|
+
fee: stellarSdk.BASE_FEE,
|
|
2087
|
+
networkPassphrase: this.adapter.passphrase
|
|
2088
|
+
}).addOperation(unsignedOp).setTimeout(180).build();
|
|
2089
|
+
const sim = await server.simulateTransaction(simTx);
|
|
2090
|
+
if (stellarSdk.rpc.Api.isSimulationError(sim)) {
|
|
2091
|
+
throw new Error(`kit/stellar: simulation failed: ${sim.error}`);
|
|
2092
|
+
}
|
|
2093
|
+
const validUntil = (await server.getLatestLedger()).sequence + 100;
|
|
2094
|
+
const entries = sim.result?.auth ?? [];
|
|
2095
|
+
const signedAuth = [];
|
|
2096
|
+
for (const entry of entries) {
|
|
2097
|
+
if (authAccount && isAddressCredentialFor(entry, authAccount)) {
|
|
2098
|
+
signedAuth.push(await this.adapter.signAuthEntry(entry, validUntil));
|
|
2099
|
+
} else {
|
|
2100
|
+
signedAuth.push(entry);
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
const account = await server.getAccount(sourceAddr);
|
|
2104
|
+
const finalOp = stellarSdk.Operation.invokeHostFunction({ func, auth: signedAuth });
|
|
2105
|
+
const built = new stellarSdk.TransactionBuilder(account, {
|
|
2106
|
+
fee: stellarSdk.BASE_FEE,
|
|
2107
|
+
networkPassphrase: this.adapter.passphrase
|
|
2108
|
+
}).addOperation(finalOp).setTimeout(180).build();
|
|
2109
|
+
const authSim = await server.simulateTransaction(built);
|
|
2110
|
+
if (stellarSdk.rpc.Api.isSimulationError(authSim)) {
|
|
2111
|
+
throw new Error(`kit/stellar: auth simulation failed: ${authSim.error}`);
|
|
2112
|
+
}
|
|
2113
|
+
const assembled = stellarSdk.rpc.assembleTransaction(built, authSim).build();
|
|
2114
|
+
if (this.relayer) {
|
|
2115
|
+
return this.relayer.submit(assembled.toXDR());
|
|
2116
|
+
}
|
|
2117
|
+
if (this.sourceKeypair) {
|
|
2118
|
+
assembled.sign(this.sourceKeypair);
|
|
2119
|
+
return this.sendAndConfirm(assembled);
|
|
2120
|
+
}
|
|
2121
|
+
throw new Error("kit/stellar: no relayer or sourceKeypair configured to submit");
|
|
2122
|
+
}
|
|
2123
|
+
/** Submit a signed tx via RPC and poll to confirmation. Returns the hash. */
|
|
2124
|
+
async sendAndConfirm(tx2) {
|
|
2125
|
+
const server = this.adapter.server();
|
|
2126
|
+
const sent = await server.sendTransaction(tx2);
|
|
2127
|
+
if (sent.status === "ERROR") {
|
|
2128
|
+
throw new Error(`kit/stellar: submit rejected: ${JSON.stringify(sent.errorResult)}`);
|
|
2129
|
+
}
|
|
2130
|
+
const hash6 = sent.hash;
|
|
2131
|
+
for (let i = 0; i < 30; i++) {
|
|
2132
|
+
const got = await server.getTransaction(hash6);
|
|
2133
|
+
if (got.status === stellarSdk.rpc.Api.GetTransactionStatus.SUCCESS) return hash6;
|
|
2134
|
+
if (got.status === stellarSdk.rpc.Api.GetTransactionStatus.FAILED) {
|
|
2135
|
+
throw new Error(`kit/stellar: tx ${hash6} failed`);
|
|
2136
|
+
}
|
|
2137
|
+
await new Promise((r) => setTimeout(r, 1e3));
|
|
2138
|
+
}
|
|
2139
|
+
throw new Error(`kit/stellar: tx ${hash6} not confirmed in time`);
|
|
2140
|
+
}
|
|
2141
|
+
};
|
|
2142
|
+
function isAddressCredentialFor(entry, accountAddress) {
|
|
2143
|
+
const creds = entry.credentials();
|
|
2144
|
+
if (creds.switch() !== stellarSdk.xdr.SorobanCredentialsType.sorobanCredentialsAddress()) return false;
|
|
2145
|
+
return stellarSdk.Address.fromScAddress(creds.address().address()).toString() === accountAddress;
|
|
2146
|
+
}
|
|
2147
|
+
var defaultRegistry2 = new InMemoryWalletRegistry();
|
|
2148
|
+
|
|
2149
|
+
// src/recovery/HttpRecoveryClient.ts
|
|
2150
|
+
function toHex2(n) {
|
|
2151
|
+
return "0x" + n.toString(16);
|
|
2152
|
+
}
|
|
2153
|
+
function fromHex2(s) {
|
|
2154
|
+
return BigInt(s);
|
|
2155
|
+
}
|
|
2156
|
+
function deviceLabel() {
|
|
2157
|
+
if (typeof navigator !== "undefined") {
|
|
2158
|
+
return navigator.userAgent || "a new device";
|
|
2159
|
+
}
|
|
2160
|
+
return "a new device";
|
|
2161
|
+
}
|
|
2162
|
+
var HttpRecoveryClient = class {
|
|
2163
|
+
constructor(opts) {
|
|
2164
|
+
this.opts = opts;
|
|
2165
|
+
}
|
|
2166
|
+
async requestDeviceAddition(params) {
|
|
2167
|
+
const res = await fetch(new URL("/api/devices/request", this.opts.baseUrl), {
|
|
2168
|
+
method: "POST",
|
|
2169
|
+
headers: { "Content-Type": "application/json" },
|
|
2170
|
+
body: JSON.stringify({
|
|
2171
|
+
app_id: this.opts.appId,
|
|
2172
|
+
wallet_address: params.accountAddress,
|
|
2173
|
+
new_pub_x: toHex2(params.newSigner.x),
|
|
2174
|
+
new_pub_y: toHex2(params.newSigner.y),
|
|
2175
|
+
device_label: params.deviceLabel ?? deviceLabel(),
|
|
2176
|
+
...params.email ? { email: params.email } : {}
|
|
2177
|
+
})
|
|
2178
|
+
});
|
|
2179
|
+
if (!res.ok) {
|
|
2180
|
+
const t = await res.text().catch(() => "");
|
|
2181
|
+
throw new Error(`requestDeviceAddition failed: ${res.status} ${t}`);
|
|
2182
|
+
}
|
|
2183
|
+
const data = await res.json();
|
|
2184
|
+
return { requestId: data.request_id };
|
|
2185
|
+
}
|
|
2186
|
+
async getPendingRequest(requestId) {
|
|
2187
|
+
const url = new URL("/api/devices/request", this.opts.baseUrl);
|
|
2188
|
+
url.searchParams.set("id", requestId);
|
|
2189
|
+
const res = await fetch(url, { headers: { "Content-Type": "application/json" } });
|
|
2190
|
+
if (!res.ok) throw new Error(`getPendingRequest failed: ${res.status}`);
|
|
2191
|
+
const data = await res.json();
|
|
2192
|
+
if (!data.found) return null;
|
|
2193
|
+
const status = data.status;
|
|
2194
|
+
return {
|
|
2195
|
+
requestId: data.request_id,
|
|
2196
|
+
appId: data.app_id,
|
|
2197
|
+
userId: "",
|
|
2198
|
+
// the approving device already knows its own identity
|
|
2199
|
+
accountAddress: data.wallet_address,
|
|
2200
|
+
newSigner: { x: fromHex2(data.new_pub_x), y: fromHex2(data.new_pub_y) },
|
|
2201
|
+
createdAt: data.created_at,
|
|
2202
|
+
status
|
|
2203
|
+
};
|
|
2204
|
+
}
|
|
2205
|
+
async confirmDeviceAddition(params) {
|
|
2206
|
+
const res = await fetch(
|
|
2207
|
+
new URL(`/api/devices/request/${params.requestId}/confirm`, this.opts.baseUrl),
|
|
2208
|
+
{
|
|
2209
|
+
method: "POST",
|
|
2210
|
+
headers: { "Content-Type": "application/json" },
|
|
2211
|
+
body: JSON.stringify({ tx_hash: params.txHash })
|
|
2212
|
+
}
|
|
2213
|
+
);
|
|
2214
|
+
if (!res.ok) {
|
|
2215
|
+
const t = await res.text().catch(() => "");
|
|
2216
|
+
throw new Error(`confirmDeviceAddition failed: ${res.status} ${t}`);
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
};
|
|
2220
|
+
|
|
2221
|
+
// src/Cavos.ts
|
|
2222
|
+
var STARKNET_ENV = {
|
|
2223
|
+
mainnet: "mainnet",
|
|
2224
|
+
testnet: "sepolia"
|
|
2225
|
+
};
|
|
2226
|
+
var SOLANA_ENV = {
|
|
2227
|
+
mainnet: "solana-mainnet",
|
|
2228
|
+
testnet: "solana-devnet"
|
|
2229
|
+
};
|
|
2230
|
+
var STELLAR_ENV = {
|
|
2231
|
+
mainnet: "stellar-mainnet",
|
|
2232
|
+
testnet: "stellar-testnet"
|
|
2233
|
+
};
|
|
2234
|
+
var Cavos = class _Cavos {
|
|
2235
|
+
constructor(identity, address, status, account, adapter, devicePubkey, paymaster) {
|
|
2236
|
+
this.identity = identity;
|
|
2237
|
+
this.address = address;
|
|
2238
|
+
this.status = status;
|
|
2239
|
+
this.account = account;
|
|
2240
|
+
this.adapter = adapter;
|
|
2241
|
+
this.devicePubkey = devicePubkey;
|
|
2242
|
+
this.paymaster = paymaster;
|
|
2243
|
+
/** Discriminant for the `CavosWallet` union — narrows `execute()` per chain. */
|
|
2244
|
+
this.chain = "starknet";
|
|
2245
|
+
/** Request id of the pending device-addition, when status is needs-device-approval. */
|
|
2246
|
+
this.pendingRequestId = null;
|
|
2247
|
+
}
|
|
2248
|
+
/**
|
|
2249
|
+
* Unified entry point. Pick a `chain` and an `network` environment; the kit
|
|
2250
|
+
* resolves the concrete network (sepolia/devnet for testnet, mainnet for
|
|
2251
|
+
* mainnet) and returns a chain-native wallet. The result is a discriminated
|
|
2252
|
+
* union (`wallet.chain`), so `execute()` keeps each chain's native signature:
|
|
2253
|
+
*
|
|
2254
|
+
* const wallet = await Cavos.connect({ chain: "solana", network: "testnet", identity, appSalt, appId });
|
|
2255
|
+
* if (wallet.chain === "starknet") await wallet.execute(calls);
|
|
2256
|
+
* else await wallet.execute(amount, dest);
|
|
2257
|
+
*/
|
|
2258
|
+
static async connect(opts) {
|
|
2259
|
+
if (opts.chain === "solana") {
|
|
2260
|
+
return CavosSolana.connect({
|
|
2261
|
+
network: SOLANA_ENV[opts.network],
|
|
2262
|
+
...opts.auth ? { auth: opts.auth } : {},
|
|
2263
|
+
...opts.identity ? { identity: opts.identity } : {},
|
|
2264
|
+
appSalt: opts.appSalt,
|
|
2265
|
+
...opts.appId ? { appId: opts.appId } : {},
|
|
2266
|
+
...opts.backendUrl ? { backendUrl: opts.backendUrl } : {},
|
|
2267
|
+
...opts.registry ? { registry: opts.registry } : {},
|
|
2268
|
+
...opts.rpcUrl ? { rpcUrl: opts.rpcUrl } : {},
|
|
2269
|
+
...opts.programId ? { programId: opts.programId } : {},
|
|
2270
|
+
...opts.createSigner ? { createSigner: opts.createSigner } : {},
|
|
2271
|
+
...opts.relayer ? { relayer: opts.relayer } : {},
|
|
2272
|
+
...opts.feePayer ? { feePayer: opts.feePayer } : {}
|
|
2273
|
+
});
|
|
2274
|
+
}
|
|
2275
|
+
if (opts.chain === "stellar") {
|
|
2276
|
+
return CavosStellar.connect({
|
|
2277
|
+
network: STELLAR_ENV[opts.network],
|
|
2278
|
+
...opts.auth ? { auth: opts.auth } : {},
|
|
2279
|
+
...opts.identity ? { identity: opts.identity } : {},
|
|
2280
|
+
appSalt: opts.appSalt,
|
|
2281
|
+
...opts.appId ? { appId: opts.appId } : {},
|
|
2282
|
+
...opts.backendUrl ? { backendUrl: opts.backendUrl } : {},
|
|
2283
|
+
...opts.registry ? { registry: opts.registry } : {},
|
|
2284
|
+
...opts.rpcUrl ? { rpcUrl: opts.rpcUrl } : {},
|
|
2285
|
+
...opts.factoryId ? { factoryId: opts.factoryId } : {},
|
|
2286
|
+
...opts.createSigner ? { createSigner: opts.createSigner } : {},
|
|
2287
|
+
...opts.stellarRelayer ? { relayer: opts.stellarRelayer } : {},
|
|
2288
|
+
...opts.stellarSourceKeypair ? { sourceKeypair: opts.stellarSourceKeypair } : {}
|
|
2289
|
+
});
|
|
2290
|
+
}
|
|
2291
|
+
if (!opts.paymasterApiKey) {
|
|
2292
|
+
throw new Error("kit: `paymasterApiKey` is required for Starknet connections");
|
|
2293
|
+
}
|
|
2294
|
+
return _Cavos.connectStarknet({
|
|
2295
|
+
network: STARKNET_ENV[opts.network],
|
|
2296
|
+
auth: opts.auth,
|
|
2297
|
+
identity: opts.identity,
|
|
2298
|
+
appSalt: opts.appSalt,
|
|
2299
|
+
appId: opts.appId,
|
|
2300
|
+
backendUrl: opts.backendUrl,
|
|
2301
|
+
registry: opts.registry,
|
|
2302
|
+
recovery: opts.recovery,
|
|
2303
|
+
paymasterApiKey: opts.paymasterApiKey,
|
|
2304
|
+
paymasterUrl: opts.paymasterUrl,
|
|
2305
|
+
rpcUrl: opts.rpcUrl,
|
|
2306
|
+
classHash: opts.classHash,
|
|
2307
|
+
createSigner: opts.createSigner
|
|
2308
|
+
});
|
|
2309
|
+
}
|
|
2310
|
+
static async connectStarknet(opts) {
|
|
2311
|
+
const identity = opts.identity ?? await opts.auth?.authenticate();
|
|
2312
|
+
if (!identity) throw new Error("kit: connect requires `identity` or `auth`");
|
|
2313
|
+
const classHash = opts.classHash ?? DEVICE_ACCOUNT_CLASS_HASH[opts.network];
|
|
2314
|
+
if (!classHash) throw new Error(`kit: no DeviceAccount class hash for ${opts.network}`);
|
|
2315
|
+
const provider = new starknet.RpcProvider({
|
|
2316
|
+
nodeUrl: opts.rpcUrl ?? STARKNET_NETWORKS[opts.network].rpcUrl
|
|
2317
|
+
});
|
|
2318
|
+
const paymasterUrl = opts.paymasterUrl ?? CAVOS_PAYMASTER_URL[opts.network];
|
|
2319
|
+
const paymasterConfig = { url: paymasterUrl, apiKey: opts.paymasterApiKey };
|
|
2320
|
+
const paymaster = new starknet.PaymasterRpc({
|
|
2321
|
+
nodeUrl: paymasterUrl,
|
|
2322
|
+
headers: { "x-paymaster-api-key": opts.paymasterApiKey }
|
|
2323
|
+
});
|
|
2324
|
+
const addressSeed = deriveAddressSeed({ userId: identity.userId, appSalt: opts.appSalt });
|
|
2325
|
+
const signer = opts.createSigner ? await opts.createSigner(`${identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${identity.userId}:${opts.appSalt}` });
|
|
2326
|
+
const devicePubkey = await signer.getPublicKey();
|
|
2327
|
+
const adapter = new StarknetAdapter({ classHash, signer, provider });
|
|
2328
|
+
const makeAccount = (address2) => new starknet.Account({
|
|
2329
|
+
provider,
|
|
2330
|
+
address: address2,
|
|
2331
|
+
signer: new StarknetDeviceSigner(signer),
|
|
2332
|
+
paymaster,
|
|
2333
|
+
cairoVersion: "1"
|
|
2334
|
+
});
|
|
2335
|
+
const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
|
|
2336
|
+
const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry3);
|
|
2337
|
+
const recovery = opts.recovery ?? (opts.appId ? new HttpRecoveryClient({ baseUrl: backendUrl, appId: opts.appId }) : null);
|
|
2338
|
+
const existing = await registry.lookup(identity.userId);
|
|
2339
|
+
if (existing) {
|
|
2340
|
+
const account2 = makeAccount(existing.address);
|
|
2341
|
+
const isSigner2 = await adapter.isAuthorizedSigner(existing.address, devicePubkey);
|
|
2342
|
+
const cavos = new _Cavos(
|
|
2343
|
+
identity,
|
|
2344
|
+
existing.address,
|
|
2345
|
+
isSigner2 ? "ready" : "needs-device-approval",
|
|
2346
|
+
account2,
|
|
2347
|
+
adapter,
|
|
2348
|
+
devicePubkey,
|
|
2349
|
+
paymasterConfig
|
|
2350
|
+
);
|
|
2351
|
+
if (!isSigner2 && recovery) {
|
|
2352
|
+
const dedup = lastDeviceRequest.get(identity.userId);
|
|
2353
|
+
const fresh = dedup && Date.now() - dedup.requestedAt < DEVICE_REQUEST_DEDUP_MS;
|
|
2354
|
+
try {
|
|
2355
|
+
if (fresh) {
|
|
2356
|
+
cavos.pendingRequestId = dedup.requestId;
|
|
2357
|
+
} else {
|
|
2358
|
+
const { requestId } = await recovery.requestDeviceAddition({
|
|
2359
|
+
userId: identity.userId,
|
|
2360
|
+
accountAddress: existing.address,
|
|
2361
|
+
newSigner: devicePubkey,
|
|
2362
|
+
...identity.email ? { email: identity.email } : {}
|
|
2363
|
+
});
|
|
2364
|
+
cavos.pendingRequestId = requestId;
|
|
2365
|
+
lastDeviceRequest.set(identity.userId, { requestId, requestedAt: Date.now() });
|
|
2366
|
+
}
|
|
2367
|
+
} catch (e) {
|
|
2368
|
+
console.warn("[Cavos] requestDeviceAddition failed:", e);
|
|
2369
|
+
}
|
|
2370
|
+
}
|
|
2371
|
+
return cavos;
|
|
2372
|
+
}
|
|
2373
|
+
const address = adapter.computeAddress({ addressSeed, initialSigner: devicePubkey });
|
|
2374
|
+
const account = makeAccount(address);
|
|
2375
|
+
const alreadyDeployed = await isDeployed(provider, address);
|
|
2376
|
+
if (!alreadyDeployed) {
|
|
2377
|
+
const deploymentData = {
|
|
2378
|
+
address,
|
|
2379
|
+
class_hash: classHash,
|
|
2380
|
+
salt: starknet.num.toHex(addressSeed),
|
|
2381
|
+
calldata: adapter.constructorCalldata(addressSeed, devicePubkey),
|
|
2382
|
+
version: 1
|
|
2383
|
+
};
|
|
2384
|
+
const deployRes = await account.executePaymasterTransaction([], {
|
|
2385
|
+
feeMode: { mode: "sponsored" },
|
|
2386
|
+
deploymentData
|
|
2387
|
+
});
|
|
2388
|
+
try {
|
|
2389
|
+
await provider.waitForTransaction(deployRes.transaction_hash);
|
|
2390
|
+
} catch (e) {
|
|
2391
|
+
console.warn("[Cavos] deploy receipt wait failed:", e);
|
|
2392
|
+
}
|
|
2393
|
+
}
|
|
2394
|
+
await registry.register({ userId: identity.userId, address, initialSigner: devicePubkey });
|
|
2395
|
+
let isSigner;
|
|
2396
|
+
try {
|
|
2397
|
+
isSigner = await adapter.isAuthorizedSigner(address, devicePubkey);
|
|
2398
|
+
} catch (e) {
|
|
2399
|
+
console.warn("[Cavos] isAuthorizedSigner read failed:", e);
|
|
816
2400
|
isSigner = !alreadyDeployed;
|
|
817
2401
|
}
|
|
818
2402
|
return new _Cavos(
|
|
@@ -821,7 +2405,8 @@ var Cavos = class _Cavos {
|
|
|
821
2405
|
isSigner ? "ready" : "needs-device-approval",
|
|
822
2406
|
account,
|
|
823
2407
|
adapter,
|
|
824
|
-
devicePubkey
|
|
2408
|
+
devicePubkey,
|
|
2409
|
+
paymasterConfig
|
|
825
2410
|
);
|
|
826
2411
|
}
|
|
827
2412
|
/** This device's public key (e.g. to request addition to an existing wallet). */
|
|
@@ -842,6 +2427,92 @@ var Cavos = class _Cavos {
|
|
|
842
2427
|
async addSigner(pubkey) {
|
|
843
2428
|
return this.execute([this.adapter.buildAddSigner(this.address, pubkey)]);
|
|
844
2429
|
}
|
|
2430
|
+
/**
|
|
2431
|
+
* Enroll a passkey as an APPROVER so the user can later add devices from any
|
|
2432
|
+
* browser (2FA-style step-up). Requires a ready device (the enrollment call is
|
|
2433
|
+
* device-signed and gasless). Idempotent: a no-op if the passkey is already an
|
|
2434
|
+
* approver. Call this whenever the app decides to prompt "turn on device
|
|
2435
|
+
* approvals". Returns the passkey's public key + the enrollment tx hash.
|
|
2436
|
+
*/
|
|
2437
|
+
async enrollPasskey(passkey, params) {
|
|
2438
|
+
const enrolled = await passkey.enroll(params);
|
|
2439
|
+
const { transactionHash } = await this.addApprover(enrolled.publicKey);
|
|
2440
|
+
return { publicKey: enrolled.publicKey, transactionHash };
|
|
2441
|
+
}
|
|
2442
|
+
/**
|
|
2443
|
+
* Register an ALREADY-enrolled passkey public key as an approver (gasless,
|
|
2444
|
+
* device-signed). Idempotent. Use this to register ONE passkey across multiple
|
|
2445
|
+
* chains without re-prompting `passkey.enroll()` on each: enroll once, then
|
|
2446
|
+
* call `addApprover(pubkey)` on each chain's wallet.
|
|
2447
|
+
*/
|
|
2448
|
+
async addApprover(pubkey) {
|
|
2449
|
+
if (this.status !== "ready") {
|
|
2450
|
+
throw new Error("kit: addApprover requires a ready, authorized device");
|
|
2451
|
+
}
|
|
2452
|
+
if (await this.adapter.isApprover(this.address, pubkey)) return {};
|
|
2453
|
+
const { transactionHash } = await this.execute([
|
|
2454
|
+
this.adapter.buildAddApprover(this.address, pubkey)
|
|
2455
|
+
]);
|
|
2456
|
+
return { transactionHash };
|
|
2457
|
+
}
|
|
2458
|
+
/**
|
|
2459
|
+
* From a brand-new browser (status `needs-device-approval`), use the user's
|
|
2460
|
+
* synced passkey to authorize adding THIS device — no trip back to an already-
|
|
2461
|
+
* authorized device.
|
|
2462
|
+
*
|
|
2463
|
+
* `add_signer_via_passkey` is a public external authorized by the embedded
|
|
2464
|
+
* WebAuthn assertion (no device signature), so by default we sponsor it through
|
|
2465
|
+
* the Cavos paymaster's `paymaster_executeDirectTransaction` (the forwarder's
|
|
2466
|
+
* `execute_sponsored` runs a generic call — it does NOT require SNIP-9). Pass a
|
|
2467
|
+
* custom `submit` to route it through your own relayer instead. Returns the tx.
|
|
2468
|
+
*/
|
|
2469
|
+
async approveThisDeviceWithPasskey(opts) {
|
|
2470
|
+
if (this.status === "ready") {
|
|
2471
|
+
throw new Error("kit: this device is already an authorized signer");
|
|
2472
|
+
}
|
|
2473
|
+
const { leaf, nonce } = await this.passkeyLeafForThisDevice();
|
|
2474
|
+
const leaves = [leaf];
|
|
2475
|
+
const assertion = await opts.passkey.assert(batchChallenge(leaves));
|
|
2476
|
+
return this.submitPasskeyApproval(assertion, leaves, 0, nonce, opts.submit);
|
|
2477
|
+
}
|
|
2478
|
+
/** This device's leaf + the current passkey nonce, for a (possibly multi-chain)
|
|
2479
|
+
* passkey approval batch. See `approveDeviceEverywhere`. */
|
|
2480
|
+
async passkeyLeafForThisDevice() {
|
|
2481
|
+
const nonce = await this.adapter.getPasskeyNonce(this.address);
|
|
2482
|
+
return { leaf: this.adapter.passkeyLeaf(this.devicePubkey, nonce), nonce };
|
|
2483
|
+
}
|
|
2484
|
+
/** Submit `add_signer_via_passkey` given a (shared) assertion + this chain's
|
|
2485
|
+
* position in the batch. The assertion doesn't carry the passkey pubkey, so we
|
|
2486
|
+
* recover both candidates and pick the enrolled approver via the on-chain view
|
|
2487
|
+
* (no backend). Defaults to sponsoring through the paymaster. */
|
|
2488
|
+
async submitPasskeyApproval(assertion, leaves, leafIndex, nonce, submit) {
|
|
2489
|
+
const digest = webauthnDigest(assertion.authenticatorData, assertion.clientDataJSON);
|
|
2490
|
+
const candidates = recoverCandidatePublicKeys(assertion.r, assertion.s, digest);
|
|
2491
|
+
let yParity = null;
|
|
2492
|
+
for (const cand of candidates) {
|
|
2493
|
+
if (await this.adapter.isApprover(this.address, cand.publicKey)) {
|
|
2494
|
+
yParity = cand.yParity;
|
|
2495
|
+
break;
|
|
2496
|
+
}
|
|
2497
|
+
}
|
|
2498
|
+
if (yParity === null) {
|
|
2499
|
+
throw new Error("kit: this passkey is not a registered approver of the wallet");
|
|
2500
|
+
}
|
|
2501
|
+
const call = this.adapter.buildAddSignerViaPasskey(
|
|
2502
|
+
this.address,
|
|
2503
|
+
this.devicePubkey,
|
|
2504
|
+
nonce,
|
|
2505
|
+
leaves,
|
|
2506
|
+
leafIndex,
|
|
2507
|
+
assertion,
|
|
2508
|
+
yParity
|
|
2509
|
+
);
|
|
2510
|
+
if (submit) return submit(call);
|
|
2511
|
+
if (!this.paymaster) {
|
|
2512
|
+
throw new Error("kit: no paymaster configured \u2014 pass a `submit` relayer to approveThisDeviceWithPasskey");
|
|
2513
|
+
}
|
|
2514
|
+
return paymasterExecuteDirect(this.paymaster, this.address, call);
|
|
2515
|
+
}
|
|
845
2516
|
/**
|
|
846
2517
|
* Register a self-custodial backup signer derived from `code`, so the account
|
|
847
2518
|
* can be recovered after the user loses every device. Idempotent: if the
|
|
@@ -868,13 +2539,14 @@ var Cavos = class _Cavos {
|
|
|
868
2539
|
* re-derive the backup key. The backend never sees the code.
|
|
869
2540
|
*/
|
|
870
2541
|
static async recover(opts) {
|
|
871
|
-
const
|
|
872
|
-
|
|
2542
|
+
const network = STARKNET_ENV[opts.network];
|
|
2543
|
+
const classHash = opts.classHash ?? DEVICE_ACCOUNT_CLASS_HASH[network];
|
|
2544
|
+
if (!classHash) throw new Error(`kit: no DeviceAccount class hash for ${network}`);
|
|
873
2545
|
const provider = new starknet.RpcProvider({
|
|
874
|
-
nodeUrl: opts.rpcUrl ?? STARKNET_NETWORKS[
|
|
2546
|
+
nodeUrl: opts.rpcUrl ?? STARKNET_NETWORKS[network].rpcUrl
|
|
875
2547
|
});
|
|
876
2548
|
const paymaster = new starknet.PaymasterRpc({
|
|
877
|
-
nodeUrl: opts.paymasterUrl ?? CAVOS_PAYMASTER_URL[
|
|
2549
|
+
nodeUrl: opts.paymasterUrl ?? CAVOS_PAYMASTER_URL[network],
|
|
878
2550
|
headers: { "x-paymaster-api-key": opts.paymasterApiKey }
|
|
879
2551
|
});
|
|
880
2552
|
const signer = opts.createSigner ? await opts.createSigner(`${opts.identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${opts.identity.userId}:${opts.appSalt}` });
|
|
@@ -882,7 +2554,7 @@ var Cavos = class _Cavos {
|
|
|
882
2554
|
const backup = BackupSigner.fromCode(opts.code);
|
|
883
2555
|
const backupAdapter = new StarknetAdapter({ classHash, signer: backup, provider });
|
|
884
2556
|
const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
|
|
885
|
-
const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network
|
|
2557
|
+
const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network }) : defaultRegistry3);
|
|
886
2558
|
const existing = await registry.lookup(opts.identity.userId);
|
|
887
2559
|
if (!existing) {
|
|
888
2560
|
throw new Error("kit: no account found for this identity \u2014 nothing to recover");
|
|
@@ -917,7 +2589,7 @@ var Cavos = class _Cavos {
|
|
|
917
2589
|
return new _Cavos(opts.identity, existing.address, "ready", account, adapter, devicePubkey);
|
|
918
2590
|
}
|
|
919
2591
|
};
|
|
920
|
-
var
|
|
2592
|
+
var defaultRegistry3 = new InMemoryWalletRegistry();
|
|
921
2593
|
var DEVICE_REQUEST_DEDUP_MS = 5 * 60 * 1e3;
|
|
922
2594
|
var lastDeviceRequest = /* @__PURE__ */ new Map();
|
|
923
2595
|
async function isDeployed(provider, address) {
|
|
@@ -928,6 +2600,58 @@ async function isDeployed(provider, address) {
|
|
|
928
2600
|
return false;
|
|
929
2601
|
}
|
|
930
2602
|
}
|
|
2603
|
+
async function approveDeviceEverywhere(wallets, passkey) {
|
|
2604
|
+
const targets = wallets.filter((w) => w.status === "needs-device-approval");
|
|
2605
|
+
if (targets.length === 0) return [];
|
|
2606
|
+
const infos = await Promise.all(targets.map((w) => w.passkeyLeafForThisDevice()));
|
|
2607
|
+
const leaves = infos.map((i) => i.leaf);
|
|
2608
|
+
const assertion = await passkey.assert(batchChallenge(leaves));
|
|
2609
|
+
const out = [];
|
|
2610
|
+
for (let i = 0; i < targets.length; i++) {
|
|
2611
|
+
const { transactionHash } = await targets[i].submitPasskeyApproval(
|
|
2612
|
+
assertion,
|
|
2613
|
+
leaves,
|
|
2614
|
+
i,
|
|
2615
|
+
infos[i].nonce
|
|
2616
|
+
);
|
|
2617
|
+
out.push({ chain: targets[i].chain, transactionHash });
|
|
2618
|
+
}
|
|
2619
|
+
return out;
|
|
2620
|
+
}
|
|
2621
|
+
async function paymasterExecuteDirect(paymaster, userAddress, call) {
|
|
2622
|
+
const body = {
|
|
2623
|
+
jsonrpc: "2.0",
|
|
2624
|
+
id: 1,
|
|
2625
|
+
method: "paymaster_executeDirectTransaction",
|
|
2626
|
+
params: {
|
|
2627
|
+
transaction: {
|
|
2628
|
+
type: "invoke",
|
|
2629
|
+
invoke: {
|
|
2630
|
+
user_address: userAddress,
|
|
2631
|
+
execute_from_outside_call: {
|
|
2632
|
+
to: call.contractAddress,
|
|
2633
|
+
selector: starknet.hash.getSelectorFromName(call.entrypoint),
|
|
2634
|
+
calldata: call.calldata.map((c) => starknet.num.toHex(c))
|
|
2635
|
+
}
|
|
2636
|
+
}
|
|
2637
|
+
},
|
|
2638
|
+
parameters: { version: "0x1", fee_mode: { mode: "sponsored" } }
|
|
2639
|
+
}
|
|
2640
|
+
};
|
|
2641
|
+
const res = await fetch(paymaster.url, {
|
|
2642
|
+
method: "POST",
|
|
2643
|
+
headers: {
|
|
2644
|
+
"Content-Type": "application/json",
|
|
2645
|
+
...paymaster.apiKey ? { "x-paymaster-api-key": paymaster.apiKey } : {}
|
|
2646
|
+
},
|
|
2647
|
+
body: JSON.stringify(body)
|
|
2648
|
+
});
|
|
2649
|
+
const json = await res.json();
|
|
2650
|
+
if (json.error) {
|
|
2651
|
+
throw new Error(`kit: paymaster passkey approval failed: ${JSON.stringify(json.error)}`);
|
|
2652
|
+
}
|
|
2653
|
+
return { transactionHash: json.result?.transaction_hash ?? json.result?.tracking_id };
|
|
2654
|
+
}
|
|
931
2655
|
|
|
932
2656
|
// src/auth/AuthProvider.ts
|
|
933
2657
|
var StaticIdentity = class {
|
|
@@ -1075,29 +2799,143 @@ function bytesToChunks(bytes) {
|
|
|
1075
2799
|
for (const b of bytes.subarray(0, 31)) w = w << 8n | BigInt(b);
|
|
1076
2800
|
return w;
|
|
1077
2801
|
}
|
|
2802
|
+
var PasskeySigner = class {
|
|
2803
|
+
constructor(opts = {}) {
|
|
2804
|
+
if (typeof window === "undefined" || !navigator.credentials) {
|
|
2805
|
+
throw new Error("kit/passkey: WebAuthn is only available in a browser");
|
|
2806
|
+
}
|
|
2807
|
+
this.rpId = opts.rpId ?? window.location.hostname;
|
|
2808
|
+
this.rpName = opts.rpName ?? this.rpId;
|
|
2809
|
+
if (isIpAddress(this.rpId)) {
|
|
2810
|
+
throw new Error(
|
|
2811
|
+
`kit/passkey: passkeys can't use an IP address as the domain ("${this.rpId}"). Use http://localhost, a real HTTPS domain, or a tunnel (cloudflared/ngrok) \u2014 or pass an explicit \`rpId\`. (The silent device key works over an IP; passkeys don't.)`
|
|
2812
|
+
);
|
|
2813
|
+
}
|
|
2814
|
+
}
|
|
2815
|
+
/** True if this platform advertises a usable passkey (platform authenticator). */
|
|
2816
|
+
static async isSupported() {
|
|
2817
|
+
if (typeof window === "undefined" || !window.PublicKeyCredential) return false;
|
|
2818
|
+
try {
|
|
2819
|
+
return await window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable();
|
|
2820
|
+
} catch {
|
|
2821
|
+
return false;
|
|
2822
|
+
}
|
|
2823
|
+
}
|
|
2824
|
+
/** Create a new synced passkey and return its P-256 public key. */
|
|
2825
|
+
async enroll(params) {
|
|
2826
|
+
const challenge = crypto.getRandomValues(new Uint8Array(32));
|
|
2827
|
+
const cred = await navigator.credentials.create({
|
|
2828
|
+
publicKey: {
|
|
2829
|
+
challenge: buf(challenge),
|
|
2830
|
+
rp: { id: this.rpId, name: this.rpName },
|
|
2831
|
+
user: {
|
|
2832
|
+
id: buf(userHandle(params.userId)),
|
|
2833
|
+
name: params.userName,
|
|
2834
|
+
displayName: params.displayName ?? params.userName
|
|
2835
|
+
},
|
|
2836
|
+
pubKeyCredParams: [{ type: "public-key", alg: -7 }],
|
|
2837
|
+
// ES256 (P-256)
|
|
2838
|
+
authenticatorSelection: {
|
|
2839
|
+
residentKey: "required",
|
|
2840
|
+
requireResidentKey: true,
|
|
2841
|
+
userVerification: "preferred"
|
|
2842
|
+
},
|
|
2843
|
+
attestation: "none"
|
|
2844
|
+
}
|
|
2845
|
+
});
|
|
2846
|
+
if (!cred) throw new Error("kit/passkey: enrollment cancelled");
|
|
2847
|
+
const response = cred.response;
|
|
2848
|
+
const spki = new Uint8Array(response.getPublicKey());
|
|
2849
|
+
return { publicKey: spkiToPublicKey(spki), credentialId: new Uint8Array(cred.rawId) };
|
|
2850
|
+
}
|
|
2851
|
+
/**
|
|
2852
|
+
* Produce a WebAuthn assertion over `challenge` (a 32-byte value the caller
|
|
2853
|
+
* derives from the signer being added + the on-chain nonce). Uses discoverable
|
|
2854
|
+
* credentials — no `allowCredentials` — so it works on a brand-new browser.
|
|
2855
|
+
*/
|
|
2856
|
+
async assert(challenge) {
|
|
2857
|
+
const cred = await navigator.credentials.get({
|
|
2858
|
+
publicKey: {
|
|
2859
|
+
challenge: buf(challenge),
|
|
2860
|
+
rpId: this.rpId,
|
|
2861
|
+
allowCredentials: [],
|
|
2862
|
+
userVerification: "preferred"
|
|
2863
|
+
}
|
|
2864
|
+
});
|
|
2865
|
+
if (!cred) throw new Error("kit/passkey: assertion cancelled");
|
|
2866
|
+
const response = cred.response;
|
|
2867
|
+
const authenticatorData = new Uint8Array(response.authenticatorData);
|
|
2868
|
+
const clientDataJSON = new Uint8Array(response.clientDataJSON);
|
|
2869
|
+
const { r, s } = derToRs(new Uint8Array(response.signature));
|
|
2870
|
+
const challengeOffset = challengeOffsetOf(clientDataJSON, base64urlEncode(challenge));
|
|
2871
|
+
return { authenticatorData, clientDataJSON, r, s, challengeOffset };
|
|
2872
|
+
}
|
|
2873
|
+
};
|
|
2874
|
+
function isIpAddress(host) {
|
|
2875
|
+
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(host)) return true;
|
|
2876
|
+
if (host.includes(":")) return true;
|
|
2877
|
+
return false;
|
|
2878
|
+
}
|
|
2879
|
+
function userHandle(userId) {
|
|
2880
|
+
const bytes = new TextEncoder().encode(userId);
|
|
2881
|
+
return bytes.length <= 64 ? bytes : sha256.sha256(bytes);
|
|
2882
|
+
}
|
|
2883
|
+
function buf(bytes) {
|
|
2884
|
+
return bytes.slice();
|
|
2885
|
+
}
|
|
1078
2886
|
|
|
1079
2887
|
exports.BackupSigner = BackupSigner;
|
|
1080
2888
|
exports.Cavos = Cavos;
|
|
1081
2889
|
exports.CavosAuth = CavosAuth;
|
|
2890
|
+
exports.CavosSolana = CavosSolana;
|
|
2891
|
+
exports.CavosStellar = CavosStellar;
|
|
1082
2892
|
exports.DEVICE_ACCOUNT_CLASS_HASH = DEVICE_ACCOUNT_CLASS_HASH;
|
|
2893
|
+
exports.DEVICE_ACCOUNT_PROGRAM_ID = DEVICE_ACCOUNT_PROGRAM_ID;
|
|
2894
|
+
exports.DEVICE_ACCOUNT_WASM_HASH = DEVICE_ACCOUNT_WASM_HASH;
|
|
2895
|
+
exports.FACTORY_CONTRACT_ID = FACTORY_CONTRACT_ID;
|
|
1083
2896
|
exports.HttpRecoveryClient = HttpRecoveryClient;
|
|
1084
2897
|
exports.HttpWalletRegistry = HttpWalletRegistry;
|
|
1085
2898
|
exports.InMemoryWalletRegistry = InMemoryWalletRegistry;
|
|
2899
|
+
exports.NATIVE_SAC_ID = NATIVE_SAC_ID;
|
|
2900
|
+
exports.PasskeySigner = PasskeySigner;
|
|
2901
|
+
exports.SECP256R1_PROGRAM_ID = SECP256R1_PROGRAM_ID;
|
|
2902
|
+
exports.SOLANA_NETWORKS = SOLANA_NETWORKS;
|
|
1086
2903
|
exports.STARKNET_NETWORKS = STARKNET_NETWORKS;
|
|
2904
|
+
exports.STELLAR_NETWORKS = STELLAR_NETWORKS;
|
|
2905
|
+
exports.SolanaAdapter = SolanaAdapter;
|
|
2906
|
+
exports.SolanaRelayer = SolanaRelayer;
|
|
1087
2907
|
exports.StarknetAdapter = StarknetAdapter;
|
|
1088
2908
|
exports.StarknetDeviceSigner = StarknetDeviceSigner;
|
|
1089
2909
|
exports.StaticIdentity = StaticIdentity;
|
|
2910
|
+
exports.StellarAdapter = StellarAdapter;
|
|
2911
|
+
exports.StellarRelayer = StellarRelayer;
|
|
1090
2912
|
exports.UDC_ADDRESS = UDC_ADDRESS;
|
|
1091
2913
|
exports.WebCryptoSigner = WebCryptoSigner;
|
|
2914
|
+
exports.anchorDiscriminator = anchorDiscriminator;
|
|
2915
|
+
exports.approveDeviceEverywhere = approveDeviceEverywhere;
|
|
2916
|
+
exports.base64urlEncode = base64urlEncode;
|
|
2917
|
+
exports.batchChallenge = batchChallenge;
|
|
1092
2918
|
exports.bigIntTo32Bytes = bigIntTo32Bytes;
|
|
2919
|
+
exports.buildSecp256r1Instruction = buildSecp256r1Instruction;
|
|
1093
2920
|
exports.bytesToBigInt = bytesToBigInt;
|
|
1094
2921
|
exports.bytesToHex = bytesToHex;
|
|
2922
|
+
exports.compressedPubkey = compressedPubkey;
|
|
1095
2923
|
exports.deriveAddressSeed = deriveAddressSeed;
|
|
2924
|
+
exports.deriveAddressSeedSolana = deriveAddressSeedSolana;
|
|
2925
|
+
exports.deriveAddressSeedStellar = deriveAddressSeedStellar;
|
|
1096
2926
|
exports.deriveBackupKey = deriveBackupKey;
|
|
2927
|
+
exports.deviceSignatureScVal = deviceSignatureScVal;
|
|
2928
|
+
exports.encodeLowSSignature = encodeLowSSignature;
|
|
2929
|
+
exports.encodeStellarLowSSignature = encodeLowSSignature2;
|
|
1097
2930
|
exports.generateRecoveryCode = generateRecoveryCode;
|
|
1098
2931
|
exports.hexToBytes = hexToBytes;
|
|
2932
|
+
exports.lowS = lowS;
|
|
2933
|
+
exports.recoverCandidatePublicKeys = recoverCandidatePublicKeys;
|
|
1099
2934
|
exports.recoverYParity = recoverYParity;
|
|
2935
|
+
exports.sec1Pubkey = sec1Pubkey;
|
|
2936
|
+
exports.serializeInstructions = serializeInstructions;
|
|
1100
2937
|
exports.signatureToFelts = signatureToFelts;
|
|
1101
2938
|
exports.u256ToFelts = u256ToFelts;
|
|
2939
|
+
exports.webauthnDigest = webauthnDigest;
|
|
1102
2940
|
//# sourceMappingURL=index.js.map
|
|
1103
2941
|
//# sourceMappingURL=index.js.map
|