@chipi-stack/chipi-react 13.12.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
@@ -522,46 +516,76 @@ function useMigrateWalletToPasskey() {
522
516
  reset: mutation.reset
523
517
  };
524
518
  }
519
+ function useUpdateWalletEncryption() {
520
+ const { chipiSDK } = useChipiContext();
521
+ const queryClient = reactQuery.useQueryClient();
522
+ const mutation = reactQuery.useMutation({
523
+ mutationFn: async (input) => {
524
+ const { bearerToken, ...params } = input;
525
+ return chipiSDK.wallets.updateWalletEncryption(params, bearerToken);
526
+ },
527
+ onSuccess: (_, variables) => {
528
+ queryClient.invalidateQueries({
529
+ queryKey: ["chipi-wallet", variables.externalUserId]
530
+ });
531
+ }
532
+ });
533
+ return {
534
+ updateWalletEncryption: mutation.mutate,
535
+ updateWalletEncryptionAsync: mutation.mutateAsync,
536
+ data: mutation.data,
537
+ isLoading: mutation.isPending,
538
+ isError: mutation.isError,
539
+ error: mutation.error,
540
+ isSuccess: mutation.isSuccess,
541
+ reset: mutation.reset
542
+ };
543
+ }
525
544
  function useTransfer() {
526
545
  const { chipiSDK, passkeyAdapter } = useChipiContext();
527
- const mutation = reactQuery.useMutation(
528
- {
529
- mutationFn: async (input) => {
530
- let encryptKey = input.params.encryptKey;
531
- if (input.params.usePasskey) {
532
- if (!input.params.externalUserId) {
533
- throw new Error(
534
- "externalUserId is required when usePasskey is true"
535
- );
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");
559
+ }
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");
536
564
  }
537
- try {
538
- const key = await passkeyAdapter.getWalletEncryptKey({
539
- externalUserId: input.params.externalUserId
540
- });
541
- if (!key) {
542
- throw new Error("Passkey authentication was cancelled");
543
- }
544
- encryptKey = key;
545
- } catch (error) {
546
- if (error instanceof Error) {
547
- throw new Error(
548
- `Passkey authentication failed: ${error.message}`
549
- );
550
- }
551
- throw new Error("Failed to authenticate with passkey");
565
+ if (error instanceof Error) {
566
+ throw error;
552
567
  }
568
+ throw new Error("Failed to authenticate with passkey");
553
569
  }
554
- return chipiSDK.transfer({
555
- params: {
556
- ...input.params,
557
- amount: String(input.params.amount),
558
- encryptKey
559
- },
560
- bearerToken: input.bearerToken
561
- });
562
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
+ });
563
587
  }
564
- );
588
+ });
565
589
  return {
566
590
  transfer: mutation.mutate,
567
591
  transferAsync: mutation.mutateAsync,
@@ -597,10 +621,20 @@ function useApprove() {
597
621
  throw new Error("Failed to authenticate with passkey");
598
622
  }
599
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;
600
634
  return chipiSDK.approve({
601
635
  params: {
602
- ...params.params,
603
- amount: String(params.params.amount),
636
+ ...approveParams,
637
+ amount: String(approveParams.amount),
604
638
  encryptKey
605
639
  },
606
640
  bearerToken: params.bearerToken
@@ -650,9 +684,8 @@ function useGetTransactionList(input) {
650
684
  });
651
685
  const fetchTransactionList = async (newInput) => {
652
686
  return queryClient.fetchQuery({
653
- queryKey: buildQueryKey(newInput?.query),
687
+ queryKey: buildQueryKey(newInput.query),
654
688
  queryFn: async () => {
655
- if (!newInput?.getBearerToken) throw new Error("getBearerToken is required");
656
689
  const bearerToken = await newInput.getBearerToken();
657
690
  if (!bearerToken) throw new Error("Bearer token is required");
658
691
  return chipiSDK.getTransactionList(newInput.query, bearerToken);
@@ -709,8 +742,7 @@ function useGetSkuPurchase(input) {
709
742
  queryKey: ["sku-purchase", newInput?.id],
710
743
  queryFn: async () => {
711
744
  if (!newInput?.id) throw new Error("id is required");
712
- if (!newInput?.getBearerToken)
713
- throw new Error("getBearerToken is required");
745
+ if (!newInput?.getBearerToken) throw new Error("getBearerToken is required");
714
746
  const bearerToken = await newInput.getBearerToken();
715
747
  if (!bearerToken) throw new Error("Bearer token is required");
716
748
  return chipiSDK.getSkuPurchase(newInput.id, bearerToken);
@@ -784,9 +816,7 @@ function useGetSku(input) {
784
816
  if (!bearerToken) throw new Error("Bearer token is required");
785
817
  return chipiSDK.getSku(input.id, bearerToken);
786
818
  },
787
- enabled: Boolean(
788
- input?.id && input?.getBearerToken && input.id.trim() !== ""
789
- ),
819
+ enabled: Boolean(input?.id && input?.getBearerToken && input.id.trim() !== ""),
790
820
  retry: (failureCount, error) => {
791
821
  if (error instanceof shared.ChipiApiError || error?.status) {
792
822
  return false;
@@ -799,14 +829,13 @@ function useGetSku(input) {
799
829
  return queryClient.fetchQuery({
800
830
  queryKey: ["sku", input2?.id],
801
831
  queryFn: async () => {
802
- if (!input2?.getBearerToken)
803
- throw new Error("getBearerToken is required");
832
+ if (!input2?.getBearerToken) throw new Error("getBearerToken is required");
804
833
  const bearerToken = await input2.getBearerToken();
805
834
  if (!bearerToken) throw new Error("Bearer token is required");
806
835
  return chipiSDK.getSku(input2.id, bearerToken);
807
836
  },
808
837
  retry: (failureCount, error) => {
809
- if (error instanceof shared.ChipiApiError || error?.code) {
838
+ if (error instanceof shared.ChipiApiError || error?.status) {
810
839
  return false;
811
840
  }
812
841
  return failureCount < 3;
@@ -825,9 +854,7 @@ function useCallAnyContract() {
825
854
  let encryptKey = params.params.encryptKey;
826
855
  if (params.params.usePasskey) {
827
856
  if (!params.params.externalUserId) {
828
- throw new Error(
829
- "externalUserId is required when usePasskey is true"
830
- );
857
+ throw new Error("externalUserId is required when usePasskey is true");
831
858
  }
832
859
  try {
833
860
  const key = await passkeyAdapter.getWalletEncryptKey({
@@ -838,17 +865,26 @@ function useCallAnyContract() {
838
865
  }
839
866
  encryptKey = key;
840
867
  } catch (error) {
868
+ if (error instanceof DOMException && error.name === "NotAllowedError") {
869
+ throw new Error("Passkey authentication was cancelled by the user");
870
+ }
841
871
  if (error instanceof Error) {
842
- throw new Error(
843
- `Passkey authentication failed: ${error.message}`
844
- );
872
+ throw error;
845
873
  }
846
874
  throw new Error("Failed to authenticate with passkey");
847
875
  }
848
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;
849
885
  return chipiSDK.callAnyContract({
850
886
  params: {
851
- ...params.params,
887
+ ...contractParams,
852
888
  encryptKey
853
889
  },
854
890
  bearerToken: params.bearerToken
@@ -916,7 +952,8 @@ function useGetTokenBalance(input) {
916
952
  return queryClient.fetchQuery({
917
953
  queryKey: buildQueryKey(newInput?.params),
918
954
  queryFn: async () => {
919
- 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");
920
957
  const bearerToken = await newInput.getBearerToken();
921
958
  if (!bearerToken) throw new Error("Bearer token is required");
922
959
  return chipiSDK.getTokenBalance(newInput.params, bearerToken);
@@ -953,10 +990,7 @@ function useCreateSessionKey() {
953
990
  function useAddSessionKeyToContract() {
954
991
  const { chipiSDK } = useChipiContext();
955
992
  const mutation = reactQuery.useMutation({
956
- mutationFn: (input) => chipiSDK.sessions.addSessionKeyToContract(
957
- input.params,
958
- input.bearerToken
959
- )
993
+ mutationFn: (input) => chipiSDK.sessions.addSessionKeyToContract(input.params, input.bearerToken)
960
994
  });
961
995
  return {
962
996
  addSessionKeyToContract: mutation.mutate,
@@ -988,11 +1022,7 @@ function useRevokeSessionKey() {
988
1022
  function useGetSessionData(params, options) {
989
1023
  const { chipiSDK } = useChipiContext();
990
1024
  const query = reactQuery.useQuery({
991
- queryKey: [
992
- "sessionData",
993
- params?.walletAddress,
994
- params?.sessionPublicKey
995
- ],
1025
+ queryKey: ["sessionData", params?.walletAddress, params?.sessionPublicKey],
996
1026
  queryFn: () => {
997
1027
  if (!params) {
998
1028
  throw new Error("Session data params are required");
@@ -1050,5 +1080,6 @@ exports.usePurchaseSku = usePurchaseSku;
1050
1080
  exports.useRecordSendTransaction = useRecordSendTransaction;
1051
1081
  exports.useRevokeSessionKey = useRevokeSessionKey;
1052
1082
  exports.useTransfer = useTransfer;
1083
+ exports.useUpdateWalletEncryption = useUpdateWalletEncryption;
1053
1084
  //# sourceMappingURL=hooks.js.map
1054
1085
  //# sourceMappingURL=hooks.js.map