forked from onflow/flow-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request onflow#6190 from onflow/ramtin/evm-avoid-hex-encod…
…ing-for-event-bytes [Flow EVM] Avoid hex encoding/decoding for event fields of type bytes
- Loading branch information
Showing
9 changed files
with
98 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/onflow/cadence" | ||
) | ||
|
||
// BytesToCadenceUInt8ArrayValue converts bytes into a Cadence array of type UInt8 | ||
func BytesToCadenceUInt8ArrayValue(b []byte) cadence.Array { | ||
values := make([]cadence.Value, len(b)) | ||
for i, v := range b { | ||
values[i] = cadence.NewUInt8(v) | ||
} | ||
return cadence.NewArray(values).WithType( | ||
cadence.NewVariableSizedArrayType(cadence.UInt8Type), | ||
) | ||
} | ||
|
||
var cadenceArrayTypeOfUInt8 = cadence.NewVariableSizedArrayType(cadence.UInt8Type) | ||
|
||
// CadenceUInt8ArrayValueToBytes converts a Cadence array of type UInt8 into a byte slice | ||
func CadenceUInt8ArrayValueToBytes(a cadence.Value) ([]byte, error) { | ||
aa, ok := a.(cadence.Array) | ||
if !ok { | ||
return nil, fmt.Errorf("value is not an array") | ||
} | ||
|
||
arrayType := aa.Type() | ||
// if array type is empty, continue | ||
if arrayType != nil && !arrayType.Equal(cadenceArrayTypeOfUInt8) { | ||
return nil, fmt.Errorf("invalid array type") | ||
} | ||
|
||
values := make([]byte, len(aa.Values)) | ||
for i, v := range aa.Values { | ||
values[i] = byte(v.(cadence.UInt8)) | ||
} | ||
return values, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/onflow/cadence" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/onflow/flow-go/fvm/evm/types" | ||
) | ||
|
||
func TestCadenceConversion(t *testing.T) { | ||
input := []byte{1, 2, 3, 4, 5} | ||
|
||
inCadence := types.BytesToCadenceUInt8ArrayValue(input) | ||
output, err := types.CadenceUInt8ArrayValueToBytes(inCadence) | ||
require.NoError(t, err) | ||
require.Equal(t, input, output) | ||
|
||
invalidTypeArray := cadence.NewArray(nil). | ||
WithType(cadence.NewVariableSizedArrayType(cadence.UFix64Type)) | ||
_, err = types.CadenceUInt8ArrayValueToBytes(invalidTypeArray) | ||
require.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters