@chipi-stack/chipi-react 14.2.1 → 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/hooks.mjs CHANGED
@@ -21,6 +21,10 @@ function useCreateWallet() {
21
21
  const mutation = useMutation({
22
22
  mutationFn: async (input) => {
23
23
  let encryptKey = input.params.encryptKey;
24
+ let encryptKeyBackup;
25
+ let credentialId;
26
+ let prfSupported;
27
+ let authMethod;
24
28
  if (input.params.usePasskey) {
25
29
  if (!input.params.externalUserId) {
26
30
  throw new Error("externalUserId is required when using passkey");
@@ -29,15 +33,25 @@ function useCreateWallet() {
29
33
  const passkeyResult = await createWalletPasskey(
30
34
  input.params.externalUserId,
31
35
  input.params.externalUserId
32
- // Using externalUserId as userName
33
36
  );
34
- encryptKey = passkeyResult.encryptKey;
37
+ credentialId = passkeyResult.credentialId;
38
+ prfSupported = passkeyResult.prfSupported;
39
+ if (encryptKey) {
40
+ encryptKeyBackup = encryptKey;
41
+ encryptKey = passkeyResult.encryptKey;
42
+ authMethod = "passkey+pin";
43
+ } else {
44
+ encryptKey = passkeyResult.encryptKey;
45
+ authMethod = "passkey";
46
+ }
35
47
  } catch (error) {
36
48
  if (error instanceof Error) {
37
49
  throw new Error(`Passkey creation failed: ${error.message}`);
38
50
  }
39
51
  throw new Error("Failed to create passkey for wallet");
40
52
  }
53
+ } else {
54
+ authMethod = encryptKey ? "pin" : void 0;
41
55
  }
42
56
  if (!encryptKey) {
43
57
  throw new Error("encryptKey is required when usePasskey is false");
@@ -45,7 +59,12 @@ function useCreateWallet() {
45
59
  return chipiSDK.createWallet({
46
60
  params: {
47
61
  ...input.params,
48
- encryptKey
62
+ encryptKey,
63
+ encryptKeyBackup,
64
+ authMethod,
65
+ credentialId,
66
+ prfSupported,
67
+ deviceType: "web"
49
68
  },
50
69
  bearerToken: input.bearerToken
51
70
  });
@@ -645,31 +664,40 @@ function useUpdateWalletEncryption() {
645
664
  reset: mutation.reset
646
665
  };
647
666
  }
648
- function useTransfer() {
667
+ function useTransfer(config) {
649
668
  const { chipiSDK, passkeyAdapter } = useChipiContext();
650
669
  const mutation = useMutation({
651
670
  mutationFn: async (input) => {
652
671
  let encryptKey = input.params.encryptKey;
672
+ let walletForTransfer = input.params.wallet;
653
673
  if (input.params.usePasskey) {
654
674
  if (!input.params.externalUserId) {
655
675
  throw new Error("externalUserId is required when usePasskey is true");
656
676
  }
657
- try {
658
- const key = await passkeyAdapter.getWalletEncryptKey({
659
- externalUserId: input.params.externalUserId
660
- });
661
- if (!key) {
662
- throw new Error("Passkey authentication was cancelled");
663
- }
664
- encryptKey = key;
665
- } catch (error) {
666
- if (error instanceof DOMException && error.name === "NotAllowedError") {
667
- throw new Error("Passkey authentication was cancelled by the user");
668
- }
669
- if (error instanceof Error) {
670
- throw error;
677
+ const passkeyKey = await passkeyAdapter.getWalletEncryptKey({
678
+ externalUserId: input.params.externalUserId
679
+ });
680
+ if (passkeyKey) {
681
+ encryptKey = passkeyKey;
682
+ } else {
683
+ const wallet = input.params.wallet;
684
+ if (wallet.encryptedPrivateKeyBackup && config?.onPinRequired) {
685
+ const pin = await config.onPinRequired();
686
+ if (!pin) {
687
+ throw new Error("Transfer cancelled \u2014 PIN required as passkey fallback");
688
+ }
689
+ encryptKey = pin;
690
+ walletForTransfer = {
691
+ ...wallet,
692
+ encryptedPrivateKey: wallet.encryptedPrivateKeyBackup
693
+ };
694
+ } else if (wallet.encryptedPrivateKeyBackup) {
695
+ throw new Error(
696
+ "Passkey authentication failed. Provide an onPinRequired callback to use PIN backup."
697
+ );
698
+ } else {
699
+ throw new Error("Passkey authentication failed and no PIN backup available.");
671
700
  }
672
- throw new Error("Failed to authenticate with passkey");
673
701
  }
674
702
  }
675
703
  if (!encryptKey) {
@@ -683,6 +711,7 @@ function useTransfer() {
683
711
  return chipiSDK.transfer({
684
712
  params: {
685
713
  ...transferParams,
714
+ wallet: walletForTransfer,
686
715
  amount: String(transferParams.amount),
687
716
  encryptKey
688
717
  },
@@ -905,6 +934,31 @@ function useGetTransactionStatus(input) {
905
934
  fetchTransactionStatus
906
935
  };
907
936
  }
937
+ function useSyncOnChainTransfers() {
938
+ const { chipiSDK } = useChipiContext();
939
+ const queryClient = useQueryClient();
940
+ const mutation = useMutation({
941
+ mutationFn: async (input) => {
942
+ const { bearerToken, ...params } = input;
943
+ return chipiSDK.transactions.syncOnChainTransfers(params, bearerToken);
944
+ },
945
+ onSuccess: () => {
946
+ queryClient.invalidateQueries({
947
+ predicate: (query) => query.queryKey[0] === "transaction-list"
948
+ });
949
+ }
950
+ });
951
+ return {
952
+ syncTransfers: mutation.mutate,
953
+ syncTransfersAsync: mutation.mutateAsync,
954
+ data: mutation.data,
955
+ isLoading: mutation.isPending,
956
+ isError: mutation.isError,
957
+ error: mutation.error,
958
+ isSuccess: mutation.isSuccess,
959
+ reset: mutation.reset
960
+ };
961
+ }
908
962
  function usePurchaseSku() {
909
963
  const { chipiSDK } = useChipiContext();
910
964
  const mutation = useMutation({
@@ -1496,6 +1550,6 @@ function useX402Payment(config) {
1496
1550
  };
1497
1551
  }
1498
1552
 
1499
- export { useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuPurchase, useGetTokenBalance, useGetTransaction, useGetTransactionList, useGetTransactionStatus, useGetWallet, useMigrateWalletToPasskey, usePurchaseSku, useRecordSendTransaction, useRevokeSessionKey, useTransfer, useUpdateWalletEncryption, useX402Payment };
1553
+ export { useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuPurchase, useGetTokenBalance, useGetTransaction, useGetTransactionList, useGetTransactionStatus, useGetWallet, useMigrateWalletToPasskey, usePurchaseSku, useRecordSendTransaction, useRevokeSessionKey, useSyncOnChainTransfers, useTransfer, useUpdateWalletEncryption, useX402Payment };
1500
1554
  //# sourceMappingURL=hooks.mjs.map
1501
1555
  //# sourceMappingURL=hooks.mjs.map