ethdigest

package module
v0.0.0-...-821edb2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2024 License: MIT Imports: 7 Imported by: 1

README

go-ethdigest

Package ethdigest provides tools for working with eth-digests, for the Go programming language.

The eth-digest (also called an ethereum-digest, evm-digest, and mistakenly also called an ethereum-hash or evm-hash) is a 32-byte digest — and is commonly used by EVM based networks, as well as other places.

ethdigest.Digest is meant to be a replacement for go-ethereum/common.Hash from the official Ethereum golang package.

Examples

Here is an example of loading an ethdigest.Digest from a hexadecimal-literal stored in a Go string:

digest, err := ethdigest.ParseString("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed0f1e2d3c4b5a69788796a5b4")

Here is an example of loading an ethdigest.Digest from a hexadecimal-literal stored in a Go []byte:

var bytes []byte = []byte("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed0f1e2d3c4b5a69788796a5b4")

digest, err := ethdigest.Parse(bytes)

Here is an example of loading an ethdigest.Digest from a Go [20]byte:

digest := ethdigest.Something( [32]{0x5a,0xAe,0xb6,0x05,0x3F,0x3E,0x94,0xC9,0xb9,0xA0,0x9f,0x33,0x66,0x94,0x35,0xE7,0xEf,0x1B,0xeA,0xed,0x0f,0x1e,0x2d,0x3c,0x4b,0x5a,0x69,0x78,0x87,0x96,0xa5,0xb4} )

Here is an example of loading an ethdigest.Digest from a Go *big.Int:

var bigint *big.Int = // ...

digest := ethdigest.BigInt(bigint)

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-ethdigest

GoDoc

Import

To import package ethdigest use import code like the follownig:

import "github.com/reiver/go-ethdigest"

Installation

To install package ethdigest do the following:

GOPROXY=direct go get https://github.com/reiver/go-ethdigest

Author

Package ethdigest was written by Charles Iliya Krempeaux

Documentation

Index

Constants

View Source
const DigestLength = 32

DigestLength is the length of the eth-digest measured in number of bytes.

Variables

This section is empty.

Functions

This section is empty.

Types

type Digest

type Digest struct {
	// contains filtered or unexported fields
}

Digest holds the eth-digest.

Note that type Digest is an optional-type.

(The 'optional-type' is also known as an 'option-type' and 'maybe-type' in other programming languages and libraries.)

func BigInt

func BigInt(bigint *big.Int) (Digest, error)

BigInt returns the eth-digest represented by the *big.Int.

func BigIntElsePanic

func BigIntElsePanic(bigint *big.Int) Digest

BigIntElsePanic is similar to the BigInt func, except that it panic()s if there is an error.

func Nothing

func Nothing() Digest

Nothing returns an empty digest.

For example:

var digest ethdigest.Digest = ethdigest.Nothing()

Nothing is is NOT the same thing as 0x0000000000000000000000000000000000000000

func Parse

func Parse(text []byte) (Digest, error)

Parse returns the eth-digest represented by the hexadecimal-literal.

func ParseElsePanic

func ParseElsePanic(text []byte) Digest

ParseElsePanic is similar to the Parse func, except that it panic()s if there is an error.

func ParseString

func ParseString(text string) (Digest, error)

ParseString returns the eth-digest represented by the hexadecimal-literal.

func ParseStringElsePanic

func ParseStringElsePanic(text string) Digest

ParseStringElsePanic is similar to the ParseString func, except that it panic()s if there is an error.

func Something

func Something(value [DigestLength]byte) Digest

Something returns an digest whose value is equal to the contents of the 'value' variable.

For example:

// 0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed
var digest ethdigest.Digest = ethdigest.Something( [32]byte{0x5a,0xae,0xb6,0x05,0x3f,0x3e,0x94,0xc9,0xb9,0xa0,0x9f,0x33,0x66,0x94,0x35,0xe7,0xef,0x1b,0xea,0xed,0x5a,0xae,0xb6,0x05,0x3f,0x3e,0x94,0xc9,0xb9,0xa0,0x9f,0x33} )

func (Digest) BigInt

func (receiver Digest) BigInt() *big.Int

If the receiver contains nothing, then BigInt returns nil. If the receiver contains something, then BitInt returns that something as a *big.Int.

For example:

