Skip to content

Commit 1ede683

Browse files
committed
accounts/abi: add another unpack interface
1 parent 5603715 commit 1ede683

5 files changed

Lines changed: 395 additions & 14 deletions

File tree

‎accounts/abi/abi.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
136136

137137
// MethodById looks up a method by the 4-byte id
138138
// returns nil if none found
139-
func (abi *ABI) MethodById(sigdata []byte) *Method {
139+
func (abi *ABI) MethodById(sigdata []byte) (*Method, error){
140140
for _, method := range abi.Methods {
141141
if bytes.Equal(method.Id(), sigdata[:4]) {
142-
return &method
142+
return &method, nil
143143
}
144144
}
145-
return nil
145+
return nil, fmt.Errorf("ABI spec does not contain method signature in data: 0x%x", sigdata[:4])
146146
}

‎accounts/abi/abi_test.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,11 @@ func TestABI_MethodById(t *testing.T) {
689689
}
690690
for name, m := range abi.Methods {
691691
a := fmt.Sprintf("%v", m)
692-
b := fmt.Sprintf("%v", abi.MethodById(m.Id()))
692+
m2,err := abi.MethodById(m.Id())
693+
if err != nil {
694+
t.Fatal(err)
695+
}
696+
b := fmt.Sprintf("%v", m2)
693697
if a != b {
694698
t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(m.Id()))
695699
}

‎accounts/abi/argument.go‎

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ func (arguments Arguments) LengthNonIndexed() int {
6767
return out
6868
}
6969

70+
func (arguments Arguments) NonIndexed() Arguments{
71+
var ret []Argument
72+
for _,arg := range arguments{
73+
if !arg.Indexed{
74+
ret = append(ret, arg)
75+
}
76+
}
77+
return ret
78+
}
79+
7080
// isTuple returns true for non-atomic constructs, like (uint,uint) or uint[]
7181
func (arguments Arguments) isTuple() bool {
7282
return len(arguments) > 1
@@ -114,14 +124,9 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
114124
// `j` counts the number of complex types.
115125
// both `i` and `j` are used to to correctly compute `data` offset.
116126

117-
i, j := -1, 0
118-
for _, arg := range arguments {
127+
j := 0
128+
for i, arg := range arguments.NonIndexed() {
119129

120-
if arg.Indexed {
121-
// can't read, continue
122-
continue
123-
}
124-
i++
125130
marshalledValue, err := toGoType((i+j)*32, arg.Type, output)
126131
if err != nil {
127132
return err
@@ -178,15 +183,51 @@ func (arguments Arguments) unpackAtomic(v interface{}, output []byte) error {
178183
}
179184

180185
value := valueOf.Elem()
181-
182186
marshalledValue, err := toGoType(0, arg.Type, output)
183187
if err != nil {
184188
return err
185189
}
186190
return set(value, reflect.ValueOf(marshalledValue), arg)
187191
}
188192

189-
// Unpack performs the operation Go format -> Hexdata
193+
// UnpackValues can be used to unpack ABI-encoded hexdata according to the ABI-specification,
194+
// without supplying a struct to unpack into. Instead, this method returns a list containing the
195+
// values. An atomic argument will be a list with one element.
196+
func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error){
197+
198+
retval := make([]interface{},0,arguments.LengthNonIndexed())
199+
200+
virtualArgs := 0
201+
202+
for index,arg:= range arguments.NonIndexed(){
203+
204+
marshalledValue, err := toGoType((index + virtualArgs) * 32, arg.Type, data)
205+
206+
if arg.Type.T == ArrayTy {
207+
//If we have a static array, like [3]uint256, these are coded as
208+
// just like uint256,uint256,uint256.
209+
// This means that we need to add two 'virtual' arguments when
210+
// we count the index from now on
211+
212+
virtualArgs += arg.Type.Size - 1
213+
}
214+
215+
if err != nil{
216+
return nil, err
217+
}
218+
retval = append(retval, marshalledValue)
219+
}
220+
return retval, nil
221+
}
222+
223+
// UnpackValues performs the operation Go format -> Hexdata
224+
// It is the semantic opposite of UnpackValues
225+
func (arguments Arguments) PackValues(args []interface{}) ([]byte, error) {
226+
return arguments.Pack(args...)
227+
}
228+
229+
230+
// Pack performs the operation Go format -> Hexdata
190231
func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
191232
// Make sure arguments match up and pack them
192233
abiArgs := arguments

‎accounts/abi/unpack_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ var unpackTests = []unpackTest{
130130
{
131131
def: `[{"type": "bytes32"}]`,
132132
enc: "0100000000000000000000000000000000000000000000000000000000000000",
133-
want: common.HexToHash("0100000000000000000000000000000000000000000000000000000000000000"),
133+
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},
134134
},
135135
{
136136
def: `[{"type": "function"}]`,

0 commit comments

Comments
 (0)