Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions accounts/abi/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,37 @@ func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
case StringTy:
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil
case AddressTy:
var slices []byte
if reflectValue.Kind() == reflect.Array {
reflectValue = mustArrayToByteSlice(reflectValue)
slices = mustArrayToByteSlice(reflectValue)
} else {
slices = reflectValue.Bytes()
}

return common.LeftPadBytes(reflectValue.Bytes(), 32), nil
return common.LeftPadBytes(slices, 32), nil
case BoolTy:
if reflectValue.Bool() {
return math.PaddedBigBytes(common.Big1, 32), nil
}
return math.PaddedBigBytes(common.Big0, 32), nil
case BytesTy:
var slices []byte
if reflectValue.Kind() == reflect.Array {
reflectValue = mustArrayToByteSlice(reflectValue)
}
if reflectValue.Type() != reflect.TypeOf([]byte{}) {
return []byte{}, errors.New("bytes type is neither slice nor array")
slices = mustArrayToByteSlice(reflectValue)
} else {
if reflectValue.Type() != reflect.TypeOf([]byte{}) {
return []byte{}, errors.New("bytes type is neither slice nor array")
}
slices = reflectValue.Bytes()
}
return packBytesSlice(reflectValue.Bytes(), reflectValue.Len()), nil
return packBytesSlice(slices, len(slices)), nil
case FixedBytesTy, FunctionTy:
var slices []byte
if reflectValue.Kind() == reflect.Array {
reflectValue = mustArrayToByteSlice(reflectValue)
slices = mustArrayToByteSlice(reflectValue)
} else {
slices = reflectValue.Bytes()
}
return common.RightPadBytes(reflectValue.Bytes(), 32), nil
return common.RightPadBytes(slices, 32), nil
default:
return []byte{}, fmt.Errorf("could not pack element, unknown type: %v", t.T)
}
Expand Down
9 changes: 5 additions & 4 deletions accounts/abi/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ func reflectIntType(unsigned bool, size int) reflect.Type {

// mustArrayToByteSlice creates a new byte slice with the exact same size as value
// and copies the bytes in value to the new slice.
func mustArrayToByteSlice(value reflect.Value) reflect.Value {
slice := reflect.ValueOf(make([]byte, value.Len()))
reflect.Copy(slice, value)
return slice
func mustArrayToByteSlice(value reflect.Value) []byte {
ptr := reflect.New(value.Type())
ptr.Elem().Set(value)
ret := ptr.Elem().Bytes()
return ret
}

// set attempts to assign src to dst by either setting, copying or otherwise.
Expand Down