-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.go
246 lines (203 loc) · 5.94 KB
/
message.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// SPDX-FileCopyrightText: 2021 The go-metafeed Authors
//
// SPDX-License-Identifier: MIT
package metafeed
import (
"crypto/sha256"
"encoding"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"time"
"github.com/zeebo/bencode"
"golang.org/x/crypto/ed25519"
"github.com/ssbc/go-metafeed/internal/sign"
refs "github.com/ssbc/go-ssb-refs"
)
// Message is used to create the (un)marshal a message to and from bencode while also acting as refs.Message for the rest of the ssb system.
type Message struct {
Data bencode.RawMessage
Signature []byte
payload *Payload
}
var (
_ bencode.Marshaler = (*Message)(nil)
_ bencode.Unmarshaler = (*Message)(nil)
)
// MarshalBencode turns data and signature into an bencode array [content, signature]
func (msg *Message) MarshalBencode() ([]byte, error) {
return bencode.EncodeBytes([]interface{}{
msg.Data,
msg.Signature,
})
}
// UnmarshalBencode expects a benocded array of [content, signature]
func (msg *Message) UnmarshalBencode(input []byte) error {
if len(input) > maxMessageSize {
return errors.New("metafeed: message too big")
}
var raw []bencode.RawMessage
err := bencode.DecodeBytes(input, &raw)
if err != nil {
return fmt.Errorf("failed to decode raw Message array: %w", err)
}
if n := len(raw); n != 2 {
return fmt.Errorf("metafeed/Message: expected two elemnts in the array, got %d", n)
}
// just take the data as is (that it's valid bencode was settled by the first decode pass)
msg.Data = raw[0]
// make sure it's a valid byte string
err = bencode.DecodeBytes(raw[1], &msg.Signature)
if err != nil {
return fmt.Errorf("metafeed/Message: failed to decode signature portion: %w", err)
}
if n := len(msg.Signature); n != ed25519.SignatureSize+2 {
return fmt.Errorf("metafeed/Message: expected %d bytes of signture - only got %d", ed25519.SignatureSize+2, n)
}
return nil
}
// for storage
var (
_ encoding.BinaryMarshaler = (*Message)(nil)
_ encoding.BinaryUnmarshaler = (*Message)(nil)
)
// MarshalBinary for now, calls the bencode versions (performance profiling pending)
func (msg *Message) MarshalBinary() ([]byte, error) {
return msg.MarshalBencode()
}
// UnmarshalBinary for now, calls the bencode versions (performance profiling pending)
func (msg *Message) UnmarshalBinary(input []byte) error {
return msg.UnmarshalBencode(input)
}
// Verify returns true if the Message was signed by the author specified by the meta portion of the message
func (msg *Message) Verify(hmacKey *[32]byte) bool {
if err := msg.getPayload(); err != nil {
return false
}
pubKey := msg.payload.Author.PubKey()
return sign.Verify(msg.Data, msg.Signature, pubKey, hmacKey)
}
// Payload returns the message payload inside the data portion of the Message object.
func (msg *Message) Payload() (Payload, error) {
if err := msg.getPayload(); err != nil {
return Payload{}, err
}
return *msg.payload, nil
}
func (msg *Message) getPayload() error {
if msg.payload != nil {
return nil
}
var p Payload
if err := p.UnmarshalBencode(msg.Data); err != nil {
return err
}
msg.payload = &p
return nil
}
// go-ssb compatability
var _ refs.Message = (*Message)(nil)
// Key returns the hash reference of the message
func (msg *Message) Key() refs.MessageRef {
// TODO: do this lazy and cache the result
bytes, err := msg.MarshalBencode()
if err != nil {
panic(err)
}
h := sha256.New()
h.Write(bytes)
msgKey, err := refs.NewMessageRefFromBytes(h.Sum(nil), refs.RefAlgoMessageBendyButt)
if err != nil {
panic(err)
}
return msgKey
}
// Seq returns the sequence of th message
func (msg *Message) Seq() int64 {
err := msg.getPayload()
if err != nil {
log.Println("metafeed/verify payload decoding failed:", err)
return -1
}
return int64(msg.payload.Sequence)
}
// Author returns the author who signed the message
func (msg *Message) Author() refs.FeedRef {
err := msg.getPayload()
if err != nil {
panic(err)
}
return msg.payload.Author
}
// Previous return nil for the first message and otherwise the hash reference of the previous message
func (msg *Message) Previous() *refs.MessageRef {
err := msg.getPayload()
if err != nil {
panic(err)
}
if msg.payload.Sequence == 1 {
return nil
}
return msg.payload.Previous
}
// Received needs to be repalced by the database (this spoofs it as the calimed timestamp)
func (msg *Message) Received() time.Time {
log.Println("received time is spoofed to claimed")
return msg.Claimed()
}
// Claimed returns the time the message claims as it's timestamp
func (msg *Message) Claimed() time.Time {
err := msg.getPayload()
if err != nil {
panic(err)
}
return msg.payload.Timestamp
}
// ContentBytes returns the pure bencoded content portion of the message
func (msg *Message) ContentBytes() []byte {
var arr []bencode.RawMessage
err := bencode.DecodeBytes(msg.Data, &arr)
if err != nil {
panic(err)
}
return arr[4]
}
// ValueContent returns a ssb.Value that can be represented as JSON.
// Note that it's signature is useless for verification in this form.
// Get the whole Message message and use msg.Verify()
func (msg *Message) ValueContent() *refs.Value {
err := msg.getPayload()
if err != nil {
panic(err)
}
var val refs.Value
if msg.payload.Sequence > 1 {
val.Previous = msg.payload.Previous
}
val.Author = msg.payload.Author
val.Sequence = int64(msg.payload.Sequence)
val.Hash = "metafeed-v1"
val.Signature = base64.StdEncoding.EncodeToString(msg.Signature) + ".metafeed-v1.sig.ed25519"
val.Timestamp = refs.Millisecs(msg.Claimed())
// TODO: peek at first byte (tfk indicating box2 for instance)
var helper interface{}
err = bencode.DecodeBytes(msg.payload.Content, &helper)
if err != nil {
panic(err)
}
val.Content, err = json.Marshal(helper)
if err != nil {
panic(err)
}
return &val
}
// ValueContentJSON encodes the Message into JSON like a normal SSB message.
func (msg *Message) ValueContentJSON() json.RawMessage {
jsonB, err := json.Marshal(msg.ValueContent())
if err != nil {
panic(err.Error())
}
return jsonB
}