@chipi-stack/chipi-react 13.13.0 → 14.0.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.js CHANGED
@@ -40,6 +40,9 @@ function useCreateWallet() {
40
40
  throw new Error("Failed to create passkey for wallet");
41
41
  }
42
42
  }
43
+ if (!encryptKey) {
44
+ throw new Error("encryptKey is required when usePasskey is false");
45
+ }
43
46
  return chipiSDK.createWallet({
44
47
  params: {
45
48
  ...input.params,
@@ -66,8 +69,7 @@ function useGetWallet(input) {
66
69
  const query = reactQuery.useQuery({
67
70
  queryKey: ["wallet", input?.params?.externalUserId],
68
71
  queryFn: async () => {
69
- if (!input || !input.params || !input.getBearerToken)
70
- throw new Error("Input is required");
72
+ if (!input || !input.params || !input.getBearerToken) throw new Error("Input is required");
71
73
  const bearerToken = await input.getBearerToken();
72
74
  if (!bearerToken) throw new Error("Bearer token is required");
73
75
  return chipiSDK.getWallet(input.params, bearerToken);
@@ -109,22 +111,16 @@ function useGetWallet(input) {
109
111
  function useChipiWallet(config) {
110
112
  const { chipiSDK } = useChipiContext();
111
113
  const queryClient = reactQuery.useQueryClient();
112
- const {
113
- externalUserId,
114
- getBearerToken,
115
- defaultToken = "USDC",
116
- enabled = true
117
- } = config;
118
- const isEnabled = Boolean(
119
- enabled && externalUserId && externalUserId.trim() !== ""
120
- );
114
+ const { externalUserId, getBearerToken, defaultToken = "USDC", enabled = true } = config;
115
+ const trimmedUserId = externalUserId?.trim() || null;
116
+ const isEnabled = Boolean(enabled && trimmedUserId);
121
117
  const walletQuery = reactQuery.useQuery({
122
- queryKey: ["chipi-wallet", externalUserId],
118
+ queryKey: ["chipi-wallet", trimmedUserId],
123
119
  queryFn: async () => {
124
- if (!externalUserId) return null;
120
+ if (!trimmedUserId) return null;
125
121
  const bearerToken = await getBearerToken();
126
122
  if (!bearerToken) throw new Error("Bearer token is required");
127
- return chipiSDK.getWallet({ externalUserId }, bearerToken);
123
+ return chipiSDK.getWallet({ externalUserId: trimmedUserId }, bearerToken);
128
124
  },
129
125
  enabled: isEnabled,
130
126
  retry: (failureCount, error) => {
@@ -150,7 +146,7 @@ function useChipiWallet(config) {
150
146
  bearerToken
151
147
  );
152
148
  },
153
- enabled: Boolean(walletPublicKey),
149
+ enabled: Boolean(isEnabled && walletPublicKey),
154
150
  retry: (failureCount, error) => {
155
151
  if (error instanceof shared.ChipiApiError || error?.status) {
156
152
  return false;
@@ -160,14 +156,14 @@ function useChipiWallet(config) {
160
156
  });
161
157
  const createWalletMutation = reactQuery.useMutation({
162
158
  mutationFn: async ({ encryptKey, walletType = "CHIPI" }) => {
163
- if (!externalUserId) throw new Error("External user ID is required");
159
+ if (!trimmedUserId) throw new Error("External user ID is required");
164
160
  const bearerToken = await getBearerToken();
165
161
  if (!bearerToken) throw new Error("Bearer token is required");
166
162
  return chipiSDK.createWallet({
167
163
  params: {
168
164
  chain: types.Chain.STARKNET,
169
165
  encryptKey,
170
- externalUserId,
166
+ externalUserId: trimmedUserId,
171
167
  walletType
172
168
  },
173
169
  bearerToken
@@ -175,7 +171,7 @@ function useChipiWallet(config) {
175
171
  },
176
172
  onSuccess: () => {
177
173
  queryClient.invalidateQueries({
178
- queryKey: ["chipi-wallet", externalUserId]
174
+ queryKey: ["chipi-wallet", trimmedUserId]
179
175
  });
180
176
  }
181
177
  });
@@ -250,14 +246,10 @@ function useChipiSession(config) {
250
246
  defaultMaxCalls = 1e3,
251
247
  autoCheckStatus = true
252
248
  } = config;
253
- const [localSession, setLocalSession] = react.useState(
254
- storedSession ?? null
255
- );
249
+ const [localSession, setLocalSession] = react.useState(storedSession ?? null);
256
250
  const [error, setError] = react.useState(null);
257
251
  react.useEffect(() => {
258
- if (storedSession) {
259
- setLocalSession(storedSession);
260
- }
252
+ setLocalSession(storedSession ?? null);
261
253
  }, [storedSession]);
262
254
  const supportsSession = react.useMemo(() => {
263
255
  if (!wallet) return false;
@@ -293,27 +285,32 @@ function useChipiSession(config) {
293
285
  const hasActiveSession = sessionState === "active";
294
286
  const isSessionExpired = sessionState === "expired";
295
287
  const remainingCalls = sessionStatusQuery.data?.remainingCalls;
296
- const createSessionMutation = reactQuery.useMutation({
297
- mutationFn: async (params = {}) => {
298
- if (!encryptKey) throw new Error("Encryption key (PIN) is required");
299
- if (!supportsSession) throw new Error("Wallet does not support sessions. Only CHIPI wallets support session keys.");
300
- const sessionData = chipiSDK.sessions.createSessionKey({
301
- encryptKey,
302
- durationSeconds: params.durationSeconds ?? defaultDurationSeconds
303
- });
304
- return sessionData;
305
- },
306
- onSuccess: async (sessionData) => {
307
- setLocalSession(sessionData);
308
- setError(null);
309
- if (onSessionCreated) {
310
- await onSessionCreated(sessionData);
288
+ const createSessionMutation = reactQuery.useMutation(
289
+ {
290
+ mutationFn: async (params = {}) => {
291
+ if (!encryptKey) throw new Error("Encryption key (PIN) is required");
292
+ if (!supportsSession)
293
+ throw new Error(
294
+ "Wallet does not support sessions. Only CHIPI wallets support session keys."
295
+ );
296
+ const sessionData = chipiSDK.sessions.createSessionKey({
297
+ encryptKey,
298
+ durationSeconds: params.durationSeconds ?? defaultDurationSeconds
299
+ });
300
+ return sessionData;
301
+ },
302
+ onSuccess: async (sessionData) => {
303
+ setLocalSession(sessionData);
304
+ setError(null);
305
+ if (onSessionCreated) {
306
+ await onSessionCreated(sessionData);
307
+ }
308
+ },
309
+ onError: (err) => {
310
+ setError(err);
311
311
  }
312
- },
313
- onError: (err) => {
314
- setError(err);
315
312
  }
316
- });
313
+ );
317
314
  const registerSessionMutation = reactQuery.useMutation({
318
315
  mutationFn: async (sessionConfig = {}) => {
319
316
  if (!wallet) throw new Error("Wallet is required");
@@ -474,22 +471,19 @@ function useMigrateWalletToPasskey() {
474
471
  mutationFn: async (input) => {
475
472
  const { wallet, oldEncryptKey, externalUserId, bearerToken } = input;
476
473
  try {
477
- const passkeyResult = await chipiPasskey.createWalletPasskey(
478
- externalUserId,
479
- externalUserId
480
- // Using externalUserId as userName
481
- );
482
474
  let decryptedPrivateKey;
483
475
  try {
484
- decryptedPrivateKey = backend.decryptPrivateKey(
485
- wallet.encryptedPrivateKey,
486
- oldEncryptKey
487
- );
488
- } catch (error) {
476
+ decryptedPrivateKey = backend.decryptPrivateKey(wallet.encryptedPrivateKey, oldEncryptKey);
477
+ } catch {
489
478
  throw new Error(
490
479
  "Failed to decrypt wallet with provided encryptKey. Please verify your PIN/password is correct."
491
480
  );
492
481
  }
482
+ const passkeyResult = await chipiPasskey.createWalletPasskey(
483
+ externalUserId,
484
+ externalUserId
485
+ // Using externalUserId as userName
486
+ );
493
487
  const newEncryptedPrivateKey = backend.encryptPrivateKey(
494
488
  decryptedPrivateKey,
495
489
  passkeyResult.encryptKey
@@ -549,44 +543,49 @@ function useUpdateWalletEncryption() {
549
543
  }
550
544
  function useTransfer() {
551
545
  const { chipiSDK, passkeyAdapter } = useChipiContext();
552
- const mutation = reactQuery.useMutation(
553
- {
554
- mutationFn: async (input) => {
555
- let encryptKey = input.params.encryptKey;
556
- if (input.params.usePasskey) {
557
- if (!input.params.externalUserId) {
558
- throw new Error(
559
- "externalUserId is required when usePasskey is true"
560
- );
546
+ const mutation = reactQuery.useMutation({
547
+ mutationFn: async (input) => {
548
+ let encryptKey = input.params.encryptKey;
549
+ if (input.params.usePasskey) {
550
+ if (!input.params.externalUserId) {
551
+ throw new Error("externalUserId is required when usePasskey is true");
552
+ }
553
+ try {
554
+ const key = await passkeyAdapter.getWalletEncryptKey({
555
+ externalUserId: input.params.externalUserId
556
+ });
557
+ if (!key) {
558
+ throw new Error("Passkey authentication was cancelled");
561
559
  }
562
- try {
563
- const key = await passkeyAdapter.getWalletEncryptKey({
564
- externalUserId: input.params.externalUserId
565
- });
566
- if (!key) {
567
- throw new Error("Passkey authentication was cancelled");
568
- }
569
- encryptKey = key;
570
- } catch (error) {
571
- if (error instanceof Error) {
572
- throw new Error(
573
- `Passkey authentication failed: ${error.message}`
574
- );
575
- }
576
- throw new Error("Failed to authenticate with passkey");
560
+ encryptKey = key;
561
+ } catch (error) {
562
+ if (error instanceof DOMException && error.name === "NotAllowedError") {
563
+ throw new Error("Passkey authentication was cancelled by the user");
564
+ }
565
+ if (error instanceof Error) {
566
+ throw error;
577
567
  }
568
+ throw new Error("Failed to authenticate with passkey");
578
569
  }
579
- return chipiSDK.transfer({
580
- params: {
581
- ...input.params,
582
- amount: String(input.params.amount),
583
- encryptKey
584
- },
585
- bearerToken: input.bearerToken
586
- });
587
570
  }
571
+ if (!encryptKey) {
572
+ throw new Error("encryptKey is required when usePasskey is false");
573
+ }
574
+ const {
575
+ usePasskey: _usePasskey,
576
+ externalUserId: _externalUserId,
577
+ ...transferParams
578
+ } = input.params;
579
+ return chipiSDK.transfer({
580
+ params: {
581
+ ...transferParams,
582
+ amount: String(transferParams.amount),
583
+ encryptKey
584
+ },
585
+ bearerToken: input.bearerToken
586
+ });
588
587
  }
589
- );
588
+ });
590
589
  return {
591
590
  transfer: mutation.mutate,
592
591
  transferAsync: mutation.mutateAsync,
@@ -622,10 +621,20 @@ function useApprove() {
622
621
  throw new Error("Failed to authenticate with passkey");
623
622
  }
624
623
  }
624
+ if (!encryptKey) {
625
+ throw new Error(
626
+ "encryptKey is required when usePasskey is false. Provide a PIN or enable usePasskey."
627
+ );
628
+ }
629
+ const {
630
+ usePasskey: _usePasskey,
631
+ externalUserId: _externalUserId,
632
+ ...approveParams
633
+ } = params.params;
625
634
  return chipiSDK.approve({
626
635
  params: {
627
- ...params.params,
628
- amount: String(params.params.amount),
636
+ ...approveParams,
637
+ amount: String(approveParams.amount),
629
638
  encryptKey
630
639
  },
631
640
  bearerToken: params.bearerToken
@@ -675,9 +684,8 @@ function useGetTransactionList(input) {
675
684
  });
676
685
  const fetchTransactionList = async (newInput) => {
677
686
  return queryClient.fetchQuery({
678
- queryKey: buildQueryKey(newInput?.query),
687
+ queryKey: buildQueryKey(newInput.query),
679
688
  queryFn: async () => {
680
- if (!newInput?.getBearerToken) throw new Error("getBearerToken is required");
681
689
  const bearerToken = await newInput.getBearerToken();
682
690
  if (!bearerToken) throw new Error("Bearer token is required");
683
691
  return chipiSDK.getTransactionList(newInput.query, bearerToken);
@@ -734,8 +742,7 @@ function useGetSkuPurchase(input) {
734
742
  queryKey: ["sku-purchase", newInput?.id],
735
743
  queryFn: async () => {
736
744
  if (!newInput?.id) throw new Error("id is required");
737
- if (!newInput?.getBearerToken)
738
- throw new Error("getBearerToken is required");
745
+ if (!newInput?.getBearerToken) throw new Error("getBearerToken is required");
739
746
  const bearerToken = await newInput.getBearerToken();
740
747
  if (!bearerToken) throw new Error("Bearer token is required");
741
748
  return chipiSDK.getSkuPurchase(newInput.id, bearerToken);
@@ -809,9 +816,7 @@ function useGetSku(input) {
809
816
  if (!bearerToken) throw new Error("Bearer token is required");
810
817
  return chipiSDK.getSku(input.id, bearerToken);
811
818
  },
812
- enabled: Boolean(
813
- input?.id && input?.getBearerToken && input.id.trim() !== ""
814
- ),
819
+ enabled: Boolean(input?.id && input?.getBearerToken && input.id.trim() !== ""),
815
820
  retry: (failureCount, error) => {
816
821
  if (error instanceof shared.ChipiApiError || error?.status) {
817
822
  return false;
@@ -824,14 +829,13 @@ function useGetSku(input) {
824
829
  return queryClient.fetchQuery({
825
830
  queryKey: ["sku", input2?.id],
826
831
  queryFn: async () => {
827
- if (!input2?.getBearerToken)
828
- throw new Error("getBearerToken is required");
832
+ if (!input2?.getBearerToken) throw new Error("getBearerToken is required");
829
833
  const bearerToken = await input2.getBearerToken();
830
834
  if (!bearerToken) throw new Error("Bearer token is required");
831
835
  return chipiSDK.getSku(input2.id, bearerToken);
832
836
  },
833
837
  retry: (failureCount, error) => {
834
- if (error instanceof shared.ChipiApiError || error?.code) {
838
+ if (error instanceof shared.ChipiApiError || error?.status) {
835
839
  return false;
836
840
  }
837
841
  return failureCount < 3;
@@ -850,9 +854,7 @@ function useCallAnyContract() {
850
854
  let encryptKey = params.params.encryptKey;
851
855
  if (params.params.usePasskey) {
852
856
  if (!params.params.externalUserId) {
853
- throw new Error(
854
- "externalUserId is required when usePasskey is true"
855
- );
857
+ throw new Error("externalUserId is required when usePasskey is true");
856
858
  }
857
859
  try {
858
860
  const key = await passkeyAdapter.getWalletEncryptKey({
@@ -863,17 +865,26 @@ function useCallAnyContract() {
863
865
  }
864
866
  encryptKey = key;
865
867
  } catch (error) {
868
+ if (error instanceof DOMException && error.name === "NotAllowedError") {
869
+ throw new Error("Passkey authentication was cancelled by the user");
870
+ }
866
871
  if (error instanceof Error) {
867
- throw new Error(
868
- `Passkey authentication failed: ${error.message}`
869
- );
872
+ throw error;
870
873
  }
871
874
  throw new Error("Failed to authenticate with passkey");
872
875
  }
873
876
  }
877
+ if (!encryptKey) {
878
+ throw new Error("encryptKey is required when usePasskey is false");
879
+ }
880
+ const {
881
+ usePasskey: _usePasskey,
882
+ externalUserId: _externalUserId,
883
+ ...contractParams
884
+ } = params.params;
874
885
  return chipiSDK.callAnyContract({
875
886
  params: {
876
- ...params.params,
887
+ ...contractParams,
877
888
  encryptKey
878
889
  },
879
890
  bearerToken: params.bearerToken
@@ -941,7 +952,8 @@ function useGetTokenBalance(input) {
941
952
  return queryClient.fetchQuery({
942
953
  queryKey: buildQueryKey(newInput?.params),
943
954
  queryFn: async () => {
944
- if (!newInput || !newInput.getBearerToken || !newInput.params) throw new Error("Input is required");
955
+ if (!newInput || !newInput.getBearerToken || !newInput.params)
956
+ throw new Error("Input is required");
945
957
  const bearerToken = await newInput.getBearerToken();
946
958
  if (!bearerToken) throw new Error("Bearer token is required");
947
959
  return chipiSDK.getTokenBalance(newInput.params, bearerToken);
@@ -978,10 +990,7 @@ function useCreateSessionKey() {
978
990
  function useAddSessionKeyToContract() {
979
991
  const { chipiSDK } = useChipiContext();
980
992
  const mutation = reactQuery.useMutation({
981
- mutationFn: (input) => chipiSDK.sessions.addSessionKeyToContract(
982
- input.params,
983
- input.bearerToken
984
- )
993
+ mutationFn: (input) => chipiSDK.sessions.addSessionKeyToContract(input.params, input.bearerToken)
985
994
  });
986
995
  return {
987
996
  addSessionKeyToContract: mutation.mutate,
@@ -1013,11 +1022,7 @@ function useRevokeSessionKey() {
1013
1022
  function useGetSessionData(params, options) {
1014
1023
  const { chipiSDK } = useChipiContext();
1015
1024
  const query = reactQuery.useQuery({
1016
- queryKey: [
1017
- "sessionData",
1018
- params?.walletAddress,
1019
- params?.sessionPublicKey
1020
- ],
1025
+ queryKey: ["sessionData", params?.walletAddress, params?.sessionPublicKey],
1021
1026
  queryFn: () => {
1022
1027
  if (!params) {
1023
1028
  throw new Error("Session data params are required");