• This repository has been archived on 30/Jul/2022
  • Stars
    star
    571
  • Rank 78,127 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ethereum smart contract transaction input data decoder


logo


ethereum-input-data-decoder

Ethereum smart contract transaction input data decoder

License Build Status dependencies Status NPM version PRs Welcome

Demo

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

Install

npm install ethereum-input-data-decoder

Getting started

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);
{
  "method": "registerOffChainDonation",
  "types": [
    "address",
    "uint256",
    "uint256",
    "string",
    "bytes32"
    ],
    "inputs": [
      <BN: 5a9dac9315fdd1c3d13ef8af7fdfeb522db08f02>,
      <BN: 58a20230>,
      <BN: 402934>,
      "BTC",
      <Buffer f3 df ... 71 c8>
    ],
    "names": [
      "addr",
      "timestamp",
      "chfCents",
      "currency",
      "memo"
    ]
}

Example using input response from web3.getTransaction:

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

Decoding tuple and tuple[] types

Where OrderData is

struct OrderData {
  uint256 amount;
  address buyer;
}

Decoding input to a method someMethod(address,OrderData,OrderData[]) returns data in format

{
  method: 'someMethod',
  types: ['address', '(uint256,address)', '(uint256,address)[]'],
  inputs: [
    '0x81c55017F7Ce6E72451cEd49FF7bAB1e3DF64d0C',
    [100, '0xA37dE6790861B5541b0dAa7d0C0e651F44c6f4D9']
    [[274, '0xea674fdde714fd979de3edf0f56aa9716b898ec8']]
  ],
  names: ['sender', ['order', ['amount', 'buyer']], ['allOrders', ['amount', 'buyer']]]
}
  • In the types field, tuples are represented as a string containing types contained in the tuple
  • In the inputs field, tuples are represented as an array containing values contained in the tuple
  • In the names field, tuples are represented as an array with 2 items. Item 1 is the name of the tuple, item 2 is an array containing the names of the values contained in the tuple.

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!

CLI

Install

npm install -g ethereum-input-data-decoder

Usage

$ ethereum_input_data_decoder --help

  Ethereum smart contract transaction input data decoder

  Usage
    $ ethereum_input_data_decoder [flags] [input]

  Options
    --abi, -a  ABI file path
    --input, -i Input data file path

  Examples
    $ ethereum_input_data_decoder --abi token.abi --input data.txt
    $ ethereum_input_data_decoder --abi token.abi "0x23b872dd..."

Example

Pass ABI file and input data as file:

$ ethereum_input_data_decoder --abi abi.json --input data.tx

method   registerOffChainDonation

address  addr       0x5a9dac9315fdd1c3d13ef8af7fdfeb522db08f02
uint256  timestamp  1487012400
uint256  chfCents   4204852
string   currency   BTC
bytes32  memo       0xf3df64775a2dfb6bc9e09dced96d0816ff5055bf95da13ce5b6c3f53b97071c8

Pass ABI file and input data as string:

$ ethereum_input_data_decoder --abi abi.json 0x67043cae0...000000

method   registerOffChainDonation

address  addr       0x5a9dac9315fdd1c3d13ef8af7fdfeb522db08f02
uint256  timestamp  1487012400
uint256  chfCents   4204852
string   currency   BTC
bytes32  memo       0xf3df64775a2dfb6bc9e09dced96d0816ff5055bf95da13ce5b6c3f53b97071c8

You can also pipe the input data:

$ cat data.txt | ethereum_input_data_decoder --abi abi.json

method   registerOffChainDonation

address  addr       0x5a9dac9315fdd1c3d13ef8af7fdfeb522db08f02
uint256  timestamp  1487012400
uint256  chfCents   4204852
string   currency   BTC
bytes32  memo       0xf3df64775a2dfb6bc9e09dced96d0816ff5055bf95da13ce5b6c3f53b97071c8

Test

npm test

Development

  1. Clone repository:
git clone [email protected]:miguelmota/ethereum-input-data-decoder.git

cd ethereum-input-data-decoder/
  1. Install dependencies:
npm install
  1. Make changes.

  2. Run tests:

npm test
  1. Run linter:
npm run lint
  1. Build:
npm run build

Contributing

Pull requests are welcome!

For contributions please create a new branch and submit a pull request for review.

Many thanks to all the contributors for making this a better library for everyone 🙏

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.
  • Q: Does this library support ABIEncoderV2?

    • A: Yes, but it's buggy. Please submit a bug report if you encounter issues.

License

MIT

More Repositories

1

golang-for-nodejs-developers

Examples of Golang compared to Node.js for learning 🤓
Go
4,577
star
2

ethereum-development-with-go-book

📖 A little guide book on Ethereum Development with Go (golang)
Go
1,724
star
3

streamhut

Stream your terminal to web without installing anything 🌐
Go
887
star
4

awesome-amazon-alexa

