@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/index.mjs CHANGED
@@ -438,6 +438,61 @@ function useChipiSession(config) {
438
438
  setError(err);
439
439
  }
440
440
  });
441
+ const setSpendingPolicyMutation = useMutation({
442
+ mutationFn: async (policyConfig) => {
443
+ if (!wallet) throw new Error("Wallet is required");
444
+ if (!localSession)
445
+ throw new Error("No session to set policy on. Create and register a session first.");
446
+ if (!encryptKey) throw new Error("Encryption key (PIN) is required");
447
+ if (!supportsSession) throw new Error("Wallet does not support sessions");
448
+ const bearerToken = await getBearerToken();
449
+ if (!bearerToken) throw new Error("Bearer token is required");
450
+ const txHash = await chipiSDK.sessions.setSpendingPolicy(
451
+ {
452
+ encryptKey,
453
+ wallet,
454
+ spendingPolicyConfig: {
455
+ sessionPublicKey: localSession.publicKey,
456
+ ...policyConfig
457
+ }
458
+ },
459
+ bearerToken
460
+ );
461
+ return txHash;
462
+ },
463
+ onSuccess: () => {
464
+ setError(null);
465
+ },
466
+ onError: (err) => {
467
+ setError(err);
468
+ }
469
+ });
470
+ const removeSpendingPolicyMutation = useMutation({
471
+ mutationFn: async (token) => {
472
+ if (!wallet) throw new Error("Wallet is required");
473
+ if (!localSession) throw new Error("No session to remove policy from");
474
+ if (!encryptKey) throw new Error("Encryption key (PIN) is required");
475
+ if (!supportsSession) throw new Error("Wallet does not support sessions");
476
+ const bearerToken = await getBearerToken();
477
+ if (!bearerToken) throw new Error("Bearer token is required");
478
+ const txHash = await chipiSDK.sessions.removeSpendingPolicy(
479
+ {
480
+ encryptKey,
481
+ wallet,
482
+ sessionPublicKey: localSession.publicKey,
483
+ token
484
+ },
485
+ bearerToken
486
+ );
487
+ return txHash;
488
+ },
489
+ onSuccess: () => {
490
+ setError(null);
491
+ },
492
+ onError: (err) => {
493
+ setError(err);
494
+ }
495
+ });
441
496
  const createSession = useCallback(
442
497
  async (params) => {
443
498
  return createSessionMutation.mutateAsync(params ?? {});
@@ -466,14 +521,40 @@ function useChipiSession(config) {
466
521
  const refetchStatus = useCallback(async () => {
467
522
  await sessionStatusQuery.refetch();
468
523
  }, [sessionStatusQuery]);
524
+ const setSpendingPolicy = useCallback(
525
+ async (policyConfig) => {
526
+ return setSpendingPolicyMutation.mutateAsync(policyConfig);
527
+ },
528
+ [setSpendingPolicyMutation]
529
+ );
530
+ const getSpendingPolicy = useCallback(
531
+ async (token) => {
532
+ if (!wallet?.publicKey) throw new Error("Wallet is required");
533
+ if (!localSession?.publicKey) throw new Error("No session to query policy for");
534
+ return chipiSDK.sessions.getSpendingPolicy({
535
+ walletAddress: wallet.publicKey,
536
+ sessionPublicKey: localSession.publicKey,
537
+ token
538
+ });
539
+ },
540
+ [chipiSDK, wallet, localSession]
541
+ );
542
+ const removeSpendingPolicy = useCallback(
543
+ async (token) => {
544
+ return removeSpendingPolicyMutation.mutateAsync(token);
545
+ },
546
+ [removeSpendingPolicyMutation]
547
+ );
469
548
  const combinedError = useMemo(() => {
470
- return error || createSessionMutation.error || registerSessionMutation.error || revokeSessionMutation.error || executeWithSessionMutation.error || sessionStatusQuery.error || null;
549
+ return error || createSessionMutation.error || registerSessionMutation.error || revokeSessionMutation.error || executeWithSessionMutation.error || setSpendingPolicyMutation.error || removeSpendingPolicyMutation.error || sessionStatusQuery.error || null;
471
550
  }, [
472
551
  error,
473
552
  createSessionMutation.error,
474
553
  registerSessionMutation.error,
475
554
  revokeSessionMutation.error,
476
555
  executeWithSessionMutation.error,
556
+ setSpendingPolicyMutation.error,
557
+ removeSpendingPolicyMutation.error,
477
558
  sessionStatusQuery.error
478
559
  ]);
479
560
  return {
@@ -492,18 +573,25 @@ function useChipiSession(config) {
492
573
  executeWithSession,
493
574
  clearSession,
494
575
  refetchStatus,
576
+ // Spending Policy Actions
577
+ setSpendingPolicy,
578
+ getSpendingPolicy,
579
+ removeSpendingPolicy,
495
580
  // Loading states
496
581
  isCreating: createSessionMutation.isPending,
497
582
  isRegistering: registerSessionMutation.isPending,
498
583
  isRevoking: revokeSessionMutation.isPending,
499
584
  isExecuting: executeWithSessionMutation.isPending,
500
585
  isLoadingStatus: sessionStatusQuery.isLoading,
586
+ isSettingSpendingPolicy: setSpendingPolicyMutation.isPending,
587
+ isRemovingSpendingPolicy: removeSpendingPolicyMutation.isPending,
501
588
  // Errors
502
589
  error: combinedError
503
590
  };
504
591
  }
505
592
  function useMigrateWalletToPasskey() {
506
593
  const { chipiSDK } = useChipiContext();
594
+ const queryClient = useQueryClient();
507
595
  const mutation = useMutation({
508
596
  mutationFn: async (input) => {
509
597
  const { wallet, oldEncryptKey, externalUserId, bearerToken } = input;
@@ -525,10 +613,27 @@ function useMigrateWalletToPasskey() {
525
613
  decryptedPrivateKey,
526
614
  passkeyResult.encryptKey
527
615
  );
616
+ const updateResult = await chipiSDK.wallets.updateWalletEncryption(
617
+ {
618
+ externalUserId,
619
+ newEncryptedPrivateKey,
620
+ publicKey: wallet.publicKey
621
+ },
622
+ bearerToken
623
+ );
624
+ if (updateResult?.success === false) {
625
+ throw new Error("Backend rejected wallet encryption update");
626
+ }
528
627
  const updatedWallet = {
529
628
  ...wallet,
530
629
  encryptedPrivateKey: newEncryptedPrivateKey
531
630
  };
631
+ queryClient.invalidateQueries({
632
+ queryKey: ["chipi-wallet", externalUserId]
633
+ });
634
+ queryClient.invalidateQueries({
635
+ queryKey: ["wallet", externalUserId]
636
+ });
532
637
  return {
533
638
  success: true,
534
639
  wallet: updatedWallet,