@@ -18,35 +18,38 @@ package abi
18
18
19
19
import (
20
20
"bytes"
21
+ "fmt"
22
+ "log"
21
23
"math/big"
22
24
"reflect"
23
25
"strings"
24
26
"testing"
25
27
28
+ "github.com/ethereum/go-ethereum/common"
26
29
"github.com/ethereum/go-ethereum/crypto"
27
30
)
28
31
29
32
const jsondata = `
30
33
[
31
34
{ "name" : "balance", "const" : true },
32
- { "name" : "send", "const" : false, "input " : [ { "name" : "amount", "type" : "uint256" } ] }
35
+ { "name" : "send", "const" : false, "inputs " : [ { "name" : "amount", "type" : "uint256" } ] }
33
36
]`
34
37
35
38
const jsondata2 = `
36
39
[
37
40
{ "name" : "balance", "const" : true },
38
- { "name" : "send", "const" : false, "input " : [ { "name" : "amount", "type" : "uint256" } ] },
39
- { "name" : "test", "const" : false, "input " : [ { "name" : "number", "type" : "uint32" } ] },
40
- { "name" : "string", "const" : false, "input " : [ { "name" : "input ", "type" : "string" } ] },
41
- { "name" : "bool", "const" : false, "input " : [ { "name" : "input ", "type" : "bool" } ] },
42
- { "name" : "address", "const" : false, "input " : [ { "name" : "input ", "type" : "address" } ] },
43
- { "name" : "string32", "const" : false, "input " : [ { "name" : "input ", "type" : "string32" } ] },
44
- { "name" : "uint64[2]", "const" : false, "input " : [ { "name" : "input ", "type" : "uint64[2]" } ] },
45
- { "name" : "uint64[]", "const" : false, "input " : [ { "name" : "input ", "type" : "uint64[]" } ] },
46
- { "name" : "foo", "const" : false, "input " : [ { "name" : "input ", "type" : "uint32" } ] },
47
- { "name" : "bar", "const" : false, "input " : [ { "name" : "input ", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
48
- { "name" : "slice", "const" : false, "input " : [ { "name" : "input ", "type" : "uint32[2]" } ] },
49
- { "name" : "slice256", "const" : false, "input " : [ { "name" : "input ", "type" : "uint256[2]" } ] }
41
+ { "name" : "send", "const" : false, "inputs " : [ { "name" : "amount", "type" : "uint256" } ] },
42
+ { "name" : "test", "const" : false, "inputs " : [ { "name" : "number", "type" : "uint32" } ] },
43
+ { "name" : "string", "const" : false, "inputs " : [ { "name" : "inputs ", "type" : "string" } ] },
44
+ { "name" : "bool", "const" : false, "inputs " : [ { "name" : "inputs ", "type" : "bool" } ] },
45
+ { "name" : "address", "const" : false, "inputs " : [ { "name" : "inputs ", "type" : "address" } ] },
46
+ { "name" : "string32", "const" : false, "inputs " : [ { "name" : "inputs ", "type" : "string32" } ] },
47
+ { "name" : "uint64[2]", "const" : false, "inputs " : [ { "name" : "inputs ", "type" : "uint64[2]" } ] },
48
+ { "name" : "uint64[]", "const" : false, "inputs " : [ { "name" : "inputs ", "type" : "uint64[]" } ] },
49
+ { "name" : "foo", "const" : false, "inputs " : [ { "name" : "inputs ", "type" : "uint32" } ] },
50
+ { "name" : "bar", "const" : false, "inputs " : [ { "name" : "inputs ", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
51
+ { "name" : "slice", "const" : false, "inputs " : [ { "name" : "inputs ", "type" : "uint32[2]" } ] },
52
+ { "name" : "slice256", "const" : false, "inputs " : [ { "name" : "inputs ", "type" : "uint256[2]" } ] }
50
53
]`
51
54
52
55
func TestType (t * testing.T ) {
@@ -344,3 +347,49 @@ func TestPackSliceBig(t *testing.T) {
344
347
t .Errorf ("expected %x got %x" , sig , packed )
345
348
}
346
349
}
350
+
351
+ func ExampleJSON () {
352
+ const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]`
353
+
354
+ abi , err := JSON (strings .NewReader (definition ))
355
+ if err != nil {
356
+ log .Fatalln (err )
357
+ }
358
+ out , err := abi .Pack ("isBar" , common .HexToAddress ("01" ))
359
+ if err != nil {
360
+ log .Fatalln (err )
361
+ }
362
+
363
+ fmt .Printf ("%x\n " , out )
364
+ // Output:
365
+ // 1f2c40920000000000000000000000000000000000000000000000000000000000000001
366
+ }
367
+
368
+ func TestBytes (t * testing.T ) {
369
+ const definition = `[
370
+ { "name" : "balance", "const" : true, "inputs" : [ { "name" : "address", "type" : "bytes20" } ] },
371
+ { "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
372
+ ]`
373
+
374
+ abi , err := JSON (strings .NewReader (definition ))
375
+ if err != nil {
376
+ t .Fatal (err )
377
+ }
378
+ ok := make ([]byte , 20 )
379
+ _ , err = abi .Pack ("balance" , ok )
380
+ if err != nil {
381
+ t .Error (err )
382
+ }
383
+
384
+ toosmall := make ([]byte , 19 )
385
+ _ , err = abi .Pack ("balance" , toosmall )
386
+ if err != nil {
387
+ t .Error (err )
388
+ }
389
+
390
+ toobig := make ([]byte , 21 )
391
+ _ , err = abi .Pack ("balance" , toobig )
392
+ if err == nil {
393
+ t .Error ("expected error" )
394
+ }
395
+ }
0 commit comments