@chipi-stack/chipi-react 14.2.1 → 14.3.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/hooks.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { CreateWalletParams, CreateWalletResponse, GetWalletParams, GetWalletResponse, ChainToken, GetTokenBalanceResponse, WalletType, WalletData, SessionKeyData, SessionDataResponse, CreateSessionKeyParams, SessionConfig, Call, SpendingPolicyConfig, SpendingPolicyData, MigrateWalletToPasskeyParams, UpdateWalletEncryptionParams, UpdateWalletEncryptionResponse, TransferHookInput, ApproveHookInput, GetTransactionListQuery, PaginatedResponse, Transaction, TransactionStatusResponse, CreateSkuPurchaseParams, GetSkuListQuery, Sku, CallAnyContractParams, RecordSendTransactionParams, GetTokenBalanceParams, AddSessionKeyParams, RevokeSessionKeyParams, GetSessionDataParams, ExecuteWithSessionParams, PaymentRequirement, PaymentPayload } from '@chipi-stack/types';
1
+ import { CreateWalletParams, CreateWalletResponse, GetWalletParams, GetWalletResponse, ChainToken, GetTokenBalanceResponse, WalletType, WalletData, SessionKeyData, SessionDataResponse, CreateSessionKeyParams, SessionConfig, Call, SpendingPolicyConfig, SpendingPolicyData, MigrateWalletToPasskeyParams, UpdateWalletEncryptionParams, UpdateWalletEncryptionResponse, TransferHookInput, ApproveHookInput, GetTransactionListQuery, PaginatedResponse, Transaction, TransactionStatusResponse, SyncOnChainTransfersParams, SyncOnChainTransfersResponse, CreateSkuPurchaseParams, GetSkuListQuery, Sku, CallAnyContractParams, RecordSendTransactionParams, GetTokenBalanceParams, AddSessionKeyParams, RevokeSessionKeyParams, GetSessionDataParams, ExecuteWithSessionParams, PaymentRequirement, PaymentPayload } from '@chipi-stack/types';
2
2
  import * as _tanstack_react_query from '@tanstack/react-query';
