diff options
Diffstat (limited to 'common/hexutil/hexutil.go')
-rw-r--r-- | common/hexutil/hexutil.go | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/common/hexutil/hexutil.go b/common/hexutil/hexutil.go index 4ec0ee8e6..6b128ae36 100644 --- a/common/hexutil/hexutil.go +++ b/common/hexutil/hexutil.go @@ -49,6 +49,7 @@ var ( ErrOddLength = errors.New("hex string has odd length") ErrUint64Range = errors.New("hex number does not fit into 64 bits") ErrUintRange = fmt.Errorf("hex number does not fit into %d bits", uintBits) + ErrBig256Range = errors.New("hex number does not fit into 256 bits") ) // Decode decodes a hex string with 0x prefix. @@ -59,7 +60,11 @@ func Decode(input string) ([]byte, error) { if !has0xPrefix(input) { return nil, ErrMissingPrefix } - return hex.DecodeString(input[2:]) + b, err := hex.DecodeString(input[2:]) + if err != nil { + err = mapError(err) + } + return b, err } // MustDecode decodes a hex string with 0x prefix. It panics for invalid input. @@ -126,11 +131,15 @@ func init() { } // DecodeBig decodes a hex string with 0x prefix as a quantity. +// Numbers larger than 256 bits are not accepted. func DecodeBig(input string) (*big.Int, error) { raw, err := checkNumber(input) if err != nil { return nil, err } + if len(raw) > 64 { + return nil, ErrBig256Range + } words := make([]big.Word, len(raw)/bigWordNibbles+1) end := len(raw) for i := range words { @@ -169,7 +178,7 @@ func EncodeBig(bigint *big.Int) string { if nbits == 0 { return "0x0" } - return fmt.Sprintf("0x%x", bigint) + return fmt.Sprintf("%#x", bigint) } func has0xPrefix(input string) bool { |