-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudchaincode.go
529 lines (376 loc) · 11.4 KB
/
cloudchaincode.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
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
)
type DocumentStoreSmartContract struct {
contractapi.Contract
}
type User struct {
UserId string `json:"UserId"`
UserKey string `json:"UserKey"`
}
type Document struct {
DocId string `json:"DocId"`
Hash string `json:"Hash"`
OwnerId string `json:"OwnerId"`
SharingId string `json:"SharingId"`
EncKey string `json:"EncKey"`
Verification string `json:"Verification"`
}
type Dummy struct {
DummyId string `json:"UserId"`
DummyKey string `json:"UserKey"`
}
type Response struct {
DocumentId string `json:"DocumentId"`
Content string `json:"Content"`
}
// This function register a new user id
func (pc *DocumentStoreSmartContract) Register(ctx contractapi.TransactionContextInterface, UserId string) (*User, error) {
var user string = "UserId"
user += UserId
userJSON, err := ctx.GetStub().GetState(user)
if err != nil {
return nil, fmt.Errorf("Failed to read the data from world state", err)
}
if userJSON != nil {
return nil, fmt.Errorf("the UserId %s already exists", UserId)
}
//generate key
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
s := make([]rune, 10)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
var userkey string = string(s)
//generate key
//var userkey string = "1"
var registration *User = new(User)
registration.UserId = UserId
registration.UserKey = userkey
registrationJson, err := json.Marshal(registration)
if err != nil {
return nil, fmt.Errorf("Failed to Marshal the data from world state", err)
}
return registration, ctx.GetStub().PutState(user, registrationJson)
}
////////
var aesbyte = []byte{35, 46, 57, 24, 85, 35, 24, 74, 87, 35, 88, 98, 66, 32, 14, 05}
func Encode(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
func Decode(s string) []byte {
data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return data
}
func Encrypt(text string, MySecret string) (string, error) {
block, err := aes.NewCipher([]byte(MySecret))
if err != nil {
return "", err
}
plainText := []byte(text)
cfb := cipher.NewCFBEncrypter(block, aesbyte)
cipherText := make([]byte, len(plainText))
cfb.XORKeyStream(cipherText, plainText)
return Encode(cipherText), nil
}
func Decrypt(text, MySecret string) (string, error) {
block, err := aes.NewCipher([]byte(MySecret))
if err != nil {
return "", err
}
cipherText := Decode(text)
cfb := cipher.NewCFBDecrypter(block, aesbyte)
plainText := make([]byte, len(cipherText))
cfb.XORKeyStream(plainText, cipherText)
return string(plainText), nil
}
/////////
// This function uploads the document
func (pc *DocumentStoreSmartContract) UploadDocument(ctx contractapi.TransactionContextInterface, Content string, UserKey string, UserId string, DocHash string, ShareId string) (*Document, error) {
//verify UserId
var user string = "UserId"
user += UserId
userJSON, err := ctx.GetStub().GetState(user)
if err != nil {
return nil, err
}
if userJSON == nil {
return nil, fmt.Errorf("User Data not found")
}
var registration *User = new(User)
err = json.Unmarshal(userJSON, ®istration)
if err != nil {
return nil, err
}
if registration.UserKey != UserKey {
return nil, fmt.Errorf("the UserId and key does not match")
}
//verify doc
//calculate hash sha1 and compare with provided hash
h := sha1.New()
h.Write([]byte(Content))
hs := h.Sum(nil)
hashstr := fmt.Sprintf("%x", hs)
if hashstr != DocHash {
return nil, fmt.Errorf("the Document and Hash does not match")
}
//generate document Id
var temp_doc_id int
for temp_doc_id = 1; temp_doc_id < 100; temp_doc_id++ {
tmp := fmt.Sprintf("%d", temp_doc_id)
var tmp2 string = "DocId"
tmp2 += tmp
docJSON, err := ctx.GetStub().GetState(tmp2)
if err != nil {
return nil, err
}
if docJSON == nil {
break
}
}
if temp_doc_id == 100 {
return nil, fmt.Errorf("No documentId available to give")
}
final_doc_id := fmt.Sprintf("%d", temp_doc_id)
final_doc_key := "DocId" + final_doc_id
//generate enryption key
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
s := make([]rune, 24)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
var EncrKey = string(s)
//Encypt
EncyptedContent,_ := Encrypt(Content,EncrKey);
//upload on DataBase --TODO
//----------------------------------------
var requestBody *Response = new(Response)
requestBody.DocumentId=final_doc_id
requestBody.Content=EncyptedContent
requestJson, err := json.Marshal(requestBody)
if err != nil {
fmt.Errorf("Failed to Marshal the data from world state", err)
}
response,_ :=http.Post("http://172.30.137.4:5000/upload", "application/json", bytes.NewBuffer(requestJson))
if err != nil {
panic(err)
}
defer response.Body.Close()
content,_ := ioutil.ReadAll(response.Body)
var responds *Response = new(Response)
err = json.Unmarshal(content, &responds)
if err != nil {
return nil, err
}
//-------------------------------------------
var doc_info *Document = new(Document)
doc_info.DocId = final_doc_id
doc_info.EncKey = EncrKey
doc_info.OwnerId = UserId
doc_info.Hash = DocHash
doc_info.SharingId = ShareId
doc_info.Verification = "NotVerified"
docinfoJson, err := json.Marshal(doc_info)
if err != nil {
return nil, err
}
return doc_info, ctx.GetStub().PutState(final_doc_key, docinfoJson)
}
func (pc *DocumentStoreSmartContract) VerifyUpload(ctx contractapi.TransactionContextInterface, EncrKey string, UserKey string, UserId string, DocHash string, ShareId string, DocumentId string) (*Document, error) {
//verify UserId
var user string = "UserId"
user += UserId
userJSON, err := ctx.GetStub().GetState(user)
if err != nil {
return nil, err
}
if userJSON == nil {
return nil, fmt.Errorf("User Data not found")
}
var registration *User = new(User)
err = json.Unmarshal(userJSON, ®istration)
if err != nil {
return nil, err
}
if registration.UserKey != UserKey {
return nil, fmt.Errorf("the UserId and key does not match")
}
var doc string = "DocId"
doc += DocumentId
docJSON, err := ctx.GetStub().GetState(doc)
if err != nil {
return nil, err
}
if docJSON == nil {
return nil, fmt.Errorf("Document Data not found")
}
var docinfo *Document = new(Document)
err = json.Unmarshal(docJSON, &docinfo)
if err != nil {
return nil, err
}
//pull document calculate hash check if it is equal
var requestBody *Response = new(Response)
requestBody.DocumentId = DocumentId
requestJson, err := json.Marshal(requestBody)
if err != nil {
fmt.Errorf("Failed to Marshal the data from world state", err)
}
response,_ :=http.Post("http://172.30.137.4:5000/verify/retrieve", "application/json", bytes.NewBuffer(requestJson))
defer response.Body.Close()
content,_ := ioutil.ReadAll(response.Body)
var responds *Response = new(Response)
err = json.Unmarshal(content, &responds)
decryptedContent,_ := Decrypt(responds.Content,EncrKey)
//calculate hash of decrypted content
h := sha1.New()
h.Write([]byte(decryptedContent))
hs := h.Sum(nil)
hashstr := fmt.Sprintf("%x", hs)
if hashstr != DocHash {
return nil, fmt.Errorf("Upload Verification failure as hash does not match")
}
//check ledger
if docinfo.OwnerId != UserId {
return nil, fmt.Errorf("Upload Verification failure as Owner id and User Id does not match")
}
if docinfo.SharingId != ShareId {
return nil, fmt.Errorf("Upload Verification failure as Share id does not match")
}
if docinfo.EncKey != EncrKey {
return nil, fmt.Errorf("Upload Verification failure as Encryption key does not match")
}
//send commit to database
var requestBodycommit *Response = new(Response)
requestBodycommit.DocumentId = DocumentId
requestJsoncommit, err := json.Marshal(requestBodycommit)
if err != nil {
fmt.Errorf("Failed to Marshal the data from world state", err)
}
responsecommit,_ :=http.Post("http://172.30.137.4:5000/verify/upload", "application/json", bytes.NewBuffer(requestJsoncommit))
defer responsecommit.Body.Close()
contentcommit,_ := ioutil.ReadAll(responsecommit.Body)
var respondcommit *Response = new(Response)
err = json.Unmarshal(contentcommit, &respondcommit)
//-------------------------------------------
//put verified
docinfo.Verification = "Verified"
docinfoJson, err := json.Marshal(docinfo)
return docinfo, ctx.GetStub().PutState(doc, docinfoJson)
//return
}
// This function uploads the document
func (pc *DocumentStoreSmartContract) ReadDocument(ctx contractapi.TransactionContextInterface, UserKey string, UserId string, DocumentId string) (*Response, error) {
//verify UserId
var user string = "UserId"
user += UserId
userJSON, err := ctx.GetStub().GetState(user)
if err != nil {
return nil, err
}
if userJSON == nil {
return nil, fmt.Errorf("User Data not found")
}
var registration *User = new(User)
err = json.Unmarshal(userJSON, ®istration)
if err != nil {
return nil, err
}
if registration.UserKey != UserKey {
return nil, fmt.Errorf("the UserId and key does not match")
}
//Read Transaction and check Sharing Status and verify status
var doc string = "DocId"
doc += DocumentId
docJSON, err := ctx.GetStub().GetState(doc)
if err != nil {
return nil, err
}
if docJSON == nil {
return nil, fmt.Errorf("Document Data not found")
}
var docinfo *Document = new(Document)
err = json.Unmarshal(docJSON, &docinfo)
if err != nil {
return nil, err
}
if docinfo.Verification != "Verified" {
return nil, fmt.Errorf("Document Not Verified")
}
if docinfo.SharingId != UserId {
return nil, fmt.Errorf("Document is inaccessible to user")
}
var requestBody *Response = new(Response)
requestBody.DocumentId = DocumentId
requestJson, err := json.Marshal(requestBody)
if err != nil {
fmt.Errorf("Failed to Marshal the data from world state", err)
}
response,_ :=http.Post("http://172.30.137.4:5000/read", "application/json", bytes.NewBuffer(requestJson))
defer response.Body.Close()
content,_ := ioutil.ReadAll(response.Body)
var responds *Response = new(Response)
err = json.Unmarshal(content, &responds)
if err != nil {
return nil, err
}
decryptedContent,_:=Decrypt(responds.Content,docinfo.EncKey)
responds.Content=decryptedContent
//put dummy on ledger
var dum string = "Dummy"
dumJSON, err := ctx.GetStub().GetState(dum)
if dumJSON == nil {
var dum_info *Dummy = new(Dummy)
dum_info.DummyId = "Dummy"
dum_info.DummyKey = "Dummy1"
dum_infoJson, err := json.Marshal(dum_info)
if err != nil {
return nil, err
}
return responds, ctx.GetStub().PutState(dum, dum_infoJson)
}
var dum_info *Dummy = new(Dummy)
err = json.Unmarshal(dumJSON, &dum_info)
if err != nil {
return nil, err
}
if dum_info.DummyKey == "Dummy1" {
dum_info.DummyKey = "Dummy2"
dum_infoJson, err := json.Marshal(dum_info)
if err != nil {
return nil, err
}
return responds, ctx.GetStub().PutState(dum, dum_infoJson)
}
dum_info.DummyKey = "Dummy1"
dum_infoJson, err := json.Marshal(dum_info)
if err != nil {
return nil, err
}
return responds, ctx.GetStub().PutState(dum, dum_infoJson)
//return
}
func main() {
DocStoreSmartContract := new(DocumentStoreSmartContract)
cc, err := contractapi.NewChaincode(DocStoreSmartContract)
if err != nil {
panic(err.Error())
}
if err := cc.Start(); err != nil {
panic(err.Error())
}
}