forked from wealdtech/ethereal
/
contract.go
415 lines (398 loc) · 12.4 KB
/
contract.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright © 2017 Weald Technology Trading
// 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 cmd
import (
"encoding/hex"
"fmt"
"io"
"math/big"
"os"
"reflect"
"strings"
"unsafe"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
"github.com/wealdtech/ethereal/cli"
"github.com/wealdtech/ethereal/util"
)
var contractStr string
var contractAbi string
var contractJSON string
var contractName string
// contractCmd represents the contract command
var contractCmd = &cobra.Command{
Use: "contract",
Short: "Manage contracts",
Long: `Call contracts directly.`,
}
func init() {
RootCmd.AddCommand(contractCmd)
}
func contractFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&contractStr, "contract", "", "address of the contract")
cmd.Flags().StringVar(&contractAbi, "abi", "", "ABI, or path to ABI, for the contract")
cmd.Flags().StringVar(&contractJSON, "json", "", "JSON, or path to JSON, for the contract as output by solc --combined-json=bin,abi")
cmd.Flags().StringVar(&contractName, "name", "", "Name of the contract (required when using json)")
}
// parse contract given the information from various flags
func parseContract(binStr string) *util.Contract {
var contract *util.Contract
if contractJSON != "" {
cli.Assert(contractName != "", quiet, "--name required with JSON")
contract, err = util.ParseCombinedJSON(contractJSON, contractName)
cli.ErrCheck(err, quiet, "Failed to parse JSON")
} else {
contract = &util.Contract{}
// Add name if present
if contractName != "" {
contract.Name = contractName
}
// Add binary if present
var bin []byte
bin, err = hex.DecodeString(strings.TrimPrefix(binStr, "0x"))
cli.ErrCheck(err, quiet, "Failed to decode data")
contract = &util.Contract{Binary: bin}
// Add ABI if present
if contractAbi != "" {
abi, err := contractParseAbi(contractAbi)
cli.ErrCheck(err, quiet, fmt.Sprintf("Failed to parse ABI %s", contractAbi))
contract.Abi = abi
}
}
return contract
}
func contractParseAbi(input string) (output abi.ABI, err error) {
var reader io.Reader
if strings.HasPrefix(contractAbi, "[") {
// ABI is direct
reader = strings.NewReader(input)
} else {
// ABI value is a path
reader, err = os.Open(input)
if err != nil {
return
}
}
return abi.JSON(reader)
}
func contractUnpack(abi abi.ABI, name string, data []byte) (result *[]*interface{}, err error) {
method, exists := abi.Methods[name]
if !exists {
return nil, fmt.Errorf("The method %s does not exist", name)
}
var res []*interface{}
result = &res
if len(method.Outputs) == 0 {
return
} else if len(method.Outputs) == 1 {
output := reflect.New(method.Outputs[0].Type.Type).Elem().Interface()
err = abi.Unpack(&output, name, data)
res = append(res, &output)
} else {
for i := range method.Outputs {
output := reflect.New(method.Outputs[i].Type.Type.Elem()).Elem().Interface()
res = append(res, &output)
}
err = abi.Unpack(&res, name, data)
}
return
}
func contractStringToValue(argType abi.Type, val string) (interface{}, error) {
val = strings.Trim(val, " ")
switch argType.T {
case abi.IntTy:
res := big.NewInt(0)
res, success := res.SetString(val, 10)
if !success {
return nil, fmt.Errorf("Bad integer %s", val)
}
switch argType.Size {
case 8:
return int8(res.Uint64()), nil
case 16:
return int16(res.Uint64()), nil
case 32:
return int32(res.Uint64()), nil
case 64:
return int64(res.Uint64()), nil
default:
return res, nil
}
case abi.UintTy:
res := big.NewInt(0)
res, success := res.SetString(val, 10)
if !success {
return nil, fmt.Errorf("Bad integer %s", val)
}
switch argType.Size {
case 8:
return uint8(res.Uint64()), nil
case 16:
return uint16(res.Uint64()), nil
case 32:
return uint32(res.Uint64()), nil
case 64:
return uint64(res.Uint64()), nil
default:
return res, nil
}
case abi.BoolTy:
if val == "true" || val == "True" || val == "1" {
return true, nil
}
return false, nil
case abi.StringTy:
return val, nil
case abi.SliceTy:
return nil, fmt.Errorf("Unhandled type slice (%s)", argType)
case abi.ArrayTy:
return nil, fmt.Errorf("Unhandled type array (%s)", argType)
case abi.AddressTy:
return common.HexToAddress(val), nil
case abi.FixedBytesTy:
slice := make([]byte, argType.Size)
var decoded []byte
decoded, err = hex.DecodeString(strings.TrimPrefix(val, "0x"))
if err == nil {
copy(slice[argType.Size-len(decoded):argType.Size], decoded)
}
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
switch argType.Size {
case 1:
return *(*[1]uint8)(unsafe.Pointer(hdr.Data)), nil
case 2:
return *(*[2]uint8)(unsafe.Pointer(hdr.Data)), nil
case 3:
return *(*[3]uint8)(unsafe.Pointer(hdr.Data)), nil
case 4:
return *(*[4]uint8)(unsafe.Pointer(hdr.Data)), nil
case 5:
return *(*[5]uint8)(unsafe.Pointer(hdr.Data)), nil
case 6:
return *(*[6]uint8)(unsafe.Pointer(hdr.Data)), nil
case 7:
return *(*[7]uint8)(unsafe.Pointer(hdr.Data)), nil
case 8:
return *(*[8]uint8)(unsafe.Pointer(hdr.Data)), nil
case 9:
return *(*[9]uint8)(unsafe.Pointer(hdr.Data)), nil
case 10:
return *(*[10]uint8)(unsafe.Pointer(hdr.Data)), nil
case 11:
return *(*[11]uint8)(unsafe.Pointer(hdr.Data)), nil
case 12:
return *(*[12]uint8)(unsafe.Pointer(hdr.Data)), nil
case 13:
return *(*[13]uint8)(unsafe.Pointer(hdr.Data)), nil
case 14:
return *(*[14]uint8)(unsafe.Pointer(hdr.Data)), nil
case 15:
return *(*[15]uint8)(unsafe.Pointer(hdr.Data)), nil
case 16:
return *(*[16]uint8)(unsafe.Pointer(hdr.Data)), nil
case 17:
return *(*[17]uint8)(unsafe.Pointer(hdr.Data)), nil
case 18:
return *(*[18]uint8)(unsafe.Pointer(hdr.Data)), nil
case 19:
return *(*[19]uint8)(unsafe.Pointer(hdr.Data)), nil
case 20:
return *(*[20]uint8)(unsafe.Pointer(hdr.Data)), nil
case 21:
return *(*[21]uint8)(unsafe.Pointer(hdr.Data)), nil
case 22:
return *(*[22]uint8)(unsafe.Pointer(hdr.Data)), nil
case 23:
return *(*[23]uint8)(unsafe.Pointer(hdr.Data)), nil
case 24:
return *(*[24]uint8)(unsafe.Pointer(hdr.Data)), nil
case 25:
return *(*[25]uint8)(unsafe.Pointer(hdr.Data)), nil
case 26:
return *(*[26]uint8)(unsafe.Pointer(hdr.Data)), nil
case 27:
return *(*[27]uint8)(unsafe.Pointer(hdr.Data)), nil
case 28:
return *(*[28]uint8)(unsafe.Pointer(hdr.Data)), nil
case 29:
return *(*[29]uint8)(unsafe.Pointer(hdr.Data)), nil
case 30:
return *(*[30]uint8)(unsafe.Pointer(hdr.Data)), nil
case 31:
return *(*[31]uint8)(unsafe.Pointer(hdr.Data)), nil
case 32:
return *(*[32]uint8)(unsafe.Pointer(hdr.Data)), nil
}
return nil, fmt.Errorf("Invalid byte size %d", argType.Size)
case abi.BytesTy:
return hex.DecodeString(strings.TrimPrefix(val, "0x"))
case abi.HashTy:
return common.HexToHash(val), nil
case abi.FixedPointTy:
return nil, fmt.Errorf("Unhandled type %v", argType)
case abi.FunctionTy:
return nil, fmt.Errorf("Unhandled type %v", argType)
default:
return nil, fmt.Errorf("Unknown type %v", argType)
}
}
func contractValueToString(argType abi.Type, val interface{}) (string, error) {
switch argType.T {
case abi.IntTy:
return fmt.Sprintf("%v", val), nil
case abi.UintTy:
return fmt.Sprintf("%v", val), nil
case abi.BoolTy:
if val.(bool) == true {
return "true", nil
}
return "false", nil
case abi.StringTy:
return val.(string), nil
case abi.SliceTy:
res := make([]string, 0)
arrayVal := reflect.ValueOf(val)
for i := 0; i < arrayVal.Len(); i++ {
elemRes, err := contractValueToString(*argType.Elem, arrayVal.Index(i).Interface())
if err != nil {
return "", err
}
res = append(res, elemRes)
}
return "[" + strings.Join(res, ",") + "]", nil
case abi.ArrayTy:
res := make([]string, 0)
arrayVal := reflect.ValueOf(val)
for i := 0; i < arrayVal.Len(); i++ {
elemRes, err := contractValueToString(*argType.Elem, arrayVal.Index(i).Interface())
if err != nil {
return "", err
}
res = append(res, elemRes)
}
return "[" + strings.Join(res, ",") + "]", nil
case abi.AddressTy:
return val.(common.Address).Hex(), nil
case abi.FixedBytesTy:
switch argType.Size {
case 1:
castVal := val.([1]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 2:
castVal := val.([2]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 3:
castVal := val.([3]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 4:
castVal := val.([4]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 5:
castVal := val.([5]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 6:
castVal := val.([6]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 7:
castVal := val.([7]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 8:
castVal := val.([8]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 9:
castVal := val.([9]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 10:
castVal := val.([10]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 11:
castVal := val.([11]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 12:
castVal := val.([12]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 13:
castVal := val.([13]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 14:
castVal := val.([14]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 15:
castVal := val.([15]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 16:
castVal := val.([16]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 17:
castVal := val.([17]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 18:
castVal := val.([18]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 19:
castVal := val.([19]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 20:
castVal := val.([20]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 21:
castVal := val.([21]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 22:
castVal := val.([22]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 23:
castVal := val.([23]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 24:
castVal := val.([24]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 25:
castVal := val.([25]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 26:
castVal := val.([26]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 27:
castVal := val.([27]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 28:
castVal := val.([28]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 29:
castVal := val.([29]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 30:
castVal := val.([30]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 31:
castVal := val.([31]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
case 32:
castVal := val.([32]byte)
return fmt.Sprintf("0x%s", hex.EncodeToString(castVal[:])), nil
}
return "", fmt.Errorf("Invalid byte size %d", argType.Size)
case abi.BytesTy:
return fmt.Sprintf("0x%s", hex.EncodeToString(val.([]byte))), nil
case abi.HashTy:
return val.(common.Hash).Hex(), nil
case abi.FixedPointTy:
return "", fmt.Errorf("Unhandled type %v", argType)
case abi.FunctionTy:
return "", fmt.Errorf("Unhandled type %v", argType)
default:
return "", fmt.Errorf("Unknown type %v", argType)
}
}