aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2017-03-02 21:03:20 +0800
committerFelix Lange <fjl@twurst.com>2017-03-23 22:58:42 +0800
commitd42a56afc53cf1d12dc1b9089f0ddce2c9155d72 (patch)
tree09059388036ebceb2d4db422bd41e4e5dd9a1d9a /common
parentb4547a560b861e2e5463bf6fed6d61958c4e9411 (diff)
downloadgo-tangerine-d42a56afc53cf1d12dc1b9089f0ddce2c9155d72.tar
go-tangerine-d42a56afc53cf1d12dc1b9089f0ddce2c9155d72.tar.gz
go-tangerine-d42a56afc53cf1d12dc1b9089f0ddce2c9155d72.tar.bz2
go-tangerine-d42a56afc53cf1d12dc1b9089f0ddce2c9155d72.tar.lz
go-tangerine-d42a56afc53cf1d12dc1b9089f0ddce2c9155d72.tar.xz
go-tangerine-d42a56afc53cf1d12dc1b9089f0ddce2c9155d72.tar.zst
go-tangerine-d42a56afc53cf1d12dc1b9089f0ddce2c9155d72.zip
common: add UnprefixedHash, UnprefixedAddress
Diffstat (limited to 'common')
-rw-r--r--common/types.go44
1 files changed, 30 insertions, 14 deletions
diff --git a/common/types.go b/common/types.go
index 9c50beb13..05288bf46 100644
--- a/common/types.go
+++ b/common/types.go
@@ -17,6 +17,7 @@
package common
import (
+ "encoding/hex"
"fmt"
"math/big"
"math/rand"
@@ -30,13 +31,8 @@ const (
AddressLength = 20
)
-type (
- // Hash represents the 32 byte Keccak256 hash of arbitrary data.
- Hash [HashLength]byte
-
- // Address represents the 20 byte address of an Ethereum account.
- Address [AddressLength]byte
-)
+// Hash represents the 32 byte Keccak256 hash of arbitrary data.
+type Hash [HashLength]byte
func BytesToHash(b []byte) Hash {
var h Hash
@@ -113,7 +109,24 @@ func EmptyHash(h Hash) bool {
return h == Hash{}
}
+// 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
+}
+
/////////// Address
+
+// Address represents the 20 byte address of an Ethereum account.
+type Address [AddressLength]byte
+
func BytesToAddress(b []byte) Address {
var a Address
a.SetBytes(b)
@@ -181,12 +194,15 @@ func (a *Address) UnmarshalText(input []byte) error {
return hexutil.UnmarshalFixedText("Address", input, a[:])
}
-// PP Pretty Prints a byte slice in the following format:
-// hex(value[:4])...(hex[len(value)-4:])
-func PP(value []byte) string {
- if len(value) <= 8 {
- return Bytes2Hex(value)
- }
+// UnprefixedHash allows marshaling an Address without 0x prefix.
+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[:])
+}
- return fmt.Sprintf("%x...%x", value[:4], value[len(value)-4])
+// MarshalText encodes the address as hex.
+func (a UnprefixedAddress) MarshalText() ([]byte, error) {
+ return []byte(hex.EncodeToString(a[:])), nil
}