// 0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed0f1e2d3c4b5a69788796a5b4
var digest ethdigest.Something( [32]byte{0x5a, 0xae, 0xb6, 0x05, 0x3f, 0x3e, 0x94, 0xc9, 0xb9, 0xa0, 0x9f, 0x33, 0x66, 0x94, 0x35, 0xe7, 0xef, 0x1b, 0xea, 0xed, 0x0f, 0x1e, 0x2d, 0x3c, 0x4b, 0x5a, 0x69, 0x78, 0x87, 0x96, 0xa5, 0xb4 )

bigint := digest.BigInt()

func (Digest) Bytes

func (receiver Digest) Bytes() []byte

Bytes returns the (decoded) bytes of the eth-digest.

For example, if the hexadecimal-literal of an eth-addres is:

"0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed0f1e2d3c4b5a69788796a5b4"

Then this Bytes method would return:

[]byte{0x5a, 0xae, 0xb6, 0x05, 0x3f, 0x3e, 0x94, 0xc9, 0xb9, 0xa0, 0x9f, 0x33, 0x66, 0x94, 0x35, 0xe7, 0xef, 0x1b, 0xea, 0xed, 0x0f, 0x1e, 0x2d, 0x3c, 0x4b, 0x5a, 0x69, 0x78, 0x87, 0x96, 0xa5, 0xb4}

It would NOT return []byte("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed0f1e2d3c4b5a69788796a5b4")

I.e., it returns the "binary" representation (NOT the ASCII or UTF-8 "text" representation).

func (Digest) Get

func (receiver Digest) Get() ([DigestLength]byte, bool)

If Digest contains nothing, then method Get returns false. If Digest contains something, then method Get return true and then [32]byte that represents the digest.

For example:

var digest ethdigest.Digest = ethdigest.Nothing()

// something == false
array, something := digest.Get()

And, for example

// 0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed0f1e2d3c4b5a69788796a5b4
var digest ethdigest.Digest = ethdigest.Something( [32]byte{0x5a,0xAe,0xb6,0x05,0x3F,0x3E,0x94,0xC9,0xb9,0xA0,0x9f,0x33,0x66,0x94,0x35,0xE7,0xEf,0x1B,0xeA,0xed,0x0f,0x1e,0x2d,0x3c,0x4b,0x5a,0x69,0x78,0x87,0x96,0xa5,0xb4} )

// something == true
// array == [32]byte{0x5a,0xAe,0xb6,0x05,0x3F,0x3E,0x94,0xC9,0xb9,0xA0,0x9f,0x33,0x66,0x94,0x35,0xE7,0xEf,0x1B,0xeA,0xed,0x0f,0x1e,0x2d,0x3c,0x4b,0x5a,0x69,0x78,0x87,0x96,0xa5,0xb4}
array, something := digest.Get()

func (Digest) GetElse

func (receiver Digest) GetElse(alternative [DigestLength]byte) [DigestLength]byte

If Digest contains nothing, then method GetElse returns the value of the 'alternative' parameter. If Digest contains something, then method GetElse returns the [32]byte that represents the digest.

For example:

var alternative [32]byte{0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE}

var digest ethdigest.Digest = ethdigest.Nothing()

// array == alternative == [32]byte{0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE}
array := digest.GetElse()

And, for example

var alternative [32]byte{0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE,0xEE}

// 0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed0f1e2d3c4b5a69788796a5b4
var digest ethdigest.Digest = ethdigest.Something( [32]byte{0x5a,0xAe,0xb6,0x05,0x3F,0x3E,0x94,0xC9,0xb9,0xA0,0x9f,0x33,0x66,0x94,0x35,0xE7,0xEf,0x1B,0xeA,0xed,0x0f,0x1e,0x2d,0x3c,0x4b,0x5a,0x69,0x78,0x87,0x96,0xa5,0xb4} )

// array == [32]byte{0x5a,0xAe,0xb6,0x05,0x3F,0x3E,0x94,0xC9,0xb9,0xA0,0x9f,0x33,0x66,0x94,0x35,0xE7,0xEf,0x1B,0xeA,0xed,0x0f,0x1e,0x2d,0x3c,0x4b,0x5a,0x69,0x78,0x87,0x96,0xa5,0xb4}
array := digest.GetElse()

func (Digest) GoString

func (receiver Digest) GoString() string

GoString returns the value of the receiver in Go syntax.

This function is called when the "%#v" verb is used with the printing-functions from the Go built-in "fmt" package.

For example:

var digest ethdigest = ethdigest.Something([32]byte{0x5a,0xAe,0xb6,0x05,0x3F,0x3E,0x94,0xC9,0xb9,0xA0,0x9f,0x33,0x66,0x94,0x35,0xE7,0xEf,0x1B,0xeA,0xed,0x0f,0x1e,0x2d,0x3c,0x4b,0x5a,0x69,0x78,0x87,0x96,0xa5,0xb4})

fmt.Printf("digest = %#v \n", digest)

func (Digest) IsNothing

func (receiver Digest) IsNothing() bool

IsNothing return true if the receiver digest contains nothing. IsNothing return false if the receiver digest contains something.

func (Digest) IsSomething

func (receiver Digest) IsSomething() bool

IsSomething return true if the receiver digest contains something. IsSomething return false if the receiver digest contains nothing.

func (Digest) MarshalBinary

func (receiver Digest) MarshalBinary() ([]byte, error)

MarshalBinary returns the eth-digest in its binary form as a []byte.

func (Digest) MarshalText

func (receiver Digest) MarshalText() ([]byte, error)

MarshalText returns the eth-digest in its textual form as a []byte.

(Note that this is different than the "binary" form of the eth-digest as a []byte.)

func (Digest) String

func (receiver Digest) String() string

MarshalText returns the eth-digest in its textual form as a string.

func (*Digest) UnmarshalBinary

func (receiver *Digest) UnmarshalBinary(data []byte) error

UnmarshalBinary sets the receiver to the eth-digest in its binary form as a []byte.

func (*Digest) UnmarshalText

func (receiver *Digest) UnmarshalText(text []byte) error

UnmarshalText sets the receiver the eth-digest represented by the hexadecimal-literal.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL