aboutsummaryrefslogtreecommitdiffstats
path: root/common/math/big.go
diff options
context:
space:
mode:
Diffstat (limited to 'common/math/big.go')
-rw-r--r--common/math/big.go19
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) {