diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-03-23 23:36:38 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-23 23:36:38 +0800 |
commit | 8771c3061f340451d0966adcc547338a25f2231f (patch) | |
tree | c566cab81cf95a39f85fbe2c98a932af9495eb68 /common/math/big.go | |
parent | 11e7a712f469fb24ddb88ecebcefab6ed8880eb8 (diff) | |
parent | 37dd9086ec491900311fc39837f4a62ef5fd3a4a (diff) | |
download | dexon-8771c3061f340451d0966adcc547338a25f2231f.tar dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.gz dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.bz2 dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.lz dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.xz dexon-8771c3061f340451d0966adcc547338a25f2231f.tar.zst dexon-8771c3061f340451d0966adcc547338a25f2231f.zip |
Merge pull request #3794 from fjl/core-genesis-refactor
core: refactor genesis handling
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) { |