-
Notifications
You must be signed in to change notification settings - Fork 20.2k
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
crypto: replace ToECDSAPub with error-checking func UnmarshalPubkey #16932
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,8 @@ var ( | |
secp256k1halfN = new(big.Int).Div(secp256k1N, big.NewInt(2)) | ||
) | ||
|
||
var errInvalidPubkey = errors.New("invalid secp256k1 public key") | ||
|
||
// Keccak256 calculates and returns the Keccak256 hash of the input data. | ||
func Keccak256(data ...[]byte) []byte { | ||
d := sha3.NewKeccak256() | ||
|
@@ -122,12 +124,13 @@ func FromECDSA(priv *ecdsa.PrivateKey) []byte { | |
return math.PaddedBigBytes(priv.D, priv.Params().BitSize/8) | ||
} | ||
|
||
func ToECDSAPub(pub []byte) *ecdsa.PublicKey { | ||
if len(pub) == 0 { | ||
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. There seems to be no unit test that covers the case when |
||
return nil | ||
} | ||
// UnmarshalPubkey converts bytes to a secp256k1 public key. | ||
func UnmarshalPubkey(pub []byte) (*ecdsa.PublicKey, error) { | ||
x, y := elliptic.Unmarshal(S256(), pub) | ||
return &ecdsa.PublicKey{Curve: S256(), X: x, Y: y} | ||
if x == nil { | ||
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. I would also check that 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. package elliptic is a standard library package and returning nil on failure is a known shortcoming of that API. That's one of the reasons we have the wrapper in the first place. |
||
return nil, errInvalidPubkey | ||
} | ||
return &ecdsa.PublicKey{Curve: S256(), X: x, Y: y}, nil | ||
} | ||
|
||
func FromECDSAPub(pub *ecdsa.PublicKey) []byte { | ||
|
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 would make it exportable, so that whoever calls it can compare the error, like:
I'm aware you can still
String()
-compare, but it's nice to keep the type info as well.