Skip to content

Commit

Permalink
k1util: trim newline when decoding charon-enr-private-key (#1875)
Browse files Browse the repository at this point in the history
Trims newlines when decoding `charon-enr-private-key`.

category: bug
ticket: none
  • Loading branch information
xenowits authored Mar 13, 2023
1 parent ff59c49 commit 9ede198
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/k1util/k1util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package k1util
import (
"encoding/hex"
"os"
"strings"

k1 "github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
Expand Down Expand Up @@ -118,7 +119,7 @@ func Load(file string) (*k1.PrivateKey, error) {
return nil, errors.Wrap(err, "read private key from disk", z.Str("file", file))
}

b, err := hex.DecodeString(string(hexStr))
b, err := hex.DecodeString(strings.TrimSpace(string(hexStr)))
if err != nil {
return nil, errors.Wrap(err, "decode private key hex")
}
Expand Down
42 changes: 41 additions & 1 deletion app/k1util/k1util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package k1util_test
import (
"encoding/hex"
"math/rand"
"os"
"path"
"testing"

k1 "github.com/decred/dcrd/dcrec/secp256k1/v4"
Expand Down Expand Up @@ -40,7 +42,7 @@ const (
// digest,
// sig[:len(sig)-1])
// require.True(t, ok)
//}
// }

func TestK1Util(t *testing.T) {
key := k1.PrivKeyFromBytes(fromHex(t, privKey1))
Expand Down Expand Up @@ -92,6 +94,44 @@ func TestRandom(t *testing.T) {
require.True(t, key.PubKey().IsEqual(recovered))
}

func TestLoad(t *testing.T) {
key, err := k1.GeneratePrivateKey()
require.NoError(t, err)
filePath := path.Join(t.TempDir(), "charon-enr-private-key")

t.Run("nonexistent file", func(t *testing.T) {
_, err := k1util.Load("nonexistent-file")
require.ErrorContains(t, err, "read private key from disk")
})

t.Run("invalid hex encoded file", func(t *testing.T) {
invalidHexStr := "abcXYZ123" // Invalid hex string
err = os.WriteFile(filePath, []byte(invalidHexStr), 0o600)
require.NoError(t, err)

_, err := k1util.Load(filePath)
require.ErrorContains(t, err, "decode private key hex")
})

t.Run("valid hex strings", func(t *testing.T) {
hexStrs := []string{
hex.EncodeToString(key.Serialize()) + "\n", // Hex string ending with '\n'
hex.EncodeToString(key.Serialize()) + "\r\n", // Hex string ending with '\r\n'
hex.EncodeToString(key.Serialize()) + " ", // Hex string ending with a space
hex.EncodeToString(key.Serialize()), // Hex string
}

for _, hexStr := range hexStrs {
err = os.WriteFile(filePath, []byte(hexStr), 0o600)
require.NoError(t, err)

pkey, err := k1util.Load(filePath)
require.NoError(t, err)
require.Equal(t, key, pkey)
}
})
}

// BenchmarkRecoverVerify benchmarks recovery vs verification.
//
// TL;DR: verify is slightly faster than recover, both in the order of hundreds of microseconds.
Expand Down

0 comments on commit 9ede198

Please sign in to comment.