@chipi-stack/chipi-react 14.15.1 → 14.17.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/hooks.mjs CHANGED
@@ -1268,6 +1268,22 @@ function useSignTypedData() {
1268
1268
  reset: mutation.reset
1269
1269
  };
1270
1270
  }
1271
+ function useExternalSignerExecute() {
1272
+ const { chipiSDK } = useChipiContext();
1273
+ const mutation = useMutation({
1274
+ mutationFn: async (input) => chipiSDK.executeWithExternalSigner(input)
1275
+ });
1276
+ return {
1277
+ execute: mutation.mutate,
1278
+ executeAsync: mutation.mutateAsync,
1279
+ data: mutation.data,
1280
+ isLoading: mutation.isPending,
1281
+ isError: mutation.isError,
1282
+ error: mutation.error,
1283
+ isSuccess: mutation.isSuccess,
1284
+ reset: mutation.reset
1285
+ };
1286
+ }
1271
1287
 
1272
1288
  // ../node_modules/.pnpm/@starknet-io+types-js@0.9.2/node_modules/@starknet-io/types-js/dist/esm/index.js
1273
1289
  var esm_exports = {};
@@ -20346,6 +20362,41 @@ function hexEq(a, b) {
20346
20362
  return a === b;
20347
20363
  }
20348
20364
  }
