@graphprotocol/toolshed
Version:
A collection of tools and utilities for the Graph Protocol Typescript components
86 lines • 3.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EIP712_RAV_TYPES = exports.EIP712_DOMAIN_VERSION = exports.EIP712_DOMAIN_NAME = void 0;
exports.generateSignedRAV = generateSignedRAV;
exports.recoverRAVSigner = recoverRAVSigner;
exports.generateSignerProof = generateSignerProof;
const ethers_1 = require("ethers");
exports.EIP712_DOMAIN_NAME = 'GraphTallyCollector';
exports.EIP712_DOMAIN_VERSION = '1';
exports.EIP712_RAV_TYPES = {
ReceiptAggregateVoucher: [
{ name: 'collectionId', type: 'bytes32' },
{ name: 'payer', type: 'address' },
{ name: 'serviceProvider', type: 'address' },
{ name: 'dataService', type: 'address' },
{ name: 'timestampNs', type: 'uint64' },
{ name: 'valueAggregate', type: 'uint128' },
{ name: 'metadata', type: 'bytes' },
],
};
/**
* Generates a signed RAV
* @param allocationId The allocation ID
* @param payer The payer
* @param serviceProvider The service provider
* @param dataService The data service
* @param timestampNs The timestamp in nanoseconds
* @param valueAggregate The value aggregate
* @param metadata The metadata
* @param signerPrivateKey The private key of the signer
* @param graphTallyCollectorAddress The address of the Graph Tally Collector contract
* @param chainId The chain ID
* @returns The encoded signed RAV calldata
*/
async function generateSignedRAV(allocationId, payer, serviceProvider, dataService, timestampNs, valueAggregate, metadata, signerPrivateKey, graphTallyCollectorAddress, chainId) {
// Create the domain for the EIP712 signature
const domain = {
name: exports.EIP712_DOMAIN_NAME,
version: exports.EIP712_DOMAIN_VERSION,
chainId,
verifyingContract: graphTallyCollectorAddress,
};
// Create the RAV data
const ravData = {
collectionId: ethers_1.ethers.zeroPadValue(allocationId, 32),
payer: payer,
serviceProvider: serviceProvider,
dataService: dataService,
timestampNs: timestampNs,
valueAggregate: valueAggregate,
metadata: metadata,
};
// Sign the RAV data
const signer = new ethers_1.Wallet(signerPrivateKey);
const signature = await signer.signTypedData(domain, exports.EIP712_RAV_TYPES, ravData);
// Return the signed RAV
return { rav: ravData, signature: signature };
}
function recoverRAVSigner(rav, signature, graphTallyCollectorAddress, chainId) {
const domain = {
name: exports.EIP712_DOMAIN_NAME,
version: exports.EIP712_DOMAIN_VERSION,
chainId,
verifyingContract: graphTallyCollectorAddress,
};
return ethers_1.ethers.verifyTypedData(domain, exports.EIP712_RAV_TYPES, rav, signature);
}
/**
* Generates a signer proof for authorizing a signer in the Graph Tally Collector
* @param graphTallyCollector The Graph Tally Collector contract
* @param signer The signer
* @param chainId The chain ID
* @param proofDeadline The deadline for the proof
* @param signerPrivateKey The private key of the signer
* @returns The encoded signer proof
*/
function generateSignerProof(proofDeadline, payer, signerPrivateKey, graphTallyCollectorAddress, chainId) {
// Create the message hash
const messageHash = ethers_1.ethers.keccak256(ethers_1.ethers.solidityPacked(['uint256', 'address', 'string', 'uint256', 'address'], [chainId, graphTallyCollectorAddress, 'authorizeSignerProof', proofDeadline, payer]));
// Convert to EIP-191 signed message hash (this is the proofToDigest)
const proofToDigest = ethers_1.ethers.hashMessage(ethers_1.ethers.getBytes(messageHash));
// Sign the message
const signer = new ethers_1.Wallet(signerPrivateKey);
return ethers_1.Signature.from(signer.signingKey.sign(proofToDigest)).serialized;
}
//# sourceMappingURL=graph-tally.js.map