diff options
author | obscuren <geffobscura@gmail.com> | 2015-03-16 18:59:52 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-03-16 18:59:52 +0800 |
commit | f486c0ae563eaf89a601ca5d60f30be96db2e69a (patch) | |
tree | a134ae6e38b90a268d91c61eab4ceb17856f2144 /common/types.go | |
parent | b5234413611ce5984292f85a01de1f56c045b490 (diff) | |
download | dexon-f486c0ae563eaf89a601ca5d60f30be96db2e69a.tar dexon-f486c0ae563eaf89a601ca5d60f30be96db2e69a.tar.gz dexon-f486c0ae563eaf89a601ca5d60f30be96db2e69a.tar.bz2 dexon-f486c0ae563eaf89a601ca5d60f30be96db2e69a.tar.lz dexon-f486c0ae563eaf89a601ca5d60f30be96db2e69a.tar.xz dexon-f486c0ae563eaf89a601ca5d60f30be96db2e69a.tar.zst dexon-f486c0ae563eaf89a601ca5d60f30be96db2e69a.zip |
new type + additional methods
Diffstat (limited to 'common/types.go')
-rw-r--r-- | common/types.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/common/types.go b/common/types.go index 7646b2c34..11ac39815 100644 --- a/common/types.go +++ b/common/types.go @@ -4,3 +4,39 @@ type ( Hash [32]byte Address [20]byte ) + +// Don't use the default 'String' method in case we want to overwrite + +// Get the string representation of the underlying hash +func (h Hash) Str() string { + return string(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) { + panic("unable to set bytes. too big") + } + + // reverse loop + for i := len(b); i >= 0; i-- { + h[i] = b[i] + } +} + +// Get the string representation of the underlying address +func (a Address) Str() string { + return string(a[:]) +} + +// Sets the address to the value of b. If b is larger than len(a) it will panic +func (h Address) SetBytes(b []byte) { + if len(b) > len(h) { + panic("unable to set bytes. too big") + } + + // reverse loop + for i := len(b); i >= 0; i-- { + h[i] = b[i] + } +} |