20365
+ function parseCapAmount(value, field, token) {
20366
+ try {
20367
+ const v = typeof value === "bigint" ? value : BigInt(String(value).trim());
20368
+ if (v < 0n) throw new Error("negative");
20369
+ return v;
20370
+ } catch {
20371
+ throw new Error(
20372
+ `session_spend_cap_invalid: ${field} for token ${token} must be a non-negative integer in base units (got "${String(value)}")`
20373
+ );
20374
+ }
20375
+ }
20376
+ function toSpendingPolicies(caps, lifetimeSeconds) {
20377
+ return caps.map((cap) => {
20378
+ const policy = "maxPerWindow" in cap ? {
20379
+ token: cap.token,
20380
+ maxPerCall: parseCapAmount(cap.maxPerCall, "maxPerCall", cap.token),
20381
+ maxPerWindow: parseCapAmount(cap.maxPerWindow, "maxPerWindow", cap.token),
20382
+ windowSeconds: cap.windowSeconds
20383
+ } : (() => {
20384
+ const amount = parseCapAmount(cap.amount, "amount", cap.token);
20385
+ return {
20386
+ token: cap.token,
20387
+ maxPerCall: amount,
20388
+ maxPerWindow: amount,
20389
+ windowSeconds: Math.max(1, lifetimeSeconds)
20390
+ };
20391
+ })();
20392
+ if (policy.maxPerWindow === 0n) {
20393
+ throw new Error(
20394
+ `session_spend_cap_zero: the cap for token ${cap.token} resolves to maxPerWindow = 0, which means unlimited on-chain. Use a positive cap.`
20395
+ );
20396
+ }
20397
+ return policy;
20398
+ });
20399
+ }
20349
20400
  function sessionCovers(policy, calls) {
20350
20401
  if (!policy?.allowedCalls?.length) return false;
20351
20402
  return calls.every(
@@ -20360,6 +20411,10 @@ function useChipiWalletObject(config3) {
20360
20411
  const { signTypedDataAsync } = useSignTypedData();
20361
20412
  const { createSessionKeyAsync } = useCreateSessionKey();
20362
20413
  const { addSessionKeyToContractAsync } = useAddSessionKeyToContract();
20414
+ const { chipiSDK } = useChipiContext();
20415
+ const sdk = useRef(chipiSDK);
20416
+ sdk.current = chipiSDK;
20417
+ const warnedMissingApproval = useRef(false);
20363
20418
  const cfg = useRef(config3);
20364
20419
  cfg.current = config3;
20365
20420
  const requireWallet = () => {
@@ -20372,28 +20427,33 @@ function useChipiWalletObject(config3) {
20372
20427
  if (policy.validUntil <= nowSec()) throw new Error("session_policy_expired");
20373
20428
  const c = cfg.current;
20374
20429
  const wallet = requireWallet();
20430
+ const lifetimeSeconds = Math.max(1, policy.validUntil - nowSec());
20431
+ const spendingPolicies = policy.spendCaps?.length ? toSpendingPolicies(policy.spendCaps, lifetimeSeconds) : [];
20375
20432
  const encryptKey = await c.getEncryptKey();
20376
20433
  const session = await createSessionKeyAsync({
20377
20434
  encryptKey,
20378
- durationSeconds: Math.max(1, policy.validUntil - nowSec())
20435
+ durationSeconds: lifetimeSeconds
20379
20436
  });
20380
20437
  const bearerToken = await c.getBearerToken();
20381
- await addSessionKeyToContractAsync({
20382
- params: {
20383
- encryptKey,
20384
- wallet,
20385
- sessionConfig: {
20386
- sessionPublicKey: session.publicKey,
20387
- validUntil: policy.validUntil,
20388
- maxCalls: 1e3,
20389
- // selectors; empty = all entrypoints allowed
20390
- allowedEntrypoints: policy.allowedCalls.map(
20391
- (a) => hash_exports.getSelectorFromName(a.entrypoint)
20392
- )
20393
- }
20394
- },
20395
- bearerToken
20396
- });
20438
+ const params = {
20439
+ encryptKey,
20440
+ wallet,
20441
+ sessionConfig: {
20442
+ sessionPublicKey: session.publicKey,
20443
+ validUntil: policy.validUntil,
20444
+ maxCalls: 1e3,
20445
+ // Selectors; empty = all entrypoints allowed. On-chain this is a
20446
+ // per-selector whitelist, NOT scoped to allowedCalls' contracts.
20447
+ allowedEntrypoints: policy.allowedCalls.map(
20448
+ (a) => hash_exports.getSelectorFromName(a.entrypoint)
20449
+ )
20450
+ }
20451
+ };
20452
+ if (spendingPolicies.length > 0) {
20453
+ await sdk.current.sessions.setupSession({ ...params, spendingPolicies }, bearerToken);
20454
+ } else {
20455
+ await addSessionKeyToContractAsync({ params, bearerToken });
20456
+ }
20397
20457
  return session;
20398
20458
  },
20399
20459
  [createSessionKeyAsync, addSessionKeyToContractAsync]
@@ -20433,12 +20493,23 @@ function useChipiWalletObject(config3) {
20433
20493
  return signTypedDataAsync({ encryptKey, wallet, typedData });
20434
20494
  },
20435
20495
  getDeploymentData: async () => {
20436
- throw new Error("account_already_deployed");
20496
+ if (cfg.current.isDeployed === true) throw new Error("account_already_deployed");
20497
+ throw new Error("account_deployment_data_unavailable");
20437
20498
  },
20438
20499
  createSession: async (policy) => {
20439
20500
  await createSession(policy);
20440
20501
  },
20441
- requestApproval: (req) => cfg.current.requestApproval?.(req) ?? Promise.resolve(true)
20502
+ requestApproval: (req) => {
20503
+ const gate = cfg.current.requestApproval;
20504
+ if (gate) return gate(req);
20505
+ if (!warnedMissingApproval.current) {
20506
+ warnedMissingApproval.current = true;
20507
+ console.warn(
20508
+ "[ChipiSDK:Connect] No `requestApproval` was provided to useChipiWalletObject, so every approval (connect, execute, signTypedData, session) is denied. Pass `requestApproval` to show the user an approval UI and resolve true/false."
20509
+ );
20510
+ }
20511
+ return Promise.resolve(false);
20512
+ }
20442
20513
  }),
20443
20514
  [callAnyContractAsync, executeWithSessionAsync, signTypedDataAsync, createSession]
20444
20515
  );
@@ -21119,6 +21190,6 @@ pako/dist/pako.esm.mjs:
21119
21190
  (*! pako 2.1.0 https://github.com/nodeca/pako @license (MIT AND Zlib) *)
21120
21191
  */
21121
21192
 
21122
- export { derivePrivacyAccount, netPrivateBalance, useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiSession, useChipiWallet, useChipiWalletObject, useCreateSessionKey, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuPurchase, useGetTokenBalance, useGetTransaction, useGetTransactionList, useGetTransactionStatus, useGetWallet, useGuardianRecovery, useMigrateWalletToPasskey, useMigrateWalletToShhh, usePrivateBalance, usePurchaseSku, useRecordSendTransaction, useRevokeSessionKey, useShhhSigner, useShield, useSignTypedData, useSyncOnChainTransfers, useThresholdSign, useTransfer, useUnshield, useUpdateWalletEncryption, useX402Payment };
21193
+ export { derivePrivacyAccount, netPrivateBalance, useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiSession, useChipiWallet, useChipiWalletObject, useCreateSessionKey, useCreateWallet, useExecuteWithSession, useExternalSignerExecute, useGetSessionData, useGetSku, useGetSkuList, useGetSkuPurchase, useGetTokenBalance, useGetTransaction, useGetTransactionList, useGetTransactionStatus, useGetWallet, useGuardianRecovery, useMigrateWalletToPasskey, useMigrateWalletToShhh, usePrivateBalance, usePurchaseSku, useRecordSendTransaction, useRevokeSessionKey, useShhhSigner, useShield, useSignTypedData, useSyncOnChainTransfers, useThresholdSign, useTransfer, useUnshield, useUpdateWalletEncryption, useX402Payment };
21123
21194
  //# sourceMappingURL=hooks.mjs.map
21124
21195
  //# sourceMappingURL=hooks.mjs.map