🗣Curated list of awesome resources for the Amazon Alexa platform.
560
star
5

go-ethereum-hdwallet

Ethereum HD Wallet derivations in Go (golang)
Go
412
star
6

solidity-idiosyncrasies

Solidity gotchas, pitfalls, limitations, and idiosyncrasies.
Solidity
347
star
7

bash-streams-handbook

💻 Learn Bash streams, pipelines and redirection, from beginner to advanced.
238
star
8

spectrogram

Audio spectrogram in canvas.
JavaScript
187
star
9

Navigator.sendBeacon

Polyfill for Navigator.sendBeacon()
JavaScript
160
star
10

alexa-voice-service.js

Library for interacting with Alexa Voice Service (AVS) in the browser.
JavaScript
130
star
11

merkletreejs-solidity

Construct merkle trees with MerkleTree.js and verify merkle proofs in Solidity.
JavaScript
126
star
12

solidity-create2-example

Example of how to use the CREATE2 opcode released in the Constantinople update for Ethereum
JavaScript
119
star
13

sobel

Sobel Filter algorithm in JavaScript.
JavaScript
96
star
14

pixelate

Pixelate an image with canvas.
JavaScript
94
star
15

merkletreejs-nft-whitelist

Solidity NFT whitelist contract example using MerkleTree.js for constructing merkle root and merkle proofs.
JavaScript
74
star
16

awesome-token-curated-registries

Curated list of awesome Token Curated Registry (TCR) resources.
69
star
17

is-valid-domain

Validate domain name in JavaScript
JavaScript
68
star
18

solidity-audit-checklist

A checklist of things to look for when auditing Solidity smart contracts.
64
star
19

go-solidity-sha3

Generate Solidity SHA3 (Keccak256) hashes in Go (golang)
Go
61
star
20

go-web3-examples

Example of how to use "Web3" in golang.
Go
61
star
21

keccak256

A wrapper for the keccak library to compute 256 bit keccak hash in JavaScript.
JavaScript
60
star
22

cryptocharts

Cryptocurrency stats and charts displayed in your terminal.
Go
56
star
23

ethereum-hdwallet

CLI and Node.js library for Ethereum HD Wallet derivations from mnemonic
JavaScript
53
star
24

merkletreejs-multiproof-solidity

Verifying merkle multiproofs in solidity example (unaudited)
JavaScript
44
star
25

is-class

Check if function is an ES6 class.
JavaScript
43
star
26

go-coinmarketcap

The unofficial CoinMarketCap API client for Go.
Go
42
star
27

zksnarks-example

An example of how generate zero-knowledge proofs and verify using an Ethereum smart contract.
36
star
28

miguelmota.github.io

My Hugo powered blog
HTML
35
star
29

inview

Detect when element scrolled to view
JavaScript
34
star
30

intent-utterance-expander

Expand custom utterance slots of phrases, to use with Alexa Skills Kit Sample Utterances.
JavaScript
30
star
31

audio-oscilloscope

Audio oscilloscope in canvas.
JavaScript
30
star
32

is-base64

Predicate that returns true if base64 string.
JavaScript
29
star
33

eth-send

Simple way to send ether.
JavaScript
27
star
34

threejs-earth

Rotating planet Earth
JavaScript
27
star
35

global-keypress

Global key press event emitter.
C
26
star
36

sass-cheatsheet

Cheatsheet guide for Sass
25
star
37

ethereum-unit-converter

Ethereum unit converter in JavaScript
JavaScript
24
star
38

ethereum-private-key-to-address

Convert an Ethereum private key to a public address
JavaScript
24
star
39

ethereum-abi-caller

ABI method caller tool
TypeScript
23
star
40

audiobuffer-slice

Slice out a portion of an AudioBuffer.
JavaScript
23
star
41

ethereum-devtools

🛠️ A simple GUI of Ethereum tools and utilities for debugging
TypeScript
23
star
42

bitcoin-development-with-go

[Work in Progress] A little book on Bitcoin Development with Go (golang)
Go
22
star
43

go-ethutil

Ethereum utility functions for Go.
Go
21
star
44

merkletree-viz

Merke tree visualization library for browser, works with merkletreejs
JavaScript
21
star
45

ethereum-checksum-address

Convert Ethereum address to a checksummed address
JavaScript
21
star
46

ethereum-private-key-to-public-key

Convert an Ethereum private key to a public key
JavaScript
20
star
47

C4.5

C4.5 decision tree generation algorithm in JavaScript.
JavaScript
20
star
48

AVS-server

[DEPRECATED] Node.js web server for interacting with the Alexa Voice Service.
JavaScript
19
star
49

vwap

Calculate the Volume-Weighted Average Price (VWAP)
JavaScript
19
star
50

dotfiles

My dotfiles
Emacs Lisp
18
star
51

ethereum-public-key-to-address

Convert an Ethereum public key to an address
JavaScript
18
star
52

