@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/index.js CHANGED
@@ -37,11 +37,7 @@ var createDefaultQueryClient = () => new reactQuery.QueryClient({
37
37
  }
38
38
  }
39
39
  });
40
- function ChipiProvider({
41
- children,
42
- config,
43
- passkeyAdapter
44
- }) {
40
+ function ChipiProvider({ children, config, passkeyAdapter }) {
45
41
  const queryClient = React__default.default.useMemo(() => createDefaultQueryClient(), []);
46
42
  const chipiSDK = React__default.default.useMemo(() => new backend.ChipiSDK(config), [config]);
47
43
  const resolvedPasskeyAdapter = passkeyAdapter ?? defaultPasskeyAdapter;
@@ -85,6 +81,9 @@ function useCreateWallet() {
85
81
  throw new Error("Failed to create passkey for wallet");
86
82
  }
87
83
  }
84
+ if (!encryptKey) {
85
+ throw new Error("encryptKey is required when usePasskey is false");
86
+ }
88
87
  return chipiSDK.createWallet({
89
88
  params: {
90
89
  ...input.params,
@@ -111,8 +110,7 @@ function useGetWallet(input) {
111
110
  const query = reactQuery.useQuery({
112
111
  queryKey: ["wallet", input?.params?.externalUserId],
113
112
  queryFn: async () => {
114
- if (!input || !input.params || !input.getBearerToken)
115
- throw new Error("Input is required");
113
+ if (!input || !input.params || !input.getBearerToken) throw new Error("Input is required");
116
114
  const bearerToken = await input.getBearerToken();
117
115
  if (!bearerToken) throw new Error("Bearer token is required");
118
116
  return chipiSDK.getWallet(input.params, bearerToken);
@@ -154,22 +152,16 @@ function useGetWallet(input) {
154
152
  function useChipiWallet(config) {
155
153
  const { chipiSDK } = useChipiContext();
156
154
  const queryClient = reactQuery.useQueryClient();
157
- const {
158
- externalUserId,
159
- getBearerToken,
160
- defaultToken = "USDC",
161
- enabled = true
162
- } = config;
163
- const isEnabled = Boolean(
164
- enabled && externalUserId && externalUserId.trim() !== ""
165
- );
155
+ const { externalUserId, getBearerToken, defaultToken = "USDC", enabled = true } = config;
156
+ const trimmedUserId = externalUserId?.trim() || null;
157
+ const isEnabled = Boolean(enabled && trimmedUserId);
166
158
  const walletQuery = reactQuery.useQuery({
167
- queryKey: ["chipi-wallet", externalUserId],
159
+ queryKey: ["chipi-wallet", trimmedUserId],
168
160
  queryFn: async () => {
169
- if (!externalUserId) return null;
161
+ if (!trimmedUserId) return null;
170
162
  const bearerToken = await getBearerToken();
171
163
  if (!bearerToken) throw new Error("Bearer token is required");
172
- return chipiSDK.getWallet({ externalUserId }, bearerToken);
164
+ return chipiSDK.getWallet({ externalUserId: trimmedUserId }, bearerToken);
173
165
  },
174
166
  enabled: isEnabled,
175
167
  retry: (failureCount, error) => {
@@ -195,7 +187,7 @@ function useChipiWallet(config) {
195
187
  bearerToken
196
188
  );
197
189
  },
198
- enabled: Boolean(walletPublicKey),
190
+ enabled: Boolean(isEnabled && walletPublicKey),
199
191
  retry: (failureCount, error) => {
200
192
  if (error instanceof shared.ChipiApiError || error?.status) {
201
193
  return false;
@@ -205,14 +197,14 @@ function useChipiWallet(config) {
205
197
  });
206
198
  const createWalletMutation = reactQuery.useMutation({
207
199
  mutationFn: async ({ encryptKey, walletType = "CHIPI" }) => {
208
- if (!externalUserId) throw new Error("External user ID is required");
200
+ if (!trimmedUserId) throw new Error("External user ID is required");
209
201
  const bearerToken = await getBearerToken();
210
202
  if (!bearerToken) throw new Error("Bearer token is required");
211
203
  return chipiSDK.createWallet({
212
204
  params: {
213
205
  chain: types.Chain.STARKNET,
214
206
  encryptKey,
215
- externalUserId,
207
+ externalUserId: trimmedUserId,
216
208
  walletType
217
209
  },
218
210
  bearerToken
@@ -220,7 +212,7 @@ function useChipiWallet(config) {
220
212
  },
221
213
  onSuccess: () => {
222
214
  queryClient.invalidateQueries({
223
- queryKey: ["chipi-wallet", externalUserId]
215
+ queryKey: ["chipi-wallet", trimmedUserId]
224
216
  });
225
217
  }
226
218
  });
@@ -295,14 +287,10 @@ function useChipiSession(config) {
295
287
  defaultMaxCalls = 1e3,
296
288
  autoCheckStatus = true
297
289
  } = config;
298
- const [localSession, setLocalSession] = React.useState(
299
- storedSession ?? null
300
- );
290
+ const [localSession, setLocalSession] = React.useState(storedSession ?? null);
301
291
  const [error, setError] = React.useState(null);
302
292
  React.useEffect(() => {
303
- if (storedSession) {
304
- setLocalSession(storedSession);
305
- }
293
+ setLocalSession(storedSession ?? null);
306
294
  }, [storedSession]);
307
295
  const supportsSession = React.useMemo(() => {
308
296
  if (!wallet) return false;
@@ -338,27 +326,32 @@ function useChipiSession(config) {
338
326
  const hasActiveSession = sessionState === "active";
339
327
  const isSessionExpired = sessionState === "expired";
340
328
  const remainingCalls = sessionStatusQuery.data?.remainingCalls;
341
- const createSessionMutation = reactQuery.useMutation({
342
- mutationFn: async (params = {}) => {
343
- if (!encryptKey) throw new Error("Encryption key (PIN) is required");
344
- if (!supportsSession) throw new Error("Wallet does not support sessions. Only CHIPI wallets support session keys.");
345
- const sessionData = chipiSDK.sessions.createSessionKey({
346
- encryptKey,
347
- durationSeconds: params.durationSeconds ?? defaultDurationSeconds
348
- });
349
- return sessionData;
350
- },
351
- onSuccess: async (sessionData) => {
352
- setLocalSession(sessionData);
353
- setError(null);
354
- if (onSessionCreated) {
355
- await onSessionCreated(sessionData);
329
+ const createSessionMutation = reactQuery.useMutation(
330
+ {
331
+ mutationFn: async (params = {}) => {
332
+ if (!encryptKey) throw new Error("Encryption key (PIN) is required");
333
+ if (!supportsSession)
334
+ throw new Error(
335
+ "Wallet does not support sessions. Only CHIPI wallets support session keys."
336
+ );
337
+ const sessionData = chipiSDK.sessions.createSessionKey({
338
+ encryptKey,
339
+ durationSeconds: params.durationSeconds ?? defaultDurationSeconds
340
+ });
341
+ return sessionData;
342
+ },
343
+ onSuccess: async (sessionData) => {
344
+ setLocalSession(sessionData);
345
+ setError(null);
346
+ if (onSessionCreated) {
347
+ await onSessionCreated(sessionData);
348
+ }
349
+ },
350
+ onError: (err) => {
351
+ setError(err);
356
352
  }
357
- },
358
- onError: (err) => {
359
- setError(err);
360
353
  }
361
- });
354
+ );
362
355
  const registerSessionMutation = reactQuery.useMutation({
363
356
  mutationFn: async (sessionConfig = {}) => {
364
357
  if (!wallet) throw new Error("Wallet is required");
@@ -519,22 +512,19 @@ function useMigrateWalletToPasskey() {
519
512
  mutationFn: async (input) => {
520
513
  const { wallet, oldEncryptKey, externalUserId, bearerToken } = input;
521
514
  try {
522
- const passkeyResult = await chipiPasskey.createWalletPasskey(
523
- externalUserId,
524
- externalUserId
525
- // Using externalUserId as userName
526
- );
527
515
  let decryptedPrivateKey;
528
516
  try {
529
- decryptedPrivateKey = backend.decryptPrivateKey(
530
- wallet.encryptedPrivateKey,
531
- oldEncryptKey
532
- );
533
- } catch (error) {
517
+ decryptedPrivateKey = backend.decryptPrivateKey(wallet.encryptedPrivateKey, oldEncryptKey);
518
+ } catch {
534
519
  throw new Error(
535
520
  "Failed to decrypt wallet with provided encryptKey. Please verify your PIN/password is correct."
536
521
  );
537
522
  }
523
+ const passkeyResult = await chipiPasskey.createWalletPasskey(
524
+ externalUserId,
525
+ externalUserId
526
+ // Using externalUserId as userName
527
+ );
538
528
  const newEncryptedPrivateKey = backend.encryptPrivateKey(
539
529
  decryptedPrivateKey,
540
530
  passkeyResult.encryptKey
@@ -594,44 +584,49 @@ function useUpdateWalletEncryption() {
594
584
  }
595
585
  function useTransfer() {
596
586
  const { chipiSDK, passkeyAdapter } = useChipiContext();
597
- const mutation = reactQuery.useMutation(
598
- {
599
- mutationFn: async (input) => {
600
- let encryptKey = input.params.encryptKey;
601
- if (input.params.usePasskey) {
602
- if (!input.params.externalUserId) {
603
- throw new Error(
604
- "externalUserId is required when usePasskey is true"
605
- );
587
+ const mutation = reactQuery.useMutation({
588
+ mutationFn: async (input) => {
589
+ let encryptKey = input.params.encryptKey;
590
+ if (input.params.usePasskey) {
591
+ if (!input.params.externalUserId) {
592
+ throw new Error("externalUserId is required when usePasskey is true");
593
+ }
594
+ try {
595
+ const key = await passkeyAdapter.getWalletEncryptKey({
596
+ externalUserId: input.params.externalUserId
597
+ });
598
+ if (!key) {
599
+ throw new Error("Passkey authentication was cancelled");
606
600
  }
607
- try {
608
- const key = await passkeyAdapter.getWalletEncryptKey({
609
- externalUserId: input.params.externalUserId
610
- });
611
- if (!key) {
612
- throw new Error("Passkey authentication was cancelled");
613
- }
614
- encryptKey = key;
615
- } catch (error) {
616
- if (error instanceof Error) {
617
- throw new Error(
618
- `Passkey authentication failed: ${error.message}`
619
- );
620
- }
621
- throw new Error("Failed to authenticate with passkey");
601
+ encryptKey = key;
602
+ } catch (error) {
603
+ if (error instanceof DOMException && error.name === "NotAllowedError") {
604
+ throw new Error("Passkey authentication was cancelled by the user");
605
+ }
606
+ if (error instanceof Error) {
607
+ throw error;
622
608
  }
609
+ throw new Error("Failed to authenticate with passkey");
623
610
  }
624
- return chipiSDK.transfer({
625
- params: {
626
- ...input.params,
627
- amount: String(input.params.amount),
628
- encryptKey
629
- },
630
- bearerToken: input.bearerToken
631
- });
632
611
  }
612
+ if (!encryptKey) {
613
+ throw new Error("encryptKey is required when usePasskey is false");
614
+ }
615
+ const {
616
+ usePasskey: _usePasskey,
617
+ externalUserId: _externalUserId,
618
+ ...transferParams
619
+ } = input.params;
620
+ return chipiSDK.transfer({
621
+ params: {
622
+ ...transferParams,
623
+ amount: String(transferParams.amount),
624
+ encryptKey
625
+ },
626
+ bearerToken: input.bearerToken
627
+ });
633
628
  }
634
- );
629
+ });
635
630
  return {
636
631
  transfer: mutation.mutate,
637
632
  transferAsync: mutation.mutateAsync,
@@ -667,10 +662,20 @@ function useApprove() {
667
662
  throw new Error("Failed to authenticate with passkey");
668
663
  }
669
664
  }
665
+ if (!encryptKey) {
666
+ throw new Error(
667
+ "encryptKey is required when usePasskey is false. Provide a PIN or enable usePasskey."
668
+ );
669
+ }
670
+ const {
671
+ usePasskey: _usePasskey,
672
+ externalUserId: _externalUserId,
673
+ ...approveParams
674
+ } = params.params;
670
675
  return chipiSDK.approve({
671
676
  params: {
672
- ...params.params,
673
- amount: String(params.params.amount),
677
+ ...approveParams,
678
+ amount: String(approveParams.amount),
674
679
  encryptKey
675
680
  },
676
681
  bearerToken: params.bearerToken
@@ -720,9 +725,8 @@ function useGetTransactionList(input) {
720
725
  });
721
726
  const fetchTransactionList = async (newInput) => {
722
727
  return queryClient.fetchQuery({
723
- queryKey: buildQueryKey(newInput?.query),
728
+ queryKey: buildQueryKey(newInput.query),
724
729
  queryFn: async () => {
725
- if (!newInput?.getBearerToken) throw new Error("getBearerToken is required");
726
730
  const bearerToken = await newInput.getBearerToken();
727
731
  if (!bearerToken) throw new Error("Bearer token is required");
728
732
  return chipiSDK.getTransactionList(newInput.query, bearerToken);
@@ -779,8 +783,7 @@ function useGetSkuPurchase(input) {
779
783
  queryKey: ["sku-purchase", newInput?.id],
780
784
  queryFn: async () => {
781
785
  if (!newInput?.id) throw new Error("id is required");
782
- if (!newInput?.getBearerToken)
783
- throw new Error("getBearerToken is required");
786
+ if (!newInput?.getBearerToken) throw new Error("getBearerToken is required");
784
787
  const bearerToken = await newInput.getBearerToken();
785
788
  if (!bearerToken) throw new Error("Bearer token is required");
786
789
  return chipiSDK.getSkuPurchase(newInput.id, bearerToken);
@@ -854,9 +857,7 @@ function useGetSku(input) {
854
857
  if (!bearerToken) throw new Error("Bearer token is required");
855
858
  return chipiSDK.getSku(input.id, bearerToken);
856
859
  },
857
- enabled: Boolean(
858
- input?.id && input?.getBearerToken && input.id.trim() !== ""
859
- ),
860
+ enabled: Boolean(input?.id && input?.getBearerToken && input.id.trim() !== ""),
860
861
  retry: (failureCount, error) => {
861
862
  if (error instanceof shared.ChipiApiError || error?.status) {
862
863
  return false;
@@ -869,14 +870,13 @@ function useGetSku(input) {
869
870
  return queryClient.fetchQuery({
870
871
  queryKey: ["sku", input2?.id],
871
872
  queryFn: async () => {
872
- if (!input2?.getBearerToken)
873
- throw new Error("getBearerToken is required");
873
+ if (!input2?.getBearerToken) throw new Error("getBearerToken is required");
874
874
  const bearerToken = await input2.getBearerToken();
875
875
  if (!bearerToken) throw new Error("Bearer token is required");
876
876
  return chipiSDK.getSku(input2.id, bearerToken);
877
877
  },
878
878
  retry: (failureCount, error) => {
879
- if (error instanceof shared.ChipiApiError || error?.code) {
879
+ if (error instanceof shared.ChipiApiError || error?.status) {
880
880
  return false;
881
881
  }
882
882
  return failureCount < 3;
@@ -895,9 +895,7 @@ function useCallAnyContract() {
895
895
  let encryptKey = params.params.encryptKey;
896
896
  if (params.params.usePasskey) {
897
897
  if (!params.params.externalUserId) {
898
- throw new Error(
899
- "externalUserId is required when usePasskey is true"
900
- );
898
+ throw new Error("externalUserId is required when usePasskey is true");
901
899
  }
902
900
  try {
903
901
  const key = await passkeyAdapter.getWalletEncryptKey({
@@ -908,17 +906,26 @@ function useCallAnyContract() {
908
906
  }
909
907
  encryptKey = key;
910
908
  } catch (error) {
909
+ if (error instanceof DOMException && error.name === "NotAllowedError") {
910
+ throw new Error("Passkey authentication was cancelled by the user");
911
+ }
911
912
  if (error instanceof Error) {
912
- throw new Error(
913
- `Passkey authentication failed: ${error.message}`
914
- );
913
+ throw error;
915
914
  }
916
915
  throw new Error("Failed to authenticate with passkey");
917
916
  }
918
917
  }
918
+ if (!encryptKey) {
919
+ throw new Error("encryptKey is required when usePasskey is false");
920
+ }
921
+ const {
922
+ usePasskey: _usePasskey,
923
+ externalUserId: _externalUserId,
924
+ ...contractParams
925
+ } = params.params;
919
926
  return chipiSDK.callAnyContract({
920
927
  params: {
921
- ...params.params,
928
+ ...contractParams,
922
929
  encryptKey
923
930
  },
924
931
  bearerToken: params.bearerToken
@@ -986,7 +993,8 @@ function useGetTokenBalance(input) {
986
993
  return queryClient.fetchQuery({
987
994
  queryKey: buildQueryKey(newInput?.params),
988
995
  queryFn: async () => {
989
- if (!newInput || !newInput.getBearerToken || !newInput.params) throw new Error("Input is required");
996
+ if (!newInput || !newInput.getBearerToken || !newInput.params)
997
+ throw new Error("Input is required");
990
998
  const bearerToken = await newInput.getBearerToken();
991
999
  if (!bearerToken) throw new Error("Bearer token is required");
992
1000
  return chipiSDK.getTokenBalance(newInput.params, bearerToken);
@@ -1023,10 +1031,7 @@ function useCreateSessionKey() {
1023
1031
  function useAddSessionKeyToContract() {
1024
1032
  const { chipiSDK } = useChipiContext();
1025
1033
  const mutation = reactQuery.useMutation({
1026
- mutationFn: (input) => chipiSDK.sessions.addSessionKeyToContract(
1027
- input.params,
1028
- input.bearerToken
1029
- )
1034
+ mutationFn: (input) => chipiSDK.sessions.addSessionKeyToContract(input.params, input.bearerToken)
1030
1035
  });
1031
1036
  return {
1032
1037
  addSessionKeyToContract: mutation.mutate,
@@ -1058,11 +1063,7 @@ function useRevokeSessionKey() {
1058
1063
  function useGetSessionData(params, options) {
1059
1064
  const { chipiSDK } = useChipiContext();
1060
1065
  const query = reactQuery.useQuery({
1061
- queryKey: [
1062
- "sessionData",
1063
- params?.walletAddress,
1064
- params?.sessionPublicKey
1065
- ],
1066
+ queryKey: ["sessionData", params?.walletAddress, params?.sessionPublicKey],
1066
1067
  queryFn: () => {
1067
1068
  if (!params) {
1068
1069
  throw new Error("Session data params are required");