From 8cccdaa579ff8daaaa8d3f151633ac17b2567e08 Mon Sep 17 00:00:00 2001 From: "ev.lekht" Date: Thu, 19 Sep 2024 17:37:26 +0400 Subject: [PATCH] linter fixes --- internal/matrix/msg_assembler.go | 2 -- pkg/cheques/eth_typed_data.go | 9 +++++---- pkg/cheques/signer.go | 16 ++++++++++------ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/internal/matrix/msg_assembler.go b/internal/matrix/msg_assembler.go index c45e4d19..36c9f6ad 100644 --- a/internal/matrix/msg_assembler.go +++ b/internal/matrix/msg_assembler.go @@ -47,8 +47,6 @@ func (a *messageAssembler) AssembleMessage(msg *matrix.CaminoMatrixMessage) (*ma } a.partialMessages[id] = append(a.partialMessages[id], msg) - // TODO: I believe it's safe to assume the number of chunks will not overflow - // #nosec G115 if len(a.partialMessages[id]) == int(msg.Metadata.NumberOfChunks) { decompressedCaminoMsg, err := a.assembleAndDecompressCaminoMatrixMessages(a.partialMessages[id]) delete(a.partialMessages, id) diff --git a/pkg/cheques/eth_typed_data.go b/pkg/cheques/eth_typed_data.go index a60d347d..18e15a7d 100644 --- a/pkg/cheques/eth_typed_data.go +++ b/pkg/cheques/eth_typed_data.go @@ -10,7 +10,7 @@ import ( "github.com/ethereum/go-ethereum/signer/core/apitypes" ) -// TODO@ code was copy-pasted from eth; does it need license header? +// TODO@ code was copy-pasted (with small modifications) from eth; does it need license header? func hashStructWithTypeHash(typedData *apitypes.TypedData, dataType string, typeHash []byte) ([]byte, error) { if exp, got := len(typedData.Types[dataType]), len(typedData.Message); exp < got { @@ -24,7 +24,8 @@ func hashStructWithTypeHash(typedData *apitypes.TypedData, dataType string, type for _, field := range typedData.Types[chequeType] { encType := field.Type encValue := typedData.Message[field.Name] - if encType[len(encType)-1:] == "]" { + switch { + case encType[len(encType)-1:] == "]": arrayValue, err := convertDataToSlice(encValue) if err != nil { return nil, dataMismatchError(encType, encValue) @@ -53,7 +54,7 @@ func hashStructWithTypeHash(typedData *apitypes.TypedData, dataType string, type } buffer.Write(crypto.Keccak256(arrayBuffer.Bytes())) - } else if typedData.Types[field.Type] != nil { + case typedData.Types[field.Type] != nil: mapValue, ok := encValue.(map[string]interface{}) if !ok { return nil, dataMismatchError(encType, encValue) @@ -63,7 +64,7 @@ func hashStructWithTypeHash(typedData *apitypes.TypedData, dataType string, type return nil, err } buffer.Write(crypto.Keccak256(encodedData)) - } else { + default: byteValue, err := typedData.EncodePrimitiveValue(encType, encValue, depth) if err != nil { return nil, err diff --git a/pkg/cheques/signer.go b/pkg/cheques/signer.go index 7900c525..e32308dd 100644 --- a/pkg/cheques/signer.go +++ b/pkg/cheques/signer.go @@ -35,14 +35,18 @@ var ( } ) -type chequeSigner struct { +type Signer interface { + SignCheque(cheque *Cheque) (*SignedCheque, error) +} + +type signer struct { privateKey *ecdsa.PrivateKey domainSeparator []byte chequeTypeHash []byte domain *apitypes.TypedDataDomain } -func NewChequeSigner(privateKey *ecdsa.PrivateKey, chainID *big.Int) (*chequeSigner, error) { +func NewSigner(privateKey *ecdsa.PrivateKey, chainID *big.Int) (Signer, error) { domain := apitypes.TypedDataDomain{ Name: "CaminoMessenger", Version: "1", @@ -63,7 +67,7 @@ func NewChequeSigner(privateKey *ecdsa.PrivateKey, chainID *big.Int) (*chequeSig return nil, err } - return &chequeSigner{ + return &signer{ privateKey: privateKey, domainSeparator: domainSeparator, chequeTypeHash: data.TypeHash(chequeType), @@ -71,7 +75,7 @@ func NewChequeSigner(privateKey *ecdsa.PrivateKey, chainID *big.Int) (*chequeSig }, nil } -func (cs *chequeSigner) SignCheque(cheque *Cheque) (*SignedCheque, error) { +func (cs *signer) SignCheque(cheque *Cheque) (*SignedCheque, error) { message := apitypes.TypedDataMessage{ "fromCMAccount": cheque.FromCMAccount.Hex(), "toCMAccount": cheque.ToCMAccount.Hex(), @@ -91,7 +95,7 @@ func (cs *chequeSigner) SignCheque(cheque *Cheque) (*SignedCheque, error) { typedDataHash, err := hashStructWithTypeHash(data, chequeType, cs.chequeTypeHash) if err != nil { - return nil, fmt.Errorf("failed to hash struct: %v", err) + return nil, fmt.Errorf("failed to hash struct: %w", err) } finalHash := crypto.Keccak256( @@ -102,7 +106,7 @@ func (cs *chequeSigner) SignCheque(cheque *Cheque) (*SignedCheque, error) { signature, err := crypto.Sign(finalHash, cs.privateKey) if err != nil { - return nil, fmt.Errorf("failed to sign the hash: %v", err) + return nil, fmt.Errorf("failed to sign the hash: %w", err) } // adjust recovery byte for compatibility