@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/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
@@ -567,46 +557,76 @@ function useMigrateWalletToPasskey() {
567
557
  reset: mutation.reset
568
558
  };
569
559
  }
560
+ function useUpdateWalletEncryption() {
561
+ const { chipiSDK } = useChipiContext();
562
+ const queryClient = reactQuery.useQueryClient();
563
+ const mutation = reactQuery.useMutation({
564
+ mutationFn: async (input) => {
565
+ const { bearerToken, ...params } = input;
566
+ return chipiSDK.wallets.updateWalletEncryption(params, bearerToken);
567
+ },
568
+ onSuccess: (_, variables) => {
569
+ queryClient.invalidateQueries({
570
+ queryKey: ["chipi-wallet", variables.externalUserId]
571
+ });
572
+ }
573
+ });
574
+ return {
575
+ updateWalletEncryption: mutation.mutate,
576
+ updateWalletEncryptionAsync: mutation.mutateAsync,
577
+ data: mutation.data,
578
+ isLoading: mutation.isPending,
579
+ isError: mutation.isError,
580
+ error: mutation.error,
581
+ isSuccess: mutation.isSuccess,
582
+ reset: mutation.reset
583
+ };
584
+ }
570
585
  function useTransfer() {
571
586
  const { chipiSDK, passkeyAdapter } = useChipiContext();
572
- const mutation = reactQuery.useMutation(
573
- {
574
- mutationFn: async (input) => {
575
- let encryptKey = input.params.encryptKey;
576
- if (input.params.usePasskey) {
577
- if (!input.params.externalUserId) {
578
- throw new Error(
579
- "externalUserId is required when usePasskey is true"
580
- );
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");
600
+ }
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");
581
605
  }
582
- try {
583
- const key = await passkeyAdapter.getWalletEncryptKey({
584
- externalUserId: input.params.externalUserId
585
- });
586
- if (!key) {
587
- throw new Error("Passkey authentication was cancelled");
588
- }
589
- encryptKey = key;
590
- } catch (error) {
591
- if (error instanceof Error) {
592
- throw new Error(
593
- `Passkey authentication failed: ${error.message}`
594
- );
595
- }
596
- throw new Error("Failed to authenticate with passkey");
606
+ if (error instanceof Error) {
607
+ throw error;
597
608
  }
609
+ throw new Error("Failed to authenticate with passkey");
598
610
  }
599
- return chipiSDK.transfer({
600
- params: {
601
- ...input.params,
602
- amount: String(input.params.amount),
603
- encryptKey
604
- },
605
- bearerToken: input.bearerToken
606
- });
607
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
+ });
608
628
  }
609
- );
629
+ });
610
630
  return {
611
631
  transfer: mutation.mutate,
612
632
  transferAsync: mutation.mutateAsync,
@@ -642,10 +662,20 @@ function useApprove() {
642
662
  throw new Error("Failed to authenticate with passkey");
643
663
  }
644
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;
645
675
  return chipiSDK.approve({
646
676
  params: {
647
- ...params.params,
648
- amount: String(params.params.amount),
677
+ ...approveParams,
678
+ amount: String(approveParams.amount),
649
679
  encryptKey
650
680
  },
651
681
  bearerToken: params.bearerToken
@@ -695,9 +725,8 @@ function useGetTransactionList(input) {
695
725
  });
696
726
  const fetchTransactionList = async (newInput) => {
697
727
  return queryClient.fetchQuery({
698
- queryKey: buildQueryKey(newInput?.query),
728
+ queryKey: buildQueryKey(newInput.query),
699
729
  queryFn: async () => {
700
- if (!newInput?.getBearerToken) throw new Error("getBearerToken is required");
701
730
  const bearerToken = await newInput.getBearerToken();
702
731
  if (!bearerToken) throw new Error("Bearer token is required");
703
732
  return chipiSDK.getTransactionList(newInput.query, bearerToken);
@@ -754,8 +783,7 @@ function useGetSkuPurchase(input) {
754
783
  queryKey: ["sku-purchase", newInput?.id],
755
784
  queryFn: async () => {
756
785
  if (!newInput?.id) throw new Error("id is required");
757
- if (!newInput?.getBearerToken)
758
- throw new Error("getBearerToken is required");
786
+ if (!newInput?.getBearerToken) throw new Error("getBearerToken is required");
759
787
  const bearerToken = await newInput.getBearerToken();
760
788
  if (!bearerToken) throw new Error("Bearer token is required");
761
789
  return chipiSDK.getSkuPurchase(newInput.id, bearerToken);
@@ -829,9 +857,7 @@ function useGetSku(input) {
829
857
  if (!bearerToken) throw new Error("Bearer token is required");
830
858
  return chipiSDK.getSku(input.id, bearerToken);
831
859
  },
832
- enabled: Boolean(
833
- input?.id && input?.getBearerToken && input.id.trim() !== ""
834
- ),
860
+ enabled: Boolean(input?.id && input?.getBearerToken && input.id.trim() !== ""),
835
861
  retry: (failureCount, error) => {
836
862
  if (error instanceof shared.ChipiApiError || error?.status) {
837
863
  return false;
@@ -844,14 +870,13 @@ function useGetSku(input) {
844
870
  return queryClient.fetchQuery({
845
871
  queryKey: ["sku", input2?.id],
846
872
  queryFn: async () => {
847
- if (!input2?.getBearerToken)
848
- throw new Error("getBearerToken is required");
873
+ if (!input2?.getBearerToken) throw new Error("getBearerToken is required");
849
874
  const bearerToken = await input2.getBearerToken();
850
875
  if (!bearerToken) throw new Error("Bearer token is required");
851
876
  return chipiSDK.getSku(input2.id, bearerToken);
852
877
  },
853
878
  retry: (failureCount, error) => {
854
- if (error instanceof shared.ChipiApiError || error?.code) {
879
+ if (error instanceof shared.ChipiApiError || error?.status) {
855
880
  return false;
856
881
  }
857
882
  return failureCount < 3;
@@ -870,9 +895,7 @@ function useCallAnyContract() {
870
895
  let encryptKey = params.params.encryptKey;
871
896
  if (params.params.usePasskey) {
872
897
  if (!params.params.externalUserId) {
873
- throw new Error(
874
- "externalUserId is required when usePasskey is true"
875
- );
898
+ throw new Error("externalUserId is required when usePasskey is true");
876
899
  }
877
900
  try {
878
901
  const key = await passkeyAdapter.getWalletEncryptKey({
@@ -883,17 +906,26 @@ function useCallAnyContract() {
883
906
  }
884
907
  encryptKey = key;
885
908
  } catch (error) {
909
+ if (error instanceof DOMException && error.name === "NotAllowedError") {
910
+ throw new Error("Passkey authentication was cancelled by the user");
911
+ }
886
912
  if (error instanceof Error) {
887
- throw new Error(
888
- `Passkey authentication failed: ${error.message}`
889
- );
913
+ throw error;
890
914
  }
891
915
  throw new Error("Failed to authenticate with passkey");
892
916
  }
893
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;
894
926
  return chipiSDK.callAnyContract({
895
927
  params: {
896
- ...params.params,
928
+ ...contractParams,
897
929
  encryptKey
898
930
  },
899
931
  bearerToken: params.bearerToken
@@ -961,7 +993,8 @@ function useGetTokenBalance(input) {
961
993
  return queryClient.fetchQuery({
962
994
  queryKey: buildQueryKey(newInput?.params),
963
995
  queryFn: async () => {
964
- 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");
965
998
  const bearerToken = await newInput.getBearerToken();
966
999
  if (!bearerToken) throw new Error("Bearer token is required");
967
1000
  return chipiSDK.getTokenBalance(newInput.params, bearerToken);
@@ -998,10 +1031,7 @@ function useCreateSessionKey() {
998
1031
  function useAddSessionKeyToContract() {
999
1032
  const { chipiSDK } = useChipiContext();
1000
1033
  const mutation = reactQuery.useMutation({
1001
- mutationFn: (input) => chipiSDK.sessions.addSessionKeyToContract(
1002
- input.params,
1003
- input.bearerToken
1004
- )
1034
+ mutationFn: (input) => chipiSDK.sessions.addSessionKeyToContract(input.params, input.bearerToken)
1005
1035
  });
1006
1036
  return {
1007
1037
  addSessionKeyToContract: mutation.mutate,
@@ -1033,11 +1063,7 @@ function useRevokeSessionKey() {
1033
1063
  function useGetSessionData(params, options) {
1034
1064
  const { chipiSDK } = useChipiContext();
1035
1065
  const query = reactQuery.useQuery({
1036
- queryKey: [
1037
- "sessionData",
1038
- params?.walletAddress,
1039
- params?.sessionPublicKey
1040
- ],
1066
+ queryKey: ["sessionData", params?.walletAddress, params?.sessionPublicKey],
1041
1067
  queryFn: () => {
1042
1068
  if (!params) {
1043
1069
  throw new Error("Session data params are required");
@@ -1105,5 +1131,6 @@ exports.usePurchaseSku = usePurchaseSku;
1105
1131
  exports.useRecordSendTransaction = useRecordSendTransaction;
1106
1132
  exports.useRevokeSessionKey = useRevokeSessionKey;
1107
1133
  exports.useTransfer = useTransfer;
1134
+ exports.useUpdateWalletEncryption = useUpdateWalletEncryption;
1108
1135
  //# sourceMappingURL=index.js.map
1109
1136
  //# sourceMappingURL=index.js.map