11package api
22
33import (
4+ "bytes"
45 "testing"
56
67 "github.com/ethereum/go-ethereum/common"
@@ -10,23 +11,23 @@ func TestAddrUnpacked(t *testing.T) {
1011 cases := []struct {
1112 leaf []byte
1213 ltd []string
13- want common. Address
14+ want [] byte
1415 }{
1516 {
16- common .Hex2Bytes ("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001" ),
17+ common .FromHex ("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001" ),
1718 []string {"uint32" , "address" },
18- common .HexToAddress ("0x0000000000000000000000000000000000000001" ),
19+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
1920 },
2021 {
21- common .Hex2Bytes ("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000" ),
22+ common .FromHex ("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000" ),
2223 []string {"address" , "uint32" },
23- common .HexToAddress ("0x0000000000000000000000000000000000000001" ),
24+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
2425 },
2526 }
2627
2728 for _ , c := range cases {
2829 addr := addrUnpacked (c .leaf , c .ltd )
29- if addr != c .want {
30+ if ! bytes . Equal ( addr , c .want ) {
3031 t .Errorf ("expected: %v got: %v" , c .want , addr )
3132 }
3233 }
@@ -36,24 +37,90 @@ func TestAddrPacked(t *testing.T) {
3637 cases := []struct {
3738 leaf []byte
3839 ltd []string
39- want common. Address
40+ want [] byte
4041 }{
4142 {
42- common .Hex2Bytes ("000000000000000000000000000000000000000000000001" ),
43+ common .FromHex ("000000000000000000000000000000000000000000000001" ),
4344 []string {"uint32" , "address" },
44- common .HexToAddress ("0x0000000000000000000000000000000000000001" ),
45+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
4546 },
4647 {
47- common .Hex2Bytes ("000000000000000000000000000000000000000100000000" ),
48+ common .FromHex ("000000000000000000000000000000000000000100000000" ),
4849 []string {"address" , "uint32" },
49- common .HexToAddress ("0x0000000000000000000000000000000000000001" ),
50+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
5051 },
5152 }
5253
5354 for _ , c := range cases {
5455 addr := addrPacked (c .leaf , c .ltd )
55- if addr != c .want {
56+ if ! bytes . Equal ( addr , c .want ) {
5657 t .Errorf ("expected: %v got: %v" , c .want , addr )
5758 }
5859 }
5960}
61+
62+ func TestLeaf2Addr (t * testing.T ) {
63+ cases := []struct {
64+ leaf []byte
65+ ltd []string
66+ packed bool
67+ want []byte
68+ }{
69+ {
70+ common .FromHex ("000000000000000000000000000000000000000000000001" ),
71+ []string {"uint32" , "address" },
72+ true ,
73+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
74+ },
75+ {
76+ common .FromHex ("000000000000000000000000000000000000000100000000" ),
77+ []string {"address" , "uint32" },
78+ true ,
79+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
80+ },
81+ {
82+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
83+ []string {"address" },
84+ false ,
85+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
86+ },
87+ {
88+ common .FromHex ("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001" ),
89+ []string {"uint32" , "address" },
90+ false ,
91+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
92+ },
93+ {
94+ common .FromHex ("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000" ),
95+ []string {"address" , "uint32" },
96+ false ,
97+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
98+ },
99+ {
100+ common .FromHex ("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001" ),
101+ []string {"uint256" , "address" },
102+ false ,
103+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
104+ },
105+ {
106+ common .FromHex ("0x0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002d" ),
107+ []string {"address" , "uint256" },
108+ false ,
109+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
110+ },
111+ {
112+ common .FromHex ("0x0000000000000000000000000000000000000000000000000000000000000001" ),
113+ []string {"address" },
114+ true ,
115+ common .FromHex ("0x0000000000000000000000000000000000000001" ),
116+ },
117+ }
118+
119+ for _ , c := range cases {
120+ addr := leaf2Addr (c .leaf , c .ltd , c .packed )
121+ if ! bytes .Equal (addr , c .want ) {
122+ t .Errorf ("expected: %v got: %v" , c .want , addr )
123+ }
124+ }
125+
126+ }
0 commit comments