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
Next Next commit
avoid computing new v2 keys in case of error
  • Loading branch information
XaviFP committed Jul 13, 2022
commit 9c2338f754dd19195585d38f03959df52c1e86c5
40 changes: 16 additions & 24 deletions v2_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ type V2AsymmetricPublicKey struct {
func NewV2AsymmetricPublicKeyFromHex(hexEncoded string) (V2AsymmetricPublicKey, error) {
publicKey, err := hex.DecodeString(hexEncoded)
if err != nil {
// even though we return error, return a random key here rather than
// a nil key
return NewV2AsymmetricSecretKey().Public(), err

return V2AsymmetricPublicKey{}, err
}

return NewV2AsymmetricPublicKeyFromBytes(publicKey)
Expand All @@ -28,9 +27,8 @@ func NewV2AsymmetricPublicKeyFromHex(hexEncoded string) (V2AsymmetricPublicKey,
// NewV2AsymmetricPublicKeyFromBytes Construct a v2 public key from bytes
func NewV2AsymmetricPublicKeyFromBytes(publicKey []byte) (V2AsymmetricPublicKey, error) {
if len(publicKey) != 32 {
// even though we return error, return a random key here rather than
// a nil key
return NewV2AsymmetricSecretKey().Public(), errors.New("Key incorrect length")

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

return V2AsymmetricPublicKey{publicKey}, nil
Expand Down Expand Up @@ -95,9 +93,8 @@ func NewV2AsymmetricSecretKeyFromHex(hexEncoded string) (V2AsymmetricSecretKey,
privateKey, err := hex.DecodeString(hexEncoded)

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

return V2AsymmetricSecretKey{}, err
}

return NewV2AsymmetricSecretKeyFromBytes(privateKey)
Expand All @@ -106,9 +103,8 @@ func NewV2AsymmetricSecretKeyFromHex(hexEncoded string) (V2AsymmetricSecretKey,
// NewV2AsymmetricSecretKeyFromBytes creates a secret key from bytes
func NewV2AsymmetricSecretKeyFromBytes(privateKey []byte) (V2AsymmetricSecretKey, error) {
if len(privateKey) != 64 {
// even though we return error, return a random key here rather than
// a nil key
return NewV2AsymmetricSecretKey(), errors.New("Key incorrect length")

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

return V2AsymmetricSecretKey{privateKey}, nil
Expand All @@ -119,15 +115,13 @@ func NewV2AsymmetricSecretKeyFromSeed(hexEncoded string) (V2AsymmetricSecretKey,
seedBytes, err := hex.DecodeString(hexEncoded)

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

return V2AsymmetricSecretKey{}, err
}

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

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

return V2AsymmetricSecretKey{ed25519.NewKeyFromSeed(seedBytes)}, nil
Expand Down Expand Up @@ -160,9 +154,8 @@ func (k V2SymmetricKey) ExportBytes() []byte {
func V2SymmetricKeyFromHex(hexEncoded string) (V2SymmetricKey, 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 NewV2SymmetricKey(), err

return V2SymmetricKey{}, err
}

return V2SymmetricKeyFromBytes(bytes)
Expand All @@ -171,9 +164,8 @@ func V2SymmetricKeyFromHex(hexEncoded string) (V2SymmetricKey, error) {
// V2SymmetricKeyFromBytes constructs a key from bytes
func V2SymmetricKeyFromBytes(bytes []byte) (V2SymmetricKey, error) {
if len(bytes) != 32 {
// even though we return error, return a random key here rather than
// a nil key
return NewV2SymmetricKey(), errors.New("Key incorrect length")

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

var material [32]byte
Expand Down