UNPKG

@unruggable/gateways

Version:

Trustless Ethereum Multichain CCIP-Read Gateway

66 lines (65 loc) 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TrustedRollup = void 0; const rollup_js_1 = require("./rollup.cjs"); const utils_js_1 = require("./utils.cjs"); const cached_js_1 = require("./cached.cjs"); const VoidProvider_js_1 = require("./VoidProvider.cjs"); const constants_1 = require("ethers/constants"); const transaction_1 = require("ethers/transaction"); const hash_1 = require("ethers/hash"); class TrustedRollup extends rollup_js_1.AbstractRollup { factory; signingKey; latest; #signed = 0n; constructor(provider2, factory, signingKey) { super({ provider1: VoidProvider_js_1.VOID_PROVIDER, provider2 }); this.factory = factory; this.signingKey = signingKey; this.latest = new cached_js_1.CachedValue(async () => { const prover = await factory.latest(this.provider2, this.latestBlockTag); const stateRoot = await prover.fetchStateRoot(); const signedAt = Math.ceil(Date.now() / 1000); const hash = (0, hash_1.solidityPackedKeccak256)(['bytes', 'address', 'uint64', 'bytes32'], ['0x1900', constants_1.ZeroAddress, signedAt, stateRoot]); const signature = this.signingKey.sign(hash).serialized; return { index: this.#signed++, prover, stateRoot, signature, signedAt, }; }, 60000); } get signerAddress() { return (0, transaction_1.computeAddress)(this.signingKey); } get unfinalized() { return true; } async fetchLatestCommitIndex() { return (await this.latest.get()).index; } async _fetchParentCommitIndex(_commit) { return -1n; } async _fetchCommit(_index) { return this.latest.get(); } encodeWitness(commit, proofSeq) { return utils_js_1.ABI_CODER.encode(['(bytes,uint64,bytes32,bytes[],bytes)'], [ [ commit.signature, commit.signedAt, commit.stateRoot, proofSeq.proofs, proofSeq.order, ], ]); } windowFromSec(sec) { return sec; } } exports.TrustedRollup = TrustedRollup;