From dc3e8c204a222692fb7440db32a3558e87d18168 Mon Sep 17 00:00:00 2001 From: dzimiks Date: Tue, 17 Sep 2024 12:02:46 +0200 Subject: [PATCH 1/4] fix: update transaction handler --- packages/snap/snap.manifest.json | 2 +- packages/snap/src/index.ts | 5 +++- packages/snap/src/tenderly/simulation.ts | 17 +++++++------- packages/snap/src/tenderly/utils.ts | 30 ++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/packages/snap/snap.manifest.json b/packages/snap/snap.manifest.json index 56310ae..839fb9b 100644 --- a/packages/snap/snap.manifest.json +++ b/packages/snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/tenderly/tenderly-snap.git" }, "source": { - "shasum": "vNrNCKPLLktsGjVi1vgs3YKLy4zdPJZ4yMueXNGvWTA=", + "shasum": "Vvfzy7pDNGsGUO2z4kRqOkAurOomEleHjKiXzcdLWf0=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/snap/src/index.ts b/packages/snap/src/index.ts index f950983..43c6111 100644 --- a/packages/snap/src/index.ts +++ b/packages/snap/src/index.ts @@ -51,11 +51,13 @@ export const onRpcRequest: OnRpcRequestHandler = ({ origin, request }) => { * * @param args - The request handler args as object. * @param args.transaction - The transaction to handle. - * @param args.transactionOrigin - The transaction origin. + * @param args.transactionOrigin - The transaction origin. + * @param args.chainId - The chain ID of the transaction. */ export const onTransaction: OnTransactionHandler = async ({ transaction, transactionOrigin, + chainId, }) => { if (!isObject(transaction) || !hasProperty(transaction, 'to')) { return { @@ -67,6 +69,7 @@ export const onTransaction: OnTransactionHandler = async ({ } const simulationResponse = await simulate( + chainId, transaction, transactionOrigin || '', ); diff --git a/packages/snap/src/tenderly/simulation.ts b/packages/snap/src/tenderly/simulation.ts index 628b022..d5be8a9 100644 --- a/packages/snap/src/tenderly/simulation.ts +++ b/packages/snap/src/tenderly/simulation.ts @@ -3,7 +3,7 @@ import { Json } from '@metamask/utils'; import { TenderlyApi, TenderlySnapVersion } from '../constants'; import { fetchCredentials, TenderlyCredentials } from './credentials-access'; import { formatResponse, formatSimulationUrl } from './formatter'; -import { hex2int, requestSnapPrompt } from './utils'; +import { hex2int, parseChainId, requestSnapPrompt } from './utils'; /** * Updates the credentials associated with Tenderly project. @@ -37,13 +37,19 @@ export async function fetchPublicTenderlyNetworks() { * handles any errors returned by the API, and if there are no errors, * formats the response received from the Tenderly API for output. * + * @param chainIdOrigin - The chain ID of the transaction. * @param transaction - The transaction to simulate. * @param transactionOrigin - The origin of the transaction. */ export async function simulate( + chainIdOrigin: string, transaction: { [key: string]: Json }, transactionOrigin: string, ): Promise { + if (!chainIdOrigin) { + throw new Error('Chain ID is not provided.'); + } + const credentials: TenderlyCredentials | null = await fetchCredentials(); if (!credentials) { @@ -54,13 +60,8 @@ export async function simulate( ]); } - // Get chain id - const chainId = await ethereum.request({ method: 'eth_chainId' }); - const networkId = hex2int(chainId as string); - - if (!chainId) { - throw new Error('Chain ID is not provided.'); - } + const chainId: string = parseChainId(chainIdOrigin); + const networkId = hex2int(chainId); // Fetch Tenderly-supported networks const tenderlyNetworks = await fetchPublicTenderlyNetworks(); diff --git a/packages/snap/src/tenderly/utils.ts b/packages/snap/src/tenderly/utils.ts index 99b96c8..7ab47e2 100644 --- a/packages/snap/src/tenderly/utils.ts +++ b/packages/snap/src/tenderly/utils.ts @@ -107,3 +107,33 @@ export const isTenderlyDomain = (origin: string) => { return false; } }; + +/** + * Parses a chain ID string and returns the formatted hexadecimal chain ID. + * + * This function takes a chain ID string in the format "eip155:networkId", + * extracts the network ID, and returns it as a hexadecimal string + * prefixed with "0x". + * + * @param chainId - The chain ID string to parse (e.g., "eip155:1"). + * @returns The formatted hexadecimal chain ID (e.g., "0x1"). + * @throws {Error} If the input format is invalid or processing fails. + * @example + * parseChainId("eip155:1") // returns "0x1" + * parseChainId("eip155:a86a") // returns "0xa86a" + * parseChainId("eip155:76adf1") // returns "0x76adf1" + */ +export const parseChainId = (chainId: string): string => { + try { + // Split the chainId string by ':' and get the last part + const parts = chainId.split(':'); + const networkId = parts[parts.length - 1]; + + // Append '0x' to the networkId + return `0x${networkId}`; + } catch (error) { + throw new Error( + `An unexpected error occurred while parsing chainId (${chainId}): ${error.message}`, + ); + } +}; From c7dfab8b9d7d9f91abf705f40ea53e0a8bcf55a2 Mon Sep 17 00:00:00 2001 From: dzimiks Date: Tue, 17 Sep 2024 15:08:41 +0200 Subject: [PATCH 2/4] chore: update snap manifest --- packages/snap/snap.manifest.json | 2 +- yarn.lock | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/snap/snap.manifest.json b/packages/snap/snap.manifest.json index 839fb9b..1f8a3e1 100644 --- a/packages/snap/snap.manifest.json +++ b/packages/snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/tenderly/tenderly-snap.git" }, "source": { - "shasum": "Vvfzy7pDNGsGUO2z4kRqOkAurOomEleHjKiXzcdLWf0=", + "shasum": "pcgwTcLtDnRri+iqOgMf1wNEUEqIJECvD32AlY5T0ns=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/yarn.lock b/yarn.lock index da4e383..21ba569 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8657,9 +8657,9 @@ __metadata: linkType: hard "caniuse-lite@npm:^1.0.0, caniuse-lite@npm:^1.0.30001400, caniuse-lite@npm:^1.0.30001407": - version: 1.0.30001419 - resolution: "caniuse-lite@npm:1.0.30001419" - checksum: 7a4dc2794a6773574b5aebcd1c9c0d56159654821714152d8a0b04e261e1522bfd3d86589b8406ce81c7bf5b706118b73b2cb85d577ae433e303dd48ac9ff65f + version: 1.0.30001660 + resolution: "caniuse-lite@npm:1.0.30001660" + checksum: 8b2c5de2f5facd31980426afbba68238270984acfe8c1ae925b8b6480448eea2fae292f815674617e9170c730c8a238d7cc0db919f184dc0e3cd9bec18f5e5ad languageName: node linkType: hard From 4beb8cf22edcb580c458103a09fbaeff4ec75376 Mon Sep 17 00:00:00 2001 From: dzimiks Date: Fri, 20 Sep 2024 11:49:19 +0200 Subject: [PATCH 3/4] chore: update node version --- .github/workflows/build-lint-test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-lint-test.yml b/.github/workflows/build-lint-test.yml index 02d37df..db32d86 100644 --- a/.github/workflows/build-lint-test.yml +++ b/.github/workflows/build-lint-test.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node-version: [16.x, 18.x] + node-version: [16.x, 18.17] steps: - uses: actions/checkout@v3 - name: Use Node.js ${{ matrix.node-version }} @@ -24,7 +24,7 @@ jobs: - run: yarn lint # - run: yarn test - name: Cache snap build - if: ${{ matrix.node-version == '18.x' }} + if: ${{ matrix.node-version == '18.17' }} uses: actions/cache@v3 with: path: ./packages/snap/dist From 6b29d502465b50b486856bcc7bb757583e4799f5 Mon Sep 17 00:00:00 2001 From: dzimiks Date: Fri, 20 Sep 2024 17:54:34 +0200 Subject: [PATCH 4/4] fix: update parse chain id method --- packages/snap/snap.manifest.json | 2 +- packages/snap/src/tenderly/utils.ts | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/packages/snap/snap.manifest.json b/packages/snap/snap.manifest.json index 1f8a3e1..1726cdb 100644 --- a/packages/snap/snap.manifest.json +++ b/packages/snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/tenderly/tenderly-snap.git" }, "source": { - "shasum": "pcgwTcLtDnRri+iqOgMf1wNEUEqIJECvD32AlY5T0ns=", + "shasum": "5cRgUkIM5DUnrcysawb1az3in+dJ0tMwWWOG9PF1wSQ=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/snap/src/tenderly/utils.ts b/packages/snap/src/tenderly/utils.ts index 7ab47e2..fe09474 100644 --- a/packages/snap/src/tenderly/utils.ts +++ b/packages/snap/src/tenderly/utils.ts @@ -109,15 +109,15 @@ export const isTenderlyDomain = (origin: string) => { }; /** - * Parses a chain ID string and returns the formatted hexadecimal chain ID. + * Parses a CAIP-2 compliant chain ID string and returns the formatted hexadecimal chain ID. * - * This function takes a chain ID string in the format "eip155:networkId", - * extracts the network ID, and returns it as a hexadecimal string - * prefixed with "0x". + * This function takes a CAIP-2 chain ID string in the format "namespace:reference", + * validates the namespace, and for "eip155" namespace, formats the reference as a + * lowercase hexadecimal string prefixed with "0x". * - * @param chainId - The chain ID string to parse (e.g., "eip155:1"). - * @returns The formatted hexadecimal chain ID (e.g., "0x1"). - * @throws {Error} If the input format is invalid or processing fails. + * @param chainId - The CAIP-2 chain ID string to parse (e.g., "eip155:1"). + * @returns The formatted hexadecimal chain ID (e.g., "0x1") for eip155 namespace. + * @throws {Error} If the input format is invalid, or processing fails. * @example * parseChainId("eip155:1") // returns "0x1" * parseChainId("eip155:a86a") // returns "0xa86a" @@ -125,15 +125,14 @@ export const isTenderlyDomain = (origin: string) => { */ export const parseChainId = (chainId: string): string => { try { - // Split the chainId string by ':' and get the last part - const parts = chainId.split(':'); - const networkId = parts[parts.length - 1]; + // Split the chainId string + const [, reference] = chainId.split(':'); - // Append '0x' to the networkId - return `0x${networkId}`; + // Append '0x' to the reference and return + return `0x${reference}`; } catch (error) { throw new Error( - `An unexpected error occurred while parsing chainId (${chainId}): ${error.message}`, + `An unexpected error occurred while parsing CAIP-2 chainId (${chainId}): ${error.message}`, ); } };