@show-karma/karma-gap-sdk 0.4.16 → 0.4.18

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.
Files changed (188) hide show
  1. package/core/abi/AirdropNFT.json +1 -1
  2. package/core/abi/Allo.json +860 -860
  3. package/core/abi/AlloRegistry.json +578 -578
  4. package/core/abi/CommunityResolverABI.json +506 -506
  5. package/core/abi/Donations.json +251 -251
  6. package/core/abi/EAS.json +1 -1
  7. package/core/abi/MultiAttester.json +746 -746
  8. package/core/abi/ProjectResolver.json +574 -574
  9. package/core/abi/SchemaRegistry.json +1 -1
  10. package/core/abi/index.d.ts +1114 -0
  11. package/core/abi/index.js +26 -0
  12. package/core/class/AllGapSchemas.d.ts +9 -0
  13. package/core/class/AllGapSchemas.js +19 -0
  14. package/core/class/Attestation.d.ts +173 -0
  15. package/core/class/Attestation.js +333 -0
  16. package/core/class/Fetcher.d.ts +175 -0
  17. package/core/class/Fetcher.js +13 -0
  18. package/core/class/GAP.d.ts +254 -0
  19. package/core/class/GAP.js +289 -0
  20. package/core/class/GapSchema.d.ts +34 -0
  21. package/core/class/GapSchema.js +62 -0
  22. package/core/class/Gelato/{Gelato.ts → Gelato.js} +0 -23
  23. package/core/class/GrantProgramRegistry/Allo.d.ts +17 -0
  24. package/core/class/GrantProgramRegistry/Allo.js +137 -0
  25. package/core/class/GrantProgramRegistry/AlloRegistry.d.ts +15 -0
  26. package/core/class/GrantProgramRegistry/AlloRegistry.js +70 -0
  27. package/core/class/GraphQL/AxiosGQL.d.ts +6 -0
  28. package/core/class/GraphQL/AxiosGQL.js +25 -0
  29. package/core/class/GraphQL/EASClient.d.ts +16 -0
  30. package/core/class/GraphQL/EASClient.js +26 -0
  31. package/core/class/GraphQL/GapEasClient.d.ts +71 -0
  32. package/core/class/GraphQL/GapEasClient.js +451 -0
  33. package/core/class/GraphQL/index.js +19 -0
  34. package/core/class/Schema.d.ts +233 -0
  35. package/core/class/Schema.js +490 -0
  36. package/core/class/SchemaError.d.ts +30 -0
  37. package/core/class/SchemaError.js +39 -0
  38. package/core/class/contract/GapContract.d.ts +102 -0
  39. package/core/class/contract/GapContract.js +285 -0
  40. package/core/class/entities/Community.d.ts +34 -0
  41. package/core/class/entities/Community.js +109 -0
  42. package/core/class/entities/ContributorProfile.d.ts +41 -0
  43. package/core/class/entities/ContributorProfile.js +69 -0
  44. package/core/class/entities/Grant.d.ts +54 -0
  45. package/core/class/entities/Grant.js +223 -0
  46. package/core/class/entities/GrantUpdate.d.ts +62 -0
  47. package/core/class/entities/GrantUpdate.js +114 -0
  48. package/core/class/entities/MemberOf.d.ts +11 -0
  49. package/core/class/entities/MemberOf.js +33 -0
  50. package/core/class/entities/Milestone.d.ts +168 -0
  51. package/core/class/entities/Milestone.js +657 -0
  52. package/core/class/entities/Project.d.ts +92 -0
  53. package/core/class/entities/Project.js +418 -0
  54. package/core/class/entities/ProjectImpact.d.ts +50 -0
  55. package/core/class/entities/ProjectImpact.js +112 -0
  56. package/core/class/entities/ProjectMilestone.d.ts +60 -0
  57. package/core/class/entities/ProjectMilestone.js +174 -0
  58. package/core/class/entities/ProjectPointer.d.ts +12 -0
  59. package/core/class/entities/ProjectPointer.js +22 -0
  60. package/core/class/entities/ProjectUpdate.d.ts +50 -0
  61. package/core/class/entities/ProjectUpdate.js +110 -0
  62. package/core/class/entities/Track.d.ts +16 -0
  63. package/core/class/entities/Track.js +21 -0
  64. package/core/class/entities/index.js +26 -0
  65. package/core/class/index.js +26 -0
  66. package/core/class/karma-indexer/GapIndexerClient.d.ts +66 -0
  67. package/core/class/karma-indexer/GapIndexerClient.js +207 -0
  68. package/core/class/karma-indexer/api/GapIndexerApi.d.ts +73 -0
  69. package/core/class/karma-indexer/api/GapIndexerApi.js +256 -0
  70. package/core/class/karma-indexer/api/types.d.ts +309 -0
  71. package/core/class/karma-indexer/api/types.js +2 -0
  72. package/core/class/remote-storage/IpfsStorage.d.ts +23 -0
  73. package/core/class/remote-storage/IpfsStorage.js +56 -0
  74. package/core/class/remote-storage/RemoteStorage.d.ts +41 -0
  75. package/core/class/remote-storage/RemoteStorage.js +38 -0
  76. package/core/class/types/allo.d.ts +78 -0
  77. package/core/class/types/allo.js +2 -0
  78. package/core/class/types/attestations.d.ts +173 -0
  79. package/core/class/types/attestations.js +66 -0
  80. package/core/consts.d.ts +50 -0
  81. package/core/consts.js +699 -0
  82. package/core/index.js +24 -0
  83. package/core/shared/types.d.ts +13 -0
  84. package/core/shared/types.js +2 -0
  85. package/core/types.d.ts +131 -0
  86. package/core/types.js +13 -0
  87. package/core/utils/gelato/index.js +19 -0
  88. package/core/utils/gelato/send-gelato-txn.d.ts +55 -0
  89. package/core/utils/gelato/send-gelato-txn.js +100 -0
  90. package/core/utils/gelato/sponsor-handler.d.ts +9 -0
  91. package/core/utils/gelato/sponsor-handler.js +60 -0
  92. package/core/utils/gelato/watch-gelato-txn.d.ts +7 -0
  93. package/core/utils/gelato/watch-gelato-txn.js +63 -0
  94. package/core/utils/get-date.d.ts +1 -0
  95. package/core/utils/get-date.js +7 -0
  96. package/core/utils/get-ipfs-data.d.ts +1 -0
  97. package/core/utils/get-ipfs-data.js +20 -0
  98. package/core/utils/get-web3-provider.d.ts +2 -0
  99. package/core/utils/get-web3-provider.js +18 -0
  100. package/core/utils/gql-queries.d.ts +12 -0
  101. package/core/utils/gql-queries.js +90 -0
  102. package/core/utils/index.js +23 -0
  103. package/core/utils/map-filter.d.ts +8 -0
  104. package/core/utils/map-filter.js +20 -0
  105. package/core/utils/serialize-bigint.d.ts +1 -0
  106. package/core/utils/serialize-bigint.js +8 -0
  107. package/core/utils/to-unix.d.ts +1 -0
  108. package/core/utils/to-unix.js +25 -0
  109. package/index.js +17 -0
  110. package/package.json +45 -45
  111. package/.cursorrules +0 -43
  112. package/core/abi/index.ts +0 -21
  113. package/core/class/AllGapSchemas.ts +0 -21
  114. package/core/class/Attestation.ts +0 -429
  115. package/core/class/Fetcher.ts +0 -224
  116. package/core/class/GAP.ts +0 -481
  117. package/core/class/GapSchema.ts +0 -93
  118. package/core/class/GrantProgramRegistry/Allo.ts +0 -188
  119. package/core/class/GrantProgramRegistry/AlloRegistry.ts +0 -101
  120. package/core/class/GraphQL/AxiosGQL.ts +0 -29
  121. package/core/class/GraphQL/EASClient.ts +0 -34
  122. package/core/class/GraphQL/GapEasClient.ts +0 -869
  123. package/core/class/Schema.ts +0 -659
  124. package/core/class/SchemaError.ts +0 -42
  125. package/core/class/contract/GapContract.ts +0 -457
  126. package/core/class/entities/Community.ts +0 -148
  127. package/core/class/entities/ContributorProfile.ts +0 -108
  128. package/core/class/entities/Grant.ts +0 -321
  129. package/core/class/entities/GrantUpdate.ts +0 -187
  130. package/core/class/entities/MemberOf.ts +0 -52
  131. package/core/class/entities/Milestone.ts +0 -898
  132. package/core/class/entities/Project.ts +0 -672
  133. package/core/class/entities/ProjectImpact.ts +0 -170
  134. package/core/class/entities/ProjectMilestone.ts +0 -254
  135. package/core/class/entities/ProjectPointer.ts +0 -39
  136. package/core/class/entities/ProjectUpdate.ts +0 -176
  137. package/core/class/entities/Track.ts +0 -32
  138. package/core/class/karma-indexer/GapIndexerClient.ts +0 -383
  139. package/core/class/karma-indexer/api/GapIndexerApi.ts +0 -446
  140. package/core/class/karma-indexer/api/types.ts +0 -313
  141. package/core/class/remote-storage/IpfsStorage.ts +0 -76
  142. package/core/class/remote-storage/RemoteStorage.ts +0 -65
  143. package/core/class/types/allo.ts +0 -93
  144. package/core/class/types/attestations.ts +0 -223
  145. package/core/consts.ts +0 -775
  146. package/core/scripts/create-grant.ts +0 -102
  147. package/core/scripts/create-program.ts +0 -43
  148. package/core/scripts/create-schemas.ts +0 -65
  149. package/core/scripts/deploy.ts +0 -65
  150. package/core/scripts/index.ts +0 -1
  151. package/core/scripts/milestone-multi-grants.ts +0 -125
  152. package/core/shared/types.ts +0 -13
  153. package/core/types.ts +0 -224
  154. package/core/utils/gelato/send-gelato-txn.ts +0 -114
  155. package/core/utils/gelato/sponsor-handler.ts +0 -77
  156. package/core/utils/gelato/watch-gelato-txn.ts +0 -67
  157. package/core/utils/get-date.ts +0 -3
  158. package/core/utils/get-ipfs-data.ts +0 -13
  159. package/core/utils/get-web3-provider.ts +0 -18
  160. package/core/utils/gql-queries.ts +0 -133
  161. package/core/utils/map-filter.ts +0 -21
  162. package/core/utils/serialize-bigint.ts +0 -7
  163. package/core/utils/to-unix.ts +0 -18
  164. package/create-community-example.ts +0 -119
  165. package/csv-upload/README.md +0 -74
  166. package/csv-upload/config.ts +0 -41
  167. package/csv-upload/example.csv +0 -2
  168. package/csv-upload/keys.example.json +0 -8
  169. package/csv-upload/scripts/run.ts +0 -417
  170. package/csv-upload/types.ts +0 -39
  171. package/docs/.gitkeep +0 -0
  172. package/docs/images/attestation-architecture.png +0 -0
  173. package/docs/images/dfd-get-projects.png +0 -0
  174. package/gap-schema.yaml +0 -155
  175. package/milestone-workflow-example.ts +0 -353
  176. package/readme.md +0 -872
  177. package/schemas/.gitkeep +0 -0
  178. package/schemas/GAP-schemas-1692135812877.json +0 -33
  179. package/test-file-indexer-api.ts +0 -25
  180. package/tsconfig.json +0 -26
  181. /package/{csv-upload/.gitkeep → core/class/Gelato/Gelato.d.ts} +0 -0
  182. /package/core/class/GraphQL/{index.ts → index.d.ts} +0 -0
  183. /package/core/class/entities/{index.ts → index.d.ts} +0 -0
  184. /package/core/class/{index.ts → index.d.ts} +0 -0
  185. /package/core/{index.ts → index.d.ts} +0 -0
  186. /package/core/utils/gelato/{index.ts → index.d.ts} +0 -0
  187. /package/core/utils/{index.ts → index.d.ts} +0 -0
  188. /package/{index.ts → index.d.ts} +0 -0
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.abis = void 0;
7
+ const AirdropNFT_json_1 = __importDefault(require("./AirdropNFT.json"));
8
+ const Allo_json_1 = __importDefault(require("./Allo.json"));
9
+ const AlloRegistry_json_1 = __importDefault(require("./AlloRegistry.json"));
10
+ const CommunityResolverABI_json_1 = __importDefault(require("./CommunityResolverABI.json"));
11
+ const Donations_json_1 = __importDefault(require("./Donations.json"));
12
+ const EAS_json_1 = __importDefault(require("./EAS.json"));
13
+ const MultiAttester_json_1 = __importDefault(require("./MultiAttester.json"));
14
+ const ProjectResolver_json_1 = __importDefault(require("./ProjectResolver.json"));
15
+ const SchemaRegistry_json_1 = __importDefault(require("./SchemaRegistry.json"));
16
+ exports.abis = {
17
+ AirdropNFT: AirdropNFT_json_1.default,
18
+ Allo: Allo_json_1.default,
19
+ AlloRegistry: AlloRegistry_json_1.default,
20
+ CommunityResolverABI: CommunityResolverABI_json_1.default,
21
+ Donations: Donations_json_1.default,
22
+ EAS: EAS_json_1.default,
23
+ MultiAttester: MultiAttester_json_1.default,
24
+ ProjectResolver: ProjectResolver_json_1.default,
25
+ SchemaRegistry: SchemaRegistry_json_1.default,
26
+ };
@@ -0,0 +1,9 @@
1
+ import { SchemaInterface, TNetwork, TSchemaName } from "../types";
2
+ import { GapSchema } from "./GapSchema";
3
+ export declare class AllGapSchemas {
4
+ allSchemas: {
5
+ [network: string]: SchemaInterface<TSchemaName>[];
6
+ };
7
+ constructor();
8
+ findSchema(name: TSchemaName, network: TNetwork): GapSchema;
9
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AllGapSchemas = void 0;
4
+ const consts_1 = require("../../core/consts");
5
+ const GAP_1 = require("./GAP");
6
+ const GapSchema_1 = require("./GapSchema");
7
+ class AllGapSchemas {
8
+ constructor() {
9
+ this.allSchemas = {};
10
+ Object.keys(consts_1.Networks).forEach((network) => {
11
+ this.allSchemas[network] = Object.values((0, consts_1.MountEntities)(consts_1.Networks[network]));
12
+ });
13
+ }
14
+ findSchema(name, network) {
15
+ const schema = this.allSchemas[network].find((s) => s.name === name);
16
+ return new GapSchema_1.GapSchema(schema, GAP_1.GAP.getInstance({ network }), false, false);
17
+ }
18
+ }
19
+ exports.AllGapSchemas = AllGapSchemas;
@@ -0,0 +1,173 @@
1
+ import { Hex, IAttestation, JSONStr, MultiAttestData, SignerOrProvider, TNetwork } from "../types";
2
+ import { Schema } from "./Schema";
3
+ import { SchemaItem, SchemaValue } from "@ethereum-attestation-service/eas-sdk";
4
+ import { GapSchema } from "./GapSchema";
5
+ import { AttestationWithTx } from "./types/attestations";
6
+ export interface AttestationArgs<T = unknown, S extends Schema = Schema> {
7
+ data: T | string;
8
+ schema: S;
9
+ uid?: Hex;
10
+ refUID?: Hex;
11
+ attester?: Hex;
12
+ recipient: Hex;
13
+ revoked?: boolean;
14
+ revocationTime?: Date | number;
15
+ createdAt?: Date | number;
16
+ chainID?: number;
17
+ }
18
+ /**
19
+ * Represents the EAS Attestation and provides methods to manage attestations.
20
+ * @example
21
+ *
22
+ * ```ts
23
+ * const grantee = new Attestation({
24
+ * schema: Schema.get("Grantee", "network-name"), // Use this.schema.gap.findSchema("SchemaName") if using default GAP schemas
25
+ * data: { grantee: true },
26
+ * uid: "0xabc123",
27
+ * });
28
+ *
29
+ * const granteeDetails = new Attestation({
30
+ * schema: Schema.get("GranteeDetails", "optimism"),
31
+ * data: {
32
+ * name: "John Doe",
33
+ * description: "A description",
34
+ * imageURL: "https://example.com/image.png",
35
+ * },
36
+ * uid: "0xab234"
37
+ * );
38
+ *
39
+ * // Return the refferenced attestation
40
+ * const ref = granteeDetails.reference<Grantee>();
41
+ *
42
+ * // Revoke attestation
43
+ * granteeDetails.revoke();
44
+ *
45
+ * // Get attestation data from a decoded JSON string.
46
+ * granteeDetails.fromDecodedSchema(granteeDetails.data);
47
+ * ```
48
+ */
49
+ export declare class Attestation<T = unknown, S extends Schema = GapSchema> implements AttestationArgs<T, S> {
50
+ readonly schema: S;
51
+ private _data;
52
+ protected _uid: Hex;
53
+ readonly refUID?: Hex;
54
+ readonly attester?: Hex;
55
+ readonly recipient: Hex;
56
+ readonly revoked?: boolean;
57
+ readonly revocationTime?: Date;
58
+ readonly createdAt: Date;
59
+ private _chainID;
60
+ private _reference?;
61
+ constructor(args: AttestationArgs<T, S>);
62
+ /**
63
+ * Encodes the schema.
64
+ * @returns
65
+ */
66
+ encodeSchema(schema: SchemaItem[]): string;
67
+ /**
68
+ * Sets a field in the schema.
69
+ */
70
+ setValue<K extends keyof T>(key: K, value: SchemaValue): void;
71
+ /**
72
+ * Set attestation values to be uploaded.
73
+ * @param values
74
+ */
75
+ setValues(values: T): void;
76
+ /**
77
+ * Returns the referenced attestation
78
+ */
79
+ reference<Ref = unknown, RefSchema extends Schema = Schema>(): Attestation<Ref, RefSchema>;
80
+ /**
81
+ * Returns the attestation data as a JSON string.
82
+ * @param data
83
+ * @returns
84
+ */
85
+ fromDecodedSchema(data: T | JSONStr): T;
86
+ /**
87
+ * Revokes this attestation.
88
+ * @param eas
89
+ * @param signer
90
+ * @returns
91
+ */
92
+ revoke(signer: SignerOrProvider, callback?: Function): Promise<AttestationWithTx>;
93
+ /**
94
+ * Attests the data using the specified signer and schema.
95
+ * @param signer - The signer or provider to use for attestation.
96
+ * @param args - Additional arguments to pass to the schema's `attest` method.
97
+ * @returns A Promise that resolves to the UID of the attestation.
98
+ * @throws An `AttestationError` if an error occurs during attestation.
99
+ */
100
+ attest(signer: SignerOrProvider, ...args: unknown[]): Promise<AttestationWithTx>;
101
+ /**
102
+ * Validates the payload.
103
+ *
104
+ * If an attestation should have anything
105
+ * specifically explicit, it should be implemented in
106
+ * order to avoid errors.
107
+ * @returns
108
+ */
109
+ protected assertPayload(): boolean;
110
+ /**
111
+ * Get the multi attestation payload for the referred index.
112
+ *
113
+ * The index should be the array position this payload wants
114
+ * to reference.
115
+ *
116
+ * E.g:
117
+ *
118
+ * 1. Project is index 0;
119
+ * 2. Project details is index 1;
120
+ * 3. Grant is index 2;
121
+ * 4. Grant details is index 3;
122
+ * 5. Milestone is index 4;
123
+ *
124
+ * `[Project, projectDetails, grant, grantDetails, milestone]`
125
+ *
126
+ * -> Project.payloadFor(0); // refs itself (no effect)
127
+ *
128
+ * -> project.details.payloadFor(0); // ref project
129
+ *
130
+ * -> grant.payloadFor(0); // ref project
131
+ *
132
+ * -> grant.details.payloadFor(2); // ref grant
133
+ *
134
+ * -> milestone.payloadFor(2); // ref grant
135
+ *
136
+ *
137
+ * @param refIdx
138
+ * @returns [Encoded payload, Raw payload]
139
+ */
140
+ payloadFor(refIdx: number): Promise<{
141
+ payload: MultiAttestData;
142
+ raw: MultiAttestData;
143
+ }>;
144
+ /**
145
+ * Returns an Attestation instance from a JSON decoded schema.
146
+ * @param data
147
+ * @returns
148
+ */
149
+ static fromDecodedSchema<T>(data: JSONStr): T;
150
+ /**
151
+ * Transform attestation interface-based into class-based.
152
+ */
153
+ static fromInterface<T extends Attestation = Attestation>(attestations: IAttestation[], network: TNetwork): T[];
154
+ /**
155
+ * Asserts if schema is valid.
156
+ * > Does not check refUID if `strict = false`. To check refUID use `Schema.validate()`
157
+ * @param args
158
+ */
159
+ protected assert(args: AttestationArgs, strict?: boolean): void;
160
+ get chainID(): number;
161
+ get data(): T;
162
+ get uid(): Hex;
163
+ set uid(uid: Hex);
164
+ /**
165
+ * Create attestation to serve as Attestation data.
166
+ * @param data Data to attest
167
+ * @param schema selected schema
168
+ * @param from attester
169
+ * @param to recipient
170
+ * @returns
171
+ */
172
+ static factory<T = unknown>(data: T, schema: Schema, from: Hex, to: Hex): Attestation<T, Schema<string>>;
173
+ }
@@ -0,0 +1,333 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Attestation = void 0;
4
+ const Schema_1 = require("./Schema");
5
+ const SchemaError_1 = require("./SchemaError");
6
+ const get_date_1 = require("../utils/get-date");
7
+ const consts_1 = require("../consts");
8
+ const GapContract_1 = require("./contract/GapContract");
9
+ /**
10
+ * Represents the EAS Attestation and provides methods to manage attestations.
11
+ * @example
12
+ *
13
+ * ```ts
14
+ * const grantee = new Attestation({
15
+ * schema: Schema.get("Grantee", "network-name"), // Use this.schema.gap.findSchema("SchemaName") if using default GAP schemas
16
+ * data: { grantee: true },
17
+ * uid: "0xabc123",
18
+ * });
19
+ *
20
+ * const granteeDetails = new Attestation({
21
+ * schema: Schema.get("GranteeDetails", "optimism"),
22
+ * data: {
23
+ * name: "John Doe",
24
+ * description: "A description",
25
+ * imageURL: "https://example.com/image.png",
26
+ * },
27
+ * uid: "0xab234"
28
+ * );
29
+ *
30
+ * // Return the refferenced attestation
31
+ * const ref = granteeDetails.reference<Grantee>();
32
+ *
33
+ * // Revoke attestation
34
+ * granteeDetails.revoke();
35
+ *
36
+ * // Get attestation data from a decoded JSON string.
37
+ * granteeDetails.fromDecodedSchema(granteeDetails.data);
38
+ * ```
39
+ */
40
+ class Attestation {
41
+ constructor(args) {
42
+ this.schema = args.schema;
43
+ this._data = this.fromDecodedSchema(args.data);
44
+ this.setValues(this._data);
45
+ this._uid = args.uid || consts_1.nullRef;
46
+ this.refUID = args.refUID || consts_1.nullRef;
47
+ this.attester = args.attester;
48
+ this.recipient = args.recipient;
49
+ this.revoked = args.revoked;
50
+ this.revocationTime = (0, get_date_1.getDate)(args.revocationTime);
51
+ this.createdAt = (0, get_date_1.getDate)(args.createdAt || Date.now() / 1000);
52
+ this._chainID = args.chainID || consts_1.Networks[this.schema.gap.network].chainId;
53
+ }
54
+ /**
55
+ * Encodes the schema.
56
+ * @returns
57
+ */
58
+ encodeSchema(schema) {
59
+ return this.schema.encode(schema);
60
+ }
61
+ /**
62
+ * Sets a field in the schema.
63
+ */
64
+ setValue(key, value) {
65
+ this.schema.setValue(key, value);
66
+ }
67
+ /**
68
+ * Set attestation values to be uploaded.
69
+ * @param values
70
+ */
71
+ setValues(values) {
72
+ const isJsonSchema = this.schema.isJsonSchema();
73
+ if (isJsonSchema)
74
+ this.schema.setValue("json", JSON.stringify(values));
75
+ this._data = values;
76
+ Object.entries(values).forEach(([key, value]) => {
77
+ this[key] = value;
78
+ if (!isJsonSchema)
79
+ this.setValue(key, value.value || value);
80
+ });
81
+ }
82
+ /**
83
+ * Returns the referenced attestation
84
+ */
85
+ reference() {
86
+ return this._reference;
87
+ }
88
+ /**
89
+ * Returns the attestation data as a JSON string.
90
+ * @param data
91
+ * @returns
92
+ */
93
+ fromDecodedSchema(data) {
94
+ return typeof data === "string"
95
+ ? Attestation.fromDecodedSchema(data)
96
+ : data;
97
+ }
98
+ /**
99
+ * Revokes this attestation.
100
+ * @param eas
101
+ * @param signer
102
+ * @returns
103
+ */
104
+ async revoke(signer, callback) {
105
+ try {
106
+ callback?.("preparing");
107
+ const { tx } = await GapContract_1.GapContract.multiRevoke(signer, [
108
+ {
109
+ data: [
110
+ {
111
+ uid: this.uid,
112
+ value: 0n,
113
+ },
114
+ ],
115
+ schema: this.schema.uid,
116
+ },
117
+ ]).then((res) => {
118
+ callback?.("confirmed");
119
+ return res;
120
+ });
121
+ return { tx, uids: [this.uid] };
122
+ }
123
+ catch (error) {
124
+ console.error(error);
125
+ throw new SchemaError_1.SchemaError("REVOKE_ERROR", "Error revoking attestation.", error);
126
+ }
127
+ }
128
+ /**
129
+ * Attests the data using the specified signer and schema.
130
+ * @param signer - The signer or provider to use for attestation.
131
+ * @param args - Additional arguments to pass to the schema's `attest` method.
132
+ * @returns A Promise that resolves to the UID of the attestation.
133
+ * @throws An `AttestationError` if an error occurs during attestation.
134
+ */
135
+ async attest(signer, ...args) {
136
+ const callback = typeof args[args.length - 1] === "function"
137
+ ? args.pop()
138
+ : null;
139
+ console.log(`Attesting ${this.schema.name}`);
140
+ try {
141
+ const { tx, uids } = await this.schema.attest({
142
+ data: this.data,
143
+ to: this.recipient,
144
+ refUID: this.refUID,
145
+ signer,
146
+ callback: callback,
147
+ });
148
+ this._uid = uids[0];
149
+ console.log(`Attested ${this.schema.name} with UID ${this.uid}`);
150
+ return { tx, uids };
151
+ }
152
+ catch (error) {
153
+ console.error(error);
154
+ throw new SchemaError_1.AttestationError("ATTEST_ERROR", "Error during attestation.", error);
155
+ }
156
+ }
157
+ /**
158
+ * Validates the payload.
159
+ *
160
+ * If an attestation should have anything
161
+ * specifically explicit, it should be implemented in
162
+ * order to avoid errors.
163
+ * @returns
164
+ */
165
+ assertPayload() {
166
+ return true;
167
+ }
168
+ /**
169
+ * Get the multi attestation payload for the referred index.
170
+ *
171
+ * The index should be the array position this payload wants
172
+ * to reference.
173
+ *
174
+ * E.g:
175
+ *
176
+ * 1. Project is index 0;
177
+ * 2. Project details is index 1;
178
+ * 3. Grant is index 2;
179
+ * 4. Grant details is index 3;
180
+ * 5. Milestone is index 4;
181
+ *
182
+ * `[Project, projectDetails, grant, grantDetails, milestone]`
183
+ *
184
+ * -> Project.payloadFor(0); // refs itself (no effect)
185
+ *
186
+ * -> project.details.payloadFor(0); // ref project
187
+ *
188
+ * -> grant.payloadFor(0); // ref project
189
+ *
190
+ * -> grant.details.payloadFor(2); // ref grant
191
+ *
192
+ * -> milestone.payloadFor(2); // ref grant
193
+ *
194
+ *
195
+ * @param refIdx
196
+ * @returns [Encoded payload, Raw payload]
197
+ */
198
+ async payloadFor(refIdx) {
199
+ this.assertPayload();
200
+ if (this.schema.isJsonSchema()) {
201
+ if (this.type) {
202
+ this._data.type = this.type;
203
+ this.schema.setValue("json", JSON.stringify(this._data));
204
+ }
205
+ }
206
+ const payload = (encode = true) => ({
207
+ uid: consts_1.nullRef,
208
+ refIdx,
209
+ multiRequest: {
210
+ schema: this.schema.uid,
211
+ data: [
212
+ {
213
+ refUID: this.refUID,
214
+ expirationTime: 0n,
215
+ revocable: this.schema.revocable || true,
216
+ value: 0n,
217
+ data: (encode ? this.schema.encode() : this.schema.schema),
218
+ recipient: this.recipient,
219
+ },
220
+ ],
221
+ },
222
+ });
223
+ return {
224
+ payload: payload(),
225
+ raw: payload(false),
226
+ };
227
+ }
228
+ /**
229
+ * Returns an Attestation instance from a JSON decoded schema.
230
+ * @param data
231
+ * @returns
232
+ */
233
+ static fromDecodedSchema(data) {
234
+ try {
235
+ const parsed = JSON.parse(data);
236
+ if (data.length < 2 && !/\{.*\}/gim.test(data))
237
+ return {};
238
+ if (parsed.length === 1 && parsed[0].name === "json") {
239
+ const { value } = parsed[0];
240
+ return (typeof value.value === "string"
241
+ ? JSON.parse(value.value)
242
+ : value.value);
243
+ }
244
+ if (parsed && Array.isArray(parsed)) {
245
+ return parsed.reduce((acc, curr) => {
246
+ const { value } = curr.value;
247
+ if (curr.type.includes("uint")) {
248
+ acc[curr.name] = ["string", "bigint"].includes(typeof value)
249
+ ? BigInt(value)
250
+ : Number(value);
251
+ }
252
+ else
253
+ acc[curr.name] = value;
254
+ return acc;
255
+ }, {});
256
+ }
257
+ throw new SchemaError_1.SchemaError("INVALID_DATA", "Data must be a valid JSON array string.");
258
+ }
259
+ catch (error) {
260
+ console.error(error);
261
+ throw new SchemaError_1.SchemaError("INVALID_DATA", "Data must be a valid JSON string.", error);
262
+ }
263
+ }
264
+ /**
265
+ * Transform attestation interface-based into class-based.
266
+ */
267
+ static fromInterface(attestations, network) {
268
+ const result = [];
269
+ attestations.forEach((attestation) => {
270
+ try {
271
+ const schema = Schema_1.Schema.get(attestation.schemaId, network);
272
+ result.push(new Attestation({
273
+ ...attestation,
274
+ schema,
275
+ chainID: consts_1.Networks[network].chainId,
276
+ data: attestation.decodedDataJson,
277
+ }));
278
+ }
279
+ catch (e) {
280
+ console.log(e);
281
+ }
282
+ });
283
+ return result;
284
+ }
285
+ /**
286
+ * Asserts if schema is valid.
287
+ * > Does not check refUID if `strict = false`. To check refUID use `Schema.validate()`
288
+ * @param args
289
+ */
290
+ assert(args, strict = false) {
291
+ const { schema, uid } = args;
292
+ if (!schema || !(schema instanceof Schema_1.Schema)) {
293
+ throw new SchemaError_1.SchemaError("MISSING_FIELD", "Schema must be an array.");
294
+ }
295
+ if (!uid) {
296
+ throw new SchemaError_1.SchemaError("MISSING_FIELD", "Schema uid is required");
297
+ }
298
+ if (strict)
299
+ Schema_1.Schema.validate(this.schema.gap.network);
300
+ }
301
+ get chainID() {
302
+ return this._chainID;
303
+ }
304
+ get data() {
305
+ return this._data;
306
+ }
307
+ get uid() {
308
+ return this._uid;
309
+ }
310
+ set uid(uid) {
311
+ this._uid = uid;
312
+ }
313
+ /**
314
+ * Create attestation to serve as Attestation data.
315
+ * @param data Data to attest
316
+ * @param schema selected schema
317
+ * @param from attester
318
+ * @param to recipient
319
+ * @returns
320
+ */
321
+ static factory(data, schema, from, to) {
322
+ return new Attestation({
323
+ data: data,
324
+ recipient: to,
325
+ attester: from,
326
+ schema,
327
+ uid: "0x0",
328
+ createdAt: new Date(),
329
+ chainID: consts_1.Networks[schema.gap.network].chainId,
330
+ });
331
+ }
332
+ }
333
+ exports.Attestation = Attestation;