@cavos/kit 0.0.4 → 0.0.5

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.
@@ -5,7 +5,7 @@ import { PublicKey, TransactionInstruction, SystemProgram, SYSVAR_INSTRUCTIONS_P
5
5
  import { hkdf } from '@noble/hashes/hkdf';
6
6
  import { pbkdf2 } from '@noble/hashes/pbkdf2';
7
7
  import { randomBytes } from '@noble/hashes/utils';
8
- import { rpc, hash as hash$1, xdr, Address, StrKey, nativeToScVal, Operation, scValToNative, Account, TransactionBuilder, BASE_FEE } from '@stellar/stellar-sdk';
8
+ import { Horizon, TransactionBuilder, BASE_FEE, Operation, Asset, Account, Keypair } from '@stellar/stellar-sdk';
9
9
 
10
10
  // src/crypto/encoding.ts
11
11
  var U128_MASK = (1n << 128n) - 1n;
@@ -23,8 +23,8 @@ function bytesToHex(bytes) {
23
23
  return s;
24
24
  }
25
25
  function hexToBytes(hex) {
26
- const clean = hex.startsWith("0x") ? hex.slice(2) : hex;
27
- const padded = clean.length % 2 ? "0" + clean : clean;
26
+ const clean2 = hex.startsWith("0x") ? hex.slice(2) : hex;
27
+ const padded = clean2.length % 2 ? "0" + clean2 : clean2;
28
28
  const out = new Uint8Array(padded.length / 2);
29
29
  for (let i = 0; i < out.length; i++) out[i] = parseInt(padded.slice(i * 2, i * 2 + 2), 16);
30
30
  return out;
@@ -176,7 +176,7 @@ var CAVOS_PAYMASTER_URL = {
176
176
  mainnet: "https://paymaster.cavos.xyz"
177
177
  };
178
178
  var DEVICE_ACCOUNT_CLASS_HASH = {
179
- sepolia: "0x25cbc5423e8ee895febb0ef2c3945b408da44d0039d915fbdd681fe6b6ba66b",
179
+ sepolia: "0x765f000b7021577aa0d6c206fb3d8ac73571686b3e76b9dc9b6d59a372e8c2c",
180
180
  mainnet: "0x1840aded59e8a0d2b440a134cb9079a7fc11b06c77f58ed189ab436a034ca6a"
181
181
  };
182
182
  var StarknetAdapter = class {
@@ -184,21 +184,34 @@ var StarknetAdapter = class {
184
184
  this.opts = opts;
185
185
  this.chain = "starknet";
186
186
  }
187
- computeAddress({ addressSeed, initialSigner, salt }) {
187
+ /**
188
+ * Deterministic address = f(addressSeed) ONLY. The device pubkey is NOT
189
+ * part of the derivation — anti-squatting is the integrator's responsibility
190
+ * (keep `appSalt` secret; deploy on first login). This makes the address
191
+ * recomputable by the user from (userId, appSalt) alone, even after losing
192
+ * every device.
193
+ *
194
+ * `initialSigner` in `ComputeAddressParams` is IGNORED on Starknet (kept in
195
+ * the shared type for Solana/Stellar, which still include it).
196
+ */
197
+ computeAddress({ addressSeed, salt }) {
188
198
  return hash.calculateContractAddressFromHash(
189
199
  num.toHex(salt ?? addressSeed),
190
200
  this.opts.classHash,
191
- this.constructorCalldata(addressSeed, initialSigner),
201
+ this.constructorCalldata(addressSeed),
192
202
  0
193
203
  // deployerAddress 0 => deterministic counterfactual address
194
204
  );
195
205
  }
196
- /** Single UDC deploy; the constructor registers the first device signer, so
197
- * the account is ready the moment it is deployed (fits the paymaster's
198
- * deploy + execute_from_outside bundle). */
206
+ /**
207
+ * UDC deploy call. The constructor takes ONLY the seed no device pubkey
208
+ * so the account is born with no signers. The caller MUST follow up with
209
+ * `buildInitialize` in the same multicall (or a separate tx) to register the
210
+ * first device signer; otherwise the account is unusable.
211
+ */
199
212
  buildDeploy(params) {
200
213
  const salt = params.salt ?? params.addressSeed;
201
- const calldata = this.constructorCalldata(params.addressSeed, params.initialSigner);
214
+ const calldata = this.constructorCalldata(params.addressSeed);
202
215
  return [
203
216
  {
204
217
  contractAddress: UDC_ADDRESS,
@@ -214,9 +227,22 @@ var StarknetAdapter = class {
214
227
  }
215
228
  ];
216
229
  }
217
- /** Constructor calldata: [address_seed, pub_x_low, pub_x_high, pub_y_low, pub_y_high]. */
218
- constructorCalldata(addressSeed, initialSigner) {
219
- return [num.toHex(addressSeed), ...pubkeyCalldata(initialSigner)];
230
+ /** Constructor calldata: [address_seed]. Device pubkey is registered post-deploy via initialize. */
231
+ constructorCalldata(addressSeed) {
232
+ return [num.toHex(addressSeed)];
233
+ }
234
+ /**
235
+ * `initialize` call: registers the first device signer. Callable only while
236
+ * the account has no signers (one-shot). In production this is bundled with
237
+ * the UDC deploy in a single sponsored multicall — see `connectStarknet`.
238
+ * Anti-squatting is NOT enforced on-chain.
239
+ */
240
+ buildInitialize(accountAddress, devicePubkey) {
241
+ return {
242
+ contractAddress: accountAddress,
243
+ entrypoint: "initialize",
244
+ calldata: pubkeyCalldata(devicePubkey)
245
+ };
220
246
  }
221
247
  buildAddSigner(accountAddress, signer) {
222
248
  return { contractAddress: accountAddress, entrypoint: "add_signer", calldata: pubkeyCalldata(signer) };
@@ -450,26 +476,30 @@ var SolanaAdapter = class {
450
476
  this.chain = "solana";
451
477
  this.programId = new PublicKey(opts.programId ?? DEVICE_ACCOUNT_PROGRAM_ID);
452
478
  }
453
- /** Deterministic account address: PDA of [seed, address_seed, initial_signer_x]. */
454
- computeAddress(addressSeed, initialSigner) {
455
- return this.pda(addressSeed, compressedPubkey(initialSigner)).toBase58();
479
+ /**
480
+ * Deterministic account address: PDA of [ACCOUNT_SEED, addressSeed] — the
481
+ * device pubkey is NOT part of the seeds, so the address is recomputable
482
+ * from (userId, appSalt) alone. Anti-squatting is the integrator's
483
+ * responsibility (keep `appSalt` secret; deploy on first login).
484
+ */
485
+ computeAddress(addressSeed) {
486
+ return this.pda(addressSeed).toBase58();
456
487
  }
457
- pda(addressSeed, initialCompressed) {
488
+ pda(addressSeed) {
458
489
  const [pda] = PublicKey.findProgramAddressSync(
459
- [
460
- Buffer.from(ACCOUNT_SEED),
461
- Buffer.from(addressSeed),
462
- Buffer.from(initialCompressed.slice(1, 33))
463
- // x-coordinate
464
- ],
490
+ [Buffer.from(ACCOUNT_SEED), Buffer.from(addressSeed)],
465
491
  this.programId
466
492
  );
467
493
  return pda;
468
494
  }
469
- /** `initialize` instruction creating the account with its first device signer. */
495
+ /**
496
+ * `initialize` instruction: creates the account PDA and registers the first
497
+ * device signer. No attestation is required — anti-squatting is NOT enforced
498
+ * on-chain.
499
+ */
470
500
  buildInitialize(addressSeed, payer, initialSigner) {
471
501
  const initialCompressed = compressedPubkey(initialSigner);
472
- const account = this.pda(addressSeed, initialCompressed);
502
+ const account = this.pda(addressSeed);
473
503
  const data = Buffer.concat([
474
504
  anchorDiscriminator("initialize"),
475
505
  Buffer.from(addressSeed),
@@ -477,7 +507,7 @@ var SolanaAdapter = class {
477
507
  Buffer.from(initialCompressed)
478
508
  // [u8;33]
479
509
  ]);
480
- return new TransactionInstruction({
510
+ const programIx = new TransactionInstruction({
481
511
  programId: this.programId,
482
512
  keys: [
483
513
  { pubkey: account, isSigner: false, isWritable: true },
@@ -486,6 +516,7 @@ var SolanaAdapter = class {
486
516
  ],
487
517
  data
488
518
  });
519
+ return [programIx];
489
520
  }
490
521
  /** `[precompile, add_signer]` bundle, authorized by an existing device signer. */
491
522
  async buildAddSigner(account, newSigner) {
@@ -824,8 +855,8 @@ function u64le(n) {
824
855
  new DataView(b.buffer, b.byteOffset, 8).setBigUint64(0, BigInt(n), true);
825
856
  return b;
826
857
  }
827
- function readU64le(buf2, offset) {
828
- return new DataView(buf2.buffer, buf2.byteOffset, buf2.length).getBigUint64(
858
+ function readU64le(buf3, offset) {
859
+ return new DataView(buf3.buffer, buf3.byteOffset, buf3.length).getBigUint64(
829
860
  offset,
830
861
  true
831
862
  );
@@ -884,11 +915,11 @@ var SolanaRelayer = class {
884
915
  async send(instructions) {
885
916
  const feePayer = await this.getFeePayer();
886
917
  const { blockhash } = await this.opts.connection.getLatestBlockhash("confirmed");
887
- const tx2 = new Transaction();
888
- tx2.feePayer = feePayer;
889
- tx2.recentBlockhash = blockhash;
890
- tx2.add(...instructions);
891
- const serialized = tx2.serialize({ requireAllSignatures: false, verifySignatures: false }).toString("base64");
918
+ const tx3 = new Transaction();
919
+ tx3.feePayer = feePayer;
920
+ tx3.recentBlockhash = blockhash;
921
+ tx3.add(...instructions);
922
+ const serialized = tx3.serialize({ requireAllSignatures: false, verifySignatures: false }).toString("base64");
892
923
  const res = await fetch(`${this.opts.baseUrl}/api/solana/relay`, {
893
924
  method: "POST",
894
925
  headers: { "Content-Type": "application/json" },
@@ -1332,16 +1363,16 @@ var CavosSolana = class _CavosSolana {
1332
1363
  opts.feePayer
1333
1364
  );
1334
1365
  }
1335
- const address = adapter.computeAddress(addressSeed, devicePubkey);
1366
+ const address = adapter.computeAddress(addressSeed);
1336
1367
  const deployed = await connection.getAccountInfo(new PublicKey(address)) !== null;
1337
1368
  if (!deployed) {
1338
1369
  if (relayer) {
1339
1370
  const payer = await relayer.getFeePayer();
1340
- const ix = adapter.buildInitialize(addressSeed, payer.toBase58(), devicePubkey);
1341
- await relayer.send([ix]);
1371
+ const ixs = adapter.buildInitialize(addressSeed, payer.toBase58(), devicePubkey);
1372
+ await relayer.send(ixs);
1342
1373
  } else if (opts.feePayer) {
1343
- const ix = adapter.buildInitialize(addressSeed, opts.feePayer.publicKey.toBase58(), devicePubkey);
1344
- await sendAndConfirmTransaction(connection, new Transaction().add(ix), [opts.feePayer]);
1374
+ const ixs = adapter.buildInitialize(addressSeed, opts.feePayer.publicKey.toBase58(), devicePubkey);
1375
+ await sendAndConfirmTransaction(connection, new Transaction().add(...ixs), [opts.feePayer]);
1345
1376
  } else {
1346
1377
  throw new Error("kit/solana: a relayer (appId) or feePayer is required to initialize a new account");
1347
1378
  }
@@ -1440,12 +1471,12 @@ var CavosSolana = class _CavosSolana {
1440
1471
  return { transactionHash: await this.send(ixs) };
1441
1472
  }
1442
1473
  /** Move `amount` lamports out of the account to `destination` (device-signed). */
1443
- async execute(amount, destination) {
1474
+ async execute(amount, destination, opts) {
1444
1475
  if (this.status !== "ready") {
1445
1476
  throw new Error("kit/solana: this device is not yet an authorized signer of the wallet");
1446
1477
  }
1447
1478
  const ixs = await this.adapter.buildExecuteTransfer(this.address, destination, amount);
1448
- return this.send(ixs);
1479
+ return this.send(ixs, opts);
1449
1480
  }
1450
1481
  /**
1451
1482
  * Run arbitrary CPI `instructions` with the account PDA as signer (device-
@@ -1455,14 +1486,16 @@ var CavosSolana = class _CavosSolana {
1455
1486
  *
1456
1487
  * What the relayer will sponsor is constrained by the app's Solana program
1457
1488
  * allowlist (configured in the dashboard) — programs outside the allowlist are
1458
- * rejected before co-signing.
1489
+ * rejected before co-signing. Pass `{ sponsored: false }` to bypass the relayer
1490
+ * and pay the fee from a configured `feePayer` (e.g. for allowlisted programs
1491
+ * the relayer rejects, or to test the device signature end-to-end).
1459
1492
  */
1460
- async executeInstructions(instructions) {
1493
+ async executeInstructions(instructions, opts) {
1461
1494
  if (this.status !== "ready") {
1462
1495
  throw new Error("kit/solana: this device is not yet an authorized signer of the wallet");
1463
1496
  }
1464
1497
  const ixs = await this.adapter.buildExecute(this.address, instructions);
1465
- return this.send(ixs);
1498
+ return this.send(ixs, opts);
1466
1499
  }
1467
1500
  /**
1468
1501
  * Register the backup signer derived from `code` as an authorized signer of this
@@ -1541,321 +1574,982 @@ var CavosSolana = class _CavosSolana {
1541
1574
  opts.feePayer
1542
1575
  );
1543
1576
  }
1544
- async send(ixs) {
1545
- if (this.relayer) return this.relayer.send(ixs);
1577
+ /**
1578
+ * Submit a built instruction bundle. Sponsored by default (relayer pays the
1579
+ * fee); pass `{ sponsored: false }` to self-fund via the configured `feePayer`.
1580
+ * The device signature is embedded inside the secp256r1 precompile instruction,
1581
+ * NOT applied as a Solana tx signature — so switching only changes who pays,
1582
+ * never the signing identity.
1583
+ */
1584
+ async send(ixs, opts) {
1585
+ const sponsored = opts?.sponsored !== false;
1586
+ if (sponsored && this.relayer) return this.relayer.send(ixs);
1546
1587
  if (this.feePayer) {
1547
1588
  return sendAndConfirmTransaction(this.connection, new Transaction().add(...ixs), [this.feePayer]);
1548
1589
  }
1549
- throw new Error("kit/solana: no relayer or feePayer configured to submit transactions");
1590
+ throw new Error(
1591
+ `kit/solana: cannot ${sponsored ? "sponsor" : "self-fund"} \u2014 no ${sponsored ? "relayer" : "feePayer"} configured`
1592
+ );
1550
1593
  }
1551
1594
  };
1552
1595
  var defaultRegistry = new InMemoryWalletRegistry();
1553
1596
 
1554
1597
  // src/chains/stellar/constants.ts
1555
- var FACTORY_CONTRACT_ID = {
1556
- // Re-deployed 2026-07-01 with the passkey-approval device-account wasm (batched
1557
- // multi-chain challenge). The factory pins the wasm hash immutably, so a new
1558
- // wasm needs a new factory → new account addresses; testnet has no prod wallets.
1559
- "stellar-testnet": "CBCJIODXIEBOXXD66KCUCF7ZDYJARKI4ZIVQOVWPULOBH5XGNCDP6W3I",
1560
- // Set once the factory is deployed to mainnet (its address differs — network id
1561
- // is part of contract-address derivation).
1562
- "stellar-mainnet": ""
1563
- };
1564
- var DEVICE_ACCOUNT_WASM_HASH = {
1565
- "stellar-testnet": "2671b085578e59a385ef5a5664e42f0450322fe3249539f588e1263ed5a31dce",
1566
- "stellar-mainnet": ""
1567
- };
1568
1598
  var STELLAR_NETWORKS = {
1569
1599
  "stellar-testnet": {
1570
- rpcUrl: "https://soroban-testnet.stellar.org",
1571
1600
  passphrase: "Test SDF Network ; September 2015"
1572
1601
  },
1573
1602
  "stellar-mainnet": {
1574
- rpcUrl: "https://soroban-rpc.mainnet.stellar.gateway.fm",
1575
1603
  passphrase: "Public Global Stellar Network ; September 2015"
1576
1604
  }
1577
1605
  };
1578
- var NATIVE_SAC_ID = {
1579
- "stellar-testnet": "CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC",
1580
- "stellar-mainnet": "CAS3J7GYLGXMF6TDJBBYYSE3HQ6BBSMLNUQ34T6TZMYMW2EVH34XOWMA"
1606
+ var HORIZON_URL = {
1607
+ "stellar-testnet": "https://horizon-testnet.stellar.org",
1608
+ "stellar-mainnet": "https://horizon.stellar.org"
1609
+ };
1610
+ var XLM_DECIMALS = 7;
1611
+
1612
+ // node_modules/@noble/ciphers/esm/utils.js
1613
+ function isBytes(a) {
1614
+ return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
1615
+ }
1616
+ function abytes(b, ...lengths) {
1617
+ if (!isBytes(b))
1618
+ throw new Error("Uint8Array expected");
1619
+ if (lengths.length > 0 && !lengths.includes(b.length))
1620
+ throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
1621
+ }
1622
+ function aexists(instance, checkFinished = true) {
1623
+ if (instance.destroyed)
1624
+ throw new Error("Hash instance has been destroyed");
1625
+ if (checkFinished && instance.finished)
1626
+ throw new Error("Hash#digest() has already been called");
1627
+ }
1628
+ function aoutput(out, instance) {
1629
+ abytes(out);
1630
+ const min = instance.outputLen;
1631
+ if (out.length < min) {
1632
+ throw new Error("digestInto() expects output buffer of length at least " + min);
1633
+ }
1634
+ }
1635
+ function u8(arr) {
1636
+ return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
1637
+ }
1638
+ function u32(arr) {
1639
+ return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
1640
+ }
1641
+ function clean(...arrays) {
1642
+ for (let i = 0; i < arrays.length; i++) {
1643
+ arrays[i].fill(0);
1644
+ }
1645
+ }
1646
+ function createView(arr) {
1647
+ return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
1648
+ }
1649
+ var isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
1650
+ function utf8ToBytes(str) {
1651
+ if (typeof str !== "string")
1652
+ throw new Error("string expected");
1653
+ return new Uint8Array(new TextEncoder().encode(str));
1654
+ }
1655
+ function toBytes(data) {
1656
+ if (typeof data === "string")
1657
+ data = utf8ToBytes(data);
1658
+ else if (isBytes(data))
1659
+ data = copyBytes(data);
1660
+ else
1661
+ throw new Error("Uint8Array expected, got " + typeof data);
1662
+ return data;
1663
+ }
1664
+ function equalBytes(a, b) {
1665
+ if (a.length !== b.length)
1666
+ return false;
1667
+ let diff = 0;
1668
+ for (let i = 0; i < a.length; i++)
1669
+ diff |= a[i] ^ b[i];
1670
+ return diff === 0;
1671
+ }
1672
+ var wrapCipher = /* @__NO_SIDE_EFFECTS__ */ (params, constructor) => {
1673
+ function wrappedCipher(key, ...args) {
1674
+ abytes(key);
1675
+ if (!isLE)
1676
+ throw new Error("Non little-endian hardware is not yet supported");
1677
+ if (params.nonceLength !== void 0) {
1678
+ const nonce = args[0];
1679
+ if (!nonce)
1680
+ throw new Error("nonce / iv required");
1681
+ if (params.varSizeNonce)
1682
+ abytes(nonce);
1683
+ else
1684
+ abytes(nonce, params.nonceLength);
1685
+ }
1686
+ const tagl = params.tagLength;
1687
+ if (tagl && args[1] !== void 0) {
1688
+ abytes(args[1]);
1689
+ }
1690
+ const cipher = constructor(key, ...args);
1691
+ const checkOutput = (fnLength, output) => {
1692
+ if (output !== void 0) {
1693
+ if (fnLength !== 2)
1694
+ throw new Error("cipher output not supported");
1695
+ abytes(output);
1696
+ }
1697
+ };
1698
+ let called = false;
1699
+ const wrCipher = {
1700
+ encrypt(data, output) {
1701
+ if (called)
1702
+ throw new Error("cannot encrypt() twice with same key + nonce");
1703
+ called = true;
1704
+ abytes(data);
1705
+ checkOutput(cipher.encrypt.length, output);
1706
+ return cipher.encrypt(data, output);
1707
+ },
1708
+ decrypt(data, output) {
1709
+ abytes(data);
1710
+ if (tagl && data.length < tagl)
1711
+ throw new Error("invalid ciphertext length: smaller than tagLength=" + tagl);
1712
+ checkOutput(cipher.decrypt.length, output);
1713
+ return cipher.decrypt(data, output);
1714
+ }
1715
+ };
1716
+ return wrCipher;
1717
+ }
1718
+ Object.assign(wrappedCipher, params);
1719
+ return wrappedCipher;
1720
+ };
1721
+ function getOutput(expectedLength, out, onlyAligned = true) {
1722
+ if (out === void 0)
1723
+ return new Uint8Array(expectedLength);
1724
+ if (out.length !== expectedLength)
1725
+ throw new Error("invalid output length, expected " + expectedLength + ", got: " + out.length);
1726
+ if (onlyAligned && !isAligned32(out))
1727
+ throw new Error("invalid output, must be aligned");
1728
+ return out;
1729
+ }
1730
+ function setBigUint64(view, byteOffset, value, isLE2) {
1731
+ if (typeof view.setBigUint64 === "function")
1732
+ return view.setBigUint64(byteOffset, value, isLE2);
1733
+ const _32n = BigInt(32);
1734
+ const _u32_max = BigInt(4294967295);
1735
+ const wh = Number(value >> _32n & _u32_max);
1736
+ const wl = Number(value & _u32_max);
1737
+ const h = 0;
1738
+ const l = 4;
1739
+ view.setUint32(byteOffset + h, wh, isLE2);
1740
+ view.setUint32(byteOffset + l, wl, isLE2);
1741
+ }
1742
+ function u64Lengths(dataLength, aadLength, isLE2) {
1743
+ const num5 = new Uint8Array(16);
1744
+ const view = createView(num5);
1745
+ setBigUint64(view, 0, BigInt(aadLength), isLE2);
1746
+ setBigUint64(view, 8, BigInt(dataLength), isLE2);
1747
+ return num5;
1748
+ }
1749
+ function isAligned32(bytes) {
1750
+ return bytes.byteOffset % 4 === 0;
1751
+ }
1752
+ function copyBytes(bytes) {
1753
+ return Uint8Array.from(bytes);
1754
+ }
1755
+
1756
+ // node_modules/@noble/ciphers/esm/_polyval.js
1757
+ var BLOCK_SIZE = 16;
1758
+ var ZEROS16 = /* @__PURE__ */ new Uint8Array(16);
1759
+ var ZEROS32 = u32(ZEROS16);
1760
+ var POLY = 225;
1761
+ var mul2 = (s0, s1, s2, s3) => {
1762
+ const hiBit = s3 & 1;
1763
+ return {
1764
+ s3: s2 << 31 | s3 >>> 1,
1765
+ s2: s1 << 31 | s2 >>> 1,
1766
+ s1: s0 << 31 | s1 >>> 1,
1767
+ s0: s0 >>> 1 ^ POLY << 24 & -(hiBit & 1)
1768
+ // reduce % poly
1769
+ };
1770
+ };
1771
+ var swapLE = (n) => (n >>> 0 & 255) << 24 | (n >>> 8 & 255) << 16 | (n >>> 16 & 255) << 8 | n >>> 24 & 255 | 0;
1772
+ function _toGHASHKey(k) {
1773
+ k.reverse();
1774
+ const hiBit = k[15] & 1;
1775
+ let carry = 0;
1776
+ for (let i = 0; i < k.length; i++) {
1777
+ const t = k[i];
1778
+ k[i] = t >>> 1 | carry;
1779
+ carry = (t & 1) << 7;
1780
+ }
1781
+ k[0] ^= -hiBit & 225;
1782
+ return k;
1783
+ }
1784
+ var estimateWindow = (bytes) => {
1785
+ if (bytes > 64 * 1024)
1786
+ return 8;
1787
+ if (bytes > 1024)
1788
+ return 4;
1789
+ return 2;
1790
+ };
1791
+ var GHASH = class {
1792
+ // We select bits per window adaptively based on expectedLength
1793
+ constructor(key, expectedLength) {
1794
+ this.blockLen = BLOCK_SIZE;
1795
+ this.outputLen = BLOCK_SIZE;
1796
+ this.s0 = 0;
1797
+ this.s1 = 0;
1798
+ this.s2 = 0;
1799
+ this.s3 = 0;
1800
+ this.finished = false;
1801
+ key = toBytes(key);
1802
+ abytes(key, 16);
1803
+ const kView = createView(key);
1804
+ let k0 = kView.getUint32(0, false);
1805
+ let k1 = kView.getUint32(4, false);
1806
+ let k2 = kView.getUint32(8, false);
1807
+ let k3 = kView.getUint32(12, false);
1808
+ const doubles = [];
1809
+ for (let i = 0; i < 128; i++) {
1810
+ doubles.push({ s0: swapLE(k0), s1: swapLE(k1), s2: swapLE(k2), s3: swapLE(k3) });
1811
+ ({ s0: k0, s1: k1, s2: k2, s3: k3 } = mul2(k0, k1, k2, k3));
1812
+ }
1813
+ const W = estimateWindow(expectedLength || 1024);
1814
+ if (![1, 2, 4, 8].includes(W))
1815
+ throw new Error("ghash: invalid window size, expected 2, 4 or 8");
1816
+ this.W = W;
1817
+ const bits = 128;
1818
+ const windows = bits / W;
1819
+ const windowSize = this.windowSize = 2 ** W;
1820
+ const items = [];
1821
+ for (let w = 0; w < windows; w++) {
1822
+ for (let byte = 0; byte < windowSize; byte++) {
1823
+ let s0 = 0, s1 = 0, s2 = 0, s3 = 0;
1824
+ for (let j = 0; j < W; j++) {
1825
+ const bit = byte >>> W - j - 1 & 1;
1826
+ if (!bit)
1827
+ continue;
1828
+ const { s0: d0, s1: d1, s2: d2, s3: d3 } = doubles[W * w + j];
1829
+ s0 ^= d0, s1 ^= d1, s2 ^= d2, s3 ^= d3;
1830
+ }
1831
+ items.push({ s0, s1, s2, s3 });
1832
+ }
1833
+ }
1834
+ this.t = items;
1835
+ }
1836
+ _updateBlock(s0, s1, s2, s3) {
1837
+ s0 ^= this.s0, s1 ^= this.s1, s2 ^= this.s2, s3 ^= this.s3;
1838
+ const { W, t, windowSize } = this;
1839
+ let o0 = 0, o1 = 0, o2 = 0, o3 = 0;
1840
+ const mask = (1 << W) - 1;
1841
+ let w = 0;
1842
+ for (const num5 of [s0, s1, s2, s3]) {
1843
+ for (let bytePos = 0; bytePos < 4; bytePos++) {
1844
+ const byte = num5 >>> 8 * bytePos & 255;
1845
+ for (let bitPos = 8 / W - 1; bitPos >= 0; bitPos--) {
1846
+ const bit = byte >>> W * bitPos & mask;
1847
+ const { s0: e0, s1: e1, s2: e2, s3: e3 } = t[w * windowSize + bit];
1848
+ o0 ^= e0, o1 ^= e1, o2 ^= e2, o3 ^= e3;
1849
+ w += 1;
1850
+ }
1851
+ }
1852
+ }
1853
+ this.s0 = o0;
1854
+ this.s1 = o1;
1855
+ this.s2 = o2;
1856
+ this.s3 = o3;
1857
+ }
1858
+ update(data) {
1859
+ aexists(this);
1860
+ data = toBytes(data);
1861
+ abytes(data);
1862
+ const b32 = u32(data);
1863
+ const blocks = Math.floor(data.length / BLOCK_SIZE);
1864
+ const left = data.length % BLOCK_SIZE;
1865
+ for (let i = 0; i < blocks; i++) {
1866
+ this._updateBlock(b32[i * 4 + 0], b32[i * 4 + 1], b32[i * 4 + 2], b32[i * 4 + 3]);
1867
+ }
1868
+ if (left) {
1869
+ ZEROS16.set(data.subarray(blocks * BLOCK_SIZE));
1870
+ this._updateBlock(ZEROS32[0], ZEROS32[1], ZEROS32[2], ZEROS32[3]);
1871
+ clean(ZEROS32);
1872
+ }
1873
+ return this;
1874
+ }
1875
+ destroy() {
1876
+ const { t } = this;
1877
+ for (const elm of t) {
1878
+ elm.s0 = 0, elm.s1 = 0, elm.s2 = 0, elm.s3 = 0;
1879
+ }
1880
+ }
1881
+ digestInto(out) {
1882
+ aexists(this);
1883
+ aoutput(out, this);
1884
+ this.finished = true;
1885
+ const { s0, s1, s2, s3 } = this;
1886
+ const o32 = u32(out);
1887
+ o32[0] = s0;
1888
+ o32[1] = s1;
1889
+ o32[2] = s2;
1890
+ o32[3] = s3;
1891
+ return out;
1892
+ }
1893
+ digest() {
1894
+ const res = new Uint8Array(BLOCK_SIZE);
1895
+ this.digestInto(res);
1896
+ this.destroy();
1897
+ return res;
1898
+ }
1899
+ };
1900
+ var Polyval = class extends GHASH {
1901
+ constructor(key, expectedLength) {
1902
+ key = toBytes(key);
1903
+ abytes(key);
1904
+ const ghKey = _toGHASHKey(copyBytes(key));
1905
+ super(ghKey, expectedLength);
1906
+ clean(ghKey);
1907
+ }
1908
+ update(data) {
1909
+ data = toBytes(data);
1910
+ aexists(this);
1911
+ const b32 = u32(data);
1912
+ const left = data.length % BLOCK_SIZE;
1913
+ const blocks = Math.floor(data.length / BLOCK_SIZE);
1914
+ for (let i = 0; i < blocks; i++) {
1915
+ this._updateBlock(swapLE(b32[i * 4 + 3]), swapLE(b32[i * 4 + 2]), swapLE(b32[i * 4 + 1]), swapLE(b32[i * 4 + 0]));
1916
+ }
1917
+ if (left) {
1918
+ ZEROS16.set(data.subarray(blocks * BLOCK_SIZE));
1919
+ this._updateBlock(swapLE(ZEROS32[3]), swapLE(ZEROS32[2]), swapLE(ZEROS32[1]), swapLE(ZEROS32[0]));
1920
+ clean(ZEROS32);
1921
+ }
1922
+ return this;
1923
+ }
1924
+ digestInto(out) {
1925
+ aexists(this);
1926
+ aoutput(out, this);
1927
+ this.finished = true;
1928
+ const { s0, s1, s2, s3 } = this;
1929
+ const o32 = u32(out);
1930
+ o32[0] = s0;
1931
+ o32[1] = s1;
1932
+ o32[2] = s2;
1933
+ o32[3] = s3;
1934
+ return out.reverse();
1935
+ }
1581
1936
  };
1582
- var SECP256R1_N2 = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n;
1937
+ function wrapConstructorWithKey(hashCons) {
1938
+ const hashC = (msg, key) => hashCons(key, msg.length).update(toBytes(msg)).digest();
1939
+ const tmp = hashCons(new Uint8Array(16), 0);
1940
+ hashC.outputLen = tmp.outputLen;
1941
+ hashC.blockLen = tmp.blockLen;
1942
+ hashC.create = (key, expectedLength) => hashCons(key, expectedLength);
1943
+ return hashC;
1944
+ }
1945
+ var ghash = wrapConstructorWithKey((key, expectedLength) => new GHASH(key, expectedLength));
1946
+ wrapConstructorWithKey((key, expectedLength) => new Polyval(key, expectedLength));
1947
+
1948
+ // node_modules/@noble/ciphers/esm/aes.js
1949
+ var BLOCK_SIZE2 = 16;
1950
+ var BLOCK_SIZE32 = 4;
1951
+ var EMPTY_BLOCK = /* @__PURE__ */ new Uint8Array(BLOCK_SIZE2);
1952
+ var POLY2 = 283;
1953
+ function mul22(n) {
1954
+ return n << 1 ^ POLY2 & -(n >> 7);
1955
+ }
1956
+ function mul(a, b) {
1957
+ let res = 0;
1958
+ for (; b > 0; b >>= 1) {
1959
+ res ^= a & -(b & 1);
1960
+ a = mul22(a);
1961
+ }
1962
+ return res;
1963
+ }
1964
+ var sbox = /* @__PURE__ */ (() => {
1965
+ const t = new Uint8Array(256);
1966
+ for (let i = 0, x = 1; i < 256; i++, x ^= mul22(x))
1967
+ t[i] = x;
1968
+ const box = new Uint8Array(256);
1969
+ box[0] = 99;
1970
+ for (let i = 0; i < 255; i++) {
1971
+ let x = t[255 - i];
1972
+ x |= x << 8;
1973
+ box[t[i]] = (x ^ x >> 4 ^ x >> 5 ^ x >> 6 ^ x >> 7 ^ 99) & 255;
1974
+ }
1975
+ clean(t);
1976
+ return box;
1977
+ })();
1978
+ var rotr32_8 = (n) => n << 24 | n >>> 8;
1979
+ var rotl32_8 = (n) => n << 8 | n >>> 24;
1980
+ function genTtable(sbox2, fn) {
1981
+ if (sbox2.length !== 256)
1982
+ throw new Error("Wrong sbox length");
1983
+ const T0 = new Uint32Array(256).map((_, j) => fn(sbox2[j]));
1984
+ const T1 = T0.map(rotl32_8);
1985
+ const T2 = T1.map(rotl32_8);
1986
+ const T3 = T2.map(rotl32_8);
1987
+ const T01 = new Uint32Array(256 * 256);
1988
+ const T23 = new Uint32Array(256 * 256);
1989
+ const sbox22 = new Uint16Array(256 * 256);
1990
+ for (let i = 0; i < 256; i++) {
1991
+ for (let j = 0; j < 256; j++) {
1992
+ const idx = i * 256 + j;
1993
+ T01[idx] = T0[i] ^ T1[j];
1994
+ T23[idx] = T2[i] ^ T3[j];
1995
+ sbox22[idx] = sbox2[i] << 8 | sbox2[j];
1996
+ }
1997
+ }
1998
+ return { sbox: sbox2, sbox2: sbox22, T0, T1, T2, T3, T01, T23 };
1999
+ }
2000
+ var tableEncoding = /* @__PURE__ */ genTtable(sbox, (s) => mul(s, 3) << 24 | s << 16 | s << 8 | mul(s, 2));
2001
+ var xPowers = /* @__PURE__ */ (() => {
2002
+ const p = new Uint8Array(16);
2003
+ for (let i = 0, x = 1; i < 16; i++, x = mul22(x))
2004
+ p[i] = x;
2005
+ return p;
2006
+ })();
2007
+ function expandKeyLE(key) {
2008
+ abytes(key);
2009
+ const len = key.length;
2010
+ if (![16, 24, 32].includes(len))
2011
+ throw new Error("aes: invalid key size, should be 16, 24 or 32, got " + len);
2012
+ const { sbox2 } = tableEncoding;
2013
+ const toClean = [];
2014
+ if (!isAligned32(key))
2015
+ toClean.push(key = copyBytes(key));
2016
+ const k32 = u32(key);
2017
+ const Nk = k32.length;
2018
+ const subByte = (n) => applySbox(sbox2, n, n, n, n);
2019
+ const xk = new Uint32Array(len + 28);
2020
+ xk.set(k32);
2021
+ for (let i = Nk; i < xk.length; i++) {
2022
+ let t = xk[i - 1];
2023
+ if (i % Nk === 0)
2024
+ t = subByte(rotr32_8(t)) ^ xPowers[i / Nk - 1];
2025
+ else if (Nk > 6 && i % Nk === 4)
2026
+ t = subByte(t);
2027
+ xk[i] = xk[i - Nk] ^ t;
2028
+ }
2029
+ clean(...toClean);
2030
+ return xk;
2031
+ }
2032
+ function apply0123(T01, T23, s0, s1, s2, s3) {
2033
+ return T01[s0 << 8 & 65280 | s1 >>> 8 & 255] ^ T23[s2 >>> 8 & 65280 | s3 >>> 24 & 255];
2034
+ }
2035
+ function applySbox(sbox2, s0, s1, s2, s3) {
2036
+ return sbox2[s0 & 255 | s1 & 65280] | sbox2[s2 >>> 16 & 255 | s3 >>> 16 & 65280] << 16;
2037
+ }
2038
+ function encrypt(xk, s0, s1, s2, s3) {
2039
+ const { sbox2, T01, T23 } = tableEncoding;
2040
+ let k = 0;
2041
+ s0 ^= xk[k++], s1 ^= xk[k++], s2 ^= xk[k++], s3 ^= xk[k++];
2042
+ const rounds = xk.length / 4 - 2;
2043
+ for (let i = 0; i < rounds; i++) {
2044
+ const t02 = xk[k++] ^ apply0123(T01, T23, s0, s1, s2, s3);
2045
+ const t12 = xk[k++] ^ apply0123(T01, T23, s1, s2, s3, s0);
2046
+ const t22 = xk[k++] ^ apply0123(T01, T23, s2, s3, s0, s1);
2047
+ const t32 = xk[k++] ^ apply0123(T01, T23, s3, s0, s1, s2);
2048
+ s0 = t02, s1 = t12, s2 = t22, s3 = t32;
2049
+ }
2050
+ const t0 = xk[k++] ^ applySbox(sbox2, s0, s1, s2, s3);
2051
+ const t1 = xk[k++] ^ applySbox(sbox2, s1, s2, s3, s0);
2052
+ const t2 = xk[k++] ^ applySbox(sbox2, s2, s3, s0, s1);
2053
+ const t3 = xk[k++] ^ applySbox(sbox2, s3, s0, s1, s2);
2054
+ return { s0: t0, s1: t1, s2: t2, s3: t3 };
2055
+ }
2056
+ function ctr32(xk, isLE2, nonce, src, dst) {
2057
+ abytes(nonce, BLOCK_SIZE2);
2058
+ abytes(src);
2059
+ dst = getOutput(src.length, dst);
2060
+ const ctr = nonce;
2061
+ const c32 = u32(ctr);
2062
+ const view = createView(ctr);
2063
+ const src32 = u32(src);
2064
+ const dst32 = u32(dst);
2065
+ const ctrPos = 12;
2066
+ const srcLen = src.length;
2067
+ let ctrNum = view.getUint32(ctrPos, isLE2);
2068
+ let { s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]);
2069
+ for (let i = 0; i + 4 <= src32.length; i += 4) {
2070
+ dst32[i + 0] = src32[i + 0] ^ s0;
2071
+ dst32[i + 1] = src32[i + 1] ^ s1;
2072
+ dst32[i + 2] = src32[i + 2] ^ s2;
2073
+ dst32[i + 3] = src32[i + 3] ^ s3;
2074
+ ctrNum = ctrNum + 1 >>> 0;
2075
+ view.setUint32(ctrPos, ctrNum, isLE2);
2076
+ ({ s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]));
2077
+ }
2078
+ const start = BLOCK_SIZE2 * Math.floor(src32.length / BLOCK_SIZE32);
2079
+ if (start < srcLen) {
2080
+ const b32 = new Uint32Array([s0, s1, s2, s3]);
2081
+ const buf3 = u8(b32);
2082
+ for (let i = start, pos = 0; i < srcLen; i++, pos++)
2083
+ dst[i] = src[i] ^ buf3[pos];
2084
+ clean(b32);
2085
+ }
2086
+ return dst;
2087
+ }
2088
+ function computeTag(fn, isLE2, key, data, AAD) {
2089
+ const aadLength = AAD ? AAD.length : 0;
2090
+ const h = fn.create(key, data.length + aadLength);
2091
+ if (AAD)
2092
+ h.update(AAD);
2093
+ const num5 = u64Lengths(8 * data.length, 8 * aadLength, isLE2);
2094
+ h.update(data);
2095
+ h.update(num5);
2096
+ const res = h.digest();
2097
+ clean(num5);
2098
+ return res;
2099
+ }
2100
+ var gcm = /* @__PURE__ */ wrapCipher({ blockSize: 16, nonceLength: 12, tagLength: 16, varSizeNonce: true }, function aesgcm(key, nonce, AAD) {
2101
+ if (nonce.length < 8)
2102
+ throw new Error("aes/gcm: invalid nonce length");
2103
+ const tagLength = 16;
2104
+ function _computeTag(authKey, tagMask, data) {
2105
+ const tag = computeTag(ghash, false, authKey, data, AAD);
2106
+ for (let i = 0; i < tagMask.length; i++)
2107
+ tag[i] ^= tagMask[i];
2108
+ return tag;
2109
+ }
2110
+ function deriveKeys() {
2111
+ const xk = expandKeyLE(key);
2112
+ const authKey = EMPTY_BLOCK.slice();
2113
+ const counter = EMPTY_BLOCK.slice();
2114
+ ctr32(xk, false, counter, counter, authKey);
2115
+ if (nonce.length === 12) {
2116
+ counter.set(nonce);
2117
+ } else {
2118
+ const nonceLen = EMPTY_BLOCK.slice();
2119
+ const view = createView(nonceLen);
2120
+ setBigUint64(view, 8, BigInt(nonce.length * 8), false);
2121
+ const g = ghash.create(authKey).update(nonce).update(nonceLen);
2122
+ g.digestInto(counter);
2123
+ g.destroy();
2124
+ }
2125
+ const tagMask = ctr32(xk, false, counter, EMPTY_BLOCK);
2126
+ return { xk, authKey, counter, tagMask };
2127
+ }
2128
+ return {
2129
+ encrypt(plaintext) {
2130
+ const { xk, authKey, counter, tagMask } = deriveKeys();
2131
+ const out = new Uint8Array(plaintext.length + tagLength);
2132
+ const toClean = [xk, authKey, counter, tagMask];
2133
+ if (!isAligned32(plaintext))
2134
+ toClean.push(plaintext = copyBytes(plaintext));
2135
+ ctr32(xk, false, counter, plaintext, out.subarray(0, plaintext.length));
2136
+ const tag = _computeTag(authKey, tagMask, out.subarray(0, out.length - tagLength));
2137
+ toClean.push(tag);
2138
+ out.set(tag, plaintext.length);
2139
+ clean(...toClean);
2140
+ return out;
2141
+ },
2142
+ decrypt(ciphertext) {
2143
+ const { xk, authKey, counter, tagMask } = deriveKeys();
2144
+ const toClean = [xk, authKey, tagMask, counter];
2145
+ if (!isAligned32(ciphertext))
2146
+ toClean.push(ciphertext = copyBytes(ciphertext));
2147
+ const data = ciphertext.subarray(0, -tagLength);
2148
+ const passedTag = ciphertext.subarray(-tagLength);
2149
+ const tag = _computeTag(authKey, tagMask, data);
2150
+ toClean.push(tag);
2151
+ if (!equalBytes(tag, passedTag))
2152
+ throw new Error("aes/gcm: invalid ghash tag");
2153
+ const out = ctr32(xk, false, counter, data);
2154
+ clean(...toClean);
2155
+ return out;
2156
+ }
2157
+ };
2158
+ });
2159
+ var NONCE_LEN = 12;
2160
+ var RECOVERY_INFO = "cavos-stellar-dek-recovery";
2161
+ var PASSKEY_INFO = "cavos-stellar-dek-passkey";
2162
+ var ECIES_INFO = "cavos-stellar-dek-ecies";
2163
+ var RECOVERY_PBKDF2_ITERATIONS = 21e4;
2164
+ var RECOVERY_KDF_SALT = "cavos-stellar-recovery-v1";
2165
+ function generateDEK() {
2166
+ return randomBytes(32);
2167
+ }
2168
+ function sealControlSeed(controlSeed, dek) {
2169
+ const nonce = randomBytes(NONCE_LEN);
2170
+ const ct = gcm(dek, nonce).encrypt(controlSeed);
2171
+ return concat(nonce, ct);
2172
+ }
2173
+ function openControlSeed(sealed, dek) {
2174
+ const { nonce, ct } = splitNonce(sealed);
2175
+ return gcm(dek, nonce).decrypt(ct);
2176
+ }
2177
+ function wrapDEK(dek, kek) {
2178
+ const nonce = randomBytes(NONCE_LEN);
2179
+ return concat(nonce, gcm(kek, nonce).encrypt(dek));
2180
+ }
2181
+ function unwrapDEK(wrapped, kek) {
2182
+ const { nonce, ct } = splitNonce(wrapped);
2183
+ return gcm(kek, nonce).decrypt(ct);
2184
+ }
2185
+ function deriveRecoveryKEK(code) {
2186
+ const normalised = code.trim().replace(/\s+/g, " ").toLowerCase();
2187
+ if (!normalised) throw new Error("kit/stellar: recovery code is empty");
2188
+ const stretched = pbkdf2(sha256, new TextEncoder().encode(normalised), new TextEncoder().encode(RECOVERY_KDF_SALT), {
2189
+ c: RECOVERY_PBKDF2_ITERATIONS,
2190
+ dkLen: 32
2191
+ });
2192
+ return hkdf(sha256, stretched, void 0, RECOVERY_INFO, 32);
2193
+ }
2194
+ function derivePasskeyKEK(prfOutput) {
2195
+ if (prfOutput.length < 32) throw new Error("kit/stellar: passkey PRF output too short");
2196
+ return hkdf(sha256, prfOutput, void 0, PASSKEY_INFO, 32);
2197
+ }
2198
+ function eciesWrapDEK(dek, recipientPubSec1) {
2199
+ const ephPriv = p256.utils.randomPrivateKey();
2200
+ const ephPubCompressed = p256.getPublicKey(ephPriv, true);
2201
+ const kek = eciesKEK(p256.getSharedSecret(ephPriv, recipientPubSec1, false), ephPubCompressed);
2202
+ const wrapped = wrapDEK(dek, kek);
2203
+ return concat(ephPubCompressed, wrapped);
2204
+ }
2205
+ function eciesUnwrapDEK(blob, recipientPrivScalar) {
2206
+ const ephPubCompressed = blob.subarray(0, 33);
2207
+ const wrapped = blob.subarray(33);
2208
+ const kek = eciesKEK(p256.getSharedSecret(recipientPrivScalar, ephPubCompressed, false), ephPubCompressed);
2209
+ return unwrapDEK(wrapped, kek);
2210
+ }
2211
+ function eciesKEK(sharedUncompressed, ephPubCompressed) {
2212
+ return eciesKEKFromX(sharedUncompressed.subarray(1, 33), ephPubCompressed);
2213
+ }
2214
+ function eciesKEKFromX(sharedX, ephPubCompressed) {
2215
+ return hkdf(sha256, sharedX, ephPubCompressed, ECIES_INFO, 32);
2216
+ }
2217
+ var DATA_ENTRY_MAX = 64;
2218
+ function chunkTo64(blob) {
2219
+ if (blob.length <= DATA_ENTRY_MAX) return [blob];
2220
+ const chunks = [];
2221
+ for (let i = 0; i < blob.length; i += DATA_ENTRY_MAX) {
2222
+ chunks.push(blob.subarray(i, i + DATA_ENTRY_MAX));
2223
+ }
2224
+ return chunks;
2225
+ }
2226
+ function unchunk(chunks) {
2227
+ const total = chunks.reduce((n, c) => n + c.length, 0);
2228
+ const out = new Uint8Array(total);
2229
+ let off = 0;
2230
+ for (const c of chunks) {
2231
+ out.set(c, off);
2232
+ off += c.length;
2233
+ }
2234
+ return out;
2235
+ }
2236
+ function concat(a, b) {
2237
+ const out = new Uint8Array(a.length + b.length);
2238
+ out.set(a, 0);
2239
+ out.set(b, a.length);
2240
+ return out;
2241
+ }
2242
+ function splitNonce(blob) {
2243
+ return { nonce: blob.subarray(0, NONCE_LEN), ct: blob.subarray(NONCE_LEN) };
2244
+ }
2245
+
2246
+ // src/chains/stellar/datamap.ts
2247
+ var CT_BASE = "cv:ct";
2248
+ var PASSKEY_BASE = "cv:wp";
2249
+ var RECOVERY_BASE = "cv:wr";
2250
+ var VERSION_KEY = "cv:v";
2251
+ var ENVELOPE_VERSION = 1;
2252
+ function deviceBase(slot) {
2253
+ return `cv:wd:${slot}`;
2254
+ }
2255
+ function toDataEntries(env) {
2256
+ const out = {};
2257
+ writeChunked(out, CT_BASE, env.ct);
2258
+ for (const [slot, blob] of Object.entries(env.deviceWraps)) {
2259
+ writeChunked(out, deviceBase(slot), blob);
2260
+ }
2261
+ if (env.passkeyWrap) writeChunked(out, PASSKEY_BASE, env.passkeyWrap);
2262
+ if (env.recoveryWrap) writeChunked(out, RECOVERY_BASE, env.recoveryWrap);
2263
+ out[VERSION_KEY] = Uint8Array.of(ENVELOPE_VERSION);
2264
+ return out;
2265
+ }
2266
+ function fromDataEntries(entries) {
2267
+ const ct = readChunked(entries, CT_BASE);
2268
+ if (!ct) throw new Error("kit/stellar: account has no control-seed ciphertext (cv:ct)");
2269
+ const deviceWraps = {};
2270
+ const seenSlots = /* @__PURE__ */ new Set();
2271
+ for (const key of Object.keys(entries)) {
2272
+ const m = key.match(/^cv:wd:([^/]+)\/\d+$/);
2273
+ if (m) seenSlots.add(m[1]);
2274
+ }
2275
+ for (const slot of seenSlots) {
2276
+ const blob = readChunked(entries, deviceBase(slot));
2277
+ if (blob) deviceWraps[slot] = blob;
2278
+ }
2279
+ return {
2280
+ ct,
2281
+ deviceWraps,
2282
+ passkeyWrap: readChunked(entries, PASSKEY_BASE),
2283
+ recoveryWrap: readChunked(entries, RECOVERY_BASE)
2284
+ };
2285
+ }
2286
+ function deviceWrapEntries(slot, blob) {
2287
+ const out = {};
2288
+ writeChunked(out, deviceBase(slot), blob);
2289
+ return out;
2290
+ }
2291
+ function writeChunked(out, base, blob) {
2292
+ const chunks = chunkTo64(blob);
2293
+ chunks.forEach((chunk, i) => {
2294
+ out[`${base}/${i}`] = chunk;
2295
+ });
2296
+ }
2297
+ function readChunked(entries, base) {
2298
+ const chunks = [];
2299
+ for (let i = 0; ; i++) {
2300
+ const chunk = entries[`${base}/${i}`];
2301
+ if (!chunk) break;
2302
+ chunks.push(chunk);
2303
+ }
2304
+ return chunks.length ? unchunk(chunks) : void 0;
2305
+ }
2306
+ var TX_TIMEOUT = 180;
1583
2307
  var StellarAdapter = class {
1584
2308
  constructor(opts) {
1585
2309
  this.chain = "stellar";
1586
2310
  this.network = opts.network;
1587
2311
  this.passphrase = STELLAR_NETWORKS[opts.network].passphrase;
1588
- this.rpcUrl = opts.rpcUrl ?? STELLAR_NETWORKS[opts.network].rpcUrl;
1589
- this.factoryId = opts.factoryId ?? FACTORY_CONTRACT_ID[opts.network];
1590
- if (!this.factoryId) {
1591
- throw new Error(`kit/stellar: no factory contract id configured for ${opts.network}`);
1592
- }
1593
- this.signer = opts.signer;
2312
+ this.horizonUrl = opts.horizonUrl ?? HORIZON_URL[opts.network];
1594
2313
  }
1595
2314
  server() {
1596
2315
  if (!this._server) {
1597
- this._server = new rpc.Server(this.rpcUrl, {
1598
- allowHttp: this.rpcUrl.startsWith("http://")
2316
+ this._server = new Horizon.Server(this.horizonUrl, {
2317
+ allowHttp: this.horizonUrl.startsWith("http://")
1599
2318
  });
1600
2319
  }
1601
2320
  return this._server;
1602
2321
  }
1603
- networkId() {
1604
- return hash$1(Buffer.from(this.passphrase));
2322
+ /** Whether the classic account already exists on-chain. */
2323
+ async isDeployed(address) {
2324
+ try {
2325
+ await this.server().loadAccount(address);
2326
+ return true;
2327
+ } catch (e) {
2328
+ if (isNotFound(e)) return false;
2329
+ throw e;
2330
+ }
2331
+ }
2332
+ /** Read the account's `MANAGE_DATA` entries as raw bytes (name → value). */
2333
+ async loadDataEntries(address) {
2334
+ const account = await this.server().loadAccount(address);
2335
+ const out = {};
2336
+ for (const [name, b64] of Object.entries(account.data_attr ?? {})) {
2337
+ out[name] = new Uint8Array(Buffer.from(b64, "base64"));
2338
+ }
2339
+ return out;
2340
+ }
2341
+ /** Native XLM balance in stroops. Returns 0n if the account doesn't exist. */
2342
+ async balance(address) {
2343
+ try {
2344
+ const account = await this.server().loadAccount(address);
2345
+ const native = account.balances.find((b) => b.asset_type === "native");
2346
+ return native ? toStroops(native.balance) : 0n;
2347
+ } catch (e) {
2348
+ if (isNotFound(e)) return 0n;
2349
+ throw e;
2350
+ }
1605
2351
  }
1606
2352
  /**
1607
- * Deterministic account address for `(addressSeed, initialSigner)` computed
1608
- * off-chain, byte-identical to the factory's on-chain `account_address`.
1609
- * `contractId = sha256(HashIdPreimage(networkId, factory, salt))` with
1610
- * `salt = sha256(addressSeed || sec1(initialSigner))`.
2353
+ * Build the account-creation transaction (source = funder, the relayer or a
2354
+ * self-funded keypair):
2355
+ * 1. `createAccount` funds the deterministic `G…` master address,
2356
+ * 2. `manageData` writes the control-key envelope entries (authorized by the
2357
+ * still-weight-1 master),
2358
+ * 3. `setOptions` adds the control signer (weight 1), sets all thresholds to
2359
+ * 1, and zeroes the master weight — after this the master can never sign.
2360
+ *
2361
+ * The returned tx must be signed by BOTH the master keypair (for the account's
2362
+ * own ops) and the funder (source + fee). Sponsorship of reserves is layered on
2363
+ * in Phase 3.
1611
2364
  */
1612
- computeAddress(addressSeed, initialSigner) {
1613
- const salt = this.accountSalt(addressSeed, initialSigner);
1614
- const preimage = xdr.HashIdPreimage.envelopeTypeContractId(
1615
- new xdr.HashIdPreimageContractId({
1616
- networkId: this.networkId(),
1617
- contractIdPreimage: xdr.ContractIdPreimage.contractIdPreimageFromAddress(
1618
- new xdr.ContractIdPreimageFromAddress({
1619
- address: new Address(this.factoryId).toScAddress(),
1620
- salt
1621
- })
1622
- )
2365
+ async buildCreateTx(params) {
2366
+ const funderAccount = await this.server().loadAccount(params.funder);
2367
+ const builder = new TransactionBuilder(funderAccount, {
2368
+ fee: BASE_FEE,
2369
+ networkPassphrase: this.passphrase
2370
+ });
2371
+ builder.addOperation(
2372
+ Operation.createAccount({
2373
+ destination: params.masterAddress,
2374
+ startingBalance: fromStroops(params.startingBalance)
1623
2375
  })
1624
2376
  );
1625
- return StrKey.encodeContract(hash$1(preimage.toXDR()));
1626
- }
1627
- /** `salt = sha256(addressSeed(32) || sec1(initialSigner)(65))` — matches the factory. */
1628
- accountSalt(addressSeed, initialSigner) {
1629
- return hash$1(Buffer.concat([Buffer.from(addressSeed), Buffer.from(sec1Pubkey(initialSigner))]));
1630
- }
1631
- /** Host function: `factory.deploy(address_seed, initial_signer)`. */
1632
- buildDeploy(addressSeed, initialSigner) {
1633
- return invokeFunc(this.factoryId, "deploy", [
1634
- bytesScVal(addressSeed),
1635
- bytesScVal(sec1Pubkey(initialSigner))
1636
- ]);
1637
- }
1638
- /** Host function: `account.add_signer(new_signer)` (requires device auth). */
1639
- buildAddSigner(accountAddress, signer) {
1640
- return invokeFunc(accountAddress, "add_signer", [bytesScVal(sec1Pubkey(signer))]);
1641
- }
1642
- /** Host function: `account.remove_signer(signer)` (requires device auth). */
1643
- buildRemoveSigner(accountAddress, signer) {
1644
- return invokeFunc(accountAddress, "remove_signer", [bytesScVal(sec1Pubkey(signer))]);
1645
- }
1646
- /** Host function: `account.add_approver(passkey)` (requires device auth). */
1647
- buildAddApprover(accountAddress, passkey) {
1648
- return invokeFunc(accountAddress, "add_approver", [bytesScVal(sec1Pubkey(passkey))]);
1649
- }
1650
- /** Host function: `account.remove_approver(passkey)` (requires device auth). */
1651
- buildRemoveApprover(accountAddress, passkey) {
1652
- return invokeFunc(accountAddress, "remove_approver", [bytesScVal(sec1Pubkey(passkey))]);
1653
- }
1654
- /** This chain's leaf for approving `add_signer(newSigner)` at `nonce`:
1655
- * `sha256(sec1(new_signer) || nonce_be8)`. The batch challenge the passkey signs
1656
- * is `sha256(concat(leaves))` across chains. */
1657
- passkeyLeaf(newSigner, nonce) {
1658
- const msg = new Uint8Array(65 + 8);
1659
- msg.set(sec1Pubkey(newSigner), 0);
1660
- const n = new Uint8Array(8);
1661
- let v = nonce;
1662
- for (let i = 7; i >= 0; i--) {
1663
- n[i] = Number(v & 0xffn);
1664
- v >>= 8n;
1665
- }
1666
- msg.set(n, 65);
1667
- return sha256(msg);
1668
- }
1669
- /** Host function: passkey-authorized `add_signer_via_passkey` (no device auth —
1670
- * authorized by the embedded WebAuthn assertion, so any relayer can submit).
1671
- * `leaves`/`leafIndex` place this chain's leaf in the multi-chain batch. */
1672
- buildAddSignerViaPasskey(accountAddress, newSigner, passkey, nonce, leaves, leafIndex, assertion) {
1673
- const sig = encodeLowSSignature2({ r: assertion.r, s: assertion.s});
1674
- const leavesScVal = xdr.ScVal.scvVec(leaves.map((l) => bytesScVal(l)));
1675
- return invokeFunc(accountAddress, "add_signer_via_passkey", [
1676
- bytesScVal(sec1Pubkey(newSigner)),
1677
- bytesScVal(sec1Pubkey(passkey)),
1678
- nativeToScVal(nonce, { type: "u64" }),
1679
- leavesScVal,
1680
- nativeToScVal(leafIndex, { type: "u32" }),
1681
- bytesScVal(assertion.authenticatorData),
1682
- bytesScVal(assertion.clientDataJSON),
1683
- nativeToScVal(assertion.challengeOffset, { type: "u32" }),
1684
- bytesScVal(sig)
1685
- ]);
1686
- }
1687
- /** Read whether `passkey` is a registered approver (read-only simulation). */
1688
- /** True if the account has at least one passkey registered as an approver.
1689
- * Reads the contract's `approvers` view and checks the list is non-empty. */
1690
- async hasPasskeyApprover(accountAddress, readSource) {
1691
- if (!await this.isDeployed(accountAddress)) return false;
1692
- const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
1693
- const src = new Account3(readSource, "0");
1694
- const op = Operation.invokeHostFunction({
1695
- func: invokeFunc(accountAddress, "approvers", []),
1696
- auth: []
1697
- });
1698
- const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
1699
- const sim = await this.server().simulateTransaction(tx2);
1700
- if (rpc.Api.isSimulationError(sim)) {
1701
- throw new Error(`kit/stellar: approvers simulation failed: ${sim.error}`);
1702
- }
1703
- if (!sim.result?.retval) return false;
1704
- const list = scValToNative(sim.result.retval);
1705
- return Array.isArray(list) && list.length > 0;
1706
- }
1707
- async isApprover(accountAddress, passkey, readSource) {
1708
- if (!await this.isDeployed(accountAddress)) return false;
1709
- const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
1710
- const src = new Account3(readSource, "0");
1711
- const op = Operation.invokeHostFunction({
1712
- func: invokeFunc(accountAddress, "is_approver", [bytesScVal(sec1Pubkey(passkey))]),
1713
- auth: []
1714
- });
1715
- const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
1716
- const sim = await this.server().simulateTransaction(tx2);
1717
- if (rpc.Api.isSimulationError(sim)) {
1718
- throw new Error(`kit/stellar: is_approver simulation failed: ${sim.error}`);
1719
- }
1720
- if (!sim.result?.retval) return false;
1721
- return scValToNative(sim.result.retval) === true;
1722
- }
1723
- /** Read the current passkey-approval nonce (read-only simulation). */
1724
- async passkeyNonce(accountAddress, readSource) {
1725
- if (!await this.isDeployed(accountAddress)) return 0n;
1726
- const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
1727
- const src = new Account3(readSource, "0");
1728
- const op = Operation.invokeHostFunction({
1729
- func: invokeFunc(accountAddress, "passkey_nonce", []),
1730
- auth: []
1731
- });
1732
- const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
1733
- const sim = await this.server().simulateTransaction(tx2);
1734
- if (rpc.Api.isSimulationError(sim)) {
1735
- throw new Error(`kit/stellar: passkey_nonce simulation failed: ${sim.error}`);
1736
- }
1737
- if (!sim.result?.retval) return 0n;
1738
- return BigInt(scValToNative(sim.result.retval));
1739
- }
1740
- /** Host function: SEP-41 `token.transfer(from=account, to, amount)` (device auth). */
1741
- buildTransfer(tokenId, accountAddress, destination, amount) {
1742
- return invokeFunc(tokenId, "transfer", [
1743
- new Address(accountAddress).toScVal(),
1744
- new Address(destination).toScVal(),
1745
- nativeToScVal(amount, { type: "i128" })
1746
- ]);
2377
+ for (const [name, value] of Object.entries(toDataEntries(params.envelope))) {
2378
+ builder.addOperation(
2379
+ Operation.manageData({ name, value: Buffer.from(value), source: params.masterAddress })
2380
+ );
2381
+ }
2382
+ builder.addOperation(
2383
+ Operation.setOptions({
2384
+ source: params.masterAddress,
2385
+ masterWeight: 0,
2386
+ lowThreshold: 1,
2387
+ medThreshold: 1,
2388
+ highThreshold: 1,
2389
+ signer: { ed25519PublicKey: params.controlAddress, weight: 1 }
2390
+ })
2391
+ );
2392
+ return builder.setTimeout(TX_TIMEOUT).build();
1747
2393
  }
1748
2394
  /**
1749
- * Sign a Soroban authorization entry with the silent device key, producing the
1750
- * `Vec<DeviceSignature>` the account's `__check_auth` verifies. The device
1751
- * signs `sha256(preimage)` (WebCrypto hashes once more internally), which is
1752
- * exactly what the contract recomputes. Mutates + returns the entry.
2395
+ * Build a **sponsored** account-creation transaction whose source is the
2396
+ * relayer. Wraps the account setup in `beginSponsoringFutureReserves` /
2397
+ * `endSponsoringFutureReserves`, so the relayer (not the user) pays every
2398
+ * reserve the account is created with a 0 starting balance and holds no
2399
+ * locked XLM of the user's. Ops:
2400
+ * 0. beginSponsoringFutureReserves(G) source = relayer
2401
+ * 1. createAccount(G, 0) source = relayer
2402
+ * 2. manageData(cv:… envelope) source = G (master-signed)
2403
+ * 3. setOptions(control signer, master → 0) source = G
2404
+ * 4. endSponsoringFutureReserves() source = G
2405
+ *
2406
+ * Signed by the master (for the G ops, while it's still weight 1); the relayer
2407
+ * co-signs (source + fee + sponsorship) before submitting.
1753
2408
  */
1754
- async signAuthEntry(entry, validUntilLedger) {
1755
- const addrCreds = entry.credentials().address();
1756
- addrCreds.signatureExpirationLedger(validUntilLedger);
1757
- const preimage = xdr.HashIdPreimage.envelopeTypeSorobanAuthorization(
1758
- new xdr.HashIdPreimageSorobanAuthorization({
1759
- networkId: this.networkId(),
1760
- nonce: addrCreds.nonce(),
1761
- signatureExpirationLedger: validUntilLedger,
1762
- invocation: entry.rootInvocation()
2409
+ async buildSponsoredCreateTx(params) {
2410
+ const relayerAccount = await this.server().loadAccount(params.relayer);
2411
+ const builder = new TransactionBuilder(relayerAccount, {
2412
+ fee: BASE_FEE,
2413
+ networkPassphrase: this.passphrase
2414
+ });
2415
+ builder.addOperation(
2416
+ Operation.beginSponsoringFutureReserves({ sponsoredId: params.masterAddress, source: params.relayer })
2417
+ );
2418
+ builder.addOperation(
2419
+ Operation.createAccount({ destination: params.masterAddress, startingBalance: "0", source: params.relayer })
2420
+ );
2421
+ for (const [name, value] of Object.entries(toDataEntries(params.envelope))) {
2422
+ builder.addOperation(
2423
+ Operation.manageData({ name, value: Buffer.from(value), source: params.masterAddress })
2424
+ );
2425
+ }
2426
+ builder.addOperation(
2427
+ Operation.setOptions({
2428
+ source: params.masterAddress,
2429
+ masterWeight: 0,
2430
+ lowThreshold: 1,
2431
+ medThreshold: 1,
2432
+ highThreshold: 1,
2433
+ signer: { ed25519PublicKey: params.controlAddress, weight: 1 }
1763
2434
  })
1764
2435
  );
1765
- const payload = hash$1(preimage.toXDR());
1766
- const sig = await this.signer.sign(new Uint8Array(payload));
1767
- const pubkey = await this.signer.getPublicKey();
1768
- addrCreds.signature(deviceSignatureScVal(pubkey, sig));
1769
- return entry;
2436
+ builder.addOperation(Operation.endSponsoringFutureReserves({ source: params.masterAddress }));
2437
+ return builder.setTimeout(TX_TIMEOUT).build();
1770
2438
  }
1771
2439
  /**
1772
- * Read a SEP-41 token balance of `account` via a read-only simulation of
1773
- * `token.balance(account)`. Returns 0 when the account isn't deployed or holds
1774
- * none. `readSource` is any funded G-account (used only for the simulation).
2440
+ * Build a classic native-XLM payment as an *inner* transaction whose source is
2441
+ * the account itself (`G…`), signed by the control key. Wrap it in a fee-bump
2442
+ * (see `wrapFeeBump`) so the relayer pays the fee gasless.
1775
2443
  */
1776
- async readBalance(tokenId, account, readSource) {
1777
- if (!await this.isDeployed(account)) return 0n;
1778
- const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
1779
- const src = new Account3(readSource, "0");
1780
- const op = Operation.invokeHostFunction({
1781
- func: invokeFunc(tokenId, "balance", [new Address(account).toScVal()]),
1782
- auth: []
1783
- });
1784
- const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
1785
- const sim = await this.server().simulateTransaction(tx2);
1786
- if (rpc.Api.isSimulationError(sim) || !sim.result?.retval) return 0n;
1787
- return BigInt(scValToNative(sim.result.retval));
2444
+ async buildPaymentTx(params) {
2445
+ const source = await this.server().loadAccount(params.from);
2446
+ return new TransactionBuilder(source, { fee: BASE_FEE, networkPassphrase: this.passphrase }).addOperation(
2447
+ Operation.payment({
2448
+ destination: params.to,
2449
+ asset: Asset.native(),
2450
+ amount: fromStroops(params.amount)
2451
+ })
2452
+ ).setTimeout(TX_TIMEOUT).build();
1788
2453
  }
1789
- /** Whether the account contract instance exists on-chain (is deployed). */
1790
- async isDeployed(accountAddress) {
1791
- try {
1792
- const res = await this.server().getContractData(
1793
- accountAddress,
1794
- xdr.ScVal.scvLedgerKeyContractInstance(),
1795
- rpc.Durability.Persistent
1796
- );
1797
- return !!res;
1798
- } catch {
1799
- return false;
2454
+ /**
2455
+ * Build a data-entry write (e.g. re-wrapping the DEK for a newly approved
2456
+ * device) as an inner tx sourced by the account, signed by the control key.
2457
+ */
2458
+ async buildDataTx(params) {
2459
+ const source = await this.server().loadAccount(params.account);
2460
+ const builder = new TransactionBuilder(source, { fee: BASE_FEE, networkPassphrase: this.passphrase });
2461
+ for (const [name, value] of Object.entries(params.entries)) {
2462
+ builder.addOperation(Operation.manageData({ name, value: Buffer.from(value) }));
1800
2463
  }
2464
+ return builder.setTimeout(TX_TIMEOUT).build();
1801
2465
  }
1802
2466
  /**
1803
- * Read whether `signer` is a currently-authorized signer of the account, via a
1804
- * read-only simulation of `account.is_authorized(signer)`. `readSource` is any
1805
- * funded G-account (used only for the simulation's source/sequence).
2467
+ * Build a **sponsored** data-entry write whose source is the relayer, so the
2468
+ * relayer (not the 0-balance account) pays the reserve of any NEW subentry.
2469
+ * Adding a factor (passkey / recovery) or a device slot creates a data entry
2470
+ * that needs ~0.5 XLM of reserve; a sponsored account holds no XLM, so — like
2471
+ * account creation — the write must be wrapped in begin/endSponsoringFuture
2472
+ * reserves with the relayer as sponsor. Ops:
2473
+ * 0. beginSponsoringFutureReserves(account) source = relayer
2474
+ * 1..n. manageData(cv:…) source = account (control-signed)
2475
+ * last. endSponsoringFutureReserves() source = account
2476
+ *
2477
+ * Signed by the control key (account ops) + the relayer (source + fee + sponsor).
1806
2478
  */
1807
- async isAuthorizedSigner(accountAddress, signer, readSource) {
1808
- if (!await this.isDeployed(accountAddress)) return false;
1809
- const { Account: Account3, TransactionBuilder: TransactionBuilder2, BASE_FEE: BASE_FEE2 } = await import('@stellar/stellar-sdk');
1810
- const src = new Account3(readSource, "0");
1811
- const op = Operation.invokeHostFunction({
1812
- func: invokeFunc(accountAddress, "is_authorized", [bytesScVal(sec1Pubkey(signer))]),
1813
- auth: []
2479
+ async buildSponsoredDataTx(params) {
2480
+ const relayerAccount = await this.server().loadAccount(params.relayer);
2481
+ const builder = new TransactionBuilder(relayerAccount, {
2482
+ fee: BASE_FEE,
2483
+ networkPassphrase: this.passphrase
1814
2484
  });
1815
- const tx2 = new TransactionBuilder2(src, { fee: BASE_FEE2, networkPassphrase: this.passphrase }).addOperation(op).setTimeout(30).build();
1816
- const sim = await this.server().simulateTransaction(tx2);
1817
- if (rpc.Api.isSimulationError(sim)) {
1818
- throw new Error(`kit/stellar: is_authorized simulation failed: ${sim.error}`);
2485
+ builder.addOperation(
2486
+ Operation.beginSponsoringFutureReserves({ sponsoredId: params.account, source: params.relayer })
2487
+ );
2488
+ for (const [name, value] of Object.entries(params.entries)) {
2489
+ builder.addOperation(Operation.manageData({ name, value: Buffer.from(value), source: params.account }));
2490
+ }
2491
+ builder.addOperation(Operation.endSponsoringFutureReserves({ source: params.account }));
2492
+ return builder.setTimeout(TX_TIMEOUT).build();
2493
+ }
2494
+ /** Wrap a control-signed inner tx in a fee-bump whose fee source is `feeSource`
2495
+ * (the relayer). The inner tx pays nothing; the relayer pays all fees. */
2496
+ wrapFeeBump(inner, feeSource) {
2497
+ return TransactionBuilder.buildFeeBumpTransaction(feeSource, BASE_FEE, inner, this.passphrase);
2498
+ }
2499
+ /** Submit a fully-signed classic transaction and return its hash. Throws with
2500
+ * the Horizon result codes on failure. */
2501
+ async submit(tx3) {
2502
+ try {
2503
+ const res = await this.server().submitTransaction(tx3);
2504
+ return res.hash;
2505
+ } catch (e) {
2506
+ throw new Error(`kit/stellar: submit failed: ${horizonError(e)}`);
1819
2507
  }
1820
- if (!sim.result?.retval) return false;
1821
- return scValToNative(sim.result.retval) === true;
2508
+ }
2509
+ /** Build an `Account` handle for a known address + sequence (avoids a Horizon
2510
+ * round-trip when the caller already has the sequence). */
2511
+ accountAt(address, sequence) {
2512
+ return new Account(address, sequence);
1822
2513
  }
1823
2514
  };
1824
- function sec1Pubkey(pk) {
1825
- const out = new Uint8Array(65);
1826
- out[0] = 4;
1827
- out.set(bigIntTo32Bytes(pk.x), 1);
1828
- out.set(bigIntTo32Bytes(pk.y), 33);
1829
- return out;
2515
+ function isNotFound(e) {
2516
+ const status = e?.response?.status ?? e?.status;
2517
+ return status === 404;
1830
2518
  }
1831
- function encodeLowSSignature2(sig) {
1832
- const lowS2 = sig.s > SECP256R1_N2 / 2n ? SECP256R1_N2 - sig.s : sig.s;
1833
- const out = new Uint8Array(64);
1834
- out.set(bigIntTo32Bytes(sig.r), 0);
1835
- out.set(bigIntTo32Bytes(lowS2), 32);
1836
- return out;
2519
+ function horizonError(e) {
2520
+ const codes = e?.response?.data?.extras?.result_codes;
2521
+ return codes ? JSON.stringify(codes) : String(e?.message ?? e);
1837
2522
  }
1838
- function deviceSignatureScVal(pubkey, sig) {
1839
- const element = nativeToScVal(
1840
- {
1841
- public_key: Buffer.from(sec1Pubkey(pubkey)),
1842
- signature: Buffer.from(encodeLowSSignature2(sig))
1843
- },
1844
- { type: { public_key: ["symbol", "bytes"], signature: ["symbol", "bytes"] } }
1845
- );
1846
- return xdr.ScVal.scvVec([element]);
1847
- }
1848
- function invokeFunc(contractId, method, args) {
1849
- return xdr.HostFunction.hostFunctionTypeInvokeContract(
1850
- new xdr.InvokeContractArgs({
1851
- contractAddress: new Address(contractId).toScAddress(),
1852
- functionName: method,
1853
- args
1854
- })
1855
- );
2523
+ function toStroops(amount) {
2524
+ const [whole, frac = ""] = amount.split(".");
2525
+ const fracPadded = (frac + "0000000").slice(0, 7);
2526
+ return BigInt(whole) * 10000000n + BigInt(fracPadded);
2527
+ }
2528
+ function fromStroops(stroops) {
2529
+ const neg = stroops < 0n;
2530
+ const abs = neg ? -stroops : stroops;
2531
+ const whole = abs / 10000000n;
2532
+ const frac = (abs % 10000000n).toString().padStart(7, "0");
2533
+ return `${neg ? "-" : ""}${whole}.${frac}`;
2534
+ }
2535
+ var MASTER_HKDF_INFO = "cavos-stellar-master";
2536
+ function deriveStellarMasterSeed(identity) {
2537
+ const ikm = deriveAddressSeedStellar(identity);
2538
+ return hkdf(sha256, ikm, void 0, MASTER_HKDF_INFO, 32);
1856
2539
  }
1857
- function bytesScVal(bytes) {
1858
- return xdr.ScVal.scvBytes(Buffer.from(bytes));
2540
+ function deriveStellarMasterKeypair(identity) {
2541
+ return Keypair.fromRawEd25519Seed(Buffer.from(deriveStellarMasterSeed(identity)));
2542
+ }
2543
+ function deriveStellarAddress(identity) {
2544
+ return deriveStellarMasterKeypair(identity).publicKey();
2545
+ }
2546
+ function generateControlKey() {
2547
+ const seed = randomBytes(32);
2548
+ return { keypair: Keypair.fromRawEd25519Seed(Buffer.from(seed)), seed };
2549
+ }
2550
+ function controlKeypairFromSeed(seed) {
2551
+ if (seed.length !== 32) throw new Error("kit/stellar: control seed must be 32 bytes");
2552
+ return Keypair.fromRawEd25519Seed(Buffer.from(seed));
1859
2553
  }
1860
2554
 
1861
2555
  // src/chains/stellar/StellarRelayer.ts
@@ -1863,7 +2557,7 @@ var StellarRelayer = class {
1863
2557
  constructor(opts) {
1864
2558
  this.opts = opts;
1865
2559
  }
1866
- /** The relayer's source/fee-payer G-account (fetched + cached from the backend). */
2560
+ /** The relayer's source/fee-payer/sponsor G-account (fetched + cached). */
1867
2561
  async getSource() {
1868
2562
  if (this.source) return this.source;
1869
2563
  const res = await fetch(`${this.opts.baseUrl}/api/stellar/relay?network=${this.opts.network}`);
@@ -1872,17 +2566,16 @@ var StellarRelayer = class {
1872
2566
  this.source = fee_payer;
1873
2567
  return this.source;
1874
2568
  }
1875
- /**
1876
- * POST the assembled, device-authorized transaction XDR to the relayer to sign
1877
- * the envelope + submit. Returns the confirmed transaction hash.
1878
- */
1879
- async submit(transactionXdr) {
2569
+ /** POST a (partially) signed transaction XDR for the relayer to co-sign + submit.
2570
+ * `kind` selects the validation gate. Returns the confirmed transaction hash. */
2571
+ async submit(kind, transactionXdr) {
1880
2572
  const res = await fetch(`${this.opts.baseUrl}/api/stellar/relay`, {
1881
2573
  method: "POST",
1882
2574
  headers: { "Content-Type": "application/json" },
1883
2575
  body: JSON.stringify({
1884
2576
  app_id: this.opts.appId,
1885
2577
  network: this.opts.network,
2578
+ kind,
1886
2579
  transaction: transactionXdr
1887
2580
  })
1888
2581
  });
@@ -1890,311 +2583,413 @@ var StellarRelayer = class {
1890
2583
  const detail = await res.text().catch(() => "");
1891
2584
  throw new Error(`kit/stellar: relay failed (${res.status}) ${detail}`);
1892
2585
  }
1893
- const { hash: hash6 } = await res.json();
1894
- return hash6;
2586
+ const { hash: hash5 } = await res.json();
2587
+ return hash5;
1895
2588
  }
1896
2589
  };
2590
+
2591
+ // src/chains/stellar/CavosStellar.ts
2592
+ var DEFAULT_STARTING_BALANCE = 50000000n;
1897
2593
  var CavosStellar = class _CavosStellar {
1898
- constructor(identity, address, status, network, adapter, devicePubkey, relayer, sourceKeypair) {
2594
+ constructor(identity, address, status, network, adapter, deviceKey, control, dek, relayer) {
1899
2595
  this.identity = identity;
1900
2596
  this.address = address;
1901
- this.status = status;
1902
2597
  this.network = network;
1903
2598
  this.adapter = adapter;
1904
- this.devicePubkey = devicePubkey;
2599
+ this.deviceKey = deviceKey;
2600
+ this.control = control;
2601
+ this.dek = dek;
1905
2602
  this.relayer = relayer;
1906
- this.sourceKeypair = sourceKeypair;
1907
- /** Discriminant for the `CavosWallet` union narrows `execute()` per chain. */
2603
+ // Discriminant for the `CavosWallet` union. Classic `G…` IS the Stellar chain
2604
+ // now (the Soroban `C…` path was removed), so this is "stellar".
1908
2605
  this.chain = "stellar";
1909
- /** True when this connect just created a brand-new account (first sign-up). */
1910
2606
  this.isNewAccount = false;
2607
+ this.statusValue = status;
1911
2608
  }
1912
- get publicKey() {
1913
- return this.devicePubkey;
2609
+ get status() {
2610
+ return this.statusValue;
1914
2611
  }
1915
2612
  static async connect(opts) {
1916
2613
  const identity = opts.identity ?? await opts.auth?.authenticate();
1917
2614
  if (!identity) throw new Error("kit/stellar: connect requires `identity` or `auth`");
1918
- const signer = opts.createSigner ? await opts.createSigner(`${identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${identity.userId}:${opts.appSalt}` });
1919
- const devicePubkey = await signer.getPublicKey();
1920
- const adapter = new StellarAdapter({
1921
- network: opts.network,
1922
- rpcUrl: opts.rpcUrl,
1923
- factoryId: opts.factoryId,
1924
- signer
1925
- });
1926
- const addressSeed = deriveAddressSeedStellar({ userId: identity.userId, appSalt: opts.appSalt });
2615
+ const adapter = new StellarAdapter({ network: opts.network, horizonUrl: opts.horizonUrl });
2616
+ const master = deriveStellarMasterKeypair({ userId: identity.userId, appSalt: opts.appSalt });
2617
+ const address = master.publicKey();
2618
+ const startingBalance = opts.startingBalance ?? DEFAULT_STARTING_BALANCE;
1927
2619
  const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
1928
- const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry2);
1929
2620
  const relayer = opts.relayer ?? (opts.appId ? new StellarRelayer({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : void 0);
1930
- const build = (address2, status) => new _CavosStellar(identity, address2, status, opts.network, adapter, devicePubkey, relayer, opts.sourceKeypair);
1931
- const self = build("", "needs-device-approval");
1932
- const readSource = await self.resolveSource();
1933
- const existing = await registry.lookup(identity.userId);
1934
- if (existing) {
1935
- const isSigner2 = await adapter.isAuthorizedSigner(existing.address, devicePubkey, readSource);
1936
- return build(existing.address, isSigner2 ? "ready" : "needs-device-approval");
1937
- }
1938
- const address = adapter.computeAddress(addressSeed, devicePubkey);
1939
- const wasDeployed = await adapter.isDeployed(address);
1940
- if (!wasDeployed) {
1941
- const func = adapter.buildDeploy(addressSeed, devicePubkey);
1942
- await self.submitHostFunction(func, void 0);
2621
+ const build = (status, unlocked) => new _CavosStellar(
2622
+ identity,
2623
+ address,
2624
+ status,
2625
+ opts.network,
2626
+ adapter,
2627
+ opts.deviceKey,
2628
+ unlocked?.control,
2629
+ unlocked?.dek,
2630
+ relayer
2631
+ );
2632
+ if (await adapter.isDeployed(address)) {
2633
+ const unlocked = await unlockViaDevice(adapter, address, opts.deviceKey);
2634
+ return build(unlocked ? "ready" : "needs-device-approval", unlocked ?? void 0);
2635
+ }
2636
+ if (!relayer && !opts.sourceKeypair) {
2637
+ throw new Error("kit/stellar: a relayer (appId) or sourceKeypair is required to create the account");
2638
+ }
2639
+ const { keypair: control, seed: controlSeed } = generateControlKey();
2640
+ const dek = generateDEK();
2641
+ const envelope = {
2642
+ ct: sealControlSeed(controlSeed, dek),
2643
+ deviceWraps: { [opts.deviceKey.slotId()]: eciesWrapDEK(dek, opts.deviceKey.publicKeySec1()) }
2644
+ };
2645
+ if (relayer) {
2646
+ const relayerSource = await relayer.getSource();
2647
+ const tx3 = await adapter.buildSponsoredCreateTx({
2648
+ relayer: relayerSource,
2649
+ masterAddress: address,
2650
+ controlAddress: control.publicKey(),
2651
+ envelope
2652
+ });
2653
+ tx3.sign(master);
2654
+ await relayer.submit("create", tx3.toXDR());
2655
+ } else {
2656
+ const funder = opts.sourceKeypair;
2657
+ const tx3 = await adapter.buildCreateTx({
2658
+ funder: funder.publicKey(),
2659
+ masterAddress: address,
2660
+ controlAddress: control.publicKey(),
2661
+ envelope,
2662
+ startingBalance
2663
+ });
2664
+ tx3.sign(master, funder);
2665
+ await adapter.submit(tx3);
1943
2666
  }
1944
- await registry.register({ userId: identity.userId, address, initialSigner: devicePubkey });
1945
- const isSigner = await adapter.isAuthorizedSigner(address, devicePubkey, readSource);
1946
- const wallet = build(address, isSigner ? "ready" : "needs-device-approval");
1947
- wallet.isNewAccount = !wasDeployed && isSigner;
2667
+ const wallet = build("ready", { control, dek });
2668
+ wallet.isNewAccount = true;
1948
2669
  return wallet;
1949
2670
  }
1950
- /** Authorize an additional device signer (device-signed via `__check_auth`). */
1951
- async addSigner(pubkey) {
1952
- const func = this.adapter.buildAddSigner(this.address, pubkey);
1953
- return this.submitHostFunction(func, this.address);
1954
- }
1955
- /**
1956
- * Enroll a passkey as an approver (2FA-style step-up). Device-signed + gasless;
1957
- * requires a ready device. Idempotent. Returns the passkey pubkey + tx hash.
1958
- */
1959
- async enrollPasskey(passkey, params) {
1960
- const enrolled = await passkey.enroll(params);
1961
- const { transactionHash } = await this.addApprover(enrolled.publicKey);
1962
- return { publicKey: enrolled.publicKey, transactionHash };
1963
- }
1964
- /** Register an already-enrolled passkey pubkey as an approver (gasless).
1965
- * Idempotent. Lets one passkey be registered across chains without re-prompting. */
1966
- async addApprover(pubkey) {
1967
- if (this.status !== "ready") {
1968
- throw new Error("kit/stellar: addApprover requires a ready, authorized device");
1969
- }
1970
- const readSource = await this.resolveSource();
1971
- if (await this.adapter.isApprover(this.address, pubkey, readSource)) return {};
1972
- const func = this.adapter.buildAddApprover(this.address, pubkey);
1973
- const transactionHash = await this.submitHostFunction(func, this.address);
1974
- return { transactionHash };
2671
+ /** Native XLM balance of the account, in stroops. */
2672
+ async balance() {
2673
+ return this.adapter.balance(this.address);
1975
2674
  }
1976
- /** True if this account already has a passkey enrolled as an approver, so a
1977
- * new device can be approved with the passkey instead of the email flow. */
2675
+ /** True if the account has a passkey factor enrolled (`cv:wp`), so a new device
2676
+ * can be approved with the passkey instead of a recovery code. Mirrors the
2677
+ * other chains' `hasPasskey()` for the React provider. */
1978
2678
  async hasPasskey() {
1979
- const readSource = await this.resolveSource();
1980
- return this.adapter.hasPasskeyApprover(this.address, readSource);
2679
+ try {
2680
+ const env = fromDataEntries(await this.adapter.loadDataEntries(this.address));
2681
+ return !!env.passkeyWrap;
2682
+ } catch {
2683
+ return false;
2684
+ }
1981
2685
  }
1982
- /** Re-read (from chain) whether THIS device is now an authorized signer.
1983
- * Used to poll for readiness after a passkey approval before it's indexed. */
2686
+ /** Whether the control key is unlocked on this device (status ready). Classic
2687
+ * approvals land synchronously via Horizon, so this reflects state immediately
2688
+ * (no indexing delay to poll for). */
1984
2689
  async isReady() {
1985
- const readSource = await this.resolveSource();
1986
- return this.adapter.isAuthorizedSigner(this.address, this.devicePubkey, readSource);
2690
+ return this.statusValue === "ready";
1987
2691
  }
1988
2692
  /**
1989
- * From a fresh browser (status `needs-device-approval`), approve adding THIS
1990
- * device using the user's synced passkey. Gasless via the relayer — the call
1991
- * carries the WebAuthn assertion, so no device signature is needed. Returns the
1992
- * tx hash. No trip back to an already-authorized device.
2693
+ * Move `amount` stroops of native XLM to `destination`, signed by the control
2694
+ * key. Sponsored by default (the relayer fee-bumps and pays the fee); pass
2695
+ * `{ sponsored: false }` to submit directly the account pays its own (tiny)
2696
+ * fee from its XLM balance. The control key signs identically in both modes;
2697
+ * only the fee payer differs.
1993
2698
  */
1994
- async approveThisDeviceWithPasskey(passkey) {
1995
- if (this.status === "ready") {
1996
- throw new Error("kit/stellar: this device is already an authorized signer");
1997
- }
1998
- const { leaf, nonce } = await this.passkeyLeafForThisDevice();
1999
- const leaves = [leaf];
2000
- const assertion = await passkey.assert(batchChallenge(leaves));
2001
- const { transactionHash } = await this.submitPasskeyApproval(assertion, leaves, 0, nonce);
2002
- return transactionHash;
2699
+ async execute(amount, destination, opts) {
2700
+ const control = this.requireControl();
2701
+ const inner = await this.adapter.buildPaymentTx({ from: this.address, to: destination, amount });
2702
+ return this.submitInner(inner, control, opts);
2003
2703
  }
2004
- /** This device's leaf + passkey nonce for a (possibly multi-chain) batch. */
2005
- async passkeyLeafForThisDevice() {
2006
- const readSource = await this.resolveSource();
2007
- const nonce = await this.adapter.passkeyNonce(this.address, readSource);
2008
- return { leaf: this.adapter.passkeyLeaf(this.devicePubkey, nonce), nonce };
2704
+ /**
2705
+ * Enroll a passkey as an unlock factor: wrap the DEK under the passkey's PRF
2706
+ * output and write the `cv:wp` entry. This is the synced anchor used to approve
2707
+ * a new device or recover — it survives device loss. Idempotent-ish: writing it
2708
+ * again just overwrites the wrap of the same DEK. Requires a ready device.
2709
+ */
2710
+ async enrollPasskey(prfOutput) {
2711
+ const { control, dek } = this.requireUnlocked();
2712
+ const wrap = wrapDEK(dek, derivePasskeyKEK(prfOutput));
2713
+ return this.writeFactor(PASSKEY_BASE, wrap, control);
2009
2714
  }
2010
- /** Submit `add_signer_via_passkey` given a shared assertion + batch position.
2011
- * No device auth entry authorized purely by the passkey assertion. */
2012
- async submitPasskeyApproval(assertion, leaves, leafIndex, nonce) {
2013
- const readSource = await this.resolveSource();
2014
- const digest = webauthnDigest(assertion.authenticatorData, assertion.clientDataJSON);
2015
- const candidates = recoverCandidatePublicKeys(assertion.r, assertion.s, digest);
2016
- let approver = null;
2017
- for (const cand of candidates) {
2018
- if (await this.adapter.isApprover(this.address, cand.publicKey, readSource)) {
2019
- approver = cand.publicKey;
2020
- break;
2021
- }
2022
- }
2023
- if (!approver) throw new Error("kit/stellar: this passkey is not a registered approver");
2024
- const func = this.adapter.buildAddSignerViaPasskey(
2025
- this.address,
2026
- this.devicePubkey,
2027
- approver,
2028
- nonce,
2029
- leaves,
2030
- leafIndex,
2031
- assertion
2032
- );
2033
- return { transactionHash: await this.submitHostFunction(func, void 0) };
2715
+ /**
2716
+ * Set up a recovery code as an unlock factor: wrap the DEK under the code's KEK
2717
+ * and write the `cv:wr` entry. Optional in v1 — the integrating app decides when
2718
+ * to surface it. The code never leaves the device; only the wrap goes on-chain.
2719
+ * Requires a ready device.
2720
+ */
2721
+ async setupRecovery(code) {
2722
+ const { control, dek } = this.requireUnlocked();
2723
+ const wrap = wrapDEK(dek, deriveRecoveryKEK(code));
2724
+ return this.writeFactor(RECOVERY_BASE, wrap, control);
2034
2725
  }
2035
- /** Move `amount` stroops of native XLM to `destination` (device-signed). */
2036
- async execute(amount, destination) {
2037
- return this.executeTransfer(NATIVE_SAC_ID[this.network], amount, destination);
2726
+ /**
2727
+ * From a new browser/device (`needs-device-approval`), approve THIS device using
2728
+ * the user's synced passkey: unlock the DEK via the passkey factor, then wrap it
2729
+ * to this device's slot so future sessions unlock silently. Flips status to
2730
+ * `ready`. No trip back to an already-authorized device.
2731
+ */
2732
+ async approveThisDeviceWithPasskey(prfOutput) {
2733
+ return this.approveThisDevice(
2734
+ await unlockViaPasskey(this.adapter, this.address, prfOutput),
2735
+ "passkey"
2736
+ );
2038
2737
  }
2039
- /** Read this account's balance of `tokenId` (defaults to native XLM), in stroops. */
2040
- async balance(tokenId = NATIVE_SAC_ID[this.network]) {
2041
- const readSource = await this.resolveSource();
2042
- return this.adapter.readBalance(tokenId, this.address, readSource);
2738
+ /** Approve THIS device using the recovery code (same as the passkey path, for
2739
+ * the backup factor). */
2740
+ async approveThisDeviceWithRecovery(code) {
2741
+ return this.approveThisDevice(
2742
+ await unlockViaRecovery(this.adapter, this.address, code),
2743
+ "recovery code"
2744
+ );
2043
2745
  }
2044
- /** Transfer `amount` of any SEP-41 token out of the account (device-signed). */
2045
- async executeTransfer(tokenId, amount, destination) {
2046
- if (this.status !== "ready") {
2047
- throw new Error("kit/stellar: this device is not yet an authorized signer of the wallet");
2048
- }
2049
- const func = this.adapter.buildTransfer(tokenId, this.address, destination, amount);
2050
- return this.submitHostFunction(func, this.address);
2746
+ /** The control key's public G address (the weight-1 real signer), for display. */
2747
+ get controlAddress() {
2748
+ return this.control?.publicKey();
2749
+ }
2750
+ // --- internals ----------------------------------------------------------
2751
+ async approveThisDevice(unlocked, factor) {
2752
+ if (this.statusValue === "ready") {
2753
+ throw new Error("kit/stellar: this device is already authorized");
2754
+ }
2755
+ if (!unlocked) {
2756
+ throw new Error(`kit/stellar: could not unlock the account with the ${factor} \u2014 wrong factor or not enrolled`);
2757
+ }
2758
+ const slot = this.deviceKey.slotId();
2759
+ const wrap = eciesWrapDEK(unlocked.dek, this.deviceKey.publicKeySec1());
2760
+ const hash5 = await this.submitDataWrite(deviceWrapEntries(slot, wrap), unlocked.control);
2761
+ this.control = unlocked.control;
2762
+ this.dek = unlocked.dek;
2763
+ this.statusValue = "ready";
2764
+ return hash5;
2765
+ }
2766
+ /** Write a single-factor wrap (passkey/recovery) into the account data entries,
2767
+ * signed by the control key. Overwrites cleanly if the base already existed and
2768
+ * the new blob has the same chunk count. */
2769
+ async writeFactor(base, wrap, control) {
2770
+ const entries = {};
2771
+ chunkTo64(wrap).forEach((chunk, i) => {
2772
+ entries[`${base}/${i}`] = chunk;
2773
+ });
2774
+ return this.submitDataWrite(entries, control);
2051
2775
  }
2052
2776
  /**
2053
- * Register the backup signer derived from `code` as an authorized signer of
2054
- * this account (device-signed). Idempotent. The code never leaves the device
2055
- * only the derived public key travels on-chain. Mirrors the other chains.
2777
+ * Sign an inner (account-sourced) payment tx with the control key and submit it:
2778
+ * - sponsored (default) with a relayer, wrap in a fee-bump (relayer pays
2779
+ * the fee) and POST; falls back to self-funded if no relayer;
2780
+ * - `{ sponsored: false }` → submit directly (the account pays its own fee).
2781
+ * Payments add no subentries, so no reserve sponsorship is needed here.
2056
2782
  */
2057
- async setupRecovery(code) {
2058
- if (this.status !== "ready") {
2059
- throw new Error("kit/stellar: setupRecovery requires a ready, registered device");
2783
+ async submitInner(inner, control, opts) {
2784
+ inner.sign(control);
2785
+ const sponsored = opts?.sponsored !== false;
2786
+ if (sponsored && this.relayer) {
2787
+ const feeSource = await this.relayer.getSource();
2788
+ const bump = this.adapter.wrapFeeBump(inner, feeSource);
2789
+ return this.relayer.submit("fee-bump", bump.toXDR());
2060
2790
  }
2061
- const { publicKey: backupPubkey } = deriveBackupKey(code);
2062
- const readSource = await this.resolveSource();
2063
- if (await this.adapter.isAuthorizedSigner(this.address, backupPubkey, readSource)) return void 0;
2064
- return this.addSigner(backupPubkey);
2791
+ return this.adapter.submit(inner);
2065
2792
  }
2066
2793
  /**
2067
- * Recover an account after losing every device signer: derive the backup key
2068
- * from `code`, use it (not the new device) to authorize `add_signer(newDevice)`,
2069
- * and return a ready handle bound to the new device. The address is unchanged.
2794
+ * Write data entries (add a factor / device slot) which create NEW subentries
2795
+ * that each need ~0.5 XLM of reserve. A relayer-sponsored account holds no XLM,
2796
+ * so the write must be sponsored by the relayer (source + sponsor), exactly like
2797
+ * account creation — a plain fee-bump would fail with `op_low_reserve`.
2798
+ * - sponsored (default) → with a relayer, build a sponsored write (relayer
2799
+ * source + begin/end sponsoring), control-sign the account ops, relay
2800
+ * co-signs + submits; falls back to self-funded if no relayer;
2801
+ * - `{ sponsored: false }` → the account writes directly (it must hold its
2802
+ * own reserve for the new subentries).
2070
2803
  */
2071
- static async recover(opts) {
2072
- const signer = opts.createSigner ? await opts.createSigner(`${opts.identity.userId}:${opts.appSalt}`) : await WebCryptoSigner.loadOrCreate({ keyId: `${opts.identity.userId}:${opts.appSalt}` });
2073
- const devicePubkey = await signer.getPublicKey();
2074
- const backup = BackupSigner.fromCode(opts.code);
2075
- const backupAdapter = new StellarAdapter({
2076
- network: opts.network,
2077
- rpcUrl: opts.rpcUrl,
2078
- factoryId: opts.factoryId,
2079
- signer: backup
2080
- });
2081
- const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
2082
- const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry2);
2083
- const existing = await registry.lookup(opts.identity.userId);
2084
- if (!existing) {
2085
- throw new Error("kit/stellar: no account found for this identity \u2014 nothing to recover");
2804
+ async submitDataWrite(entries, control, opts) {
2805
+ const sponsored = opts?.sponsored !== false;
2806
+ if (sponsored && this.relayer) {
2807
+ const relayerSource = await this.relayer.getSource();
2808
+ const tx4 = await this.adapter.buildSponsoredDataTx({
2809
+ relayer: relayerSource,
2810
+ account: this.address,
2811
+ entries
2812
+ });
2813
+ tx4.sign(control);
2814
+ return this.relayer.submit("sponsored-data", tx4.toXDR());
2086
2815
  }
2087
- const relayer = opts.relayer ?? (opts.appId ? new StellarRelayer({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : void 0);
2088
- const backupHandle = new _CavosStellar(
2089
- opts.identity,
2090
- existing.address,
2091
- "ready",
2092
- opts.network,
2093
- backupAdapter,
2094
- devicePubkey,
2095
- relayer,
2096
- opts.sourceKeypair
2097
- );
2098
- const readSource = await backupHandle.resolveSource();
2099
- if (!await backupAdapter.isAuthorizedSigner(existing.address, devicePubkey, readSource)) {
2100
- await backupHandle.addSigner(devicePubkey);
2816
+ const tx3 = await this.adapter.buildDataTx({ account: this.address, entries });
2817
+ tx3.sign(control);
2818
+ return this.adapter.submit(tx3);
2819
+ }
2820
+ requireControl() {
2821
+ if (this.statusValue !== "ready" || !this.control) {
2822
+ throw new Error("kit/stellar: control key not unlocked on this device (needs approval)");
2101
2823
  }
2102
- const adapter = new StellarAdapter({
2103
- network: opts.network,
2104
- rpcUrl: opts.rpcUrl,
2105
- factoryId: opts.factoryId,
2106
- signer
2107
- });
2108
- return new _CavosStellar(
2109
- opts.identity,
2110
- existing.address,
2111
- "ready",
2112
- opts.network,
2113
- adapter,
2114
- devicePubkey,
2115
- relayer,
2116
- opts.sourceKeypair
2824
+ return this.control;
2825
+ }
2826
+ requireUnlocked() {
2827
+ const control = this.requireControl();
2828
+ if (!this.dek) throw new Error("kit/stellar: DEK unavailable on this device");
2829
+ return { control, dek: this.dek };
2830
+ }
2831
+ };
2832
+ async function unlockViaDevice(adapter, address, deviceKey) {
2833
+ const env = await loadEnvelope(adapter, address);
2834
+ const wrap = env.deviceWraps[deviceKey.slotId()];
2835
+ if (!wrap) return null;
2836
+ try {
2837
+ const dek = await deviceKey.unwrap(wrap);
2838
+ return openControl(env, dek);
2839
+ } catch {
2840
+ return null;
2841
+ }
2842
+ }
2843
+ async function unlockViaPasskey(adapter, address, prfOutput) {
2844
+ const env = await loadEnvelope(adapter, address);
2845
+ if (!env.passkeyWrap) return null;
2846
+ try {
2847
+ const dek = unwrapDEK(env.passkeyWrap, derivePasskeyKEK(prfOutput));
2848
+ return openControl(env, dek);
2849
+ } catch {
2850
+ return null;
2851
+ }
2852
+ }
2853
+ async function unlockViaRecovery(adapter, address, code) {
2854
+ const env = await loadEnvelope(adapter, address);
2855
+ if (!env.recoveryWrap) return null;
2856
+ try {
2857
+ const dek = unwrapDEK(env.recoveryWrap, deriveRecoveryKEK(code));
2858
+ return openControl(env, dek);
2859
+ } catch {
2860
+ return null;
2861
+ }
2862
+ }
2863
+ async function loadEnvelope(adapter, address) {
2864
+ return fromDataEntries(await adapter.loadDataEntries(address));
2865
+ }
2866
+ function openControl(env, dek) {
2867
+ const controlSeed = openControlSeed(env.ct, dek);
2868
+ return { control: controlKeypairFromSeed(controlSeed), dek };
2869
+ }
2870
+ var LocalDeviceUnwrapKey = class _LocalDeviceUnwrapKey {
2871
+ constructor(scalar) {
2872
+ this.scalar = scalar;
2873
+ }
2874
+ /** Generate a fresh device unwrap key. */
2875
+ static generate() {
2876
+ return new _LocalDeviceUnwrapKey(p256.utils.randomPrivateKey());
2877
+ }
2878
+ /** Rebuild from a persisted 32-byte scalar. */
2879
+ static fromScalar(scalar) {
2880
+ if (scalar.length !== 32) throw new Error("kit/stellar: device unwrap scalar must be 32 bytes");
2881
+ return new _LocalDeviceUnwrapKey(scalar);
2882
+ }
2883
+ /** The raw scalar, for the caller to persist in secure storage. */
2884
+ export() {
2885
+ return this.scalar;
2886
+ }
2887
+ publicKeySec1() {
2888
+ return p256.getPublicKey(this.scalar, false);
2889
+ }
2890
+ slotId() {
2891
+ return deviceSlotId(this.publicKeySec1());
2892
+ }
2893
+ async unwrap(blob) {
2894
+ return eciesUnwrapDEK(blob, this.scalar);
2895
+ }
2896
+ };
2897
+ function deviceSlotId(publicKeySec1) {
2898
+ const h = sha256(publicKeySec1);
2899
+ let s = "";
2900
+ for (const b of h.subarray(0, 4)) s += b.toString(16).padStart(2, "0");
2901
+ return s;
2902
+ }
2903
+ var IDB_NAME2 = "cavos-kit-stellar";
2904
+ var IDB_STORE2 = "unwrap-keys";
2905
+ var WebCryptoDeviceUnwrapKey = class _WebCryptoDeviceUnwrapKey {
2906
+ constructor(privateKey, publicRaw, keyId) {
2907
+ this.privateKey = privateKey;
2908
+ this.publicRaw = publicRaw;
2909
+ this.keyId = keyId;
2910
+ }
2911
+ /** Create a fresh device unwrap key and persist it. */
2912
+ static async create(opts) {
2913
+ assertSecureContext2();
2914
+ const pair = await crypto.subtle.generateKey(
2915
+ { name: "ECDH", namedCurve: "P-256" },
2916
+ false,
2917
+ // private key is NON-extractable
2918
+ ["deriveBits"]
2117
2919
  );
2920
+ const publicRaw = new Uint8Array(await crypto.subtle.exportKey("raw", pair.publicKey));
2921
+ await idbPut2(opts.keyId, { privateKey: pair.privateKey, publicRaw });
2922
+ return new _WebCryptoDeviceUnwrapKey(pair.privateKey, publicRaw, opts.keyId);
2118
2923
  }
2119
- /** The transaction source/fee-payer G-address (relayer or self-funded). */
2120
- async resolveSource() {
2121
- if (this.relayer) return this.relayer.getSource();
2122
- if (this.sourceKeypair) return this.sourceKeypair.publicKey();
2123
- throw new Error("kit/stellar: a relayer (appId) or sourceKeypair is required");
2924
+ /** Load an existing device unwrap key, or null if none exists yet. */
2925
+ static async load(opts) {
2926
+ const rec = await idbGet2(opts.keyId);
2927
+ if (!rec) return null;
2928
+ return new _WebCryptoDeviceUnwrapKey(rec.privateKey, rec.publicRaw, opts.keyId);
2124
2929
  }
2125
- /**
2126
- * Build simulate → device-sign auth → assemble → submit an invoke-contract
2127
- * host function. `authAccount` is the account whose `__check_auth` must sign the
2128
- * operation's Soroban auth entry (undefined for a plain factory deploy).
2129
- */
2130
- async submitHostFunction(func, authAccount) {
2131
- const server = this.adapter.server();
2132
- const sourceAddr = await this.resolveSource();
2133
- const simSource = new Account(sourceAddr, "0");
2134
- const unsignedOp = Operation.invokeHostFunction({ func, auth: [] });
2135
- const simTx = new TransactionBuilder(simSource, {
2136
- fee: BASE_FEE,
2137
- networkPassphrase: this.adapter.passphrase
2138
- }).addOperation(unsignedOp).setTimeout(180).build();
2139
- const sim = await server.simulateTransaction(simTx);
2140
- if (rpc.Api.isSimulationError(sim)) {
2141
- throw new Error(`kit/stellar: simulation failed: ${sim.error}`);
2142
- }
2143
- const validUntil = (await server.getLatestLedger()).sequence + 100;
2144
- const entries = sim.result?.auth ?? [];
2145
- const signedAuth = [];
2146
- for (const entry of entries) {
2147
- if (authAccount && isAddressCredentialFor(entry, authAccount)) {
2148
- signedAuth.push(await this.adapter.signAuthEntry(entry, validUntil));
2149
- } else {
2150
- signedAuth.push(entry);
2151
- }
2152
- }
2153
- const account = await server.getAccount(sourceAddr);
2154
- const finalOp = Operation.invokeHostFunction({ func, auth: signedAuth });
2155
- const built = new TransactionBuilder(account, {
2156
- fee: BASE_FEE,
2157
- networkPassphrase: this.adapter.passphrase
2158
- }).addOperation(finalOp).setTimeout(180).build();
2159
- const authSim = await server.simulateTransaction(built);
2160
- if (rpc.Api.isSimulationError(authSim)) {
2161
- throw new Error(`kit/stellar: auth simulation failed: ${authSim.error}`);
2162
- }
2163
- const assembled = rpc.assembleTransaction(built, authSim).build();
2164
- if (this.relayer) {
2165
- return this.relayer.submit(assembled.toXDR());
2166
- }
2167
- if (this.sourceKeypair) {
2168
- assembled.sign(this.sourceKeypair);
2169
- return this.sendAndConfirm(assembled);
2170
- }
2171
- throw new Error("kit/stellar: no relayer or sourceKeypair configured to submit");
2172
- }
2173
- /** Submit a signed tx via RPC and poll to confirmation. Returns the hash. */
2174
- async sendAndConfirm(tx2) {
2175
- const server = this.adapter.server();
2176
- const sent = await server.sendTransaction(tx2);
2177
- if (sent.status === "ERROR") {
2178
- throw new Error(`kit/stellar: submit rejected: ${JSON.stringify(sent.errorResult)}`);
2179
- }
2180
- const hash6 = sent.hash;
2181
- for (let i = 0; i < 30; i++) {
2182
- const got = await server.getTransaction(hash6);
2183
- if (got.status === rpc.Api.GetTransactionStatus.SUCCESS) return hash6;
2184
- if (got.status === rpc.Api.GetTransactionStatus.FAILED) {
2185
- throw new Error(`kit/stellar: tx ${hash6} failed`);
2186
- }
2187
- await new Promise((r) => setTimeout(r, 1e3));
2188
- }
2189
- throw new Error(`kit/stellar: tx ${hash6} not confirmed in time`);
2930
+ /** Load the device unwrap key, creating one on first use. */
2931
+ static async loadOrCreate(opts) {
2932
+ return await _WebCryptoDeviceUnwrapKey.load(opts) ?? await _WebCryptoDeviceUnwrapKey.create(opts);
2933
+ }
2934
+ publicKeySec1() {
2935
+ return this.publicRaw;
2936
+ }
2937
+ slotId() {
2938
+ return deviceSlotId(this.publicRaw);
2939
+ }
2940
+ async unwrap(blob) {
2941
+ const ephPubCompressed = blob.subarray(0, 33);
2942
+ const wrapped = blob.subarray(33);
2943
+ const ephUncompressed = p256.ProjectivePoint.fromHex(ephPubCompressed).toRawBytes(false);
2944
+ const ephKey = await crypto.subtle.importKey(
2945
+ "raw",
2946
+ ephUncompressed,
2947
+ { name: "ECDH", namedCurve: "P-256" },
2948
+ false,
2949
+ []
2950
+ );
2951
+ const sharedX = new Uint8Array(
2952
+ await crypto.subtle.deriveBits({ name: "ECDH", public: ephKey }, this.privateKey, 256)
2953
+ );
2954
+ const kek = eciesKEKFromX(sharedX, ephPubCompressed);
2955
+ return unwrapDEK(wrapped, kek);
2190
2956
  }
2191
2957
  };
2192
- function isAddressCredentialFor(entry, accountAddress) {
2193
- const creds = entry.credentials();
2194
- if (creds.switch() !== xdr.SorobanCredentialsType.sorobanCredentialsAddress()) return false;
2195
- return Address.fromScAddress(creds.address().address()).toString() === accountAddress;
2958
+ function assertSecureContext2() {
2959
+ const ok = typeof crypto !== "undefined" && typeof crypto.subtle !== "undefined" && (typeof window === "undefined" || window.isSecureContext);
2960
+ if (!ok) {
2961
+ throw new Error(
2962
+ "Cavos: WebCrypto is unavailable. Device keys require a secure context \u2014 use HTTPS, or http://localhost."
2963
+ );
2964
+ }
2965
+ }
2966
+ function openDb2() {
2967
+ return new Promise((resolve, reject) => {
2968
+ const req = indexedDB.open(IDB_NAME2, 1);
2969
+ req.onupgradeneeded = () => req.result.createObjectStore(IDB_STORE2);
2970
+ req.onsuccess = () => resolve(req.result);
2971
+ req.onerror = () => reject(req.error);
2972
+ });
2973
+ }
2974
+ async function idbPut2(keyId, value) {
2975
+ const db = await openDb2();
2976
+ await tx2(db, "readwrite", (store) => store.put(value, keyId));
2977
+ db.close();
2978
+ }
2979
+ async function idbGet2(keyId) {
2980
+ const db = await openDb2();
2981
+ const result = await tx2(db, "readonly", (store) => store.get(keyId));
2982
+ db.close();
2983
+ return result ?? null;
2984
+ }
2985
+ function tx2(db, mode, run) {
2986
+ return new Promise((resolve, reject) => {
2987
+ const store = db.transaction(IDB_STORE2, mode).objectStore(IDB_STORE2);
2988
+ const req = run(store);
2989
+ req.onsuccess = () => resolve(req.result);
2990
+ req.onerror = () => reject(req.error);
2991
+ });
2196
2992
  }
2197
- var defaultRegistry2 = new InMemoryWalletRegistry();
2198
2993
 
2199
2994
  // src/recovery/HttpRecoveryClient.ts
2200
2995
  function toHex2(n) {
@@ -2324,17 +3119,16 @@ var Cavos = class _Cavos {
2324
3119
  });
2325
3120
  }
2326
3121
  if (opts.chain === "stellar") {
3122
+ const identity = opts.identity ?? (opts.auth ? await opts.auth.authenticate() : void 0);
3123
+ if (!identity) throw new Error("kit: Stellar connect requires `identity` or `auth`");
3124
+ const deviceKey = opts.stellarDeviceKey ?? await WebCryptoDeviceUnwrapKey.loadOrCreate({ keyId: `${identity.userId}:${opts.appSalt}` });
2327
3125
  return CavosStellar.connect({
2328
3126
  network: STELLAR_ENV[opts.network],
2329
- ...opts.auth ? { auth: opts.auth } : {},
2330
- ...opts.identity ? { identity: opts.identity } : {},
3127
+ identity,
2331
3128
  appSalt: opts.appSalt,
3129
+ deviceKey,
2332
3130
  ...opts.appId ? { appId: opts.appId } : {},
2333
3131
  ...opts.backendUrl ? { backendUrl: opts.backendUrl } : {},
2334
- ...opts.registry ? { registry: opts.registry } : {},
2335
- ...opts.rpcUrl ? { rpcUrl: opts.rpcUrl } : {},
2336
- ...opts.factoryId ? { factoryId: opts.factoryId } : {},
2337
- ...opts.createSigner ? { createSigner: opts.createSigner } : {},
2338
3132
  ...opts.stellarRelayer ? { relayer: opts.stellarRelayer } : {},
2339
3133
  ...opts.stellarSourceKeypair ? { sourceKeypair: opts.stellarSourceKeypair } : {}
2340
3134
  });
@@ -2384,7 +3178,7 @@ var Cavos = class _Cavos {
2384
3178
  cairoVersion: "1"
2385
3179
  });
2386
3180
  const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
2387
- const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry3);
3181
+ const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network: opts.network }) : defaultRegistry2);
2388
3182
  const recovery = opts.recovery ?? (opts.appId ? new HttpRecoveryClient({ baseUrl: backendUrl, appId: opts.appId }) : null);
2389
3183
  const existing = await registry.lookup(identity.userId);
2390
3184
  if (existing) {
@@ -2421,7 +3215,7 @@ var Cavos = class _Cavos {
2421
3215
  }
2422
3216
  return cavos2;
2423
3217
  }
2424
- const address = adapter.computeAddress({ addressSeed, initialSigner: devicePubkey });
3218
+ const address = adapter.computeAddress({ addressSeed });
2425
3219
  const account = makeAccount(address);
2426
3220
  const alreadyDeployed = await isDeployed(provider, address);
2427
3221
  if (!alreadyDeployed) {
@@ -2429,10 +3223,11 @@ var Cavos = class _Cavos {
2429
3223
  address,
2430
3224
  class_hash: classHash,
2431
3225
  salt: num.toHex(addressSeed),
2432
- calldata: adapter.constructorCalldata(addressSeed, devicePubkey),
3226
+ calldata: adapter.constructorCalldata(addressSeed),
2433
3227
  version: 1
2434
3228
  };
2435
- const deployRes = await account.executePaymasterTransaction([], {
3229
+ const initCall = adapter.buildInitialize(address, devicePubkey);
3230
+ const deployRes = await account.executePaymasterTransaction([initCall], {
2436
3231
  feeMode: { mode: "sponsored" },
2437
3232
  deploymentData
2438
3233
  });
@@ -2467,18 +3262,25 @@ var Cavos = class _Cavos {
2467
3262
  return this.devicePubkey;
2468
3263
  }
2469
3264
  /** Execute a sponsored (gasless) multicall, signed silently by the device. */
2470
- async execute(calls) {
3265
+ async execute(calls, opts) {
2471
3266
  if (this.status !== "ready") {
2472
3267
  throw new Error("kit: this device is not yet an authorized signer of the wallet");
2473
3268
  }
3269
+ if (opts?.sponsored === false) {
3270
+ const res2 = await this.account.execute(calls);
3271
+ return { transactionHash: res2.transaction_hash };
3272
+ }
2474
3273
  const res = await this.account.executePaymasterTransaction(calls, {
2475
3274
  feeMode: { mode: "sponsored" }
2476
3275
  });
2477
3276
  return { transactionHash: res.transaction_hash };
2478
3277
  }
2479
- /** Authorize an additional device signer (sponsored). Self-submitted. */
2480
- async addSigner(pubkey) {
2481
- return this.execute([this.adapter.buildAddSigner(this.address, pubkey)]);
3278
+ /**
3279
+ * Authorize an additional device signer. Sponsored by default; pass
3280
+ * `{ sponsored: false }` to pay the fee from the account's own ETH balance.
3281
+ */
3282
+ async addSigner(pubkey, opts) {
3283
+ return this.execute([this.adapter.buildAddSigner(this.address, pubkey)], opts);
2482
3284
  }
2483
3285
  /**
2484
3286
  * Enroll a passkey as an APPROVER so the user can later add devices from any
@@ -2487,25 +3289,27 @@ var Cavos = class _Cavos {
2487
3289
  * approver. Call this whenever the app decides to prompt "turn on device
2488
3290
  * approvals". Returns the passkey's public key + the enrollment tx hash.
2489
3291
  */
2490
- async enrollPasskey(passkey, params) {
3292
+ async enrollPasskey(passkey, params, opts) {
2491
3293
  const enrolled = await passkey.enroll(params);
2492
- const { transactionHash } = await this.addApprover(enrolled.publicKey);
3294
+ const { transactionHash } = await this.addApprover(enrolled.publicKey, opts);
2493
3295
  return { publicKey: enrolled.publicKey, transactionHash };
2494
3296
  }
2495
3297
  /**
2496
- * Register an ALREADY-enrolled passkey public key as an approver (gasless,
2497
- * device-signed). Idempotent. Use this to register ONE passkey across multiple
2498
- * chains without re-prompting `passkey.enroll()` on each: enroll once, then
2499
- * call `addApprover(pubkey)` on each chain's wallet.
3298
+ * Register an ALREADY-enrolled passkey public key as an approver (gasless by
3299
+ * default, device-signed). Idempotent. Use this to register ONE passkey across
3300
+ * multiple chains without re-prompting `passkey.enroll()` on each: enroll once,
3301
+ * then call `addApprover(pubkey)` on each chain's wallet. Pass
3302
+ * `{ sponsored: false }` to pay the fee from the account's own balance.
2500
3303
  */
2501
- async addApprover(pubkey) {
3304
+ async addApprover(pubkey, opts) {
2502
3305
  if (this.status !== "ready") {
2503
3306
  throw new Error("kit: addApprover requires a ready, authorized device");
2504
3307
  }
2505
3308
  if (await this.adapter.isApprover(this.address, pubkey)) return {};
2506
- const { transactionHash } = await this.execute([
2507
- this.adapter.buildAddApprover(this.address, pubkey)
2508
- ]);
3309
+ const { transactionHash } = await this.execute(
3310
+ [this.adapter.buildAddApprover(this.address, pubkey)],
3311
+ opts
3312
+ );
2509
3313
  try {
2510
3314
  await this.account.waitForTransaction(transactionHash);
2511
3315
  } catch (e) {
@@ -2592,11 +3396,11 @@ var Cavos = class _Cavos {
2592
3396
  * add_signer (gasless). Returns the transaction hash (or undefined when the
2593
3397
  * backup was already set up).
2594
3398
  */
2595
- async setupRecovery(code) {
3399
+ async setupRecovery(code, opts) {
2596
3400
  const { publicKey: backupPubkey } = deriveBackupKey(code);
2597
3401
  const already = await this.adapter.isAuthorizedSigner(this.address, backupPubkey);
2598
3402
  if (already) return void 0;
2599
- return this.addSigner(backupPubkey);
3403
+ return this.addSigner(backupPubkey, opts);
2600
3404
  }
2601
3405
  /**
2602
3406
  * Recover an account after losing every device signer. Derives the backup key
@@ -2623,11 +3427,11 @@ var Cavos = class _Cavos {
2623
3427
  const backup = BackupSigner.fromCode(opts.code);
2624
3428
  const backupAdapter = new StarknetAdapter({ classHash, signer: backup, provider });
2625
3429
  const backendUrl = opts.backendUrl ?? "https://cavos.xyz";
2626
- const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network }) : defaultRegistry3);
2627
- const existing = await registry.lookup(opts.identity.userId);
2628
- if (!existing) {
3430
+ const address = opts.address ?? await lookupAddress(opts, backendUrl, network);
3431
+ if (!address) {
2629
3432
  throw new Error("kit: no account found for this identity \u2014 nothing to recover");
2630
3433
  }
3434
+ const existing = { address };
2631
3435
  const backupAccount = new Account$1({
2632
3436
  provider,
2633
3437
  address: existing.address,
@@ -2658,7 +3462,7 @@ var Cavos = class _Cavos {
2658
3462
  return new _Cavos(opts.identity, existing.address, "ready", account, adapter, devicePubkey);
2659
3463
  }
2660
3464
  };
2661
- var defaultRegistry3 = new InMemoryWalletRegistry();
3465
+ var defaultRegistry2 = new InMemoryWalletRegistry();
2662
3466
  var DEVICE_REQUEST_DEDUP_MS = 5 * 60 * 1e3;
2663
3467
  var lastDeviceRequest = /* @__PURE__ */ new Map();
2664
3468
  async function isDeployed(provider, address) {
@@ -2669,6 +3473,11 @@ async function isDeployed(provider, address) {
2669
3473
  return false;
2670
3474
  }
2671
3475
  }
3476
+ async function lookupAddress(opts, backendUrl, network) {
3477
+ const registry = opts.registry ?? (opts.appId ? new HttpWalletRegistry({ baseUrl: backendUrl, appId: opts.appId, network }) : defaultRegistry2);
3478
+ const existing = await registry.lookup(opts.identity.userId);
3479
+ return existing?.address ?? null;
3480
+ }
2672
3481
  async function approveDeviceEverywhere(wallets, passkey) {
2673
3482
  const targets = wallets.filter((w) => w.status === "needs-device-approval");
2674
3483
  if (targets.length === 0) return [];
@@ -2856,6 +3665,101 @@ function bytesToChunks(bytes) {
2856
3665
  for (const b of bytes.subarray(0, 31)) w = w << 8n | BigInt(b);
2857
3666
  return w;
2858
3667
  }
3668
+ var PRF_SALT = sha256(new TextEncoder().encode("cavos-stellar-prf-v1"));
3669
+ var PasskeyPrf = class {
3670
+ constructor(opts = {}) {
3671
+ if (typeof window === "undefined" || !navigator.credentials) {
3672
+ throw new Error("kit/passkey-prf: WebAuthn is only available in a browser");
3673
+ }
3674
+ this.rpId = opts.rpId ?? window.location.hostname;
3675
+ this.rpName = opts.rpName ?? this.rpId;
3676
+ if (isIpAddress(this.rpId)) {
3677
+ throw new Error(
3678
+ `kit/passkey-prf: passkeys can't use an IP address as the domain ("${this.rpId}"). Use http://localhost, a real HTTPS domain, or a tunnel \u2014 or pass an explicit \`rpId\`.`
3679
+ );
3680
+ }
3681
+ }
3682
+ /** True if this platform advertises a usable passkey (platform authenticator). */
3683
+ static async isSupported() {
3684
+ if (typeof window === "undefined" || !window.PublicKeyCredential) return false;
3685
+ try {
3686
+ return await window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable();
3687
+ } catch {
3688
+ return false;
3689
+ }
3690
+ }
3691
+ /**
3692
+ * Create a new synced, discoverable passkey with the PRF extension enabled, then
3693
+ * immediately read its PRF secret. Returns the credential id and the 32-byte
3694
+ * secret so the caller can enroll the passkey factor in one step. Some
3695
+ * authenticators don't return PRF results on create — in that case `secret` is
3696
+ * undefined and the caller should follow up with `getSecret()`.
3697
+ */
3698
+ async enroll(params) {
3699
+ const cred = await navigator.credentials.create({
3700
+ publicKey: {
3701
+ challenge: buf(crypto.getRandomValues(new Uint8Array(32))),
3702
+ rp: { id: this.rpId, name: this.rpName },
3703
+ user: {
3704
+ id: buf(userHandle(params.userId)),
3705
+ name: params.userName,
3706
+ displayName: params.displayName ?? params.userName
3707
+ },
3708
+ pubKeyCredParams: [{ type: "public-key", alg: -7 }],
3709
+ // ES256 (P-256)
3710
+ authenticatorSelection: {
3711
+ residentKey: "required",
3712
+ requireResidentKey: true,
3713
+ userVerification: "required"
3714
+ },
3715
+ attestation: "none",
3716
+ extensions: { prf: { eval: { first: buf(PRF_SALT) } } }
3717
+ }
3718
+ });
3719
+ if (!cred) throw new Error("kit/passkey-prf: enrollment cancelled");
3720
+ return { credentialId: new Uint8Array(cred.rawId), secret: readPrf(cred) };
3721
+ }
3722
+ /**
3723
+ * Get the passkey's 32-byte PRF secret. Uses discoverable credentials (no
3724
+ * `allowCredentials`), so it works from a brand-new browser — the OS shows the
3725
+ * synced passkey picker. Throws if the authenticator doesn't support PRF.
3726
+ */
3727
+ async getSecret() {
3728
+ const cred = await navigator.credentials.get({
3729
+ publicKey: {
3730
+ challenge: buf(crypto.getRandomValues(new Uint8Array(32))),
3731
+ rpId: this.rpId,
3732
+ allowCredentials: [],
3733
+ userVerification: "required",
3734
+ extensions: { prf: { eval: { first: buf(PRF_SALT) } } }
3735
+ }
3736
+ });
3737
+ if (!cred) throw new Error("kit/passkey-prf: assertion cancelled");
3738
+ const secret = readPrf(cred);
3739
+ if (!secret) {
3740
+ throw new Error(
3741
+ "kit/passkey-prf: this authenticator did not return a PRF result \u2014 PRF is unsupported here"
3742
+ );
3743
+ }
3744
+ return secret;
3745
+ }
3746
+ };
3747
+ function readPrf(cred) {
3748
+ const results = cred.getClientExtensionResults().prf?.results?.first;
3749
+ return results ? new Uint8Array(results) : void 0;
3750
+ }
3751
+ function isIpAddress(host) {
3752
+ if (/^\d{1,3}(\.\d{1,3}){3}$/.test(host)) return true;
3753
+ if (host.includes(":")) return true;
3754
+ return false;
3755
+ }
3756
+ function userHandle(userId) {
3757
+ const bytes = new TextEncoder().encode(userId);
3758
+ return bytes.length <= 64 ? bytes : sha256(bytes);
3759
+ }
3760
+ function buf(bytes) {
3761
+ return bytes.slice();
3762
+ }
2859
3763
  var PasskeySigner = class {
2860
3764
  constructor(opts = {}) {
2861
3765
  if (typeof window === "undefined" || !navigator.credentials) {
@@ -2863,7 +3767,7 @@ var PasskeySigner = class {
2863
3767
  }
2864
3768
  this.rpId = opts.rpId ?? window.location.hostname;
2865
3769
  this.rpName = opts.rpName ?? this.rpId;
2866
- if (isIpAddress(this.rpId)) {
3770
+ if (isIpAddress2(this.rpId)) {
2867
3771
  throw new Error(
2868
3772
  `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.)`
2869
3773
  );
@@ -2883,10 +3787,10 @@ var PasskeySigner = class {
2883
3787
  const challenge = crypto.getRandomValues(new Uint8Array(32));
2884
3788
  const cred = await navigator.credentials.create({
2885
3789
  publicKey: {
2886
- challenge: buf(challenge),
3790
+ challenge: buf2(challenge),
2887
3791
  rp: { id: this.rpId, name: this.rpName },
2888
3792
  user: {
2889
- id: buf(userHandle(params.userId)),
3793
+ id: buf2(userHandle2(params.userId)),
2890
3794
  name: params.userName,
2891
3795
  displayName: params.displayName ?? params.userName
2892
3796
  },
@@ -2913,7 +3817,7 @@ var PasskeySigner = class {
2913
3817
  async assert(challenge) {
2914
3818
  const cred = await navigator.credentials.get({
2915
3819
  publicKey: {
2916
- challenge: buf(challenge),
3820
+ challenge: buf2(challenge),
2917
3821
  rpId: this.rpId,
2918
3822
  allowCredentials: [],
2919
3823
  userVerification: "preferred"
@@ -2928,19 +3832,24 @@ var PasskeySigner = class {
2928
3832
  return { authenticatorData, clientDataJSON, r, s, challengeOffset };
2929
3833
  }
2930
3834
  };
2931
- function isIpAddress(host) {
3835
+ function isIpAddress2(host) {
2932
3836
  if (/^\d{1,3}(\.\d{1,3}){3}$/.test(host)) return true;
2933
3837
  if (host.includes(":")) return true;
2934
3838
  return false;
2935
3839
  }
2936
- function userHandle(userId) {
3840
+ function userHandle2(userId) {
2937
3841
  const bytes = new TextEncoder().encode(userId);
2938
3842
  return bytes.length <= 64 ? bytes : sha256(bytes);
2939
3843
  }
2940
- function buf(bytes) {
3844
+ function buf2(bytes) {
2941
3845
  return bytes.slice();
2942
3846
  }
3847
+ /*! Bundled license information:
3848
+
3849
+ @noble/ciphers/esm/utils.js:
3850
+ (*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) *)
3851
+ */
2943
3852
 
2944
- export { BackupSigner, Cavos, CavosAuth, CavosSolana, CavosStellar, DEVICE_ACCOUNT_CLASS_HASH, DEVICE_ACCOUNT_PROGRAM_ID, DEVICE_ACCOUNT_WASM_HASH, FACTORY_CONTRACT_ID, HttpRecoveryClient, HttpWalletRegistry, InMemoryWalletRegistry, NATIVE_SAC_ID, PasskeySigner, SECP256R1_PROGRAM_ID, SOLANA_NETWORKS, STARKNET_NETWORKS, STELLAR_NETWORKS, SolanaAdapter, SolanaRelayer, StarknetAdapter, StarknetDeviceSigner, StellarAdapter, StellarRelayer, UDC_ADDRESS, WebCryptoSigner, anchorDiscriminator, approveDeviceEverywhere, base64urlEncode, batchChallenge, bigIntTo32Bytes, buildSecp256r1Instruction, bytesToBigInt, bytesToHex, compressedPubkey, deriveAddressSeed, deriveAddressSeedSolana, deriveAddressSeedStellar, deriveBackupKey, deviceSignatureScVal, encodeLowSSignature, encodeLowSSignature2, generateRecoveryCode, hexToBytes, lowS, recoverCandidatePublicKeys, recoverYParity, sec1Pubkey, serializeInstructions, signatureToFelts, u256ToFelts, webauthnDigest };
2945
- //# sourceMappingURL=chunk-F2J25XSL.mjs.map
2946
- //# sourceMappingURL=chunk-F2J25XSL.mjs.map
3853
+ export { BackupSigner, Cavos, CavosAuth, CavosSolana, CavosStellar, DEVICE_ACCOUNT_CLASS_HASH, DEVICE_ACCOUNT_PROGRAM_ID, HORIZON_URL, HttpRecoveryClient, HttpWalletRegistry, InMemoryWalletRegistry, LocalDeviceUnwrapKey, PasskeyPrf, PasskeySigner, SECP256R1_PROGRAM_ID, SOLANA_NETWORKS, STARKNET_NETWORKS, STELLAR_NETWORKS, SolanaAdapter, SolanaRelayer, StarknetAdapter, StarknetDeviceSigner, StellarAdapter, StellarRelayer, UDC_ADDRESS, WebCryptoDeviceUnwrapKey, WebCryptoSigner, XLM_DECIMALS, anchorDiscriminator, approveDeviceEverywhere, base64urlEncode, batchChallenge, bigIntTo32Bytes, buildSecp256r1Instruction, bytesToBigInt, bytesToHex, compressedPubkey, deriveAddressSeed, deriveAddressSeedSolana, deriveAddressSeedStellar, deriveBackupKey, derivePasskeyKEK, deriveRecoveryKEK, deriveStellarAddress, deriveStellarMasterKeypair, deviceSlotId, eciesUnwrapDEK, eciesWrapDEK, encodeLowSSignature, fromDataEntries, generateControlKey, generateDEK, generateRecoveryCode, hexToBytes, lowS, openControlSeed, recoverCandidatePublicKeys, recoverYParity, sealControlSeed, serializeInstructions, signatureToFelts, toDataEntries, u256ToFelts, unwrapDEK, webauthnDigest, wrapDEK };
3854
+ //# sourceMappingURL=chunk-M5BGBODC.mjs.map
3855
+ //# sourceMappingURL=chunk-M5BGBODC.mjs.map