@naculus/connect-appkit-core 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -41,6 +41,7 @@ __export(src_exports, {
41
41
  createDelegationPolicyFlow: () => createDelegationPolicyFlow,
42
42
  createNotification: () => createNotification,
43
43
  createNotificationSettingsStorage: () => createNotificationSettingsStorage,
44
+ createSolanaSessionKeyFlow: () => createSolanaSessionKeyFlow,
44
45
  decodeRevertReason: () => decodeRevertReason,
45
46
  defaultSiwxDomain: () => defaultSiwxDomain,
46
47
  defaultSiwxUri: () => defaultSiwxUri,
@@ -1419,8 +1420,88 @@ function describePayment(input, result) {
1419
1420
  };
1420
1421
  }
1421
1422
 
1422
- // src/web3-store.ts
1423
+ // src/solana-session-key.ts
1423
1424
  var import_connect_core17 = require("@naculus/connect-core");
1425
+ function send(rpc) {
1426
+ const sendTransaction = rpc.sendTransaction;
1427
+ if (!sendTransaction) {
1428
+ throw new import_connect_core17.WalletError(
1429
+ "invalid_input",
1430
+ "The Solana RPC must implement sendTransaction to approve or revoke."
1431
+ );
1432
+ }
1433
+ return (tx) => sendTransaction.call(rpc, tx);
1434
+ }
1435
+ function createSolanaSessionKeyFlow(deps) {
1436
+ const { manager, rpc, owner } = deps;
1437
+ const list = async () => (await manager.listSessions()).filter((key) => key.owner === owner.address).sort((a, b) => b.createdAt - a.createdAt);
1438
+ return {
1439
+ list,
1440
+ async create(scope) {
1441
+ const broadcast = send(rpc);
1442
+ const draft = await manager.createSessionKey(scope, owner.address, rpc);
1443
+ let approval;
1444
+ try {
1445
+ const { transaction, recentBlockhash } = await manager.prepareApproval(
1446
+ draft.id,
1447
+ rpc
1448
+ );
1449
+ const signed = await owner.signTransaction(transaction);
1450
+ approval = await manager.attachApproval(
1451
+ draft.id,
1452
+ signed,
1453
+ recentBlockhash
1454
+ );
1455
+ } catch (cause) {
1456
+ await manager.revoke(draft.id).catch(() => {
1457
+ });
1458
+ throw cause;
1459
+ }
1460
+ let signature;
1461
+ try {
1462
+ signature = await broadcast(approval);
1463
+ } catch (cause) {
1464
+ await manager.revoke(draft.id).catch(() => {
1465
+ });
1466
+ throw new import_connect_core17.WalletError(
1467
+ "rpc_error",
1468
+ `The approval could not be confirmed as sent (${cause instanceof Error ? cause.message : String(cause)}); the key is revoked here. Call revoke("${draft.id}") to send the on-chain Revoke in case it landed.`
1469
+ );
1470
+ }
1471
+ const key = (await list().catch(() => [])).find((k) => k.id === draft.id);
1472
+ return { key: key ?? { ...draft, status: "active" }, signature };
1473
+ },
1474
+ async revoke(id) {
1475
+ const key = (await list()).find((k) => k.id === id);
1476
+ if (!key) {
1477
+ throw new import_connect_core17.WalletError(
1478
+ "invalid_input",
1479
+ `No Solana session key ${id} for this owner.`
1480
+ );
1481
+ }
1482
+ await manager.revoke(id);
1483
+ const broadcast = send(rpc);
1484
+ const { transaction, recentBlockhash } = await manager.prepareRevocation(
1485
+ id,
1486
+ rpc
1487
+ );
1488
+ const signed = await owner.signTransaction(transaction);
1489
+ const revocation = (0, import_connect_core17.verifySignedOwnerTransaction)(signed, {
1490
+ kind: "revoke",
1491
+ revocation: {
1492
+ owner: key.owner,
1493
+ mint: key.scope.mint,
1494
+ tokenProgram: key.tokenProgram,
1495
+ recentBlockhash
1496
+ }
1497
+ });
1498
+ return { signature: await broadcast(revocation) };
1499
+ }
1500
+ };
1501
+ }
1502
+
1503
+ // src/web3-store.ts
1504
+ var import_connect_core18 = require("@naculus/connect-core");
1424
1505
  function withTimeout(promise, ms, label = "Operation") {
1425
1506
  let timeoutId;
1426
1507
  const timeout = new Promise((_, reject) => {
@@ -1442,7 +1523,7 @@ async function withRetry(fn, options) {
1442
1523
  lastError = error;
1443
1524
  if (attempt < options.maxRetries) {
1444
1525
  const delay = options.baseDelay * 2 ** attempt;
1445
- import_connect_core17.logger.warn(
1526
+ import_connect_core18.logger.warn(
1446
1527
  "appkit/core",
1447
1528
  `Retry attempt ${attempt + 1}/${options.maxRetries} after ${delay}ms`
1448
1529
  );
@@ -1454,18 +1535,25 @@ async function withRetry(fn, options) {
1454
1535
  }
1455
1536
  function normalizeEip155ChainId(value) {
1456
1537
  if (typeof value !== "string") return void 0;
1538
+ let raw;
1539
+ if (value.startsWith("eip155:")) {
1540
+ raw = value.slice("eip155:".length);
1541
+ if (!/^\d+$/.test(raw)) return void 0;
1542
+ } else if (/^0x[0-9a-f]+$/i.test(value) || /^\d+$/.test(value)) {
1543
+ raw = value;
1544
+ } else {
1545
+ return void 0;
1546
+ }
1547
+ let numeric;
1457
1548
  try {
1458
- if (value.startsWith("eip155:")) {
1459
- const reference = value.slice("eip155:".length);
1460
- if (!/^\d+$/.test(reference)) return void 0;
1461
- return `eip155:${BigInt(reference).toString(10)}`;
1462
- }
1463
- if (/^0x[0-9a-f]+$/i.test(value) || /^\d+$/.test(value)) {
1464
- return `eip155:${BigInt(value).toString(10)}`;
1465
- }
1549
+ numeric = BigInt(raw);
1466
1550
  } catch {
1551
+ return void 0;
1467
1552
  }
1468
- return void 0;
1553
+ if (numeric <= 0n || numeric > BigInt(Number.MAX_SAFE_INTEGER)) {
1554
+ return void 0;
1555
+ }
1556
+ return `eip155:${numeric.toString(10)}`;
1469
1557
  }
1470
1558
  function toEip155Accounts(accounts, chainId) {
1471
1559
  const reference = chainId.split(":")[1];
@@ -1570,6 +1658,7 @@ function siwxResultToSession(result, now = /* @__PURE__ */ new Date()) {
1570
1658
  createDelegationPolicyFlow,
1571
1659
  createNotification,
1572
1660
  createNotificationSettingsStorage,
1661
+ createSolanaSessionKeyFlow,
1573
1662
  decodeRevertReason,
1574
1663
  defaultSiwxDomain,
1575
1664
  defaultSiwxUri,