Skip to content

Commit

Permalink
Merge pull request #78 from make-software/fix/public-key-interface
Browse files Browse the repository at this point in the history
Fixed interaction with public key API
  • Loading branch information
Volodymyr-Kuchinskyi authored Feb 28, 2024
2 parents 4781926 + 29fe0ee commit 7f7e952
Showing 1 changed file with 17 additions and 0 deletions.
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

0 comments on commit 7f7e952

Please sign in to comment.