forked from lyriarte/blockchain-ssm
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ssm.go
545 lines (491 loc) · 13.6 KB
/
ssm.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// Copyright Luc Yriarte <luc.yriarte@thingagora.org> 2018
// License: Apache-2.0
//
// Signing State Machines chaincode
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
package main
import (
"errors"
"strings"
"fmt"
"encoding/json"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
//
// chaincode interface
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
type SSMChaincode struct {
}
//
// chaincode initialization
//
func (self *SSMChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
_, args := stub.GetFunctionAndParameters()
if len(args) != 1 {
return shim.Error("Incorrect arg count. Expecting 1")
}
// "init", admins: <Agent>*
var admins []Agent
// Create admins array from JSON string
err := json.Unmarshal([]byte(args[0]), &admins)
if (err != nil) {
return shim.Error(err.Error())
}
// Verify admins public key before storing
for i := 0; i < len(admins); i++ {
_, err = admins[i].PublicKey()
if err != nil {
return shim.Error(err.Error())
}
}
for i := 0; i < len(admins); i++ {
// Store every admin
err = admins[i].Put(stub, "ADMIN_" + admins[i].Name)
if (err != nil) {
return shim.Error(err.Error())
}
}
// Upgrade DB
err = self.UpgradeDB(stub)
if (err != nil) {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
//
// chaincode invocation for transactions and queries
//
func (self *SSMChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
errmsg := "Incorrect arg count."
var err error
//
// transactions
//
// "register", user:Agent, admin_name:string, signature:b64
if function == "register" {
if len(args) != 3 {
return shim.Error(errmsg)
}
err = self.Verify(stub, args, "ADMIN")
if (err != nil) {
err = self.CheckGrants(stub, args, function)
if (err != nil) {
return shim.Error(err.Error())
}
}
return self.Register(stub, args)
}
// "create", ssm:SigningStateMachine, admin_name:string, signature:b64
if function == "create" {
if len(args) != 3 {
return shim.Error(errmsg)
}
err = self.Verify(stub, args, "ADMIN")
if (err != nil) {
err = self.CheckGrants(stub, args, function)
if (err != nil) {
return shim.Error(err.Error())
}
}
return self.Create(stub, args)
}
// "start", init:State, admin_name:string, signature:b64
if function == "start" {
if len(args) != 3 {
return shim.Error(errmsg)
}
err = self.Verify(stub, args, "ADMIN")
if (err != nil) {
err = self.CheckGrants(stub, args, function)
if (err != nil) {
return shim.Error(err.Error())
}
}
return self.Start(stub, args)
}
// "limit", context:State, admin_name:string, signature:b64
if function == "limit" {
if len(args) != 3 {
return shim.Error(errmsg)
}
err = self.Verify(stub, args, "ADMIN")
if (err != nil) {
return shim.Error(err.Error())
}
return self.Limit(stub, args)
}
// "grant", rights:Grant, admin_name:string, signature:b64
if function == "grant" {
if len(args) != 3 {
return shim.Error(errmsg)
}
err = self.Verify(stub, args, "ADMIN")
if (err != nil) {
return shim.Error(err.Error())
}
return self.Grant(stub, args)
}
// "perform", action:string, context:State, user_name:string, signature:b64
if function == "perform" {
if len(args) != 4 {
return shim.Error(errmsg)
}
err = self.Verify(stub, args, "USER")
if (err != nil) {
return shim.Error(err.Error())
}
return self.Perform(stub, args)
}
//
// queries
//
if len(args) != 1 {
return shim.Error(errmsg)
}
errmsg = "Unknown operation."
var dat []byte
if function == "session" {
// "session", <session id>
dat, err = stub.GetState("STATE_" + args[0])
} else if function == "ssm" {
// "ssm", <ssm name>
dat, err = stub.GetState("SSM_" + args[0])
} else if function == "user" {
// "user", <user name>
dat, err = stub.GetState("USER_" + args[0])
} else if function == "credits" {
// "credits", <user name>
dat, err = stub.GetState("GRANT_" + args[0])
} else if function == "admin" {
// "admin", <admin name>
dat, err = stub.GetState("ADMIN_" + args[0])
} else if function == "list" {
// "list", <session|ssm|user|admin>
var lst []string
pre := strings.ToUpper(args[0]) + "_"
if args[0] == "session" {
pre = "STATE_"
}
itr, _ := stub.GetStateByRange(" ", "~")
defer itr.Close()
for itr.HasNext() {
key, _ := itr.Next()
if strings.HasPrefix(key.Key, pre) {
lst = append(lst, "\"" + strings.TrimPrefix(key.Key, pre) + "\"")
}
}
dat = []byte("[" + strings.Join(lst,",") + "]")
} else if function == "log" {
// "log", <session id>
var lst []string
itr, _ := stub.GetHistoryForKey("STATE_" + args[0])
defer itr.Close()
for itr.HasNext() {
key, _ := itr.Next()
lst = append(lst, "{\"txId\": \"" + key.GetTxId() + "\", \"state\": " + string(key.GetValue()) + "}")
}
dat = []byte("[" + strings.Join(lst,",") + "]")
} else {
return shim.Error(errmsg)
}
if (err != nil) {
return shim.Error(err.Error())
}
return shim.Success(dat)
}
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
//
// transactions API implementation
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// "register", user:Agent, admin_name:string, signature:b64
func (self *SSMChaincode) Register(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
var user Agent
// Create user from JSON string
err = user.Deserialize([]byte(args[0]))
if (err != nil) {
return shim.Error(err.Error())
}
// Verify user public key before storing
_, err = user.PublicKey()
if err != nil {
return shim.Error(err.Error())
}
// Store user if not alreay existing
key := "USER_" + user.Name
err = self.CheckUnique(stub, key)
if (err != nil) {
return shim.Error(err.Error())
}
err = user.Put(stub, key)
if (err != nil) {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
// "create", ssm:SigningStateMachine, admin_name:string, signature:b64
func (self *SSMChaincode) Create(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
var ssm SigningStateMachine
// Create ssm from JSON string
err = ssm.Deserialize([]byte(args[0]))
if (err != nil) {
return shim.Error(err.Error())
}
// Store ssm if not alreay existing
key := "SSM_" + ssm.Name
err = self.CheckUnique(stub, key)
if (err != nil) {
return shim.Error(err.Error())
}
err = ssm.Put(stub, key)
if (err != nil) {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
// "start", init:State, admin_name:string, signature:b64
func (self *SSMChaincode) Start(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
var state State
// Create state from JSON string
err = state.Deserialize([]byte(args[0]))
if (err != nil) {
return shim.Error(err.Error())
}
// Store state if not alreay existing
key := "STATE_" + state.Session
err = self.CheckUnique(stub, key)
if (err != nil) {
return shim.Error(err.Error())
}
err = state.Put(stub, key)
if (err != nil) {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
// "limit", context:State, admin_name:string, signature:b64
func (self *SSMChaincode) Limit(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
var update State
// Create state update from JSON string
err = update.Deserialize([]byte(args[0]))
if (err != nil) {
return shim.Error(err.Error())
}
// Get the session state referenced in the update
var session State
err = session.Get(stub, "STATE_" + update.Session)
if (err != nil) {
return shim.Error(err.Error())
}
// Let the current session update its state with the new limit
err = session.SetLimit(&update)
if (err != nil) {
return shim.Error(err.Error())
}
// Save the session state
err = session.Put(stub, "STATE_" + session.Session)
if (err != nil) {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
// "grant", rights:Grant, admin_name:string, signature:b64
func (self *SSMChaincode) Grant(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
var update Grant
// Create grant update from JSON string
err = update.Deserialize([]byte(args[0]))
if (err != nil) {
return shim.Error(err.Error())
}
// Get the user referenced by the grant update
var user Agent
err = user.Get(stub, "USER_" + update.User)
if (err != nil) {
return shim.Error(err.Error())
}
// Get the user grant to update if any
var grant Grant
err = grant.Get(stub, "GRANT_" + update.User)
if (err != nil) {
// No preexisting grant, use the update for current grant
grant = update
}
// Let the current grant update its credits with the new grant
err = grant.SetCredits(&update)
if (err != nil) {
return shim.Error(err.Error())
}
// Save the user grant
err = grant.Put(stub, "GRANT_" + grant.User)
if (err != nil) {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
// "perform", action:string, context:State, user_name:string, signature:b64
func (self *SSMChaincode) Perform(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
var update State
// Create state update from JSON string
err = update.Deserialize([]byte(args[1]))
if (err != nil) {
return shim.Error(err.Error())
}
// Get the session state referenced in the update
var session State
err = session.Get(stub, "STATE_" + update.Session)
if (err != nil) {
return shim.Error(err.Error())
}
// Get the user's role in the current session
role := session.Roles[args[2]]
if (role == "") {
return shim.Error("No role for user " + args[2] + " in session " + session.Session)
}
// Get the SSM for the current session
var ssm SigningStateMachine
err = ssm.Get(stub, "SSM_" + session.Ssm)
if (err != nil) {
return shim.Error(err.Error())
}
// Get the next state for the proposed transition
update.Current = ssm.NextState(session.Current, role, args[0])
if (update.Current == -1) {
return shim.Error("No valid transition from state " + string(session.Current))
}
// Let the current session validate and perform the update
err = session.Perform(&update, role, args[0])
if (err != nil) {
return shim.Error(err.Error())
}
// Save the updated state for the current session
err = session.Put(stub, "STATE_" + session.Session)
if (err != nil) {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
//
// utilities
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// agent signature verification
func (self *SSMChaincode) Verify(stub shim.ChaincodeStubInterface, args []string, agentType string) error {
// Need at least 3 args: message, verifier, signature
argCount := len(args)
if argCount < 3 {
return errors.New("Incorrect arg count. Expecting 3 or more")
}
// Verifier agent type is either "USER" or "ADMIN"
var verifier Agent
err := verifier.Get(stub, agentType + "_" + args[argCount - 2])
if (err != nil) {
return err
}
// message to verify is args 0 to n-3
message := args[0]
for i := 1; i < argCount-2; i++ {
message += args[i]
}
return verifier.Verify(message, args[argCount - 1])
}
// user grants verification
func (self *SSMChaincode) CheckGrants(stub shim.ChaincodeStubInterface, args []string, api string) error {
// user signature verification
err := self.Verify(stub, args, "USER")
if (err != nil) {
return err
}
// Get user name from args
user := args[len(args) - 2]
// Get the user grant
var grant Grant
err = grant.Get(stub, "GRANT_" + user)
if (err != nil) {
return err
}
// Let grant verify and update credits
err = grant.ApiGrant(user, api)
if (err != nil) {
return err
}
// Save the updated grants
err = grant.Put(stub, "GRANT_" + user)
if (err != nil) {
return err
}
// All done
return nil
}
// ensure a key is not already in use
func (self *SSMChaincode) CheckUnique(stub shim.ChaincodeStubInterface, key string) error {
data, err := stub.GetState(key);
if (err != nil) {
return err
}
if (data != nil) {
return errors.New("Identifier " + key + " already in use.")
}
return nil
}
// Object read/rewrite for DB upgrade
func (self *SSMChaincode) ObjectRewrite(stub shim.ChaincodeStubInterface, obj Storable, key string) error {
err := obj.Get(stub, key)
if (err != nil) {
return err
}
err = obj.Put(stub, key)
return err
}
// Upgrade DB
func (self *SSMChaincode) UpgradeDB(stub shim.ChaincodeStubInterface) error {
// Add docType to all models
itr, err := stub.GetStateByRange(" ", "~")
if (err != nil) {
return err
}
defer itr.Close()
for itr.HasNext() {
key, _ := itr.Next()
if strings.HasPrefix(key.Key, "USER") || strings.HasPrefix(key.Key, "ADMIN") {
var obj Agent
err = self.ObjectRewrite(stub, &obj, key.Key)
} else if strings.HasPrefix(key.Key, "GRANT") {
var obj Grant
err = self.ObjectRewrite(stub, &obj, key.Key)
} else if strings.HasPrefix(key.Key, "STATE") {
var obj State
err = self.ObjectRewrite(stub, &obj, key.Key)
} else if strings.HasPrefix(key.Key, "SSM") {
var obj SigningStateMachine
err = self.ObjectRewrite(stub, &obj, key.Key)
}
if (err != nil) {
return err
}
}
return nil
}
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
//
// main function
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func main() {
err := shim.Start(new(SSMChaincode))
if err != nil {
fmt.Println("Signing State Machines Chaincode init error:", err)
}
}