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