@chipi-stack/chipi-react 12.1.0 → 12.3.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
@@ -1,7 +1,8 @@
1
1
  import { useMutation, useQueryClient, useQuery } from '@tanstack/react-query';
2
2
  import { createContext, useMemo, useCallback, useState, useEffect, useContext } from 'react';
3
- import '@chipi-stack/backend';
3
+ import { decryptPrivateKey, encryptPrivateKey } from '@chipi-stack/backend';
4
4
  import 'react/jsx-runtime';
5
+ import { createWalletPasskey, getWalletEncryptKey } from '@chipi-stack/chipi-passkey';
5
6
  import { ChipiApiError } from '@chipi-stack/shared';
6
7
 
7
8
  // src/hooks/useCreateWallet.ts
@@ -13,15 +14,37 @@ function useChipiContext() {
13
14
  }
14
15
  return context;
15
16
  }
16
-
17
- // src/hooks/useCreateWallet.ts
18
17
  function useCreateWallet() {
19
18
  const { chipiSDK } = useChipiContext();
20
19
  const mutation = useMutation({
21
- mutationFn: (input) => chipiSDK.createWallet({
22
- params: input.params,
23
- bearerToken: input.bearerToken
24
- })
20
+ mutationFn: async (input) => {
21
+ let encryptKey = input.params.encryptKey;
22
+ if (input.params.usePasskey) {
23
+ if (!input.params.externalUserId) {
24
+ throw new Error("externalUserId is required when using passkey");
25
+ }
26
+ try {
27
+ const passkeyResult = await createWalletPasskey(
28
+ input.params.externalUserId,
29
+ input.params.externalUserId
30
+ // Using externalUserId as userName
31
+ );
32
+ encryptKey = passkeyResult.encryptKey;
33
+ } catch (error) {
34
+ if (error instanceof Error) {
35
+ throw new Error(`Passkey creation failed: ${error.message}`);
36
+ }
37
+ throw new Error("Failed to create passkey for wallet");
38
+ }
39
+ }
40
+ return chipiSDK.createWallet({
41
+ params: {
42
+ ...input.params,
43
+ encryptKey
44
+ },
45
+ bearerToken: input.bearerToken
46
+ });
47
+ }
25
48
  });
26
49
  return {
27
50
  createWallet: mutation.mutate,
@@ -441,17 +464,91 @@ function useChipiSession(config) {
441
464
  error: combinedError
442
465
  };
443
466
  }
467
+ function useMigrateWalletToPasskey() {
468
+ const { chipiSDK } = useChipiContext();
469
+ const mutation = useMutation({
470
+ mutationFn: async (input) => {
471
+ const { wallet, oldEncryptKey, externalUserId, bearerToken } = input;
472
+ try {
473
+ const passkeyResult = await createWalletPasskey(
474
+ externalUserId,
475
+ externalUserId
476
+ // Using externalUserId as userName
477
+ );
478
+ let decryptedPrivateKey;
479
+ try {
480
+ decryptedPrivateKey = decryptPrivateKey(
481
+ wallet.encryptedPrivateKey,
482
+ oldEncryptKey
483
+ );
484
+ } catch (error) {
485
+ throw new Error(
486
+ "Failed to decrypt wallet with provided encryptKey. Please verify your PIN/password is correct."
487
+ );
488
+ }
489
+ const newEncryptedPrivateKey = encryptPrivateKey(
490
+ decryptedPrivateKey,
491
+ passkeyResult.encryptKey
492
+ );
493
+ const updatedWallet = {
494
+ ...wallet,
495
+ encryptedPrivateKey: newEncryptedPrivateKey
496
+ };
497
+ return {
498
+ success: true,
499
+ wallet: updatedWallet,
500
+ credentialId: passkeyResult.credentialId
501
+ };
502
+ } catch (error) {
503
+ if (error instanceof Error) {
504
+ throw new Error(`Migration failed: ${error.message}`);
505
+ }
506
+ throw new Error("Failed to migrate wallet to passkey");
507
+ }
508
+ }
509
+ });
510
+ return {
511
+ migrateWalletToPasskey: mutation.mutate,
512
+ migrateWalletToPasskeyAsync: mutation.mutateAsync,
513
+ data: mutation.data,
514
+ isLoading: mutation.isPending,
515
+ isError: mutation.isError,
516
+ error: mutation.error,
517
+ isSuccess: mutation.isSuccess,
518
+ reset: mutation.reset
519
+ };
520
+ }
444
521
  function useTransfer() {
445
522
  const { chipiSDK } = useChipiContext();
446
523
  const mutation = useMutation(
447
524
  {
448
- mutationFn: (input) => chipiSDK.transfer({
449
- params: {
450
- ...input.params,
451
- amount: String(input.params.amount)
452
- },
453
- bearerToken: input.bearerToken
454
- })
525
+ mutationFn: async (input) => {
526
+ let encryptKey = input.params.encryptKey;
527
+ if (input.params.usePasskey) {
528
+ try {
529
+ const key = await getWalletEncryptKey();
530
+ if (!key) {
531
+ throw new Error("Passkey authentication was cancelled");
532
+ }
533
+ encryptKey = key;
534
+ } catch (error) {
535
+ if (error instanceof Error) {
536
+ throw new Error(
537
+ `Passkey authentication failed: ${error.message}`
538
+ );
539
+ }
540
+ throw new Error("Failed to authenticate with passkey");
541
+ }
542
+ }
543
+ return chipiSDK.transfer({
544
+ params: {
545
+ ...input.params,
546
+ amount: String(input.params.amount),
547
+ encryptKey
548
+ },
549
+ bearerToken: input.bearerToken
550
+ });
551
+ }
455
552
  }
456
553
  );
457
554
  return {
@@ -468,13 +565,31 @@ function useTransfer() {
468
565
  function useApprove() {
469
566
  const { chipiSDK } = useChipiContext();
470
567
  const mutation = useMutation({
471
- mutationFn: (params) => chipiSDK.approve({
472
- params: {
473
- ...params.params,
474
- amount: String(params.params.amount)
475
- },
476
- bearerToken: params.bearerToken
477
- })
568
+ mutationFn: async (params) => {
569
+ let encryptKey = params.params.encryptKey;
570
+ if (params.params.usePasskey) {
571
+ try {
572
+ const key = await getWalletEncryptKey();
573
+ if (!key) {
574
+ throw new Error("Passkey authentication was cancelled");
575
+ }
576
+ encryptKey = key;
577
+ } catch (error) {
578
+ if (error instanceof Error) {
579
+ throw new Error(`Passkey authentication failed: ${error.message}`);
580
+ }
581
+ throw new Error("Failed to authenticate with passkey");
582
+ }
583
+ }
584
+ return chipiSDK.approve({
585
+ params: {
586
+ ...params.params,
587
+ amount: String(params.params.amount),
588
+ encryptKey
589
+ },
590
+ bearerToken: params.bearerToken
591
+ });
592
+ }
478
593
  });
479
594
  return {
480
595
  approve: mutation.mutate,
@@ -689,13 +804,33 @@ function useGetSku(input) {
689
804
  function useStakeVesuUsdc() {
690
805
  const { chipiSDK } = useChipiContext();
691
806
  const mutation = useMutation({
692
- mutationFn: (params) => chipiSDK.stakeVesuUsdc({
693
- params: {
694
- ...params.params,
695
- amount: String(params.params.amount)
696
- },
697
- bearerToken: params.bearerToken
698
- })
807
+ mutationFn: async (params) => {
808
+ let encryptKey = params.params.encryptKey;
809
+ if (params.params.usePasskey) {
810
+ try {
811
+ const key = await getWalletEncryptKey();
812
+ if (!key) {
813
+ throw new Error("Passkey authentication was cancelled");
814
+ }
815
+ encryptKey = key;
816
+ } catch (error) {
817
+ if (error instanceof Error) {
818
+ throw new Error(
819
+ `Passkey authentication failed: ${error.message}`
820
+ );
821
+ }
822
+ throw new Error("Failed to authenticate with passkey");
823
+ }
824
+ }
825
+ return chipiSDK.stakeVesuUsdc({
826
+ params: {
827
+ ...params.params,
828
+ amount: String(params.params.amount),
829
+ encryptKey
830
+ },
831
+ bearerToken: params.bearerToken
832
+ });
833
+ }
699
834
  });
700
835
  return {
701
836
  stakeVesuUsdc: mutation.mutate,
@@ -711,13 +846,33 @@ function useStakeVesuUsdc() {
711
846
  function useWithdrawVesuUsdc() {
712
847
  const { chipiSDK } = useChipiContext();
713
848
  const mutation = useMutation({
714
- mutationFn: (params) => chipiSDK.withdrawVesuUsdc({
715
- params: {
716
- ...params.params,
717
- amount: String(params.params.amount)
718
- },
719
- bearerToken: params.bearerToken
720
- })
849
+ mutationFn: async (params) => {
850
+ let encryptKey = params.params.encryptKey;
851
+ if (params.params.usePasskey) {
852
+ try {
853
+ const key = await getWalletEncryptKey();
854
+ if (!key) {
855
+ throw new Error("Passkey authentication was cancelled");
856
+ }
857
+ encryptKey = key;
858
+ } catch (error) {
859
+ if (error instanceof Error) {
860
+ throw new Error(
861
+ `Passkey authentication failed: ${error.message}`
862
+ );
863
+ }
864
+ throw new Error("Failed to authenticate with passkey");
865
+ }
866
+ }
867
+ return chipiSDK.withdrawVesuUsdc({
868
+ params: {
869
+ ...params.params,
870
+ amount: String(params.params.amount),
871
+ encryptKey
872
+ },
873
+ bearerToken: params.bearerToken
874
+ });
875
+ }
721
876
  });
722
877
  return {
723
878
  withdrawVesuUsdc: mutation.mutate,
@@ -733,7 +888,32 @@ function useWithdrawVesuUsdc() {
733
888
  function useCallAnyContract() {
734
889
  const { chipiSDK } = useChipiContext();
735
890
  const mutation = useMutation({
736
- mutationFn: (params) => chipiSDK.callAnyContract(params)
891
+ mutationFn: async (params) => {
892
+ let encryptKey = params.params.encryptKey;
893
+ if (params.params.usePasskey) {
894
+ try {
895
+ const key = await getWalletEncryptKey();
896
+ if (!key) {
897
+ throw new Error("Passkey authentication was cancelled");
898
+ }
899
+ encryptKey = key;
900
+ } catch (error) {
901
+ if (error instanceof Error) {
902
+ throw new Error(
903
+ `Passkey authentication failed: ${error.message}`
904
+ );
905
+ }
906
+ throw new Error("Failed to authenticate with passkey");
907
+ }
908
+ }
909
+ return chipiSDK.callAnyContract({
910
+ params: {
911
+ ...params.params,
912
+ encryptKey
913
+ },
914
+ bearerToken: params.bearerToken
915
+ });
916
+ }
737
917
  });
738
918
  return {
739
919
  callAnyContract: mutation.mutate,
@@ -994,6 +1174,6 @@ function useExecuteWithSession() {
994
1174
  };
995
1175
  }
996
1176
 
997
- export { useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateSkuTransaction, useCreateUser, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuTransaction, useGetTokenBalance, useGetTransactionList, useGetUser, useGetWallet, useRecordSendTransaction, useRevokeSessionKey, useStakeVesuUsdc, useTransfer, useWithdrawVesuUsdc };
1177
+ export { useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateSkuTransaction, useCreateUser, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuTransaction, useGetTokenBalance, useGetTransactionList, useGetUser, useGetWallet, useMigrateWalletToPasskey, useRecordSendTransaction, useRevokeSessionKey, useStakeVesuUsdc, useTransfer, useWithdrawVesuUsdc };
998
1178
  //# sourceMappingURL=hooks.mjs.map
999
1179
  //# sourceMappingURL=hooks.mjs.map