-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_abi.go
292 lines (265 loc) · 7.4 KB
/
api_abi.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package ethapi
import (
"encoding/json"
"fmt"
"math/big"
"strings"
"github.com/dece-cash/go-dece/czero/c_superzk"
"github.com/pkg/errors"
"github.com/dece-cash/go-dece/czero/c_type"
"github.com/dece-cash/go-dece/accounts/abi"
"github.com/dece-cash/go-dece/common"
"github.com/dece-cash/go-dece/common/hexutil"
"github.com/dece-cash/go-dece/common/math"
)
type PublicAbiAPI struct {
}
func decodeOutputParms(abiArgs abi.Arguments) (interface{}, bool) {
for _, arg := range abiArgs {
switch arg.Type.String() {
case "address":
return common.ContractAddress{}, true
case "address[]":
return []common.ContractAddress{}, true
case "uint8":
return hexutil.Uint8(0), false
case "uint16":
return hexutil.Uint16(0), false
case "uint32":
return hexutil.Uint32(0), false
case "uint64":
return hexutil.Uint64(0), false
case "uint256":
var big hexutil.Big
return big, false
case "string":
return string(""), false
case "string[]":
return []string{}, false
case "bool":
var result bool
return result, false
default:
return nil, false
}
}
return nil, false
}
func decodeResult(abiArgs abi.Arguments, result interface{}) interface{} {
for _, arg := range abiArgs {
switch arg.Type.String() {
case "address":
return result
case "address[]":
return result
case "uint8":
return hexutil.Uint64(result.(uint8))
case "uint16":
return hexutil.Uint64(result.(uint16))
case "uint32":
return hexutil.Uint64(result.(uint32))
case "uint64":
return hexutil.Uint64(result.(uint64))
case "uint256":
return hexutil.Big(*(result.(*big.Int)))
case "string":
return result
case "string[]":
return result
case "bool":
return result
default:
return nil
}
}
return nil
}
func encodeStringParams(abiArgs abi.Arguments, args []string) ([]interface{}, []PKrAddress, error) {
argsLen := len(args)
packArgs := make([]interface{}, argsLen)
address := []PKrAddress{}
for index, arg := range abiArgs {
switch arg.Type.String() {
case "address":
var addr AllMixedAddress
err := addr.UnmarshalText([]byte(args[index]))
if err != nil {
return nil, nil, err
}
//caddr := (c_superzk.HashPKr(addr.ToPKr().NewRef()))
packArgs[index] = addr
address = append(address, addr.ToPKrAddress())
case "address[]":
var addrs []AllMixedAddress
err := json.Unmarshal([]byte(args[index]), &addrs)
if err != nil {
return nil, nil, err
}
packArgs[index] = addrs
pkrs := convertToAddr(addrs)
address = append(address, pkrs...)
case "uint8":
var num hexutil.Uint8
err := num.UnmarshalText([]byte(args[index]))
if err != nil {
return nil, nil, err
}
packArgs[index] = uint8(num)
case "uint16":
var num hexutil.Uint16
err := num.UnmarshalText([]byte(args[index]))
if err != nil {
return nil, nil, err
}
packArgs[index] = uint16(num)
case "uint32":
var num hexutil.Uint32
err := num.UnmarshalText([]byte(args[index]))
if err != nil {
return nil, nil, err
}
packArgs[index] = uint32(num)
case "uint64":
var num hexutil.Uint64
err := num.UnmarshalText([]byte(args[index]))
if err != nil {
return nil, nil, err
}
packArgs[index] = uint64(num)
case "uint256":
var num hexutil.Big
err := num.UnmarshalText([]byte(args[index]))
if err != nil {
return nil, nil, err
}
packArgs[index] = num.ToInt()
case "string":
packArgs[index] = args[index]
case "string[]":
strs := []string{}
value := strings.Replace(strings.Replace(args[index], "[", "", 1), "]", "", 1)
values := strings.Split(value, ",")
for _, vv := range values {
strs = append(strs, vv)
}
packArgs[index] = strs
case "bool":
switch args[index] {
case "true":
packArgs[index] = true
case "false":
packArgs[index] = false
default:
return nil, nil, errors.New("error args !")
}
default:
return nil, nil, fmt.Errorf("unsupported arg type")
}
}
return packArgs[:], address, nil
}
func PackMethod(abi *abi.ABI, contractAddr ContractAddress, methodName string, args []string) (hexutil.Bytes, error) {
if abi == nil {
return hexutil.Bytes{}, errors.New("ABI can not be nil")
}
method, exist := abi.Methods[methodName]
if !exist {
return nil, fmt.Errorf("method '%s' not found", methodName)
}
abiArgs := method.Inputs
if len(abiArgs) != len(args) {
return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(abiArgs))
}
packArgs, address, err := encodeStringParams(abiArgs, args)
input, err := abi.Pack(methodName, packArgs...)
if err != nil {
return hexutil.Bytes{}, err
}
prefix := [18]byte{}
addressLen := len(address)
copy(prefix[:], contractAddr[:16])
if len(address) > 1000 {
return nil, errors.New("too many address args")
}
lenBytes := math.PaddedBigBytes(big.NewInt(int64(addressLen)), 2)
copy(prefix[16:], lenBytes[:])
result := []byte{}
result = append(result, prefix[:]...)
for _, addr := range address {
result = append(result, addr[:]...)
}
result = append(result, input[:]...)
return hexutil.Bytes(result[:]), nil
}
func (s *PublicAbiAPI) PackMethod(abi *abi.ABI, contractAddr ContractAddress, methodName string, args []string) (hexutil.Bytes, error) {
return PackMethod(abi, contractAddr, methodName, args)
}
func convertToContractAddr(addrs []AllMixedAddress) (result []common.ContractAddress) {
for _, addr := range addrs {
caddr := (c_superzk.HashPKr(addr.ToPKr().NewRef()))
result = append(result, common.BytesToContractAddress(caddr[:]))
}
return
}
func convertToAddr(addrs []AllMixedAddress) (result []PKrAddress) {
for _, addr := range addrs {
result = append(result, addr.ToPKrAddress())
}
return
}
func PackConstruct(abi *abi.ABI, data hexutil.Bytes, args []string) (hexutil.Bytes, error) {
if abi == nil {
return hexutil.Bytes{}, errors.New("ABI can not be nil")
}
abiArgs := abi.Constructor.Inputs
if len(abiArgs) != len(args) {
return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(abiArgs))
}
packArgs, address, err := encodeStringParams(abiArgs, args)
if err != nil {
return hexutil.Bytes{}, err
}
input, err := abi.Pack("", packArgs...)
if err != nil {
return hexutil.Bytes{}, err
}
prefix := [18]byte{}
addressLen := len(address)
rand := c_type.RandUint128()
copy(prefix[:], rand[:])
if len(address) > 1000 {
return nil, errors.New("too many address args")
}
lenBytes := math.PaddedBigBytes(big.NewInt(int64(addressLen)), 2)
copy(prefix[16:], lenBytes[:])
result := []byte{}
result = append(result, prefix[:]...)
for _, addr := range address {
result = append(result, addr[:]...)
}
fmt.Printf(hexutil.Encode(data))
result = append(result, data[:]...)
result = append(result, input[:]...)
return hexutil.Bytes(result[:]), nil
}
func (s *PublicAbiAPI) PackConstruct(abi *abi.ABI, data hexutil.Bytes, args []string) (hexutil.Bytes, error) {
return PackConstruct(abi, data, args)
}
func UnPack(abi *abi.ABI, name string, output hexutil.Bytes) (interface{}, error) {
if abi == nil {
return hexutil.Bytes{}, errors.New("ABI can not be nil")
}
if method, ok := abi.Methods[name]; ok {
marshalledValues, err := method.Outputs.UnpackValues(output)
if err != nil {
return nil, err
}
return marshalledValues, nil
} else {
return nil, errors.New("can not find method=" + name)
}
return nil, nil
}
func (s *PublicAbiAPI) UnPack(abi *abi.ABI, name string, output hexutil.Bytes) (interface{}, error) {
return UnPack(abi, name, output)
}