forked from umbracle/ethgo
/
util.go
46 lines (40 loc) · 803 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package jsonrpc
import (
"encoding/hex"
"fmt"
"math/big"
"strconv"
"strings"
)
func encodeUintToHex(i uint64) string {
return fmt.Sprintf("0x%x", i)
}
func parseBigInt(str string) *big.Int {
if strings.HasPrefix(str, "0x") {
str = str[2:]
}
num := new(big.Int)
num.SetString(str, 16)
return num
}
func parseUint64orHex(str string) (uint64, error) {
base := 10
if strings.HasPrefix(str, "0x") {
str = str[2:]
base = 16
}
return strconv.ParseUint(str, base, 64)
}
func encodeToHex(b []byte) string {
return "0x" + hex.EncodeToString(b)
}
func parseHexBytes(str string) ([]byte, error) {
if !strings.HasPrefix(str, "0x") {
return nil, fmt.Errorf("it does not have 0x prefix")
}
buf, err := hex.DecodeString(str[2:])
if err != nil {
return nil, err
}
return buf, nil
}