@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/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
  });
@@ -683,31 +712,40 @@ function useUpdateWalletEncryption() {
683
712
  reset: mutation.reset
684
713
  };
685
714
  }
686
- function useTransfer() {
715
+ function useTransfer(config) {
687
716
  const { chipiSDK, passkeyAdapter } = useChipiContext();
688
717
  const mutation = useMutation({
689
718
  mutationFn: async (input) => {
690
719
  let encryptKey = input.params.encryptKey;
720
+ let walletForTransfer = input.params.wallet;
691
721
  if (input.params.usePasskey) {
692
722
  if (!input.params.externalUserId) {
693
723
  throw new Error("externalUserId is required when usePasskey is true");
694
724
  }
695
- try {
696
- const key = await passkeyAdapter.getWalletEncryptKey({
697
- externalUserId: input.params.externalUserId
698
- });
699
- if (!key) {
700
- throw new Error("Passkey authentication was cancelled");
701
- }
702
- encryptKey = key;
703
- } catch (error) {
704
- if (error instanceof DOMException && error.name === "NotAllowedError") {
705
- throw new Error("Passkey authentication was cancelled by the user");
706
- }
707
- if (error instanceof Error) {
708
- 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.");
709
748
  }
710
- throw new Error("Failed to authenticate with passkey");
711
749
  }
712
750
  }
713
751
  if (!encryptKey) {
@@ -721,6 +759,7 @@ function useTransfer() {
721
759
  return chipiSDK.transfer({
722
760
  params: {
723
761
  ...transferParams,
762
+ wallet: walletForTransfer,
724
763
  amount: String(transferParams.amount),
725
764
  encryptKey
726
765
  },
@@ -943,6 +982,31 @@ function useGetTransactionStatus(input) {
943
982
  fetchTransactionStatus
944
983
  };
945
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
+ }
946
1010
  function usePurchaseSku() {
947
1011
  const { chipiSDK } = useChipiContext();
948
1012
  const mutation = useMutation({
@@ -1534,6 +1598,6 @@ function useX402Payment(config) {
1534
1598
  };
1535
1599
  }
1536
1600
 
1537
- 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 };
1538
1602
  //# sourceMappingURL=index.mjs.map
1539
1603
  //# sourceMappingURL=index.mjs.map