diff options
author | Felix Lange <fjl@twurst.com> | 2015-03-19 19:17:43 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-03-19 19:17:43 +0800 |
commit | e13c6739804604849c7e43d27b073e68fba58191 (patch) | |
tree | b3471931b63210a4d107875dffd6572bcd96239e /common | |
parent | 965c9babe336cfa8d5c740d5356acbc5f9ba4a72 (diff) | |
parent | 5f35e6778f10d9e2c6418beff7ed201be80448c4 (diff) | |
download | dexon-e13c6739804604849c7e43d27b073e68fba58191.tar dexon-e13c6739804604849c7e43d27b073e68fba58191.tar.gz dexon-e13c6739804604849c7e43d27b073e68fba58191.tar.bz2 dexon-e13c6739804604849c7e43d27b073e68fba58191.tar.lz dexon-e13c6739804604849c7e43d27b073e68fba58191.tar.xz dexon-e13c6739804604849c7e43d27b073e68fba58191.tar.zst dexon-e13c6739804604849c7e43d27b073e68fba58191.zip |
Merge remote-tracking branch 'ethereum/conversion' into conversion
Diffstat (limited to 'common')
-rw-r--r-- | common/common.go | 9 | ||||
-rw-r--r-- | common/types_template.go | 48 |
2 files changed, 57 insertions, 0 deletions
diff --git a/common/common.go b/common/common.go index 1371d18d4..155bb5c2a 100644 --- a/common/common.go +++ b/common/common.go @@ -65,6 +65,15 @@ func DefaultDataDir() string { } } +func ToHex(b []byte) string { + hex := Bytes2Hex(b) + // Prefer output of "0x0" instead of "0x" + if len(hex) == 0 { + hex = "0" + } + return "0x" + hex +} + func FromHex(s string) []byte { if len(s) > 1 { if s[0:2] == "0x" { diff --git a/common/types_template.go b/common/types_template.go new file mode 100644 index 000000000..1c82a36dc --- /dev/null +++ b/common/types_template.go @@ -0,0 +1,48 @@ +// +build none +//sed -e 's/_N_/Hash/g' -e 's/_S_/32/g' -e '1d' types_template.go | gofmt -w hash.go + +package common + +import "math/big" + +type _N_ [_S_]byte + +func BytesTo_N_(b []byte) _N_ { + var h _N_ + h.SetBytes(b) + return h +} +func StringTo_N_(s string) _N_ { return BytesTo_N_([]byte(s)) } +func BigTo_N_(b *big.Int) _N_ { return BytesTo_N_(b.Bytes()) } +func HexTo_N_(s string) _N_ { return BytesTo_N_(FromHex(s)) } + +// Don't use the default 'String' method in case we want to overwrite + +// Get the string representation of the underlying hash +func (h _N_) Str() string { return string(h[:]) } +func (h _N_) Bytes() []byte { return h[:] } +func (h _N_) Big() *big.Int { return Bytes2Big(h[:]) } +func (h _N_) 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 *_N_) SetBytes(b []byte) { + // Use the right most bytes + if len(b) > len(h) { + b = b[len(b)-_S_:] + } + + // Reverse the loop + for i := len(b) - 1; i >= 0; i-- { + h[_S_-len(b)+i] = b[i] + } +} + +// Set string `s` to h. If s is larger than len(h) it will panic +func (h *_N_) SetString(s string) { h.SetBytes([]byte(s)) } + +// Sets h to other +func (h *_N_) Set(other _N_) { + for i, v := range other { + h[i] = v + } +} |