diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-11-27 17:41:22 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-11-27 17:41:22 +0800 |
commit | 7dde2b902cf81e90b484b1a48f6d45e0abd10e0f (patch) | |
tree | 9b92cb3e42269697e0a2b553ba31c36aef73cc25 /common/types.go | |
parent | ffe58bf5abe5100b29ac1091c882f586cd3a2ef9 (diff) | |
parent | 3e1000fda3424d880bc43ebbb16d8a33447d4182 (diff) | |
download | go-tangerine-7dde2b902cf81e90b484b1a48f6d45e0abd10e0f.tar go-tangerine-7dde2b902cf81e90b484b1a48f6d45e0abd10e0f.tar.gz go-tangerine-7dde2b902cf81e90b484b1a48f6d45e0abd10e0f.tar.bz2 go-tangerine-7dde2b902cf81e90b484b1a48f6d45e0abd10e0f.tar.lz go-tangerine-7dde2b902cf81e90b484b1a48f6d45e0abd10e0f.tar.xz go-tangerine-7dde2b902cf81e90b484b1a48f6d45e0abd10e0f.tar.zst go-tangerine-7dde2b902cf81e90b484b1a48f6d45e0abd10e0f.zip |
Merge pull request #1970 from karalabe/customizable-protocol-stacks
Customizable protocol stacks
Diffstat (limited to 'common/types.go')
-rw-r--r-- | common/types.go | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/common/types.go b/common/types.go index 624f4b826..ea5838188 100644 --- a/common/types.go +++ b/common/types.go @@ -24,13 +24,13 @@ import ( ) const ( - hashLength = 32 - addressLength = 20 + HashLength = 32 + AddressLength = 20 ) type ( - Hash [hashLength]byte - Address [addressLength]byte + Hash [HashLength]byte + Address [AddressLength]byte ) func BytesToHash(b []byte) Hash { @@ -53,10 +53,10 @@ func (h Hash) Hex() string { return "0x" + Bytes2Hex(h[:]) } // Sets the hash to the value of b. If b is larger than len(h) it will panic func (h *Hash) SetBytes(b []byte) { if len(b) > len(h) { - b = b[len(b)-hashLength:] + b = b[len(b)-HashLength:] } - copy(h[hashLength-len(b):], b) + copy(h[HashLength-len(b):], b) } // Set string `s` to h. If s is larger than len(h) it will panic @@ -92,6 +92,18 @@ func StringToAddress(s string) Address { return BytesToAddress([]byte(s)) } func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) } func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) } +// IsHexAddress verifies whether a string can represent a valid hex-encoded +// Ethereum address or not. +func IsHexAddress(s string) bool { + if len(s) == 2+2*AddressLength && IsHex(s[2:]) { + return true + } + if len(s) == 2*AddressLength && IsHex(s) { + return true + } + return false +} + // Get the string representation of the underlying address func (a Address) Str() string { return string(a[:]) } func (a Address) Bytes() []byte { return a[:] } @@ -102,9 +114,9 @@ func (a Address) Hex() string { return "0x" + Bytes2Hex(a[:]) } // Sets the address to the value of b. If b is larger than len(a) it will panic func (a *Address) SetBytes(b []byte) { if len(b) > len(a) { - b = b[len(b)-addressLength:] + b = b[len(b)-AddressLength:] } - 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 |