From 70c8c5b29b64949dadf3af5639307cb02dbfaa42 Mon Sep 17 00:00:00 2001 From: Dan Buchholz Date: Wed, 28 Feb 2024 11:48:38 -0800 Subject: [PATCH] refactor: remove unneeded methods, tests | err check fix Removed the `SignatureBytesToHex` since its just a wrapper on std hex lib method and refactored where needed. Removed failure tests for `HexToECDSA` and `FileToECDSA` since these are just wrappers for eth crypto. And made a small fix on order of err check in `uploader.go`. --- cmd/vaults/commands.go | 3 ++- internal/app/uploader.go | 3 ++- pkg/signing/signing.go | 6 ------ pkg/signing/signing_test.go | 35 +++-------------------------------- 4 files changed, 7 insertions(+), 40 deletions(-) diff --git a/cmd/vaults/commands.go b/cmd/vaults/commands.go index d916483..38ac8cc 100644 --- a/cmd/vaults/commands.go +++ b/cmd/vaults/commands.go @@ -3,6 +3,7 @@ package main import ( "context" "crypto/ecdsa" + "encoding/hex" "encoding/json" "errors" "fmt" @@ -625,7 +626,7 @@ func newSignCommand() *cli.Command { if err != nil { return fmt.Errorf("failed to sign file: %s", err) } - signature := signing.SignatureBytesToHex(signatureBytes) + signature := hex.EncodeToString(signatureBytes) fmt.Println(signature) return nil diff --git a/internal/app/uploader.go b/internal/app/uploader.go index ba0bfbe..e6a6620 100644 --- a/internal/app/uploader.go +++ b/internal/app/uploader.go @@ -3,6 +3,7 @@ package app import ( "context" "crypto/ecdsa" + "encoding/hex" "fmt" "io" "os" @@ -45,10 +46,10 @@ func (bu *VaultsUploader) Upload( signer := signing.NewSigner(bu.privateKey) signatureBytes, err := signer.SignFile(filepath) - signature := signing.SignatureBytesToHex(signatureBytes) if err != nil { return fmt.Errorf("signing the file: %s", err) } + signature := hex.EncodeToString(signatureBytes) filename := filepath if strings.Contains(filepath, "/") { diff --git a/pkg/signing/signing.go b/pkg/signing/signing.go index b77d7aa..1e369f6 100644 --- a/pkg/signing/signing.go +++ b/pkg/signing/signing.go @@ -3,7 +3,6 @@ package signing import ( "bufio" "crypto/ecdsa" - "encoding/hex" "fmt" "io" "os" @@ -123,8 +122,3 @@ func (s *Signer) SignBytes(data []byte) ([]byte, error) { return signature, nil } - -// SignatureBytesToHex converts a byte slice to a hex-encoded string. -func SignatureBytesToHex(b []byte) string { - return hex.EncodeToString(b) -} diff --git a/pkg/signing/signing_test.go b/pkg/signing/signing_test.go index 7808ff5..f779751 100644 --- a/pkg/signing/signing_test.go +++ b/pkg/signing/signing_test.go @@ -2,6 +2,7 @@ package signing import ( "crypto/ecdsa" + "encoding/hex" "os" "testing" @@ -72,7 +73,7 @@ func TestSignFile(t *testing.T) { defer cleanup() signatureBytes, err := signer.SignFile(filename) - signature := SignatureBytesToHex(signatureBytes) + signature := hex.EncodeToString(signatureBytes) if tc.wantErr != "" { require.Error(t, err, "Expected an error for %v", tc.name) require.Contains(t, err.Error(), tc.wantErr, "SignFile() error = %v, wantErr %v", err, tc.wantErr) @@ -111,7 +112,7 @@ func TestSignBytes(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { signatureBytes, err := signer.SignBytes(tc.content) - signature := SignatureBytesToHex(signatureBytes) + signature := hex.EncodeToString(signatureBytes) if tc.wantErr != "" { require.Error(t, err, "Expected an error for %v", tc.name) require.Contains(t, err.Error(), tc.wantErr, "SignBytes() error = %v, wantErr %v", err, tc.wantErr) @@ -154,36 +155,6 @@ func TestPrivateKey(t *testing.T) { }, wantErr: "", }, - { - name: "should fail to load 0x prefixed string", - setup: func() (pk string, filename string, cleanup func()) { - pk = "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d" - return pk, "", func() {} - }, - wantErr: "invalid hex character 'x' in private key", - }, - { - name: "should fail to load random string", - setup: func() (pk string, filename string, cleanup func()) { - pk = "1234abcd" - return pk, "", func() {} - }, - wantErr: "invalid length, need 256 bits", - }, - { - name: "should fail to load empty private key file", - setup: func() (pk string, filename string, cleanup func()) { - tmpFile, _ := os.CreateTemp("", "test_file") - name := tmpFile.Name() - err := tmpFile.Close() - require.NoError(t, err, "Error closing file") - return pk, name, func() { - err = os.Remove(name) - require.NoError(t, err, "Error removing file") - } - }, - wantErr: "key file too short, want 64 hex characters", - }, } for _, tc := range testCases {