-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.go
174 lines (143 loc) · 3.85 KB
/
node.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
package node
import (
"context"
"crypto/sha256"
"encoding/json"
"os"
"path/filepath"
"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
"github.com/celestiaorg/celestia-node/logs"
"github.com/celestiaorg/celestia-node/nodebuilder"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
appns "github.com/celestiaorg/celestia-app/pkg/namespace"
"github.com/celestiaorg/celestia-node/blob"
"github.com/celestiaorg/celestia-node/share"
"github.com/ipfs/go-log/v2"
)
const (
nodeCoreIp = "rpc-mocha.pops.one"
nodePath = "~/.blobusign"
nodeType = node.Light
nodeNetwork = p2p.Mocha
startHash = "0E005E02A1EE6F9350E2B74EF388925F65B8EC1E6D11E3DD92DDD20C82A860F8"
startHeight = 1213740
)
type Node struct {
celnode *nodebuilder.Node
signer keyring.Keyring
}
func NewNode() (*Node, error) {
logs.SetAllLoggers(log.LevelInfo)
keysPath := filepath.Join(nodePath, "keys")
encConf := encoding.MakeConfig(app.ModuleEncodingRegisters...)
signer, err := keyring.New(app.Name, keyring.BackendTest, keysPath, os.Stdin, encConf.Codec)
if err != nil {
return nil, err
}
if !nodebuilder.IsInit(nodePath) {
cfg := nodebuilder.DefaultConfig(nodeType)
cfg.Header.TrustedHash = startHash
cfg.DASer.SampleFrom = startHeight
cfg.Core.IP = nodeCoreIp
err = nodebuilder.Init(*cfg, nodePath, nodeType)
if err != nil {
return nil, err
}
}
store, err := nodebuilder.OpenStore(nodePath, signer)
if err != nil {
return nil, err
}
node, err := nodebuilder.New(nodeType, nodeNetwork, store)
if err != nil {
return nil, err
}
return &Node{celnode: node, signer: signer}, nil
}
func (n *Node) Start(ctx context.Context) error {
return n.celnode.Start(ctx)
}
func (n *Node) Stop(ctx context.Context) error {
return n.celnode.Stop(ctx)
}
func (n *Node) Publish(ctx context.Context, data []byte) (ID, error) {
hash := sha256.Sum256(data)
ns, err := share.NewBlobNamespaceV0(hash[:appns.NamespaceVersionZeroIDSize])
if err != nil {
return nil, err
}
b, err := blob.NewBlobV0(ns, data)
if err != nil {
return nil, err
}
height, err := n.celnode.BlobServ.Submit(ctx, []*blob.Blob{b}, blob.DefaultGasPrice())
if err != nil {
return nil, err
}
return NewID(height, ns, b.Commitment), nil
}
func (n *Node) Get(ctx context.Context, id ID) (SignedDocument, error) {
signedDoc := SignedDocument{
Document: nil,
Signatures: make([]Signature, 0),
}
namespace := id.Namespace()
earliestHeight := id.Height()
latestHeader, err := n.celnode.HeaderServ.NetworkHead(ctx)
if err != nil {
return signedDoc, err
}
for height := latestHeader.Height(); height >= earliestHeight; height-- {
blobs, err := n.celnode.BlobServ.GetAll(ctx, height, []share.Namespace{namespace})
if err != nil {
return signedDoc, err
}
for _, blob := range blobs {
var sigData Signature
err := json.Unmarshal(blob.Data, &sigData)
if err != nil {
// assume it's not a signature
signedDoc.Document = blob.Data
continue
}
signedDoc.Signatures = append(signedDoc.Signatures, sigData)
}
}
return signedDoc, nil
}
func (n *Node) Sign(ctx context.Context, id ID) error {
keys, err := n.signer.List()
if err != nil {
return err
}
blob, err := n.celnode.BlobServ.Get(ctx, id.Height(), id.Namespace(), id.Committment())
if err != nil {
return err
}
signature, pubkey, err := n.signer.Sign(keys[0].Name, blob.Data)
if err != nil {
return err
}
data, err := json.Marshal(&Signature{
signature, pubkey.Bytes(),
})
if err != nil {
return err
}
_, err = n.Publish(ctx, data) // Maybe we wanna use that sigID?
if err != nil {
return err
}
return nil
}
type Signature struct {
Signature []byte
PubKey []byte
}
type SignedDocument struct {
Document []byte
Signatures []Signature
}