• Stars
    star
    294
  • Rank 141,303 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated almost 3 years ago

Reviews

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

Repository Details

[DEPRECATED] Decoder and encoder for the Ethereum ABI

This library is deprecated and will not be further maintained. It specifically doesn't support the new ABI Coder v2 which is now activated by default starting with Solidity v0.8.0 released in December 2020.

We recommend to use the Ethers.js v5 ABI Encoder as a replacement which supports the new ABI Coder v2 types and is under active development.


ethereumjs-abi

NPM Package Build Status Coverage Status js-standard-style Discord

Module implementing the Ethereum ABI in Javascript. Can be used with RPC libraries for communication or with ethereumjs-vm to implement a fully fledged simulator.

Usage

Manual encoding and decoding

There are three methods of interest:

  • methodID to create a function signature
  • rawEncode to encode fields and
  • rawDecode to decode fields

Example code:

var abi = require('ethereumjs-abi')

// returns the encoded binary (as a Buffer) data to be sent
var encoded = abi.rawEncode([ "address" ], [ "0x0000000000000000000000000000000000000000" ])

// returns the decoded array of arguments
var decoded = abi.rawDecode([ "address" ], data)

Encoding and decoding aided by the JSON ABI definition

Planned for the future is supporting the JSON ABI definition:

var abi = require('ethereumjs-abi')

// need to have the ABI definition in JSON as per specification
var tokenAbi = [{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"type":"function"},{"inputs":[],"type":"constructor"}]

var encoded = abi.encode(tokenAbi, "balanceOf(uint256 address)", [ "0x0000000000000000000000000000000000000000" ])

var decoded = abi.decode(tokenAbi, "balanceOf(uint256 address)", data)

Simple encoding and decoding

var abi = require('ethereumjs-abi')

// returns the encoded binary (as a Buffer) data to be sent
var encoded = abi.simpleEncode("balanceOf(address):(uint256)", "0x0000000000000000000000000000000000000000")

// returns the decoded array of arguments
var decoded = abi.simpleDecode("balanceOf(address):(uint256)", data)

Solidity tightly packed formats

This library also supports creating Solidity's tightly packed data constructs, which are used together with sha3, sha256 and ripemd160 to create hashes.

Solidity code:

contract HashTest {
  function testSha3() returns (bytes32) {
   address addr1 = 0x43989fb883ba8111221e89123897538475893837;
   address addr2 = 0;
   uint val = 10000;
   uint timestamp = 1448075779;

   return sha3(addr1, addr2, val, timestamp); // will return 0xc3ab5ca31a013757f26a88561f0ff5057a97dfcc33f43d6b479abc3ac2d1d595
 }
}

Creating the same hash using this library:

var abi = require('ethereumjs-abi')
var BN = require('bn.js')

abi.soliditySHA3(
    [ "address", "address", "uint", "uint" ],
    [ new BN("43989fb883ba8111221e89123897538475893837", 16), 0, 10000, 1448075779 ]
).toString('hex')

For the same data structure:

  • sha3 will return 0xc3ab5ca31a013757f26a88561f0ff5057a97dfcc33f43d6b479abc3ac2d1d595
  • sha256 will return 0x344d8cb0711672efbdfe991f35943847c1058e1ecf515ff63ad936b91fd16231
  • ripemd160 will return 0x000000000000000000000000a398cc72490f72048efa52c4e92067e8499672e7 (NOTE: it is 160bits, left padded to 256bits)

Note that ripemd160() in Solidity returns bytes20 and if you cast it to bytes32, it will be right padded with zeroes.

Using Serpent types

Serpent uses a different notation for the types, even though it will serialize to the same ABI.

We provide two helpers to convert between these notations:

  • fromSerpent: convert a Serpent notation to the ABI notation
  • toSerpent: the other way around

Example usage:

abi.fromSerpent('s')    // [ 'bytes' ]
abi.fromSerpent('i')    // [ 'int256' ]
abi.fromSerpent('a')    // [ 'int256[]' ]
abi.fromSerpent('b8')   // [ 'bytes8' ]
abi.fromSerpent('b8i')  // [ 'bytes8', 'int256' ]

abi.toSerpent([ 'bytes' ])             // 's'
abi.toSerpent([ 'int256' ])            // 'i'
abi.toSerpent([ 'int256[]' ])          // 'a'
abi.toSerpent([ 'bytes8' ])            // 'b8'
abi.toSerpent([ 'bytes8', 'int256' ])  // 'b8i'

It is to be used in conjunction with rawEncode and rawDecode:

var encoded = abi.rawEncode(abi.fromSerpent("i"), [ "0x0000000000000000000000000000000000000000" ])

var decoded = abi.rawDecode([...abi.fromSerpent("i"), ...abi.fromSerpent("i")], data)

Note: Serpent uses arbitary binary fields. If you want to store strings it is preferable to ensure it is stored as UTF8. Buffer.from(<string>, 'utf8') can be used to ensure it is properly encoded.

Contributing

I am more than happy to receive improvements. Please send me a pull request or reach out on email or twitter.

There is a lot missing, grep for FIXME in the source code to find inspiration.

