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

Fixed interaction with public key API #78

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions types/keypair/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
ErrEmptySignature = errors.New("empty signature")
ErrInvalidPublicKeyAlgo = errors.New("invalid public key algorithm")
ErrInvalidSignature = errors.New("invalid signature")
ErrEmptyPublicKey = errors.New("empty public key")
)

type PublicKeyInternal interface {
Expand All @@ -32,6 +33,10 @@ type PublicKey struct {
}

func (v PublicKey) Bytes() []byte {
if v.key == nil {
return nil
}

return append([]byte{byte(v.cryptoAlg)}, v.key.Bytes()...)
}

Expand Down Expand Up @@ -79,6 +84,10 @@ func (v PublicKey) GobEncode() ([]byte, error) {
}

func (v PublicKey) AccountHash() key.AccountHash {
if v.key == nil {
return key.AccountHash{}
}

bytesToHash := make([]byte, 0, len(v.cryptoAlg.String())+1+len(v.key.Bytes()))

bytesToHash = append(bytesToHash, []byte(strings.ToLower(v.cryptoAlg.String()))...)
Expand Down Expand Up @@ -122,6 +131,10 @@ func (v PublicKey) VerifySignature(message []byte, sig []byte) error {
return ErrEmptySignature
}

if v.key == nil {
return ErrEmptyPublicKey
}

// Trim first byte with algorithm data
sig = sig[1:]

Expand All @@ -135,6 +148,10 @@ func (v PublicKey) VerifySignature(message []byte, sig []byte) error {
// VerifyRawSignature verifies message using raw signature
// Deprecated: won't work with Casper node, use VerifySignature method to achieve compatibility
func (v PublicKey) VerifyRawSignature(message []byte, sig []byte) error {
if v.key == nil {
return ErrEmptyPublicKey
}

if v.key.VerifySignature(message, sig) {
return nil
}
Expand Down
Loading