https://github.com/blocky/abi
Minimal Go library for generic ABI encoding/decoding without reflection or code generation
https://github.com/blocky/abi
Last synced: 5 months ago
JSON representation
Minimal Go library for generic ABI encoding/decoding without reflection or code generation
- Host: GitHub
- URL: https://github.com/blocky/abi
- Owner: blocky
- Created: 2025-10-23T17:28:09.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-10-24T04:49:34.000Z (8 months ago)
- Last Synced: 2025-10-24T06:24:29.714Z (8 months ago)
- Language: Nix
- Size: 4.88 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# abi
[](https://pkg.go.dev/github.com/blocky/abi)
[](https://goreportcard.com/report/github.com/blocky/abi)
[](https://golang.org/dl/)
A minimal Go library for **ABI encoding and decoding** without reflection or code generation.
## Why abi?
- ๐ **Zero dependencies** (except testing)
- โก **No reflection** - fast and predictable performance
- ๐ง **No code generation** - simple integration
- ๐ **ABI compliant** - follows Ethereum ABI encoding standards
- ๐งช **Well tested** - comprehensive test suite
- ๐ฆ **Lightweight** - minimal API surface
## Quick Start
### Installation
```bash
go get github.com/blocky/abi
```
### Basic Usage
```go
package main
import (
"fmt"
"log"
"github.com/blocky/abi"
)
func main() {
// Encode a tuple (uint64, bytes, uint64)
encoded, err := abi.NewTupleEncoder().
Uint64(42).
Bytes([]byte("hello world")).
Uint64(123).
Encode()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Encoded tuple: %x\n", encoded)
// Decode the tuple back
var num1 uint64
var data []byte
var num2 uint64
err = abi.NewTupleDecoder().
Uint64(&num1).
Bytes(&data).
Uint64(&num2).
Decode(encoded)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Decoded: %d, %s, %d\n", num1, data, num2)
// Output:
// Decoded: 42, hello world, 123
}
```
### Simple Types
```go
// Encode/decode individual types
encoded := abi.EncodeUint64(42)
decoded, err := abi.DecodeUint64(encoded)
encodedBytes, err := abi.EncodeBytes([]byte("hello"))
decodedBytes, err := abi.DecodeBytes(encodedBytes)
```
## Features
### Supported ABI Types
- **`uint64`** - 64-bit unsigned integers
- **`bytes`** - Dynamic byte arrays
- **`[]bytes`** - Array of byte arrays
- **Tuples** - Complex structures combining multiple types
With more planned, feel free to open an issue or PR!
## Development
If you have nix installed, you can create a development environment with:
```bash
nix develop
```
Or set up your environment manually by installing standard Go tools and `just`.
### Running Tests
```bash
# Run all tests
go test ./...
# or use just
just test
```
### Project Structure
```
โโโ abi.go # Main library code
โโโ abi_test.go # Public API tests
โโโ abi_internal_test.go # Internal function tests
โโโ abitestdata_test.go # Test data and fixtures
โโโ assets/ # Documentation assets
```
To maintain compatibility with the ethereum ABI standard, we test against
outputs from the [go-ethereum](https://github.com/ethereum/go-ethereum)
library. We generate these outputs using the `abi-testdata` tool, which
is developed in [abi-testdata](https://github.com/blocky/abi-testdata).
If you would like to add new types, you will likely need to update that
project to add additional test data.
### Contributing
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Make your changes
4. Add tests for new functionality
5. Ensure all tests pass (`just pre-pr`)
6. Commit your changes (`git commit -am 'Add amazing feature'`)
7. Push to the branch (`git push origin feature/amazing-feature`)
8. Open a Pull Request