Socket
Socket
Sign inDemoInstall

ethereum-input-data-decoder

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ethereum-input-data-decoder - npm Package Compare versions

Comparing version 0.1.5 to 0.1.6

test/data/abiv2_input_data.txt

63

dist/index.js

@@ -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) {

@@ -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) {

2

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc