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

chore: Infer key type from public key and private key #535

Merged
merged 6 commits into from
Feb 3, 2020

Conversation

olimpias
Copy link
Contributor

@olimpias olimpias commented Feb 2, 2020

Solves #403

if err != nil {
continue
}
matches = append(matches, cPrivateKey)
Copy link
Contributor

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)

// 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 {
Copy link
Contributor

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) {
Copy link
Contributor

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)

if err != nil {
return nil, err
}
if bytes.Equal(privateKey.PublicKey().Bytes(), publicKey) {
Copy link
Contributor

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)

if err != nil {
return nil, err
}
if bytes.Equal(privateKey.PublicKey().Bytes(), publicKey) {
Copy link
Contributor

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
Copy link
Contributor

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)

}
}

func verifyPrivateAndPublicKey(publicKey []byte, privateKey []byte, kind string) (crypto.PrivateKey, error) {
Copy link
Contributor

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)

ErrKindNotFound = errors.New("key kind is not found")

// Possible key kinds for now
possibleKeyKinds = []string{crypto.KindED25519, crypto.KindSECP256K1, crypto.KindSR25519}
Copy link
Contributor

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)

@@ -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) {
Copy link
Contributor

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)

switch len(matches) {
case 0:
return nil, ErrNoMatch
case 1:
Copy link
Contributor

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)

ErrKindNotFound = errors.New("key kind is not found")

// PossibleKeyKinds current possible key kinds
PossibleKeyKinds = []string{crypto.KindED25519, crypto.KindSECP256K1, crypto.KindSR25519}
Copy link
Contributor

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)

switch len(matches) {
case 0:
return nil, ErrNoMatch
case 1: //nolint:gomnd
Copy link
Contributor

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)

Copy link
Member

@robdefeo robdefeo left a 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 💯

"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)
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

@@ -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) {
Copy link
Member

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.

Comment on lines +181 to +244
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,
},
}
Copy link
Member

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 💯

@@ -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) {
Copy link
Member

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.

func TestRemoveDuplicates(t *testing.T) {
assert := assert.New(t)
assertion := assert.New(t)
Copy link
Member

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.

Copy link
Member

@robdefeo robdefeo left a 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 💯

Copy link
Member

@robdefeo robdefeo left a 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 👍

@robdefeo robdefeo merged commit 5809404 into mailchain:master Feb 3, 2020
@olimpias
Copy link
Contributor Author

olimpias commented Feb 3, 2020

Thank you for your review :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants