Skip to content

Commit 1f2eec7

Browse files
committed
Add Creating Multi Onchain Attestations example
1 parent 017ee20 commit 1f2eec7

File tree

1 file changed

+80
-34
lines changed

1 file changed

+80
-34
lines changed

README.md

Lines changed: 80 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This repository contains the Ethereum Attestation Service SDK, used to interact
1313
- [Getting an Attestation](#getting-an-attestation)
1414
- [Creating Onchain Attestations](#creating-onchain-attestations)
1515
- [Example: Creating Onchain Attestations](#example-creating-onchain-attestations)
16+
- [Example: Creating Multi Onchain Attestations](#example-creating-multi-onchain-attestations)
1617
- [Revoking Onchain Attestations](#revoking-onchain-attestations)
1718
- [Example: Revoking Onchain Attestations](#example-revoking-onchain-attestations)
1819
- [Creating Offchain Attestations](#creating-offchain-attestations)
@@ -82,7 +83,7 @@ The `getAttestation` function allows you to retrieve an on-chain attestation for
8283
#### Usage
8384

8485
```javascript
85-
import { EAS } from '@ethereum-attestation-service/eas-sdk';
86+
import { EAS, NO_EXPIRATION } from '@ethereum-attestation-service/eas-sdk';
8687

8788
const eas = new EAS(EASContractAddress);
8889
eas.connect(provider);
@@ -117,7 +118,7 @@ Example output:
117118
schema: '0x27d06e3659317e9a4f8154d1e849eb53d43d91fb4f219884d1684f86d797804a',
118119
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
119120
time: 1671219600,
120-
expirationTime: 0,
121+
expirationTime: NO_EXPIRATION,
121122
revocationTime: 1671219636,
122123
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
123124
attester: '0x1e3de6aE412cA218FD2ae3379750388D414532dc',
@@ -144,7 +145,7 @@ The function returns a `Promise` that resolves to the UID of the newly created a
144145
#### Example: Creating Onchain Attestations
145146

146147
```javascript
147-
import { EAS, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
148+
import { EAS, NO_EXPIRATION, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
148149

149150
const eas = new EAS(EASContractAddress);
150151
eas.connect(signer);
@@ -162,7 +163,7 @@ const transaction = await eas.attest({
162163
schema: schemaUID,
163164
data: {
164165
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
165-
expirationTime: 0,
166+
expirationTime: NO_EXPIRATION,
166167
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
167168
data: encodedData
168169
}
@@ -175,6 +176,54 @@ console.log('New attestation UID:', newAttestationUID);
175176
console.log('Transaction receipt:', transaction.receipt);
176177
```
177178

179+
#### Example: Creating Multi Onchain Attestations
180+
181+
```javascript
182+
import { EAS, NO_EXPIRATION, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
183+
184+
const eas = new EAS(EASContractAddress);
185+
eas.connect(signer);
186+
187+
// Initialize SchemaEncoder with the schema string
188+
const schemaEncoder = new SchemaEncoder('uint256 eventId, uint8 voteIndex');
189+
const encodedData = schemaEncoder.encodeData([
190+
{ name: 'eventId', value: 1, type: 'uint256' },
191+
{ name: 'voteIndex', value: 1, type: 'uint8' }
192+
]);
193+
const encodedData2 = schemaEncoder.encodeData([
194+
{ name: 'eventId', value: 10, type: 'uint256' },
195+
{ name: 'voteIndex', value: 2, type: 'uint8' }
196+
]);
197+
198+
const schemaUID = '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995';
199+
200+
const transaction = await eas.multiAttest([
201+
{
202+
schema: schemaId,
203+
data: [
204+
{
205+
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
206+
expirationTime: NO_EXPIRATION,
207+
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
208+
data: encodedData
209+
},
210+
{
211+
recipient: '0xA1207F3BBa224E2c9c3c6D5aF63D0eb1582Ce587',
212+
expirationTime: NO_EXPIRATION,
213+
revocable: false,
214+
data: encodedData2
215+
}
216+
]
217+
}
218+
]);
219+
220+
const newAttestationUID = await transaction.wait();
221+
222+
console.log('New attestation UID:', newAttestationUID);
223+
224+
console.log('Transaction receipt:', transaction.receipt);
225+
```
226+
178227
### Revoking Onchain Attestations
179228

180229
The `revoke` function allows you to revoke an on-chain attestation. This function takes an object with the following properties:
@@ -203,7 +252,7 @@ To create an offchain attestation, you can use the `signOffchainAttestation` fun
203252
#### Example: Creating Offchain Attestations
204253

205254
```javascript
206-
import { EAS, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
255+
import { EAS, NO_EXPIRATION, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
207256

208257
// Initialize EAS with the EAS contract address on whichever chain where your schema is defined
209258
const eas = new EAS(EASContractAddress);
@@ -225,7 +274,7 @@ const signer = new ethers.Wallet(privateKey, provider);
225274
const offchainAttestation = await offchain.signOffchainAttestation(
226275
{
227276
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
228-
expirationTime: 0n, // Unix timestamp of when attestation expires (0 for no expiration)
277+
expirationTime: NO_EXPIRATION, // Unix timestamp of when attestation expires (0 for no expiration)
229278
time: BigInt(Math.floor(Date.now() / 1000)), // Unix timestamp of current time
230279
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
231280
schema: '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995',
@@ -265,7 +314,7 @@ The function returns a `Promise` that resolves to the UID of the newly created a
265314
#### Example: Creating Delegated Onchain Attestations
266315

267316
```javascript
268-
import { EAS, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
317+
import { EAS, NO_EXPIRATION, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
269318

270319
const eas = new EAS(EASContractAddress);
271320

@@ -290,11 +339,11 @@ const response = await delegated.signDelegatedAttestation(
290339
{
291340
schema: '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995',
292341
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
293-
expirationTime: 0n, // Unix timestamp of when attestation expires (0 for no expiration)
342+
expirationTime: NO_EXPIRATION, // Unix timestamp of when attestation expires (0 for no expiration)
294343
revocable: true,
295344
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
296345
data: encodedData,
297-
deadline: 0n, // Unix timestamp of when signature expires (0 for no expiration)
346+
deadline: NO_EXPIRATION, // Unix timestamp of when signature expires (0 for no expiration)
298347
value: 0n
299348
},
300349
signer
@@ -304,7 +353,7 @@ const transaction = await eas.attestByDelegation({
304353
schema: '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995',
305354
data: {
306355
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
307-
expirationTime: 0n, // Unix timestamp of when attestation expires (0 for no expiration),
356+
expirationTime: NO_EXPIRATION, // Unix timestamp of when attestation expires (0 for no expiration),
308357
revocable: true,
309358
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
310359
data: encodedData
@@ -476,46 +525,43 @@ const attestation = {
476525
// your offchain attestation
477526
sig: {
478527
domain: {
479-
name: "EAS Attestation",
480-
version: "0.26",
528+
name: 'EAS Attestation',
529+
version: '0.26',
481530
chainId: 1,
482-
verifyingContract: "0xA1207F3BBa224E2c9c3c6D5aF63D0eb1582Ce587",
531+
verifyingContract: '0xA1207F3BBa224E2c9c3c6D5aF63D0eb1582Ce587'
483532
},
484-
primaryType: "Attest",
533+
primaryType: 'Attest',
485534
types: {
486-
Attest: [],
535+
Attest: []
487536
},
488537
signature: {
489-
r: "",
490-
s: "",
491-
v: 28,
538+
r: '',
539+
s: '',
540+
v: 28
492541
},
493-
uid: "0x5134f511e0533f997e569dac711952dde21daf14b316f3cce23835defc82c065",
542+
uid: '0x5134f511e0533f997e569dac711952dde21daf14b316f3cce23835defc82c065',
494543
message: {
495544
version: OffchainAttestationVersion.Version2,
496-
schema: "0x27d06e3659317e9a4f8154d1e849eb53d43d91fb4f219884d1684f86d797804a",
497-
refUID: "0x0000000000000000000000000000000000000000000000000000000000000000",
545+
schema: '0x27d06e3659317e9a4f8154d1e849eb53d43d91fb4f219884d1684f86d797804a',
546+
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
498547
time: 1671219600,
499-
expirationTime: 0,
500-
recipient: "0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165",
501-
attester: "0x1e3de6aE412cA218FD2ae3379750388D414532dc",
548+
expirationTime: NO_EXPIRATION,
549+
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
550+
attester: '0x1e3de6aE412cA218FD2ae3379750388D414532dc',
502551
revocable: true,
503-
data: "0x0000000000000000000000000000000000000000000000000000000000000000",
504-
},
552+
data: '0x0000000000000000000000000000000000000000000000000000000000000000'
553+
}
505554
},
506-
signer: "0x1e3de6aE412cA218FD2ae3379750388D414532dc",
555+
signer: '0x1e3de6aE412cA218FD2ae3379750388D414532dc'
507556
};
508557

509558
const EAS_CONFIG: OffchainConfig = {
510559
address: attestation.sig.domain.verifyingContract,
511560
version: attestation.sig.domain.version,
512-
chainId: attestation.sig.domain.chainId,
561+
chainId: attestation.sig.domain.chainId
513562
};
514563
const offchain = new Offchain(EAS_CONFIG, OffchainAttestationVersion.Version2);
515-
const isValidAttestation = offchain.verifyOffchainAttestationSignature(
516-
attestation.signer,
517-
attestation.sig
518-
);
564+
const isValidAttestation = offchain.verifyOffchainAttestationSignature(attestation.signer, attestation.sig);
519565
```
520566

521567
### Registering a Schema
@@ -644,7 +690,7 @@ console.log('Is Full Tree Valid?', calculatedRoot === fullTree.root);
644690
Here's an example of how you might use the `PrivateData` class in conjunction with the EAS SDK to create an attestation with private data:
645691

646692
```typescript
647-
import { EAS, MerkleValue, PrivateData, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
693+
import { EAS, NO_EXPIRATION, MerkleValue, PrivateData, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
648694
import { ethers } from 'ethers';
649695

650696
// Initialize EAS
@@ -674,7 +720,7 @@ const transaction = await eas.attest({
674720
schema: schemaUID,
675721
data: {
676722
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
677-
expirationTime: 0,
723+
expirationTime: NO_EXPIRATION,
678724
revocable: true,
679725
data: encodedData
680726
}

0 commit comments

Comments
 (0)