Skip to content

Commit

Permalink
Update parse() to handle args that are tuples (#6)
Browse files Browse the repository at this point in the history
* Update parse() to handle args that are tuples, and therefore include parenthesis in the arg list.

* Update package.json version to 1.2
  • Loading branch information
danjm authored and danfinlay committed Apr 15, 2019
1 parent fd420e3 commit d6ea959
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
54 changes: 54 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit d6ea959

Please sign in to comment.