From a99bbd6d00afe75a1dca2205c37d7c84f4327380 Mon Sep 17 00:00:00 2001 From: Bartek Kiepuszewski Date: Sat, 6 Jan 2024 16:43:53 +0100 Subject: [PATCH] initial commit --- packages/optimism-decoder/src/analyze.ts | 2 + packages/optimism-decoder/src/decode.ts | 139 ++++++++++++++--------- 2 files changed, 86 insertions(+), 55 deletions(-) diff --git a/packages/optimism-decoder/src/analyze.ts b/packages/optimism-decoder/src/analyze.ts index 9b8632ac..fd846779 100644 --- a/packages/optimism-decoder/src/analyze.ts +++ b/packages/optimism-decoder/src/analyze.ts @@ -6,6 +6,7 @@ const ctcMapping: Record = { '0x56a76bcC92361f6DF8D75476feD8843EdC70e1C9': 'Metis', '0x6A1DB7d799FBA381F2a518cA859ED30cB8E1d41a': 'Metis 2.0', '0xfBd2541e316948B259264c02f370eD088E04c3Db': 'Boba Network', + '0x5f7f7f6DB967F0ef10BdA0678964DBA185d16c50': 'Lyra', } export async function analyzeTransaction( @@ -14,6 +15,7 @@ export async function analyzeTransaction( ) { const tx = await provider.getTransaction(txHash) const project = ctcMapping[tx.to ?? ''] ?? 'Unknown' + console.log('Tx submits data to', tx.to, 'hence it is', project) return { data: tx.data, diff --git a/packages/optimism-decoder/src/decode.ts b/packages/optimism-decoder/src/decode.ts index 6611e9ad..ec4004fc 100644 --- a/packages/optimism-decoder/src/decode.ts +++ b/packages/optimism-decoder/src/decode.ts @@ -24,70 +24,99 @@ export async function decodeSequencerBatch( data: string, fourBytesApi: FourBytesApi, ): Promise { - console.log('Decoding', kind, 'L1 Sequencer transaction batch...') + console.log('Decoding', kind, 'L1 Sequencer transaction batch ...') let reader = new BufferReader(Buffer.from(data.slice(2), 'hex')) - const methodName = reader.readBytes(4).toString('hex') - console.log('MethodName:', methodName) + if (kind === 'Lyra') { + const version = reader.readBytes(1).toString('hex') + console.log('Version:', version) + const channelId = reader.readBytes(16).toString('hex') + console.log('ChannelId:', channelId) + const frame_number = reader.readU16BE() + console.log('Frame Number:', frame_number) + const frame_data_length = reader.readU32BE() + console.log('Frame Data Length:', frame_data_length) + console.log(reader.left()) + const bytes = reader.readBytes(reader.left() - 1) + const inflated = zlib.inflateSync(bytes) + reader = new BufferReader(inflated) + const decompressedBytes = reader.readBytes(reader.left()) + console.log(add0x(decompressedBytes.toString('hex'))) + const decoded = ethers.utils.RLP.decode( + // TODO: why this is failing ???? + add0x(decompressedBytes.toString('hex')), + ) + console.log(decoded) + } else { + const methodName = reader.readBytes(4).toString('hex') + console.log('MethodName:', methodName) - if (kind === 'Metis' || kind === 'Metis 2.0') { - const chainId = reader.readBytes(32).toString('hex') - console.log('ChainId:', chainId) - } - const shouldStartAtElement = reader.readU40BE() - const totalElementsToAppend = reader.readU24BE() - const contextCount = reader.readU24BE() + if (kind === 'Metis' || kind === 'Metis 2.0') { + const chainId = reader.readBytes(32).toString('hex') + console.log('ChainId:', chainId) + } + const shouldStartAtElement = reader.readU40BE() + const totalElementsToAppend = reader.readU24BE() + const contextCount = reader.readU24BE() - console.log('Should start at Element:', shouldStartAtElement) - console.log('Total Elements to Append:', totalElementsToAppend) - console.log('contextCount:', contextCount) + console.log('Should start at Element:', shouldStartAtElement) + console.log('Total Elements to Append:', totalElementsToAppend) + console.log('contextCount:', contextCount) - const contexts = [] - for (let i = 0; i < contextCount; i++) { - const sequencerTxCount = reader.readU24BE() - const queueTxCount = reader.readU24BE() - const timestamp = reader.readU40BE() - const blockNumber = reader.readU40BE() - contexts.push({ - sequencerTxCount, - queueTxCount, - timestamp, - blockNumber, - }) - console.log(sequencerTxCount, queueTxCount, timestamp, blockNumber) - } + const contexts = [] + for (let i = 0; i < contextCount; i++) { + const sequencerTxCount = reader.readU24BE() + const queueTxCount = reader.readU24BE() + const timestamp = reader.readU40BE() + const blockNumber = reader.readU40BE() + contexts.push({ + sequencerTxCount, + queueTxCount, + timestamp, + blockNumber, + }) + console.log(sequencerTxCount, queueTxCount, timestamp, blockNumber) + } - if (contexts[0].blockNumber === 0 && kind === 'Optimism OVM 2.0') { - console.log( - 'Block number = 0 ? Transactions are compressed, nice.... Decompressing....', - ) - contexts.slice(1) // remove dummy context that indicates compressed transaction data - const bytes = reader.readBytes(reader.left()) - const inflated = zlib.inflateSync(bytes) - reader = new BufferReader(inflated) - } + if (contexts[0].blockNumber === 0 && kind === 'Optimism OVM 2.0') { + console.log( + 'Block number = 0 ? Transactions are compressed, nice.... Decompressing....', + ) + contexts.slice(1) // remove dummy context that indicates compressed transaction data + const bytes = reader.readBytes(reader.left()) + const inflated = zlib.inflateSync(bytes) + reader = new BufferReader(inflated) + } - const transactions = [] - for (const context of contexts) { - console.log('Block:', context.blockNumber, 'Timestamp:', context.timestamp) - for (let i = 0; i < context.sequencerTxCount; i++) { - const size = reader.readU24BE() - const raw = reader.readBytes(size).toString('hex') - const parsed = ethers.utils.parseTransaction(add0x(raw)) - const methodHash = parsed.data.slice(0, 10) - const methodSignature = await fourBytesApi.getMethodSignature(methodHash) - transactions.push(add0x(raw)) - console.log(' ', trimLong(add0x(raw)), methodHash, methodSignature) + const transactions = [] + for (const context of contexts) { + console.log( + 'Block:', + context.blockNumber, + 'Timestamp:', + context.timestamp, + ) + for (let i = 0; i < context.sequencerTxCount; i++) { + const size = reader.readU24BE() + const raw = reader.readBytes(size).toString('hex') + const parsed = ethers.utils.parseTransaction(add0x(raw)) + const methodHash = parsed.data.slice(0, 10) + const methodSignature = await fourBytesApi.getMethodSignature( + methodHash, + ) + transactions.push(add0x(raw)) + console.log(' ', trimLong(add0x(raw)), methodHash, methodSignature) + } } - } - console.log('Decoded', transactions.length, 'transactions') - console.log('Done decoding...') + console.log('Decoded', transactions.length, 'transactions') + console.log('Done decoding...') - return { - shouldStartAtElement, - totalElementsToAppend, - contexts, - transactions, + return { + shouldStartAtElement, + totalElementsToAppend, + contexts, + transactions, + } } }