Repository navigation
Expand file tree
/
Copy pathtypes.go
More file actions
232 lines (200 loc) · 4.64 KB
/
Copy pathtypes.go
File metadata and controls
232 lines (200 loc) · 4.64 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package zvcgo
import (
"encoding/hex"
"errors"
"fmt"
"github.com/zvchain/zvchain/common"
"io"
"regexp"
"strconv"
"strings"
)
var (
ErrorInvalidAssetString = errors.New("invalid asset string")
ErrorInvalidAddressString = errors.New("invalid address string")
ErrorInvalidPrivateKey = errors.New("invalid private key")
ErrorInvalid0xString = errors.New("invalid hex String")
ErrorInvalidZVString = errors.New("invalid address String")
ErrorSignerNotFound = errors.New("signer not found")
ErrorSourceEmpty = errors.New("source should not be empty")
)
const AddrPrefix = "zv"
const HashPrefix = "0x"
const AddressLength = 32
const Idlength = 32
const (
Ra = 1
kRa = 1000
mRa = 1000000
ZVC = 1000000000
)
type Address struct {
data []byte
}
var addrReg = regexp.MustCompile("^[Zz][Vv][0-9a-fA-F]{64}$")
func ValidateAddress(addr string) bool {
return addrReg.MatchString(addr)
}
func NewAddressFromString(s string) (addr Address, err error) {
if !ValidateAddress(s) {
return Address{}, ErrorInvalidAddressString
}
addr.data, err = hex.DecodeString(s[2:])
return addr, err
}
func NewAddressFromBytes(bs []byte) (addr Address, err error) {
if len(bs) != AddressLength {
return addr, ErrorInvalidAddressString
}
addr.data = bs
return addr, err
}
func (a Address) String() string {
return AddrPrefix + hex.EncodeToString(a.data)
}
func (a Address) Bytes() []byte {
return a.data
}
func (a *Address) UnmarshalJSON(input []byte) error {
js := string(input)
js = js[1 : len(js)-1]
data, err := DecodeZV(js)
a.data = data
return err
}
func (a *Address) MarshalJSON() ([]byte, error) {
str := "\"" + ToAddrHex(a.data) + "\""
return []byte(str), nil
}
type Asset struct {
Value uint64
}
func NewAssetFromString(s string) (Asset, error) {
asset := Asset{}
var valuePart, symbol []byte
reader := strings.NewReader(s)
for {
b, err := reader.ReadByte()
if err == io.EOF {
break
}
if b == ' ' {
continue
} else if b == '.' || (b >= '0' && b <= '9') {
if len(symbol) != 0 {
return Asset{}, ErrorInvalidAssetString
}
valuePart = append(valuePart, b)
} else if b >= 'a' && b <= 'z' {
symbol = append(symbol, b)
} else if b >= 'A' && b <= 'Z' {
symbol = append(symbol, b+32)
}
}
value, err := strconv.ParseFloat(string(valuePart), 64)
if err != nil {
return Asset{}, ErrorInvalidAssetString
}
switch string(symbol) {
case "ra":
asset.Value = uint64(value * Ra)
case "mra":
asset.Value = uint64(value * mRa)
case "kra":
asset.Value = uint64(value * kRa)
case "zvc":
asset.Value = uint64(value * ZVC)
default:
return Asset{}, ErrorInvalidAssetString
}
return asset, nil
}
func (a Asset) ZVC() uint64 {
return a.Value / ZVC
}
func (a Asset) KRa() uint64 {
return a.Value / kRa
}
func (a Asset) MRa() uint64 {
return a.Value / mRa
}
func (a Asset) Ra() uint64 {
return a.Value
}
type Hash struct {
data []byte
}
func (h Hash) Bytes() []byte {
return h.data
}
func (h Hash) String() string {
if len(h.data) == 0 {
return "0x00"
}
return HashPrefix + hex.EncodeToString(h.data)
}
func (h *Hash) UnmarshalJSON(input []byte) error {
js := string(input)
js = js[1 : len(js)-1]
data, err := Decode(js)
h.data = data
return err
}
func (h *Hash) MarshalJSON() ([]byte, error) {
str := "\"" + h.String() + "\""
return []byte(str), nil
}
func (id *ID) MarshalJSON() ([]byte, error) {
str := "\"" + ToAddrHex(id.Serialize()) + "\""
return []byte(str), nil
}
// ToZvHex converts the input byte array to a hex string
func ToAddrHex(b []byte) string {
h := hex.EncodeToString(b)
// Prefer output of "0x0" instead of "0x"
if len(h) == 0 {
h = "0"
}
return AddrPrefix + h
}
// Serialize convert ID to byte slice (LittleEndian)
func (id ID) Serialize() []byte {
idBytes := id.value.Bytes()
if len(idBytes) == Idlength {
return idBytes
}
if len(idBytes) > Idlength {
// hold it for now
panic("ID Serialize error: ID bytes is more than Idlength")
}
buff := make([]byte, Idlength)
copy(buff[Idlength-len(idBytes):Idlength], idBytes)
return buff
}
func (id *ID) UnmarshalJSON(data []byte) error {
str := string(data[:])
if len(str) < 2 {
return fmt.Errorf("data size less than min")
}
str = str[1 : len(str)-1]
js, err := DecodeZV(str)
id.value.SetBytes(js)
return err
}
type Event struct {
Deleted bool
Height uint64
TxHash Hash
Index uint64
Message map[string]interface{}
Default []interface{}
}
type ABI struct {
FuncName string `json:"func_name"`
Args []interface{} `json:"args"`
}
type Contract struct {
Code string `json:"code"`
ContractName string `json:"contract_name"`
ContractAddress *common.Address `json:"-"`
}