From 1db166448b54db8bc76af478e2dac00ce3bffbe1 Mon Sep 17 00:00:00 2001 From: Peter Broadhurst Date: Sun, 8 Sep 2024 13:44:37 -0400 Subject: [PATCH] Include empty arrays for components/inputs/outputs on JSON Serialize Signed-off-by: Peter Broadhurst --- pkg/abi/abi.go | 2 +- pkg/abi/abi_test.go | 19 +++++++++++++++++++ pkg/abi/signedi256.go | 6 +++--- pkg/abi/typecomponents.go | 5 ++++- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/pkg/abi/abi.go b/pkg/abi/abi.go index ef0466a4..f94135d5 100644 --- a/pkg/abi/abi.go +++ b/pkg/abi/abi.go @@ -242,7 +242,7 @@ type Parameter struct { Name string `ffstruct:"EthABIParameter" json:"name"` // The name of the argument - does not affect the signature Type string `ffstruct:"EthABIParameter" json:"type"` // The canonical type of the parameter InternalType string `ffstruct:"EthABIParameter" json:"internalType,omitempty"` // Additional internal type information that might be generated by the compiler - Components ParameterArray `ffstruct:"EthABIParameter" json:"components,omitempty"` // An ordered list (tuple) of nested elements for array/object types + Components ParameterArray `ffstruct:"EthABIParameter" json:"components"` // An ordered list (tuple) of nested elements for array/object types Indexed bool `ffstruct:"EthABIParameter" json:"indexed,omitempty"` // Events only: Whether the parameter is indexed into one of the topics of the log, or in the log's data segment parsed *typeComponent // cached components diff --git a/pkg/abi/abi_test.go b/pkg/abi/abi_test.go index 87ed8b1b..af993ab2 100644 --- a/pkg/abi/abi_test.go +++ b/pkg/abi/abi_test.go @@ -26,6 +26,7 @@ import ( "github.com/hyperledger/firefly-common/pkg/ffapi" "github.com/hyperledger/firefly-signer/pkg/ethtypes" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) const sampleABI1 = `[ @@ -1027,3 +1028,21 @@ func TestComplexStructSolidityDef(t *testing.T) { }, childStructs) } + +func TestRoundTripEmptyComponents(t *testing.T) { + + abiString := `[ + {"type":"function", "name":"fn1", "inputs": [ + { "type": "tuple", "name":"param1","components": [] } + ], "outputs": []} + ]` + var abi ABI + err := json.Unmarshal([]byte(abiString), &abi) + require.NoError(t, err) + + roundTripped, err := json.Marshal(&abi) + require.NoError(t, err) + + assert.JSONEq(t, abiString, string(roundTripped)) + +} diff --git a/pkg/abi/signedi256.go b/pkg/abi/signedi256.go index 1e11856d..9e2324b3 100644 --- a/pkg/abi/signedi256.go +++ b/pkg/abi/signedi256.go @@ -26,9 +26,9 @@ var posMax = map[uint16]*big.Int{} var negMax = map[uint16]*big.Int{} func init() { - for i := 8; i <= 256; i += 8 { - posMax[uint16(i)] = maxPositiveSignedInt(uint(i)) - negMax[uint16(i)] = maxNegativeSignedInt(uint(i)) + for i := uint16(8); i <= uint16(256); i += 8 { + posMax[i] = maxPositiveSignedInt(uint(i)) + negMax[i] = maxNegativeSignedInt(uint(i)) } } diff --git a/pkg/abi/typecomponents.go b/pkg/abi/typecomponents.go index 91ebda60..28324061 100644 --- a/pkg/abi/typecomponents.go +++ b/pkg/abi/typecomponents.go @@ -630,6 +630,7 @@ func parseMSuffix(ctx context.Context, abiTypeString string, ec *typeComponent, if err != nil { return i18n.WrapError(ctx, err, signermsgs.MsgInvalidABISuffix, abiTypeString, ec.elementaryType) } + //nolint:gosec // we used bitSize on ParseUint above ec.m = uint16(val) if ec.m < ec.elementaryType.mMin || ec.m > ec.elementaryType.mMax { return i18n.NewError(ctx, signermsgs.MsgInvalidABISuffix, abiTypeString, ec.elementaryType) @@ -646,6 +647,7 @@ func parseNSuffix(ctx context.Context, abiTypeString string, ec *typeComponent, if err != nil { return i18n.WrapError(ctx, err, signermsgs.MsgInvalidABISuffix, abiTypeString, ec.elementaryType) } + //nolint:gosec // we used bitSize on ParseUint above ec.n = uint16(val) if ec.n < ec.elementaryType.nMin || ec.n > ec.elementaryType.nMax { return i18n.NewError(ctx, signermsgs.MsgInvalidABISuffix, abiTypeString, ec.elementaryType) @@ -672,10 +674,11 @@ func parseMxNSuffix(ctx context.Context, abiTypeString string, ec *typeComponent // parseArrayM parses the "8" in "uint256[8]" for a fixed length array of [M] func parseArrayM(ctx context.Context, abiTypeString string, ac *typeComponent, mStr string) error { - val, err := strconv.ParseUint(mStr, 10, 64) + val, err := strconv.ParseUint(mStr, 10, 32) if err != nil { return i18n.WrapError(ctx, err, signermsgs.MsgInvalidABIArraySpec, abiTypeString) } + //nolint:gosec // we used bitSize on ParseUint above ac.arrayLength = int(val) return nil }