UNPKG

@verax-attestation-registry/verax-sdk

Version:

Verax Attestation Registry SDK to interact with the subgraph and the contracts

226 lines 11.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const constants_1 = require("../utils/constants"); const BaseDataMapper_1 = __importDefault(require("./BaseDataMapper")); const DefaultPortal_1 = require("../abi/DefaultPortal"); const abiCoder_1 = require("../utils/abiCoder"); const PortalRegistry_1 = require("../abi/PortalRegistry"); const errorHandler_1 = require("../utils/errorHandler"); const transactionSender_1 = require("../utils/transactionSender"); class PortalDataMapper extends BaseDataMapper_1.default { constructor() { super(...arguments); this.typeName = "portal"; this.gqlInterface = `{ id ownerAddress modules isRevocable name description ownerName attestationCounter }`; } async simulateAttest(portalAddress, attestationPayload, validationPayloads, options) { const matchingSchema = await this.veraxSdk.schema.findOneById(attestationPayload.schemaId); if (!matchingSchema) { throw new Error("No matching Schema"); } const attestationData = (0, abiCoder_1.encode)(matchingSchema.schema, attestationPayload.attestationData); return this.simulatePortalContract(portalAddress, "attest", [ [attestationPayload.schemaId, attestationPayload.expirationDate, attestationPayload.subject, attestationData], validationPayloads, ], options?.value, options?.customAbi); } async attest(portalAddress, attestationPayload, validationPayloads, options) { const request = await this.simulateAttest(portalAddress, attestationPayload, validationPayloads, options); return (0, transactionSender_1.executeTransaction)(request, this.web3Client, this.walletClient, options?.waitForConfirmation); } async simulateBulkAttest(portalAddress, attestationPayloads, validationPayloads, options) { const attestationPayloadsArg = []; for (const attestationPayload of attestationPayloads) { const matchingSchema = await this.veraxSdk.schema.findOneById(attestationPayload.schemaId); if (!matchingSchema) { throw new Error("No matching Schema"); } const attestationData = (0, abiCoder_1.encode)(matchingSchema.schema, attestationPayload.attestationData); attestationPayloadsArg.push([ attestationPayload.schemaId, attestationPayload.expirationDate, attestationPayload.subject, attestationData, ]); } return this.simulatePortalContract(portalAddress, "bulkAttest", [attestationPayloadsArg, validationPayloads], options?.value, options?.customAbi); } async simulateAttestV2(portalAddress, attestationPayload, validationPayloads, options) { const matchingSchema = await this.veraxSdk.schema.findOneById(attestationPayload.schemaId); if (!matchingSchema) { throw new Error("No matching Schema"); } const attestationData = (0, abiCoder_1.encode)(matchingSchema.schema, attestationPayload.attestationData); return this.simulatePortalContract(portalAddress, "attestV2", [ [attestationPayload.schemaId, attestationPayload.expirationDate, attestationPayload.subject, attestationData], validationPayloads, ], options?.value, options?.customAbi); } async attestV2(portalAddress, attestationPayload, validationPayloads, options) { const request = await this.simulateAttestV2(portalAddress, attestationPayload, validationPayloads, options); return (0, transactionSender_1.executeTransaction)(request, this.web3Client, this.walletClient, options?.waitForConfirmation); } async bulkAttest(portalAddress, attestationPayloads, validationPayloads, options) { const request = await this.simulateBulkAttest(portalAddress, attestationPayloads, validationPayloads, options); return (0, transactionSender_1.executeTransaction)(request, this.web3Client, this.walletClient, options?.waitForConfirmation); } async simulateRevoke(portalAddress, attestationId, options) { return this.simulatePortalContract(portalAddress, "revoke", [attestationId], options?.value, options?.customAbi); } async revoke(portalAddress, attestationId, options) { const request = await this.simulateRevoke(portalAddress, attestationId, options); return (0, transactionSender_1.executeTransaction)(request, this.web3Client, this.walletClient, options?.waitForConfirmation); } async simulateBulkRevoke(portalAddress, attestationIds, options) { return this.simulatePortalContract(portalAddress, "bulkRevoke", [attestationIds], options?.value, options?.customAbi); } async bulkRevoke(portalAddress, attestationIds, options) { const request = await this.simulateBulkRevoke(portalAddress, attestationIds, options); return (0, transactionSender_1.executeTransaction)(request, this.web3Client, this.walletClient, options?.waitForConfirmation); } async simulateReplace(portalAddress, attestationId, attestationPayload, validationPayloads, options) { const matchingSchema = await this.veraxSdk.schema.findOneById(attestationPayload.schemaId); if (!matchingSchema) { throw new Error("No matching Schema"); } const attestationData = (0, abiCoder_1.encode)(matchingSchema.schema, attestationPayload.attestationData); return this.simulatePortalContract(portalAddress, "replace", [ attestationId, [attestationPayload.schemaId, attestationPayload.expirationDate, attestationPayload.subject, attestationData], validationPayloads, ], options?.value, options?.customAbi); } async replace(portalAddress, attestationId, attestationPayload, validationPayloads, options) { const request = await this.simulateReplace(portalAddress, attestationId, attestationPayload, validationPayloads, options); return (0, transactionSender_1.executeTransaction)(request, this.web3Client, this.walletClient, options?.waitForConfirmation); } async simulateBulkReplace(portalAddress, attestationIds, attestationPayloads, validationPayloads, options) { const attestationPayloadsArg = []; for (const attestationPayload of attestationPayloads) { const matchingSchema = await this.veraxSdk.schema.findOneById(attestationPayload.schemaId); if (!matchingSchema) { throw new Error("No matching Schema"); } const attestationData = (0, abiCoder_1.encode)(matchingSchema.schema, attestationPayload.attestationData); attestationPayloadsArg.push([ attestationPayload.schemaId, attestationPayload.expirationDate, attestationPayload.subject, attestationData, ]); } return this.simulatePortalContract(portalAddress, "bulkReplace", [attestationIds, attestationPayloadsArg, validationPayloads], options?.value, options?.customAbi); } async bulkReplace(portalAddress, attestationIds, attestationPayloads, validationPayloads, options) { const request = await this.simulateBulkReplace(portalAddress, attestationIds, attestationPayloads, validationPayloads, options); return (0, transactionSender_1.executeTransaction)(request, this.web3Client, this.walletClient, options?.waitForConfirmation); } async simulateRegister(id, name, description, isRevocable, ownerName) { return this.simulatePortalRegistryContract("register", [id, name, description, isRevocable, ownerName]); } async register(id, name, description, isRevocable, ownerName, options) { const request = await this.simulateRegister(id, name, description, isRevocable, ownerName); return (0, transactionSender_1.executeTransaction)(request, this.web3Client, this.walletClient, options?.waitForConfirmation); } async simulateDeployDefaultPortal(modules, name, description, isRevocable, ownerName) { return this.simulatePortalRegistryContract("deployDefaultPortal", [ modules, name, description, isRevocable, ownerName, ]); } async deployDefaultPortal(modules, name, description, isRevocable, ownerName, options) { const request = await this.simulateDeployDefaultPortal(modules, name, description, isRevocable, ownerName); return (0, transactionSender_1.executeTransaction)(request, this.web3Client, this.walletClient, options?.waitForConfirmation); } async getPortalByAddress(address) { return await this.web3Client.readContract({ address: this.conf.portalRegistryAddress, abi: PortalRegistry_1.abiPortalRegistry, functionName: "getPortal", args: [address], }); } async getPortalOwner(address) { return await this.web3Client.readContract({ address: this.conf.portalRegistryAddress, abi: PortalRegistry_1.abiPortalRegistry, functionName: "getPortalOwner", args: [address], }); } async getPortalRevocability(address) { return await this.web3Client.readContract({ address: this.conf.portalRegistryAddress, abi: PortalRegistry_1.abiPortalRegistry, functionName: "getPortalRevocability", args: [address], }); } async getPortalsNumber() { return super.findTotalCount(); } async isPortalRegistered(id) { return this.executePortalRegistryReadMethod("isRegistered", [id]); } async executePortalRegistryReadMethod(functionName, args) { return this.web3Client.readContract({ abi: PortalRegistry_1.abiPortalRegistry, address: this.conf.portalRegistryAddress, functionName, args, }); } async simulatePortalRegistryContract(functionName, args) { if (!this.walletClient) throw new Error("VeraxSDK - Wallet not available"); try { const { request } = await this.web3Client.simulateContract({ address: this.conf.portalRegistryAddress, abi: PortalRegistry_1.abiPortalRegistry, functionName, account: this.walletClient.account, args, }); return request; } catch (err) { (0, errorHandler_1.handleError)(constants_1.ActionType.Simulation, err); } } async simulatePortalContract(portalAddress, functionName, args, value = 0n, customAbi) { if (!this.walletClient) throw new Error("VeraxSDK - Wallet not available"); const abi = [...DefaultPortal_1.abiDefaultPortal, ...(customAbi || [])]; try { const { request } = await this.web3Client.simulateContract({ address: portalAddress, abi, functionName, account: this.walletClient.account, args, value, }); return request; } catch (err) { (0, errorHandler_1.handleError)(constants_1.ActionType.Simulation, err); } } } exports.default = PortalDataMapper; //# sourceMappingURL=PortalDataMapper.js.map