Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make errors faithful #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
avoid computing new v4 keys in case of error
  • Loading branch information
XaviFP committed Jul 13, 2022
commit 6a934f97711b167dd91c119ed02a422dcc0594a7
40 changes: 16 additions & 24 deletions v4_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ func NewV4AsymmetricPublicKeyFromHex(hexEncoded string) (V4AsymmetricPublicKey,
publicKey, err := hex.DecodeString(hexEncoded)

if err != nil {
// even though we return error, return a random key here rather than
// a nil key
return NewV4AsymmetricSecretKey().Public(), err

return V4AsymmetricPublicKey{}, err
}

return NewV4AsymmetricPublicKeyFromBytes(publicKey)
Expand All @@ -30,9 +29,8 @@ func NewV4AsymmetricPublicKeyFromHex(hexEncoded string) (V4AsymmetricPublicKey,
// NewV4AsymmetricPublicKeyFromBytes Construct a v4 public key from bytes
func NewV4AsymmetricPublicKeyFromBytes(publicKey []byte) (V4AsymmetricPublicKey, error) {
if len(publicKey) != 32 {
// even though we return error, return a random key here rather than
// a nil key
return NewV4AsymmetricSecretKey().Public(), errors.New("Key incorrect length")

return V4AsymmetricPublicKey{}, errors.New("Key incorrect length")
}

return V4AsymmetricPublicKey{publicKey}, nil
Expand Down Expand Up @@ -97,9 +95,8 @@ func NewV4AsymmetricSecretKeyFromHex(hexEncoded string) (V4AsymmetricSecretKey,
privateKey, err := hex.DecodeString(hexEncoded)

if err != nil {
// even though we return error, return a random key here rather than
// a nil key
return NewV4AsymmetricSecretKey(), err

return V4AsymmetricSecretKey{}, err
}

return NewV4AsymmetricSecretKeyFromBytes(privateKey)
Expand All @@ -108,9 +105,8 @@ func NewV4AsymmetricSecretKeyFromHex(hexEncoded string) (V4AsymmetricSecretKey,
// NewV4AsymmetricSecretKeyFromBytes creates a secret key from bytes
func NewV4AsymmetricSecretKeyFromBytes(privateKey []byte) (V4AsymmetricSecretKey, error) {
if len(privateKey) != 64 {
// even though we return error, return a random key here rather than
// a nil key
return NewV4AsymmetricSecretKey(), errors.New("Key incorrect length")

return V4AsymmetricSecretKey{}, errors.New("Key incorrect length")
}

return V4AsymmetricSecretKey{privateKey}, nil
Expand All @@ -121,15 +117,13 @@ func NewV4AsymmetricSecretKeyFromSeed(hexEncoded string) (V4AsymmetricSecretKey,
seedBytes, err := hex.DecodeString(hexEncoded)

if err != nil {
// even though we return error, return a random key here rather than
// a nil key
return NewV4AsymmetricSecretKey(), err

return V4AsymmetricSecretKey{}, err
}

if len(seedBytes) != 32 {
// even though we return error, return a random key here rather than
// a nil key
return NewV4AsymmetricSecretKey(), errors.New("Key incorrect length")

return V4AsymmetricSecretKey{}, errors.New("Key incorrect length")
}

return V4AsymmetricSecretKey{ed25519.NewKeyFromSeed(seedBytes)}, nil
Expand Down Expand Up @@ -163,9 +157,8 @@ func V4SymmetricKeyFromHex(hexEncoded string) (V4SymmetricKey, error) {
bytes, err := hex.DecodeString(hexEncoded)

if err != nil {
// even though we return error, return a random key here rather than
// a nil key
return NewV4SymmetricKey(), err

return V4SymmetricKey{}, err
}

return V4SymmetricKeyFromBytes(bytes)
Expand All @@ -174,9 +167,8 @@ func V4SymmetricKeyFromHex(hexEncoded string) (V4SymmetricKey, error) {
// V4SymmetricKeyFromBytes constructs a key from bytes
func V4SymmetricKeyFromBytes(bytes []byte) (V4SymmetricKey, error) {
if len(bytes) != 32 {
// even though we return error, return a random key here rather than
// a nil key
return NewV4SymmetricKey(), errors.New("Key incorrect length")

return V4SymmetricKey{}, errors.New("Key incorrect length")
}

var material [32]byte
Expand Down