gundb-port-go

GunDB port examples in Go (golang)
Go
18
star
53

eos-merkle-proof

EOS smart contract to verify merkle proofs
C++
15
star
54

ethnotary

Document notarization on the Ethereum blockchain.
JavaScript
14
star
55

wos

Monitor traffic for unencrypted data and display a dashboard.
JavaScript
14
star
56

x86-assembly-examples

x86 Assembly language exercises I did while in school.
Assembly
14
star
57

lunch-wheel

A lunch wheel for when you can't decide.
JavaScript
13
star
58

AVS-client

DEPRECATED. Front-end application for interacting with Alexa Voice Service.
JavaScript
13
star
59

web-accessibility

Resources for developing with web accessibility in mind.
13
star
60

ASK-HackerNews

Alexa Skills Kit Hacker News app.
JavaScript
13
star
61

ethereum-checkpoint-git-commit

Checkpoint git commits to an Ethereum smart contract and verify commits as Merkle tree
JavaScript
13
star
62

http-message-parser

HTTP message parser in JavaScript.
JavaScript
13
star
63

loopback-file-upload-example

Example of using file system storage or AWS S3 for file uploads in Loopback
JavaScript
12
star
64

k-means

K-Means clustering in JavaScript.
JavaScript
12
star
65

intent-utterance-generator

Alexa Skills Kit Sample Utterances generator.
JavaScript
12
star
66

trollbox

Instant trollbox using Firebase.
JavaScript
12
star
67

mootron

A Tron like terminal theme
Vim Script
12
star
68

hidden-markov-model

Hidden Markov Models in JavaScript.
JavaScript
12
star
69

cairo-learning

Cairo lang learning notes
Makefile
11
star
70

twitter-purge

Unfollow inactive twitter accounts.
Ruby
11
star
71

metamask-vault-decrypter

CLI to decrypt MetaMask encrypted vault with password
JavaScript
11
star
72

arraybuffer-to-audiobuffer

Convert ArrayBuffer to AudioBuffer
JavaScript
11
star
73

geth-docker

Run a geth node that syncs up with rinkeby testnet in a Docker container.
Dockerfile
10
star
74

big-red-button

A node-hid based driver to read actions from the Dream Cheeky Big Red Button
JavaScript
10
star
75

is-valid-email

Validate email address
JavaScript
10
star
76

solidity-mt-vs-mmr

Solidity example comparing Merkle Tree vs Merkle Mountain Range tree gas usage.
Solidity
10
star
77

buffer-to-arraybuffer

Convert Buffer to ArrayBuffer
JavaScript
10
star
78

arraybuffer-to-buffer

Convert ArrayBuffer to Buffer
JavaScript
10
star
79

fkill

Simple bash script to kill process by name or port
Shell
10
star
80

rust-wasm-example

Example on compiling a Rust library into WebAssembly
JavaScript
10
star
81

zksync-messenger-l1-to-l2-example

Send a message from L1 Goerli to L2 zkSync testnet.
JavaScript
10
star
82

cairo.vim

Cairo lang plugin for Vim
Vim Script
10
star
83

s3scanner

Scan for open public S3 buckets
JavaScript
10
star
84

py-etherdelta

Python client for interacting with the EtherDelta API and Smart Contracts.
Python
10
star
85

terminal-tab

Open a terminal tab programatically
JavaScript
10
star
86

eth-balance

Simple way to check ether balance of an account address.
JavaScript
10
star
87

jsdoc-oblivion

A gray and blue theme for JSDoc.
JavaScript
10
star
88

vscode-ethereum-private-key-to-address

A simple VSCode extension that displays the public address of an Ethereum private key on hover.
TypeScript
10
star
89

etherdelta-gdax-theme-extension

EtherDelta GDAX-like Theme Chrome Extension
CSS
10
star
90

scroll-messenger-l2-to-l1-example

Send a message from L2 Scroll zkEVM testnet to L1 Goerli.
JavaScript
10
star
91

is-valid-hostname

Validate hostname in JavaScript based on RFC-1123
JavaScript
9
star
92

ethereum-keystore

Ethereum keystore generator and reader
JavaScript
9
star
93

tla-learning

Some examples and notes while learning TLA+ modeling language.
9
star
94

to-hex

Convert values to hex string
JavaScript
9
star
95

eos-ecverify

EOS smart contract that does ECDSA verification (ecrecover/ecverify)
C++
9
star
96

interpolate-rgb

Interpolate RGB colors in JavaScript.
JavaScript
8
star
97

mov2gif

A bash script to convert QuickTime movie (.mov) to animated gif (.gif)
Shell
8
star
98

sieve-of-eratosthenes

Sieve of Eratosthenes JavaScript implementation
JavaScript
8
star
99

stableswap-curve-example

JavaScript
8
star
100

go-webhook-server

A simple server to receive webhooks and execute commands
Go
8
star