Skip to content

Commit

Permalink
Merge branch 'main' into upgrade-go
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Whitehead <[email protected]>
  • Loading branch information
matthew1001 authored Sep 11, 2024
2 parents c0de54b + 27d2240 commit c586e8c
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 89 deletions.
13 changes: 13 additions & 0 deletions config.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,25 @@ nav_order: 2
|initWaitTime|The initial retry delay|[`time.Duration`](https://pkg.go.dev/time#Duration)|`250ms`
|maxWaitTime|The maximum retry delay|[`time.Duration`](https://pkg.go.dev/time#Duration)|`30s`

## backend.throttle

|Key|Description|Type|Default Value|
|---|-----------|----|-------------|
|burst|The maximum number of requests that can be made in a short period of time before the throttling kicks in.|`int`|`<nil>`
|requestsPerSecond|The average rate at which requests are allowed to pass through over time.|`int`|`<nil>`

## backend.tls

|Key|Description|Type|Default Value|
|---|-----------|----|-------------|
|ca|The TLS certificate authority in PEM format (this option is ignored if caFile is also set)|`string`|`<nil>`
|caFile|The path to the CA file for TLS on this API|`string`|`<nil>`
|cert|The TLS certificate in PEM format (this option is ignored if certFile is also set)|`string`|`<nil>`
|certFile|The path to the certificate file for TLS on this API|`string`|`<nil>`
|clientAuth|Enables or disables client auth for TLS on this API|`string`|`<nil>`
|enabled|Enables or disables TLS on this API|`boolean`|`false`
|insecureSkipHostVerify|When to true in unit test development environments to disable TLS verification. Use with extreme caution|`boolean`|`<nil>`
|key|The TLS certificate key in PEM format (this option is ignored if keyFile is also set)|`string`|`<nil>`
|keyFile|The path to the private key file for TLS on this API|`string`|`<nil>`
|requiredDNAttributes|A set of required subject DN attributes. Each entry is a regular expression, and the subject certificate must have a matching attribute of the specified type (CN, C, O, OU, ST, L, STREET, POSTALCODE, SERIALNUMBER are valid attributes)|`map[string]string`|`<nil>`

Expand Down Expand Up @@ -181,10 +191,13 @@ nav_order: 2

|Key|Description|Type|Default Value|
|---|-----------|----|-------------|
|ca|The TLS certificate authority in PEM format (this option is ignored if caFile is also set)|`string`|`<nil>`
|caFile|The path to the CA file for TLS on this API|`string`|`<nil>`
|cert|The TLS certificate in PEM format (this option is ignored if certFile is also set)|`string`|`<nil>`
|certFile|The path to the certificate file for TLS on this API|`string`|`<nil>`
|clientAuth|Enables or disables client auth for TLS on this API|`string`|`<nil>`
|enabled|Enables or disables TLS on this API|`boolean`|`false`
|insecureSkipHostVerify|When to true in unit test development environments to disable TLS verification. Use with extreme caution|`boolean`|`<nil>`
|key|The TLS certificate key in PEM format (this option is ignored if keyFile is also set)|`string`|`<nil>`
|keyFile|The path to the private key file for TLS on this API|`string`|`<nil>`
|requiredDNAttributes|A set of required subject DN attributes. Each entry is a regular expression, and the subject certificate must have a matching attribute of the specified type (CN, C, O, OU, ST, L, STREET, POSTALCODE, SERIALNUMBER are valid attributes)|`map[string]string`|`<nil>`
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/hyperledger/firefly-common => github.com/kaleido-io/firefly-common v0.0.0-20240827134901-edb07289f156
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ github.com/jarcoal/httpmock v1.2.0 h1:gSvTxxFR/MEMfsGrvRbdfpRUMBStovlSRLw0Ep1bww
github.com/jarcoal/httpmock v1.2.0/go.mod h1:oCoTsnAz4+UoOUIf5lJOWV2QQIW5UoeUI6aM2YnWAZk=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kaleido-io/firefly-common v0.0.0-20240827134901-edb07289f156 h1:HQpScPoAm9xsACbu9r31wVQ5sQFxLsfe9XzPGY5c4rI=
github.com/kaleido-io/firefly-common v0.0.0-20240827134901-edb07289f156/go.mod h1:dXewcVMFNON2SvQ1UPvu64OWUt77+M3p8qy61lT1kE4=
github.com/karlseguin/ccache v2.0.3+incompatible h1:j68C9tWOROiOLWTS/kCGg9IcJG+ACqn5+0+t8Oh83UU=
github.com/karlseguin/ccache v2.0.3+incompatible/go.mod h1:CM9tNPzT6EdRh14+jiW8mEF9mkNZuuE51qmgGYUB93w=
github.com/karlseguin/expect v1.0.8 h1:Bb0H6IgBWQpadY25UDNkYPDB9ITqK1xnSoZfAq362fw=
Expand Down
5 changes: 5 additions & 0 deletions internal/signermsgs/en_error_messges.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,9 @@ var (
MsgInvalidEIP1559Transaction = ffe("FF22084", "Transaction payload invalid (EIP-1559): %v")
MsgInvalidEIP155TransactionV = ffe("FF22085", "Invalid V value from EIP-155 transaction (chainId=%d)")
MsgInvalidChainID = ffe("FF22086", "Invalid chainId expected=%d actual=%d")
MsgSigningInvalidCompactRSV = ffe("FF22087", "Invalid signature data (compact R,S,V) length=%d (expected=65)")
MsgInvalidNumberString = ffe("FF22088", "Invalid integer string '%s'")
MsgInvalidIntPrecisionLoss = ffe("FF22089", "String %s cannot be converted to integer without losing precision")
MsgInvalidUint64PrecisionLoss = ffe("FF22090", "String %s cannot be converted to a uint64 without losing precision")
MsgInvalidJSONTypeForBigInt = ffe("FF22091", "JSON parsed '%T' cannot be converted to an integer")
)
18 changes: 12 additions & 6 deletions pkg/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,13 @@ func (e *Entry) SolidityDef() (string, []string, error) {
// SolidityDefCtx returns a Solidity-like descriptor of the entry, including its type
func (e *Entry) SolidityDefCtx(ctx context.Context) (string, []string, error) {
// Everything apart from event and error is a type of function
isFunction := e.Type != Error && e.Type != Event
var fieldType SolFieldType
switch e.Type {
case Error, Event:
fieldType = EventOrErrorField
default:
fieldType = FunctionInput
}

allChildStructs := []string{}
buff := new(strings.Builder)
Expand All @@ -713,7 +719,7 @@ func (e *Entry) SolidityDefCtx(ctx context.Context) (string, []string, error) {
if i > 0 {
buff.WriteString(", ")
}
s, childStructs, err := p.SolidityDefCtx(ctx, isFunction)
s, childStructs, err := p.SolidityDefCtx(ctx, fieldType)
if err != nil {
return "", nil, err
}
Expand All @@ -722,7 +728,7 @@ func (e *Entry) SolidityDefCtx(ctx context.Context) (string, []string, error) {
}
buff.WriteRune(')')

if isFunction {
if fieldType == FunctionInput {
buff.WriteString(" external")
if e.StateMutability != "" &&
// The state mutability nonpayable is reflected in Solidity by not specifying a state mutability modifier at all.
Expand All @@ -736,7 +742,7 @@ func (e *Entry) SolidityDefCtx(ctx context.Context) (string, []string, error) {
if i > 0 {
buff.WriteString(", ")
}
s, childStructs, err := p.SolidityDefCtx(ctx, isFunction)
s, childStructs, err := p.SolidityDefCtx(ctx, fieldType)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -782,13 +788,13 @@ func (p *Parameter) SignatureStringCtx(ctx context.Context) (string, error) {
return tc.String(), nil
}

func (p *Parameter) SolidityDefCtx(ctx context.Context, inFunction bool) (string, []string, error) {
func (p *Parameter) SolidityDefCtx(ctx context.Context, fieldType SolFieldType) (string, []string, error) {
// Ensure the type component tree has been parsed
tc, err := p.TypeComponentTreeCtx(ctx)
if err != nil {
return "", nil, err
}
solDef, childStructs := tc.SolidityParamDef(inFunction)
solDef, childStructs := tc.SolidityParamDef(fieldType)
return solDef, childStructs, nil
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/abi/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ const sampleABI5 = `[
"internalType": "struct AribtraryWidgets.Widget[]",
"name": "widgets",
"type": "tuple[]"
},
{
"name": "account",
"type": "address",
"indexed": true
}
],
"name": "Invoiced",
Expand Down Expand Up @@ -1015,7 +1020,7 @@ func TestComplexStructSolidityDef(t *testing.T) {

solDef, childStructs, err = abi.Events()["Invoiced"].SolidityDef()
assert.NoError(t, err)
assert.Equal(t, "event Invoiced(Customer customer, Widget[] widgets)", solDef)
assert.Equal(t, "event Invoiced(Customer customer, Widget[] widgets, address indexed account)", solDef)
assert.Equal(t, []string{
"struct Customer { address owner; bytes32 locator; }",
"struct Widget { string description; uint256 price; string[] attributes; }",
Expand Down
55 changes: 24 additions & 31 deletions pkg/abi/inputparsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ package abi
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"reflect"
"strings"

"github.com/hyperledger/firefly-common/pkg/i18n"
"github.com/hyperledger/firefly-signer/internal/signermsgs"
"github.com/hyperledger/firefly-signer/pkg/ethtypes"
)

var (
Expand Down Expand Up @@ -128,58 +130,49 @@ func getFloat64IfConvertible(v interface{}) (float64, bool) {
// with a focus on those generated by the result of an Unmarshal using Go's default
// unmarshalling.
func getIntegerFromInterface(ctx context.Context, desc string, v interface{}) (*big.Int, error) {
i := new(big.Int)
switch vt := v.(type) {
case json.Number:
i, err := ethtypes.BigIntegerFromString(ctx, vt.String())
if err != nil {
return nil, i18n.WrapError(ctx, err, signermsgs.MsgInvalidIntegerABIInput, vt, v, desc)
}
return i, nil
case string:
// We use Go's default '0' base integer parsing, where `0x` means hex,
// no prefix means decimal etc.
i, ok := i.SetString(vt, 0)
if !ok {
return nil, i18n.NewError(ctx, signermsgs.MsgInvalidIntegerABIInput, vt, v, desc)
i, err := ethtypes.BigIntegerFromString(ctx, vt)
if err != nil {
return nil, i18n.WrapError(ctx, err, signermsgs.MsgInvalidIntegerABIInput, vt, v, desc)
}
return i, nil
case *big.Float:
i, _ := vt.Int(i)
i, _ := vt.Int(nil)
return i, nil
case *big.Int:
return vt, nil
case float64:
// This is how JSON numbers come in (no distinction between integers/floats)
i.SetInt64(int64(vt))
return i, nil
return new(big.Int).SetInt64(int64(vt)), nil
case float32:
i.SetInt64(int64(vt))
return i, nil
return new(big.Int).SetInt64(int64(vt)), nil
case int64:
i.SetInt64(vt)
return i, nil
return new(big.Int).SetInt64(vt), nil
case int32:
i.SetInt64(int64(vt))
return i, nil
return new(big.Int).SetInt64(int64(vt)), nil
case int16:
i.SetInt64(int64(vt))
return i, nil
return new(big.Int).SetInt64(int64(vt)), nil
case int8:
i.SetInt64(int64(vt))
return i, nil
return new(big.Int).SetInt64(int64(vt)), nil
case int:
i.SetInt64(int64(vt))
return i, nil
return new(big.Int).SetInt64(int64(vt)), nil
case uint64:
i.SetInt64(int64(vt))
return i, nil
return new(big.Int).SetUint64(vt), nil
case uint32:
i.SetInt64(int64(vt))
return i, nil
return new(big.Int).SetInt64(int64(vt)), nil
case uint16:
i.SetInt64(int64(vt))
return i, nil
return new(big.Int).SetInt64(int64(vt)), nil
case uint8:
i.SetInt64(int64(vt))
return i, nil
return new(big.Int).SetInt64(int64(vt)), nil
case uint:
i.SetInt64(int64(vt))
return i, nil
return new(big.Int).SetUint64(uint64(vt)), nil
default:
if str, ok := getStringIfConvertible(v); ok {
return getIntegerFromInterface(ctx, desc, str)
Expand Down
26 changes: 26 additions & 0 deletions pkg/abi/inputparsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package abi

import (
"context"
"encoding/json"
"math/big"
"testing"

"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-signer/pkg/ethtypes"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -186,6 +188,30 @@ func TestGetIntegerFromInterface(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "-12345", i.String())

var scientificNumber fftypes.JSONAny
err = json.Unmarshal([]byte("1.0000000000000000000000001e+25"), &scientificNumber)
assert.NoError(t, err)

i, err = getIntegerFromInterface(ctx, "ut", scientificNumber)
assert.NoError(t, err)
assert.Equal(t, "10000000000000000000000001", i.String())

var hugeNumber fftypes.JSONAny
err = json.Unmarshal([]byte("10000000000000000000000000000001"), &hugeNumber)
assert.NoError(t, err)

i, err = getIntegerFromInterface(ctx, "ut", hugeNumber)
assert.NoError(t, err)
assert.Equal(t, "10000000000000000000000000000001", i.String())

var jsonNumber json.Number
err = json.Unmarshal([]byte("20000000000000000000000000000002"), &jsonNumber)
assert.NoError(t, err)

i, err = getIntegerFromInterface(ctx, "ut", jsonNumber)
assert.NoError(t, err)
assert.Equal(t, "20000000000000000000000000000002", i.String())

i32 := int32(-12345)
var iPI32 TestInt32PtrCustomType = &i32
i, err = getIntegerFromInterface(ctx, "ut", iPI32)
Expand Down
25 changes: 19 additions & 6 deletions pkg/abi/typecomponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type TypeComponent interface {
DecodeABIData(d []byte, offset int) (*ComponentValue, error)
DecodeABIDataCtx(ctx context.Context, d []byte, offest int) (*ComponentValue, error)

SolidityParamDef(inFunction bool) (solDef string, structDefs []string) // gives a string that can be used to define this param in solidity
SolidityParamDef(fieldType SolFieldType) (solDef string, structDefs []string) // gives a string that can be used to define this param in solidity
SolidityTypeDef() (isRef bool, typeDef string, childStructs []string)
SolidityStructDef() (structName string, structs []string)
}
Expand Down Expand Up @@ -187,6 +187,14 @@ const (
BaseTypeString BaseTypeName = "string"
)

type SolFieldType int

const (
FunctionInput SolFieldType = iota // input to a function, or a constructor
EventOrErrorField // a field of an event or an error
StructField // a field of a struct
)

// tupleTypeString appears in the same place in the ABI as elementary type strings, but it is not an elementary type.
// We treat it separately.
const tupleTypeString = "tuple"
Expand Down Expand Up @@ -371,13 +379,18 @@ func (tc *typeComponent) String() string {
}
}

func (tc *typeComponent) SolidityParamDef(inFunction bool) (string, []string) {
func (tc *typeComponent) SolidityParamDef(fieldType SolFieldType) (string, []string) {
isRef, paramDef, childStructs := tc.SolidityTypeDef()
if isRef && inFunction {
paramDef = fmt.Sprintf("%s memory", paramDef)
if isRef && fieldType == FunctionInput {
paramDef += " memory"
}
if tc.parameter != nil && tc.parameter.Name != "" {
paramDef = fmt.Sprintf("%s %s", paramDef, tc.parameter.Name)
if tc.parameter != nil {
if fieldType == EventOrErrorField && tc.parameter.Indexed {
paramDef += " indexed"
}
if tc.parameter.Name != "" {
paramDef = fmt.Sprintf("%s %s", paramDef, tc.parameter.Name)
}
}
return paramDef, childStructs
}
Expand Down
29 changes: 9 additions & 20 deletions pkg/ethtypes/hexinteger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -18,7 +18,6 @@ package ethtypes

import (
"context"
"encoding/json"
"fmt"
"math/big"

Expand All @@ -37,25 +36,15 @@ func (h HexInteger) MarshalJSON() ([]byte, error) {
}

func (h *HexInteger) UnmarshalJSON(b []byte) error {
var i interface{}
_ = json.Unmarshal(b, &i)
switch i := i.(type) {
case float64:
*h = HexInteger(*big.NewInt(int64(i)))
return nil
case string:
bi, ok := new(big.Int).SetString(i, 0)
if !ok {
return fmt.Errorf("unable to parse integer: %s", i)
}
if bi.Sign() < 0 {
return fmt.Errorf("negative values are not supported: %s", i)
}
*h = HexInteger(*bi)
return nil
default:
return fmt.Errorf("unable to parse integer from type %T", i)
bi, err := UnmarshalBigInt(context.Background(), b)
if err != nil {
return err
}
if bi.Sign() < 0 {
return fmt.Errorf("negative values are not supported: %s", b)
}
*h = HexInteger(*bi)
return nil
}

func (h *HexInteger) BigInt() *big.Int {
Expand Down
Loading

0 comments on commit c586e8c

Please sign in to comment.