3
3
  import { UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
4
4
 
@@ -7,7 +7,35 @@ type CreateWalletInput = {
7
7
  bearerToken: string;
8
8
  };
9
9
  /**
10
- * Hook for creating a new wallet
10
+ * Hook for creating a new wallet.
11
+ *
12
+ * Supports dual-key wrapping: when `usePasskey: true` AND `encryptKey` (PIN) provided,
13
+ * the private key is encrypted twice — once with the passkey-derived key (primary) and
14
+ * once with the PIN (backup). If passkey fails later, PIN recovers the wallet.
15
+ *
16
+ * @example
17
+ * ```tsx
18
+ * // Passkey + PIN (recommended)
19
+ * await createWalletAsync({
20
+ * params: {
21
+ * encryptKey: pin, // PIN backup
22
+ * externalUserId: userId,
23
+ * chain: Chain.STARKNET,
24
+ * usePasskey: true, // Creates passkey + dual encryption
25
+ * },
26
+ * bearerToken,
27
+ * });
28
+ *
29
+ * // PIN only (backward compatible)
30
+ * await createWalletAsync({
31
+ * params: {
32
+ * encryptKey: pin,
33
+ * externalUserId: userId,
34
+ * chain: Chain.STARKNET,
35
+ * },
36
+ * bearerToken,
37
+ * });
38
+ * ```
11
39
  */
12
40
  declare function useCreateWallet(): {
13
41
  createWallet: (input: CreateWalletInput) => void;
@@ -498,9 +526,36 @@ type TransferInput = {
498
526
  bearerToken: string;
499
527
  };
500
528
  /**
501
- * Hook for transferring tokens
529
+ * Configuration for useTransfer hook.
502
530
  */
503
- declare function useTransfer(): {
531
+ interface UseTransferConfig {
532
+ /**
533
+ * Callback to prompt user for PIN when passkey fails.
534
+ * Should show a PIN input dialog and return the PIN, or null to cancel.
535
+ * Only called when usePasskey: true and passkey auth fails.
536
+ */
537
+ onPinRequired?: () => Promise<string | null>;
538
+ }
539
+ /**
540
+ * Hook for transferring tokens.
541
+ *
542
+ * When `usePasskey: true`:
543
+ * 1. Tries passkey authentication first
544
+ * 2. If passkey fails and wallet has `encryptedPrivateKeyBackup`:
545
+ * calls `onPinRequired` to get PIN, uses PIN to decrypt backup
546
+ * 3. If no backup or no `onPinRequired`, throws
547
+ *
548
+ * @example
549
+ * ```tsx
550
+ * const { transferAsync } = useTransfer({
551
+ * onPinRequired: async () => {
552
+ * // Show PIN dialog, return PIN or null to cancel
553
+ * return await showPinDialog();
554
+ * },
555
+ * });
556
+ * ```
557
+ */
558
+ declare function useTransfer(config?: UseTransferConfig): {
504
559
  transfer: (input: TransferInput) => void;
505
560
  transferAsync: (input: TransferInput) => Promise<string>;
506
561
  data: string | undefined;
@@ -1068,6 +1123,39 @@ declare function useGetTransactionStatus(input?: GetTransactionStatusInput): {
1068
1123
  promise: Promise<TransactionStatusResponse>;
1069
1124
  };
1070
1125
 
1126
+ type SyncOnChainInput = SyncOnChainTransfersParams & {
1127
+ bearerToken: string;
1128
+ };
1129
+ /**
1130
+ * Hook for syncing on-chain transfers from Voyager into the DB.
1131
+ *
1132
+ * Discovers transactions not made through the SDK (e.g., external USDC receives
1133
+ * from Argent X or other wallets). After sync, useGetTransactionList returns
1134
+ * the newly discovered transactions automatically.
1135
+ *
1136
+ * @example
1137
+ * ```tsx
1138
+ * const { syncTransfers, data, isLoading } = useSyncOnChainTransfers();
1139
+ *
1140
+ * // Sync on-chain transfers for a wallet
1141
+ * const result = await syncTransfersAsync({
1142
+ * walletAddress: wallet.publicKey,
1143
+ * bearerToken: await getToken(),
1144
+ * });
1145
+ * console.log(`Synced ${result.synced} new transfers`);
1146
+ * ```
1147
+ */
1148
+ declare function useSyncOnChainTransfers(): {
1149
+ syncTransfers: (input: SyncOnChainInput) => void;
1150
+ syncTransfersAsync: (input: SyncOnChainInput) => Promise<SyncOnChainTransfersResponse>;
1151
+ data: SyncOnChainTransfersResponse | undefined;
1152
+ isLoading: boolean;
1153
+ isError: boolean;
1154
+ error: Error | null;
1155
+ isSuccess: boolean;
1156
+ reset: () => void;
1157
+ };
1158
+
1071
1159
  type PurchaseSkuInput = {
1072
1160
  params: CreateSkuPurchaseParams;
1073
1161
  bearerToken: string;
@@ -2101,4 +2189,4 @@ interface UseX402PaymentReturn {
2101
2189
  */
2102
2190
  declare function useX402Payment(config: UseX402PaymentConfig): UseX402PaymentReturn;
2103
2191
 
2104
- export { type ChipiWalletData, type SessionState, type SessionWallet, type UseChipiSessionConfig, type UseChipiSessionReturn, type UseChipiWalletConfig, type UseChipiWalletReturn, type UseX402PaymentConfig, type UseX402PaymentReturn, useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuPurchase, useGetTokenBalance, useGetTransaction, useGetTransactionList, useGetTransactionStatus, useGetWallet, useMigrateWalletToPasskey, usePurchaseSku, useRecordSendTransaction, useRevokeSessionKey, useTransfer, useUpdateWalletEncryption, useX402Payment };
2192
+ export { type ChipiWalletData, type SessionState, type SessionWallet, type UseChipiSessionConfig, type UseChipiSessionReturn, type UseChipiWalletConfig, type UseChipiWalletReturn, type UseTransferConfig, type UseX402PaymentConfig, type UseX402PaymentReturn, 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 };
package/dist/hooks.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CreateWalletParams, CreateWalletResponse, GetWalletParams, GetWalletResponse, ChainToken, GetTokenBalanceResponse, WalletType, WalletData, SessionKeyData, SessionDataResponse, CreateSessionKeyParams, SessionConfig, Call, SpendingPolicyConfig, SpendingPolicyData, MigrateWalletToPasskeyParams, UpdateWalletEncryptionParams, UpdateWalletEncryptionResponse, TransferHookInput, ApproveHookInput, GetTransactionListQuery, PaginatedResponse, Transaction, TransactionStatusResponse, CreateSkuPurchaseParams, GetSkuListQuery, Sku, CallAnyContractParams, RecordSendTransactionParams, GetTokenBalanceParams, AddSessionKeyParams, RevokeSessionKeyParams, GetSessionDataParams, ExecuteWithSessionParams, PaymentRequirement, PaymentPayload } from '@chipi-stack/types';
1
+ import { CreateWalletParams, CreateWalletResponse, GetWalletParams, GetWalletResponse, ChainToken, GetTokenBalanceResponse, WalletType, WalletData, SessionKeyData, SessionDataResponse, CreateSessionKeyParams, SessionConfig, Call, SpendingPolicyConfig, SpendingPolicyData, MigrateWalletToPasskeyParams, UpdateWalletEncryptionParams, UpdateWalletEncryptionResponse, TransferHookInput, ApproveHookInput, GetTransactionListQuery, PaginatedResponse, Transaction, TransactionStatusResponse, SyncOnChainTransfersParams, SyncOnChainTransfersResponse, CreateSkuPurchaseParams, GetSkuListQuery, Sku, CallAnyContractParams, RecordSendTransactionParams, GetTokenBalanceParams, AddSessionKeyParams, RevokeSessionKeyParams, GetSessionDataParams, ExecuteWithSessionParams, PaymentRequirement, PaymentPayload } from '@chipi-stack/types';
2
2
  import * as _tanstack_react_query from '@tanstack/react-query';
3
3
  import { UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
4
4
 
@@ -7,7 +7,35 @@ type CreateWalletInput = {
7
7
  bearerToken: string;
8
8
  };
9
9
  /**
10
- * Hook for creating a new wallet
10
+ * Hook for creating a new wallet.
11
+ *
12
+ * Supports dual-key wrapping: when `usePasskey: true` AND `encryptKey` (PIN) provided,
13
+ * the private key is encrypted twice — once with the passkey-derived key (primary) and
14
+ * once with the PIN (backup). If passkey fails later, PIN recovers the wallet.
15
+ *
16
+ * @example
17
+ * ```tsx
18
+ * // Passkey + PIN (recommended)
19
+ * await createWalletAsync({
20
+ * params: {
21
+ * encryptKey: pin, // PIN backup
22
+ * externalUserId: userId,
23
+ * chain: Chain.STARKNET,
24
+ * usePasskey: true, // Creates passkey + dual encryption
25
+ * },
26
+ * bearerToken,
27
+ * });
28
+ *
29
+ * // PIN only (backward compatible)
30
+ * await createWalletAsync({
31
+ * params: {
32
+ * encryptKey: pin,
33
+ * externalUserId: userId,
34
+ * chain: Chain.STARKNET,
35
+ * },
36
+ * bearerToken,
37
+ * });
38
+ * ```
11
39
  */
12
40
  declare function useCreateWallet(): {
13
41
  createWallet: (input: CreateWalletInput) => void;
@@ -498,9 +526,36 @@ type TransferInput = {
498
526
  bearerToken: string;
499
527
  };
500
528
  /**
501
- * Hook for transferring tokens
529
+ * Configuration for useTransfer hook.
502
530
  */
503
- declare function useTransfer(): {
531
+ interface UseTransferConfig {
532
+ /**
533
+ * Callback to prompt user for PIN when passkey fails.
534
+ * Should show a PIN input dialog and return the PIN, or null to cancel.
535
+ * Only called when usePasskey: true and passkey auth fails.
536
+ */
537
+ onPinRequired?: () => Promise<string | null>;
538
+ }
539
+ /**
540
+ * Hook for transferring tokens.
541
+ *
542
+ * When `usePasskey: true`:
543
+ * 1. Tries passkey authentication first
544
+ * 2. If passkey fails and wallet has `encryptedPrivateKeyBackup`:
545
+ * calls `onPinRequired` to get PIN, uses PIN to decrypt backup
546
+ * 3. If no backup or no `onPinRequired`, throws
547
+ *
548
+ * @example
549
+ * ```tsx
550
+ * const { transferAsync } = useTransfer({
551
+ * onPinRequired: async () => {
552
+ * // Show PIN dialog, return PIN or null to cancel
553
+ * return await showPinDialog();
554
+ * },
555
+ * });
556
+ * ```
557
+ */
558
+ declare function useTransfer(config?: UseTransferConfig): {
504
559
  transfer: (input: TransferInput) => void;
505
560
  transferAsync: (input: TransferInput) => Promise<string>;
506
561
  data: string | undefined;
@@ -1068,6 +1123,39 @@ declare function useGetTransactionStatus(input?: GetTransactionStatusInput): {
1068
1123
  promise: Promise<TransactionStatusResponse>;
1069
1124
  };
1070
1125
 
1126
+ type SyncOnChainInput = SyncOnChainTransfersParams & {
1127
+ bearerToken: string;
1128
+ };
1129
+ /**
1130
+ * Hook for syncing on-chain transfers from Voyager into the DB.
1131
+ *
1132
+ * Discovers transactions not made through the SDK (e.g., external USDC receives
1133
+ * from Argent X or other wallets). After sync, useGetTransactionList returns
1134
+ * the newly discovered transactions automatically.
1135
+ *
1136
+ * @example
1137
+ * ```tsx
1138
+ * const { syncTransfers, data, isLoading } = useSyncOnChainTransfers();
1139
+ *
1140
+ * // Sync on-chain transfers for a wallet
1141
+ * const result = await syncTransfersAsync({
1142
+ * walletAddress: wallet.publicKey,
1143
+ * bearerToken: await getToken(),
1144
+ * });
1145
+ * console.log(`Synced ${result.synced} new transfers`);
1146
+ * ```
1147
+ */
1148
+ declare function useSyncOnChainTransfers(): {
1149
+ syncTransfers: (input: SyncOnChainInput) => void;
1150
+ syncTransfersAsync: (input: SyncOnChainInput) => Promise<SyncOnChainTransfersResponse>;
1151
+ data: SyncOnChainTransfersResponse | undefined;
1152
+ isLoading: boolean;
1153
+ isError: boolean;
1154
+ error: Error | null;
1155
+ isSuccess: boolean;
1156
+ reset: () => void;
1157
+ };
1158
+
1071
1159
  type PurchaseSkuInput = {
1072
1160
  params: CreateSkuPurchaseParams;
1073
1161
  bearerToken: string;
@@ -2101,4 +2189,4 @@ interface UseX402PaymentReturn {
2101
2189
  */
2102
2190
  declare function useX402Payment(config: UseX402PaymentConfig): UseX402PaymentReturn;
2103
2191
 
2104
- export { type ChipiWalletData, type SessionState, type SessionWallet, type UseChipiSessionConfig, type UseChipiSessionReturn, type UseChipiWalletConfig, type UseChipiWalletReturn, type UseX402PaymentConfig, type UseX402PaymentReturn, useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuPurchase, useGetTokenBalance, useGetTransaction, useGetTransactionList, useGetTransactionStatus, useGetWallet, useMigrateWalletToPasskey, usePurchaseSku, useRecordSendTransaction, useRevokeSessionKey, useTransfer, useUpdateWalletEncryption, useX402Payment };
2192
+ export { type ChipiWalletData, type SessionState, type SessionWallet, type UseChipiSessionConfig, type UseChipiSessionReturn, type UseChipiWalletConfig, type UseChipiWalletReturn, type UseTransferConfig, type UseX402PaymentConfig, type UseX402PaymentReturn, 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 };
package/dist/hooks.js CHANGED
@@ -23,6 +23,10 @@ function useCreateWallet() {
23
23
  const mutation = reactQuery.useMutation({
24
24
  mutationFn: async (input) => {
25
25
  let encryptKey = input.params.encryptKey;
26
+ let encryptKeyBackup;
27
+ let credentialId;
28
+ let prfSupported;
29
+ let authMethod;
26
30
  if (input.params.usePasskey) {
27
31
  if (!input.params.externalUserId) {
28
32
  throw new Error("externalUserId is required when using passkey");
@@ -31,15 +35,25 @@ function useCreateWallet() {
31
35
  const passkeyResult = await chipiPasskey.createWalletPasskey(
32
36
  input.params.externalUserId,
33
37
  input.params.externalUserId
34
- // Using externalUserId as userName
35
38
  );
36
- encryptKey = passkeyResult.encryptKey;
39
+ credentialId = passkeyResult.credentialId;
40
+ prfSupported = passkeyResult.prfSupported;
41
+ if (encryptKey) {
42
+ encryptKeyBackup = encryptKey;
43
+ encryptKey = passkeyResult.encryptKey;
44
+ authMethod = "passkey+pin";
45
+ } else {
46
+ encryptKey = passkeyResult.encryptKey;
47
+ authMethod = "passkey";
48
+ }
37
49
  } catch (error) {
38
50
  if (error instanceof Error) {
39
51
  throw new Error(`Passkey creation failed: ${error.message}`);
40
52
  }
41
53
  throw new Error("Failed to create passkey for wallet");
42
54
  }
55
+ } else {
56
+ authMethod = encryptKey ? "pin" : void 0;
43
57
  }
44
58
  if (!encryptKey) {
45
59
  throw new Error("encryptKey is required when usePasskey is false");
@@ -47,7 +61,12 @@ function useCreateWallet() {
47
61
  return chipiSDK.createWallet({
48
62
  params: {
49
63
  ...input.params,
50
- encryptKey
64
+ encryptKey,
65
+ encryptKeyBackup,
66
+ authMethod,
67
+ credentialId,
68
+ prfSupported,
69
+ deviceType: "web"
51
70
  },
52
71
  bearerToken: input.bearerToken
53
72
  });
@@ -647,31 +666,40 @@ function useUpdateWalletEncryption() {
647
666
  reset: mutation.reset
648
667
  };
649
668
  }
650
- function useTransfer() {
669
+ function useTransfer(config) {
651
670
  const { chipiSDK, passkeyAdapter } = useChipiContext();
652
671
  const mutation = reactQuery.useMutation({
653
672
  mutationFn: async (input) => {
654
673
  let encryptKey = input.params.encryptKey;
674
+ let walletForTransfer = input.params.wallet;
655
675
  if (input.params.usePasskey) {
656
676
  if (!input.params.externalUserId) {
657
677
  throw new Error("externalUserId is required when usePasskey is true");
658
678
  }
659
- try {
660
- const key = await passkeyAdapter.getWalletEncryptKey({
661
- externalUserId: input.params.externalUserId
662
- });
663
- if (!key) {
664
- throw new Error("Passkey authentication was cancelled");
665
- }
666
- encryptKey = key;
667
- } catch (error) {
668
- if (error instanceof DOMException && error.name === "NotAllowedError") {
669
- throw new Error("Passkey authentication was cancelled by the user");
670
- }
671
- if (error instanceof Error) {
672
- throw error;
679
+ const passkeyKey = await passkeyAdapter.getWalletEncryptKey({
680
+ externalUserId: input.params.externalUserId
681
+ });
682
+ if (passkeyKey) {
683
+ encryptKey = passkeyKey;
684
+ } else {
685
+ const wallet = input.params.wallet;
686
+ if (wallet.encryptedPrivateKeyBackup && config?.onPinRequired) {
687
+ const pin = await config.onPinRequired();
688
+ if (!pin) {
689
+ throw new Error("Transfer cancelled \u2014 PIN required as passkey fallback");
690
+ }
691
+ encryptKey = pin;
692
+ walletForTransfer = {
693
+ ...wallet,
694
+ encryptedPrivateKey: wallet.encryptedPrivateKeyBackup
695
+ };
696
+ } else if (wallet.encryptedPrivateKeyBackup) {
697
+ throw new Error(
698
+ "Passkey authentication failed. Provide an onPinRequired callback to use PIN backup."
699
+ );
700
+ } else {
701
+ throw new Error("Passkey authentication failed and no PIN backup available.");
673
702
  }
674
- throw new Error("Failed to authenticate with passkey");
675
703
  }
676
704
  }
677
705
  if (!encryptKey) {
@@ -685,6 +713,7 @@ function useTransfer() {
685
713
  return chipiSDK.transfer({
686
714
  params: {
687
715
  ...transferParams,
716
+ wallet: walletForTransfer,
688
717
  amount: String(transferParams.amount),
689
718
  encryptKey
690
719
  },
@@ -907,6 +936,31 @@ function useGetTransactionStatus(input) {
907
936
  fetchTransactionStatus
908
937
  };
909
938
  }
939
+ function useSyncOnChainTransfers() {
940
+ const { chipiSDK } = useChipiContext();
941
+ const queryClient = reactQuery.useQueryClient();
942
+ const mutation = reactQuery.useMutation({
943
+ mutationFn: async (input) => {
944
+ const { bearerToken, ...params } = input;
945
+ return chipiSDK.transactions.syncOnChainTransfers(params, bearerToken);
946
+ },
947
+ onSuccess: () => {
948
+ queryClient.invalidateQueries({
949
+ predicate: (query) => query.queryKey[0] === "transaction-list"
950
+ });
951
+ }
952
+ });
953
+ return {
954
+ syncTransfers: mutation.mutate,
955
+ syncTransfersAsync: mutation.mutateAsync,
956
+ data: mutation.data,
957
+ isLoading: mutation.isPending,
958
+ isError: mutation.isError,
959
+ error: mutation.error,
960
+ isSuccess: mutation.isSuccess,
961
+ reset: mutation.reset
962
+ };
963
+ }
910
964
  function usePurchaseSku() {
911
965
  const { chipiSDK } = useChipiContext();
912
966
  const mutation = reactQuery.useMutation({
@@ -1519,6 +1573,7 @@ exports.useMigrateWalletToPasskey = useMigrateWalletToPasskey;
1519
1573
  exports.usePurchaseSku = usePurchaseSku;
1520
1574
  exports.useRecordSendTransaction = useRecordSendTransaction;
1521
1575
  exports.useRevokeSessionKey = useRevokeSessionKey;
1576
+ exports.useSyncOnChainTransfers = useSyncOnChainTransfers;
1522
1577
  exports.useTransfer = useTransfer;
1523
1578
  exports.useUpdateWalletEncryption = useUpdateWalletEncryption;
1524
1579
  exports.useX402Payment = useX402Payment;