-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsignature.go
80 lines (64 loc) · 1.6 KB
/
signature.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package xnyss
import (
wotsp "github.com/Re0h/xnyss/wotsp256"
"crypto/sha256"
"errors"
"bytes"
)
var (
ErrInvalidSigEncoding = errors.New("invalid signature encoding")
ErrSigMsgNotSet = errors.New("signature message is not set")
)
type Signature struct {
PubSeed []byte
Message []byte
ChildHashes [][]byte
SigBytes []byte
}
func NewSignature(sigBytes, msg []byte) (sig *Signature, err error) {
if len(sigBytes) < wotsp.SigLen+32 || (len(sigBytes) - (wotsp.SigLen+32)) % 32 != 0 {
err = ErrInvalidSigEncoding
return
}
sig = &Signature{
SigBytes: make([]byte, wotsp.SigLen),
PubSeed: make([]byte, 32),
Message: make([]byte, 32),
}
copy(sig.Message, msg)
copy(sig.SigBytes, sigBytes)
copy(sig.PubSeed, sigBytes[wotsp.SigLen:])
childBytes := sigBytes[wotsp.SigLen+32:]
if len(childBytes) > 0 {
sig.ChildHashes = make([][]byte, len(childBytes) / 32)
for i := range sig.ChildHashes {
sig.ChildHashes[i] = make([]byte, 32)
copy(sig.ChildHashes[i], childBytes[i*32:])
}
}
return
}
func (sig *Signature) PublicKey() ([]byte, error) {
if len(sig.Message) == 0 {
return nil, ErrSigMsgNotSet
}
s := sha256.New()
s.Write(sig.Message)
if sig.ChildHashes != nil {
for i := range sig.ChildHashes {
s.Write(sig.ChildHashes[i])
}
}
return wotsp.PkFromSig(sig.SigBytes, s.Sum(nil), sig.PubSeed, &wotsp.Address{}), nil
}
func (sig *Signature) Bytes() []byte {
buf := &bytes.Buffer{}
buf.Write(sig.SigBytes)
buf.Write(sig.PubSeed)
if sig.ChildHashes != nil {
for i := range sig.ChildHashes {
buf.Write(sig.ChildHashes[i])
}
}
return buf.Bytes()
}