forked from lyriarte/blockchain-ssm
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ssm.go
312 lines (272 loc) · 7.77 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
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// Copyright Luc Yriarte <luc.yriarte@thingagora.org> 2018
// License: Apache-2.0
//
// Signing State Machines chaincode
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
package main
import (
"errors"
"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())
}
}
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) {
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) {
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) {
return shim.Error(err.Error())
}
return self.Start(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 == "admin" {
// "admin", <admin name>
dat, err = stub.GetState("ADMIN_" + args[0])
} 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
err = user.Put(stub, "USER_" + user.Name)
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
err = ssm.Put(stub, "SSM_" + ssm.Name)
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
err = state.Put(stub, "STATE_" + state.Session)
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])
}
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
//
// main function
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
func main() {
err := shim.Start(new(SSMChaincode))
if err != nil {
fmt.Println("Signing State Machines Chaincode init error:", err)
}
}