From e1f832c624524bb32518f76040b6fb48899f9be7 Mon Sep 17 00:00:00 2001 From: Kris Kaczor Date: Wed, 23 Mar 2022 09:34:00 +0100 Subject: [PATCH] Prevent abiParser from breaking too easily (#661) * Prevent abiParser from breaking too easily * Improve test case name --- .changeset/wet-turtles-type.md | 5 +++++ packages/typechain/src/parser/abiParser.ts | 14 ++++++++++---- packages/typechain/test/parser/abiParser.test.ts | 4 ++++ 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 .changeset/wet-turtles-type.md diff --git a/.changeset/wet-turtles-type.md b/.changeset/wet-turtles-type.md new file mode 100644 index 000000000..318a9a587 --- /dev/null +++ b/.changeset/wet-turtles-type.md @@ -0,0 +1,5 @@ +--- +'typechain': patch +--- + +Make parsing JSON abi files more resilent. This should improve foundry integration. diff --git a/packages/typechain/src/parser/abiParser.ts b/packages/typechain/src/parser/abiParser.ts index b2a281403..0f054ed36 100644 --- a/packages/typechain/src/parser/abiParser.ts +++ b/packages/typechain/src/parser/abiParser.ts @@ -402,13 +402,19 @@ export function extractBytecode(rawContents: string): BytecodeWithLinkReferences if (!json) return undefined + function tryMatchBytecode(obj: any | undefined): any | undefined { + if (obj && obj.match instanceof Function) { + return obj.match(bytecodeRegex) + } + } + // `json.evm.bytecode` often has more information than `json.bytecode`, needs to be checked first - if (json.evm?.bytecode?.object?.match(bytecodeRegex)) { + if (tryMatchBytecode(json.evm?.bytecode?.object)) { return extractLinkReferences(json.evm.bytecode.object, json.evm.bytecode.linkReferences) } // handle json schema of @0x/sol-compiler - if (json.compilerOutput?.evm?.bytecode?.object?.match(bytecodeRegex)) { + if (tryMatchBytecode(json.compilerOutput?.evm?.bytecode?.object)) { return extractLinkReferences( json.compilerOutput.evm.bytecode.object, json.compilerOutput.evm.bytecode.linkReferences, @@ -416,11 +422,11 @@ export function extractBytecode(rawContents: string): BytecodeWithLinkReferences } // handle json schema of @foundry/forge - if (json.bytecode?.object?.match(bytecodeRegex)) { + if (tryMatchBytecode(json.bytecode?.object)) { return extractLinkReferences(json.bytecode.object, json.bytecode.linkReferences) } - if (json.bytecode?.match(bytecodeRegex)) { + if (tryMatchBytecode(json.bytecode)) { return extractLinkReferences(json.bytecode, json.linkReferences) } diff --git a/packages/typechain/test/parser/abiParser.test.ts b/packages/typechain/test/parser/abiParser.test.ts index 40c567236..ed4e67423 100644 --- a/packages/typechain/test/parser/abiParser.test.ts +++ b/packages/typechain/test/parser/abiParser.test.ts @@ -145,6 +145,10 @@ describe('extractBytecode', () => { expect(extractBytecode(`{ "bytecode": { "object": "${sampleBytecode}" } }`)).toEqual(resultBytecode) }) + it('should not throw when unexpected abi was passed', () => { + expect(extractBytecode(`{ "bytecode": { "smt_else": "${sampleBytecode}" } }`)).toEqual(undefined) + }) + it('should return undefined when nested abi bytecode is malformed', () => { expect(extractBytecode(`{ "bytecode": "surely-not-bytecode" }`)).toEqual(undefined) })