@chipi-stack/chipi-react 14.2.0 → 14.3.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.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import React, { createContext, useContext, useMemo, useCallback, useState, useEffect, useRef } from 'react';
2
2
  import { QueryClient, QueryClientProvider, useMutation, useQueryClient, useQuery } from '@tanstack/react-query';
3
3
  import { ChipiSDK, decryptPrivateKey, encryptPrivateKey } from '@chipi-stack/backend';
4
- import { getWalletEncryptKey, createWalletPasskey } from '@chipi-stack/chipi-passkey';
4
+ import { createWalletPasskey, getWalletEncryptKey } from '@chipi-stack/chipi-passkey';
5
5
  import { jsx } from 'react/jsx-runtime';
6
6
  import { ChipiApiError } from '@chipi-stack/shared';
7
7
  import { OnChainTxStatus, Chain, ChainToken } from '@chipi-stack/types';
@@ -11,8 +11,18 @@ import { buildX402TypedData } from '@chipi-stack/core';
11
11
  // src/context/ChipiProvider.tsx
12
12
  var ChipiContext = createContext(null);
13
13
  var defaultPasskeyAdapter = {
14
- // Web default: externalUserId is accepted for cross-platform API consistency
15
- getWalletEncryptKey: async () => getWalletEncryptKey()
14
+ // Web default: attempts passkey auth, returns null on failure (caller uses PIN backup)
15
+ getWalletEncryptKey: async () => {
16
+ try {
17
+ return await getWalletEncryptKey();
18
+ } catch (error) {
19
+ console.warn(
20
+ "[ChipiProvider] Passkey auth failed, falling back to PIN:",
21
+ error instanceof Error ? error.message : error
22
+ );
23
+ return null;
24
+ }
25
+ }
16
26
  };
