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