@show-karma/karma-gap-sdk 0.3.4 → 0.3.6
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/core/abi/CommunityResolverABI.json +508 -0
- package/core/class/Attestation.js +4 -0
- package/core/class/AttestationIPFS.d.ts +7 -0
- package/core/class/AttestationIPFS.js +10 -0
- package/core/class/GAP.d.ts +7 -0
- package/core/class/GAP.js +14 -0
- package/core/class/GraphQL/Fetcher.d.ts +132 -0
- package/core/class/GraphQL/Fetcher.js +7 -0
- package/core/class/GraphQL/GAPFetcher.d.ts +160 -0
- package/core/class/GraphQL/GAPFetcher.js +516 -0
- package/core/class/IPFS/IPFS.d.ts +13 -0
- package/core/class/IPFS/IPFS.js +24 -0
- package/core/class/contract/MultiAttest.d.ts +10 -0
- package/core/class/contract/MultiAttest.js +19 -0
- package/core/class/entities/Milestone.d.ts +2 -0
- package/core/class/entities/Milestone.js +4 -0
- package/core/class/types/attestations.d.ts +6 -0
- package/core/class/types/attestations.js +6 -0
- package/core/consts.js +4 -34
- package/core/types.d.ts +1 -0
- package/package.json +1 -1
- package/readme.md +34 -39
- package/config/keys.example.json +0 -6
- package/core/abi/EAS.json +0 -1
- package/core/abi/SchemaRegistry.json +0 -1
- package/core/class/Attestation.ts +0 -402
- package/core/class/Fetcher.ts +0 -202
- package/core/class/GAP.ts +0 -398
- package/core/class/GapSchema.ts +0 -90
- package/core/class/Gelato/Gelato.ts +0 -286
- package/core/class/GraphQL/AxiosGQL.ts +0 -29
- package/core/class/GraphQL/EASClient.ts +0 -34
- package/core/class/GraphQL/GapEasClient.ts +0 -845
- package/core/class/GraphQL/index.ts +0 -3
- package/core/class/Schema.ts +0 -609
- package/core/class/SchemaError.ts +0 -36
- package/core/class/contract/GapContract.ts +0 -353
- package/core/class/entities/Community.ts +0 -115
- package/core/class/entities/Grant.ts +0 -309
- package/core/class/entities/MemberOf.ts +0 -42
- package/core/class/entities/Milestone.ts +0 -269
- package/core/class/entities/Project.ts +0 -370
- package/core/class/entities/index.ts +0 -5
- package/core/class/index.ts +0 -10
- package/core/class/karma-indexer/GapIndexerClient.ts +0 -245
- package/core/class/remote-storage/IpfsStorage.ts +0 -51
- package/core/class/remote-storage/RemoteStorage.ts +0 -65
- package/core/class/types/attestations.ts +0 -158
- package/core/consts.ts +0 -282
- package/core/index.ts +0 -7
- package/core/scripts/deploy.ts +0 -67
- package/core/scripts/index.ts +0 -1
- package/core/types.ts +0 -186
- package/core/utils/gelato/index.ts +0 -3
- package/core/utils/gelato/send-gelato-txn.ts +0 -114
- package/core/utils/gelato/sponsor-handler.ts +0 -77
- package/core/utils/gelato/watch-gelato-txn.ts +0 -67
- package/core/utils/get-date.ts +0 -3
- package/core/utils/get-ipfs-data.ts +0 -13
- package/core/utils/get-web3-provider.ts +0 -20
- package/core/utils/gql-queries.ts +0 -133
- package/core/utils/index.ts +0 -7
- package/core/utils/map-filter.ts +0 -21
- package/core/utils/serialize-bigint.ts +0 -7
- package/core/utils/to-unix.ts +0 -18
- package/csv-upload/.gitkeep +0 -0
- package/csv-upload/example.csv +0 -2
- package/csv-upload/scripts/run.ts +0 -193
- package/docs/.gitkeep +0 -0
- package/docs/images/attestation-architecture.png +0 -0
- package/docs/images/dfd-get-projects.png +0 -0
- package/index.ts +0 -1
- package/schemas/.gitkeep +0 -0
- package/schemas/GAP-schemas-1692135812877.json +0 -33
- package/test-file.ts +0 -92
- package/tsconfig.json +0 -26
package/core/class/Schema.ts
DELETED
|
@@ -1,609 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AttestationRequestData,
|
|
3
|
-
OffchainAttestationParams,
|
|
4
|
-
SchemaEncoder,
|
|
5
|
-
SchemaItem,
|
|
6
|
-
SchemaValue,
|
|
7
|
-
} from '@ethereum-attestation-service/eas-sdk';
|
|
8
|
-
import {
|
|
9
|
-
AttestArgs,
|
|
10
|
-
Hex,
|
|
11
|
-
MultiRevokeArgs,
|
|
12
|
-
SchemaInterface,
|
|
13
|
-
TSchemaName,
|
|
14
|
-
SignerOrProvider,
|
|
15
|
-
RawMultiAttestPayload,
|
|
16
|
-
RawAttestationPayload,
|
|
17
|
-
TNetwork,
|
|
18
|
-
} from '../types';
|
|
19
|
-
import { AttestationError, SchemaError } from './SchemaError';
|
|
20
|
-
import { ethers } from 'ethers';
|
|
21
|
-
import { useDefaultAttestation, zeroAddress } from '../consts';
|
|
22
|
-
import { GAP } from './GAP';
|
|
23
|
-
import { Attestation } from './Attestation';
|
|
24
|
-
import { GapContract } from './contract/GapContract';
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Represents the EAS Schema and provides methods to encode and decode the schema,
|
|
28
|
-
* and validate the schema references.
|
|
29
|
-
*
|
|
30
|
-
* Also provides a set of static methods to manage the schema list.
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
* ```
|
|
34
|
-
* // You may or not attribute a schema to a variable.
|
|
35
|
-
* new Schema({
|
|
36
|
-
* name: "Grantee",
|
|
37
|
-
* schema: [{ type: "bool", name: "grantee", value: true }],
|
|
38
|
-
* uid: "0x000000000
|
|
39
|
-
* });
|
|
40
|
-
*
|
|
41
|
-
* const granteeDetails = new Schema({
|
|
42
|
-
* name: "GranteeDetails",
|
|
43
|
-
* schema: [
|
|
44
|
-
* { type: "bool", name: "name", value: null }
|
|
45
|
-
* { type: "bool", name: "description", value: null }
|
|
46
|
-
* { type: "bool", name: "imageURL", value: null }
|
|
47
|
-
* ],
|
|
48
|
-
* uid: "0x000000000,
|
|
49
|
-
* references: "Grantee"
|
|
50
|
-
* });
|
|
51
|
-
*
|
|
52
|
-
* // Validate if references are correct and all of them exist.
|
|
53
|
-
* Schema.validate();
|
|
54
|
-
*
|
|
55
|
-
* // Gets the schema by name.
|
|
56
|
-
* const grantee = Schema.get("Grantee");
|
|
57
|
-
*
|
|
58
|
-
* // Sets a single schema value.
|
|
59
|
-
* grantee.setValue("grantee", true);
|
|
60
|
-
*
|
|
61
|
-
* // Sets multiple schema values.
|
|
62
|
-
* granteeDetails.setValues({ name: "John Doe", description: "A description", imageURL: "https://example.com/image.png" });
|
|
63
|
-
*
|
|
64
|
-
* // Gets the schema encoded data, used to create an attestation.
|
|
65
|
-
* const encodedGrantee = grantee.encode();
|
|
66
|
-
*
|
|
67
|
-
* // Verify if schema exists
|
|
68
|
-
* Schema.exists("Grantee"); // true
|
|
69
|
-
* Schema.exists("GranteeDetails"); // true
|
|
70
|
-
* Schema.exists("GranteeDetails2"); // false
|
|
71
|
-
*
|
|
72
|
-
* // Get all schemas.
|
|
73
|
-
* Schema.getAll(); // [grantee, granteeDetails]
|
|
74
|
-
*
|
|
75
|
-
* // Get all schema names.
|
|
76
|
-
* Schema.getNames(); // ["Grantee", "GranteeDetails"]
|
|
77
|
-
*
|
|
78
|
-
* // Get many schemas by name. Throws an error if schema does not exist.
|
|
79
|
-
* Schema.getMany(["Grantee", "GranteeDetails"]); // [grantee, granteeDetails]
|
|
80
|
-
*
|
|
81
|
-
* // Replace all schemas. Throws an error if schema does not exist.
|
|
82
|
-
* Schema.replaceAll([grantee, granteeDetails]);
|
|
83
|
-
*
|
|
84
|
-
* // Replace one schema. This will replace a schema using the inbound schema name.. Throws an error if schema does not exist.
|
|
85
|
-
* Schema.replaceOne(grantee);
|
|
86
|
-
*
|
|
87
|
-
* // Converts a raw schema string (e.g. "uint256 id, string name") to a SchemaItem[].
|
|
88
|
-
* const schema = Schema.rawToObject("uint256 id, string name");
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
|
-
export abstract class Schema<T extends string = string>
|
|
92
|
-
implements SchemaInterface<T>
|
|
93
|
-
{
|
|
94
|
-
protected static schemas: Record<TNetwork, Schema[]> = {
|
|
95
|
-
'optimism-goerli': [],
|
|
96
|
-
optimism: [],
|
|
97
|
-
sepolia: [],
|
|
98
|
-
arbitrum: [],
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
protected encoder: SchemaEncoder;
|
|
102
|
-
private _schema: SchemaItem[] = [];
|
|
103
|
-
|
|
104
|
-
readonly uid: Hex;
|
|
105
|
-
readonly name: string;
|
|
106
|
-
|
|
107
|
-
readonly revocable?: boolean;
|
|
108
|
-
readonly references?: T;
|
|
109
|
-
|
|
110
|
-
readonly gap: GAP;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Creates a new schema instance
|
|
114
|
-
* @param args
|
|
115
|
-
* @param strict If true, will throw an error if schema reference is not valid. With this option, user should add schemas
|
|
116
|
-
* in a strict order.
|
|
117
|
-
*/
|
|
118
|
-
constructor(
|
|
119
|
-
args: SchemaInterface<T>,
|
|
120
|
-
gap?: GAP,
|
|
121
|
-
strict = false,
|
|
122
|
-
ignoreSchema = false
|
|
123
|
-
) {
|
|
124
|
-
this.assert(args, strict);
|
|
125
|
-
this.gap = gap;
|
|
126
|
-
this._schema = args.schema;
|
|
127
|
-
this.uid = args.uid;
|
|
128
|
-
this.name = args.name;
|
|
129
|
-
this.references = args.references;
|
|
130
|
-
this.revocable = args.revocable || true;
|
|
131
|
-
|
|
132
|
-
this.encoder = new SchemaEncoder(this.raw);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Encode the schema to be used as payload in the attestation
|
|
137
|
-
* @returns
|
|
138
|
-
*/
|
|
139
|
-
encode(schema?: SchemaItem[]) {
|
|
140
|
-
return this.encoder.encodeData(schema || this.schema);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Set a schema field value.
|
|
145
|
-
* @param key
|
|
146
|
-
* @param value
|
|
147
|
-
*/
|
|
148
|
-
setValue(key: string, value: SchemaValue) {
|
|
149
|
-
const idx = this._schema.findIndex((item) => item.name === key);
|
|
150
|
-
if (!~idx)
|
|
151
|
-
throw new SchemaError(
|
|
152
|
-
'INVALID_SCHEMA_FIELD',
|
|
153
|
-
`Field ${key} not found in schema ${this.name}`
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
this.assertField(this._schema[idx], value);
|
|
157
|
-
this._schema[idx].value = value;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Tests if the current schema is a JSON Schema.
|
|
162
|
-
*
|
|
163
|
-
* @returns boolean
|
|
164
|
-
*/
|
|
165
|
-
isJsonSchema() {
|
|
166
|
-
return !!this.schema.find((s) => s.name === 'json' && s.type === 'string');
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
private assertField(item: SchemaItem, value: any) {
|
|
170
|
-
const { type, name } = item;
|
|
171
|
-
|
|
172
|
-
if (type.includes('uint') && /\D/.test(value)) {
|
|
173
|
-
throw new SchemaError(
|
|
174
|
-
'INVALID_SCHEMA_FIELD',
|
|
175
|
-
`Field ${name} is of type ${type} but value is not a number.`
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
if (
|
|
180
|
-
type.includes('address') &&
|
|
181
|
-
!ethers.utils.isAddress(value) &&
|
|
182
|
-
value !== zeroAddress
|
|
183
|
-
) {
|
|
184
|
-
throw new SchemaError(
|
|
185
|
-
'INVALID_SCHEMA_FIELD',
|
|
186
|
-
`Field ${name} is of type ${type} but value is not a valid address.`
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (type.includes('bytes') && !value.startsWith('0x')) {
|
|
191
|
-
throw new SchemaError(
|
|
192
|
-
'INVALID_SCHEMA_FIELD',
|
|
193
|
-
`Field ${name} is of type ${type} but value is not a valid hex string.`
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (
|
|
198
|
-
type.includes('bool') &&
|
|
199
|
-
(!['true', 'false', true, false].includes(value) ||
|
|
200
|
-
typeof value !== 'boolean')
|
|
201
|
-
) {
|
|
202
|
-
throw new SchemaError(
|
|
203
|
-
'INVALID_SCHEMA_FIELD',
|
|
204
|
-
`Field ${name} is of type ${type} but value is not a valid boolean.`
|
|
205
|
-
);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (type.includes('tuple') && !Array.isArray(value)) {
|
|
209
|
-
throw new SchemaError(
|
|
210
|
-
'INVALID_SCHEMA_FIELD',
|
|
211
|
-
`Field ${name} is of type ${type} but value is not a valid array.`
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (type === 'string' && name === 'json') {
|
|
216
|
-
try {
|
|
217
|
-
JSON.parse(value);
|
|
218
|
-
} catch (error) {
|
|
219
|
-
throw new SchemaError(
|
|
220
|
-
'INVALID_SCHEMA_FIELD',
|
|
221
|
-
`Field ${name} is of type ${type} but value is not a valid JSON string.`
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Asserts if schema is valid.
|
|
229
|
-
* > Does not check references if `strict = false`. To check references use `Schema.validate()`
|
|
230
|
-
* @param args
|
|
231
|
-
*/
|
|
232
|
-
protected assert(args: SchemaInterface, strict = false) {
|
|
233
|
-
const { name, schema, uid, references } = args;
|
|
234
|
-
|
|
235
|
-
if (!name) {
|
|
236
|
-
throw new SchemaError('MISSING_FIELD', 'Schema name is required');
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (!schema && !Array.isArray(schema)) {
|
|
240
|
-
throw new SchemaError('MISSING_FIELD', 'Schema must be an array.');
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// if (!uid) {
|
|
244
|
-
// throw new SchemaError("MISSING_FIELD", "Schema uid is required");
|
|
245
|
-
// }
|
|
246
|
-
|
|
247
|
-
if (strict && references && !Schema.exists(references, this.gap.network)) {
|
|
248
|
-
throw new SchemaError(
|
|
249
|
-
'INVALID_REFERENCE',
|
|
250
|
-
`Schema ${name} references ${references} but it does not exist.`
|
|
251
|
-
);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Attest off chain data
|
|
257
|
-
* @returns
|
|
258
|
-
*/
|
|
259
|
-
async attestOffchain({ data, signer, to, refUID }: AttestArgs) {
|
|
260
|
-
const eas = await this.gap.eas.getOffchain();
|
|
261
|
-
const payload = <OffchainAttestationParams>{
|
|
262
|
-
data,
|
|
263
|
-
version: eas.version,
|
|
264
|
-
revocable: this.revocable,
|
|
265
|
-
expirationTime: 0n,
|
|
266
|
-
recipient: to,
|
|
267
|
-
refUID,
|
|
268
|
-
schema: this.raw,
|
|
269
|
-
time: BigInt((Date.now() / 1000).toFixed(0)),
|
|
270
|
-
};
|
|
271
|
-
return eas.signOffchainAttestation(payload, signer as any);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Revokes one off chain attestation by its UID.
|
|
276
|
-
* @param uid
|
|
277
|
-
* @param signer
|
|
278
|
-
* @returns
|
|
279
|
-
*/
|
|
280
|
-
async revokeOffchain(uid: Hex, signer: SignerOrProvider) {
|
|
281
|
-
const eas = this.gap.eas.connect(signer);
|
|
282
|
-
return eas.revokeOffchain(uid);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* Revokes multiple off chain attestations by their UIDs.
|
|
287
|
-
* @param uids
|
|
288
|
-
* @param signer
|
|
289
|
-
* @returns
|
|
290
|
-
*/
|
|
291
|
-
async multiRevokeOffchain(uids: Hex[], signer: SignerOrProvider) {
|
|
292
|
-
const eas = this.gap.eas.connect(signer);
|
|
293
|
-
return eas.multiRevokeOffchain(uids);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Validates and attests a given schema.
|
|
298
|
-
*
|
|
299
|
-
* This function checks a schema against predefined standards or rules. If the 'ipfsKey' parameter is enabled,
|
|
300
|
-
* it uploads the data to the IPFS (InterPlanetary File System). Upon successful upload, the function
|
|
301
|
-
* returns the CID (Content Identifier) within the Attestation Body, providing a reference to the data on IPFS.
|
|
302
|
-
*
|
|
303
|
-
* Usage:
|
|
304
|
-
* - Ensure that the schema to be attested conforms to the required format.
|
|
305
|
-
* - Enable 'ipfsKey' if you wish to store the data on IPFS and retrieve its CID.
|
|
306
|
-
*
|
|
307
|
-
* @param {Object} param0 - An object containing the schema and other optional settings.
|
|
308
|
-
* @returns {Object} An object containing the attestation results, including the CID if 'ipfsKey' is enabled.
|
|
309
|
-
*/
|
|
310
|
-
async attest<T>({ data, to, signer, refUID }: AttestArgs<T>): Promise<Hex> {
|
|
311
|
-
const eas = this.gap.eas.connect(signer);
|
|
312
|
-
|
|
313
|
-
if (this.references && !refUID)
|
|
314
|
-
throw new AttestationError(
|
|
315
|
-
'INVALID_REFERENCE',
|
|
316
|
-
'Attestation schema references another schema but no reference UID was provided.'
|
|
317
|
-
);
|
|
318
|
-
|
|
319
|
-
if (this.isJsonSchema()) {
|
|
320
|
-
const { remoteClient } = GAP;
|
|
321
|
-
if (remoteClient) {
|
|
322
|
-
const cid = await remoteClient.save(data, this.name);
|
|
323
|
-
const encodedData = remoteClient.encode(cid);
|
|
324
|
-
data = encodedData as T;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
this.setValue('json', JSON.stringify(data));
|
|
328
|
-
} else {
|
|
329
|
-
Object.entries(data).forEach(([key, value]) => {
|
|
330
|
-
this.setValue(key, value);
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
const payload: RawAttestationPayload = {
|
|
335
|
-
schema: this.uid,
|
|
336
|
-
data: {
|
|
337
|
-
raw: {
|
|
338
|
-
recipient: to,
|
|
339
|
-
expirationTime: 0n,
|
|
340
|
-
revocable: true,
|
|
341
|
-
data: this.schema as any,
|
|
342
|
-
refUID,
|
|
343
|
-
value: 0n,
|
|
344
|
-
},
|
|
345
|
-
payload: {
|
|
346
|
-
recipient: to,
|
|
347
|
-
expirationTime: 0n,
|
|
348
|
-
revocable: true,
|
|
349
|
-
data: this.encode(this.schema),
|
|
350
|
-
refUID,
|
|
351
|
-
value: 0n,
|
|
352
|
-
},
|
|
353
|
-
},
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
if (useDefaultAttestation.includes(this.name as TSchemaName)) {
|
|
357
|
-
const tx = await eas.attest({
|
|
358
|
-
schema: this.uid,
|
|
359
|
-
data: payload.data.payload,
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
return tx.wait() as Promise<Hex>;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
const uid = await GapContract.attest(signer, payload);
|
|
366
|
-
|
|
367
|
-
return uid;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
/**
|
|
371
|
-
* Bulk attest a set of attestations.
|
|
372
|
-
* @param signer
|
|
373
|
-
* @param entities
|
|
374
|
-
* @returns
|
|
375
|
-
*/
|
|
376
|
-
async multiAttest(signer: SignerOrProvider, entities: Attestation[] = []) {
|
|
377
|
-
entities.forEach((entity) => {
|
|
378
|
-
if (this.references && !entity.refUID)
|
|
379
|
-
throw new SchemaError(
|
|
380
|
-
'INVALID_REF_UID',
|
|
381
|
-
`Entity ${entity.schema.name} references another schema but no reference UID was provided.`
|
|
382
|
-
);
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
const eas = this.gap.eas.connect(signer);
|
|
386
|
-
|
|
387
|
-
const entityBySchema = entities.reduce(
|
|
388
|
-
(acc, entity) => {
|
|
389
|
-
const schema = entity.schema.uid;
|
|
390
|
-
if (!acc[schema]) acc[schema] = [];
|
|
391
|
-
acc[schema].push(entity);
|
|
392
|
-
return acc;
|
|
393
|
-
},
|
|
394
|
-
{} as Record<string, Attestation[]>
|
|
395
|
-
);
|
|
396
|
-
|
|
397
|
-
const payload = Object.entries(entityBySchema).map(([schema, ents]) => ({
|
|
398
|
-
schema,
|
|
399
|
-
data: ents.map((e) => ({
|
|
400
|
-
data: e.schema.encode(),
|
|
401
|
-
refUID: e.refUID,
|
|
402
|
-
recipient: e.recipient,
|
|
403
|
-
expirationTime: 0n,
|
|
404
|
-
})),
|
|
405
|
-
}));
|
|
406
|
-
|
|
407
|
-
const tx = await eas.multiAttest(payload, {
|
|
408
|
-
gasLimit: 5000000n,
|
|
409
|
-
});
|
|
410
|
-
return tx.wait();
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
/**
|
|
414
|
-
* Revokes a set of attestations by their UIDs.
|
|
415
|
-
* @param signer
|
|
416
|
-
* @param uids
|
|
417
|
-
* @returns
|
|
418
|
-
*/
|
|
419
|
-
async multiRevoke(signer: SignerOrProvider, toRevoke: MultiRevokeArgs[]) {
|
|
420
|
-
const groupBySchema = toRevoke.reduce(
|
|
421
|
-
(acc, { uid, schemaId }) => {
|
|
422
|
-
if (!acc[schemaId]) acc[schemaId] = [];
|
|
423
|
-
acc[schemaId].push(uid);
|
|
424
|
-
return acc;
|
|
425
|
-
},
|
|
426
|
-
{} as Record<string, Hex[]>
|
|
427
|
-
);
|
|
428
|
-
|
|
429
|
-
const eas = this.gap.eas.connect(signer);
|
|
430
|
-
const payload = Object.entries(groupBySchema).map(([schema, uids]) => ({
|
|
431
|
-
schema,
|
|
432
|
-
data: uids.map((uid) => ({ uid })),
|
|
433
|
-
}));
|
|
434
|
-
|
|
435
|
-
const tx = await eas.multiRevoke(payload, {
|
|
436
|
-
gasLimit: 5000000n,
|
|
437
|
-
});
|
|
438
|
-
return tx.wait();
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
static exists(name: string, network: TNetwork) {
|
|
442
|
-
return this.schemas[network].find((schema) => schema.name === name);
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
/**
|
|
446
|
-
* Adds the schema signature to a shares list. Use Schema.get("SchemaName") to get the schema.
|
|
447
|
-
*
|
|
448
|
-
* __Note that this will make the schema available to all instances
|
|
449
|
-
* of the class AND its data can be overriden by any changes.__
|
|
450
|
-
* @param schemas
|
|
451
|
-
*/
|
|
452
|
-
static add<T extends Schema>(network: TNetwork, ...schemas: T[]) {
|
|
453
|
-
schemas.forEach((schema) => {
|
|
454
|
-
if (!this.exists(schema.name, network))
|
|
455
|
-
this.schemas[network].push(schema);
|
|
456
|
-
else
|
|
457
|
-
throw new SchemaError(
|
|
458
|
-
'SCHEMA_ALREADY_EXISTS',
|
|
459
|
-
`Schema ${schema.name} already exists.`
|
|
460
|
-
);
|
|
461
|
-
});
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
static getAll<T extends Schema>(network: TNetwork): T[] {
|
|
465
|
-
return this.schemas[network] as T[];
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
static get<N extends string, T extends Schema>(
|
|
469
|
-
name: N,
|
|
470
|
-
network: TNetwork
|
|
471
|
-
): T {
|
|
472
|
-
const schema = this.schemas[network].find(
|
|
473
|
-
(schema) => schema.name === name || schema.uid === name
|
|
474
|
-
);
|
|
475
|
-
|
|
476
|
-
if (!schema)
|
|
477
|
-
throw new SchemaError(
|
|
478
|
-
'SCHEMA_NOT_FOUND',
|
|
479
|
-
`Schema ${name} not found. Available schemas: ${Schema.getNames(
|
|
480
|
-
network
|
|
481
|
-
)}`
|
|
482
|
-
);
|
|
483
|
-
|
|
484
|
-
return schema as T;
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
/**
|
|
488
|
-
* Find many schemas by name and return them as an array in the same order.
|
|
489
|
-
* @param names
|
|
490
|
-
* @returns
|
|
491
|
-
*/
|
|
492
|
-
static getMany<N extends string, T extends Schema>(
|
|
493
|
-
names: N[],
|
|
494
|
-
network: TNetwork
|
|
495
|
-
) {
|
|
496
|
-
return names.map((name) => <T>this.get(name, network));
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
static getNames(network: TNetwork): string[] {
|
|
500
|
-
return Schema.schemas[network].map((schema) => schema.name);
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
/**
|
|
504
|
-
* Validade references
|
|
505
|
-
* @throws {SchemaError} if any reference is not valid
|
|
506
|
-
* @returns {true} if references are valid
|
|
507
|
-
*/
|
|
508
|
-
static validate(network: TNetwork): true {
|
|
509
|
-
const errors: SchemaError[] = [];
|
|
510
|
-
|
|
511
|
-
this.schemas[network].forEach((schema) => {
|
|
512
|
-
if (!schema.references || Schema.exists(schema.references, network))
|
|
513
|
-
return;
|
|
514
|
-
else
|
|
515
|
-
errors.push(
|
|
516
|
-
new SchemaError(
|
|
517
|
-
'INVALID_REFERENCE',
|
|
518
|
-
`Schema ${schema.name} references ${schema.references} but it does not exist.`
|
|
519
|
-
)
|
|
520
|
-
);
|
|
521
|
-
});
|
|
522
|
-
|
|
523
|
-
if (errors.length) throw errors;
|
|
524
|
-
return true;
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
/**
|
|
528
|
-
* Replaces the schema list with a new list.
|
|
529
|
-
* @param schemas
|
|
530
|
-
*/
|
|
531
|
-
static replaceAll(schemas: Schema[], network: TNetwork) {
|
|
532
|
-
this.schemas[network] = schemas;
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
/**
|
|
536
|
-
* Replaces a schema from the schema list.
|
|
537
|
-
* @throws {SchemaError} if desired schema name does not exist.
|
|
538
|
-
*/
|
|
539
|
-
static replaceOne(schema: Schema, network: TNetwork) {
|
|
540
|
-
const idx = this.schemas[network].findIndex(
|
|
541
|
-
(item) => schema.name === item.name
|
|
542
|
-
);
|
|
543
|
-
if (!~idx)
|
|
544
|
-
throw new SchemaError(
|
|
545
|
-
'SCHEMA_NOT_FOUND',
|
|
546
|
-
`Schema ${schema.name} not found.`
|
|
547
|
-
);
|
|
548
|
-
|
|
549
|
-
this.schemas[idx] = schema;
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
/**
|
|
553
|
-
* Transforms the given raw schema to SchemaItem[]
|
|
554
|
-
*
|
|
555
|
-
* @example
|
|
556
|
-
* ```
|
|
557
|
-
* const schema = Schema.rawToObject("uint256 id, string name");
|
|
558
|
-
* // schema = [{ type: "uint256", name: "id", value: null }, { type: "string", name: "name", value: null }]
|
|
559
|
-
* ```
|
|
560
|
-
* @param abi
|
|
561
|
-
* @returns
|
|
562
|
-
*/
|
|
563
|
-
static rawToObject(abi: string) {
|
|
564
|
-
const items = abi.trim().replace(/,\s+/gim, ',').split(',');
|
|
565
|
-
const schema: SchemaItem[] = items.map((item) => {
|
|
566
|
-
const [type, name] = item.split(' ');
|
|
567
|
-
return { type, name, value: null };
|
|
568
|
-
});
|
|
569
|
-
|
|
570
|
-
return schema;
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
/**
|
|
574
|
-
* Returns the raw schema string.
|
|
575
|
-
* @example
|
|
576
|
-
* ```ts
|
|
577
|
-
* const schema = new Schema({ name: "Grantee", schema: [{ type: "bool", name: "grantee", value: true }], uid: "0x000000000" });
|
|
578
|
-
* schema.raw; // "bool grantee"
|
|
579
|
-
* ```
|
|
580
|
-
*/
|
|
581
|
-
get raw() {
|
|
582
|
-
return this.schema.map((item) => `${item.type} ${item.name}`).join(',');
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
get schema() {
|
|
586
|
-
return this._schema;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
/**
|
|
590
|
-
* Get all schemas that references this schema. Note that this
|
|
591
|
-
* will return a reference to the original schema and all
|
|
592
|
-
* the changes made to it will reflect the original instance.
|
|
593
|
-
*/
|
|
594
|
-
get children() {
|
|
595
|
-
return Schema.schemas[this.gap.network].filter(
|
|
596
|
-
(schema) =>
|
|
597
|
-
schema.references === this.name || schema.references === this.uid
|
|
598
|
-
);
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
/**
|
|
602
|
-
* Asserts and sets the schema value.
|
|
603
|
-
*/
|
|
604
|
-
set schema(schema: SchemaItem[]) {
|
|
605
|
-
schema.forEach((item) => {
|
|
606
|
-
this.setValue(item.name, item.value);
|
|
607
|
-
});
|
|
608
|
-
}
|
|
609
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
const SchemaErrorCodes = {
|
|
2
|
-
INVALID_SCHEMA: 50001,
|
|
3
|
-
INVALID_SCHEMA_NAME: 50002,
|
|
4
|
-
INVALID_SCHEMA_TYPE: 50003,
|
|
5
|
-
SCHEMA_ALREADY_EXISTS: 50004,
|
|
6
|
-
SCHEMA_NOT_FOUND: 50005,
|
|
7
|
-
SCHEMA_NOT_CREATED: 50006,
|
|
8
|
-
MISSING_FIELD: 50007,
|
|
9
|
-
INVALID_REFERENCE: 50008,
|
|
10
|
-
INVALID_SCHEMA_FIELD: 50009,
|
|
11
|
-
INVALID_DATA: 50010,
|
|
12
|
-
REVOKE_ERROR: 50011,
|
|
13
|
-
ATTEST_ERROR: 50012,
|
|
14
|
-
INVALID_REF_UID: 50013,
|
|
15
|
-
REVOKATION_ERROR: 50014,
|
|
16
|
-
NOT_REVOCABLE: 50015,
|
|
17
|
-
REMOTE_STORAGE_UPLOAD: 50016,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export class SchemaError extends Error {
|
|
21
|
-
readonly code: number;
|
|
22
|
-
private readonly _message: string;
|
|
23
|
-
|
|
24
|
-
constructor(code: keyof typeof SchemaErrorCodes, append?: string) {
|
|
25
|
-
super(`${code}${append ? `: ${append}` : ''}`);
|
|
26
|
-
this._message = append || code.replace(/_/g, ' ');
|
|
27
|
-
this.code = SchemaErrorCodes[code];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
get message(): string {
|
|
31
|
-
return this._message;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export class AttestationError extends SchemaError {}
|
|
36
|
-
export class RemoteStorageError extends SchemaError {}
|