anymal-protocol 1.0.3 → 1.0.5

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.d.mts CHANGED
@@ -3,6 +3,16 @@ declare function useVerifyAccount(): (pid: string, dbAuthToken: string, bundlerC
3
3
  message: string;
4
4
  }>;
5
5
 
6
+ declare function useVerifyWeb3AuthSession(): (idToken: string, publicKey: string, authServiceBaseUrl: string) => Promise<any>;
7
+
8
+ declare function useCreateWeb3Account(): (idToken: string, publicKey: string, authServiceBaseUrl: string) => Promise<any>;
9
+
10
+ declare function useFetchUserData(): (dbAuthToken: string, endpoint: string) => Promise<any>;
11
+
12
+ declare function useUpdateUserEmail(): (dbAuthToken: string, docID: string, email: string, endpoint: string) => Promise<void>;
13
+
14
+ declare function useUpdateUserPid(): (dbAuthToken: string, docID: string, pid: string, endpoint: string) => Promise<void>;
15
+
6
16
  declare function useMintAnymalNFT(): (pid: string, nftId: string, dbAuthToken: string, validationContractAddress: string, smartAccount: any, bundlerClient: any) => Promise<{
7
17
  success: boolean;
8
18
  message: string;
@@ -34,4 +44,13 @@ declare function useUpdateAnymalWithNFT(): (anymalPassportId: string, anymalDocI
34
44
  success: boolean;
35
45
  }>;
36
46
 
37
- export { type AnymalNftMetadataInputData, type CreateAnymalInputData, useAddAnymalToDatabase, useDeleteAnymalFromDatabase, useMintAnymalNFT, useSaveAnymalMetadata, useUpdateAnymalWithNFT, useVerifyAccount };
47
+ declare function useUploadAnymalImage(): (imageFile: File, type: string, idToken: string, publicKey: string, authServiceBaseUrl: string) => Promise<{
48
+ success: boolean;
49
+ message: string;
50
+ url: any;
51
+ type: string;
52
+ }>;
53
+
54
+ declare function useFetchBalance(): (publicClient: any, walletAddress: string, kibbleTokenAddress: string) => Promise<number | undefined>;
55
+
56
+ export { type AnymalNftMetadataInputData, type CreateAnymalInputData, useAddAnymalToDatabase, useCreateWeb3Account, useDeleteAnymalFromDatabase, useFetchBalance, useFetchUserData, useMintAnymalNFT, useSaveAnymalMetadata, useUpdateAnymalWithNFT, useUpdateUserEmail, useUpdateUserPid, useUploadAnymalImage, useVerifyAccount, useVerifyWeb3AuthSession };
package/dist/index.d.ts CHANGED
@@ -3,6 +3,16 @@ declare function useVerifyAccount(): (pid: string, dbAuthToken: string, bundlerC
3
3
  message: string;
4
4
  }>;
5
5
 
6
+ declare function useVerifyWeb3AuthSession(): (idToken: string, publicKey: string, authServiceBaseUrl: string) => Promise<any>;
7
+
8
+ declare function useCreateWeb3Account(): (idToken: string, publicKey: string, authServiceBaseUrl: string) => Promise<any>;
9
+
10
+ declare function useFetchUserData(): (dbAuthToken: string, endpoint: string) => Promise<any>;
11
+
12
+ declare function useUpdateUserEmail(): (dbAuthToken: string, docID: string, email: string, endpoint: string) => Promise<void>;
13
+
14
+ declare function useUpdateUserPid(): (dbAuthToken: string, docID: string, pid: string, endpoint: string) => Promise<void>;
15
+
6
16
  declare function useMintAnymalNFT(): (pid: string, nftId: string, dbAuthToken: string, validationContractAddress: string, smartAccount: any, bundlerClient: any) => Promise<{
7
17
  success: boolean;
8
18
  message: string;
@@ -34,4 +44,13 @@ declare function useUpdateAnymalWithNFT(): (anymalPassportId: string, anymalDocI
34
44
  success: boolean;
35
45
  }>;
36
46
 
37
- export { type AnymalNftMetadataInputData, type CreateAnymalInputData, useAddAnymalToDatabase, useDeleteAnymalFromDatabase, useMintAnymalNFT, useSaveAnymalMetadata, useUpdateAnymalWithNFT, useVerifyAccount };
47
+ declare function useUploadAnymalImage(): (imageFile: File, type: string, idToken: string, publicKey: string, authServiceBaseUrl: string) => Promise<{
48
+ success: boolean;
49
+ message: string;
50
+ url: any;
51
+ type: string;
52
+ }>;
53
+
54
+ declare function useFetchBalance(): (publicClient: any, walletAddress: string, kibbleTokenAddress: string) => Promise<number | undefined>;
55
+
56
+ export { type AnymalNftMetadataInputData, type CreateAnymalInputData, useAddAnymalToDatabase, useCreateWeb3Account, useDeleteAnymalFromDatabase, useFetchBalance, useFetchUserData, useMintAnymalNFT, useSaveAnymalMetadata, useUpdateAnymalWithNFT, useUpdateUserEmail, useUpdateUserPid, useUploadAnymalImage, useVerifyAccount, useVerifyWeb3AuthSession };
package/dist/index.js CHANGED
@@ -21,15 +21,22 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  useAddAnymalToDatabase: () => useAddAnymalToDatabase,
24
+ useCreateWeb3Account: () => useCreateWeb3Account,
24
25
  useDeleteAnymalFromDatabase: () => useDeleteAnymalFromDatabase,
26
+ useFetchBalance: () => useFetchBalance,
27
+ useFetchUserData: () => useFetchUserData,
25
28
  useMintAnymalNFT: () => useMintAnymalNFT,
26
29
  useSaveAnymalMetadata: () => useSaveAnymalMetadata,
27
30
  useUpdateAnymalWithNFT: () => useUpdateAnymalWithNFT,
28
- useVerifyAccount: () => useVerifyAccount
31
+ useUpdateUserEmail: () => useUpdateUserEmail,
32
+ useUpdateUserPid: () => useUpdateUserPid,
33
+ useUploadAnymalImage: () => useUploadAnymalImage,
34
+ useVerifyAccount: () => useVerifyAccount,
35
+ useVerifyWeb3AuthSession: () => useVerifyWeb3AuthSession
29
36
  });
