erigon-pulse/common/hexutil.go
2022-12-19 15:38:57 +07:00

49 lines
1.2 KiB
Go

/*
Copyright 2022 The Erigon contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"encoding/binary"
"encoding/hex"
)
func MustDecodeHex(in string) []byte {
in = strip0x(in)
if len(in)%2 == 1 {
in = "0" + in
}
payload, err := hex.DecodeString(in)
if err != nil {
panic(err)
}
return payload
}
func strip0x(str string) string {
if len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') {
return str[2:]
}
return str
}
// EncodeTs encodes a TimeStamp (BlockNumber or TxNumber or other uin64) as big endian
func EncodeTs(number uint64) []byte {
enc := make([]byte, 8)
binary.BigEndian.PutUint64(enc, number)
return enc
}