github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/accounts/abi/unpack_test.go (about) 1 package abi 2 3 import ( 4 "bytes" 5 "encoding/hex" 6 "fmt" 7 "math/big" 8 "reflect" 9 "strconv" 10 "strings" 11 "testing" 12 13 "github.com/neatlab/neatio/utilities/common" 14 "github.com/stretchr/testify/require" 15 ) 16 17 type unpackTest struct { 18 def string 19 enc string 20 want interface{} 21 err string 22 } 23 24 func (test unpackTest) checkError(err error) error { 25 if err != nil { 26 if len(test.err) == 0 { 27 return fmt.Errorf("expected no err but got: %v", err) 28 } else if err.Error() != test.err { 29 return fmt.Errorf("expected err: '%v' got err: %q", test.err, err) 30 } 31 } else if len(test.err) > 0 { 32 return fmt.Errorf("expected err: %v but got none", test.err) 33 } 34 return nil 35 } 36 37 var unpackTests = []unpackTest{ 38 39 { 40 def: `[{ "type": "bool" }]`, 41 enc: "0000000000000000000000000000000000000000000000000000000000000001", 42 want: true, 43 }, 44 { 45 def: `[{ "type": "bool" }]`, 46 enc: "0000000000000000000000000000000000000000000000000000000000000000", 47 want: false, 48 }, 49 { 50 def: `[{ "type": "bool" }]`, 51 enc: "0000000000000000000000000000000000000000000000000001000000000001", 52 want: false, 53 err: "abi: improperly encoded boolean value", 54 }, 55 { 56 def: `[{ "type": "bool" }]`, 57 enc: "0000000000000000000000000000000000000000000000000000000000000003", 58 want: false, 59 err: "abi: improperly encoded boolean value", 60 }, 61 62 { 63 def: `[{"type": "uint32"}]`, 64 enc: "0000000000000000000000000000000000000000000000000000000000000001", 65 want: uint32(1), 66 }, 67 { 68 def: `[{"type": "uint32"}]`, 69 enc: "0000000000000000000000000000000000000000000000000000000000000001", 70 want: uint16(0), 71 err: "abi: cannot unmarshal uint32 in to uint16", 72 }, 73 { 74 def: `[{"type": "uint17"}]`, 75 enc: "0000000000000000000000000000000000000000000000000000000000000001", 76 want: uint16(0), 77 err: "abi: cannot unmarshal *big.Int in to uint16", 78 }, 79 { 80 def: `[{"type": "uint17"}]`, 81 enc: "0000000000000000000000000000000000000000000000000000000000000001", 82 want: big.NewInt(1), 83 }, 84 { 85 def: `[{"type": "int32"}]`, 86 enc: "0000000000000000000000000000000000000000000000000000000000000001", 87 want: int32(1), 88 }, 89 { 90 def: `[{"type": "int32"}]`, 91 enc: "0000000000000000000000000000000000000000000000000000000000000001", 92 want: int16(0), 93 err: "abi: cannot unmarshal int32 in to int16", 94 }, 95 { 96 def: `[{"type": "int17"}]`, 97 enc: "0000000000000000000000000000000000000000000000000000000000000001", 98 want: int16(0), 99 err: "abi: cannot unmarshal *big.Int in to int16", 100 }, 101 { 102 def: `[{"type": "int17"}]`, 103 enc: "0000000000000000000000000000000000000000000000000000000000000001", 104 want: big.NewInt(1), 105 }, 106 { 107 def: `[{"type": "int256"}]`, 108 enc: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 109 want: big.NewInt(-1), 110 }, 111 112 { 113 def: `[{"type": "address"}]`, 114 enc: "0000000000000000000000000100000000000000000000000000000000000000", 115 want: common.Address{1}, 116 }, 117 118 { 119 def: `[{"type": "bytes32"}]`, 120 enc: "0100000000000000000000000000000000000000000000000000000000000000", 121 want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 122 }, 123 { 124 def: `[{"type": "bytes"}]`, 125 enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000", 126 want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), 127 }, 128 { 129 def: `[{"type": "bytes"}]`, 130 enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000", 131 want: [32]byte{}, 132 err: "abi: cannot unmarshal []uint8 in to [32]uint8", 133 }, 134 { 135 def: `[{"type": "bytes32"}]`, 136 enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000", 137 want: []byte(nil), 138 err: "abi: cannot unmarshal [32]uint8 in to []uint8", 139 }, 140 { 141 def: `[{"type": "bytes32"}]`, 142 enc: "0100000000000000000000000000000000000000000000000000000000000000", 143 want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 144 }, 145 146 { 147 def: `[{"type": "function"}]`, 148 enc: "0100000000000000000000000000000000000000000000000000000000000000", 149 want: [24]byte{1}, 150 }, 151 152 { 153 def: `[{"type": "uint8[]"}]`, 154 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 155 want: []uint8{1, 2}, 156 }, 157 { 158 def: `[{"type": "uint8[]"}]`, 159 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000", 160 want: []uint8{}, 161 }, 162 { 163 def: `[{"type": "uint256[]"}]`, 164 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000", 165 want: []*big.Int{}, 166 }, 167 { 168 def: `[{"type": "uint8[2]"}]`, 169 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 170 want: [2]uint8{1, 2}, 171 }, 172 173 { 174 def: `[{"type": "uint8[][]"}]`, 175 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000", 176 want: [][]uint8{}, 177 }, 178 { 179 def: `[{"type": "uint8[][]"}]`, 180 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 181 want: [][]uint8{{1, 2}, {1, 2}}, 182 }, 183 { 184 def: `[{"type": "uint8[][]"}]`, 185 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", 186 want: [][]uint8{{1, 2}, {1, 2, 3}}, 187 }, 188 { 189 def: `[{"type": "uint8[2][2]"}]`, 190 enc: "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 191 want: [2][2]uint8{{1, 2}, {1, 2}}, 192 }, 193 { 194 def: `[{"type": "uint8[][2]"}]`, 195 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 196 want: [2][]uint8{{}, {}}, 197 }, 198 { 199 def: `[{"type": "uint8[][2]"}]`, 200 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001", 201 want: [2][]uint8{{1}, {1}}, 202 }, 203 { 204 def: `[{"type": "uint8[2][]"}]`, 205 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000", 206 want: [][2]uint8{}, 207 }, 208 { 209 def: `[{"type": "uint8[2][]"}]`, 210 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 211 want: [][2]uint8{{1, 2}}, 212 }, 213 { 214 def: `[{"type": "uint8[2][]"}]`, 215 enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 216 want: [][2]uint8{{1, 2}, {1, 2}}, 217 }, 218 { 219 def: `[{"type": "uint16[]"}]`, 220 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 221 want: []uint16{1, 2}, 222 }, 223 { 224 def: `[{"type": "uint16[2]"}]`, 225 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 226 want: [2]uint16{1, 2}, 227 }, 228 { 229 def: `[{"type": "uint32[]"}]`, 230 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 231 want: []uint32{1, 2}, 232 }, 233 { 234 def: `[{"type": "uint32[2]"}]`, 235 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 236 want: [2]uint32{1, 2}, 237 }, 238 { 239 def: `[{"type": "uint32[2][3][4]"}]`, 240 enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018", 241 want: [4][3][2]uint32{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}, {{13, 14}, {15, 16}, {17, 18}}, {{19, 20}, {21, 22}, {23, 24}}}, 242 }, 243 { 244 def: `[{"type": "uint64[]"}]`, 245 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 246 want: []uint64{1, 2}, 247 }, 248 { 249 def: `[{"type": "uint64[2]"}]`, 250 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 251 want: [2]uint64{1, 2}, 252 }, 253 { 254 def: `[{"type": "uint256[]"}]`, 255 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 256 want: []*big.Int{big.NewInt(1), big.NewInt(2)}, 257 }, 258 { 259 def: `[{"type": "uint256[3]"}]`, 260 enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", 261 want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, 262 }, 263 { 264 def: `[{"type": "string[4]"}]`, 265 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b476f2d657468657265756d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000", 266 want: [4]string{"Hello", "World", "Go-ethereum", "Ethereum"}, 267 }, 268 { 269 def: `[{"type": "string[]"}]`, 270 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b676f2d657468657265756d000000000000000000000000000000000000000000", 271 want: []string{"Ethereum", "go-ethereum"}, 272 }, 273 { 274 def: `[{"type": "bytes[]"}]`, 275 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000003f0f0f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003f0f0f00000000000000000000000000000000000000000000000000000000000", 276 want: [][]byte{{0xf0, 0xf0, 0xf0}, {0xf0, 0xf0, 0xf0}}, 277 }, 278 { 279 def: `[{"type": "uint256[2][][]"}]`, 280 enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c8000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000003e80000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c8000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000003e8", 281 want: [][][2]*big.Int{{{big.NewInt(1), big.NewInt(200)}, {big.NewInt(1), big.NewInt(1000)}}, {{big.NewInt(1), big.NewInt(200)}, {big.NewInt(1), big.NewInt(1000)}}}, 282 }, 283 { 284 def: `[{"type": "int8[]"}]`, 285 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 286 want: []int8{1, 2}, 287 }, 288 { 289 def: `[{"type": "int8[2]"}]`, 290 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 291 want: [2]int8{1, 2}, 292 }, 293 { 294 def: `[{"type": "int16[]"}]`, 295 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 296 want: []int16{1, 2}, 297 }, 298 { 299 def: `[{"type": "int16[2]"}]`, 300 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 301 want: [2]int16{1, 2}, 302 }, 303 { 304 def: `[{"type": "int32[]"}]`, 305 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 306 want: []int32{1, 2}, 307 }, 308 { 309 def: `[{"type": "int32[2]"}]`, 310 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 311 want: [2]int32{1, 2}, 312 }, 313 { 314 def: `[{"type": "int64[]"}]`, 315 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 316 want: []int64{1, 2}, 317 }, 318 { 319 def: `[{"type": "int64[2]"}]`, 320 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 321 want: [2]int64{1, 2}, 322 }, 323 { 324 def: `[{"type": "int256[]"}]`, 325 enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 326 want: []*big.Int{big.NewInt(1), big.NewInt(2)}, 327 }, 328 { 329 def: `[{"type": "int256[3]"}]`, 330 enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", 331 want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, 332 }, 333 334 { 335 def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`, 336 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 337 want: struct { 338 Int1 *big.Int 339 Int2 *big.Int 340 }{big.NewInt(1), big.NewInt(2)}, 341 }, 342 { 343 def: `[{"name":"int_one","type":"int256"}]`, 344 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 345 want: struct { 346 IntOne *big.Int 347 }{big.NewInt(1)}, 348 }, 349 { 350 def: `[{"name":"int__one","type":"int256"}]`, 351 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 352 want: struct { 353 IntOne *big.Int 354 }{big.NewInt(1)}, 355 }, 356 { 357 def: `[{"name":"int_one_","type":"int256"}]`, 358 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 359 want: struct { 360 IntOne *big.Int 361 }{big.NewInt(1)}, 362 }, 363 { 364 def: `[{"name":"int_one","type":"int256"}, {"name":"intone","type":"int256"}]`, 365 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 366 want: struct { 367 IntOne *big.Int 368 Intone *big.Int 369 }{big.NewInt(1), big.NewInt(2)}, 370 }, 371 { 372 def: `[{"name":"___","type":"int256"}]`, 373 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 374 want: struct { 375 IntOne *big.Int 376 Intone *big.Int 377 }{}, 378 err: "abi: purely underscored output cannot unpack to struct", 379 }, 380 { 381 def: `[{"name":"int_one","type":"int256"},{"name":"IntOne","type":"int256"}]`, 382 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 383 want: struct { 384 Int1 *big.Int 385 Int2 *big.Int 386 }{}, 387 err: "abi: multiple outputs mapping to the same struct field 'IntOne'", 388 }, 389 { 390 def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`, 391 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 392 want: struct { 393 Int1 *big.Int 394 Int2 *big.Int 395 }{}, 396 err: "abi: multiple outputs mapping to the same struct field 'Int'", 397 }, 398 { 399 def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`, 400 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 401 want: struct { 402 Int1 *big.Int 403 Int2 *big.Int 404 }{}, 405 err: "abi: multiple outputs mapping to the same struct field 'Int'", 406 }, 407 { 408 def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`, 409 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 410 want: struct { 411 Int1 *big.Int 412 Int2 *big.Int 413 }{}, 414 err: "abi: multiple outputs mapping to the same struct field 'Int'", 415 }, 416 { 417 def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`, 418 enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", 419 want: struct { 420 Int1 *big.Int 421 Int2 *big.Int 422 }{}, 423 err: "abi: purely underscored output cannot unpack to struct", 424 }, 425 } 426 427 func TestUnpack(t *testing.T) { 428 for i, test := range unpackTests { 429 t.Run(strconv.Itoa(i), func(t *testing.T) { 430 def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) 431 abi, err := JSON(strings.NewReader(def)) 432 if err != nil { 433 t.Fatalf("invalid ABI definition %s: %v", def, err) 434 } 435 encb, err := hex.DecodeString(test.enc) 436 if err != nil { 437 t.Fatalf("invalid hex %s: %v", test.enc, err) 438 } 439 outptr := reflect.New(reflect.TypeOf(test.want)) 440 err = abi.Unpack(outptr.Interface(), "method", encb) 441 if err := test.checkError(err); err != nil { 442 t.Errorf("test %d (%v) failed: %v", i, test.def, err) 443 return 444 } 445 out := outptr.Elem().Interface() 446 if !reflect.DeepEqual(test.want, out) { 447 t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out) 448 } 449 }) 450 } 451 } 452 453 func TestUnpackSetDynamicArrayOutput(t *testing.T) { 454 abi, err := JSON(strings.NewReader(`[{"constant":true,"inputs":[],"name":"testDynamicFixedBytes15","outputs":[{"name":"","type":"bytes15[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"testDynamicFixedBytes32","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"}]`)) 455 if err != nil { 456 t.Fatal(err) 457 } 458 459 var ( 460 marshalledReturn32 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783132333435363738393000000000000000000000000000000000000000003078303938373635343332310000000000000000000000000000000000000000") 461 marshalledReturn15 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783031323334350000000000000000000000000000000000000000000000003078393837363534000000000000000000000000000000000000000000000000") 462 463 out32 [][32]byte 464 out15 [][15]byte 465 ) 466 467 err = abi.Unpack(&out32, "testDynamicFixedBytes32", marshalledReturn32) 468 if err != nil { 469 t.Fatal(err) 470 } 471 if len(out32) != 2 { 472 t.Fatalf("expected array with 2 values, got %d", len(out32)) 473 } 474 expected := common.Hex2Bytes("3078313233343536373839300000000000000000000000000000000000000000") 475 if !bytes.Equal(out32[0][:], expected) { 476 t.Errorf("expected %x, got %x\n", expected, out32[0]) 477 } 478 expected = common.Hex2Bytes("3078303938373635343332310000000000000000000000000000000000000000") 479 if !bytes.Equal(out32[1][:], expected) { 480 t.Errorf("expected %x, got %x\n", expected, out32[1]) 481 } 482 483 err = abi.Unpack(&out15, "testDynamicFixedBytes32", marshalledReturn15) 484 if err != nil { 485 t.Fatal(err) 486 } 487 if len(out15) != 2 { 488 t.Fatalf("expected array with 2 values, got %d", len(out15)) 489 } 490 expected = common.Hex2Bytes("307830313233343500000000000000") 491 if !bytes.Equal(out15[0][:], expected) { 492 t.Errorf("expected %x, got %x\n", expected, out15[0]) 493 } 494 expected = common.Hex2Bytes("307839383736353400000000000000") 495 if !bytes.Equal(out15[1][:], expected) { 496 t.Errorf("expected %x, got %x\n", expected, out15[1]) 497 } 498 } 499 500 type methodMultiOutput struct { 501 Int *big.Int 502 String string 503 } 504 505 func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) { 506 const definition = `[ 507 { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` 508 var expected = methodMultiOutput{big.NewInt(1), "hello"} 509 510 abi, err := JSON(strings.NewReader(definition)) 511 require.NoError(err) 512 513 buff := new(bytes.Buffer) 514 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 515 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 516 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 517 buff.Write(common.RightPadBytes([]byte(expected.String), 32)) 518 return abi, buff.Bytes(), expected 519 } 520 521 func TestMethodMultiReturn(t *testing.T) { 522 type reversed struct { 523 String string 524 Int *big.Int 525 } 526 527 newInterfaceSlice := func(len int) interface{} { 528 slice := make([]interface{}, len) 529 return &slice 530 } 531 532 abi, data, expected := methodMultiReturn(require.New(t)) 533 bigint := new(big.Int) 534 var testCases = []struct { 535 dest interface{} 536 expected interface{} 537 error string 538 name string 539 }{{ 540 &methodMultiOutput{}, 541 &expected, 542 "", 543 "Can unpack into structure", 544 }, { 545 &reversed{}, 546 &reversed{expected.String, expected.Int}, 547 "", 548 "Can unpack into reversed structure", 549 }, { 550 &[]interface{}{&bigint, new(string)}, 551 &[]interface{}{&expected.Int, &expected.String}, 552 "", 553 "Can unpack into a slice", 554 }, { 555 &[2]interface{}{&bigint, new(string)}, 556 &[2]interface{}{&expected.Int, &expected.String}, 557 "", 558 "Can unpack into an array", 559 }, { 560 &[2]interface{}{}, 561 &[2]interface{}{expected.Int, expected.String}, 562 "", 563 "Can unpack into interface array", 564 }, { 565 newInterfaceSlice(2), 566 &[]interface{}{expected.Int, expected.String}, 567 "", 568 "Can unpack into interface slice", 569 }, { 570 &[]interface{}{new(int), new(int)}, 571 &[]interface{}{&expected.Int, &expected.String}, 572 "abi: cannot unmarshal *big.Int in to int", 573 "Can not unpack into a slice with wrong types", 574 }, { 575 &[]interface{}{new(int)}, 576 &[]interface{}{}, 577 "abi: insufficient number of elements in the list/array for unpack, want 2, got 1", 578 "Can not unpack into a slice with wrong types", 579 }} 580 for _, tc := range testCases { 581 tc := tc 582 t.Run(tc.name, func(t *testing.T) { 583 require := require.New(t) 584 err := abi.Unpack(tc.dest, "multi", data) 585 if tc.error == "" { 586 require.Nil(err, "Should be able to unpack method outputs.") 587 require.Equal(tc.expected, tc.dest) 588 } else { 589 require.EqualError(err, tc.error) 590 } 591 }) 592 } 593 } 594 595 func TestMultiReturnWithArray(t *testing.T) { 596 const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]` 597 abi, err := JSON(strings.NewReader(definition)) 598 if err != nil { 599 t.Fatal(err) 600 } 601 buff := new(bytes.Buffer) 602 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009")) 603 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) 604 605 ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9} 606 ret2, ret2Exp := new(uint64), uint64(8) 607 if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { 608 t.Fatal(err) 609 } 610 if !reflect.DeepEqual(*ret1, ret1Exp) { 611 t.Error("array result", *ret1, "!= Expected", ret1Exp) 612 } 613 if *ret2 != ret2Exp { 614 t.Error("int result", *ret2, "!= Expected", ret2Exp) 615 } 616 } 617 618 func TestMultiReturnWithStringArray(t *testing.T) { 619 const definition = `[{"name" : "multi", "outputs": [{"name": "","type": "uint256[3]"},{"name": "","type": "address"},{"name": "","type": "string[2]"},{"name": "","type": "bool"}]}]` 620 abi, err := JSON(strings.NewReader(definition)) 621 if err != nil { 622 t.Fatal(err) 623 } 624 buff := new(bytes.Buffer) 625 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000")) 626 temp, _ := big.NewInt(0).SetString("30000000000000000000", 10) 627 ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp} 628 ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f") 629 ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"} 630 ret4, ret4Exp := new(bool), false 631 if err := abi.Unpack(&[]interface{}{ret1, ret2, ret3, ret4}, "multi", buff.Bytes()); err != nil { 632 t.Fatal(err) 633 } 634 if !reflect.DeepEqual(*ret1, ret1Exp) { 635 t.Error("big.Int array result", *ret1, "!= Expected", ret1Exp) 636 } 637 if !reflect.DeepEqual(*ret2, ret2Exp) { 638 t.Error("address result", *ret2, "!= Expected", ret2Exp) 639 } 640 if !reflect.DeepEqual(*ret3, ret3Exp) { 641 t.Error("string array result", *ret3, "!= Expected", ret3Exp) 642 } 643 if !reflect.DeepEqual(*ret4, ret4Exp) { 644 t.Error("bool result", *ret4, "!= Expected", ret4Exp) 645 } 646 } 647 648 func TestMultiReturnWithStringSlice(t *testing.T) { 649 const definition = `[{"name" : "multi", "outputs": [{"name": "","type": "string[]"},{"name": "","type": "uint256[]"}]}]` 650 abi, err := JSON(strings.NewReader(definition)) 651 if err != nil { 652 t.Fatal(err) 653 } 654 buff := new(bytes.Buffer) 655 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 656 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000120")) 657 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 658 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 659 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) 660 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) 661 buff.Write(common.Hex2Bytes("657468657265756d000000000000000000000000000000000000000000000000")) 662 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000b")) 663 buff.Write(common.Hex2Bytes("676f2d657468657265756d000000000000000000000000000000000000000000")) 664 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 665 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000064")) 666 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000065")) 667 ret1, ret1Exp := new([]string), []string{"ethereum", "go-ethereum"} 668 ret2, ret2Exp := new([]*big.Int), []*big.Int{big.NewInt(100), big.NewInt(101)} 669 if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { 670 t.Fatal(err) 671 } 672 if !reflect.DeepEqual(*ret1, ret1Exp) { 673 t.Error("string slice result", *ret1, "!= Expected", ret1Exp) 674 } 675 if !reflect.DeepEqual(*ret2, ret2Exp) { 676 t.Error("uint256 slice result", *ret2, "!= Expected", ret2Exp) 677 } 678 } 679 680 func TestMultiReturnWithDeeplyNestedArray(t *testing.T) { 681 682 const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]` 683 abi, err := JSON(strings.NewReader(definition)) 684 if err != nil { 685 t.Fatal(err) 686 } 687 buff := new(bytes.Buffer) 688 689 buff.Write(common.Hex2Bytes(strings.Join([]string{ 690 "", 691 "111", "112", "113", "121", "122", "123", 692 "211", "212", "213", "221", "222", "223", 693 "311", "312", "313", "321", "322", "323", 694 "411", "412", "413", "421", "422", "423", 695 }, "0000000000000000000000000000000000000000000000000000000000000"))) 696 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876")) 697 698 ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{ 699 {{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}}, 700 {{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}}, 701 {{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}}, 702 {{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}}, 703 } 704 ret2, ret2Exp := new(uint64), uint64(0x9876) 705 if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { 706 t.Fatal(err) 707 } 708 if !reflect.DeepEqual(*ret1, ret1Exp) { 709 t.Error("array result", *ret1, "!= Expected", ret1Exp) 710 } 711 if *ret2 != ret2Exp { 712 t.Error("int result", *ret2, "!= Expected", ret2Exp) 713 } 714 } 715 716 func TestUnmarshal(t *testing.T) { 717 const definition = `[ 718 { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] }, 719 { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] }, 720 { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] }, 721 { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, 722 { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] }, 723 { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] }, 724 { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] }, 725 { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] }, 726 { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]` 727 728 abi, err := JSON(strings.NewReader(definition)) 729 if err != nil { 730 t.Fatal(err) 731 } 732 buff := new(bytes.Buffer) 733 734 p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000") 735 p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff") 736 mixedBytes := []interface{}{&p0, &p1} 737 738 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 739 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")) 740 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a")) 741 buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000")) 742 743 err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes()) 744 if err != nil { 745 t.Error(err) 746 } else { 747 if !bytes.Equal(p0, p0Exp) { 748 t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0) 749 } 750 751 if !bytes.Equal(p1[:], p1Exp) { 752 t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1) 753 } 754 } 755 756 var Int *big.Int 757 err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 758 if err != nil { 759 t.Error(err) 760 } 761 762 if Int == nil || Int.Cmp(big.NewInt(1)) != 0 { 763 t.Error("expected Int to be 1 got", Int) 764 } 765 766 var Bool bool 767 err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 768 if err != nil { 769 t.Error(err) 770 } 771 772 if !Bool { 773 t.Error("expected Bool to be true") 774 } 775 776 buff.Reset() 777 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 778 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 779 bytesOut := common.RightPadBytes([]byte("hello"), 32) 780 buff.Write(bytesOut) 781 782 var Bytes []byte 783 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 784 if err != nil { 785 t.Error(err) 786 } 787 788 if !bytes.Equal(Bytes, bytesOut) { 789 t.Errorf("expected %x got %x", bytesOut, Bytes) 790 } 791 792 buff.Reset() 793 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 794 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 795 bytesOut = common.RightPadBytes([]byte("hello"), 64) 796 buff.Write(bytesOut) 797 798 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 799 if err != nil { 800 t.Error(err) 801 } 802 803 if !bytes.Equal(Bytes, bytesOut) { 804 t.Errorf("expected %x got %x", bytesOut, Bytes) 805 } 806 807 buff.Reset() 808 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 809 buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f")) 810 bytesOut = common.RightPadBytes([]byte("hello"), 64) 811 buff.Write(bytesOut) 812 813 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 814 if err != nil { 815 t.Error(err) 816 } 817 818 if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) { 819 t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes) 820 } 821 822 err = abi.Unpack(&Bytes, "bytes", nil) 823 if err == nil { 824 t.Error("expected error") 825 } 826 827 buff.Reset() 828 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 829 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) 830 buff.Write(common.RightPadBytes([]byte("hello"), 32)) 831 832 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 833 if err != nil { 834 t.Error(err) 835 } 836 837 if !bytes.Equal(Bytes, []byte("hello")) { 838 t.Errorf("expected %x got %x", bytesOut, Bytes) 839 } 840 841 buff.Reset() 842 buff.Write(common.RightPadBytes([]byte("hello"), 32)) 843 844 var hash common.Hash 845 err = abi.Unpack(&hash, "fixed", buff.Bytes()) 846 if err != nil { 847 t.Error(err) 848 } 849 850 helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32)) 851 if hash != helloHash { 852 t.Errorf("Expected %x to equal %x", hash, helloHash) 853 } 854 855 buff.Reset() 856 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 857 err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) 858 if err == nil { 859 t.Error("expected error") 860 } 861 862 err = abi.Unpack(&Bytes, "multi", make([]byte, 64)) 863 if err == nil { 864 t.Error("expected error") 865 } 866 867 buff.Reset() 868 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 869 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 870 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003")) 871 872 var intArray [3]*big.Int 873 err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes()) 874 if err != nil { 875 t.Error(err) 876 } 877 var testAgainstIntArray [3]*big.Int 878 testAgainstIntArray[0] = big.NewInt(1) 879 testAgainstIntArray[1] = big.NewInt(2) 880 testAgainstIntArray[2] = big.NewInt(3) 881 882 for i, Int := range intArray { 883 if Int.Cmp(testAgainstIntArray[i]) != 0 { 884 t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int) 885 } 886 } 887 888 buff.Reset() 889 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) 890 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 891 buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) 892 893 var outAddr []common.Address 894 err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) 895 if err != nil { 896 t.Fatal("didn't expect error:", err) 897 } 898 899 if len(outAddr) != 1 { 900 t.Fatal("expected 1 item, got", len(outAddr)) 901 } 902 903 if outAddr[0] != (common.Address{1}) { 904 t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0]) 905 } 906 907 buff.Reset() 908 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) 909 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) 910 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 911 buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) 912 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 913 buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000")) 914 buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000")) 915 916 var outAddrStruct struct { 917 A []common.Address 918 B []common.Address 919 } 920 err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes()) 921 if err != nil { 922 t.Fatal("didn't expect error:", err) 923 } 924 925 if len(outAddrStruct.A) != 1 { 926 t.Fatal("expected 1 item, got", len(outAddrStruct.A)) 927 } 928 929 if outAddrStruct.A[0] != (common.Address{1}) { 930 t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0]) 931 } 932 933 if len(outAddrStruct.B) != 2 { 934 t.Fatal("expected 1 item, got", len(outAddrStruct.B)) 935 } 936 937 if outAddrStruct.B[0] != (common.Address{2}) { 938 t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0]) 939 } 940 if outAddrStruct.B[1] != (common.Address{3}) { 941 t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1]) 942 } 943 944 buff.Reset() 945 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100")) 946 947 err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) 948 if err == nil { 949 t.Fatal("expected error:", err) 950 } 951 } 952 953 func TestUnpackTuple(t *testing.T) { 954 const simpleTuple = `[{"name":"tuple","constant":false,"outputs":[{"type":"tuple","name":"ret","components":[{"type":"int256","name":"a"},{"type":"int256","name":"b"}]}]}]` 955 abi, err := JSON(strings.NewReader(simpleTuple)) 956 if err != nil { 957 t.Fatal(err) 958 } 959 buff := new(bytes.Buffer) 960 961 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 962 buff.Write(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) 963 964 v := struct { 965 A *big.Int 966 B *big.Int 967 }{new(big.Int), new(big.Int)} 968 969 err = abi.Unpack(&v, "tuple", buff.Bytes()) 970 if err != nil { 971 t.Error(err) 972 } else { 973 if v.A.Cmp(big.NewInt(1)) != 0 { 974 t.Errorf("unexpected value unpacked: want %x, got %x", 1, v.A) 975 } 976 if v.B.Cmp(big.NewInt(-1)) != 0 { 977 t.Errorf("unexpected value unpacked: want %x, got %x", v.B, -1) 978 } 979 } 980 981 const nestedTuple = `[{"name":"tuple","constant":false,"outputs":[ 982 {"type":"tuple","name":"s","components":[{"type":"uint256","name":"a"},{"type":"uint256[]","name":"b"},{"type":"tuple[]","name":"c","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]}]}, 983 {"type":"tuple","name":"t","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]}, 984 {"type":"uint256","name":"a"} 985 ]}]` 986 987 abi, err = JSON(strings.NewReader(nestedTuple)) 988 if err != nil { 989 t.Fatal(err) 990 } 991 buff.Reset() 992 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) 993 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")) 994 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 995 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 996 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 997 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000060")) 998 buff.Write(common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000c0")) 999 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 1000 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 1001 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 1002 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 1003 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 1004 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 1005 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) 1006 buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) 1007 1008 type T struct { 1009 X *big.Int `abi:"x"` 1010 Z *big.Int `abi:"y"` 1011 } 1012 1013 type S struct { 1014 A *big.Int 1015 B []*big.Int 1016 C []T 1017 } 1018 1019 type Ret struct { 1020 FieldS S `abi:"s"` 1021 FieldT T `abi:"t"` 1022 A *big.Int 1023 } 1024 var ret Ret 1025 var expected = Ret{ 1026 FieldS: S{ 1027 A: big.NewInt(1), 1028 B: []*big.Int{big.NewInt(1), big.NewInt(2)}, 1029 C: []T{ 1030 {big.NewInt(1), big.NewInt(2)}, 1031 {big.NewInt(2), big.NewInt(1)}, 1032 }, 1033 }, 1034 FieldT: T{ 1035 big.NewInt(0), big.NewInt(1), 1036 }, 1037 A: big.NewInt(1), 1038 } 1039 1040 err = abi.Unpack(&ret, "tuple", buff.Bytes()) 1041 if err != nil { 1042 t.Error(err) 1043 } 1044 if reflect.DeepEqual(ret, expected) { 1045 t.Error("unexpected unpack value") 1046 } 1047 } 1048 1049 func TestOOMMaliciousInput(t *testing.T) { 1050 oomTests := []unpackTest{ 1051 { 1052 def: `[{"type": "uint8[]"}]`, 1053 enc: "0000000000000000000000000000000000000000000000000000000000000020" + 1054 "0000000000000000000000000000000000000000000000000000000000000003" + 1055 "0000000000000000000000000000000000000000000000000000000000000001" + 1056 "0000000000000000000000000000000000000000000000000000000000000002", 1057 }, 1058 { 1059 def: `[{"type": "uint8[]"}]`, 1060 enc: "0000000000000000000000000000000000000000000000000000000000000020" + 1061 "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + 1062 "0000000000000000000000000000000000000000000000000000000000000001" + 1063 "0000000000000000000000000000000000000000000000000000000000000002", 1064 }, 1065 { 1066 def: `[{"type": "uint8[]"}]`, 1067 enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + 1068 "0000000000000000000000000000000000000000000000000000000000000002" + 1069 "0000000000000000000000000000000000000000000000000000000000000001" + 1070 "0000000000000000000000000000000000000000000000000000000000000002", 1071 }, 1072 { 1073 def: `[{"type": "uint8[]"}]`, 1074 enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + 1075 "0000000000000000000000000000000000000000000000000000000000000002" + 1076 "0000000000000000000000000000000000000000000000000000000000000001" + 1077 "0000000000000000000000000000000000000000000000000000000000000002", 1078 }, 1079 { 1080 def: `[{"type": "uint8[]"}]`, 1081 enc: "000000000000000000000000000000000000000000000000f000000000000020" + 1082 "0000000000000000000000000000000000000000000000000000000000000002" + 1083 "0000000000000000000000000000000000000000000000000000000000000001" + 1084 "0000000000000000000000000000000000000000000000000000000000000002", 1085 }, 1086 1087 { 1088 def: `[{"type": "uint8[]"}]`, 1089 enc: "0000000000000000000000000000000000000000000000000000000000000020" + 1090 "000000000000000000000000000000000000000000000000f000000000000002" + 1091 "0000000000000000000000000000000000000000000000000000000000000001" + 1092 "0000000000000000000000000000000000000000000000000000000000000002", 1093 }, 1094 { 1095 def: `[{"type": "uint8[]"}]`, 1096 enc: "0000000000000000000000000000000000000000000000000000000000000020" + 1097 "0000000000000000000000000000000000000000000000007fffffffff000002" + 1098 "0000000000000000000000000000000000000000000000000000000000000001" + 1099 "0000000000000000000000000000000000000000000000000000000000000002", 1100 }, 1101 } 1102 for i, test := range oomTests { 1103 def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) 1104 abi, err := JSON(strings.NewReader(def)) 1105 if err != nil { 1106 t.Fatalf("invalid ABI definition %s: %v", def, err) 1107 } 1108 encb, err := hex.DecodeString(test.enc) 1109 if err != nil { 1110 t.Fatalf("invalid hex: %s" + test.enc) 1111 } 1112 _, err = abi.Methods["method"].Outputs.UnpackValues(encb) 1113 if err == nil { 1114 t.Fatalf("Expected error on malicious input, test %d", i) 1115 } 1116 } 1117 }