ethereum-input-data-decoder
Advanced tools
Comparing version 0.0.3 to 0.0.4
92
index.js
@@ -1,33 +0,87 @@ | ||
const fs = require('fs'); | ||
const ethabi = require('ethereumjs-abi'); | ||
const fs = require('fs') | ||
const ethabi = require('ethereumjs-abi') | ||
const ethers = require('ethers') | ||
class InputDataDecoder { | ||
constructor(prop) { | ||
this.abi = []; | ||
this.abi = [] | ||
if (typeof prop === `string`) { | ||
this.abi = JSON.parse(fs.readFileSync(prop)); | ||
this.abi = JSON.parse(fs.readFileSync(prop)) | ||
} else if (prop instanceof Object) { | ||
this.abi = prop; | ||
this.abi = prop | ||
} else { | ||
throw new TypeError(`Must pass ABI array object or file path to constructor`); | ||
throw new TypeError(`Must pass ABI array object or file path to constructor`) | ||
} | ||
} | ||
decodeData(data = ``) { | ||
decodeConstructor(data) { | ||
if (Buffer.isBuffer(data)) { | ||
data = data.toString('utf8') | ||
} | ||
if (typeof data !== `string`) { | ||
data = ``; | ||
data = `` | ||
} | ||
const dataBuf = new Buffer(data.replace(/^0x/, ``), `hex`); | ||
const methodId = dataBuf.slice(0, 4).toString(`hex`); | ||
const inputsBuf = dataBuf.slice(4); | ||
data = data.trim() | ||
for (var i = 0; i < this.abi.length; i++) { | ||
const obj = this.abi[i] | ||
if (obj.type !== 'constructor') { | ||
continue | ||
} | ||
const name = obj.name | ||
const types = obj.inputs ? obj.inputs.map(x => x.type) : [] | ||
// take last 32 bytes | ||
data = data.slice(-256) | ||
if (data.length !== 256) { | ||
throw new Error('fial') | ||
} | ||
if (data.indexOf(`0x`) !== 0) { | ||
data = `0x${data}` | ||
} | ||
const inputs = ethers.Interface.decodeParams(types, data) | ||
return { | ||
name, | ||
types, | ||
inputs | ||
} | ||
} | ||
throw new Error('not found') | ||
} | ||
decodeData(data) { | ||
if (Buffer.isBuffer(data)) { | ||
data = data.toString('utf8') | ||
} | ||
if (typeof data !== `string`) { | ||
data = `` | ||
} | ||
data = data.trim() | ||
try { | ||
return this.decodeConstructor(data) | ||
} catch(err) { } | ||
const dataBuf = new Buffer(data.replace(/^0x/, ``), `hex`) | ||
const methodId = dataBuf.slice(0, 4).toString(`hex`) | ||
const inputsBuf = dataBuf.slice(4) | ||
const result = this.abi.reduce((acc, obj) => { | ||
const name = obj.name; | ||
const types = obj.inputs ? obj.inputs.map(x => x.type) : []; | ||
const hash = ethabi.methodID(name, types).toString(`hex`); | ||
const name = obj.name | ||
const types = obj.inputs ? obj.inputs.map(x => x.type) : [] | ||
const hash = ethabi.methodID(name, types).toString(`hex`) | ||
if (hash === methodId) { | ||
const inputs = ethabi.rawDecode(types, inputsBuf, []); | ||
const inputs = ethabi.rawDecode(types, inputsBuf, []) | ||
@@ -41,9 +95,9 @@ return { | ||
return acc; | ||
}, {}); | ||
return acc | ||
}, {}) | ||
return result; | ||
return result | ||
} | ||
} | ||
module.exports = InputDataDecoder; | ||
module.exports = InputDataDecoder |
{ | ||
"name": "ethereum-input-data-decoder", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Ethereum smart contract transaction input data decoder", | ||
@@ -21,3 +21,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"ethereumjs-abi": "^0.6.4" | ||
"ethereumjs-abi": "^0.6.4", | ||
"ethers": "^2.1.3" | ||
}, | ||
@@ -24,0 +25,0 @@ "keywords": [ |
@@ -80,4 +80,18 @@ # Ethereum input data decoder | ||
## FAQ | ||
- Q: How can I retrieve the ABI? | ||
- A: You can generate the ABI from the solidity source files using the [Solidity Compiler](http://solidity.readthedocs.io/en/develop/installing-solidity.html). | ||
```bash | ||
solc --abi MyContract.sol -o build | ||
``` | ||
- Q: Can this library decode contract creation input data? | ||
- A: Yes, it can decode contract creation input data. | ||
## License | ||
MIT |
@@ -7,3 +7,3 @@ const fs = require('fs'); | ||
* Example input data from this transaction | ||
* https://etherscan.io/tx/0xa6f019f2fc916bd8df607f1c99148ebb06322999ff08bc88927fe8406acae1b2) | ||
* https://etherscan.io/tx/0xa6f019f2fc916bd8df607f1c99148ebb06322999ff08bc88927fe8406acae1b2 | ||
*/ | ||
@@ -54,2 +54,16 @@ | ||
}); | ||
}); | ||
// https://etherscan.io/tx/0xc7add41f6ae5e4fe268f654709f450983510ab7da67812be608faf2133a90b5a | ||
t.test(`contract creation data`, t => { | ||
t.plan(2); | ||
const abi = JSON.parse(fs.readFileSync(`${__dirname}/abi.json`)); | ||
const decoder = new InputDataDecoder(abi); | ||
const data = fs.readFileSync(`${__dirname}/contract_creation_data.txt`) | ||
const result = decoder.decodeData(data) | ||
t.equal(result.inputs[0].toString(16), `0xB2Cb826C945D8Df01802b5cf3c4105685D4933A0`) | ||
t.equal(result.inputs[1].toString(16), `STIFTUNG Dfinity FDC`) | ||
}) | ||
}) |
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
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
963790
25676
97
2
14
+ Addedethers@^2.1.3
+ Addedaes-js@3.0.0(transitive)
+ Addedelliptic@6.3.3(transitive)
+ Addedethers@2.2.6(transitive)
+ Addedethers-contracts@2.2.1(transitive)
+ Addedethers-providers@2.1.19(transitive)
+ Addedethers-utils@2.1.11(transitive)
+ Addedethers-wallet@2.1.8(transitive)
+ Addedinherits@2.0.1(transitive)
+ Addedjs-sha3@0.5.7(transitive)
+ Addedscrypt-js@2.0.3(transitive)
+ Addedsetimmediate@1.0.4(transitive)
+ Addeduuid@2.0.1(transitive)
+ Addedxmlhttprequest@1.8.0(transitive)