Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ethereum-attestation-service/eas-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
slavik0329 committed Jun 23, 2024
2 parents ee2f8b7 + 297b4e9 commit 4f7529b
Show file tree
Hide file tree
Showing 11 changed files with 7,110 additions and 5,348 deletions.
22 changes: 22 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Dependencies
/node_modules
pnpm-lock.yaml

# Testing
/coverage
.hardhat-dependency-compiler

# Production
/build
/dist
typechain-types
typechain

# Ignore all .env files
.env*
!.env
!.env.template

# Misc
.DS_Store
.vscode/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.3.0

- Add utilities to handle private data

## 2.2.0

- Add a method to receive the tx receipt to the Transaction object
Expand Down
87 changes: 44 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,16 @@ pnpm add @ethereum-attestation-service/eas-sdk
Import and initialize the library

```javascript
import { EAS, Offchain, SchemaEncoder, SchemaRegistry } from "@ethereum-attestation-service/eas-sdk";
import { EAS, Offchain, SchemaEncoder, SchemaRegistry } from '@ethereum-attestation-service/eas-sdk';
import { ethers } from 'ethers';

export const EASContractAddress = "0xC2679fBD37d54388Ce493F1DB75320D236e1815e"; // Sepolia v0.26
export const EASContractAddress = '0xC2679fBD37d54388Ce493F1DB75320D236e1815e'; // Sepolia v0.26

// Initialize the sdk with the address of the EAS Schema contract address
const eas = new EAS(EASContractAddress);

// Gets a default provider (in production use something else like infura/alchemy)
const provider = ethers.getDefaultProvider(
"sepolia"
);
const provider = ethers.getDefaultProvider('sepolia');

// Connects an ethers style provider/signingProvider to perform read/write functions.
// MUST be a signer to do write operations!
Expand All @@ -56,12 +54,12 @@ The `getAttestation` function allows you to retrieve an on-chain attestation for
#### Usage

```javascript
import { EAS } from "@ethereum-attestation-service/eas-sdk";
import { EAS } from '@ethereum-attestation-service/eas-sdk';

const eas = new EAS(EASContractAddress);
eas.connect(provider);

const uid = "0xff08bbf3d3e6e0992fc70ab9b9370416be59e87897c3d42b20549901d2cccc3e";
const uid = '0xff08bbf3d3e6e0992fc70ab9b9370416be59e87897c3d42b20549901d2cccc3e';

const attestation = await eas.getAttestation(uid);

Expand Down Expand Up @@ -117,43 +115,43 @@ The function returns a Promise that resolves to the UID of the newly created att
### Example

```javascript
import { EAS, SchemaEncoder } from "@ethereum-attestation-service/eas-sdk";
import { EAS, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';

const eas = new EAS(EASContractAddress);
eas.connect(signer);

// Initialize SchemaEncoder with the schema string
const schemaEncoder = new SchemaEncoder("uint256 eventId, uint8 voteIndex");
const schemaEncoder = new SchemaEncoder('uint256 eventId, uint8 voteIndex');
const encodedData = schemaEncoder.encodeData([
{ name: "eventId", value: 1, type: "uint256" },
{ name: "voteIndex", value: 1, type: "uint8" },
{ name: 'eventId', value: 1, type: 'uint256' },
{ name: 'voteIndex', value: 1, type: 'uint8' }
]);

const schemaUID = "0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995";
const schemaUID = '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995';

const tx = await eas.attest({
schema: schemaUID,
data: {
recipient: "0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165",
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: 0,
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
data: encodedData,
},
data: encodedData
}
});

const newAttestationUID = await tx.wait();

console.log("New attestation UID:", newAttestationUID);
console.log('New attestation UID:', newAttestationUID);

console.log("Transaction receipt:", tx.receipt);
console.log('Transaction receipt:', tx.receipt);
```

### Creating Offchain Attestations

To create an offchain attestation, you can use the `signOffchainAttestation` function provided by the Offchain class in the EAS SDK. Here's an example:

