@chipi-stack/chipi-react 12.2.0 → 12.4.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.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  import React, { createContext, useContext, useMemo, useCallback, useState, useEffect } from 'react';
2
2
  import { QueryClient, QueryClientProvider, useMutation, useQueryClient, useQuery } from '@tanstack/react-query';
3
- import { ChipiSDK } from '@chipi-stack/backend';
3
+ import { ChipiSDK, decryptPrivateKey, encryptPrivateKey } from '@chipi-stack/backend';
4
4
  import { jsx } from '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/context/ChipiProvider.tsx
@@ -47,10 +48,34 @@ function useChipiContext() {
47
48
  function useCreateWallet() {
48
49
  const { chipiSDK } = useChipiContext();
49
50
  const mutation = useMutation({
50
- mutationFn: (input) => chipiSDK.createWallet({
51
- params: input.params,
52
- bearerToken: input.bearerToken
53
- })
51
+ mutationFn: async (input) => {
52
+ let encryptKey = input.params.encryptKey;
53
+ if (input.params.usePasskey) {
54
+ if (!input.params.externalUserId) {
55
+ throw new Error("externalUserId is required when using passkey");
56
+ }
57
+ try {
58
+ const passkeyResult = await createWalletPasskey(
59
+ input.params.externalUserId,
60
+ input.params.externalUserId
61
+ // Using externalUserId as userName
62
+ );
63
+ encryptKey = passkeyResult.encryptKey;
64
+ } catch (error) {
65
+ if (error instanceof Error) {
66
+ throw new Error(`Passkey creation failed: ${error.message}`);
67
+ }
68
+ throw new Error("Failed to create passkey for wallet");
69
+ }
70
+ }
71
+ return chipiSDK.createWallet({
72
+ params: {
73
+ ...input.params,
74
+ encryptKey
75
+ },
76
+ bearerToken: input.bearerToken
77
+ });
78
+ }
54
79
  });
55
80
  return {
56
81
  createWallet: mutation.mutate,
@@ -470,17 +495,91 @@ function useChipiSession(config) {
470
495
  error: combinedError
471
496
  };
472
497
  }
498
+ function useMigrateWalletToPasskey() {
499
+ const { chipiSDK } = useChipiContext();
500
+ const mutation = useMutation({
501
+ mutationFn: async (input) => {
502
+ const { wallet, oldEncryptKey, externalUserId, bearerToken } = input;
503
+ try {
504
+ const passkeyResult = await createWalletPasskey(
505
+ externalUserId,
506
+ externalUserId
507
+ // Using externalUserId as userName
508
+ );
509
+ let decryptedPrivateKey;
510
+ try {
511
+ decryptedPrivateKey = decryptPrivateKey(
512
+ wallet.encryptedPrivateKey,
513
+ oldEncryptKey
514
+ );
515
+ } catch (error) {
516
+ throw new Error(
517
+ "Failed to decrypt wallet with provided encryptKey. Please verify your PIN/password is correct."
518
+ );
519
+ }
520
+ const newEncryptedPrivateKey = encryptPrivateKey(
521
+ decryptedPrivateKey,
522
+ passkeyResult.encryptKey
523
+ );
524
+ const updatedWallet = {
525
+ ...wallet,
526
+ encryptedPrivateKey: newEncryptedPrivateKey
527
+ };
528
+ return {
529
+ success: true,
530
+ wallet: updatedWallet,
531
+ credentialId: passkeyResult.credentialId
532
+ };
533
+ } catch (error) {
534
+ if (error instanceof Error) {
535
+ throw new Error(`Migration failed: ${error.message}`);
536
+ }
537
+ throw new Error("Failed to migrate wallet to passkey");
538
+ }
539
+ }
540
+ });
541
+ return {
542
+ migrateWalletToPasskey: mutation.mutate,
543
+ migrateWalletToPasskeyAsync: mutation.mutateAsync,
544
+ data: mutation.data,
545
+ isLoading: mutation.isPending,
546
+ isError: mutation.isError,
547
+ error: mutation.error,
548
+ isSuccess: mutation.isSuccess,
549
+ reset: mutation.reset
550
+ };
551
+ }
473
552
  function useTransfer() {
474
553
  const { chipiSDK } = useChipiContext();
475
554
  const mutation = useMutation(
476
555
  {
477
- mutationFn: (input) => chipiSDK.transfer({
478
- params: {
479
- ...input.params,
480
- amount: String(input.params.amount)
481
- },
482
- bearerToken: input.bearerToken
483
- })
556
+ mutationFn: async (input) => {
557
+ let encryptKey = input.params.encryptKey;
558
+ if (input.params.usePasskey) {
559
+ try {
560
+ const key = await getWalletEncryptKey();
561
+ if (!key) {
562
+ throw new Error("Passkey authentication was cancelled");
563
+ }
564
+ encryptKey = key;
565
+ } catch (error) {
566
+ if (error instanceof Error) {
567
+ throw new Error(
568
+ `Passkey authentication failed: ${error.message}`
569
+ );
570
+ }
571
+ throw new Error("Failed to authenticate with passkey");
572
+ }
573
+ }
574
+ return chipiSDK.transfer({
575
+ params: {
576
+ ...input.params,
577
+ amount: String(input.params.amount),
578
+ encryptKey
579
+ },
580
+ bearerToken: input.bearerToken
581
+ });
582
+ }
484
583
  }
485
584
  );
486
585
  return {
@@ -497,13 +596,31 @@ function useTransfer() {
497
596
  function useApprove() {
498
597
  const { chipiSDK } = useChipiContext();
499
598
  const mutation = useMutation({
500
- mutationFn: (params) => chipiSDK.approve({
501
- params: {
502
- ...params.params,
503
- amount: String(params.params.amount)
504
- },
505
- bearerToken: params.bearerToken
506
- })
599
+ mutationFn: async (params) => {
600
+ let encryptKey = params.params.encryptKey;
601
+ if (params.params.usePasskey) {
602
+ try {
603
+ const key = await getWalletEncryptKey();
604
+ if (!key) {
605
+ throw new Error("Passkey authentication was cancelled");
606
+ }
607
+ encryptKey = key;
608
+ } catch (error) {
609
+ if (error instanceof Error) {
610
+ throw new Error(`Passkey authentication failed: ${error.message}`);
611
+ }
612
+ throw new Error("Failed to authenticate with passkey");
613
+ }
614
+ }
615
+ return chipiSDK.approve({
616
+ params: {
617
+ ...params.params,
618
+ amount: String(params.params.amount),
619
+ encryptKey
620
+ },
621
+ bearerToken: params.bearerToken
622
+ });
623
+ }
507
624
  });
508
625
  return {
509
626
  approve: mutation.mutate,
@@ -718,13 +835,33 @@ function useGetSku(input) {
718
835
  function useStakeVesuUsdc() {
719
836
  const { chipiSDK } = useChipiContext();
720
837
  const mutation = useMutation({
721
- mutationFn: (params) => chipiSDK.stakeVesuUsdc({
722
- params: {
723
- ...params.params,
724
- amount: String(params.params.amount)
725
- },
726
- bearerToken: params.bearerToken
727
- })
838
+ mutationFn: async (params) => {
839
+ let encryptKey = params.params.encryptKey;
840
+ if (params.params.usePasskey) {
841
+ try {
842
+ const key = await getWalletEncryptKey();
843
+ if (!key) {
844
+ throw new Error("Passkey authentication was cancelled");
845
+ }
846
+ encryptKey = key;
847
+ } catch (error) {
848
+ if (error instanceof Error) {
849
+ throw new Error(
850
+ `Passkey authentication failed: ${error.message}`
851
+ );
852
+ }
853
+ throw new Error("Failed to authenticate with passkey");
854
+ }
855
+ }
856
+ return chipiSDK.stakeVesuUsdc({
857
+ params: {
858
+ ...params.params,
859
+ amount: String(params.params.amount),
860
+ encryptKey
861
+ },
862
+ bearerToken: params.bearerToken
863
+ });
864
+ }
728
865
  });
729
866
  return {
730
867
  stakeVesuUsdc: mutation.mutate,
@@ -740,13 +877,33 @@ function useStakeVesuUsdc() {
740
877
  function useWithdrawVesuUsdc() {
741
878
  const { chipiSDK } = useChipiContext();
742
879
  const mutation = useMutation({
743
- mutationFn: (params) => chipiSDK.withdrawVesuUsdc({
744
- params: {
745
- ...params.params,
746
- amount: String(params.params.amount)
747
- },
748
- bearerToken: params.bearerToken
749
- })
880
+ mutationFn: async (params) => {
881
+ let encryptKey = params.params.encryptKey;
882
+ if (params.params.usePasskey) {
883
+ try {
884
+ const key = await getWalletEncryptKey();
885
+ if (!key) {
886
+ throw new Error("Passkey authentication was cancelled");
887
+ }
888
+ encryptKey = key;
889
+ } catch (error) {
890
+ if (error instanceof Error) {
891
+ throw new Error(
892
+ `Passkey authentication failed: ${error.message}`
893
+ );
894
+ }
895
+ throw new Error("Failed to authenticate with passkey");
896
+ }
897
+ }
898
+ return chipiSDK.withdrawVesuUsdc({
899
+ params: {
900
+ ...params.params,
901
+ amount: String(params.params.amount),
902
+ encryptKey
903
+ },
904
+ bearerToken: params.bearerToken
905
+ });
906
+ }
750
907
  });
751
908
  return {
752
909
  withdrawVesuUsdc: mutation.mutate,
@@ -762,7 +919,32 @@ function useWithdrawVesuUsdc() {
762
919
  function useCallAnyContract() {
763
920
  const { chipiSDK } = useChipiContext();
764
921
  const mutation = useMutation({
765
- mutationFn: (params) => chipiSDK.callAnyContract(params)
922
+ mutationFn: async (params) => {
923
+ let encryptKey = params.params.encryptKey;
924
+ if (params.params.usePasskey) {
925
+ try {
926
+ const key = await getWalletEncryptKey();
927
+ if (!key) {
928
+ throw new Error("Passkey authentication was cancelled");
929
+ }
930
+ encryptKey = key;
931
+ } catch (error) {
932
+ if (error instanceof Error) {
933
+ throw new Error(
934
+ `Passkey authentication failed: ${error.message}`
935
+ );
936
+ }
937
+ throw new Error("Failed to authenticate with passkey");
938
+ }
939
+ }
940
+ return chipiSDK.callAnyContract({
941
+ params: {
942
+ ...params.params,
943
+ encryptKey
944
+ },
945
+ bearerToken: params.bearerToken
946
+ });
947
+ }
766
948
  });
767
949
  return {
768
950
  callAnyContract: mutation.mutate,
@@ -1023,6 +1205,6 @@ function useExecuteWithSession() {
1023
1205
  };
1024
1206
  }
1025
1207
 
1026
- export { ChipiProvider, useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiContext, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateSkuTransaction, useCreateUser, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuTransaction, useGetTokenBalance, useGetTransactionList, useGetUser, useGetWallet, useRecordSendTransaction, useRevokeSessionKey, useStakeVesuUsdc, useTransfer, useWithdrawVesuUsdc };
1208
+ export { ChipiProvider, useAddSessionKeyToContract, useApprove, useCallAnyContract, useChipiContext, useChipiSession, useChipiWallet, useCreateSessionKey, useCreateSkuTransaction, useCreateUser, useCreateWallet, useExecuteWithSession, useGetSessionData, useGetSku, useGetSkuList, useGetSkuTransaction, useGetTokenBalance, useGetTransactionList, useGetUser, useGetWallet, useMigrateWalletToPasskey, useRecordSendTransaction, useRevokeSessionKey, useStakeVesuUsdc, useTransfer, useWithdrawVesuUsdc };
1027
1209
  //# sourceMappingURL=index.mjs.map
1028
1210
  //# sourceMappingURL=index.mjs.map