-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathapi.go
306 lines (282 loc) · 9.91 KB
/
api.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package calypso
import (
"encoding/binary"
"time"
"go.dedis.ch/kyber/v3/sign/schnorr"
"golang.org/x/xerrors"
"go.dedis.ch/cothority/v3"
"go.dedis.ch/cothority/v3/byzcoin"
"go.dedis.ch/cothority/v3/darc"
"go.dedis.ch/cothority/v3/skipchain"
"go.dedis.ch/kyber/v3"
"go.dedis.ch/onet/v3"
"go.dedis.ch/onet/v3/network"
"go.dedis.ch/protobuf"
)
// Client is a class to communicate to the calypso service.
type Client struct {
bcClient *byzcoin.Client
c *onet.Client
ltsReply *CreateLTSReply
}
// WriteReply is returned upon successfully spawning a Write instance.
type WriteReply struct {
*byzcoin.AddTxResponse
byzcoin.InstanceID
}
// ReadReply is is returned upon successfully spawning a Read instance.
type ReadReply struct {
*byzcoin.AddTxResponse
byzcoin.InstanceID
}
// NewClient instantiates a new Client.
// It takes as input an "initialized" byzcoin client
// with an already created ledger
func NewClient(byzcoin *byzcoin.Client) *Client {
return &Client{bcClient: byzcoin, c: onet.NewClient(
cothority.Suite, ServiceName)}
}
// CreateLTS creates a random LTSID that can be used to reference the LTS group
// created. It first sends a transaction to ByzCoin to spawn a LTS instance,
// then it asks the Calypso cothority to start the DKG.
func (c *Client) CreateLTS(ltsRoster *onet.Roster, darcID darc.ID, signers []darc.Signer, counters []uint64) (reply *CreateLTSReply, err error) {
// Make the transaction and get its proof
buf, err := protobuf.Encode(&LtsInstanceInfo{*ltsRoster})
if err != nil {
return nil, xerrors.Errorf("encoding roster: %v", err)
}
inst := byzcoin.Instruction{
InstanceID: byzcoin.NewInstanceID(darcID),
Spawn: &byzcoin.Spawn{
ContractID: ContractLongTermSecretID,
Args: []byzcoin.Argument{
{
Name: "lts_instance_info",
Value: buf,
},
},
},
SignerCounter: counters,
}
tx := byzcoin.NewClientTransaction(byzcoin.CurrentVersion, inst)
if err := tx.FillSignersAndSignWith(signers...); err != nil {
return nil, xerrors.Errorf("signing txn: %v", err)
}
atr, err := c.bcClient.AddTransactionAndWait(tx, 10)
if err != nil {
return nil, xerrors.Errorf("adding transaction: %v", err)
}
id := tx.Instructions[0].DeriveID("").Slice()
resp, err := c.bcClient.GetProofAfter(id, true, &atr.Proof.Latest)
if err != nil {
return nil, xerrors.Errorf("getting txn proof: %v", err)
}
// Start the DKG
reply = &CreateLTSReply{}
err = c.c.SendProtobuf(c.bcClient.Roster.List[0], &CreateLTS{
Proof: resp.Proof,
}, reply)
if err != nil {
return nil, xerrors.Errorf("send CreateLTS message: %v", err)
}
return reply, nil
}
// Authorise adds a ByzCoinID to the list of authorized IDs. It can only be called
// from localhost, except if the COTHORITY_ALLOW_INSECURE_ADMIN is set to 'true'.
// Deprecated: please use Authorize.
func (c *Client) Authorise(who *network.ServerIdentity, what skipchain.SkipBlockID) error {
return cothority.ErrorOrNil(
c.c.SendProtobuf(who, &Authorize{ByzCoinID: what}, nil),
"send Authorize message",
)
}
// Authorize adds a ByzCoinID to the list of authorized IDs in the server. To
// be accepted, the request must be signed by the private key stored in
// private.toml. For testing purposes, the environment variable can be set:
// COTHORITY_ALLOW_INSECURE_ADMIN=true
// this disables the signature check.
//
// It should be called by the administrator at the beginning, before any other
// API calls are made. A ByzCoinID that is not authorised will not be allowed to
// call the other APIs.
func (c *Client) Authorize(who *network.ServerIdentity, what skipchain.SkipBlockID) error {
reply := &AuthorizeReply{}
ts := time.Now().Unix()
msg := append(what, make([]byte, 8)...)
binary.LittleEndian.PutUint64(msg[32:], uint64(ts))
sig, err := schnorr.Sign(cothority.Suite, who.GetPrivate(), msg)
if err != nil {
return xerrors.Errorf("creating schnorr signature: %v", err)
}
err = c.c.SendProtobuf(who, &Authorize{
ByzCoinID: what,
Timestamp: ts,
Signature: sig,
}, reply)
if err != nil {
return xerrors.Errorf("sending Authorize message: %v", err)
}
return nil
}
// DecryptKey takes as input Read- and Write- Proofs. It verifies that
// the read/write requests match and then re-encrypts the secret
// given the public key information of the reader.
func (c *Client) DecryptKey(dkr *DecryptKey) (reply *DecryptKeyReply, err error) {
reply = &DecryptKeyReply{}
err = c.c.SendProtobuf(c.bcClient.Roster.List[0], dkr, reply)
return reply, cothority.ErrorOrNil(err, "sending DecryptKey message")
}
// WaitProof calls the byzcoin client's wait proof
func (c *Client) WaitProof(id byzcoin.InstanceID, interval time.Duration,
value []byte) (*byzcoin.Proof, error) {
return c.bcClient.WaitProof(id, interval, value)
}
// AddWrite creates a Write Instance by adding a transaction on the byzcoin client.
// Input:
// - write - A Write structure
// - signer - The data owner who will sign the transaction
// - signerCtr - A monotonically increasing counter for every signer
// - darc - The DARC with a spawn:calypsoWrite rule on it
// - wait - The number of blocks to wait -- 0 means no wait
//
// Output:
// - reply - WriteReply containing the transaction response and instance id
// - err - Error if any, nil otherwise.
func (c *Client) AddWrite(write *Write, signer darc.Signer, signerCtr uint64,
darc darc.Darc, wait int) (reply *WriteReply, err error) {
reply = &WriteReply{}
writeBuf, err := protobuf.Encode(write)
if err != nil {
return nil, xerrors.Errorf("encoding Write message: %v", err)
}
ctx := byzcoin.NewClientTransaction(byzcoin.CurrentVersion,
byzcoin.Instruction{
InstanceID: byzcoin.NewInstanceID(darc.GetBaseID()),
Spawn: &byzcoin.Spawn{
ContractID: ContractWriteID,
Args: byzcoin.Arguments{{
Name: "write", Value: writeBuf}},
},
SignerCounter: []uint64{signerCtr},
},
)
//Sign the transaction
err = ctx.FillSignersAndSignWith(signer)
if err != nil {
return nil, xerrors.Errorf("signing txn: %v", err)
}
reply.InstanceID = ctx.Instructions[0].DeriveID("")
//Delegate the work to the byzcoin client
reply.AddTxResponse, err = c.bcClient.AddTransactionAndWait(ctx, wait)
if err != nil {
return nil, xerrors.Errorf("adding txn: %v", err)
}
return reply, err
}
// AddRead creates a Read Instance by adding a transaction on the byzcoin client.
//
// Input:
// - proof - A ByzCoin proof of the Write Operation.
// - signer - The data owner who will sign the transaction
// - signerCtr - A monotonically increasing counter for the signer
// - wait - The number of blocks to wait -- 0 means no wait
//
// Output:
// - reply - ReadReply containing the transaction response and instance id
// - err - Error if any, nil otherwise.
func (c *Client) AddRead(proof *byzcoin.Proof, signer darc.Signer, signerCtr uint64, wait int) (
reply *ReadReply, err error) {
var readBuf []byte
read := &Read{
Write: byzcoin.NewInstanceID(proof.InclusionProof.Key()),
Xc: signer.Ed25519.Point,
}
reply = &ReadReply{}
readBuf, err = protobuf.Encode(read)
if err != nil {
return nil, xerrors.Errorf("encoding Read message: %v", err)
}
ctx := byzcoin.NewClientTransaction(byzcoin.CurrentVersion,
byzcoin.Instruction{
InstanceID: byzcoin.NewInstanceID(proof.InclusionProof.Key()),
Spawn: &byzcoin.Spawn{
ContractID: ContractReadID,
Args: byzcoin.Arguments{{Name: "read", Value: readBuf}},
},
SignerCounter: []uint64{signerCtr},
},
)
err = ctx.FillSignersAndSignWith(signer)
if err != nil {
return nil, xerrors.Errorf("signing txn: %v", err)
}
reply.InstanceID = ctx.Instructions[0].DeriveID("")
reply.AddTxResponse, err = c.bcClient.AddTransactionAndWait(ctx, wait)
if err != nil {
return nil, xerrors.Errorf("adding txn: %v", err)
}
return reply, nil
}
// SpawnDarc spawns a Darc Instance by adding a transaction on the byzcoin client.
// Input:
// - signer - The signer authorizing the spawn of this darc (calypso "admin")
// - signerCtr - A monotonically increaing counter for every signer
// - controlDarc - The darc governing this spawning
// - spawnDarc - The darc to be spawned
// - wait - The number of blocks to wait -- 0 means no wait
//
// Output:
// - reply - AddTxResponse containing the transaction response
// - err - Error if any, nil otherwise.
func (c *Client) SpawnDarc(signer darc.Signer, signerCtr uint64,
controlDarc darc.Darc, spawnDarc darc.Darc, wait int) (
reply *byzcoin.AddTxResponse, err error) {
darcBuf, err := spawnDarc.ToProto()
if err != nil {
return nil, xerrors.Errorf("serializing darc to protobuf: %v", err)
}
ctx := byzcoin.NewClientTransaction(byzcoin.CurrentVersion,
byzcoin.Instruction{
InstanceID: byzcoin.NewInstanceID(controlDarc.GetBaseID()),
Spawn: &byzcoin.Spawn{
ContractID: byzcoin.ContractDarcID,
Args: []byzcoin.Argument{{
Name: "darc",
Value: darcBuf,
}},
},
SignerCounter: []uint64{signerCtr},
},
)
err = ctx.FillSignersAndSignWith(signer)
if err != nil {
return nil, xerrors.Errorf("signing txn: %v", err)
}
reply, err = c.bcClient.AddTransactionAndWait(ctx, wait)
return reply, cothority.ErrorOrNil(err, "adding txn")
}
// RecoverKey is used to recover the secret key once it has been
// re-encrypted to a given public key by the DecryptKey method
// in the Calypso service. The resulting secret key can be used
// with a symmetric decryption algorithm to decrypt the data
// stored in the Data field of the WriteInstance.
//
// Input:
// - xc - the private key of the reader
//
// Output:
// - key - the re-assembled key
// - err - a possible error when trying to recover the data from the point
func (r *DecryptKeyReply) RecoverKey(xc kyber.Scalar) (key []byte, err error) {
xcInv := xc.Clone().Neg(xc)
XhatDec := r.X.Clone().Mul(xcInv, r.X)
Xhat := XhatDec.Clone().Add(r.XhatEnc, XhatDec)
XhatInv := Xhat.Clone().Neg(Xhat)
// Decrypt r.C to keyPointHat
XhatInv.Add(r.C, XhatInv)
key, err = XhatInv.Data()
if err != nil {
err = xerrors.Errorf("extracting data from point: %v", err)
}
return
}