```javascript
import { EAS, SchemaEncoder } from "@ethereum-attestation-service/eas-sdk";
import { EAS, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';

// Initialize EAS with the EAS contract address on whichever chain where your schema is defined
const eas = new EAS(EASContractAddress);
Expand All @@ -163,24 +161,27 @@ const offchain = await eas.getOffchain();
// Initialize SchemaEncoder with the schema string
// Note these values are sample values and should be filled with actual values
// Code samples can be found when viewing each schema on easscan.org
const schemaEncoder = new SchemaEncoder("uint256 eventId, uint8 voteIndex");
const schemaEncoder = new SchemaEncoder('uint256 eventId, uint8 voteIndex');
const encodedData = schemaEncoder.encodeData([
{ name: "eventId", value: 1, type: "uint256" },
{ name: "voteIndex", value: 1, type: "uint8" },
{ name: 'eventId', value: 1, type: 'uint256' },
{ name: 'voteIndex', value: 1, type: 'uint8' }
]);

// Signer is an ethers.js Signer instance
const signer = new ethers.Wallet(privateKey, provider);

const offchainAttestation = await offchain.signOffchainAttestation({
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: 0n, // Unix timestamp of when attestation expires. (0 for no expiration)
time: BigInt(Math.floor(Date.now() / 1000)), // Unix timestamp of current time
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
schema: "0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995",
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
data: encodedData,
}, signer);
const offchainAttestation = await offchain.signOffchainAttestation(
{
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: 0n, // Unix timestamp of when attestation expires. (0 for no expiration)
time: BigInt(Math.floor(Date.now() / 1000)), // Unix timestamp of current time
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
schema: '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995',
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
data: encodedData
},
signer
);
```

This function will return a signed offchain attestation object containing the UID, signature, and attestation data. You can then share this object with the intended recipient or store it for future use.
Expand All @@ -195,8 +196,8 @@ Please note that using the `getOffchainUID` function for the previous legacy ver

```javascript
const transaction = await eas.revoke({
schema: "0x85500e806cf1e74844d51a20a6d893fe1ed6f6b0738b50e43d774827d08eca61",
data: { uid: "0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a" }
schema: '0x85500e806cf1e74844d51a20a6d893fe1ed6f6b0738b50e43d774827d08eca61',
data: { uid: '0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a' }
});

// Optional: Wait for transaction to be validated
Expand All @@ -208,12 +209,12 @@ await transaction.wait();
To timestamp an off-chain attestation UID on-chain, you can use the timestamp function provided by the EAS SDK. Here's an example:

```javascript
import { EAS } from "@ethereum-attestation-service/eas-sdk";
import { EAS } from '@ethereum-attestation-service/eas-sdk';

const eas = new EAS(EASContractAddress);
eas.connect(provider);

const uid = "0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a";
const uid = '0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a';

const transaction = await eas.timestamp(uid);

Expand All @@ -224,7 +225,7 @@ await transaction.wait();
To create a timestamp for a any piece of data, you can use the `timestamp` function provided by the EAS SDK. Here's an example:

```javascript
import { EAS } from "@ethereum-attestation-service/eas-sdk";
import { EAS } from '@ethereum-attestation-service/eas-sdk';

const eas = new EAS(EASContractAddress);
eas.connect(provider);
Expand All @@ -240,7 +241,7 @@ await transaction.wait();
To create timestamps for multiple pieces of data, you can use the `multiTimestamp` function:

```javascript
import { EAS } from "@ethereum-attestation-service/eas-sdk";
import { EAS } from '@ethereum-attestation-service/eas-sdk';

const eas = new EAS(EASContractAddress);
eas.connect(provider);
Expand All @@ -259,7 +260,7 @@ await transaction.wait();
To revoke an offchain attestation, you can use the `revokeOffchain` function provided by the EAS SDK. Here's an example:

