aboutsummaryrefslogtreecommitdiff
path: root/key/pub.go
blob: a9e63900a06bae1495b8834f07af6147c19d9304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package key

import "golang.org/x/crypto/ed25519"

type Pub struct {
	PKAlg  [2]byte
	KeyNum [8]byte
	Key    [ed25519.PublicKeySize]byte
}

func (v *Pub) Validate() error {
	if v.PKAlg != pkAlg {
		return ErrInvalidPK
	}
	return nil
}

func (v *Pub) MarshalBinary() ([]byte, error)    { return Marshal(v) }
func (v *Pub) UnmarshalBinary(data []byte) error { return Unmarshal(data, v) }

// NewPub returns a parsed public key
func NewPub(data []byte) (*Pub, error) {
	pub := new(Pub)
	if err := pub.UnmarshalBinary(data); err != nil {
		return nil, err
	}
	return pub, pub.Validate()
}