@chipi-stack/chipi-react 14.1.1 → 14.2.1

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
@@ -400,6 +400,61 @@ function useChipiSession(config) {
400
400
  setError(err);
401
401
  }
402
402
  });
403
+ const setSpendingPolicyMutation = useMutation({
404
+ mutationFn: async (policyConfig) => {
405
+ if (!wallet) throw new Error("Wallet is required");
406
+ if (!localSession)
407
+ throw new Error("No session to set policy on. Create and register a session first.");
408
+ if (!encryptKey) throw new Error("Encryption key (PIN) is required");
409
+ if (!supportsSession) throw new Error("Wallet does not support sessions");
410
+ const bearerToken = await getBearerToken();
411
+ if (!bearerToken) throw new Error("Bearer token is required");
412
+ const txHash = await chipiSDK.sessions.setSpendingPolicy(
413
+ {
414
+ encryptKey,
415
+ wallet,
416
+ spendingPolicyConfig: {
417
+ sessionPublicKey: localSession.publicKey,
418
+ ...policyConfig
419
+ }
420
+ },
421
+ bearerToken
422
+ );
423
+ return txHash;
424
+ },
425
+ onSuccess: () => {
426
+ setError(null);
427
+ },
428
+ onError: (err) => {
429
+ setError(err);
430
+ }
431
+ });
432
+ const removeSpendingPolicyMutation = useMutation({
433
+ mutationFn: async (token) => {
434
+ if (!wallet) throw new Error("Wallet is required");
435
+ if (!localSession) throw new Error("No session to remove policy from");
436
+ if (!encryptKey) throw new Error("Encryption key (PIN) is required");
437
+ if (!supportsSession) throw new Error("Wallet does not support sessions");
438
+ const bearerToken = await getBearerToken();
439
+ if (!bearerToken) throw new Error("Bearer token is required");
440
+ const txHash = await chipiSDK.sessions.removeSpendingPolicy(
441
+ {
442
+ encryptKey,
443
+ wallet,
444
+ sessionPublicKey: localSession.publicKey,
445
+ token
446
+ },
447
+ bearerToken
448
+ );
449
+ return txHash;
450
+ },
451
+ onSuccess: () => {
452
+ setError(null);
453
+ },
454
+ onError: (err) => {
455
+ setError(err);
456
+ }
457
+ });
403
458
  const createSession = useCallback(
404
459
  async (params) => {
405
460
  return createSessionMutation.mutateAsync(params ?? {});
@@ -428,14 +483,40 @@ function useChipiSession(config) {
428
483
  const refetchStatus = useCallback(async () => {
429
484
  await sessionStatusQuery.refetch();
430
485
  }, [sessionStatusQuery]);
486
+ const setSpendingPolicy = useCallback(
487
+ async (policyConfig) => {
488
+ return setSpendingPolicyMutation.mutateAsync(policyConfig);
489
+ },
490
+ [setSpendingPolicyMutation]
491
+ );
492
+ const getSpendingPolicy = useCallback(
493
+ async (token) => {
494
+ if (!wallet?.publicKey) throw new Error("Wallet is required");
495
+ if (!localSession?.publicKey) throw new Error("No session to query policy for");
496
+ return chipiSDK.sessions.getSpendingPolicy({
497
+ walletAddress: wallet.publicKey,
498
+ sessionPublicKey: localSession.publicKey,
499
+ token
500
+ });
501
+ },
502
+ [chipiSDK, wallet, localSession]
503
+ );
504
+ const removeSpendingPolicy = useCallback(
505
+ async (token) => {
506
+ return removeSpendingPolicyMutation.mutateAsync(token);
507
+ },
508
+ [removeSpendingPolicyMutation]
509
+ );
431
510
  const combinedError = useMemo(() => {
432
- return error || createSessionMutation.error || registerSessionMutation.error || revokeSessionMutation.error || executeWithSessionMutation.error || sessionStatusQuery.error || null;
511
+ return error || createSessionMutation.error || registerSessionMutation.error || revokeSessionMutation.error || executeWithSessionMutation.error || setSpendingPolicyMutation.error || removeSpendingPolicyMutation.error || sessionStatusQuery.error || null;
433
512
  }, [
434
513
  error,
435
514
  createSessionMutation.error,
436
515
  registerSessionMutation.error,
437
516
  revokeSessionMutation.error,
438
517
  executeWithSessionMutation.error,
518
+ setSpendingPolicyMutation.error,
519
+ removeSpendingPolicyMutation.error,
439
520
  sessionStatusQuery.error
440
521
  ]);
441
522
  return {
@@ -454,18 +535,25 @@ function useChipiSession(config) {
454
535
  executeWithSession,
455
536
  clearSession,
456
537
  refetchStatus,
538
+ // Spending Policy Actions
539
+ setSpendingPolicy,
540
+ getSpendingPolicy,
541
+ removeSpendingPolicy,
457
542
  // Loading states
458
543
  isCreating: createSessionMutation.isPending,
459
544
  isRegistering: registerSessionMutation.isPending,
460
545
  isRevoking: revokeSessionMutation.isPending,
461
546
  isExecuting: executeWithSessionMutation.isPending,
462
547
  isLoadingStatus: sessionStatusQuery.isLoading,
548
+ isSettingSpendingPolicy: setSpendingPolicyMutation.isPending,
549
+ isRemovingSpendingPolicy: removeSpendingPolicyMutation.isPending,
463
550
  // Errors
464
551
  error: combinedError
465
552
  };
466
553
  }
467
554
  function useMigrateWalletToPasskey() {
468
555
  const { chipiSDK } = useChipiContext();
556
+ const queryClient = useQueryClient();
469
557
  const mutation = useMutation({
470
558
  mutationFn: async (input) => {
471
559
  const { wallet, oldEncryptKey, externalUserId, bearerToken } = input;
@@ -487,10 +575,27 @@ function useMigrateWalletToPasskey() {
487
575
  decryptedPrivateKey,
488
576
  passkeyResult.encryptKey
489
577
  );
578
+ const updateResult = await chipiSDK.wallets.updateWalletEncryption(
579
+ {
580
+ externalUserId,
581
+ newEncryptedPrivateKey,
582
+ publicKey: wallet.publicKey
583
+ },
584
+ bearerToken
585
+ );
586
+ if (updateResult?.success === false) {
587
+ throw new Error("Backend rejected wallet encryption update");
588
+ }
490
589
  const updatedWallet = {
491
590
  ...wallet,
492
591
  encryptedPrivateKey: newEncryptedPrivateKey
493
592
  };
593
+ queryClient.invalidateQueries({
594
+ queryKey: ["chipi-wallet", externalUserId]
595
+ });
596
+ queryClient.invalidateQueries({
597
+ queryKey: ["wallet", externalUserId]
598
+ });
494
599
  return {
495
600
  success: true,
496
601
  wallet: updatedWallet,