diff options
author | Felix Lange <fjl@twurst.com> | 2017-03-08 06:19:44 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2017-03-23 22:58:42 +0800 |
commit | 04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76 (patch) | |
tree | 61cdf35a8b91b8d67cc34738cdd60253acdb9326 /common/math/big.go | |
parent | 61d2150a0750a554250c3bf090ef994be6c060f0 (diff) | |
download | dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.tar dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.tar.gz dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.tar.bz2 dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.tar.lz dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.tar.xz dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.tar.zst dexon-04fa6a374499dcefeb3f854c4cf6cfcdfb6c8c76.zip |
common/math: add HexOrDecimal64, HexOrDecimal256
Diffstat (limited to 'common/math/big.go')
-rw-r--r-- | common/math/big.go | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/common/math/big.go b/common/math/big.go index 704ca40a9..0b67a1b50 100644 --- a/common/math/big.go +++ b/common/math/big.go @@ -18,6 +18,7 @@ package math import ( + "fmt" "math/big" ) @@ -35,6 +36,24 @@ const ( wordBytes = wordBits / 8 ) +// HexOrDecimal256 marshals big.Int as hex or decimal. +type HexOrDecimal256 big.Int + +// UnmarshalText implements encoding.TextUnmarshaler. +func (i *HexOrDecimal256) UnmarshalText(input []byte) error { + bigint, ok := ParseBig256(string(input)) + if !ok { + return fmt.Errorf("invalid hex or decimal integer %q", input) + } + *i = HexOrDecimal256(*bigint) + return nil +} + +// MarshalText implements encoding.TextMarshaler. +func (i *HexOrDecimal256) MarshalText() ([]byte, error) { + return []byte(fmt.Sprintf("%#x", (*big.Int)(i))), nil +} + // ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax. // Leading zeros are accepted. The empty string parses as zero. func ParseBig256(s string) (*big.Int, bool) { |