/
payload.go
67 lines (55 loc) 路 1.49 KB
/
payload.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
package start
import (
"crypto/rand"
"encoding/json"
"time"
dag "github.com/ipfs/go-merkledag"
ft "github.com/ipfs/go-unixfs"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/pkg/errors"
)
// Payload is the underlying data that gets announced to the network
type Payload struct {
Message string
Timestamp time.Time
Random []byte
Signature []byte
}
// NewPayload generates 100 bytes of random data and initializes a Payload
// data structure. It's also signing the data for no reason.
func NewPayload(key crypto.PrivKey) (*Payload, error) {
buf := make([]byte, 100)
_, err := rand.Read(buf)
if err != nil {
return nil, errors.Wrap(err, "read random data")
}
p := &Payload{
Message: "Antares Test Data",
Timestamp: time.Now(),
Random: buf,
Signature: nil,
}
dat, err := json.Marshal(p)
if err != nil {
return nil, errors.Wrap(err, "marshal probe data")
}
signature, err := key.Sign(dat)
if err != nil {
return nil, errors.Wrap(err, "signing probe data")
}
p.Signature = signature
return p, nil
}
// Bytes returns the json representation of the data embedded into a
// DAG node so that IPFS can make sense of the data.
func (p *Payload) Bytes() ([]byte, error) {
dat, err := p.JsonBytes()
if err != nil {
return nil, errors.Wrap(err, "new probe data")
}
return dag.NodeWithData(ft.FilePBData(dat, uint64(len(dat)))).Marshal()
}
// JsonBytes returns the json representation of the data.
func (p *Payload) JsonBytes() ([]byte, error) {
return json.Marshal(p)
}