mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
common: Let Hex2Bytes and Hex2BytesFixed panic for invalid hex strings
This commit is contained in:
parent
bc0b87ca19
commit
91f01b0df7
1 changed files with 12 additions and 2 deletions
|
|
@ -76,14 +76,24 @@ func Bytes2Hex(d []byte) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hex2Bytes returns the bytes represented by the hexadecimal string str.
|
// Hex2Bytes returns the bytes represented by the hexadecimal string str.
|
||||||
|
// The string must not be prefixed with `0x`.
|
||||||
|
// It panics if str contains a non-hex char.
|
||||||
func Hex2Bytes(str string) []byte {
|
func Hex2Bytes(str string) []byte {
|
||||||
h, _ := hex.DecodeString(str)
|
h, err := hex.DecodeString(str)
|
||||||
|
if err != nil {
|
||||||
|
panic("error decoding string: " + err.Error())
|
||||||
|
}
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hex2BytesFixed returns bytes of a specified fixed length flen.
|
// Hex2BytesFixed returns bytes of a specified fixed length flen.
|
||||||
|
// The string must not be prefixed with `0x`.
|
||||||
|
// It panics if str contains a non-hex char.
|
||||||
func Hex2BytesFixed(str string, flen int) []byte {
|
func Hex2BytesFixed(str string, flen int) []byte {
|
||||||
h, _ := hex.DecodeString(str)
|
h, err := hex.DecodeString(str)
|
||||||
|
if err != nil {
|
||||||
|
panic("error decoding string: " + err.Error())
|
||||||
|
}
|
||||||
if len(h) == flen {
|
if len(h) == flen {
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue