Skip to content

Commit

Permalink
Merge pull request #2156 from ffranr/add-pubkey-serialized-type
Browse files Browse the repository at this point in the history
Add new serialized pubkey type
  • Loading branch information
Roasbeef authored Apr 10, 2024
2 parents 92b24d2 + 665eeb5 commit e4b32e0
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions btcec/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

// These constants define the lengths of serialized public keys.
const (
// PubKeyBytesLenCompressed is the bytes length of a serialized compressed
// public key.
PubKeyBytesLenCompressed = 33
)

Expand Down Expand Up @@ -49,3 +51,38 @@ type PublicKey = secp.PublicKey
func NewPublicKey(x, y *FieldVal) *PublicKey {
return secp.NewPublicKey(x, y)
}

// SerializedKey is a type for representing a public key in its compressed
// serialized form.
//
// NOTE: This type is useful when using public keys as keys in maps.
type SerializedKey [PubKeyBytesLenCompressed]byte

// ToPubKey returns the public key parsed from the serialized key.
func (s SerializedKey) ToPubKey() (*PublicKey, error) {
return ParsePubKey(s[:])
}

// SchnorrSerialized returns the Schnorr serialized, x-only 32-byte
// representation of the serialized key.
func (s SerializedKey) SchnorrSerialized() [32]byte {
var serializedSchnorr [32]byte
copy(serializedSchnorr[:], s[1:])
return serializedSchnorr
}

// CopyBytes returns a copy of the underlying array as a byte slice.
func (s SerializedKey) CopyBytes() []byte {
c := make([]byte, PubKeyBytesLenCompressed)
copy(c, s[:])

return c
}

// ToSerialized serializes a public key into its compressed form.
func ToSerialized(pubKey *PublicKey) SerializedKey {
var serialized SerializedKey
copy(serialized[:], pubKey.SerializeCompressed())

return serialized
}

0 comments on commit e4b32e0

Please sign in to comment.