Documentation ¶
Index ¶
- Constants
- Variables
- func BigPow(a, b int64) *big.Int
- func Decode(input string) ([]byte, error)
- func FromHex(s string) []byte
- func Hex2Bytes(str string) []byte
- func IsHexAddress(s string) bool
- func Keccak256(data ...[]byte) []byte
- func PaddedBigBytes(bigint *big.Int, n int) []byte
- func ParseBig256(s string) (*big.Int, bool)
- func PublicKeyToAddress(pubkey []byte) string
- func ReadBits(bigint *big.Int, buf []byte)
- func Recover(typedData *TypedData, sig []byte) (string, error)
- func Sign(k key.Key, typedData *TypedData) ([]byte, error)
- func U256(x *big.Int) *big.Int
- func U256Bytes(n *big.Int) []byte
- type Address
- type HexOrDecimal256
- type KeccakState
- type NameValueType
- type Type
- type TypePriority
- type TypedData
- func (typedData *TypedData) Dependencies(primaryType string, found []string) []string
- func (typedData *TypedData) EncodeData(primaryType string, data map[string]interface{}, depth int) ([]byte, error)
- func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interface{}, depth int) ([]byte, error)
- func (typedData *TypedData) EncodeType(primaryType string) []byte
- func (typedData *TypedData) Format() ([]*NameValueType, error)
- func (typedData *TypedData) HashStruct(primaryType string, data TypedDataMessage) ([]byte, error)
- func (typedData *TypedData) Map() map[string]interface{}
- func (typedData *TypedData) TypeHash(primaryType string) []byte
- type TypedDataDomain
- type TypedDataMessage
- type Types
Constants ¶
const ( // HashLength is the expected length of the hash HashLength = 32 // AddressLength is the expected length of the address AddressLength = 20 )
Lengths of hashes and addresses in bytes.
Variables ¶
var ( ErrEmptyString = &decError{"empty hex string"} ErrSyntax = &decError{"invalid hex string"} ErrMissingPrefix = &decError{"hex string without 0x prefix"} ErrOddLength = &decError{"hex string of odd length"} ErrEmptyNumber = &decError{"hex string \"0x\""} ErrLeadingZero = &decError{"hex number with leading zero digits"} ErrUint64Range = &decError{"hex number > 64 bits"} ErrUintRange = &decError{fmt.Sprintf("hex number > %d bits", uintBits)} ErrBig256Range = &decError{"hex number > 256 bits"} )
Errors
var ( Big1 = big.NewInt(1) Big2 = big.NewInt(2) Big3 = big.NewInt(3) Big0 = big.NewInt(0) Big32 = big.NewInt(32) Big256 = big.NewInt(256) Big257 = big.NewInt(257) )
Common big integers often used
var ( MaxBig256 = new(big.Int).Set(tt256m1) MaxBig63 = new(big.Int).Sub(tt63, big.NewInt(1)) )
Various big integer limit values.
Functions ¶
func FromHex ¶
FromHex returns the bytes represented by the hexadecimal string s. s may be prefixed with "0x".
func IsHexAddress ¶
IsHexAddress verifies whether a string can represent a valid hex-encoded Ethereum address or not.
func PaddedBigBytes ¶
PaddedBigBytes encodes a big integer as a big-endian byte slice. The length of the slice is at least n bytes.
func ParseBig256 ¶
ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax. Leading zeros are accepted. The empty string parses as zero.
func PublicKeyToAddress ¶
func ReadBits ¶
ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure that buf has enough space. If buf is too short the result will be incomplete.
Types ¶
type Address ¶
type Address [AddressLength]byte
Address represents the 20 byte address of an Ethereum account.
func BytesToAddress ¶
BytesToAddress returns Address with value b. If b is larger than len(h), b will be cropped from the left.
func HexToAddress ¶
HexToAddress returns Address with byte values of s. If s is larger than len(h), s will be cropped from the left.
type HexOrDecimal256 ¶
HexOrDecimal256 marshals big.Int as hex or decimal.
func NewHexOrDecimal256 ¶
func NewHexOrDecimal256(x int64) *HexOrDecimal256
NewHexOrDecimal256 creates a new HexOrDecimal256
func (*HexOrDecimal256) MarshalText ¶
func (i *HexOrDecimal256) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler.
func (*HexOrDecimal256) UnmarshalText ¶
func (i *HexOrDecimal256) UnmarshalText(input []byte) error
UnmarshalText implements encoding.TextUnmarshaler.
type KeccakState ¶
KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports Read to get a variable amount of data from the hash state. Read is faster than Sum because it doesn't copy the internal state, but also modifies the internal state.
type NameValueType ¶
type NameValueType struct { Name string `json:"name"` Value interface{} `json:"value"` Typ string `json:"type"` }
NameValueType is a very simple struct with Name, Value and Type. It's meant for simple json structures used to communicate signing-info about typed data with the UI
func (*NameValueType) Pprint ¶
func (nvt *NameValueType) Pprint(depth int) string
Pprint returns a pretty-printed version of nvt
type TypePriority ¶
type TypedData ¶
type TypedData struct { Types Types `json:"types"` PrimaryType string `json:"primaryType"` Domain TypedDataDomain `json:"domain"` Message TypedDataMessage `json:"message"` }
func (*TypedData) Dependencies ¶
Dependencies returns an array of custom types ordered by their hierarchical reference tree
func (*TypedData) EncodeData ¶
func (typedData *TypedData) EncodeData(primaryType string, data map[string]interface{}, depth int) ([]byte, error)
EncodeData generates the following encoding: `enc(value₁) ‖ enc(value₂) ‖ … ‖ enc(valueₙ)`
each encoded member is 32-byte long
func (*TypedData) EncodePrimitiveValue ¶
func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interface{}, depth int) ([]byte, error)
EncodePrimitiveValue deals with the primitive values found while searching through the typed data
func (*TypedData) EncodeType ¶
EncodeType generates the following encoding: `name ‖ "(" ‖ member₁ ‖ "," ‖ member₂ ‖ "," ‖ … ‖ memberₙ ")"`
each member is written as `type ‖ " " ‖ name` encodings cascade down and are sorted by name
func (*TypedData) Format ¶
func (typedData *TypedData) Format() ([]*NameValueType, error)
Format returns a representation of typedData, which can be easily displayed by a user-interface without in-depth knowledge about 712 rules
func (*TypedData) HashStruct ¶
func (typedData *TypedData) HashStruct(primaryType string, data TypedDataMessage) ([]byte, error)
HashStruct generates a keccak256 hash of the encoding of the provided data
type TypedDataDomain ¶
type TypedDataDomain struct { Name string `json:"name"` Version string `json:"version"` ChainId *HexOrDecimal256 `json:"chainId"` VerifyingContract string `json:"verifyingContract"` Salt string `json:"salt"` }
func (*TypedDataDomain) Map ¶
func (domain *TypedDataDomain) Map() map[string]interface{}
Map is a helper function to generate a map version of the domain
type TypedDataMessage ¶
type TypedDataMessage = map[string]interface{}