Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

miguelmota/go-solidity-sha3

Repository files navigation


logo


go-solidity-sha3

Generate Solidity SHA3 (Keccak256) hashes in Go.

License Build Status Go Report Card GoDoc PRs Welcome

This package is the Go equivalent of require('ethers').utils.solidityKeccak256 NPM module.

Install

go get github.com/miguelmota/go-solidity-sha3

Documentation

Documentation on GoDoc

Getting started

Simple example:

package main

import (
	"encoding/hex"
	"fmt"

	"github.com/miguelmota/go-solidity-sha3"
)

func main() {
	hash := solsha3.SoliditySHA3(
		// types
		[]string{"address", "uint256"},

		// values
		[]interface{}{
			"0x935F7770265D0797B621c49A5215849c333Cc3ce",
			"100000000000000000",
		},
	)

	fmt.Println(hex.EncodeToString(hash))
}

Output:

0a3844b522d9e3a837ae56d4c57d668feb26325834bf4ba49e153d84ed7ad53d

More complex example:

package main

import (
	"encoding/hex"
	"fmt"

	"github.com/miguelmota/go-solidity-sha3"
)

func main() {
	types := []string{"address", "bytes1", "uint8[]", "bytes32", "uint256", "address[]", "uint32"}
	values := []interface{}{
		"0x935F7770265D0797B621c49A5215849c333Cc3ce",
		"0xa",
		[]uint8{128, 255},
		"0x4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45",
		"100000000000000000",
		[]string{
			"0x13D94859b23AF5F610aEfC2Ae5254D4D7E3F191a",
			"0x473029549e9d898142a169d7234c59068EDcBB33",
		},
		123456789,
	}

	hash := solsha3.SoliditySHA3(types, values)

	fmt.Println(hex.EncodeToString(hash))
}

Output:

ad390a98c1c32cdb1f046f6887a4109f12290b690127e6e15da4ca210235510e

Contributing

Pull requests are welcome!

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

License

MIT