|
@@ -42,19 +42,30 @@ var (
|
|
|
// Hash represents the 32 byte Keccak256 hash of arbitrary data.
|
|
// Hash represents the 32 byte Keccak256 hash of arbitrary data.
|
|
|
type Hash [HashLength]byte
|
|
type Hash [HashLength]byte
|
|
|
|
|
|
|
|
|
|
+// BytesToHash sets b to hash.
|
|
|
|
|
+// If b is larger than len(h), b will be cropped from the left.
|
|
|
func BytesToHash(b []byte) Hash {
|
|
func BytesToHash(b []byte) Hash {
|
|
|
var h Hash
|
|
var h Hash
|
|
|
h.SetBytes(b)
|
|
h.SetBytes(b)
|
|
|
return h
|
|
return h
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// BigToHash sets byte representation of b to hash.
|
|
|
|
|
+// If b is larger than len(h), b will be cropped from the left.
|
|
|
func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
|
|
func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
|
|
|
-func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
|
|
|
|
|
|
|
|
|
|
-// Get the string representation of the underlying hash
|
|
|
|
|
-func (h Hash) Str() string { return string(h[:]) }
|
|
|
|
|
|
|
+// HexToHash sets byte representation of s to hash.
|
|
|
|
|
+// If b is larger than len(h), b will be cropped from the left.
|
|
|
|
|
+func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
|
|
|
|
|
+
|
|
|
|
|
+// Bytes gets the byte representation of the underlying hash.
|
|
|
func (h Hash) Bytes() []byte { return h[:] }
|
|
func (h Hash) Bytes() []byte { return h[:] }
|
|
|
|
|
+
|
|
|
|
|
+// Big converts a hash to a big integer.
|
|
|
func (h Hash) Big() *big.Int { return new(big.Int).SetBytes(h[:]) }
|
|
func (h Hash) Big() *big.Int { return new(big.Int).SetBytes(h[:]) }
|
|
|
-func (h Hash) Hex() string { return hexutil.Encode(h[:]) }
|
|
|
|
|
|
|
+
|
|
|
|
|
+// Hex converts a hash to a hex string.
|
|
|
|
|
+func (h Hash) Hex() string { return hexutil.Encode(h[:]) }
|
|
|
|
|
|
|
|
// TerminalString implements log.TerminalStringer, formatting a string for console
|
|
// TerminalString implements log.TerminalStringer, formatting a string for console
|
|
|
// output during logging.
|
|
// output during logging.
|
|
@@ -89,7 +100,8 @@ func (h Hash) MarshalText() ([]byte, error) {
|
|
|
return hexutil.Bytes(h[:]).MarshalText()
|
|
return hexutil.Bytes(h[:]).MarshalText()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Sets the hash to the value of b. If b is larger than len(h), 'b' will be cropped (from the left).
|
|
|
|
|
|
|
+// SetBytes sets the hash to the value of b.
|
|
|
|
|
+// If b is larger than len(h), b will be cropped from the left.
|
|
|
func (h *Hash) SetBytes(b []byte) {
|
|
func (h *Hash) SetBytes(b []byte) {
|
|
|
if len(b) > len(h) {
|
|
if len(b) > len(h) {
|
|
|
b = b[len(b)-HashLength:]
|
|
b = b[len(b)-HashLength:]
|
|
@@ -98,16 +110,6 @@ func (h *Hash) SetBytes(b []byte) {
|
|
|
copy(h[HashLength-len(b):], b)
|
|
copy(h[HashLength-len(b):], b)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Set string `s` to h. If s is larger than len(h) s will be cropped (from left) to fit.
|
|
|
|
|
-func (h *Hash) SetString(s string) { h.SetBytes([]byte(s)) }
|
|
|
|
|
-
|
|
|
|
|
-// Sets h to other
|
|
|
|
|
-func (h *Hash) Set(other Hash) {
|
|
|
|
|
- for i, v := range other {
|
|
|
|
|
- h[i] = v
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
// Generate implements testing/quick.Generator.
|
|
// Generate implements testing/quick.Generator.
|
|
|
func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value {
|
|
func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value {
|
|
|
m := rand.Intn(len(h))
|
|
m := rand.Intn(len(h))
|
|
@@ -117,10 +119,6 @@ func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value {
|
|
|
return reflect.ValueOf(h)
|
|
return reflect.ValueOf(h)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func EmptyHash(h Hash) bool {
|
|
|
|
|
- return h == Hash{}
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
// UnprefixedHash allows marshaling a Hash without 0x prefix.
|
|
// UnprefixedHash allows marshaling a Hash without 0x prefix.
|
|
|
type UnprefixedHash Hash
|
|
type UnprefixedHash Hash
|
|
|
|
|
|
|
@@ -139,13 +137,21 @@ func (h UnprefixedHash) MarshalText() ([]byte, error) {
|
|
|
// Address represents the 20 byte address of an Ethereum account.
|
|
// Address represents the 20 byte address of an Ethereum account.
|
|
|
type Address [AddressLength]byte
|
|
type Address [AddressLength]byte
|
|
|
|
|
|
|
|
|
|
+// BytesToAddress returns Address with value b.
|
|
|
|
|
+// If b is larger than len(h), b will be cropped from the left.
|
|
|
func BytesToAddress(b []byte) Address {
|
|
func BytesToAddress(b []byte) Address {
|
|
|
var a Address
|
|
var a Address
|
|
|
a.SetBytes(b)
|
|
a.SetBytes(b)
|
|
|
return a
|
|
return a
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// BigToAddress returns Address with byte values of b.
|
|
|
|
|
+// If b is larger than len(h), b will be cropped from the left.
|
|
|
func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) }
|
|
func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) }
|
|
|
-func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
|
|
|
|
|
|
|
+
|
|
|
|
|
+// HexToAddress returns Address with byte values of s.
|
|
|
|
|
+// If s is larger than len(h), s will be cropped from the left.
|
|
|
|
|
+func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
|
|
|
|
|
|
|
|
// IsHexAddress verifies whether a string can represent a valid hex-encoded
|
|
// IsHexAddress verifies whether a string can represent a valid hex-encoded
|
|
|
// Ethereum address or not.
|
|
// Ethereum address or not.
|
|
@@ -156,11 +162,14 @@ func IsHexAddress(s string) bool {
|
|
|
return len(s) == 2*AddressLength && isHex(s)
|
|
return len(s) == 2*AddressLength && isHex(s)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Get the string representation of the underlying address
|
|
|
|
|
-func (a Address) Str() string { return string(a[:]) }
|
|
|
|
|
|
|
+// Bytes gets the string representation of the underlying address.
|
|
|
func (a Address) Bytes() []byte { return a[:] }
|
|
func (a Address) Bytes() []byte { return a[:] }
|
|
|
|
|
+
|
|
|
|
|
+// Big converts an address to a big integer.
|
|
|
func (a Address) Big() *big.Int { return new(big.Int).SetBytes(a[:]) }
|
|
func (a Address) Big() *big.Int { return new(big.Int).SetBytes(a[:]) }
|
|
|
-func (a Address) Hash() Hash { return BytesToHash(a[:]) }
|
|
|
|
|
|
|
+
|
|
|
|
|
+// Hash converts an address to a hash by left-padding it with zeros.
|
|
|
|
|
+func (a Address) Hash() Hash { return BytesToHash(a[:]) }
|
|
|
|
|
|
|
|
// Hex returns an EIP55-compliant hex string representation of the address.
|
|
// Hex returns an EIP55-compliant hex string representation of the address.
|
|
|
func (a Address) Hex() string {
|
|
func (a Address) Hex() string {
|
|
@@ -184,7 +193,7 @@ func (a Address) Hex() string {
|
|
|
return "0x" + string(result)
|
|
return "0x" + string(result)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// String implements the stringer interface and is used also by the logger.
|
|
|
|
|
|
|
+// String implements fmt.Stringer.
|
|
|
func (a Address) String() string {
|
|
func (a Address) String() string {
|
|
|
return a.Hex()
|
|
return a.Hex()
|
|
|
}
|
|
}
|
|
@@ -195,7 +204,8 @@ func (a Address) Format(s fmt.State, c rune) {
|
|
|
fmt.Fprintf(s, "%"+string(c), a[:])
|
|
fmt.Fprintf(s, "%"+string(c), a[:])
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Sets the address to the value of b. If b is larger than len(a) it will panic
|
|
|
|
|
|
|
+// SetBytes sets the address to the value of b.
|
|
|
|
|
+// If b is larger than len(a) it will panic.
|
|
|
func (a *Address) SetBytes(b []byte) {
|
|
func (a *Address) SetBytes(b []byte) {
|
|
|
if len(b) > len(a) {
|
|
if len(b) > len(a) {
|
|
|
b = b[len(b)-AddressLength:]
|
|
b = b[len(b)-AddressLength:]
|
|
@@ -203,16 +213,6 @@ func (a *Address) SetBytes(b []byte) {
|
|
|
copy(a[AddressLength-len(b):], b)
|
|
copy(a[AddressLength-len(b):], b)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Set string `s` to a. If s is larger than len(a) it will panic
|
|
|
|
|
-func (a *Address) SetString(s string) { a.SetBytes([]byte(s)) }
|
|
|
|
|
-
|
|
|
|
|
-// Sets a to other
|
|
|
|
|
-func (a *Address) Set(other Address) {
|
|
|
|
|
- for i, v := range other {
|
|
|
|
|
- a[i] = v
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
// MarshalText returns the hex representation of a.
|
|
// MarshalText returns the hex representation of a.
|
|
|
func (a Address) MarshalText() ([]byte, error) {
|
|
func (a Address) MarshalText() ([]byte, error) {
|
|
|
return hexutil.Bytes(a[:]).MarshalText()
|
|
return hexutil.Bytes(a[:]).MarshalText()
|
|
@@ -228,7 +228,7 @@ func (a *Address) UnmarshalJSON(input []byte) error {
|
|
|
return hexutil.UnmarshalFixedJSON(addressT, input, a[:])
|
|
return hexutil.UnmarshalFixedJSON(addressT, input, a[:])
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// UnprefixedHash allows marshaling an Address without 0x prefix.
|
|
|
|
|
|
|
+// UnprefixedAddress allows marshaling an Address without 0x prefix.
|
|
|
type UnprefixedAddress Address
|
|
type UnprefixedAddress Address
|
|
|
|
|
|
|
|
// UnmarshalText decodes the address from hex. The 0x prefix is optional.
|
|
// UnmarshalText decodes the address from hex. The 0x prefix is optional.
|