-
Notifications
You must be signed in to change notification settings - Fork 8
/
types.go
122 lines (98 loc) · 2.72 KB
/
types.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package mainchain
import (
"encoding/hex"
"math/big"
ec "github.com/ethereum/go-ethereum/common"
)
var (
// ZeroAddr is all 0s
ZeroAddr Addr
// ZeroAddrHex is string of 20 0s
ZeroAddrHex = Addr2Hex(ZeroAddr)
// ZeroBigInt is big.NewInt(0)
ZeroBigInt = big.NewInt(0)
// ZeroCid is all 0s
ZeroCid CidType
)
// CidType is the type for payment channel ID
// Note we need to change all cid.Hex() to Cid2Hex() because Hash.Hex() has 0x prefix
type CidType = ec.Hash
// HashType is the type for ethereum hash type
type HashType = ec.Hash
// Addr is alias to geth common.Address
type Addr = ec.Address
// DPoSCandidateInfo contains info emitted by DPoS contract
type DPoSCandidateInfo struct {
Initialized bool
MinSelfStake *big.Int
StakingPool *big.Int
Status *big.Int
UnbondTime *big.Int
CommissionRate *big.Int
RateLockEndTime *big.Int
}
// SidechainAddr is alias to []byte
type SidechainAddr = []byte
// ========== Hex/Bytes ==========
// Hex2Bytes supports hex string with or without 0x prefix
// Calls hex.DecodeString directly and ignore err
// similar to ec.FromHex but better
func Hex2Bytes(s string) (b []byte) {
if len(s) >= 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
s = s[2:]
}
// hex.DecodeString expects an even-length string
if len(s)%2 == 1 {
s = "0" + s
}
b, _ = hex.DecodeString(s)
return b
}
// Bytes2Hex returns hex string without 0x prefix
func Bytes2Hex(b []byte) string {
return hex.EncodeToString(b)
}
// ========== Address ==========
// Hex2Addr accepts hex string with or without 0x prefix and return Addr
func Hex2Addr(s string) Addr {
return ec.HexToAddress(s)
}
// Addr2Hex returns hex without 0x
func Addr2Hex(a Addr) string {
return Bytes2Hex(a[:])
}
// Bytes2Addr returns Address from b
// Addr.Bytes() does the reverse
func Bytes2Addr(b []byte) Addr {
return ec.BytesToAddress(b)
}
// Bytes2AddrHex returns hex without 0x
func Bytes2AddrHex(b []byte) string {
return Addr2Hex(Bytes2Addr(b))
}
// FormatAddrHex formats a string into standard Addr string
func FormatAddrHex(s string) string {
return Addr2Hex(Hex2Addr(s))
}
// ========== CidType ==========
// Bytes2Cid converts bytes to CidType
func Bytes2Cid(b []byte) CidType {
return ec.BytesToHash(b)
}
// Cid2Hex returns hex without 0x prefix
func Cid2Hex(p CidType) string {
return Bytes2Hex(p[:])
}
// Hex2Cid accepts hex string with or without 0x prefix and return CidType
func Hex2Cid(s string) CidType {
return ec.HexToHash(s)
}
// ========== Hash ==========
// Hex2Hash accepts hex string with or without 0x prefix and return HashType
func Hex2Hash(s string) HashType {
return ec.HexToHash(s)
}
// Bytes2Hash converts bytes to HashType
func Bytes2Hash(b []byte) HashType {
return ec.BytesToHash(b)
}