Ethereum 验证服务 - TypeScript/JavaScript SDK
此寄存器包含Ethereum Attestation Service SDK,用于与Ethereum Attestation Service 协议交互.
目录
- 安装 EAS SDK
- 使用 EAS SDK
- 获得证明
- 估计交易用气体
- 创建链上测试
- 撤销链上检验
- 创建脱链测试
- 创建代理链上检验
- 撤销委托的链上检验
- 创建时间戳
- 撤销脱链检验
- 验证脱链测试
- 正在注册Schema
- 获取 Schema 信息
- 使用私有数据类
- 示例: 以私人数据创建验证
安装 EAS SDK
要安装 EAS SDK, 请在您的工程目录中运行以下命令 :
yarn add @ethereum-attestation-service/eas-sdk
或
npm install @ethereum-attestation-service/eas-sdk
或
pnpm add @ethereum-attestation-service/eas-sdk
使用 EAS SDK
导入并初始化库 :
import { EAS, Offchain, SchemaEncoder, SchemaRegistry } from '@ethereum-attestation-service/eas-sdk';
import { ethers } from 'ethers';
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');
// Connects an ethers style provider/signingProvider to perform read/write functions.
// MUST be a signer to do write operations!
eas.connect(provider);
获得证明
这个 getAttestation 函数允许您获取给定的 UID 的链上验证。 此函数返回包含关于证明的证明对象,例如计划、收件人、证明人等等.
使用量
import { EAS, NO_EXPIRATION } from '@ethereum-attestation-service/eas-sdk';
const eas = new EAS(EASContractAddress);
eas.connect(provider);
const uid = '0xff08bbf3d3e6e0992fc70ab9b9370416be59e87897c3d42b20549901d2cccc3e';
const attestation = await eas.getAttestation(uid);
console.log(attestation);
产出
这个 getAttestation 函数返回具有下列属性的验证对象:
uid:证明书的独特标识符.schema: 与证明书有关的计划标识符.refUID: 证明的参考UID(如有).time:证明书创建时的Unix时间戳.expirationTime: 证明到期时的Unix时间戳(0为无过期).revocationTime: 如果适用,撤销证明时的Unix时间戳.recipient: 证明的收件人Ethereum地址.attester:创造证明书的证明人伊特鲁姆地址.revocable: 表明证明书是否可以撤消的布尔.data: 以字节格式提供的证明数据.
示例输出 :
{
uid: '0x5134f511e0533f997e569dac711952dde21daf14b316f3cce23835defc82c065',
schema: '0x27d06e3659317e9a4f8154d1e849eb53d43d91fb4f219884d1684f86d797804a',
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
time: 1671219600,
expirationTime: NO_EXPIRATION,
revocationTime: 1671219636,
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
attester: '0x1e3de6aE412cA218FD2ae3379750388D414532dc',
revocable: true,
data: '0x0000000000000000000000000000000000000000000000000000000000000000'
}
估计交易用气体
这个 estimateGas 方法允许您在发送前估算任何交易的天然气成本。 这对于在执行交易前确定天然气需求和费用是有用的。 通过EAS SDK方法返回的所有交易对象都可以使用这种方法.
示例:估计供证明使用的气体
import { EAS, NO_EXPIRATION, 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 encodedData = schemaEncoder.encodeData([
{ name: 'eventId', value: 1, type: 'uint256' },
{ name: 'voteIndex', value: 1, type: 'uint8' }
]);
const schemaUID = '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995';
const transaction = await eas.attest({
schema: schemaUID,
data: {
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: NO_EXPIRATION,
revocable: true,
data: encodedData
}
});
// Estimate gas before sending the transaction
const estimatedGas = await transaction.estimateGas();
console.log('Estimated gas:', estimatedGas.toString());
// Now send the transaction
const newAttestationUID = await transaction.wait();
console.log('New attestation UID:', newAttestationUID);
示例:估计气体用于还原
const transaction = await eas.revoke({
schema: '0x85500e806cf1e74844d51a20a6d893fe1ed6f6b0738b50e43d774827d08eca61',
data: { uid: '0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a' }
});
// Estimate gas before sending the transaction
const estimatedGas = await transaction.estimateGas();
console.log('Estimated gas for revocation:', estimatedGas.toString());
// Now send the transaction
await transaction.wait();
创建链上测试
这个 attest 函数允许您为特定方案创建链上验证。 3⁄4 ̄ ̧漯B
schema: 正在为其创建证明的图案的UID.data: 含有下列属性的对象:recipient: 证明的收件人Ethereum地址.expirationTime: 代表证明到期时间的Unix时间戳. 使用0没有过期.revocable: 表明证明书是否可以撤消的布尔.refUID: (可选) 引用证明的UID。 使用ZERO_BYTES32如果没有参考文献.data: 证明的编码数据,应使用SchemaEncoder课.value: (可选) 随证明书一起发出的ETH值.
函数返回 a Promise 它决定了UID 新建的证明.
示例: 创建链上测试
import { EAS, NO_EXPIRATION, 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 encodedData = schemaEncoder.encodeData([
{ name: 'eventId', value: 1, type: 'uint256' },
{ name: 'voteIndex', value: 1, type: 'uint8' }
]);
const schemaUID = '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995';
const transaction = await eas.attest({
schema: schemaUID,
data: {
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: NO_EXPIRATION,
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
data: encodedData
}
});
// Estimate gas for the transaction before sending it
const estimatedGas = await transaction.estimateGas();
console.log('Estimated gas:', estimatedGas.toString());
const newAttestationUID = await transaction.wait();
console.log('New attestation UID:', newAttestationUID);
console.log('Transaction receipt:', transaction.receipt);
示例: 创建多链测试
import { EAS, NO_EXPIRATION, 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 encodedData = schemaEncoder.encodeData([
{ name: 'eventId', value: 1, type: 'uint256' },
{ name: 'voteIndex', value: 1, type: 'uint8' }
]);
const encodedData2 = schemaEncoder.encodeData([
{ name: 'eventId', value: 10, type: 'uint256' },
{ name: 'voteIndex', value: 2, type: 'uint8' }
]);
const schemaUID = '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995';
const transaction = await eas.multiAttest([
{
schema: schemaUID,
data: [
{
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: NO_EXPIRATION,
revocable: true, // Be aware that if your schema is not revocable, this MUST be false
data: encodedData
},
{
recipient: '0xA1207F3BBa224E2c9c3c6D5aF63D0eb1582Ce587',
expirationTime: NO_EXPIRATION,
revocable: false,
data: encodedData2
}
]
}
]);
const newAttestationUID = await transaction.wait();
console.log('New attestation UID:', newAttestationUID);
console.log('Transaction receipt:', transaction.receipt);
撤销链上检验
这个 revoke 函数允许您撤销链上验证。 3⁄4 ̄ ̧漯B
schema: 被吊销证明书的图案UID.data: 含有下列属性的对象:uid: 撤销证明书的UID.value: (可选) 撤销时发送的ETH值 .
实例:吊销链上检验
const transaction = await eas.revoke({
schema: '0x85500e806cf1e74844d51a20a6d893fe1ed6f6b0738b50e43d774827d08eca61',
data: { uid: '0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a' }
});
// Optional: Wait for transaction to be validated
await transaction.wait();
创建脱链测试
要创建脱链证明,您可以使用 signOffchainAttestation 函数由 EAS SDK 中的离链类提供。 举个例子:
示例: 创建脱链测试
import { EAS, NO_EXPIRATION, 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);
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 encodedData = schemaEncoder.encodeData([
{ 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: NO_EXPIRATION, // 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
);
此函数将返回包含 UID 、 签名和 证明数据的签名脱链验证对象 。 然后可以和预定的收件人分享此对象,或者存储它供将来使用.
版本
由于离链证明协议在不断演变和完善,我们最近增加了版本,以帮助应用程序支持更古老和更新型的证明. 从版本开始 1,我们增加了一个 version 字段到其输入的数据,二者无缝支持 signOffchainAttestation 财务报告和审定财务报表 verifyOffchainAttestationSignature 函数.
请注意使用 getOffchainUID 前一个遗留版本的函数需要通过 { version: 0 } 当然.
创建代理链上检验
这个 attestByDelegation 函数允许您为特定方案创建链上授权的证明。 3⁄4 ̄ ̧漯B
schema: 正在为其创建证明的图案的UID.data: 含有下列属性的对象:recipient: 证明的收件人Ethereum地址.expirationTime: 代表证明到期时间的Unix时间戳. 使用0没有过期.revocable: 表明证明书是否可以撤消的布尔.refUID: (可选) 引用证明的UID。 使用ZERO_BYTES32如果没有参考文献.data: 证明的编码数据,应使用SchemaEncoder课.value: (可选) 随证明书一起发出的ETH值.
attester:证明人的地址.signature: EIP712型签名(r, (中文(简体) ),s,以及v(使用signDelegatedAttestation函数).deadline: 代表签名到期时间的Unix时间戳.
函数返回 a Promise 它决定了UID 新建的证明.
示例: 创建代理链上验证
import { EAS, NO_EXPIRATION, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
const eas = new EAS(EASContractAddress);
// Use a different account to send and pay for the attestation.
eas.connect(sender);
const delegated = await eas.getDelegated();
// 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 encodedData = schemaEncoder.encodeData([
{ name: 'eventId', value: 1, type: 'uint256' },
{ name: 'voteIndex', value: 1, type: 'uint8' }
]);
const signer = new ethers.Wallet(privateKey, provider);
// Please note that if nonce isn't provided explicitly, we will try retrieving it onchain.
const response = await delegated.signDelegatedAttestation(
{
schema: '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995',
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: NO_EXPIRATION, // Unix timestamp of when attestation expires (0 for no expiration)
revocable: true,
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
data: encodedData,
deadline: NO_EXPIRATION, // Unix timestamp of when signature expires (0 for no expiration)
value: 0n
},
signer
);
const transaction = await eas.attestByDelegation({
schema: '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995',
data: {
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: NO_EXPIRATION, // Unix timestamp of when attestation expires (0 for no expiration)
revocable: true,
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
data: encodedData
},
signature: response.signature,
attester: await signer.getAddress(),
deadline: 0n // Unix timestamp of when signature expires (0 for no expiration)
});
const newAttestationUID = await transaction.wait();
console.log('New attestation UID:', newAttestationUID);
console.log('Transaction receipt:', transaction.receipt);
撤销委托的链上检验
这个 revokeByDelegation 函数允许您为特定证明创建授权的链上吊销。 3⁄4 ̄ ̧漯B
schema: 被吊销证明书的图案UID.data: 含有下列属性的对象:uid: 撤销证明书的UID.value: (可选) 撤销时发送的ETH值 .
revoker:撤销人地址.signature: EIP712型签名(r, (中文(简体) ),s,以及v(使用signDelegatedRevocation函数).deadline: 代表签名到期时间的Unix时间戳.
函数返回 a Promise 它决定了UID 新建的证明.
实例:撤销委托的链上检验
import { EAS, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
const eas = new EAS(EASContractAddress);
// Use a different account to send and pay for the attestation.
eas.connect(sender);
const delegated = await eas.getDelegated();
const signer = new ethers.Wallet(privateKey, provider);
// Please note that if nonce isn't provided explicitly, we will try retrieving it onchain.
const response = await delegated.signDelegatedRevocation(
{
schema: '0x85500e806cf1e74844d51a20a6d893fe1ed6f6b0738b50e43d774827d08eca61',
uid: '0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a',
deadline: 0n, // Unix timestamp of when signature expires (0 for no expiration)
value: 0n
},
signer
);
const transaction = await eas.revokeByDelegation({
schema: '0xb16fa048b0d597f5a821747eba64efa4762ee5143e9a80600d0005386edfc995',
data: {
uid: '0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a',
},
signature: response.signature,
revoker: await signer.getAddress(),
deadline: 0n // Unix timestamp of when signature expires (0 for no expiration)
});
// Optional: Wait for transaction to be validated
await transaction.wait();
创建时间戳
为了给链上脱链证明UID打上时间戳,您可以使用 timestamp 函数由 EAS SDK 提供。 举个例子:
import { EAS } from '@ethereum-attestation-service/eas-sdk';
const eas = new EAS(EASContractAddress);
eas.connect(provider);
const uid = '0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a';
const transaction = await eas.timestamp(uid);
// Optional: Wait for the transaction to be validated
await transaction.wait();
要为任何数据创建时间戳,请使用 timestamp 函数由 EAS SDK 提供。 举个例子:
import { EAS } from '@ethereum-attestation-service/eas-sdk';
const eas = new EAS(EASContractAddress);
eas.connect(provider);
const data = ethers.encodeBytes32String('0x1234');
const transaction = await eas.timestamp(data);
// Optional: Wait for transaction to be validated
await transaction.wait();
为多个数据创建时间戳,您可以使用 multiTimestamp 函数 :
import { EAS } from '@ethereum-attestation-service/eas-sdk';
const eas = new EAS(EASContractAddress);
eas.connect(provider);
const data1 = ethers.encodeBytes32String('0x3e23b395b2bd2d37dd0f6e4148ac6b9e7ed22f2215107958f95cc1489e4e6289');
const data2 = ethers.encodeBytes32String('0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a');
const transaction = await eas.multiTimestamp([data1, data2]);
// Optional: Wait for transaction to be validated
await transaction.wait();
撤销脱链检验
要撤销链外证明,可以使用 revokeOffchain 函数由 EAS SDK 提供。 举个例子:
import { EAS } from '@ethereum-attestation-service/eas-sdk';
const eas = new EAS(EASContractAddress);
eas.connect(provider);
const data = ethers.encodeBytes32String('0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a');
const transaction = await eas.revokeOffchain(data);
// Optional: Wait for transaction to be validated
await transaction.wait();
要撤销多个链外证明,请使用 multiRevokeOffchain 函数 :
import { EAS } from '@ethereum-attestation-service/eas-sdk';
const eas = new EAS(EASContractAddress);
eas.connect(provider);
const data1 = ethers.encodeBytes32String('0x6776de8122c352b4d671003e58ca112aedb99f34c629a1d1fe3b332504e2943a');
const data2 = ethers.encodeBytes32String('0x3e23b395b2bd2d37dd0f6e4148ac6b9e7ed22f2215107958f95cc1489e4e6289');
const transaction = await eas.multiRevokeOffchain([data1, data2]);
// Optional: Wait for transaction to be validated
await transaction.wait();
验证脱链测试
为了核实脱链证明,您可以使用 verifyOffchainAttestationSignature 函数由 EAS SDK 提供。 举个例子:
import { OffchainAttestationVersion, Offchain, OffchainConfig } from "@ethereum-attestation-service/eas-sdk";
const attestation = {
// your offchain attestation
sig: {
domain: {
name: 'EAS Attestation',
version: '0.26',
chainId: 1,
verifyingContract: '0xA1207F3BBa224E2c9c3c6D5aF63D0eb1582Ce587'
},
primaryType: 'Attest',
types: {
Attest: []
},
signature: {
r: '',
s: '',
v: 28
},
uid: '0x5134f511e0533f997e569dac711952dde21daf14b316f3cce23835defc82c065',
message: {
version: OffchainAttestationVersion.Version2,
schema: '0x27d06e3659317e9a4f8154d1e849eb53d43d91fb4f219884d1684f86d797804a',
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
time: 1671219600,
expirationTime: NO_EXPIRATION,
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
attester: '0x1e3de6aE412cA218FD2ae3379750388D414532dc',
revocable: true,
data: '0x0000000000000000000000000000000000000000000000000000000000000000'
}
},
signer: '0x1e3de6aE412cA218FD2ae3379750388D414532dc'
};
const EAS_CONFIG: OffchainConfig = {
address: attestation.sig.domain.verifyingContract,
version: attestation.sig.domain.version,
chainId: attestation.sig.domain.chainId
};
const offchain = new Offchain(EAS_CONFIG, OffchainAttestationVersion.Version2);
const isValidAttestation = offchain.verifyOffchainAttestationSignature(attestation.signer, attestation.sig);
正在注册Schema
要注册新的计划, 您可以使用 register 函数由 EAS SDK 提供。 3⁄4 ̄ ̧漯B
schema: 定义要验证的数据结构的 schema 字符串.resolverAddress:负责管理"计划"的解决者Ethereum地址.revocable: 布尔值, 表示是否可撤销用此方案创建的证明 .
以下是如何注册新计划的例子:
import { SchemaRegistry } from '@ethereum-attestation-service/eas-sdk';
import { ethers } from 'ethers';
const schemaRegistryContractAddress = '0xYourSchemaRegistryContractAddress';
const schemaRegistry = new SchemaRegistry(schemaRegistryContractAddress);
schemaRegistry.connect(signer);
const schema = 'uint256 eventId, uint8 voteIndex';
const resolverAddress = '0x0a7E2Ff54e76B8E6659aedc9103FB21c038050D0'; // Sepolia 0.26
const revocable = true;
const transaction = await schemaRegistry.register({
schema,
resolverAddress,
revocable
});
// Optional: Wait for transaction to be validated
await transaction.wait();
在注册一个计划后,您可以使用它的UID创建带有指定结构的证明.
获取 Schema 信息
要获取特定图案的图案信息, 您可以使用 getSchema 函数由 EAS SDK 提供。 举个例子:
import { SchemaRegistry } from "@ethereum-attestation-service/eas-sdk";
const schemaRegistryContractAddress = "0x0a7E2Ff54e76B8E6659aedc9103FB21c038050D0"; // Sepolia 0.26
const schemaRegistry = new SchemaRegistry(schemaRegistryContractAddress);
schemaRegistry.connect(provider);
const schemaUID = "0xYourSchemaUID";
const schemaRecord = await schemaRegistry.getSchema({ uid: schemaUID });
console.log(schemaRecord);
// Example Output
{
uid: '0xYourSchemaUID',
schema: 'bytes32 proposalId, bool vote',
resolver: '0xResolverAddress',
revocable: true
}
在输出中,您将接收一个包含 schema UID, schema 字符串, 解析器地址, 以及布尔表示 schema 是否可撤消的对象 .
使用私有数据类
这个 PrivateData 课程允许您使用默克勒树创建、证明和验证私人数据。 这对于建立证明是有用的,在这里,你想要有选择地只透露某些信息,而同时又不透露其他信息.
创建私有数据
要创建私人数据,您需要初始化一个 PrivateData 实例 MerkleValue 对象 :
import { MerkleValue, PrivateData } from '@ethereum-attestation-service/eas-sdk';
const values: MerkleValue[] = [
{ type: 'string', name: 'name', value: 'Alice Johnson' },
{ type: 'uint256', name: 'age', value: 28 },
{ type: 'bool', name: 'isStudent', value: false },
{ type: 'address', name: 'wallet', value: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e' },
{ type: 'bytes32', name: 'dataHash', value: ethers.id('confidential information') }
];
const privateData = new PrivateData(values);
获得完整的默克勒树
您可以取回完整的默克勒树, 包括根和所有值的盐类:
const fullTree = privateData.getFullTree();
console.log('Merkle Root:', fullTree.root);
生成多文件
要有选择地只披露某些信息,可以生成多防:
const proofIndexes = [0, 2, 4]; // Proving name, isStudent, and dataHash
const multiProof = privateData.generateMultiProof(proofIndexes);
验证多文件
用于验证对已知的默克勒根的多防伪:
const isValid = PrivateData.verifyMultiProof(fullTree.root, multiProof);
console.log('Is Multi-Proof Valid?', isValid);
正在验证全树
您也可以验证整个默克勒树的完整性 :
const calculatedRoot = PrivateData.verifyFullTree(fullTree);
console.log('Is Full Tree Valid?', calculatedRoot === fullTree.root);
示例: 以私人数据创建验证
举个例子来说明你如何使用 PrivateData 与EAS SDK一起编写用私人数据证明:
import { EAS, NO_EXPIRATION, MerkleValue, PrivateData, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk';
import { ethers } from 'ethers';
// Initialize EAS
const eas = new EAS(EASContractAddress);
eas.connect(signer);
// Create private data
const values: MerkleValue[] = [
{ type: 'string', name: 'name', value: 'Alice Johnson' },
{ type: 'uint256', name: 'age', value: 28 },
{ type: 'bool', name: 'isStudent', value: false },
{ type: 'address', name: 'wallet', value: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e' },
{ type: 'bytes32', name: 'dataHash', value: ethers.id('confidential information') }
];
const privateData = new PrivateData(values);
const fullTree = privateData.getFullTree();
// Create an attestation with the Merkle root
const schemaEncoder = new SchemaEncoder('bytes32 privateData');
const encodedData = schemaEncoder.encodeData([{ name: 'privateData', value: fullTree.root, type: 'bytes32' }]);
// Private data schema
const schemaUID = '0x20351f973fdec1478924c89dfa533d8f872defa108d9c3c6512267d7e7e5dbc2';
const transaction = await eas.attest({
schema: schemaUID,
data: {
recipient: '0xFD50b031E778fAb33DfD2Fc3Ca66a1EeF0652165',
expirationTime: NO_EXPIRATION,
revocable: true,
data: encodedData
}
});
// Estimate gas for the transaction before sending it
const estimatedGas = await transaction.estimateGas();
console.log('Estimated gas:', estimatedGas.toString());
const newAttestationUID = await transaction.wait();
console.log('New attestation UID:', newAttestationUID);
// Generate a multi-proof to selectively reveal some data
const proofIndexes = [0, 2]; // Revealing only name and isStudent
const multiProof = privateData.generateMultiProof(proofIndexes);
console.log('Multi-proof for selective reveal:', multiProof);
在这个例子中,我们创造了一个证明,其中只包括了私人数据的默克勒根. 实际数据仍然是私有的,但我们可以有选择地用多防能揭示部分数据. 这一证明的接受者可以验证被验证的默克勒根的多防能,以确认所披露数据的有效性而无需看到整个数据集.