/
signedhead.go
188 lines (171 loc) · 4.51 KB
/
signedhead.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package head
import (
"bytes"
"errors"
"fmt"
"io"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagjson"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/bindnode"
ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
)
type SignedHead struct {
Head ipld.Link
Topic *string
Pubkey []byte
Sig []byte
}
var (
ErrBadSignature = errors.New("invalid signature")
ErrNoSignature = errors.New("missing signature")
ErrNoPubkey = errors.New("missing public key")
)
func NewSignedHead(headCid cid.Cid, topic string, privKey ic.PrivKey) (*SignedHead, error) {
sh := &SignedHead{
Head: ipld.Link(cidlink.Link{Cid: headCid}),
}
if topic != "" {
sh.Topic = &topic
}
err := sh.Sign(privKey)
if err != nil {
return nil, err
}
return sh, nil
}
func Decode(SignedHeadReader io.Reader) (*SignedHead, error) {
builder := SignedHeadPrototype.NewBuilder()
err := dagjson.Decode(builder, SignedHeadReader)
if err != nil {
return nil, err
}
return UnwrapSignedHead(builder.Build())
}
func (s SignedHead) Encode() ([]byte, error) {
node, err := s.ToNode()
if err != nil {
return nil, err
}
var buf bytes.Buffer
err = dagjson.Encode(node, &buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// ToNode converts this SignedHead to its representation as an IPLD typed node.
//
// See: bindnode.Wrap.
func (s SignedHead) ToNode() (n ipld.Node, err error) {
// TODO: remove the panic recovery once IPLD bindnode is stabilized.
defer func() {
if r := recover(); r != nil {
err = toError(r)
}
}()
n = bindnode.Wrap(&s, SignedHeadPrototype.Type()).Representation()
return
}
// UnwrapSignedHead unwraps the given node as a SignedHead.
//
// Note that the node is reassigned to SignedHeadPrototype if its prototype is
// different. Therefore, it is recommended to load the node using the correct
// prototype function initially to avoid unnecessary node assignment.
func UnwrapSignedHead(node ipld.Node) (*SignedHead, error) {
// When an IPLD node is loaded using `Prototype.Any` unwrap with bindnode
// will not work. Here we defensively check the prototype and wrap if
// needed.
//
// The code in this repo, however should load nodes with appropriate
// prototype and never trigger this if statement.
if node.Prototype() != SignedHeadPrototype {
headBuilder := SignedHeadPrototype.NewBuilder()
err := headBuilder.AssignNode(node)
if err != nil {
return nil, fmt.Errorf("faild to convert node prototype: %w", err)
}
node = headBuilder.Build()
}
head, ok := bindnode.Unwrap(node).(*SignedHead)
if !ok || head == nil {
return nil, fmt.Errorf("unwrapped node does not match schema.SignedHead")
}
return head, nil
}
// Validate checks the signature on the SignedHead and returns the peer ID of
// the signer.
//
// Even though the signature is valid with the included public key, the caller
// will need to check if the signer's ID is allowed to sign this message.
func (s SignedHead) Validate() (peer.ID, error) {
if len(s.Sig) == 0 {
return "", ErrNoSignature
}
if len(s.Pubkey) == 0 {
return "", ErrNoPubkey
}
pubKey, err := ic.UnmarshalPublicKey(s.Pubkey)
if err != nil {
return "", err
}
var sigBuf bytes.Buffer
cidBytes := s.Head.(cidlink.Link).Cid.Bytes()
var topicLen int
if s.Topic != nil {
topicLen = len(*s.Topic)
}
sigBuf.Grow(len(cidBytes) + topicLen)
sigBuf.Write(cidBytes)
if topicLen != 0 {
sigBuf.WriteString(*s.Topic)
}
ok, err := pubKey.Verify(sigBuf.Bytes(), s.Sig)
if err != nil {
return "", err
}
if !ok {
return "", ErrBadSignature
}
// Get peer ID of signer so that caller can check that it is allowed.
signerID, err := peer.IDFromPublicKey(pubKey)
if err != nil {
return "", fmt.Errorf("cannot convert public key to peer ID: %w", err)
}
return signerID, nil
}
func (s *SignedHead) Sign(privKey ic.PrivKey) error {
var sigBuf bytes.Buffer
cidBytes := s.Head.(cidlink.Link).Cid.Bytes()
var topicLen int
if s.Topic != nil {
topicLen = len(*s.Topic)
}
sigBuf.Grow(len(cidBytes) + topicLen)
sigBuf.Write(cidBytes)
if topicLen != 0 {
sigBuf.WriteString(*s.Topic)
}
var err error
s.Pubkey, err = ic.MarshalPublicKey(privKey.GetPublic())
if err != nil {
return err
}
s.Sig, err = privKey.Sign(sigBuf.Bytes())
if err != nil {
return err
}
return nil
}
func toError(r any) error {
switch x := r.(type) {
case string:
return errors.New(x)
case error:
return x
default:
return fmt.Errorf("unknown panic: %v", r)
}
}