Socket
Socket
Sign inDemoInstall

ethereum-input-data-decoder

Package Overview
Dependencies
56
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.3 to 0.0.4

test/contract_creation_data.txt

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

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc