Skip to content

Commit 027d1bd

Browse files
committed
Merge conflicts
2 parents 5ca52a6 + 878b71a commit 027d1bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1538
-273
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 0.1.4 (Unreleased)
22

3+
- feat: Add override to `eth_call` request [[GH-240](https://github.com/umbracle/ethgo/issues/240)]
4+
- fix: Recovery of typed transactions [[GH-238](https://github.com/umbracle/ethgo/issues/238)]
35
- fix: Parse `nonce` and `mixHash` on `Block` [[GH-228](https://github.com/umbracle/ethgo/issues/228)]
46
- feat: `abi` decodes function string in multilines [[GH-212](https://github.com/umbracle/ethgo/issues/212)]
57
- feat: `abi` DecodeStruct uses the `abi` tag instead of the default `mapstructure` [[GH-211](https://github.com/umbracle/ethgo/issues/211)]

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11

22
# Eth-Go
33

4+
[![Chat Badge]][chat link]
5+
6+
[chat badge]: https://img.shields.io/badge/chat-discord-%237289da
7+
[chat link]: https://discord.gg/5A6Qm2u4yK
8+
49
Ethgo is a lightweight SDK in Go to interact with Ethereum compatible blockchains.
510

6-
- Website: https://ethgo.dev
11+
- Website: https://www.ethgoproject.io
712

813
Ethgo provides the next key features:
914

abi/abi.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,11 @@ func buildSignature(name string, typ *Type) string {
404404

405405
// ArgumentStr encodes a type object
406406
type ArgumentStr struct {
407-
Name string
408-
Type string
409-
Indexed bool
410-
Components []*ArgumentStr
407+
Name string
408+
Type string
409+
Indexed bool
410+
Components []*ArgumentStr
411+
InternalType string
411412
}
412413

413414
var keccakPool = sync.Pool{

abi/abi_test.go

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package abi
22

33
import (
4-
"fmt"
54
"reflect"
65
"testing"
76

87
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
99
)
1010

1111
func TestAbi(t *testing.T) {
@@ -106,14 +106,53 @@ func TestAbi(t *testing.T) {
106106
}
107107

108108
if !reflect.DeepEqual(abi, c.Output) {
109-
fmt.Println(reflect.DeepEqual(abi.Methods["balanceOf"].Outputs, c.Output.Methods["balanceOf"].Outputs))
110109
t.Fatal("bad")
111110
}
112-
113111
})
114112
}
115113
}
116114

115+
func TestAbi_InternalType(t *testing.T) {
116+
const abiStr = `[
117+
{
118+
"inputs": [
119+
{
120+
"components": [
121+
{
122+
"internalType": "address",
123+
"type": "address"
124+
},
125+
{
126+
"internalType": "uint256[4]",
127+
"type": "uint256[4]"
128+
}
129+
],
130+
"internalType": "struct X",
131+
"name": "newSet",
132+
"type": "tuple[]"
133+
},
134+
{
135+
"internalType": "custom_address",
136+
"name": "_to",
137+
"type": "address"
138+
}
139+
],
140+
"outputs": [],
141+
"name": "transfer",
142+
"type": "function"
143+
}
144+
]`
145+
146+
abi, err := NewABI(abiStr)
147+
require.NoError(t, err)
148+
149+
typ := abi.GetMethod("transfer").Inputs
150+
require.Equal(t, typ.tuple[0].Elem.InternalType(), "struct X")
151+
require.Equal(t, typ.tuple[0].Elem.elem.tuple[0].Elem.InternalType(), "address")
152+
require.Equal(t, typ.tuple[0].Elem.elem.tuple[1].Elem.InternalType(), "uint256[4]")
153+
require.Equal(t, typ.tuple[1].Elem.InternalType(), "custom_address")
154+
}
155+
117156
func TestAbi_Polymorphism(t *testing.T) {
118157
// This ABI contains 2 "transfer" functions (polymorphism)
119158
const polymorphicABI = `[
@@ -208,49 +247,49 @@ func TestAbi_HumanReadable(t *testing.T) {
208247
Inputs: MustNewType("tuple(string symbol, string name)"),
209248
},
210249
Methods: map[string]*Method{
211-
"transferFrom": &Method{
250+
"transferFrom": {
212251
Name: "transferFrom",
213252
Inputs: MustNewType("tuple(address from, address to, uint256 value)"),
214253
Outputs: MustNewType("tuple()"),
215254
},
216-
"balanceOf": &Method{
255+
"balanceOf": {
217256
Name: "balanceOf",
218257
Inputs: MustNewType("tuple(address owner)"),
219258
Outputs: MustNewType("tuple(uint256 balance)"),
220259
},
221-
"balanceOf0": &Method{
260+
"balanceOf0": {
222261
Name: "balanceOf",
223262
Inputs: MustNewType("tuple()"),
224263
Outputs: MustNewType("tuple()"),
225264
},
226-
"addPerson": &Method{
265+
"addPerson": {
227266
Name: "addPerson",
228267
Inputs: MustNewType("tuple(tuple(string name, uint16 age) person)"),
229268
Outputs: MustNewType("tuple()"),
230269
},
231-
"addPeople": &Method{
270+
"addPeople": {
232271
Name: "addPeople",
233272
Inputs: MustNewType("tuple(tuple(string name, uint16 age)[] person)"),
234273
Outputs: MustNewType("tuple()"),
235274
},
236-
"getPerson": &Method{
275+
"getPerson": {
237276
Name: "getPerson",
238277
Inputs: MustNewType("tuple(uint256 id)"),
239278
Outputs: MustNewType("tuple(tuple(string name, uint16 age))"),
240279
},
241280
},
242281
Events: map[string]*Event{
243-
"Transfer": &Event{
282+
"Transfer": {
244283
Name: "Transfer",
245284
Inputs: MustNewType("tuple(address indexed from, address indexed to, address value)"),
246285
},
247-
"PersonAdded": &Event{
286+
"PersonAdded": {
248287
Name: "PersonAdded",
249288
Inputs: MustNewType("tuple(uint256 indexed id, tuple(string name, uint16 age) person)"),
250289
},
251290
},
252291
Errors: map[string]*Error{
253-
"InsufficientBalance": &Error{
292+
"InsufficientBalance": {
254293
Name: "InsufficientBalance",
255294
Inputs: MustNewType("tuple(address owner, uint256 balance)"),
256295
},

abi/decode.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,10 @@ func readLength(data []byte) (int, error) {
296296
return 0, fmt.Errorf("length larger than int64: %v", lengthBig.Int64())
297297
}
298298
length := int(lengthBig.Uint64())
299-
if length > len(data) {
299+
300+
// if we trim the length in the data there should be enough
301+
// bytes to cover the length
302+
if length > len(data)-32 {
300303
return 0, fmt.Errorf("length insufficient %v require %v", len(data), length)
301304
}
302305
return length, nil

abi/decode_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
package abi
22

3-
import "testing"
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
48

59
func TestDecode_BytesBound(t *testing.T) {
610
typ := MustNewType("tuple(string)")
711
decodeTuple(typ, nil) // it should not panic
812
}
13+
14+
func TestDecode_DynamicLengthOutOfBounds(t *testing.T) {
15+
input := []byte("00000000000000000000000000000000\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 00000000000000000000000000")
16+
typ := MustNewType("tuple(bytes32, bytes, bytes)")
17+
18+
_, err := Decode(typ, input)
19+
require.Error(t, err)
20+
}

abi/encoding_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,10 +621,35 @@ func TestRandomEncoding(t *testing.T) {
621621
if err := testEncodeDecode(t, server, tt, input); err != nil {
622622
t.Fatal(err)
623623
}
624+
625+
if err := testDecodePanic(tt, input); err != nil {
626+
t.Fatal(err)
627+
}
624628
})
625629
}
626630
}
627631

632+
func testDecodePanic(tt *Type, input interface{}) error {
633+
// test that the encoded input and random permutattions of the response do not cause
634+
// panics on Decode function
635+
res1, err := Encode(input, tt)
636+
if err != nil {
637+
return err
638+
}
639+
640+
buf := make([]byte, len(res1))
641+
642+
// change each bit of the input with 1
643+
for i := 0; i < len(res1); i++ {
644+
copy(buf, res1)
645+
buf[i] = 0xff
646+
647+
Decode(tt, buf)
648+
}
649+
650+
return nil
651+
}
652+
628653
func testTypeWithContract(t *testing.T, server *testutil.TestServer, typ *Type) error {
629654
g := &generateContractImpl{}
630655
source := g.run(typ)

abi/type.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type Type struct {
104104
elem *Type
105105
tuple []*TupleElem
106106
t reflect.Type
107+
itype string
107108
}
108109

109110
func NewTupleType(inputs []*TupleElem) *Type {
@@ -145,6 +146,11 @@ func (t *Type) DecodeStruct(input []byte, out interface{}) error {
145146
return DecodeStruct(t, input, out)
146147
}
147148

149+
// InternalType returns the internal type
150+
func (t *Type) InternalType() string {
151+
return t.itype
152+
}
153+
148154
// Encode encodes an object using this type
149155
func (t *Type) Encode(v interface{}) ([]byte, error) {
150156
return Encode(v, t)
@@ -280,7 +286,50 @@ func NewTypeFromArgument(arg *ArgumentStr) (*Type, error) {
280286
if err != nil {
281287
return nil, err
282288
}
283-
return NewType(str)
289+
typ, err := NewType(str)
290+
if err != nil {
291+
return nil, err
292+
}
293+
294+
// fill-in the `internalType` field into the type elems
295+
fillIn(typ, arg)
296+
297+
return typ, nil
298+
}
299+
300+
func fillIn(typ *Type, arg *ArgumentStr) error {
301+
typ.itype = arg.InternalType
302+
303+
if len(arg.Components) == 0 {
304+
// no more items, nothing else to do
305+
return nil
306+
}
307+
308+
// tuple types in the ABI with slices are represented as
309+
// tuple()[] or tuple()[2]. Thus, there might be element in the components
310+
// section of the abi but the next item not be a tuple.
311+
for {
312+
kind := typ.kind
313+
if kind == KindTuple {
314+
break
315+
}
316+
if kind != KindArray && kind != KindSlice {
317+
// error
318+
return fmt.Errorf("array or slice not found")
319+
}
320+
typ = typ.Elem()
321+
}
322+
323+
if len(arg.Components) != len(typ.tuple) {
324+
// incorrect length
325+
return fmt.Errorf("incorrect size")
326+
}
327+
328+
for indx, i := range arg.Components {
329+
fillIn(typ.tuple[indx].Elem, i)
330+
}
331+
332+
return nil
284333
}
285334

286335
// NewType parses a type in string format

abi/type_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
89
)
910

1011
func TestType(t *testing.T) {
@@ -363,6 +364,30 @@ func TestType(t *testing.T) {
363364
}
364365
}
365366

367+
func TestTypeArgument_InternalFields(t *testing.T) {
368+
arg := &ArgumentStr{
369+
Type: "tuple",
370+
Components: []*ArgumentStr{
371+
{
372+
Type: "tuple[]",
373+
Components: []*ArgumentStr{
374+
{
375+
Type: "int32",
376+
InternalType: "c",
377+
},
378+
},
379+
InternalType: "b",
380+
},
381+
},
382+
}
383+
384+
res, err := NewTypeFromArgument(arg)
385+
require.NoError(t, err)
386+
387+
require.Equal(t, res.tuple[0].Elem.itype, "b")
388+
require.Equal(t, res.tuple[0].Elem.elem.tuple[0].Elem.itype, "c")
389+
}
390+
366391
func TestSize(t *testing.T) {
367392
cases := []struct {
368393
Input string

0 commit comments

Comments
 (0)