This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
feat: refactor key interface #34
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,10 +59,17 @@ var PrivKeyUnmarshallers = map[pb.KeyType]PrivKeyUnmarshaller{ | |
// Key represents a crypto key that can be compared to another key | ||
type Key interface { | ||
// Bytes returns a serialized, storeable representation of this key | ||
// DEPRECATED in favor of Marshal / Unmarshal | ||
Bytes() ([]byte, error) | ||
|
||
// Equals checks whether two PubKeys are the same | ||
Equals(Key) bool | ||
|
||
// Raw returns the raw bytes of the key. | ||
Raw() ([]byte, error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this is the inverse of the |
||
|
||
// Type returns the protobof key type. | ||
Type() pb.KeyType | ||
} | ||
|
||
// PrivKey represents a private key that can be used to generate a public key, | ||
|
@@ -261,7 +268,15 @@ func UnmarshalPublicKey(data []byte) (PubKey, error) { | |
// MarshalPublicKey converts a public key object into a protobuf serialized | ||
// public key | ||
func MarshalPublicKey(k PubKey) ([]byte, error) { | ||
return k.Bytes() | ||
pbmes := new(pb.PublicKey) | ||
pbmes.Type = k.Type().Enum() | ||
data, err := k.Raw() | ||
if err != nil { | ||
return nil, err | ||
} | ||
pbmes.Data = data | ||
|
||
return proto.Marshal(pbmes) | ||
} | ||
|
||
// UnmarshalPrivateKey converts a protobuf serialized private key into its | ||
|
@@ -283,17 +298,15 @@ func UnmarshalPrivateKey(data []byte) (PrivKey, error) { | |
|
||
// MarshalPrivateKey converts a key object into its protobuf serialized form. | ||
func MarshalPrivateKey(k PrivKey) ([]byte, error) { | ||
|
||
switch k.(type) { | ||
case *Ed25519PrivateKey: | ||
return k.Bytes() | ||
case *RsaPrivateKey: | ||
return k.Bytes() | ||
case *Secp256k1PrivateKey: | ||
return k.Bytes() | ||
default: | ||
return nil, ErrBadKeyType | ||
pbmes := new(pb.PrivateKey) | ||
pbmes.Type = k.Type().Enum() | ||
data, err := k.Raw() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pbmes.Data = data | ||
return proto.Marshal(pbmes) | ||
} | ||
|
||
// ConfigDecodeKey decodes from b64 (for config file), and unmarshals. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should use
k
as the receiver.