From cf08f6bbe88ce6d7fb0063af2cb1bff5ff8cd09f Mon Sep 17 00:00:00 2001 From: Dan Miller Date: Thu, 4 Apr 2019 14:06:06 -0230 Subject: [PATCH 1/2] Update parse() to handle args that are tuples, and therefore include parenthesis in the arg list. --- index.js | 18 +++++++++-------- test/index.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index e21970e..17cf978 100644 --- a/index.js +++ b/index.js @@ -23,22 +23,24 @@ class MethodRegistry { } parse (signature) { - let name = signature.match(/^.+(?=\()/) - - if (name) { - name = name[0].charAt(0).toUpperCase() + name[0].slice(1).split(/(?=[A-Z])/).join(' ') + const rawName = signature.match(new RegExp("^([^)(]*)\\((.*)\\)([^)(]*)$")) + let parsedName + + if (rawName) { + parsedName = rawName[1].charAt(0).toUpperCase() + rawName[1].slice(1).split(/(?=[A-Z])/).join(' ') } else { - name = '' + parsedName = '' } - const match = signature.match(/\(.+\)/) + const match = signature.match(new RegExp(rawName[1] + '\\(+([a-z1-9,()]+)\\)')) + let args = []; if (match) { - args = match[0].slice(1, -1).split(',').map((arg) => { return {type: arg}}) + args = match[1].match(/[A-z1-9]+/g).map((arg) => { return {type: arg}}) } return { - name, + name: parsedName, args } } diff --git a/test/index.js b/test/index.js index 1a4202a..12f9b10 100644 --- a/test/index.js +++ b/test/index.js @@ -74,3 +74,57 @@ test('parsing adds spaces to multi words', function (t) { t.end() }) +test('parse signature that includes a tuple as the first param', function (t) { + const sig = 'method((address,uint256,bytes),uint256,bytes)' + const parsed = registry.parse(sig) + + t.equal(parsed.name, 'Method') + t.equal(parsed.args.length, 5) + t.equal(parsed.args[0].type, 'address') + t.equal(parsed.args[1].type, 'uint256') + t.equal(parsed.args[2].type, 'bytes') + t.equal(parsed.args[3].type, 'uint256') + t.equal(parsed.args[4].type, 'bytes') + t.end() +}) + +test('parse signature that includes a tuple of tuples', function (t) { + const sig = 'method(((address,uint256),(address,uint256)))' + const parsed = registry.parse(sig) + + t.equal(parsed.name, 'Method') + t.equal(parsed.args.length, 4) + t.equal(parsed.args[0].type, 'address') + t.equal(parsed.args[1].type, 'uint256') + t.equal(parsed.args[2].type, 'address') + t.equal(parsed.args[3].type, 'uint256') + t.end() +}) + +test('parse signature that includes a tuple as a middle param', function (t) { + const sig = 'method(uint256,(address,uint256,bytes),bytes)' + const parsed = registry.parse(sig) + + t.equal(parsed.name, 'Method') + t.equal(parsed.args.length, 5) + t.equal(parsed.args[0].type, 'uint256') + t.equal(parsed.args[1].type, 'address') + t.equal(parsed.args[2].type, 'uint256') + t.equal(parsed.args[3].type, 'bytes') + t.equal(parsed.args[4].type, 'bytes') + t.end() +}) + +test('parse signature that includes a tuple as the last param', function (t) { + const sig = 'method(uint256,bytes,(address,uint256,bytes))' + const parsed = registry.parse(sig) + + t.equal(parsed.name, 'Method') + t.equal(parsed.args.length, 5) + t.equal(parsed.args[0].type, 'uint256') + t.equal(parsed.args[1].type, 'bytes') + t.equal(parsed.args[2].type, 'address') + t.equal(parsed.args[3].type, 'uint256') + t.equal(parsed.args[4].type, 'bytes') + t.end() +}) From 172527cc80d153ce68c492ca7e320b836f502723 Mon Sep 17 00:00:00 2001 From: Dan Miller Date: Thu, 4 Apr 2019 14:14:26 -0230 Subject: [PATCH 2/2] Update package.json version to 1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 11b5790..436ff2e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eth-method-registry", - "version": "1.1.0", + "version": "1.2.0", "description": "A module for getting method signature info from an ethereum method signature.", "main": "index.js", "scripts": {