Skip to content

Commit

Permalink
linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Sep 19, 2024
1 parent 23fc2cc commit 8cccdaa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 0 additions & 2 deletions internal/matrix/msg_assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions pkg/cheques/eth_typed_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
16 changes: 10 additions & 6 deletions pkg/cheques/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -63,15 +67,15 @@ func NewChequeSigner(privateKey *ecdsa.PrivateKey, chainID *big.Int) (*chequeSig
return nil, err
}

return &chequeSigner{
return &signer{
privateKey: privateKey,
domainSeparator: domainSeparator,
chequeTypeHash: data.TypeHash(chequeType),
domain: &domain,
}, 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(),
Expand All @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 8cccdaa

Please sign in to comment.