-
Notifications
You must be signed in to change notification settings - Fork 212
/
verifier.go
45 lines (37 loc) · 1.06 KB
/
verifier.go
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package signing
import (
"crypto/ed25519"
"github.com/spacemeshos/go-spacemesh/common/types"
)
type edVerifierOption struct {
prefix []byte
}
// VerifierOptionFunc to modify verifier.
type VerifierOptionFunc func(*edVerifierOption)
// WithVerifierPrefix sets the prefix used by PubKeyVerifier. This usually is the Network ID.
func WithVerifierPrefix(prefix []byte) VerifierOptionFunc {
return func(opts *edVerifierOption) {
opts.prefix = prefix
}
}
// EdVerifier extracts public keys from signatures.
type EdVerifier struct {
prefix []byte
}
func NewEdVerifier(opts ...VerifierOptionFunc) *EdVerifier {
cfg := &edVerifierOption{}
for _, opt := range opts {
opt(cfg)
}
return &EdVerifier{
prefix: cfg.prefix,
}
}
// Verify verifies that a signature matches public key and message.
func (es *EdVerifier) Verify(d Domain, nodeID types.NodeID, m []byte, sig types.EdSignature) bool {
msg := make([]byte, 0, len(es.prefix)+1+len(m))
msg = append(msg, es.prefix...)
msg = append(msg, byte(d))
msg = append(msg, m...)
return ed25519.Verify(nodeID[:], msg, sig[:])
}