Skip to content

Commit

Permalink
refactor: remove unneeded methods, tests | err check fix
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
dtbuchholz committed Feb 28, 2024
1 parent 841771e commit 70c8c5b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 40 deletions.
3 changes: 2 additions & 1 deletion cmd/vaults/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/app/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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, "/") {
Expand Down
6 changes: 0 additions & 6 deletions pkg/signing/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package signing
import (
"bufio"
"crypto/ecdsa"
"encoding/hex"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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)
}
35 changes: 3 additions & 32 deletions pkg/signing/signing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package signing

import (
"crypto/ecdsa"
"encoding/hex"
"os"
"testing"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 70c8c5b

Please sign in to comment.