EthereumJS

See our organizational documentation for an introduction to EthereumJS as well as information on current standards and best practices.

If you want to join for work or do improvements on the libraries have a look at our contribution guidelines.

License

Copyright (C) 2015 Alex Beregszaszi

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

ethereumjs-monorepo

Monorepo for the Ethereum VM TypeScript Implementation
TypeScript
2,525
star
2

ethereumjs-wallet

Utilities for handling Ethereum keys
TypeScript
986
star
3

ethereumjs-tx

Project is in active development and has been moved to the EthereumJS VM monorepo.
TypeScript
779
star
4

keythereum

Create, import and export Ethereum keys
JavaScript
602
star
5

ethereumjs-util

Project is in active development and has been moved to the EthereumJS monorepo.
TypeScript
601
star
6

ethereumjs-lib

[DEPRECATED] A JavaScript library of core Ethereum functions
442
star
7

merkle-patricia-tree

Project is in active development and has been moved to the EthereumJS VM monorepo.
TypeScript
306
star
8

ethereumjs-client

Project is in active development and has been moved to the EthereumJS VM monorepo.
TypeScript
192
star
9

ethereumjs-devp2p

Project is in active development and has been moved to the EthereumJS VM monorepo.
TypeScript
140
star
10

ethereumjs-blockchain

Project is in active development and has been moved to the EthereumJS VM monorepo.
TypeScript
120
star
11

rlp

Project is in active development and has been moved to the EthereumJS monorepo.
TypeScript
120
star
12

ethrpc

Maximal RPC wrapper
JavaScript
109
star
13

ultralight

Ethereum Portal Network TypeScript implementation
TypeScript
97
star
14

ethereumjs-blockstream

Reliable stream of Ethereum blocks
TypeScript
80
star
15

helpeth

Help (with) Ethereum. Purists' commandline tool for key and transaction management.
JavaScript
75
star
16

browser-builds

[DEPRECATED] Browser builds of ethereumjs libraries.
JavaScript
59
star
17

node-blockchain-server

[DEPRECATED] A simple blockchain server (downloads block and serves them, doesn't do state transitions)
57
star
18

ethashjs

Project is in active development and has been moved to the EthereumJS VM monorepo.
JavaScript
56
star
19

geth.js

Start and stop geth from Node.js
JavaScript
45
star
20

ethereumjs-account

Project is in active development and has been moved to the EthereumJS VM monorepo.
TypeScript
44
star
21

ethereumjs-block

Project is in active development and has been moved to the EthereumJS VM monorepo.
TypeScript
42
star
22

ethereumjs-units

Ethereum unit conversion.
JavaScript
41
star
23

ethereumjs-connect

[DEPRECATED] Basic Ethereum connection tasks
JavaScript
36
star
24

eth-query

minimal rpc wrapper
JavaScript
28
star
25

node-devp2p

[DEPRECATED] A node.js implementation of the RLPx transport
28
star
26

ethereumjs-common

Project is in active development and has been moved to the EthereumJS VM monorepo.
TypeScript
26
star
27

rustbn.js

Rust to Javascript/Webassembly compilation of ethereum-bn128.rs.
HTML
20
star
28

node-devp2p-dpt

[DEPRECATED] Ethereum Distubted Peer Table Implementation
15
star
29

ethereumjs-ledger

[DEPRECATED] A wrapper library around the Ledger line of devices that attempts to simplify usage and handle various failure modes/problems.
TypeScript
14
star
30

ethereumjs-icap

Utilities for handling ICAP (Ethereum in IBAN) encoding
JavaScript
14
star
31

eth-bin-to-ops

[DEPRECATED] Simple utility for parsing binary evm code into opcodes
JavaScript
13
star
32

ethereum-verified-contracts

[DEPRECATED] Ethereum Verified Contracts
JavaScript
12
star
33

organization

A repo for discussions and other non-code organizing stuff
11
star
34

ethereumjs-testing

This utility library has been moved to the EthereumJS VM monorepo.
JavaScript
11
star
35

ethereumjs.github.io

Website for an intro to the EthereumJS ecosystem
CSS
10
star
36

ethereumjs-stub-rpc-server

Stub Ethereum JSON-RPC Server
JavaScript
9
star
37

node-ethash

Node bindings for the C++ Ethash implementation.
C++
7
star
38

fixed-bn.js

a bn.js wrapper that constrains numbers to a fixed width
TypeScript
7
star
39

ethereumjs-config

[DEPRECATED] Configuration is now added within a config folder of the EthereumJS monorepo
Shell
4
star
40

node-devp2p-eth

[DEPRECATED] Ethereum wire protocol implementation
3
star
41

sharding

Serenity phase 2 shard-chain PoC
WebAssembly
2
star
42

eth-bin-to-method-ids

[DEPRECATED] Extract the four byte method ids from evm byte code
JavaScript
2
star
43

rustbn-wasm

TypeScript
1
star
44

ethereumjs-collation

[DEPRECATED] Implementation of an Ethereum sharding collation
JavaScript
1
star
45

node-devp2p-rlpx

[DEPRECATED] Implements RLPx
1
star