17
27
  var createDefaultQueryClient = () => new QueryClient({
18
28
  defaultOptions: {
@@ -59,6 +69,10 @@ function useCreateWallet() {
59
69
  const mutation = useMutation({
60
70
  mutationFn: async (input) => {
61
71
  let encryptKey = input.params.encryptKey;
72
+ let encryptKeyBackup;
73
+ let credentialId;
74
+ let prfSupported;
75
+ let authMethod;
62
76
  if (input.params.usePasskey) {
63
77
  if (!input.params.externalUserId) {
64
78
  throw new Error("externalUserId is required when using passkey");
@@ -67,15 +81,25 @@ function useCreateWallet() {
67
81
  const passkeyResult = await createWalletPasskey(
68
82
  input.params.externalUserId,
69
83
  input.params.externalUserId
70
- // Using externalUserId as userName
71
84
  );
72
- encryptKey = passkeyResult.encryptKey;
85
+ credentialId = passkeyResult.credentialId;
86
+ prfSupported = passkeyResult.prfSupported;
87
+ if (encryptKey) {
88
+ encryptKeyBackup = encryptKey;
89
+ encryptKey = passkeyResult.encryptKey;
90
+ authMethod = "passkey+pin";
91
+ } else {
92
+ encryptKey = passkeyResult.encryptKey;
93
+ authMethod = "passkey";
94
+ }
73
95
  } catch (error) {
74
96
  if (error instanceof Error) {
75
97
  throw new Error(`Passkey creation failed: ${error.message}`);
76
98
  }
77
99
  throw new Error("Failed to create passkey for wallet");
78
100
  }
101
+ } else {
102
+ authMethod = encryptKey ? "pin" : void 0;
79
103
  }
80
104
  if (!encryptKey) {
81
105
  throw new Error("encryptKey is required when usePasskey is false");
@@ -83,7 +107,12 @@ function useCreateWallet() {
83
107
  return chipiSDK.createWallet({
84
108
  params: {
85
109
  ...input.params,
86
- encryptKey
110
+ encryptKey,
111
+ encryptKeyBackup,
112
+ authMethod,
113
+ credentialId,
114
+ prfSupported,
115
+ deviceType: "web"
87
116
  },
88
117
  bearerToken: input.bearerToken
89
118
  });
@@ -591,6 +620,7 @@ function useChipiSession(config) {
591
620
  }
592
621
  function useMigrateWalletToPasskey() {
593
622
  const { chipiSDK } = useChipiContext();
623
+ const queryClient = useQueryClient();
594
624
  const mutation = useMutation({
595
625
  mutationFn: async (input) => {
596
626
  const { wallet, oldEncryptKey, externalUserId, bearerToken } = input;
@@ -612,10 +642,27 @@ function useMigrateWalletToPasskey() {
612
642
  decryptedPrivateKey,
613
643
  passkeyResult.encryptKey
614
644
  );
645
+ const updateResult = await chipiSDK.wallets.updateWalletEncryption(
646
+ {
647
+ externalUserId,
648
+ newEncryptedPrivateKey,
649
+ publicKey: wallet.publicKey
650
+ },
651
+ bearerToken
652
+ );
653
+ if (updateResult?.success === false) {
654
+ throw new Error("Backend rejected wallet encryption update");
655
+ }
615
656
  const updatedWallet = {
616
657
  ...wallet,
617
658
  encryptedPrivateKey: newEncryptedPrivateKey
618
659
  };
660
+ queryClient.invalidateQueries({
661
+ queryKey: ["chipi-wallet", externalUserId]
662
+ });
663
+ queryClient.invalidateQueries({
664
+ queryKey: ["wallet", externalUserId]
665
+ });
619
666
  return {
620
667
  success: true,
621
668
  wallet: updatedWallet,
@@ -665,31 +712,40 @@ function useUpdateWalletEncryption() {
665
712
  reset: mutation.reset
666
713
  };
667
714
  }
668
- function useTransfer() {
715
+ function useTransfer(config) {
669
716
  const { chipiSDK, passkeyAdapter } = useChipiContext();
670
717
  const mutation = useMutation({
671
718
  mutationFn: async (input) => {
672
719
  let encryptKey = input.params.encryptKey;
720
+ let walletForTransfer = input.params.wallet;
673
721
  if (input.params.usePasskey) {
674
722
  if (!input.params.externalUserId) {
675
723
  throw new Error("externalUserId is required when usePasskey is true");
676
724
  }
677
- try {
678
- const key = await passkeyAdapter.getWalletEncryptKey({
679
- externalUserId: input.params.externalUserId
680
- });
681
- if (!key) {
682
- throw new Error("Passkey authentication was cancelled");
683
- }
684
- encryptKey = key;
685
- } catch (error) {
686
- if (error instanceof DOMException && error.name === "NotAllowedError") {
687
- throw new Error("Passkey authentication was cancelled by the user");
688
- }
689
- if (error instanceof Error) {
690
- throw error;
725
+ const passkeyKey = await passkeyAdapter.getWalletEncryptKey({
726
+ externalUserId: input.params.externalUserId
727
+ });
728
+ if (passkeyKey) {
729
+ encryptKey = passkeyKey;
730
+ } else {
731
+ const wallet = input.params.wallet;
732
+ if (wallet.encryptedPrivateKeyBackup && config?.onPinRequired) {
733
+ const pin = await config.onPinRequired();
734
+ if (!pin) {
735
+ throw new Error("Transfer cancelled \u2014 PIN required as passkey fallback");
736
+ }
737
+ encryptKey = pin;
738
+ walletForTransfer = {
739
+ ...wallet,
740
+ encryptedPrivateKey: wallet.encryptedPrivateKeyBackup
741
+ };
742
+ } else if (wallet.encryptedPrivateKeyBackup) {
743
+ throw new Error(
744
+ "Passkey authentication failed. Provide an onPinRequired callback to use PIN backup."
745
+ );
746
+ } else {
747
+ throw new Error("Passkey authentication failed and no PIN backup available.");
691
748
  }
692
- throw new Error("Failed to authenticate with passkey");
693
749
  }
694
750
  }
695
751
  if (!encryptKey) {
@@ -703,6 +759,7 @@ function useTransfer() {
703
759
  return chipiSDK.transfer({
704
760
  params: {
705
761
  ...transferParams,
762
+ wallet: walletForTransfer,
706
763
  amount: String(transferParams.amount),
707
764
  encryptKey
708
765
  },
@@ -925,6 +982,31 @@ function useGetTransactionStatus(input) {
925
982
  fetchTransactionStatus
926
983
  };
927
984
  }
985
+ function useSyncOnChainTransfers() {
986
+ const { chipiSDK } = useChipiContext();
987
+ const queryClient = useQueryClient();
988
+ const mutation = useMutation({
989
+ mutationFn: async (input) => {
990
+ const { bearerToken, ...params } = input;
991
+ return chipiSDK.transactions.syncOnChainTransfers(params, bearerToken);
992
+ },
993
+ onSuccess: () => {
994
+ queryClient.invalidateQueries({
995
+ predicate: (query) => query.queryKey[0] === "transaction-list"
996
+ });
997
+ }
998
+ });
999
+ return {
1000
+ syncTransfers: mutation.mutate,
1001
+ syncTransfersAsync: mutation.mutateAsync,
1002
+ data: mutation.data,
1003
+ isLoading: mutation.isPending,
1004
+ isError: mutation.isError,
1005
+ error: mutation.error,
1006
+ isSuccess: mutation.isSuccess,
1007
+ reset: mutation.reset
1008
+ };
1009
+ }
928
1010
  function usePurchaseSku() {
929
1011
  const { chipiSDK } = useChipiContext();
930
1012
  const mutation = useMutation({
@@ -1516,6 +1598,6 @@ function useX402Payment(config) {
1516
1598
  };
1517
1599
  }
1518
1600
 
1519
- export { ChipiProvider, useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiContext, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuPurchase, useGetTokenBalance, useGetTransaction, useGetTransactionList, useGetTransactionStatus, useGetWallet, useMigrateWalletToPasskey, usePurchaseSku, useRecordSendTransaction, useRevokeSessionKey, useTransfer, useUpdateWalletEncryption, useX402Payment };
1601
+ export { ChipiProvider, useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiContext, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuPurchase, useGetTokenBalance, useGetTransaction, useGetTransactionList, useGetTransactionStatus, useGetWallet, useMigrateWalletToPasskey, usePurchaseSku, useRecordSendTransaction, useRevokeSessionKey, useSyncOnChainTransfers, useTransfer, useUpdateWalletEncryption, useX402Payment };
1520
1602
  //# sourceMappingURL=index.mjs.map
1521
1603
  //# sourceMappingURL=index.mjs.map