```javascript
import { EAS } from "@ethereum-attestation-service/eas-sdk";
import { EAS } from '@ethereum-attestation-service/eas-sdk';

const eas = new EAS(EASContractAddress);
eas.connect(provider);
Expand All @@ -275,7 +276,7 @@ await transaction.wait();
To revoke multiple offchain attestations, you can use the `multiRevokeOffchain` function:

```javascript
import { EAS } from "@ethereum-attestation-service/eas-sdk";
import { EAS } from '@ethereum-attestation-service/eas-sdk';

const eas = new EAS(EASContractAddress);
eas.connect(provider);
Expand Down Expand Up @@ -353,22 +354,22 @@ To register a new schema, you can use the `register` function provided by the EA
Here's an example of how to register a new schema:

```javascript
import { SchemaRegistry } from "@ethereum-attestation-service/eas-sdk";
import { SchemaRegistry } from '@ethereum-attestation-service/eas-sdk';
import { ethers } from 'ethers';

const schemaRegistryContractAddress = "0xYourSchemaRegistryContractAddress";
const schemaRegistryContractAddress = '0xYourSchemaRegistryContractAddress';
const schemaRegistry = new SchemaRegistry(schemaRegistryContractAddress);

schemaRegistry.connect(signer);

const schema = "uint256 eventId, uint8 voteIndex";
const resolverAddress = "0x0a7E2Ff54e76B8E6659aedc9103FB21c038050D0"; // Sepolia 0.26
const schema = 'uint256 eventId, uint8 voteIndex';
const resolverAddress = '0x0a7E2Ff54e76B8E6659aedc9103FB21c038050D0'; // Sepolia 0.26
const revocable = true;

const transaction = await schemaRegistry.register({
schema,
resolverAddress,
revocable,
revocable
});

// Optional: Wait for transaction to be validated
Expand Down
37 changes: 37 additions & 0 deletions dist/private-data.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export type FullMerkleDataTree = {
root: string;
values: MerkleValueWithSalt[];
};
export interface MerkleMultiProof {
leaves: Leaf[];
proof: string[];
proofFlags: boolean[];
}
export interface Leaf {
type: string;
name: string;
value: unknown;
salt: string;
}
export interface MerkleValue {
type: string;
name: string;
value: unknown;
}
export type MerkleValueWithSalt = MerkleValue & {
salt: string;
};
export type EncodedMerkleValue = [string, string, string, string];
export declare class PrivateData {
private tree;
private values;
constructor(values: MerkleValue[] | MerkleValueWithSalt[]);
private encodeValuesToMerkleTree;
private encodeMerkleValues;
private decodeMerkleValues;
getFullTree(): FullMerkleDataTree;
generateMultiProof(indexes: number[]): MerkleMultiProof;
static verifyMultiProof(root: string, proof: MerkleMultiProof): boolean;
private static encodeMerkleValues;
static verifyFullTree(tree: FullMerkleDataTree): string;
}
67 changes: 67 additions & 0 deletions dist/private-data.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/private-data.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ethereum-attestation-service/eas-sdk",
"version": "2.2.0",
"version": "2.3.0",
"description": "Ethereum Attestation Service - TypeScript/JavaScript SDK",
"repository": "[email protected]:ethereum-attestation-service/eas-sdk.git",
"author": "Leonid Beder <[email protected]>",
Expand All @@ -14,11 +14,12 @@
"build": "rm -rf dist && tsc --build tsconfig.json",
"test": "cd test && pnpm test",
"lint": "eslint -c .eslintrc --ext .ts src test",
"format": "prettier --check --write src/**/*.ts test/test/**/*.ts --config .prettierrc",
"prepare:release": "pnpm lint && pnpm test && pnpm build"
"format": "prettier --check --write . --config .prettierrc",
"prepare:release": "pnpm lint && pnpm format && pnpm test && pnpm build"
},
"dependencies": {
"@ethereum-attestation-service/eas-contracts": "1.4.1",
"@openzeppelin/merkle-tree": "^1.0.6",
"ethers": "^6.11.1",
"js-base64": "^3.7.7",
"lodash": "^4.17.21",
Expand Down
Loading

0 comments on commit 4f7529b

Please sign in to comment.