Giter Site home page Giter Site logo

claudiuschuster / ethereum-input-data-decoder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from miguelmota/ethereum-input-data-decoder

0.0 2.0 0.0 313 KB

Ethereum smart contract transaction input data decoder

Home Page: https://lab.miguelmota.com/ethereum-input-data-decoder

License: MIT License

JavaScript 97.75% HTML 2.24% CSS 0.01%

ethereum-input-data-decoder's Introduction


Ethereum input data decoder


Ethereum smart contract transaction input data decoder

Demo

https://lab.miguelmota.com/ethereum-input-data-decoder

Install

npm install ethereum-input-data-decoder

Usage

Pass ABI file path to decoder constructor:

const InputDataDecoder = require('ethereum-input-data-decoder');
const decoder = new InputDataDecoder(`${__dirname}/abi.json`);

Alternatively, you can pass ABI array object to constructor;

const abi = [{ ... }]
const decoder = new InputDataDecoder(abi);

example abi

Then you can decode input data:

const data = `0x67043cae0000000000000000000000005a9dac9315fdd1c3d13ef8af7fdfeb522db08f020000000000000000000000000000000000000000000000000000000058a20230000000000000000000000000000000000000000000000000000000000040293400000000000000000000000000000000000000000000000000000000000000a0f3df64775a2dfb6bc9e09dced96d0816ff5055bf95da13ce5b6c3f53b97071c800000000000000000000000000000000000000000000000000000000000000034254430000000000000000000000000000000000000000000000000000000000`;

const result = decoder.decodeData(data);

console.log(result);
{
  "name": "registerOffChainDonation",
  "types": [
    "address",
    "uint256",
    "uint256",
    "string",
    "bytes32"
    ],
    "inputs": [
      <BN: 5a9dac9315fdd1c3d13ef8af7fdfeb522db08f02>,
      <BN: 58a20230>,
      <BN: 402934>,
      "BTC",
      <Buffer f3 df ... 71 c8>
    ]
}

Example using input response from web3.getTransaction:

web3.eth.getTransaction(txHash, (error, txResult) => {
  const result = decoder.decodeData(txResult.input);
  console.log(result);
});

Decoding Big Numbers

All numbers are returned in big number format to preserve precision.

Here's an example of how to convert the big number to a human readable format.

console.log(result.inputs[0].toString(10)) // "5"
console.log(result.inputs[0].toNumber()) // 55

Please keep in mind that JavaScript only supports numbers up to 64 bits. Solidity numbers can be up to 256 bits, so you run the risk of truncation when casting or having the big number library error out when trying to parse a large number to a JavaScript Number type.

const n = new BN("543534254523452352345234523455")
console.log(n.toString(10)) // "543534254523452352345234523455"
console.log(n.toNumber()) // ERROR!

Test

npm test

FAQ

  • Q: How can I retrieve the ABI?

    • A: You can generate the ABI from the solidity source files using the Solidity Compiler.

      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

ethereum-input-data-decoder's People

Contributors

mesqueeb avatar miguelmota avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.