@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.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
@@ -520,46 +514,76 @@ function useMigrateWalletToPasskey() {
520
514
  reset: mutation.reset
521
515
  };
522
516
  }
517
+ function useUpdateWalletEncryption() {
518
+ const { chipiSDK } = useChipiContext();
519
+ const queryClient = useQueryClient();
520
+ const mutation = useMutation({
521
+ mutationFn: async (input) => {
522
+ const { bearerToken, ...params } = input;
523
+ return chipiSDK.wallets.updateWalletEncryption(params, bearerToken);
524
+ },
525
+ onSuccess: (_, variables) => {
526
+ queryClient.invalidateQueries({
527
+ queryKey: ["chipi-wallet", variables.externalUserId]
528
+ });
529
+ }
530
+ });
531
+ return {
532
+ updateWalletEncryption: mutation.mutate,
533
+ updateWalletEncryptionAsync: mutation.mutateAsync,
534
+ data: mutation.data,
535
+ isLoading: mutation.isPending,
536
+ isError: mutation.isError,
537
+ error: mutation.error,
538
+ isSuccess: mutation.isSuccess,
539
+ reset: mutation.reset
540
+ };
541
+ }
523
542
  function useTransfer() {
524
543
  const { chipiSDK, passkeyAdapter } = useChipiContext();
525
- const mutation = useMutation(
526
- {
527
- mutationFn: async (input) => {
528
- let encryptKey = input.params.encryptKey;
529
- if (input.params.usePasskey) {
530
- if (!input.params.externalUserId) {
531
- throw new Error(
532
- "externalUserId is required when usePasskey is true"
533
- );
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");
557
+ }
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");
534
562
  }
535
- try {
536
- const key = await passkeyAdapter.getWalletEncryptKey({
537
- externalUserId: input.params.externalUserId
538
- });
539
- if (!key) {
540
- throw new Error("Passkey authentication was cancelled");
541
- }
542
- encryptKey = key;
543
- } catch (error) {
544
- if (error instanceof Error) {
545
- throw new Error(
546
- `Passkey authentication failed: ${error.message}`
547
- );
548
- }
549
- throw new Error("Failed to authenticate with passkey");
563
+ if (error instanceof Error) {
564
+ throw error;
550
565
  }
566
+ throw new Error("Failed to authenticate with passkey");
551
567
  }
552
- return chipiSDK.transfer({
553
- params: {
554
- ...input.params,
555
- amount: String(input.params.amount),
556
- encryptKey
557
- },
558
- bearerToken: input.bearerToken
559
- });
560
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
+ });
561
585
  }
562
- );
586
+ });
563
587
  return {
564
588
  transfer: mutation.mutate,
565
589
  transferAsync: mutation.mutateAsync,
@@ -595,10 +619,20 @@ function useApprove() {
595
619
  throw new Error("Failed to authenticate with passkey");
596
620
  }
597
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;
598
632
  return chipiSDK.approve({
599
633
  params: {
600
- ...params.params,
601
- amount: String(params.params.amount),
634
+ ...approveParams,
635
+ amount: String(approveParams.amount),
602
636
  encryptKey
603
637
  },
604
638
  bearerToken: params.bearerToken
@@ -648,9 +682,8 @@ function useGetTransactionList(input) {
648
682
  });
649
683
  const fetchTransactionList = async (newInput) => {
650
684
  return queryClient.fetchQuery({
651
- queryKey: buildQueryKey(newInput?.query),
685
+ queryKey: buildQueryKey(newInput.query),
652
686
  queryFn: async () => {
653
- if (!newInput?.getBearerToken) throw new Error("getBearerToken is required");
654
687
  const bearerToken = await newInput.getBearerToken();
655
688
  if (!bearerToken) throw new Error("Bearer token is required");
656
689
  return chipiSDK.getTransactionList(newInput.query, bearerToken);
@@ -707,8 +740,7 @@ function useGetSkuPurchase(input) {
707
740
  queryKey: ["sku-purchase", newInput?.id],
708
741
  queryFn: async () => {
709
742
  if (!newInput?.id) throw new Error("id is required");
710
- if (!newInput?.getBearerToken)
711
- throw new Error("getBearerToken is required");
743
+ if (!newInput?.getBearerToken) throw new Error("getBearerToken is required");
712
744
  const bearerToken = await newInput.getBearerToken();
713
745
  if (!bearerToken) throw new Error("Bearer token is required");
714
746
  return chipiSDK.getSkuPurchase(newInput.id, bearerToken);
@@ -782,9 +814,7 @@ function useGetSku(input) {
782
814
  if (!bearerToken) throw new Error("Bearer token is required");
783
815
  return chipiSDK.getSku(input.id, bearerToken);
784
816
  },
785
- enabled: Boolean(
786
- input?.id && input?.getBearerToken && input.id.trim() !== ""
787
- ),
817
+ enabled: Boolean(input?.id && input?.getBearerToken && input.id.trim() !== ""),
788
818
  retry: (failureCount, error) => {
789
819
  if (error instanceof ChipiApiError || error?.status) {
790
820
  return false;
@@ -797,14 +827,13 @@ function useGetSku(input) {
797
827
  return queryClient.fetchQuery({
798
828
  queryKey: ["sku", input2?.id],
799
829
  queryFn: async () => {
800
- if (!input2?.getBearerToken)
801
- throw new Error("getBearerToken is required");
830
+ if (!input2?.getBearerToken) throw new Error("getBearerToken is required");
802
831
  const bearerToken = await input2.getBearerToken();
803
832
  if (!bearerToken) throw new Error("Bearer token is required");
804
833
  return chipiSDK.getSku(input2.id, bearerToken);
805
834
  },
806
835
  retry: (failureCount, error) => {
807
- if (error instanceof ChipiApiError || error?.code) {
836
+ if (error instanceof ChipiApiError || error?.status) {
808
837
  return false;
809
838
  }
810
839
  return failureCount < 3;
@@ -823,9 +852,7 @@ function useCallAnyContract() {
823
852
  let encryptKey = params.params.encryptKey;
824
853
  if (params.params.usePasskey) {
825
854
  if (!params.params.externalUserId) {
826
- throw new Error(
827
- "externalUserId is required when usePasskey is true"
828
- );
855
+ throw new Error("externalUserId is required when usePasskey is true");
829
856
  }
830
857
  try {
831
858
  const key = await passkeyAdapter.getWalletEncryptKey({
@@ -836,17 +863,26 @@ function useCallAnyContract() {
836
863
  }
837
864
  encryptKey = key;
838
865
  } catch (error) {
866
+ if (error instanceof DOMException && error.name === "NotAllowedError") {
867
+ throw new Error("Passkey authentication was cancelled by the user");
868
+ }
839
869
  if (error instanceof Error) {
840
- throw new Error(
841
- `Passkey authentication failed: ${error.message}`
842
- );
870
+ throw error;
843
871
  }
844
872
  throw new Error("Failed to authenticate with passkey");
845
873
  }
846
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;
847
883
  return chipiSDK.callAnyContract({
848
884
  params: {
849
- ...params.params,
885
+ ...contractParams,
850
886
  encryptKey
851
887
  },
852
888
  bearerToken: params.bearerToken
@@ -914,7 +950,8 @@ function useGetTokenBalance(input) {
914
950
  return queryClient.fetchQuery({
915
951
  queryKey: buildQueryKey(newInput?.params),
916
952
  queryFn: async () => {
917
- 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");
918
955
  const bearerToken = await newInput.getBearerToken();
919
956
  if (!bearerToken) throw new Error("Bearer token is required");
920
957
  return chipiSDK.getTokenBalance(newInput.params, bearerToken);
@@ -951,10 +988,7 @@ function useCreateSessionKey() {
951
988
  function useAddSessionKeyToContract() {
952
989
  const { chipiSDK } = useChipiContext();
953
990
  const mutation = useMutation({
954
- mutationFn: (input) => chipiSDK.sessions.addSessionKeyToContract(
955
- input.params,
956
- input.bearerToken
957
- )
991
+ mutationFn: (input) => chipiSDK.sessions.addSessionKeyToContract(input.params, input.bearerToken)
958
992
  });
959
993
  return {
960
994
  addSessionKeyToContract: mutation.mutate,
@@ -986,11 +1020,7 @@ function useRevokeSessionKey() {
986
1020
  function useGetSessionData(params, options) {
987
1021
  const { chipiSDK } = useChipiContext();
988
1022
  const query = useQuery({
989
- queryKey: [
990
- "sessionData",
991
- params?.walletAddress,
992
- params?.sessionPublicKey
993
- ],
1023
+ queryKey: ["sessionData", params?.walletAddress, params?.sessionPublicKey],
994
1024
  queryFn: () => {
995
1025
  if (!params) {
996
1026
  throw new Error("Session data params are required");
@@ -1028,6 +1058,6 @@ function useExecuteWithSession() {
1028
1058
  };
1029
1059
  }
1030
1060
 
1031
- export { useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuPurchase, useGetTokenBalance, useGetTransactionList, useGetWallet, useMigrateWalletToPasskey, usePurchaseSku, useRecordSendTransaction, useRevokeSessionKey, useTransfer };
1061
+ export { useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuPurchase, useGetTokenBalance, useGetTransactionList, useGetWallet, useMigrateWalletToPasskey, usePurchaseSku, useRecordSendTransaction, useRevokeSessionKey, useTransfer, useUpdateWalletEncryption };
1032
1062
  //# sourceMappingURL=hooks.mjs.map
1033
1063
  //# sourceMappingURL=hooks.mjs.map