30
37
  module.exports = __toCommonJS(index_exports);
31
38
 
32
- // src/utils/useVerifyAccount.ts
39
+ // src/utils/account/useVerifyAccount.ts
33
40
  var import_react = require("react");
34
41
  var import_viem = require("viem");
35
42
 
@@ -590,7 +597,7 @@ var VERIFY_ACCOUNT_ABI = [
590
597
  }
591
598
  ];
592
599
 
593
- // src/utils/useVerifyAccount.ts
600
+ // src/utils/account/useVerifyAccount.ts
594
601
  function useVerifyAccount() {
595
602
  return (0, import_react.useCallback)(
596
603
  async (pid, dbAuthToken, bundlerClient, smartAccount, accountRewardsContractAddress) => {
@@ -630,11 +637,180 @@ function useVerifyAccount() {
630
637
  );
631
638
  }
632
639
 
640
+ // src/utils/account/useVerifyWeb3AuthSession.ts
641
+ var import_react2 = require("react");
642
+ function useVerifyWeb3AuthSession() {
643
+ return (0, import_react2.useCallback)(
644
+ async (idToken, publicKey, authServiceBaseUrl) => {
645
+ const response = await fetch(
646
+ `${authServiceBaseUrl}/verify-web3-session`,
647
+ {
648
+ method: "POST",
649
+ headers: {
650
+ "Content-Type": "application/json",
651
+ Authorization: "Bearer " + idToken
652
+ },
653
+ body: JSON.stringify({ appPubKey: publicKey })
654
+ }
655
+ );
656
+ const { jwt } = await response.json();
657
+ return jwt;
658
+ },
659
+ []
660
+ );
661
+ }
662
+
663
+ // src/utils/account/useCreateWeb3Account.ts
664
+ var import_react3 = require("react");
665
+ function useCreateWeb3Account() {
666
+ return (0, import_react3.useCallback)(
667
+ async (idToken, publicKey, authServiceBaseUrl) => {
668
+ try {
669
+ const response = await fetch(`${authServiceBaseUrl}/create-account`, {
670
+ method: "POST",
671
+ headers: {
672
+ "Content-Type": "application/json",
673
+ Authorization: "Bearer " + idToken
674
+ },
675
+ body: JSON.stringify({ appPubKey: publicKey })
676
+ });
677
+ return await response.json();
678
+ } catch (error) {
679
+ console.error(error);
680
+ return null;
681
+ }
682
+ },
683
+ []
684
+ );
685
+ }
686
+
687
+ // src/utils/account/useFetchUserData.ts
688
+ var import_react4 = require("react");
689
+ function useFetchUserData() {
690
+ return (0, import_react4.useCallback)(async (dbAuthToken, endpoint) => {
691
+ try {
692
+ const query = `
693
+ query User {
694
+ User {
695
+ _docID
696
+ pid
697
+ email
698
+ isVerified
699
+ accountType
700
+ }
701
+ }
702
+ `;
703
+ const response = await fetch(endpoint, {
704
+ method: "POST",
705
+ headers: {
706
+ "Content-Type": "application/json",
707
+ Authorization: `Bearer ${dbAuthToken}`
708
+ },
709
+ body: JSON.stringify({ query })
710
+ });
711
+ if (!response.ok) {
712
+ throw new Error(`HTTP error! Status: ${response.status}`);
713
+ }
714
+ const data = await response.json();
715
+ if (data && data.data && data.data.User && data.data.User.length > 0) {
716
+ return data.data.User[0];
717
+ } else {
718
+ return null;
719
+ }
720
+ } catch (error) {
721
+ console.error("Error fetching user data:", error);
722
+ return null;
723
+ }
724
+ }, []);
725
+ }
726
+
727
+ // src/utils/account/useUpdateUserEmail.ts
728
+ var import_react5 = require("react");
729
+ function useUpdateUserEmail() {
730
+ return (0, import_react5.useCallback)(
731
+ async (dbAuthToken, docID, email, endpoint) => {
732
+ try {
733
+ const mutation = `
734
+ mutation Update_User($docID: [ID], $input: UserMutationInputArg) {
735
+ update_User(docID: $docID, input: $input) {
736
+ email
737
+ }
738
+ }
739
+ `;
740
+ const variables = {
741
+ docId: [docID],
742
+ input: {
743
+ email
744
+ }
745
+ };
746
+ const response = await fetch(endpoint, {
747
+ method: "POST",
748
+ headers: {
749
+ "Content-Type": "application/json",
750
+ Authorization: `Bearer ${dbAuthToken}`
751
+ },
752
+ body: JSON.stringify({
753
+ query: mutation,
754
+ variables
755
+ })
756
+ });
757
+ if (!response.ok) {
758
+ throw new Error(`HTTP error! Status: ${response.status}`);
759
+ }
760
+ } catch (error) {
761
+ console.error("Error updating email:", error);
762
+ }
763
+ },
764
+ []
765
+ );
766
+ }
767
+
768
+ // src/utils/account/useUpdateUserPid.ts
769
+ var import_react6 = require("react");
770
+ function useUpdateUserPid() {
771
+ return (0, import_react6.useCallback)(
772
+ async (dbAuthToken, docID, pid, endpoint) => {
773
+ try {
774
+ const mutation = `
775
+ mutation Update_User($docID: [ID], $input: UserMutationInputArg) {
776
+ update_User(docID: $docID, input: $input) {
777
+ pid
778
+ }
779
+ }
780
+ `;
781
+ const variables = {
782
+ docId: [docID],
783
+ input: {
784
+ pid
785
+ }
786
+ };
787
+ const response = await fetch(endpoint, {
788
+ method: "POST",
789
+ headers: {
790
+ "Content-Type": "application/json",
791
+ Authorization: `Bearer ${dbAuthToken}`
792
+ },
793
+ body: JSON.stringify({
794
+ query: mutation,
795
+ variables
796
+ })
797
+ });
798
+ if (!response.ok) {
799
+ throw new Error(`HTTP error! Status: ${response.status}`);
800
+ }
801
+ } catch (error) {
802
+ console.error("Error updating email:", error);
803
+ }
804
+ },
805
+ []
806
+ );
807
+ }
808
+
633
809
  // src/utils/anymals/useMintAnymalNFT.ts
634
810
  var import_viem2 = require("viem");
635
- var import_react2 = require("react");
811
+ var import_react7 = require("react");
636
812
  function useMintAnymalNFT() {
637
- return (0, import_react2.useCallback)(
813
+ return (0, import_react7.useCallback)(
638
814
  async (pid, nftId, dbAuthToken, validationContractAddress, smartAccount, bundlerClient) => {
639
815
  if (!dbAuthToken || !nftId || !bundlerClient || !smartAccount || !pid || !validationContractAddress) {
640
816
  return {
@@ -673,9 +849,9 @@ function useMintAnymalNFT() {
673
849
  }
674
850
 
675
851
  // src/utils/anymals/useAddAnymalToDatabase.ts
676
- var import_react3 = require("react");
852
+ var import_react8 = require("react");
677
853
  function useAddAnymalToDatabase() {
678
- return (0, import_react3.useCallback)(
854
+ return (0, import_react8.useCallback)(
679
855
  async (dbAuthToken, endpoint, anymalData) => {
680
856
  if (!dbAuthToken) {
681
857
  return {
@@ -740,9 +916,9 @@ function useAddAnymalToDatabase() {
740
916
  }
741
917
 
742
918
  // src/utils/anymals/useDeleteAnymalFromDatabase.ts
743
- var import_react4 = require("react");
919
+ var import_react9 = require("react");
744
920
  function useDeleteAnymalFromDatabase() {
745
- return (0, import_react4.useCallback)(
921
+ return (0, import_react9.useCallback)(
746
922
  async (dbAuthToken, endpoint, anymalDocID) => {
747
923
  if (!dbAuthToken || !endpoint || !anymalDocID) return;
748
924
  try {
@@ -780,9 +956,9 @@ function useDeleteAnymalFromDatabase() {
780
956
  }
781
957
 
782
958
  // src/utils/anymals/useSaveAnymalMetadata.ts
783
- var import_react5 = require("react");
959
+ var import_react10 = require("react");
784
960
  function useSaveAnymalMetadata() {
785
- return (0, import_react5.useCallback)(
961
+ return (0, import_react10.useCallback)(
786
962
  async (idToken, publicKey, nftMetadataInput, authServiceBaseUrl) => {
787
963
  const response = await fetch(`${authServiceBaseUrl}/process-nft`, {
788
964
  method: "POST",
@@ -809,9 +985,9 @@ function useSaveAnymalMetadata() {
809
985
  }
810
986
 
811
987
  // src/utils/anymals/useUpdateAnymalWithNFT.ts
812
- var import_react6 = require("react");
988
+ var import_react11 = require("react");
813
989
  function useUpdateAnymalWithNFT() {
814
- return (0, import_react6.useCallback)(
990
+ return (0, import_react11.useCallback)(
815
991
  async (anymalPassportId, anymalDocId, dbAuthToken, endpoint) => {
816
992
  if (!dbAuthToken || !anymalPassportId || !anymalDocId || !endpoint) {
817
993
  return {
@@ -856,12 +1032,153 @@ function useUpdateAnymalWithNFT() {
856
1032
  []
857
1033
  );
858
1034
  }
1035
+
1036
+ // src/utils/anymals/useUploadAnymalImage.ts
1037
+ var import_react12 = require("react");
1038
+
1039
+ // src/helpers/UploadImageHelper.tsx
1040
+ function resizeImage(file, maxWidth = 800, maxHeight = 800, quality = 0.7) {
1041
+ return new Promise((resolve, reject) => {
1042
+ const img = new Image();
1043
+ img.src = URL.createObjectURL(file);
1044
+ img.onload = () => {
1045
+ const canvas = document.createElement("canvas");
1046
+ const ctx = canvas.getContext("2d");
1047
+ if (!ctx) {
1048
+ reject(new Error("Failed to get canvas 2D context"));
1049
+ return;
1050
+ }
1051
+ let width = img.width;
1052
+ let height = img.height;
1053
+ if (width > height) {
1054
+ if (width > maxWidth) {
1055
+ height = Math.round(height *= maxWidth / width);
1056
+ width = maxWidth;
1057
+ }
1058
+ } else {
1059
+ if (height > maxHeight) {
1060
+ width = Math.round(width *= maxHeight / height);
1061
+ height = maxHeight;
1062
+ }
1063
+ }
1064
+ canvas.width = width;
1065
+ canvas.height = height;
1066
+ ctx.drawImage(img, 0, 0, width, height);
1067
+ canvas.toBlob(
1068
+ (blob) => {
1069
+ if (blob) resolve(blob);
1070
+ else reject(new Error("Image compression failed."));
1071
+ },
1072
+ "image/jpeg",
1073
+ quality
1074
+ );
1075
+ };
1076
+ img.onerror = () => reject(new Error("Failed to load image."));
1077
+ });
1078
+ }
1079
+ async function prepareImage(imageFile) {
1080
+ const compressedFile = await resizeImage(imageFile);
1081
+ return toBase64(compressedFile);
1082
+ }
1083
+ function toBase64(file) {
1084
+ return new Promise((resolve, reject) => {
1085
+ const reader = new FileReader();
1086
+ reader.readAsDataURL(file);
1087
+ reader.onload = () => resolve(reader.result.split(",")[1]);
1088
+ reader.onerror = (error) => reject(error);
1089
+ });
1090
+ }
1091
+
1092
+ // src/utils/anymals/useUploadAnymalImage.ts
1093
+ function useUploadAnymalImage() {
1094
+ return (0, import_react12.useCallback)(
1095
+ async (imageFile, type, idToken, publicKey, authServiceBaseUrl) => {
1096
+ if (!imageFile || !idToken) {
1097
+ return {
1098
+ success: false,
1099
+ message: "No image file selected or not authenticated.",
1100
+ url: null,
1101
+ type
1102
+ };
1103
+ }
1104
+ try {
1105
+ const base64Image = await prepareImage(imageFile);
1106
+ const payload = {
1107
+ appPubKey: publicKey,
1108
+ imageData: base64Image,
1109
+ imageName: imageFile.name.split(".")[0]
1110
+ };
1111
+ const response = await fetch(`${authServiceBaseUrl}/process-image`, {
1112
+ method: "POST",
1113
+ headers: {
1114
+ "Content-Type": "application/json",
1115
+ Authorization: `Bearer ${idToken}`
1116
+ },
1117
+ body: JSON.stringify(payload)
1118
+ });
1119
+ const data = await response.json();
1120
+ if (response.ok) {
1121
+ return {
1122
+ success: true,
1123
+ message: "Image uploaded successfully.",
1124
+ url: data.ipfsUrl,
1125
+ type
1126
+ };
1127
+ } else {
1128
+ return {
1129
+ success: false,
1130
+ message: "Image failed to upload.",
1131
+ url: null,
1132
+ type
1133
+ };
1134
+ }
1135
+ } catch (error) {
1136
+ return {
1137
+ success: false,
1138
+ message: `An error occurred during image upload: ${error.message}`,
1139
+ url: null,
1140
+ type
1141
+ };
1142
+ }
1143
+ },
1144
+ []
1145
+ );
1146
+ }
1147
+
1148
+ // src/utils/balance/useFetchBalance.ts
1149
+ var import_react13 = require("react");
1150
+ var import_viem3 = require("viem");
1151
+ function useFetchBalance() {
1152
+ return (0, import_react13.useCallback)(
1153
+ async (publicClient, walletAddress, kibbleTokenAddress) => {
1154
+ try {
1155
+ const balance = await publicClient.readContract({
1156
+ address: (0, import_viem3.getAddress)(kibbleTokenAddress),
1157
+ abi: import_viem3.erc20Abi,
1158
+ functionName: "balanceOf",
1159
+ args: [(0, import_viem3.getAddress)(walletAddress)]
1160
+ });
1161
+ return Number(balance);
1162
+ } catch (error) {
1163
+ console.error("Failed to fetch token balance:", error);
1164
+ }
1165
+ },
1166
+ []
1167
+ );
1168
+ }
859
1169
  // Annotate the CommonJS export names for ESM import in node:
860
1170
  0 && (module.exports = {
861
1171
  useAddAnymalToDatabase,
1172
+ useCreateWeb3Account,
862
1173
  useDeleteAnymalFromDatabase,
1174
+ useFetchBalance,
1175
+ useFetchUserData,
863
1176
  useMintAnymalNFT,
864
1177
  useSaveAnymalMetadata,
865
1178
  useUpdateAnymalWithNFT,
866
- useVerifyAccount
1179
+ useUpdateUserEmail,
1180
+ useUpdateUserPid,
1181
+ useUploadAnymalImage,
1182
+ useVerifyAccount,
1183
+ useVerifyWeb3AuthSession
867
1184
  });
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- // src/utils/useVerifyAccount.ts
1
+ // src/utils/account/useVerifyAccount.ts
2
2
  import { useCallback } from "react";
3
3
  import { encodeFunctionData, parseGwei } from "viem";
4
4
 
@@ -559,7 +559,7 @@ var VERIFY_ACCOUNT_ABI = [
559
559
  }
560
560
  ];
561
561
 
562
- // src/utils/useVerifyAccount.ts
562
+ // src/utils/account/useVerifyAccount.ts
563
563
  function useVerifyAccount() {
564
564
  return useCallback(
565
565
  async (pid, dbAuthToken, bundlerClient, smartAccount, accountRewardsContractAddress) => {
@@ -599,11 +599,180 @@ function useVerifyAccount() {
599
599
  );
600
600
  }
601
601
 
602
+ // src/utils/account/useVerifyWeb3AuthSession.ts
603
+ import { useCallback as useCallback2 } from "react";
604
+ function useVerifyWeb3AuthSession() {
605
+ return useCallback2(
606
+ async (idToken, publicKey, authServiceBaseUrl) => {
607
+ const response = await fetch(
608
+ `${authServiceBaseUrl}/verify-web3-session`,
609
+ {
610
+ method: "POST",
611
+ headers: {
612
+ "Content-Type": "application/json",
613
+ Authorization: "Bearer " + idToken
614
+ },
615
+ body: JSON.stringify({ appPubKey: publicKey })
616
+ }
617
+ );
618
+ const { jwt } = await response.json();
619
+ return jwt;
620
+ },
621
+ []
622
+ );
623
+ }
624
+
625
+ // src/utils/account/useCreateWeb3Account.ts
626
+ import { useCallback as useCallback3 } from "react";
627
+ function useCreateWeb3Account() {
628
+ return useCallback3(
629
+ async (idToken, publicKey, authServiceBaseUrl) => {
630
+ try {
631
+ const response = await fetch(`${authServiceBaseUrl}/create-account`, {
632
+ method: "POST",
633
+ headers: {
634
+ "Content-Type": "application/json",
635
+ Authorization: "Bearer " + idToken
636
+ },
637
+ body: JSON.stringify({ appPubKey: publicKey })
638
+ });
639
+ return await response.json();
640
+ } catch (error) {
641
+ console.error(error);
642
+ return null;
643
+ }
644
+ },
645
+ []
646
+ );
647
+ }
648
+
649
+ // src/utils/account/useFetchUserData.ts
650
+ import { useCallback as useCallback4 } from "react";
651
+ function useFetchUserData() {
652
+ return useCallback4(async (dbAuthToken, endpoint) => {
653
+ try {
654
+ const query = `
655
+ query User {
656
+ User {
657
+ _docID
658
+ pid
659
+ email
660
+ isVerified
661
+ accountType
662
+ }
663
+ }
664
+ `;
665
+ const response = await fetch(endpoint, {
666
+ method: "POST",
667
+ headers: {
668
+ "Content-Type": "application/json",
669
+ Authorization: `Bearer ${dbAuthToken}`
670
+ },
671
+ body: JSON.stringify({ query })
672
+ });
673
+ if (!response.ok) {
674
+ throw new Error(`HTTP error! Status: ${response.status}`);
675
+ }
676
+ const data = await response.json();
677
+ if (data && data.data && data.data.User && data.data.User.length > 0) {
678
+ return data.data.User[0];
679
+ } else {
680
+ return null;
681
+ }
682
+ } catch (error) {
683
+ console.error("Error fetching user data:", error);
684
+ return null;
685
+ }
686
+ }, []);
687
+ }
688
+
689
+ // src/utils/account/useUpdateUserEmail.ts
690
+ import { useCallback as useCallback5 } from "react";
691
+ function useUpdateUserEmail() {
692
+ return useCallback5(
693
+ async (dbAuthToken, docID, email, endpoint) => {
694
+ try {
695
+ const mutation = `
696
+ mutation Update_User($docID: [ID], $input: UserMutationInputArg) {
697
+ update_User(docID: $docID, input: $input) {
698
+ email
699
+ }
700
+ }
701
+ `;
702
+ const variables = {
703
+ docId: [docID],
704
+ input: {
705
+ email
706
+ }
707
+ };
708
+ const response = await fetch(endpoint, {
709
+ method: "POST",
710
+ headers: {
711
+ "Content-Type": "application/json",
712
+ Authorization: `Bearer ${dbAuthToken}`
713
+ },
714
+ body: JSON.stringify({
715
+ query: mutation,
716
+ variables
717
+ })
718
+ });
719
+ if (!response.ok) {
720
+ throw new Error(`HTTP error! Status: ${response.status}`);
721
+ }
722
+ } catch (error) {
723
+ console.error("Error updating email:", error);
724
+ }
725
+ },
726
+ []
727
+ );
728
+ }
729
+
730
+ // src/utils/account/useUpdateUserPid.ts
731
+ import { useCallback as useCallback6 } from "react";
732
+ function useUpdateUserPid() {
733
+ return useCallback6(
734
+ async (dbAuthToken, docID, pid, endpoint) => {
735
+ try {
736
+ const mutation = `
737
+ mutation Update_User($docID: [ID], $input: UserMutationInputArg) {
738
+ update_User(docID: $docID, input: $input) {
739
+ pid
740
+ }
741
+ }
742
+ `;
743
+ const variables = {
744
+ docId: [docID],
745
+ input: {
746
+ pid
747
+ }
748
+ };
749
+ const response = await fetch(endpoint, {
750
+ method: "POST",
751
+ headers: {
752
+ "Content-Type": "application/json",
753
+ Authorization: `Bearer ${dbAuthToken}`
754
+ },
755
+ body: JSON.stringify({
756
+ query: mutation,
757
+ variables
758
+ })
759
+ });
760
+ if (!response.ok) {
761
+ throw new Error(`HTTP error! Status: ${response.status}`);
762
+ }
763
+ } catch (error) {
764
+ console.error("Error updating email:", error);
765
+ }
766
+ },
767
+ []
768
+ );
769
+ }
770
+
602
771
  // src/utils/anymals/useMintAnymalNFT.ts
603
772
  import { encodeFunctionData as encodeFunctionData2, parseGwei as parseGwei2 } from "viem";
604
- import { useCallback as useCallback2 } from "react";
773
+ import { useCallback as useCallback7 } from "react";
605
774
  function useMintAnymalNFT() {
606
- return useCallback2(
775
+ return useCallback7(
607
776
  async (pid, nftId, dbAuthToken, validationContractAddress, smartAccount, bundlerClient) => {
608
777
  if (!dbAuthToken || !nftId || !bundlerClient || !smartAccount || !pid || !validationContractAddress) {
609
778
  return {
@@ -642,9 +811,9 @@ function useMintAnymalNFT() {
642
811
  }
643
812
 
644
813
  // src/utils/anymals/useAddAnymalToDatabase.ts
645
- import { useCallback as useCallback3 } from "react";
814
+ import { useCallback as useCallback8 } from "react";
646
815
  function useAddAnymalToDatabase() {
647
- return useCallback3(
816
+ return useCallback8(
648
817
  async (dbAuthToken, endpoint, anymalData) => {
649
818
  if (!dbAuthToken) {
650
819
  return {
@@ -709,9 +878,9 @@ function useAddAnymalToDatabase() {
709
878
  }
710
879
 
711
880
  // src/utils/anymals/useDeleteAnymalFromDatabase.ts
712
- import { useCallback as useCallback4 } from "react";
881
+ import { useCallback as useCallback9 } from "react";
713
882
  function useDeleteAnymalFromDatabase() {
714
- return useCallback4(
883
+ return useCallback9(
715
884
  async (dbAuthToken, endpoint, anymalDocID) => {
716
885
  if (!dbAuthToken || !endpoint || !anymalDocID) return;
717
886
  try {
@@ -749,9 +918,9 @@ function useDeleteAnymalFromDatabase() {
749
918
  }
750
919
 
751
920
  // src/utils/anymals/useSaveAnymalMetadata.ts
752
- import { useCallback as useCallback5 } from "react";
921
+ import { useCallback as useCallback10 } from "react";
753
922
  function useSaveAnymalMetadata() {
754
- return useCallback5(
923
+ return useCallback10(
755
924
  async (idToken, publicKey, nftMetadataInput, authServiceBaseUrl) => {
756
925
  const response = await fetch(`${authServiceBaseUrl}/process-nft`, {
757
926
  method: "POST",
@@ -778,9 +947,9 @@ function useSaveAnymalMetadata() {
778
947
  }
779
948
 
780
949
  // src/utils/anymals/useUpdateAnymalWithNFT.ts
781
- import { useCallback as useCallback6 } from "react";
950
+ import { useCallback as useCallback11 } from "react";
782
951
  function useUpdateAnymalWithNFT() {
783
- return useCallback6(
952
+ return useCallback11(
784
953
  async (anymalPassportId, anymalDocId, dbAuthToken, endpoint) => {
785
954
  if (!dbAuthToken || !anymalPassportId || !anymalDocId || !endpoint) {
786
955
  return {
@@ -825,11 +994,152 @@ function useUpdateAnymalWithNFT() {
825
994
  []
826
995
  );
827
996
  }
997
+
998
+ // src/utils/anymals/useUploadAnymalImage.ts
999
+ import { useCallback as useCallback12 } from "react";
1000
+
1001
+ // src/helpers/UploadImageHelper.tsx
1002
+ function resizeImage(file, maxWidth = 800, maxHeight = 800, quality = 0.7) {
1003
+ return new Promise((resolve, reject) => {
1004
+ const img = new Image();
1005
+ img.src = URL.createObjectURL(file);
1006
+ img.onload = () => {
1007
+ const canvas = document.createElement("canvas");
1008
+ const ctx = canvas.getContext("2d");
1009
+ if (!ctx) {
1010
+ reject(new Error("Failed to get canvas 2D context"));
1011
+ return;
1012
+ }
1013
+ let width = img.width;
1014
+ let height = img.height;
1015
+ if (width > height) {
1016
+ if (width > maxWidth) {
1017
+ height = Math.round(height *= maxWidth / width);
1018
+ width = maxWidth;
1019
+ }
1020
+ } else {
1021
+ if (height > maxHeight) {
1022
+ width = Math.round(width *= maxHeight / height);
1023
+ height = maxHeight;
1024
+ }
1025
+ }
1026
+ canvas.width = width;
1027
+ canvas.height = height;
1028
+ ctx.drawImage(img, 0, 0, width, height);
1029
+ canvas.toBlob(
1030
+ (blob) => {
1031
+ if (blob) resolve(blob);
1032
+ else reject(new Error("Image compression failed."));
1033
+ },
1034
+ "image/jpeg",
1035
+ quality
1036
+ );
1037
+ };
1038
+ img.onerror = () => reject(new Error("Failed to load image."));
1039
+ });
1040
+ }
1041
+ async function prepareImage(imageFile) {
1042
+ const compressedFile = await resizeImage(imageFile);
1043
+ return toBase64(compressedFile);
1044
+ }
1045
+ function toBase64(file) {
1046
+ return new Promise((resolve, reject) => {
1047
+ const reader = new FileReader();
1048
+ reader.readAsDataURL(file);
1049
+ reader.onload = () => resolve(reader.result.split(",")[1]);
1050
+ reader.onerror = (error) => reject(error);
1051
+ });
1052
+ }
1053
+
1054
+ // src/utils/anymals/useUploadAnymalImage.ts
1055
+ function useUploadAnymalImage() {
1056
+ return useCallback12(
1057
+ async (imageFile, type, idToken, publicKey, authServiceBaseUrl) => {
1058
+ if (!imageFile || !idToken) {
1059
+ return {
1060
+ success: false,
1061
+ message: "No image file selected or not authenticated.",
1062
+ url: null,
1063
+ type
1064
+ };
1065
+ }
1066
+ try {
1067
+ const base64Image = await prepareImage(imageFile);
1068
+ const payload = {
1069
+ appPubKey: publicKey,
1070
+ imageData: base64Image,
1071
+ imageName: imageFile.name.split(".")[0]
1072
+ };
1073
+ const response = await fetch(`${authServiceBaseUrl}/process-image`, {
1074
+ method: "POST",
1075
+ headers: {
1076
+ "Content-Type": "application/json",
1077
+ Authorization: `Bearer ${idToken}`
1078
+ },
1079
+ body: JSON.stringify(payload)
1080
+ });
1081
+ const data = await response.json();
1082
+ if (response.ok) {
1083
+ return {
1084
+ success: true,
1085
+ message: "Image uploaded successfully.",
1086
+ url: data.ipfsUrl,
1087
+ type
1088
+ };
1089
+ } else {
1090
+ return {
1091
+ success: false,
1092
+ message: "Image failed to upload.",
1093
+ url: null,
1094
+ type
1095
+ };
1096
+ }
1097
+ } catch (error) {
1098
+ return {
1099
+ success: false,
1100
+ message: `An error occurred during image upload: ${error.message}`,
1101
+ url: null,
1102
+ type
1103
+ };
1104
+ }
1105
+ },
1106
+ []
1107
+ );
1108
+ }
1109
+
1110
+ // src/utils/balance/useFetchBalance.ts
1111
+ import { useCallback as useCallback13 } from "react";
1112
+ import { erc20Abi, getAddress } from "viem";
1113
+ function useFetchBalance() {
1114
+ return useCallback13(
1115
+ async (publicClient, walletAddress, kibbleTokenAddress) => {
1116
+ try {
1117
+ const balance = await publicClient.readContract({
1118
+ address: getAddress(kibbleTokenAddress),
1119
+ abi: erc20Abi,
1120
+ functionName: "balanceOf",
1121
+ args: [getAddress(walletAddress)]
1122
+ });
1123
+ return Number(balance);
1124
+ } catch (error) {
1125
+ console.error("Failed to fetch token balance:", error);
1126
+ }
1127
+ },
1128
+ []
1129
+ );
1130
+ }
828
1131
  export {
829
1132
  useAddAnymalToDatabase,
1133
+ useCreateWeb3Account,
830
1134
  useDeleteAnymalFromDatabase,
1135
+ useFetchBalance,
1136
+ useFetchUserData,
831
1137
  useMintAnymalNFT,
832
1138
  useSaveAnymalMetadata,
833
1139
  useUpdateAnymalWithNFT,
834
- useVerifyAccount
1140
+ useUpdateUserEmail,
1141
+ useUpdateUserPid,
1142
+ useUploadAnymalImage,
1143
+ useVerifyAccount,
1144
+ useVerifyWeb3AuthSession
835
1145
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anymal-protocol",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -0,0 +1,63 @@
1
+ export function resizeImage(
2
+ file: File,
3
+ maxWidth = 800,
4
+ maxHeight = 800,
5
+ quality = 0.7
6
+ ): Promise<Blob> {
7
+ return new Promise((resolve, reject) => {
8
+ const img = new Image();
9
+ img.src = URL.createObjectURL(file);
10
+ img.onload = () => {
11
+ const canvas = document.createElement("canvas");
12
+ const ctx = canvas.getContext("2d");
13
+
14
+ if (!ctx) {
15
+ reject(new Error("Failed to get canvas 2D context"));
16
+ return;
17
+ }
18
+
19
+ let width = img.width;
20
+ let height = img.height;
21
+
22
+ if (width > height) {
23
+ if (width > maxWidth) {
24
+ height = Math.round((height *= maxWidth / width));
25
+ width = maxWidth;
26
+ }
27
+ } else {
28
+ if (height > maxHeight) {
29
+ width = Math.round((width *= maxHeight / height));
30
+ height = maxHeight;
31
+ }
32
+ }
33
+
34
+ canvas.width = width;
35
+ canvas.height = height;
36
+ ctx.drawImage(img, 0, 0, width, height);
37
+
38
+ canvas.toBlob(
39
+ (blob) => {
40
+ if (blob) resolve(blob);
41
+ else reject(new Error("Image compression failed."));
42
+ },
43
+ "image/jpeg",
44
+ quality
45
+ );
46
+ };
47
+ img.onerror = () => reject(new Error("Failed to load image."));
48
+ });
49
+ }
50
+
51
+ export async function prepareImage(imageFile: File): Promise<string> {
52
+ const compressedFile = (await resizeImage(imageFile)) as Blob;
53
+ return toBase64(compressedFile);
54
+ }
55
+
56
+ export function toBase64(file: Blob): Promise<string> {
57
+ return new Promise((resolve, reject) => {
58
+ const reader = new FileReader();
59
+ reader.readAsDataURL(file);
60
+ reader.onload = () => resolve((reader.result as string).split(",")[1]);
61
+ reader.onerror = (error) => reject(error);
62
+ });
63
+ }
package/src/index.ts CHANGED
@@ -1,7 +1,16 @@
1
- export * from "./utils/useVerifyAccount";
1
+ export * from "./utils/account/useVerifyAccount";
2
+ export * from "./utils/account/useVerifyWeb3AuthSession";
3
+ export * from "./utils/account/useCreateWeb3Account";
4
+ export * from "./utils/account/useFetchUserData";
5
+ export * from "./utils/account/useUpdateUserEmail";
6
+ export * from "./utils/account/useUpdateUserPid";
7
+
2
8
  export * from "./utils/anymals/useMintAnymalNFT";
3
9
  export * from "./utils/anymals/useAddAnymalToDatabase";
4
10
  export * from "./utils/anymals/useDeleteAnymalFromDatabase";
5
11
  export * from "./utils/anymals/useSaveAnymalMetadata";
6
12
  export * from "./utils/anymals/useUpdateAnymalWithNFT";
13
+ export * from "./utils/anymals/useUploadAnymalImage";
14
+
15
+ export * from "./utils/balance/useFetchBalance";
7
16
  export * from "./types/Anymal";
@@ -0,0 +1,24 @@
1
+ import { useCallback } from "react";
2
+
3
+ export function useCreateWeb3Account() {
4
+ return useCallback(
5
+ async (idToken: string, publicKey: string, authServiceBaseUrl: string) => {
6
+ try {
7
+ const response = await fetch(`${authServiceBaseUrl}/create-account`, {
8
+ method: "POST",
9
+ headers: {
10
+ "Content-Type": "application/json",
11
+ Authorization: "Bearer " + idToken,
12
+ },
13
+ body: JSON.stringify({ appPubKey: publicKey }),
14
+ });
15
+
16
+ return await response.json();
17
+ } catch (error) {
18
+ console.error(error);
19
+ return null;
20
+ }
21
+ },
22
+ []
23
+ );
24
+ }
@@ -0,0 +1,43 @@
1
+ import { useCallback } from "react";
2
+
3
+ export function useFetchUserData() {
4
+ return useCallback(async (dbAuthToken: string, endpoint: string) => {
5
+ try {
6
+ const query = `
7
+ query User {
8
+ User {
9
+ _docID
10
+ pid
11
+ email
12
+ isVerified
13
+ accountType
14
+ }
15
+ }
16
+ `;
17
+
18
+ const response = await fetch(endpoint, {
19
+ method: "POST",
20
+ headers: {
21
+ "Content-Type": "application/json",
22
+ Authorization: `Bearer ${dbAuthToken}`,
23
+ },
24
+ body: JSON.stringify({ query }),
25
+ });
26
+
27
+ if (!response.ok) {
28
+ throw new Error(`HTTP error! Status: ${response.status}`);
29
+ }
30
+
31
+ const data = await response.json();
32
+
33
+ if (data && data.data && data.data.User && data.data.User.length > 0) {
34
+ return data.data.User[0];
35
+ } else {
36
+ return null;
37
+ }
38
+ } catch (error) {
39
+ console.error("Error fetching user data:", error);
40
+ return null;
41
+ }
42
+ }, []);
43
+ }
@@ -0,0 +1,48 @@
1
+ import { useCallback } from "react";
2
+
3
+ export function useUpdateUserEmail() {
4
+ return useCallback(
5
+ async (
6
+ dbAuthToken: string,
7
+ docID: string,
8
+ email: string,
9
+ endpoint: string
10
+ ) => {
11
+ try {
12
+ const mutation = `
13
+ mutation Update_User($docID: [ID], $input: UserMutationInputArg) {
14
+ update_User(docID: $docID, input: $input) {
15
+ email
16
+ }
17
+ }
18
+ `;
19
+
20
+ const variables = {
21
+ docId: [docID],
22
+ input: {
23
+ email: email,
24
+ },
25
+ };
26
+
27
+ const response = await fetch(endpoint, {
28
+ method: "POST",
29
+ headers: {
30
+ "Content-Type": "application/json",
31
+ Authorization: `Bearer ${dbAuthToken}`,
32
+ },
33
+ body: JSON.stringify({
34
+ query: mutation,
35
+ variables,
36
+ }),
37
+ });
38
+
39
+ if (!response.ok) {
40
+ throw new Error(`HTTP error! Status: ${response.status}`);
41
+ }
42
+ } catch (error) {
43
+ console.error("Error updating email:", error);
44
+ }
45
+ },
46
+ []
47
+ );
48
+ }
@@ -0,0 +1,48 @@
1
+ import { useCallback } from "react";
2
+
3
+ export function useUpdateUserPid() {
4
+ return useCallback(
5
+ async (
6
+ dbAuthToken: string,
7
+ docID: string,
8
+ pid: string,
9
+ endpoint: string
10
+ ) => {
11
+ try {
12
+ const mutation = `
13
+ mutation Update_User($docID: [ID], $input: UserMutationInputArg) {
14
+ update_User(docID: $docID, input: $input) {
15
+ pid
16
+ }
17
+ }
18
+ `;
19
+
20
+ const variables = {
21
+ docId: [docID],
22
+ input: {
23
+ pid: pid,
24
+ },
25
+ };
26
+
27
+ const response = await fetch(endpoint, {
28
+ method: "POST",
29
+ headers: {
30
+ "Content-Type": "application/json",
31
+ Authorization: `Bearer ${dbAuthToken}`,
32
+ },
33
+ body: JSON.stringify({
34
+ query: mutation,
35
+ variables,
36
+ }),
37
+ });
38
+
39
+ if (!response.ok) {
40
+ throw new Error(`HTTP error! Status: ${response.status}`);
41
+ }
42
+ } catch (error) {
43
+ console.error("Error updating email:", error);
44
+ }
45
+ },
46
+ []
47
+ );
48
+ }
@@ -1,6 +1,6 @@
1
1
  import { useCallback } from "react";
2
2
  import { encodeFunctionData, parseGwei } from "viem";
3
- import { VERIFY_ACCOUNT_ABI } from "../helpers/BlockchainAbiHelper";
3
+ import { VERIFY_ACCOUNT_ABI } from "../../helpers/BlockchainAbiHelper";
4
4
 
5
5
  export function useVerifyAccount() {
6
6
  return useCallback(
@@ -0,0 +1,24 @@
1
+ import { useCallback } from "react";
2
+
3
+ export function useVerifyWeb3AuthSession() {
4
+ return useCallback(
5
+ async (idToken: string, publicKey: string, authServiceBaseUrl: string) => {
6
+ const response = await fetch(
7
+ `${authServiceBaseUrl}/verify-web3-session`,
8
+ {
9
+ method: "POST",
10
+ headers: {
11
+ "Content-Type": "application/json",
12
+ Authorization: "Bearer " + idToken,
13
+ },
14
+ body: JSON.stringify({ appPubKey: publicKey }),
15
+ }
16
+ );
17
+
18
+ const { jwt } = await response.json();
19
+
20
+ return jwt;
21
+ },
22
+ []
23
+ );
24
+ }
@@ -0,0 +1,70 @@
1
+ import { useCallback } from "react";
2
+ import { prepareImage } from "../../helpers/UploadImageHelper";
3
+
4
+ export function useUploadAnymalImage() {
5
+ return useCallback(
6
+ async (
7
+ imageFile: File,
8
+ type: string,
9
+ idToken: string,
10
+ publicKey: string,
11
+ authServiceBaseUrl: string
12
+ ) => {
13
+ if (!imageFile || !idToken) {
14
+ return {
15
+ success: false,
16
+ message: "No image file selected or not authenticated.",
17
+ url: null,
18
+ type,
19
+ };
20
+ }
21
+
22
+ try {
23
+ const base64Image = await prepareImage(imageFile);
24
+
25
+ const payload = {
26
+ appPubKey: publicKey,
27
+ imageData: base64Image,
28
+ imageName: imageFile.name.split(".")[0],
29
+ };
30
+
31
+ const response = await fetch(`${authServiceBaseUrl}/process-image`, {
32
+ method: "POST",
33
+ headers: {
34
+ "Content-Type": "application/json",
35
+ Authorization: `Bearer ${idToken}`,
36
+ },
37
+ body: JSON.stringify(payload),
38
+ });
39
+
40
+ const data = await response.json();
41
+
42
+ if (response.ok) {
43
+ return {
44
+ success: true,
45
+ message: "Image uploaded successfully.",
46
+ url: data.ipfsUrl,
47
+ type,
48
+ };
49
+ } else {
50
+ return {
51
+ success: false,
52
+ message: "Image failed to upload.",
53
+ url: null,
54
+ type,
55
+ };
56
+ }
57
+ } catch (error) {
58
+ return {
59
+ success: false,
60
+ message: `An error occurred during image upload: ${
61
+ (error as Error).message
62
+ }`,
63
+ url: null,
64
+ type,
65
+ };
66
+ }
67
+ },
68
+ []
69
+ );
70
+ }
@@ -0,0 +1,26 @@
1
+ import { useCallback } from "react";
2
+ import { erc20Abi, getAddress } from "viem";
3
+
4
+ export function useFetchBalance() {
5
+ return useCallback(
6
+ async (
7
+ publicClient: any,
8
+ walletAddress: string,
9
+ kibbleTokenAddress: string
10
+ ) => {
11
+ try {
12
+ const balance = await publicClient.readContract({
13
+ address: getAddress(kibbleTokenAddress),
14
+ abi: erc20Abi,
15
+ functionName: "balanceOf",
16
+ args: [getAddress(walletAddress)],
17
+ });
18
+
19
+ return Number(balance);
20
+ } catch (error) {
21
+ console.error("Failed to fetch token balance:", error);
22
+ }
23
+ },
24
+ []
25
+ );
26
+ }