-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.go
65 lines (52 loc) · 1.08 KB
/
header.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
package peppersource
import (
"encoding/json"
crypto "github.com/libp2p/go-libp2p-crypto"
)
type Head struct {
Payload Payload
Signature []byte
}
type Payload struct {
Metadata string
Hash string
}
// NewHead returns a new Head object which contains sign(metadata, hash)
func NewHead(meta []byte, hash string, pk crypto.PrivKey) (Head, error) {
var h Head
p := Payload{
Metadata: string(meta),
Hash: hash,
}
pb, err := json.Marshal(p)
if err != nil {
return h, err
}
s, err := pk.Sign(pb)
if err != nil {
return h, err
}
h.Payload = p
h.Signature = s
return h, nil
}
func (h Head) Hash() string {
return h.Payload.Hash
}
func (h Head) Metadata() []byte {
return []byte(h.Payload.Metadata)
}
// verifyHead receives a byte encoded Head and verifies if Head was signed by
// the expected entity
func verifyHead(b []byte, pubk crypto.PubKey) (bool, error) {
var h Head
err := json.Unmarshal(b, &h)
if err != nil {
return false, err
}
pb, err := json.Marshal(h.Payload)
if err != nil {
return false, err
}
return pubk.Verify(pb, h.Signature)
}