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

Add Keys package #54

Merged
merged 28 commits into from
Jul 8, 2020
Merged

Add Keys package #54

merged 28 commits into from
Jul 8, 2020

Conversation

juliankoh
Copy link
Contributor

@juliankoh juliankoh commented Jun 26, 2020

Motivation

Added a keys package to rosetta-sdk-go, which includes keygen, verify, and sign methods for Secp256k1 and Edwards25519 curves. This will be primarily used within tests for implementations of the Construction API, where we can sign & verify the constructed transactions.

Solution

Open questions

@patrick-ogrady
Copy link
Contributor

As we discussed, rebasing on #55 will remove the need for redefining all the CurveType and SignatureType in this package.

keys/keys.go Outdated Show resolved Hide resolved
keys/keys.go Outdated Show resolved Hide resolved
keys/keys_test.go Outdated Show resolved Hide resolved
keys/signing.go Outdated Show resolved Hide resolved
keys/types.go Outdated Show resolved Hide resolved
keys/signing_test.go Outdated Show resolved Hide resolved
keys/signing_test.go Outdated Show resolved Hide resolved
keys/signing.go Outdated Show resolved Hide resolved
keys/keys_test.go Outdated Show resolved Hide resolved
@juliankoh juliankoh requested a review from jakecraige July 2, 2020 13:44
keys/keys.go Outdated Show resolved Hide resolved
keys/signer_ed25519.go Outdated Show resolved Hide resolved
keys/signer_ed25519_test.go Outdated Show resolved Hide resolved
keys/signer_ed25519_test.go Outdated Show resolved Hide resolved
keys/signer_ed25519.go Outdated Show resolved Hide resolved
keys/types.go Outdated Show resolved Hide resolved
keys/types.go Outdated Show resolved Hide resolved
keys/types.go Outdated Show resolved Hide resolved
keys/keys_test.go Outdated Show resolved Hide resolved
keys/keys_test.go Outdated Show resolved Hide resolved
}
privKeyBytes := s.KeyPair.PrivateKey.Bytes

if payload.SignatureType != sigType {
Copy link
Contributor

@patrick-ogrady patrick-ogrady Jul 8, 2020

Choose a reason for hiding this comment

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

this will error if a sigType is passed in when payload.SignatureType is not populated. You should use the same logic as here: https://github.com/coinbase/rosetta-sdk-go/pull/54/files#diff-e27a53bd15dfa9595612a921778dd566R43-R45

assert.Equal(t, keypair.PrivateKey.CurveType, curve)

privKeyJSON, _ := json.Marshal(keypair.PrivateKey)
var privKey PrivateKey
Copy link
Contributor

@patrick-ogrady patrick-ogrady Jul 8, 2020

Choose a reason for hiding this comment

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

just to make sure hex marshaling is working, I'd add this test for both the secp256k1 and edwards25519 keypair:

Suggested change
var privKey PrivateKey
// Simple Hex Check
simpleType := struct {
HexBytes string `json:"hex_bytes"`
}{}
err = json.Unmarshal(j, &simpleType)
assert.NoError(t, err)
b, err := hex.DecodeString(simpleType.HexBytes)
assert.NoError(t, err)
assert.Equal(t, keypair.PrivateKey.Bytes, b)

var privKey PrivateKey
err = json.Unmarshal(privKeyJSON, &privKey)
assert.NoError(t, err)
assert.Equal(t, privKey.Bytes, keypair.PrivateKey.Bytes)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: expected should go first:

Suggested change
assert.Equal(t, privKey.Bytes, keypair.PrivateKey.Bytes)
assert.Equal(t, keypair.PrivateKey.Bytes, privKey.Bytes)

var privKey PrivateKey
err = json.Unmarshal(privKeyJSON, &privKey)
assert.NoError(t, err)
assert.Equal(t, privKey.Bytes, keypair.PrivateKey.Bytes)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: expected should go first:

Suggested change
assert.Equal(t, privKey.Bytes, keypair.PrivateKey.Bytes)
assert.Equal(t, keypair.PrivateKey.Bytes, privKey.Bytes)

keys/keys.go Outdated
"github.com/btcsuite/btcd/btcec"
)

// Currently all suppported privkeys are 32-bytes
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: comment should start with PrivKeyBytesLen

@patrick-ogrady
Copy link
Contributor

Make sure to add the keys package here so all tests run correctly: https://github.com/coinbase/rosetta-sdk-go/blob/cd4a060d72c34ce5391198e65f3bdb42890fa7dd/Makefile#L14-L15

You should also add a line item to the README

assert.Equal(t, privKey.Bytes, keypair.PrivateKey.Bytes)
}

func TestGenerateKeypairEd25519(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this should be TestGenerateKeypairEdwards25519

assert.NoError(t, err)

// Simple Hex Check
simpleType := struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

if err != nil {
return nil, fmt.Errorf("sign: unable to sign. %w", err)
}
sig = sig[:64]
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use a constant for all references of 64

patrick-ogrady
patrick-ogrady previously approved these changes Jul 8, 2020
@patrick-ogrady patrick-ogrady merged commit 3809aa1 into coinbase:master Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants