web3-plugin-eas
Version:
Web3.js plugin for Ethereum Attestation Service(EAS)
83 lines (82 loc) • 3.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EASPlugin = void 0;
const web3_1 = require("web3");
const utils_1 = require("./utils");
const schemaRegistry_1 = __importDefault(require("./abis/schemaRegistry"));
const eas_1 = __importDefault(require("./abis/eas"));
class EASPlugin extends web3_1.Web3PluginBase {
constructor() {
super(...arguments);
this.pluginNamespace = "eas";
}
/**
* This method creates EAS's SchemaRegistry Contract instance of connected chain
* @param address - SchemaRegistry Contract Address of connected chain
* @returns SchemaRegistry Contract instance
* @throws Error if address is not a valid address
* @example
* ```ts
* const web3 = new Web3("http://127.0.0.1:8545");
* web3.registerPlugin(new EASPlugin());
* const SchemaRegistry = web3.eas.SchemaRegistry(SchemaRegistryAddress);
* ```
*/
schemaRegistry(address) {
if (!web3_1.validator.isAddress(address))
throw new Error("EAS Plugin: Invalid Schema Registry Address");
const schemaRegistryContract = new web3_1.Contract(schemaRegistry_1.default, address);
schemaRegistryContract.link(this);
return schemaRegistryContract;
}
/**
* This method creates EAS's EAS Contract instance of connected chain
* @param address - EAS Contract Address of connected chain
* @returns EASCore Contract instance
* @throws Error if address is not a valid address
* @example
* ```ts
* const web3 = new Web3("http://127.0.0.1:8545");
* web3.registerPlugin(new EASPlugin());
* const easCore = web3.eas.easCore(EASAddress);
* ```
*/
easCore(address) {
if (!web3_1.validator.isAddress(address))
throw new Error("EAS Plugin: Invalid EAS Address");
const easContract = new web3_1.Contract(eas_1.default, address);
easContract.link(this);
return easContract;
}
/**
* This method returns EAS's Contract Addresses of the given chain
* * If no chainId is provided, it will use the connected chainId.
* @param chainId ChainId of the network to get contract addresses of
* @returns Contract Addresses object of the chain
* @throws Error if chainId is not supported
* @example
* ```ts
* const web3 = new Web3("http://127.0.0.1:8545");
* web3.registerPlugin(new EASPlugin());
* const addresses = web3.eas.getContractAddresses(1); //ChainId of Ethereum Mainnet
* // or
* const addresses = await web3.eas.getContractAddresses(); //returns addresses of connected chain
* ```
*/
async getContractAddresses(chainId) {
if (!chainId) {
chainId = await web3_1.eth.getChainId(this, {
number: web3_1.FMT_NUMBER.NUMBER,
bytes: web3_1.FMT_BYTES.HEX
});
}
const addressesObj = utils_1.contractAddresses.find((config) => config.id === chainId);
if (!addressesObj)
throw new Error(`EAS Plugin: Unsupported ChainId ${chainId}`);
return addressesObj;
}
}
exports.EASPlugin = EASPlugin;