@@ -18,35 +18,38 @@ package abi
1818
1919import (
2020 "bytes"
21+ "fmt"
22+ "log"
2123 "math/big"
2224 "reflect"
2325 "strings"
2426 "testing"
2527
28+ "github.com/ethereum/go-ethereum/common"
2629 "github.com/ethereum/go-ethereum/crypto"
2730)
2831
2932const jsondata = `
3033[
3134 { "name" : "balance", "const" : true },
32- { "name" : "send", "const" : false, "input " : [ { "name" : "amount", "type" : "uint256" } ] }
35+ { "name" : "send", "const" : false, "inputs " : [ { "name" : "amount", "type" : "uint256" } ] }
3336]`
3437
3538const jsondata2 = `
3639[
3740 { "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]" } ] }
5053]`
5154
5255func TestType (t * testing.T ) {
@@ -344,3 +347,49 @@ func TestPackSliceBig(t *testing.T) {
344347 t .Errorf ("expected %x got %x" , sig , packed )
345348 }
346349}
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