-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
chore: Infer key type from public key and private key #535
Conversation
if err != nil { | ||
continue | ||
} | ||
matches = append(matches, cPrivateKey) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
append only allowed to cuddle with appended value (from wsl
)
crypto/multikey/kind.go
Outdated
// Supported private key types are defined in possibleKeyKinds variable. | ||
func GetKeyKindFromBytes(publicKey []byte, privateKey []byte) (crypto.PrivateKey, error) { | ||
matches := make([]crypto.PrivateKey, 0, 1) | ||
for _, keyKind := range possibleKeyKinds { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ranges should only be cuddled with assignments used in the iteration (from wsl
)
} | ||
matches = append(matches, cPrivateKey) | ||
} | ||
switch len(matches) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only one cuddle assignment allowed before switch statement (from wsl
)
crypto/multikey/kind.go
Outdated
if err != nil { | ||
return nil, err | ||
} | ||
if bytes.Equal(privateKey.PublicKey().Bytes(), publicKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if statements should only be cuddled with assignments (from wsl
)
crypto/multikey/kind.go
Outdated
if err != nil { | ||
return nil, err | ||
} | ||
if bytes.Equal(privateKey.PublicKey().Bytes(), publicKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if statements should only be cuddled with assignments (from wsl
)
default: | ||
return nil, ErrKindNotFound | ||
} | ||
return nil, errPrivateAndPublicKeyNotMatched |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return statements should not be cuddled if block has more than two lines (from wsl
)
crypto/multikey/kind.go
Outdated
} | ||
} | ||
|
||
func verifyPrivateAndPublicKey(publicKey []byte, privateKey []byte, kind string) (crypto.PrivateKey, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cyclomatic complexity 11 of func verifyPrivateAndPublicKey
is high (> 10) (from gocyclo
)
crypto/multikey/kind.go
Outdated
ErrKindNotFound = errors.New("key kind is not found") | ||
|
||
// Possible key kinds for now | ||
possibleKeyKinds = []string{crypto.KindED25519, crypto.KindSECP256K1, crypto.KindSR25519} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
possibleKeyKinds
is a global variable (from gochecknoglobals
)
crypto/multikey/kind.go
Outdated
@@ -56,6 +70,59 @@ func KeyKindFromSignature(pubKey, message, sig []byte, keyKinds []string) (crypt | |||
} | |||
} | |||
|
|||
// GetKeyKindFromBytes extracts the private key type from the publicKey and privateKey. | |||
// Supported private key types are defined in possibleKeyKinds variable. | |||
func GetKeyKindFromBytes(publicKey []byte, privateKey []byte) (crypto.PrivateKey, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
paramTypeCombine: func(publicKey []byte, privateKey []byte) (crypto.PrivateKey, error) could be replaced with func(publicKey, privateKey []byte) (crypto.PrivateKey, error) (from gocritic
)
crypto/multikey/kind.go
Outdated
switch len(matches) { | ||
case 0: | ||
return nil, ErrNoMatch | ||
case 1: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mnd: Magic number: 1, in detected (from gomnd
)
crypto/multikey/kind.go
Outdated
ErrKindNotFound = errors.New("key kind is not found") | ||
|
||
// PossibleKeyKinds current possible key kinds | ||
PossibleKeyKinds = []string{crypto.KindED25519, crypto.KindSECP256K1, crypto.KindSR25519} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PossibleKeyKinds
is a global variable (from gochecknoglobals
)
crypto/multikey/kind.go
Outdated
switch len(matches) { | ||
case 0: | ||
return nil, ErrNoMatch | ||
case 1: //nolint:gomnd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whyNoLint: include an explanation for nolint directive (from gocritic
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @olimpias for the PR it looks great 😄 I have requested just a couple of small changes around this tests. Then it will be ready to merge.
Awesome with the depth of the tests too 💯
crypto/multikey/kind_test.go
Outdated
"github.com/mailchain/mailchain/crypto" | ||
"github.com/mailchain/mailchain/crypto/ed25519/ed25519test" | ||
"github.com/mailchain/mailchain/crypto/secp256k1/secp256k1test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestKeyKindFromSignature(t *testing.T) { | ||
assert := assert.New(t) | ||
assertion := assert.New(t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have come to realise that assert := assert.New(t)
is not a good approach. For two reasons I can see now, the first is what you have addressed; changing what assert
does part the want through a function. Second is that it gives less information about the test case that failed. Can you remove this line and instead add t
to the assert calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure
crypto/multikey/kind_test.go
Outdated
@@ -156,15 +158,108 @@ func TestKeyKindFromSignature(t *testing.T) { | |||
t.Errorf("KeyKindFromSignature() err = %v, want %v", err, tt.wantErr) | |||
} | |||
|
|||
if !assert.Equal(key, tt.wantKey) { | |||
if !assertion.Equal(key, tt.wantKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you change to if !assert.Equal(t, key, tt.wantKey) {
this will give more information about what test case failed.
name: "success-ed25519-sofia-key", | ||
args: args{ | ||
publicKey: ed25519test.SofiaPublicKey.Bytes(), | ||
privateKey: ed25519test.SofiaPrivateKey.Bytes(), | ||
}, | ||
wantKey: ed25519test.SofiaPrivateKey, | ||
}, | ||
{ | ||
name: "success-ed25519-charlotte-key", | ||
args: args{ | ||
publicKey: ed25519test.CharlottePublicKey.Bytes(), | ||
privateKey: ed25519test.CharlottePrivateKey.Bytes(), | ||
}, | ||
wantKey: ed25519test.CharlottePrivateKey, | ||
}, | ||
{ | ||
name: "success-secp256k1-sofia-key", | ||
args: args{ | ||
publicKey: secp256k1test.SofiaPublicKey.Bytes(), | ||
privateKey: secp256k1test.SofiaPrivateKey.Bytes(), | ||
}, | ||
wantKey: secp256k1test.SofiaPrivateKey, | ||
}, | ||
{ | ||
name: "success-secp256k1-charlotte-key", | ||
args: args{ | ||
publicKey: secp256k1test.CharlottePublicKey.Bytes(), | ||
privateKey: secp256k1test.CharlottePrivateKey.Bytes(), | ||
}, | ||
wantKey: secp256k1test.CharlottePrivateKey, | ||
}, | ||
{ | ||
name: "success-sr25519-sofia-key", | ||
args: args{ | ||
publicKey: sr25519test.SofiaPublicKey.Bytes(), | ||
privateKey: sr25519test.SofiaPrivateKey.Bytes(), | ||
}, | ||
wantKey: sr25519test.SofiaPrivateKey, | ||
}, | ||
{ | ||
name: "success-sr25519-charlotte-key", | ||
args: args{ | ||
publicKey: sr25519test.CharlottePublicKey.Bytes(), | ||
privateKey: sr25519test.CharlottePrivateKey.Bytes(), | ||
}, | ||
wantKey: sr25519test.CharlottePrivateKey, | ||
}, | ||
{ | ||
name: "err-invalid-public-key-bytes", | ||
args: args{ | ||
publicKey: []byte{0x1}, | ||
privateKey: sr25519test.CharlottePrivateKey.Bytes(), | ||
}, | ||
wantErr: ErrNoMatch, | ||
}, | ||
{ | ||
name: "err-no-key-kinds", | ||
args: args{ | ||
publicKey: []byte{0x1}, | ||
privateKey: []byte{0x2}, | ||
}, | ||
wantErr: ErrNoMatch, | ||
}, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great 😄 very through 💯
crypto/multikey/kind_test.go
Outdated
@@ -193,7 +288,7 @@ func TestRemoveDuplicates(t *testing.T) { | |||
for _, tt := range tests { | |||
t.Run("remove-duplicates", func(t *testing.T) { | |||
got := removeDuplicates(tt.in) | |||
if !assert.Equal(tt.want, got) { | |||
if !assertion.Equal(tt.want, got) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you change to if !assert.Equal(t, tt.want, got) { this will give more information about what test case failed.
crypto/multikey/kind_test.go
Outdated
func TestRemoveDuplicates(t *testing.T) { | ||
assert := assert.New(t) | ||
assertion := assert.New(t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have come to realise that assert := assert.New(t)
is not a good approach. For two reasons I can see now, the first is what you have addressed; changing what assert does part the want through a function. Second is that it gives less information about the test case that failed. Can you remove this line and instead add t to the assert calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @olimpias for the PR it looks great 😄 I have requested just a couple of small changes around this tests. There is one issue from https://golangci.com/r/github.com/mailchain/mailchain/pulls/535. Then it will be ready to merge.
Awesome with the depth of the tests too 💯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @olimpias for the update to the PR 😄 this is good to merge 👍
Thank you for your review :). |
Solves #403