forked from yeasy/blockchain_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchaincode_example06.go
505 lines (425 loc) · 11.8 KB
/
chaincode_example06.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
/*
author:swb
emial:[email protected]
MIT License
*/
package main
import (
"errors"
"fmt"
"strconv"
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"io"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
type SimpleChaincode struct {
}
var ExpressOrderId int = 0
var ExpressId int = 0
type ExpressOrder struct{
Id int
SenderLocation string
ReceiverLocation string
SenderAddress string
ReceiverAddress string
SenderPhone string
ReceiverPhone string
ExpressMoney int
ExpressMoneyType string
ExpressMoneySenderPay int
ExpressPointAddress []string
PayingMoney int
ExpressOrderSign string //0:收货方未签名 1:收货方签名
}
type User struct{
Name string
Location string
Address string
PriKey string
PubKey string
Phone string
Money int
}
type Express struct{
Name string
Location string
Phone string
Money int
ExpressPointerAddress []string
Address string
PriKey string
PubKey string
}
type ExpressPointer struct{
Name string
Location string
Phone string
PriKey string
PubKey string
ExpressAddress string
}
func (t *SimpleChaincode) Init(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
if function == "createUser"{
return t.createUser(stub,args)
}else if function == "craeteExpressPointer"{
return t.createExpressPointer(stub,args)
}else if function == "createExpress"{
return t.createExpress(stub,args)
}else if function == "createExpressOrder"{
return t.createExpressOrder(stub,args)
}
return nil,nil
}
func (t *SimpleChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
if function == "finishExpressOrder"{
return t.finishExpressOrder(stub,args)
}else if function == "addExpressPointer"{
return t.addExpressPointer(stub,args[0])
}else if function == "updateExpressOrder"{
return t.updateExpressOrder(stub,args[0])
}
return nil,nil
}
func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
if function == "getExpressOrderById"{
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 3")
}
_,eoBytes,err := getExpressOrderById(stub,args[0])
if err != nil {
fmt.Println("Error get data")
return nil, err
}
return eoBytes,nil
}else if function == "getExpress"{
if len(args) != 0 {
return nil, errors.New("Incorrect number of arguments. Expecting 0")
}
_,exBytes,err := getExpress(stub)
if err != nil {
fmt.Println("Error get data")
return nil, err
}
return exBytes,nil
}else if function == "getUserByAddress"{
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
_,userBytes,err := getUserByAddress(stub,args[0])
if err != nil {
fmt.Println("Error get data")
return nil, err
}
return userBytes,nil
}else if function == "getExpressPointerByAddress"{
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
_,expressCpBytes,err := getExpressPointerByAddress(stub,args[0])
if err != nil {
fmt.Println("Error get data")
return nil, err
}
return expressCpBytes,nil
}
return nil,nil
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}
//生成Address
func GetAddress() (string,string,string) {
var address,priKey,pubKey string
b := make([]byte, 48)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return "","",""
}
h := md5.New()
h.Write([]byte(base64.URLEncoding.EncodeToString(b)))
address = hex.EncodeToString(h.Sum(nil))
priKey = address+"1"
pubKey = address+"2"
return address,priKey,pubKey
}
func (t *SimpleChaincode) createUser(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
if len(args) != 4{
return nil, errors.New("Incorrect number of arguments. Expecting 4")
}
var user User
var userBytes []byte
var number int
var error err
var address,priKey,pubKey string
address,priKey,pubKey = GetAddress()
number,err= strconv.Atoi(args[3])
if err != nil{
return nil,errors.New("Want integer number")
}
user = User{Name:args[0],Loction:args[1],Address:address,PriKey:priKey,PubKey:pubKey,Phone:args[2],Money:number}
err = writeUser(stub,user)
if err!= nil{
return nil,errors.New("write error")
}
userBytes,err = json.Marshal(&user)
if err!= nil{
return nil,errors.New("Error retrieve")
}
return userBytes, nil
}
func(t *SimpleChaincode) createExpressPointer(stub *shim.ChaincodeStub,args[] string)([]byte,error){
if len(args) != 3{
return nil, errors.New("Incorrect number of arguments. Expecting 3")
}
var expressPointer ExpressPointer
var epBytes []byte
var address,priKey,pubKey string
address,priKey,pubKey = GetAddress()
expressPointer = ExpressPointer{Name:args[0],Location:args[1],ExpressAddress:address,PriKey:priKey,PubKey:pubKey,Phone:phone}
err = writeUser(stub,expressPointer)
if err!= nil{
return nil,errors.New("write error")
}
epBytes,err = json.Marshal(&expressPointer)
if err!= nil{
return nil,errors.New("Error retrieve")
}
return epBytes, nil
}
func(t *SimpleChaincode) createExpress(stub *shim.ChaincodeStub,args[] string)([]byte,error){
if len(args)!= 3{
return nil,errors.New("Incorrect number of arguments.Expecting 4")
}
if expressId != 0{
return nil,errors.New("can not create two express company")
}
var express Express
var pointerAddress []string
var expressBytes []byte
var money int
var err error
var address,priKey,pubKey string
money,err= strconv.Atoi(args[3])
if err != nil{
return nil,errors.New("Want integer number")
}
express = Express{Name:args[0],Location:args[1],Phone:args[2],Money:money,Address:address,PriKey:priKey,PubKey:pubKey,ExpressPointerAddress:pointerAddress}
err = writeExpress(stub,express)
if err!= nil{
return nil,errors.New("write error")
}
expressBytes,err = json.Marshal(&express)
if err!= nil{
return nil,errors.New("Error retrieve")
}
return expressBytes, nil
}
func(t *SimpleChaincode) createExpressOrder(stub *shim.ChaincodeStub,args[] string)([]byte,error){
if len(args) !=8{
return nil,errors.New("Incorrect number of arguments.Expecting 9")
}
var expressOrder ExpressOrder
var expressPointAddress []string
var eoBytes []byte
var err error
var money int
var payingMoney int
money,err = strconv.Atoi(args[7])
if err != nil{
return nil,errors.New("want integer number")
}
payingMoney,err = strconv.Atoi(args[8])
if err != nil{
return nil,errors.New("want integer number")
}
expressOrder = ExpressOrder{Id:ExpressOrderId,SenderLocation:args[0],ReceiverLocation:args[1],SenderAddress:args[2],ReceiverAddress:args[3],SenderPhone:args[4],ReceiverPhone:args[5],ExpressMoneyType:args[6],ExpressMoney:args[7],ExpressPointAddress:expressPointAddress,PayingMoney:payingMoney}
err = writeExpressOrder(expressOrder)
if err!= nil{
return err
}
ExpressOrderId = ExpressOrderId + 1
eoBytes,err = json.Marshal(&expressOrder)
if err!= nil{
return nil,errors.New("Error retrieve data")
}
return eoBytes,nil
}
func(t *SimpleChaincode) addExpressPointer(stub *shim.ChaincodeStub,address string)([]byte,error){
express,expressBytes,err := getExpress(stub)
if err != nil{
return nil,errors.New("Error get data")
}
expressOrder,eoBytes,error := getExpressPointerByAddress(stub,address)
if error != nil{
return nil,errors.New("Error get data")
}
express.ExpressPointerAddress = append(express.ExpressPointerAddress,expressOrder.ExpressAddress)
err = writeExpress(express)
if err != nil{
return nil,errors.New("Error write data")
}
expressBytes,err := json.Marshal(&express)
if err != nil{
return nil
}
return expressBytes,nil
}
func(t *SimpleChaincode) updateExpressOrder(stub *shim.ChaincodeStub,args[] string)([]byte,error){
if len(arg) != 2{
return nil,errors.New("Incorrect number of arguments.Expecting 2")
}
expressOrder,epBytes,err := getExpressOrderById(args[0])
if err != nil{
return nil,errors.New("get error")
}
expressOrder.ExpressPointAddress = append(expressOrder.ExpressPointAddress,args[1])
err = writeExpressOrder(expressOrder)
if err != nil{
return nil,nil
}
eoBytes,err := json.Marshal(&expressOrder)
if err != nil{
return nil
}
return eoBytes,nil
}
func(t *SimpleChaincode) finishExpressOrder(stub *shim.ChaincodeStub,args[] string)([]byte,error){
if len(arg)!= 3{
return nil,errors.New("Incorrect number of arguments.Expecting 3")
}
user,err := getUserByAddress(stub,args[0])
if err != nil{
return err
}
expressOrder,epBytes,error := getExpressOrderById(args[1])
if error != nil{
return error
}
express,epBytes,errorone := getExpress(stub)
errorone != nil{
return errorone
}
if expressOrder.ExpressMoney!=0{
express.Money = express.Money + expressOrder.ExpressMoney
expressOrder.ExpressMoney = 0
}else{
express.Money = express.Money + expressOrder.PayingMoney
user.Money = user.Money -expressOrder.PayingMoney
}
expressOrder.ExpressOrderSign = args[3]
err = writeExpress(express)
if err != nil{
return err
}
err = writeUser(user)
if err != nil{
return err
}
err = writeExpressOrder(expressOrder)
if err != nil{
return err
}
eoBytes,err := json.Marshal(&expressOrder)
if err != nil{
return nil
}
return eoBytes,nil
}
func(t *SimpleChaincode) getExpressOrderById(stub *shim.ChaincodeStub,id string)(ExpressOrder,[]byte,error){
var expressOrder ExpressOrder
eoBytes,err := stub.GetState("expressOrder"+id)
if err != nil{
fmt.Println("Error retrieving data")
}
err = json.Unmarshal(eoBygtes,&ExpessOrder)
if err != nil{
fmt.Println("Error unmarshalling data")
}
return expressOrder,eoByes,nil
}
func(t *SimpleChaincode) getExpress(stub *shim.ChaincodeStub)(Express,[]byte,error){
var express Express
exBytes,err := stub.GetState("Express")
if err != nil{
fmt.Println("Error retrieving data")
}
err = json.Unmarshal(exBytes,&express)
if err != nil {
fmt.Println("Error unmarshalling data")
}
return express,exBytes,nil
}
func(t *SimpleChaincode) getUserByAddress(stub *shim.ChaincodeStub,address string)(User,[]byte,error){
var user User
userBytes,err := stub.GetState(address)
if err != nil{
fmt.Println("Error retrieving data")
}
err = json.Unmarshal(userBytes,&user)
if err != nil {
fmt.Println("Error unmarshalling data")
}
return user,userBytes,nil
}
func(t *SimpleChaincode) getExpressPointerByAddress(stub *shim.ChaincodeStub,address string)(ExpressPointer,[]byte,error){
var expressPointer ExpressPointer
epBytes,err := stub.GetState(address)
if err != nil{
fmt.Println("Error retrieving data")
}
err = json.Unmarshal(epBytes,&expressPointer)
if err != nil {
fmt.Println("Error unmarshalling data")
}
return expressPointer,epBytes,nil
}
func(t *SimpleChaincode) writeExpress(stub *shim.ChaincodeStub,Express express)(error){
exBytes,err := json.Marshal(&express)
if err != nil {
return err
}
err = stub.PutState("Express", exBytes)
if err != nil {
return errors.New("PutState Error" + err.Error())
}
return nil
}
func(t *SimpleChaincode) writeExpressOrder(stub *shim.ChaincodeStub,ExpressOrder expressOrder)(error){
eoBytes,err := json.Marshal(&expressOrder)
if err != nil {
return err
}
id ,_:= strconv.Itoa(expressOrder.Id)
err = stub.PutState("ExpressOrder"+id,eoBytes)
if err != nil {
return errors.New("PutState Error" + err.Error())
}
return nil
}
func(t *SimpleChaincode) writeUser(stub *shim.chaincodeStub,User user)(error){
userBytes,err := json.Marshal(&user)
if err != nil {
return err
}
err = stub.PutState(user.Address, userBytes)
if err != nil {
return errors.New("PutState Error" + err.Error())
}
return nil
}
func(t *SimpleChaincdoe) writeExpressPointer(stub *shim.chaincodeStub,ExpressPointer expressPointer)(error){
epBytes,err := json.Marshal(&expressPointer)
if err != nil {
return err
}
err = stub.PutState(user.Address, userBytes)
if err != nil {
return errors.New("PutState Error" + err.Error())
}
return nil
}