2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2015-03-16 12:27:38 +02:00
|
|
|
package common
|
|
|
|
|
2015-04-27 01:49:49 +03:00
|
|
|
import (
|
common: improve printing of Hash and Address (#21834)
Both Hash and Address have a String method, which returns the value as
hex with 0x prefix. They also had a Format method which tried to print
the value using printf of []byte. The way Format worked was at odds with
String though, leading to a situation where fmt.Sprintf("%v", hash)
returned the decimal notation and hash.String() returned a hex string.
This commit makes it consistent again. Both types now support the %v,
%s, %q format verbs for 0x-prefixed hex output. %x, %X creates
unprefixed hex output. %d is also supported and returns the decimal
notation "[1 2 3...]".
For Address, the case of hex characters in %v, %s, %q output is
determined using the EIP-55 checksum. Using %x, %X with Address
disables checksumming.
Co-authored-by: Felix Lange <fjl@twurst.com>
2020-12-08 21:19:09 +03:00
|
|
|
"bytes"
|
2018-07-24 16:15:07 +03:00
|
|
|
"database/sql/driver"
|
2017-03-02 16:03:20 +03:00
|
|
|
"encoding/hex"
|
2018-04-18 13:27:20 +03:00
|
|
|
"encoding/json"
|
2019-11-21 17:34:28 +03:00
|
|
|
"errors"
|
2015-06-15 13:16:29 +03:00
|
|
|
"fmt"
|
2015-04-27 01:49:49 +03:00
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
2023-05-17 17:39:33 +03:00
|
|
|
"strconv"
|
2018-04-18 13:27:20 +03:00
|
|
|
"strings"
|
2016-11-28 02:58:22 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-01-04 01:15:26 +03:00
|
|
|
"golang.org/x/crypto/sha3"
|
2015-04-27 01:49:49 +03:00
|
|
|
)
|
2015-03-16 18:09:08 +02:00
|
|
|
|
2018-06-14 12:52:50 +03:00
|
|
|
// Lengths of hashes and addresses in bytes.
|
2015-03-16 19:42:18 +02:00
|
|
|
const (
|
2018-07-24 16:15:07 +03:00
|
|
|
// HashLength is the expected length of the hash
|
|
|
|
HashLength = 32
|
2018-09-04 15:12:16 +03:00
|
|
|
// AddressLength is the expected length of the address
|
2015-11-17 18:33:25 +02:00
|
|
|
AddressLength = 20
|
2015-03-16 19:42:18 +02:00
|
|
|
)
|
|
|
|
|
2017-06-23 11:50:49 +03:00
|
|
|
var (
|
|
|
|
hashT = reflect.TypeOf(Hash{})
|
|
|
|
addressT = reflect.TypeOf(Address{})
|
|
|
|
)
|
|
|
|
|
2017-03-02 16:03:20 +03:00
|
|
|
// Hash represents the 32 byte Keccak256 hash of arbitrary data.
|
|
|
|
type Hash [HashLength]byte
|
2015-03-16 12:59:52 +02:00
|
|
|
|
2018-05-29 13:42:21 +03:00
|
|
|
// BytesToHash sets b to hash.
|
|
|
|
// If b is larger than len(h), b will be cropped from the left.
|
2015-03-16 14:23:06 +02:00
|
|
|
func BytesToHash(b []byte) Hash {
|
|
|
|
var h Hash
|
|
|
|
h.SetBytes(b)
|
|
|
|
return h
|
|
|
|
}
|
2018-05-29 13:42:21 +03:00
|
|
|
|
|
|
|
// BigToHash sets byte representation of b to hash.
|
|
|
|
// If b is larger than len(h), b will be cropped from the left.
|
2018-04-10 14:12:07 +03:00
|
|
|
func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) }
|
2015-03-16 14:23:06 +02:00
|
|
|
|
2018-05-29 13:42:21 +03:00
|
|
|
// 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)) }
|
|
|
|
|
2023-06-20 00:38:57 +03:00
|
|
|
// Less compares two hashes.
|
|
|
|
func (h Hash) Less(other Hash) bool {
|
|
|
|
return bytes.Compare(h[:], other[:]) < 0
|
|
|
|
}
|
|
|
|
|
2018-05-29 13:42:21 +03:00
|
|
|
// Bytes gets the byte representation of the underlying hash.
|
2015-03-16 19:42:18 +02:00
|
|
|
func (h Hash) Bytes() []byte { return h[:] }
|
2018-05-29 13:42:21 +03:00
|
|
|
|
|
|
|
// Big converts a hash to a big integer.
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
func (h Hash) Big() *big.Int { return new(big.Int).SetBytes(h[:]) }
|
2018-05-29 13:42:21 +03:00
|
|
|
|
|
|
|
// Hex converts a hash to a hex string.
|
|
|
|
func (h Hash) Hex() string { return hexutil.Encode(h[:]) }
|
2015-03-16 12:59:52 +02:00
|
|
|
|
2017-02-27 18:06:40 +03:00
|
|
|
// TerminalString implements log.TerminalStringer, formatting a string for console
|
|
|
|
// output during logging.
|
|
|
|
func (h Hash) TerminalString() string {
|
2021-04-15 20:35:00 +03:00
|
|
|
return fmt.Sprintf("%x..%x", h[:3], h[29:])
|
2017-02-27 18:06:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// String implements the stringer interface and is used also by the logger when
|
|
|
|
// doing full logging into a file.
|
|
|
|
func (h Hash) String() string {
|
|
|
|
return h.Hex()
|
|
|
|
}
|
|
|
|
|
common: improve printing of Hash and Address (#21834)
Both Hash and Address have a String method, which returns the value as
hex with 0x prefix. They also had a Format method which tried to print
the value using printf of []byte. The way Format worked was at odds with
String though, leading to a situation where fmt.Sprintf("%v", hash)
returned the decimal notation and hash.String() returned a hex string.
This commit makes it consistent again. Both types now support the %v,
%s, %q format verbs for 0x-prefixed hex output. %x, %X creates
unprefixed hex output. %d is also supported and returns the decimal
notation "[1 2 3...]".
For Address, the case of hex characters in %v, %s, %q output is
determined using the EIP-55 checksum. Using %x, %X with Address
disables checksumming.
Co-authored-by: Felix Lange <fjl@twurst.com>
2020-12-08 21:19:09 +03:00
|
|
|
// Format implements fmt.Formatter.
|
2021-08-31 11:22:20 +03:00
|
|
|
// Hash supports the %v, %s, %q, %x, %X and %d format verbs.
|
2017-02-27 18:06:40 +03:00
|
|
|
func (h Hash) Format(s fmt.State, c rune) {
|
common: improve printing of Hash and Address (#21834)
Both Hash and Address have a String method, which returns the value as
hex with 0x prefix. They also had a Format method which tried to print
the value using printf of []byte. The way Format worked was at odds with
String though, leading to a situation where fmt.Sprintf("%v", hash)
returned the decimal notation and hash.String() returned a hex string.
This commit makes it consistent again. Both types now support the %v,
%s, %q format verbs for 0x-prefixed hex output. %x, %X creates
unprefixed hex output. %d is also supported and returns the decimal
notation "[1 2 3...]".
For Address, the case of hex characters in %v, %s, %q output is
determined using the EIP-55 checksum. Using %x, %X with Address
disables checksumming.
Co-authored-by: Felix Lange <fjl@twurst.com>
2020-12-08 21:19:09 +03:00
|
|
|
hexb := make([]byte, 2+len(h)*2)
|
|
|
|
copy(hexb, "0x")
|
|
|
|
hex.Encode(hexb[2:], h[:])
|
|
|
|
|
|
|
|
switch c {
|
|
|
|
case 'x', 'X':
|
|
|
|
if !s.Flag('#') {
|
|
|
|
hexb = hexb[2:]
|
|
|
|
}
|
|
|
|
if c == 'X' {
|
|
|
|
hexb = bytes.ToUpper(hexb)
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case 'v', 's':
|
|
|
|
s.Write(hexb)
|
|
|
|
case 'q':
|
|
|
|
q := []byte{'"'}
|
|
|
|
s.Write(q)
|
|
|
|
s.Write(hexb)
|
|
|
|
s.Write(q)
|
|
|
|
case 'd':
|
|
|
|
fmt.Fprint(s, ([len(h)]byte)(h))
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(s, "%%!%c(hash=%x)", c, h)
|
|
|
|
}
|
2017-02-27 18:06:40 +03:00
|
|
|
}
|
|
|
|
|
2017-02-22 19:59:59 +03:00
|
|
|
// UnmarshalText parses a hash in hex syntax.
|
|
|
|
func (h *Hash) UnmarshalText(input []byte) error {
|
|
|
|
return hexutil.UnmarshalFixedText("Hash", input, h[:])
|
2015-10-15 17:07:19 +03:00
|
|
|
}
|
|
|
|
|
2017-06-23 11:50:49 +03:00
|
|
|
// UnmarshalJSON parses a hash in hex syntax.
|
|
|
|
func (h *Hash) UnmarshalJSON(input []byte) error {
|
|
|
|
return hexutil.UnmarshalFixedJSON(hashT, input, h[:])
|
|
|
|
}
|
|
|
|
|
2017-02-22 19:59:59 +03:00
|
|
|
// MarshalText returns the hex representation of h.
|
|
|
|
func (h Hash) MarshalText() ([]byte, error) {
|
|
|
|
return hexutil.Bytes(h[:]).MarshalText()
|
2015-10-15 17:07:19 +03:00
|
|
|
}
|
|
|
|
|
2018-05-29 13:42:21 +03:00
|
|
|
// SetBytes sets the hash to the value of b.
|
|
|
|
// If b is larger than len(h), b will be cropped from the left.
|
2015-03-16 17:28:16 +02:00
|
|
|
func (h *Hash) SetBytes(b []byte) {
|
2015-03-16 12:59:52 +02:00
|
|
|
if len(b) > len(h) {
|
2015-11-17 18:33:25 +02:00
|
|
|
b = b[len(b)-HashLength:]
|
2015-03-16 12:59:52 +02:00
|
|
|
}
|
|
|
|
|
2015-11-17 18:33:25 +02:00
|
|
|
copy(h[HashLength-len(b):], b)
|
2015-03-16 12:59:52 +02:00
|
|
|
}
|
|
|
|
|
2015-04-27 01:49:49 +03:00
|
|
|
// Generate implements testing/quick.Generator.
|
|
|
|
func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value {
|
|
|
|
m := rand.Intn(len(h))
|
|
|
|
for i := len(h) - 1; i > m; i-- {
|
|
|
|
h[i] = byte(rand.Uint32())
|
|
|
|
}
|
|
|
|
return reflect.ValueOf(h)
|
|
|
|
}
|
|
|
|
|
2018-07-24 16:15:07 +03:00
|
|
|
// Scan implements Scanner for database/sql.
|
|
|
|
func (h *Hash) Scan(src interface{}) error {
|
|
|
|
srcB, ok := src.([]byte)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("can't scan %T into Hash", src)
|
|
|
|
}
|
|
|
|
if len(srcB) != HashLength {
|
|
|
|
return fmt.Errorf("can't scan []byte of len %d into Hash, want %d", len(srcB), HashLength)
|
|
|
|
}
|
|
|
|
copy(h[:], srcB)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements valuer for database/sql.
|
|
|
|
func (h Hash) Value() (driver.Value, error) {
|
|
|
|
return h[:], nil
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:38:13 +03:00
|
|
|
// ImplementsGraphQLType returns true if Hash implements the specified GraphQL type.
|
2019-11-21 17:34:28 +03:00
|
|
|
func (Hash) ImplementsGraphQLType(name string) bool { return name == "Bytes32" }
|
2019-01-21 17:38:13 +03:00
|
|
|
|
|
|
|
// UnmarshalGraphQL unmarshals the provided GraphQL query data.
|
|
|
|
func (h *Hash) UnmarshalGraphQL(input interface{}) error {
|
|
|
|
var err error
|
|
|
|
switch input := input.(type) {
|
|
|
|
case string:
|
2019-09-10 12:20:36 +03:00
|
|
|
err = h.UnmarshalText([]byte(input))
|
2019-01-21 17:38:13 +03:00
|
|
|
default:
|
2019-11-21 17:34:28 +03:00
|
|
|
err = fmt.Errorf("unexpected type %T for Hash", input)
|
2019-01-21 17:38:13 +03:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-02 16:03:20 +03:00
|
|
|
// UnprefixedHash allows marshaling a Hash without 0x prefix.
|
|
|
|
type UnprefixedHash Hash
|
|
|
|
|
|
|
|
// UnmarshalText decodes the hash from hex. The 0x prefix is optional.
|
|
|
|
func (h *UnprefixedHash) UnmarshalText(input []byte) error {
|
|
|
|
return hexutil.UnmarshalFixedUnprefixedText("UnprefixedHash", input, h[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText encodes the hash as hex.
|
|
|
|
func (h UnprefixedHash) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(hex.EncodeToString(h[:])), nil
|
|
|
|
}
|
|
|
|
|
2015-03-16 19:42:18 +02:00
|
|
|
/////////// Address
|
2017-03-02 16:03:20 +03:00
|
|
|
|
|
|
|
// Address represents the 20 byte address of an Ethereum account.
|
|
|
|
type Address [AddressLength]byte
|
|
|
|
|
2018-05-29 13:42:21 +03:00
|
|
|
// BytesToAddress returns Address with value b.
|
|
|
|
// If b is larger than len(h), b will be cropped from the left.
|
2015-03-16 19:42:18 +02:00
|
|
|
func BytesToAddress(b []byte) Address {
|
|
|
|
var a Address
|
|
|
|
a.SetBytes(b)
|
|
|
|
return a
|
|
|
|
}
|
2018-05-29 13:42:21 +03:00
|
|
|
|
|
|
|
// BigToAddress returns Address with byte values of b.
|
|
|
|
// If b is larger than len(h), b will be cropped from the left.
|
2018-04-10 14:12:07 +03:00
|
|
|
func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) }
|
2018-05-29 13:42:21 +03:00
|
|
|
|
|
|
|
// 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)) }
|
2015-03-16 17:44:27 +02:00
|
|
|
|
2015-11-17 18:33:25 +02:00
|
|
|
// IsHexAddress verifies whether a string can represent a valid hex-encoded
|
|
|
|
// Ethereum address or not.
|
|
|
|
func IsHexAddress(s string) bool {
|
2019-08-22 12:45:07 +03:00
|
|
|
if has0xPrefix(s) {
|
2017-12-04 21:34:15 +03:00
|
|
|
s = s[2:]
|
2015-11-17 18:33:25 +02:00
|
|
|
}
|
2017-12-04 21:34:15 +03:00
|
|
|
return len(s) == 2*AddressLength && isHex(s)
|
2015-11-17 18:33:25 +02:00
|
|
|
}
|
|
|
|
|
2023-06-19 10:04:16 +03:00
|
|
|
// Less compares two addresses.
|
|
|
|
func (a Address) Less(other Address) bool {
|
|
|
|
return bytes.Compare(a[:], other[:]) < 0
|
|
|
|
}
|
|
|
|
|
2018-05-29 13:42:21 +03:00
|
|
|
// Bytes gets the string representation of the underlying address.
|
2015-03-16 19:42:18 +02:00
|
|
|
func (a Address) Bytes() []byte { return a[:] }
|
2018-05-29 13:42:21 +03:00
|
|
|
|
|
|
|
// Hash converts an address to a hash by left-padding it with zeros.
|
|
|
|
func (a Address) Hash() Hash { return BytesToHash(a[:]) }
|
2017-07-16 15:26:16 +03:00
|
|
|
|
2022-11-08 14:14:11 +03:00
|
|
|
// Big converts an address to a big integer.
|
|
|
|
func (a Address) Big() *big.Int { return new(big.Int).SetBytes(a[:]) }
|
|
|
|
|
2017-07-16 15:26:16 +03:00
|
|
|
// Hex returns an EIP55-compliant hex string representation of the address.
|
|
|
|
func (a Address) Hex() string {
|
common: improve printing of Hash and Address (#21834)
Both Hash and Address have a String method, which returns the value as
hex with 0x prefix. They also had a Format method which tried to print
the value using printf of []byte. The way Format worked was at odds with
String though, leading to a situation where fmt.Sprintf("%v", hash)
returned the decimal notation and hash.String() returned a hex string.
This commit makes it consistent again. Both types now support the %v,
%s, %q format verbs for 0x-prefixed hex output. %x, %X creates
unprefixed hex output. %d is also supported and returns the decimal
notation "[1 2 3...]".
For Address, the case of hex characters in %v, %s, %q output is
determined using the EIP-55 checksum. Using %x, %X with Address
disables checksumming.
Co-authored-by: Felix Lange <fjl@twurst.com>
2020-12-08 21:19:09 +03:00
|
|
|
return string(a.checksumHex())
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements fmt.Stringer.
|
|
|
|
func (a Address) String() string {
|
|
|
|
return a.Hex()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Address) checksumHex() []byte {
|
|
|
|
buf := a.hex()
|
|
|
|
|
|
|
|
// compute checksum
|
2019-01-04 01:15:26 +03:00
|
|
|
sha := sha3.NewLegacyKeccak256()
|
common: improve printing of Hash and Address (#21834)
Both Hash and Address have a String method, which returns the value as
hex with 0x prefix. They also had a Format method which tried to print
the value using printf of []byte. The way Format worked was at odds with
String though, leading to a situation where fmt.Sprintf("%v", hash)
returned the decimal notation and hash.String() returned a hex string.
This commit makes it consistent again. Both types now support the %v,
%s, %q format verbs for 0x-prefixed hex output. %x, %X creates
unprefixed hex output. %d is also supported and returns the decimal
notation "[1 2 3...]".
For Address, the case of hex characters in %v, %s, %q output is
determined using the EIP-55 checksum. Using %x, %X with Address
disables checksumming.
Co-authored-by: Felix Lange <fjl@twurst.com>
2020-12-08 21:19:09 +03:00
|
|
|
sha.Write(buf[2:])
|
2017-07-16 15:26:16 +03:00
|
|
|
hash := sha.Sum(nil)
|
common: improve printing of Hash and Address (#21834)
Both Hash and Address have a String method, which returns the value as
hex with 0x prefix. They also had a Format method which tried to print
the value using printf of []byte. The way Format worked was at odds with
String though, leading to a situation where fmt.Sprintf("%v", hash)
returned the decimal notation and hash.String() returned a hex string.
This commit makes it consistent again. Both types now support the %v,
%s, %q format verbs for 0x-prefixed hex output. %x, %X creates
unprefixed hex output. %d is also supported and returns the decimal
notation "[1 2 3...]".
For Address, the case of hex characters in %v, %s, %q output is
determined using the EIP-55 checksum. Using %x, %X with Address
disables checksumming.
Co-authored-by: Felix Lange <fjl@twurst.com>
2020-12-08 21:19:09 +03:00
|
|
|
for i := 2; i < len(buf); i++ {
|
|
|
|
hashByte := hash[(i-2)/2]
|
2017-07-16 15:26:16 +03:00
|
|
|
if i%2 == 0 {
|
|
|
|
hashByte = hashByte >> 4
|
|
|
|
} else {
|
|
|
|
hashByte &= 0xf
|
|
|
|
}
|
common: improve printing of Hash and Address (#21834)
Both Hash and Address have a String method, which returns the value as
hex with 0x prefix. They also had a Format method which tried to print
the value using printf of []byte. The way Format worked was at odds with
String though, leading to a situation where fmt.Sprintf("%v", hash)
returned the decimal notation and hash.String() returned a hex string.
This commit makes it consistent again. Both types now support the %v,
%s, %q format verbs for 0x-prefixed hex output. %x, %X creates
unprefixed hex output. %d is also supported and returns the decimal
notation "[1 2 3...]".
For Address, the case of hex characters in %v, %s, %q output is
determined using the EIP-55 checksum. Using %x, %X with Address
disables checksumming.
Co-authored-by: Felix Lange <fjl@twurst.com>
2020-12-08 21:19:09 +03:00
|
|
|
if buf[i] > '9' && hashByte > 7 {
|
|
|
|
buf[i] -= 32
|
2017-07-16 15:26:16 +03:00
|
|
|
}
|
|
|
|
}
|
common: improve printing of Hash and Address (#21834)
Both Hash and Address have a String method, which returns the value as
hex with 0x prefix. They also had a Format method which tried to print
the value using printf of []byte. The way Format worked was at odds with
String though, leading to a situation where fmt.Sprintf("%v", hash)
returned the decimal notation and hash.String() returned a hex string.
This commit makes it consistent again. Both types now support the %v,
%s, %q format verbs for 0x-prefixed hex output. %x, %X creates
unprefixed hex output. %d is also supported and returns the decimal
notation "[1 2 3...]".
For Address, the case of hex characters in %v, %s, %q output is
determined using the EIP-55 checksum. Using %x, %X with Address
disables checksumming.
Co-authored-by: Felix Lange <fjl@twurst.com>
2020-12-08 21:19:09 +03:00
|
|
|
return buf[:]
|
2017-07-16 15:26:16 +03:00
|
|
|
}
|
2015-03-16 12:59:52 +02:00
|
|
|
|
common: improve printing of Hash and Address (#21834)
Both Hash and Address have a String method, which returns the value as
hex with 0x prefix. They also had a Format method which tried to print
the value using printf of []byte. The way Format worked was at odds with
String though, leading to a situation where fmt.Sprintf("%v", hash)
returned the decimal notation and hash.String() returned a hex string.
This commit makes it consistent again. Both types now support the %v,
%s, %q format verbs for 0x-prefixed hex output. %x, %X creates
unprefixed hex output. %d is also supported and returns the decimal
notation "[1 2 3...]".
For Address, the case of hex characters in %v, %s, %q output is
determined using the EIP-55 checksum. Using %x, %X with Address
disables checksumming.
Co-authored-by: Felix Lange <fjl@twurst.com>
2020-12-08 21:19:09 +03:00
|
|
|
func (a Address) hex() []byte {
|
|
|
|
var buf [len(a)*2 + 2]byte
|
|
|
|
copy(buf[:2], "0x")
|
|
|
|
hex.Encode(buf[2:], a[:])
|
|
|
|
return buf[:]
|
2017-02-28 14:35:17 +03:00
|
|
|
}
|
|
|
|
|
common: improve printing of Hash and Address (#21834)
Both Hash and Address have a String method, which returns the value as
hex with 0x prefix. They also had a Format method which tried to print
the value using printf of []byte. The way Format worked was at odds with
String though, leading to a situation where fmt.Sprintf("%v", hash)
returned the decimal notation and hash.String() returned a hex string.
This commit makes it consistent again. Both types now support the %v,
%s, %q format verbs for 0x-prefixed hex output. %x, %X creates
unprefixed hex output. %d is also supported and returns the decimal
notation "[1 2 3...]".
For Address, the case of hex characters in %v, %s, %q output is
determined using the EIP-55 checksum. Using %x, %X with Address
disables checksumming.
Co-authored-by: Felix Lange <fjl@twurst.com>
2020-12-08 21:19:09 +03:00
|
|
|
// Format implements fmt.Formatter.
|
2021-08-31 11:22:20 +03:00
|
|
|
// Address supports the %v, %s, %q, %x, %X and %d format verbs.
|
2017-02-28 14:35:17 +03:00
|
|
|
func (a Address) Format(s fmt.State, c rune) {
|
common: improve printing of Hash and Address (#21834)
Both Hash and Address have a String method, which returns the value as
hex with 0x prefix. They also had a Format method which tried to print
the value using printf of []byte. The way Format worked was at odds with
String though, leading to a situation where fmt.Sprintf("%v", hash)
returned the decimal notation and hash.String() returned a hex string.
This commit makes it consistent again. Both types now support the %v,
%s, %q format verbs for 0x-prefixed hex output. %x, %X creates
unprefixed hex output. %d is also supported and returns the decimal
notation "[1 2 3...]".
For Address, the case of hex characters in %v, %s, %q output is
determined using the EIP-55 checksum. Using %x, %X with Address
disables checksumming.
Co-authored-by: Felix Lange <fjl@twurst.com>
2020-12-08 21:19:09 +03:00
|
|
|
switch c {
|
|
|
|
case 'v', 's':
|
|
|
|
s.Write(a.checksumHex())
|
|
|
|
case 'q':
|
|
|
|
q := []byte{'"'}
|
|
|
|
s.Write(q)
|
|
|
|
s.Write(a.checksumHex())
|
|
|
|
s.Write(q)
|
|
|
|
case 'x', 'X':
|
|
|
|
// %x disables the checksum.
|
|
|
|
hex := a.hex()
|
|
|
|
if !s.Flag('#') {
|
|
|
|
hex = hex[2:]
|
|
|
|
}
|
|
|
|
if c == 'X' {
|
|
|
|
hex = bytes.ToUpper(hex)
|
|
|
|
}
|
|
|
|
s.Write(hex)
|
|
|
|
case 'd':
|
|
|
|
fmt.Fprint(s, ([len(a)]byte)(a))
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(s, "%%!%c(address=%x)", c, a)
|
|
|
|
}
|
2017-02-28 14:35:17 +03:00
|
|
|
}
|
|
|
|
|
2018-05-29 13:42:21 +03:00
|
|
|
// SetBytes sets the address to the value of b.
|
2020-11-16 16:08:13 +03:00
|
|
|
// If b is larger than len(a), b will be cropped from the left.
|
2015-03-16 17:44:27 +02:00
|
|
|
func (a *Address) SetBytes(b []byte) {
|
2015-03-16 13:20:17 +02:00
|
|
|
if len(b) > len(a) {
|
2015-11-17 18:33:25 +02:00
|
|
|
b = b[len(b)-AddressLength:]
|
2015-03-16 12:59:52 +02:00
|
|
|
}
|
2015-11-17 18:33:25 +02:00
|
|
|
copy(a[AddressLength-len(b):], b)
|
2015-03-16 12:59:52 +02:00
|
|
|
}
|
2015-03-16 14:23:06 +02:00
|
|
|
|
2017-02-22 19:59:59 +03:00
|
|
|
// MarshalText returns the hex representation of a.
|
|
|
|
func (a Address) MarshalText() ([]byte, error) {
|
|
|
|
return hexutil.Bytes(a[:]).MarshalText()
|
2015-10-15 17:07:19 +03:00
|
|
|
}
|
|
|
|
|
2017-02-22 19:59:59 +03:00
|
|
|
// UnmarshalText parses a hash in hex syntax.
|
|
|
|
func (a *Address) UnmarshalText(input []byte) error {
|
|
|
|
return hexutil.UnmarshalFixedText("Address", input, a[:])
|
2015-10-15 17:07:19 +03:00
|
|
|
}
|
|
|
|
|
2017-06-23 11:50:49 +03:00
|
|
|
// UnmarshalJSON parses a hash in hex syntax.
|
|
|
|
func (a *Address) UnmarshalJSON(input []byte) error {
|
|
|
|
return hexutil.UnmarshalFixedJSON(addressT, input, a[:])
|
|
|
|
}
|
|
|
|
|
2018-07-24 16:15:07 +03:00
|
|
|
// Scan implements Scanner for database/sql.
|
|
|
|
func (a *Address) Scan(src interface{}) error {
|
|
|
|
srcB, ok := src.([]byte)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("can't scan %T into Address", src)
|
|
|
|
}
|
|
|
|
if len(srcB) != AddressLength {
|
|
|
|
return fmt.Errorf("can't scan []byte of len %d into Address, want %d", len(srcB), AddressLength)
|
|
|
|
}
|
|
|
|
copy(a[:], srcB)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value implements valuer for database/sql.
|
|
|
|
func (a Address) Value() (driver.Value, error) {
|
|
|
|
return a[:], nil
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:38:13 +03:00
|
|
|
// ImplementsGraphQLType returns true if Hash implements the specified GraphQL type.
|
|
|
|
func (a Address) ImplementsGraphQLType(name string) bool { return name == "Address" }
|
|
|
|
|
|
|
|
// UnmarshalGraphQL unmarshals the provided GraphQL query data.
|
|
|
|
func (a *Address) UnmarshalGraphQL(input interface{}) error {
|
|
|
|
var err error
|
|
|
|
switch input := input.(type) {
|
|
|
|
case string:
|
2019-09-10 12:20:36 +03:00
|
|
|
err = a.UnmarshalText([]byte(input))
|
2019-01-21 17:38:13 +03:00
|
|
|
default:
|
2019-11-21 17:34:28 +03:00
|
|
|
err = fmt.Errorf("unexpected type %T for Address", input)
|
2019-01-21 17:38:13 +03:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-29 13:42:21 +03:00
|
|
|
// UnprefixedAddress allows marshaling an Address without 0x prefix.
|
2017-03-02 16:03:20 +03:00
|
|
|
type UnprefixedAddress Address
|
|
|
|
|
|
|
|
// UnmarshalText decodes the address from hex. The 0x prefix is optional.
|
|
|
|
func (a *UnprefixedAddress) UnmarshalText(input []byte) error {
|
|
|
|
return hexutil.UnmarshalFixedUnprefixedText("UnprefixedAddress", input, a[:])
|
|
|
|
}
|
2015-06-15 13:16:29 +03:00
|
|
|
|
2017-03-02 16:03:20 +03:00
|
|
|
// MarshalText encodes the address as hex.
|
|
|
|
func (a UnprefixedAddress) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(hex.EncodeToString(a[:])), nil
|
2015-06-15 13:16:29 +03:00
|
|
|
}
|
cmd/clef, signer: initial poc of the standalone signer (#16154)
* signer: introduce external signer command
* cmd/signer, rpc: Implement new signer. Add info about remote user to Context
* signer: refactored request/response, made use of urfave.cli
* cmd/signer: Use common flags
* cmd/signer: methods to validate calldata against abi
* cmd/signer: work on abi parser
* signer: add mutex around UI
* cmd/signer: add json 4byte directory, remove passwords from api
* cmd/signer: minor changes
* cmd/signer: Use ErrRequestDenied, enable lightkdf
* cmd/signer: implement tests
* cmd/signer: made possible for UI to modify tx parameters
* cmd/signer: refactors, removed channels in ui comms, added UI-api via stdin/out
* cmd/signer: Made lowercase json-definitions, added UI-signer test functionality
* cmd/signer: update documentation
* cmd/signer: fix bugs, improve abi detection, abi argument display
* cmd/signer: minor change in json format
* cmd/signer: rework json communication
* cmd/signer: implement mixcase addresses in API, fix json id bug
* cmd/signer: rename fromaccount, update pythonpoc with new json encoding format
* cmd/signer: make use of new abi interface
* signer: documentation
* signer/main: remove redundant option
* signer: implement audit logging
* signer: create package 'signer', minor changes
* common: add 0x-prefix to mixcaseaddress in json marshalling + validation
* signer, rules, storage: implement rules + ephemeral storage for signer rules
* signer: implement OnApprovedTx, change signing response (API BREAKAGE)
* signer: refactoring + documentation
* signer/rules: implement dispatching to next handler
* signer: docs
* signer/rules: hide json-conversion from users, ensure context is cleaned
* signer: docs
* signer: implement validation rules, change signature of call_info
* signer: fix log flaw with string pointer
* signer: implement custom 4byte databsae that saves submitted signatures
* signer/storage: implement aes-gcm-backed credential storage
* accounts: implement json unmarshalling of url
* signer: fix listresponse, fix gas->uint64
* node: make http/ipc start methods public
* signer: add ipc capability+review concerns
* accounts: correct docstring
* signer: address review concerns
* rpc: go fmt -s
* signer: review concerns+ baptize Clef
* signer,node: move Start-functions to separate file
* signer: formatting
2018-04-16 15:04:32 +03:00
|
|
|
|
|
|
|
// MixedcaseAddress retains the original string, which may or may not be
|
|
|
|
// correctly checksummed
|
|
|
|
type MixedcaseAddress struct {
|
|
|
|
addr Address
|
|
|
|
original string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMixedcaseAddress constructor (mainly for testing)
|
|
|
|
func NewMixedcaseAddress(addr Address) MixedcaseAddress {
|
|
|
|
return MixedcaseAddress{addr: addr, original: addr.Hex()}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMixedcaseAddressFromString is mainly meant for unit-testing
|
|
|
|
func NewMixedcaseAddressFromString(hexaddr string) (*MixedcaseAddress, error) {
|
|
|
|
if !IsHexAddress(hexaddr) {
|
2019-11-21 17:34:28 +03:00
|
|
|
return nil, errors.New("invalid address")
|
cmd/clef, signer: initial poc of the standalone signer (#16154)
* signer: introduce external signer command
* cmd/signer, rpc: Implement new signer. Add info about remote user to Context
* signer: refactored request/response, made use of urfave.cli
* cmd/signer: Use common flags
* cmd/signer: methods to validate calldata against abi
* cmd/signer: work on abi parser
* signer: add mutex around UI
* cmd/signer: add json 4byte directory, remove passwords from api
* cmd/signer: minor changes
* cmd/signer: Use ErrRequestDenied, enable lightkdf
* cmd/signer: implement tests
* cmd/signer: made possible for UI to modify tx parameters
* cmd/signer: refactors, removed channels in ui comms, added UI-api via stdin/out
* cmd/signer: Made lowercase json-definitions, added UI-signer test functionality
* cmd/signer: update documentation
* cmd/signer: fix bugs, improve abi detection, abi argument display
* cmd/signer: minor change in json format
* cmd/signer: rework json communication
* cmd/signer: implement mixcase addresses in API, fix json id bug
* cmd/signer: rename fromaccount, update pythonpoc with new json encoding format
* cmd/signer: make use of new abi interface
* signer: documentation
* signer/main: remove redundant option
* signer: implement audit logging
* signer: create package 'signer', minor changes
* common: add 0x-prefix to mixcaseaddress in json marshalling + validation
* signer, rules, storage: implement rules + ephemeral storage for signer rules
* signer: implement OnApprovedTx, change signing response (API BREAKAGE)
* signer: refactoring + documentation
* signer/rules: implement dispatching to next handler
* signer: docs
* signer/rules: hide json-conversion from users, ensure context is cleaned
* signer: docs
* signer: implement validation rules, change signature of call_info
* signer: fix log flaw with string pointer
* signer: implement custom 4byte databsae that saves submitted signatures
* signer/storage: implement aes-gcm-backed credential storage
* accounts: implement json unmarshalling of url
* signer: fix listresponse, fix gas->uint64
* node: make http/ipc start methods public
* signer: add ipc capability+review concerns
* accounts: correct docstring
* signer: address review concerns
* rpc: go fmt -s
* signer: review concerns+ baptize Clef
* signer,node: move Start-functions to separate file
* signer: formatting
2018-04-16 15:04:32 +03:00
|
|
|
}
|
|
|
|
a := FromHex(hexaddr)
|
|
|
|
return &MixedcaseAddress{addr: BytesToAddress(a), original: hexaddr}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON parses MixedcaseAddress
|
|
|
|
func (ma *MixedcaseAddress) UnmarshalJSON(input []byte) error {
|
|
|
|
if err := hexutil.UnmarshalFixedJSON(addressT, input, ma.addr[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal(input, &ma.original)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON marshals the original value
|
2023-03-30 22:04:28 +03:00
|
|
|
func (ma MixedcaseAddress) MarshalJSON() ([]byte, error) {
|
cmd/clef, signer: initial poc of the standalone signer (#16154)
* signer: introduce external signer command
* cmd/signer, rpc: Implement new signer. Add info about remote user to Context
* signer: refactored request/response, made use of urfave.cli
* cmd/signer: Use common flags
* cmd/signer: methods to validate calldata against abi
* cmd/signer: work on abi parser
* signer: add mutex around UI
* cmd/signer: add json 4byte directory, remove passwords from api
* cmd/signer: minor changes
* cmd/signer: Use ErrRequestDenied, enable lightkdf
* cmd/signer: implement tests
* cmd/signer: made possible for UI to modify tx parameters
* cmd/signer: refactors, removed channels in ui comms, added UI-api via stdin/out
* cmd/signer: Made lowercase json-definitions, added UI-signer test functionality
* cmd/signer: update documentation
* cmd/signer: fix bugs, improve abi detection, abi argument display
* cmd/signer: minor change in json format
* cmd/signer: rework json communication
* cmd/signer: implement mixcase addresses in API, fix json id bug
* cmd/signer: rename fromaccount, update pythonpoc with new json encoding format
* cmd/signer: make use of new abi interface
* signer: documentation
* signer/main: remove redundant option
* signer: implement audit logging
* signer: create package 'signer', minor changes
* common: add 0x-prefix to mixcaseaddress in json marshalling + validation
* signer, rules, storage: implement rules + ephemeral storage for signer rules
* signer: implement OnApprovedTx, change signing response (API BREAKAGE)
* signer: refactoring + documentation
* signer/rules: implement dispatching to next handler
* signer: docs
* signer/rules: hide json-conversion from users, ensure context is cleaned
* signer: docs
* signer: implement validation rules, change signature of call_info
* signer: fix log flaw with string pointer
* signer: implement custom 4byte databsae that saves submitted signatures
* signer/storage: implement aes-gcm-backed credential storage
* accounts: implement json unmarshalling of url
* signer: fix listresponse, fix gas->uint64
* node: make http/ipc start methods public
* signer: add ipc capability+review concerns
* accounts: correct docstring
* signer: address review concerns
* rpc: go fmt -s
* signer: review concerns+ baptize Clef
* signer,node: move Start-functions to separate file
* signer: formatting
2018-04-16 15:04:32 +03:00
|
|
|
if strings.HasPrefix(ma.original, "0x") || strings.HasPrefix(ma.original, "0X") {
|
|
|
|
return json.Marshal(fmt.Sprintf("0x%s", ma.original[2:]))
|
|
|
|
}
|
|
|
|
return json.Marshal(fmt.Sprintf("0x%s", ma.original))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address returns the address
|
|
|
|
func (ma *MixedcaseAddress) Address() Address {
|
|
|
|
return ma.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements fmt.Stringer
|
|
|
|
func (ma *MixedcaseAddress) String() string {
|
|
|
|
if ma.ValidChecksum() {
|
|
|
|
return fmt.Sprintf("%s [chksum ok]", ma.original)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s [chksum INVALID]", ma.original)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidChecksum returns true if the address has valid checksum
|
|
|
|
func (ma *MixedcaseAddress) ValidChecksum() bool {
|
|
|
|
return ma.original == ma.addr.Hex()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Original returns the mixed-case input string
|
|
|
|
func (ma *MixedcaseAddress) Original() string {
|
|
|
|
return ma.original
|
|
|
|
}
|
2023-05-17 11:29:56 +03:00
|
|
|
|
|
|
|
// AddressEIP55 is an alias of Address with a customized json marshaller
|
|
|
|
type AddressEIP55 Address
|
|
|
|
|
|
|
|
// String returns the hex representation of the address in the manner of EIP55.
|
|
|
|
func (addr AddressEIP55) String() string {
|
|
|
|
return Address(addr).Hex()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON marshals the address in the manner of EIP55.
|
|
|
|
func (addr AddressEIP55) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(addr.String())
|
|
|
|
}
|
2023-05-17 17:39:33 +03:00
|
|
|
|
|
|
|
type Decimal uint64
|
|
|
|
|
|
|
|
func isString(input []byte) bool {
|
|
|
|
return len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"'
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON parses a hash in hex syntax.
|
|
|
|
func (d *Decimal) UnmarshalJSON(input []byte) error {
|
|
|
|
if !isString(input) {
|
|
|
|
return &json.UnmarshalTypeError{Value: "non-string", Type: reflect.TypeOf(uint64(0))}
|
|
|
|
}
|
|
|
|
if i, err := strconv.ParseInt(string(input[1:len(input)-1]), 10, 64); err == nil {
|
|
|
|
*d = Decimal(i)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|