ethereum-input-data-decoder
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -94,12 +94,27 @@ 'use strict'; | ||
if (obj.type === 'constructor') return acc; | ||
if (obj.type === 'event') return acc; | ||
var name = obj.name || null; | ||
var types = obj.inputs ? obj.inputs.map(function (x) { | ||
return x.type; | ||
if (x.type === 'tuple[]') { | ||
return x; | ||
} else { | ||
return x.type; | ||
} | ||
}) : []; | ||
var hash = toHexString(ethabi.methodID(name, types)); | ||
var hash = genMethodId(name, types); | ||
if (hash === methodId) { | ||
inputsBuf = normalizeAddresses(types, inputsBuf); | ||
var inputs = ethabi.rawDecode(types, inputsBuf); | ||
var inputs = []; | ||
try { | ||
inputsBuf = normalizeAddresses(types, inputsBuf); | ||
inputs = ethabi.rawDecode(types, inputsBuf); | ||
} catch (err) { | ||
// TODO: normalize addresses for tuples | ||
inputs = ethers.utils.defaultAbiCoder.decode(types, inputsBuf); | ||
inputs = inputs[0]; | ||
} | ||
return { | ||
@@ -166,2 +181,42 @@ name: name, | ||
function handleInputs(input) { | ||
var tupleArray = false; | ||
if (input instanceof Object && input.components) { | ||
input = input.components; | ||
tupleArray = true; | ||
} | ||
if (!Array.isArray(input)) { | ||
if (input instanceof Object && input.type) { | ||
return input.type; | ||
} | ||
return input; | ||
} | ||
var ret = '(' + input.reduce(function (acc, x) { | ||
if (x.type === 'tuple') { | ||
acc.push(handleInputs(x.components)); | ||
} else if (x.type === 'tuple[]') { | ||
acc.push(handleInputs(x.components) + '[]'); | ||
} else { | ||
acc.push(x.type); | ||
} | ||
return acc; | ||
}, []).join(',') + ')'; | ||
if (tupleArray) { | ||
return ret + '[]'; | ||
} | ||
} | ||
function genMethodId(methodName, types) { | ||
var input = methodName + '(' + types.reduce(function (acc, x) { | ||
acc.push(handleInputs(x)); | ||
return acc; | ||
}, []).join(',') + ')'; | ||
return ethers.utils.keccak256(Buffer.from(input)).slice(2, 10); | ||
} | ||
function toHexString(byteArray) { | ||
@@ -168,0 +223,0 @@ return Array.from(byteArray, function (byte) { |
65
index.js
@@ -81,10 +81,27 @@ const fs = require('fs') | ||
if (obj.type === 'constructor') return acc | ||
if (obj.type === 'event') return acc | ||
const name = obj.name || null | ||
const types = obj.inputs ? obj.inputs.map(x => x.type) : [] | ||
const hash = toHexString(ethabi.methodID(name, types)) | ||
let types = obj.inputs ? obj.inputs.map(x => { | ||
if (x.type === 'tuple[]') { | ||
return x | ||
} else { | ||
return x.type | ||
} | ||
}) : [] | ||
const hash = genMethodId(name, types) | ||
if (hash === methodId) { | ||
inputsBuf = normalizeAddresses(types, inputsBuf) | ||
const inputs = ethabi.rawDecode(types, inputsBuf) | ||
let inputs = [] | ||
try { | ||
inputsBuf = normalizeAddresses(types, inputsBuf) | ||
inputs = ethabi.rawDecode(types, inputsBuf) | ||
} catch (err) { | ||
// TODO: normalize addresses for tuples | ||
inputs = ethers.utils.defaultAbiCoder.decode(types, inputsBuf) | ||
inputs = inputs[0] | ||
} | ||
return { | ||
@@ -148,2 +165,42 @@ name, | ||
function handleInputs (input) { | ||
let tupleArray = false | ||
if (input instanceof Object && input.components) { | ||
input = input.components | ||
tupleArray = true | ||
} | ||
if (!Array.isArray(input)) { | ||
if (input instanceof Object && input.type) { | ||
return input.type | ||
} | ||
return input | ||
} | ||
let ret = '(' + input.reduce((acc, x) => { | ||
if (x.type === 'tuple') { | ||
acc.push(handleInputs(x.components)) | ||
} else if (x.type === 'tuple[]') { | ||
acc.push(handleInputs(x.components) + '[]') | ||
} else { | ||
acc.push(x.type) | ||
} | ||
return acc | ||
}, []).join(',') + ')' | ||
if (tupleArray) { | ||
return ret + '[]' | ||
} | ||
} | ||
function genMethodId (methodName, types) { | ||
const input = methodName + '(' + (types.reduce((acc, x) => { | ||
acc.push(handleInputs(x)) | ||
return acc | ||
}, []).join(',')) + ')' | ||
return ethers.utils.keccak256(Buffer.from(input)).slice(2, 10) | ||
} | ||
function toHexString (byteArray) { | ||
@@ -150,0 +207,0 @@ return Array.from(byteArray, function (byte) { |
{ | ||
"name": "ethereum-input-data-decoder", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Ethereum smart contract transaction input data decoder", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -78,14 +78,30 @@ const fs = require('fs') | ||
}) | ||
}) | ||
// https://github.com/miguelmota/ethereum-input-data-decoder/issues/8 | ||
test('256 address', t => { | ||
t.plan(2) | ||
const decoder = new InputDataDecoder(`${__dirname}/data/abi2.json`) | ||
t.test('abiv2', t => { | ||
t.plan(5) | ||
const data = '0xa9059cbb85f1150654584d0192059454e9dc1532d9d9cf914926406a02370cea80cf32f600000000000000000000000000000000000000000000000000000000033dc10b' | ||
const abi = JSON.parse(fs.readFileSync(`${__dirname}/data/abiv2.json`)) | ||
const decoder = new InputDataDecoder(abi) | ||
const result = decoder.decodeData(data) | ||
t.equal(result.inputs[0].toString(16), 'e9dc1532d9d9cf914926406a02370cea80cf32f6') | ||
t.equal(result.inputs[1].toString(10), '54378763') | ||
const data = fs.readFileSync(`${__dirname}/data/abiv2_input_data.txt`) | ||
const result = decoder.decodeData(data) | ||
t.equal(result.inputs[0].id.toString(10), '2') | ||
t.equal(result.inputs[0].state.toString(10), '2') | ||
t.equal(result.inputs[0].valuation.toString(10), '50') | ||
t.equal(result.inputs[0].fingerprint.toString(), '0xabcd000000000000000000000000000000000000000000000000000000000000') | ||
t.equal(result.inputs[0].countdown.toString(10), '1549925124') | ||
}) | ||
// https://github.com/miguelmota/ethereum-input-data-decoder/issues/8 | ||
t.test('256 address', t => { | ||
t.plan(2) | ||
const decoder = new InputDataDecoder(`${__dirname}/data/abi2.json`) | ||
const data = '0xa9059cbb85f1150654584d0192059454e9dc1532d9d9cf914926406a02370cea80cf32f600000000000000000000000000000000000000000000000000000000033dc10b' | ||
const result = decoder.decodeData(data) | ||
t.equal(result.inputs[0].toString(16), 'e9dc1532d9d9cf914926406a02370cea80cf32f6') | ||
t.equal(result.inputs[1].toString(10), '54378763') | ||
}) | ||
}) |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
1001393
24
21171
12