-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathown.go
218 lines (204 loc) · 5.61 KB
/
own.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
package main
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/fentec-project/gofe/abe"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/peer"
"hash/fnv"
)
const (
CPABE_PUBLIC_KEY = "public_key"
CPABE_MASTER_KEY = "master_key"
)
type Behaviour struct {
Id string `json:"id"`
Result bool `json:"result"`
}
type OwnChaincode struct {
}
func (t *OwnChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response {
args := stub.GetStringArgs()
if len(args) != 2 {
return shim.Error("Should be 2 args")
}
pk, err := hex.DecodeString(args[0])
if err != nil {
return shim.Error("Fail to decode hex pk")
}
fmt.Println("Generating public key......")
if err := stub.PutState(CPABE_PUBLIC_KEY, pk); err != nil {
return shim.Error(err.Error())
}
sk, err := hex.DecodeString(args[1])
if err != nil {
return shim.Error("Fail to decode hex sk")
}
fmt.Println("Generating master key......")
if err := stub.PutState(CPABE_MASTER_KEY, sk); err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *OwnChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
name, params := stub.GetFunctionAndParameters()
switch name {
case "beforeEncrypt":
pk, err := t.beforeEncrypt(stub, params)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(pk)
case "decrypt":
data, err := t.decrypt(stub, params)
if err != nil {
//用空来判断,记录每一次下载尝试
return shim.Success([]byte(""))
}
return shim.Success(data)
case "appendTrans":
err := t.appendTrans(stub, params)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success([]byte("success"))
case "getAudit":
data, err := t.getAudit(stub, params)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(data)
default:
return shim.Error("Not supported function")
}
}
type auditHistory struct {
Audit string `json:"audit"`
TimeStamp *timestamp.Timestamp `json:"time_stamp"`
TxId string `json:"tx_id"`
}
func (t *OwnChaincode) getAudit(stub shim.ChaincodeStubInterface, params []string) ([]byte, error) {
if len(params) != 1 {
return nil, errors.New("Need one file id")
}
history, _ := stub.GetHistoryForKey(params[0])
var ret []auditHistory
for history.HasNext() {
data, _ := history.Next()
record := auditHistory{
Audit: string(data.Value),
TimeStamp: data.Timestamp,
TxId: data.TxId,
}
ret = append(ret, record)
}
data, _ := json.Marshal(ret)
return data, nil
}
func (t *OwnChaincode) beforeEncrypt(stub shim.ChaincodeStubInterface, params []string) ([]byte, error) {
if len(params) < 3 {
return nil, errors.New("Not sufficient parameters")
}
from_department_id := params[0]
to_department_id := params[1]
transaction_id := params[2]
//check whether transaction exists
transKey := fmt.Sprintf("trans_%s_%s_%s", from_department_id, to_department_id, transaction_id)
tran, err := stub.GetState(transKey)
if err != nil || tran == nil {
return nil, errors.New("No such transaction")
}
data, err := stub.GetState(CPABE_PUBLIC_KEY)
if err != nil || data == nil {
return nil, errors.New("Fail to get public key")
}
hexpk := hex.EncodeToString(data)
return []byte(hexpk), nil
}
func (t *OwnChaincode) decrypt(stub shim.ChaincodeStubInterface, params []string) ([]byte, error) {
success := false
if len(params) < 3 {
return nil, errors.New("Not sufficient parameters")
}
text := params[0]
//user id & file id
fileId := params[1]
userId := params[2]
defer func() {
_ = stub.PutState(fileId, []byte(fmt.Sprintf("%s-%t", userId, success)))
}()
attrs := params[3:]
attrInt := make([]int, len(attrs))
for k, v := range attrs {
f := fnv.New32()
f.Write([]byte(v))
attrInt[k] = int(f.Sum32())
}
sk, err := getPrivateKey(stub)
if err != nil {
return nil, err
}
//keygen
ak, err := Keygen(attrInt, sk)
if err != nil {
return nil, fmt.Errorf("Fail to generate key, %v", err)
}
//decrypt
cipher, err := DecodeCipher(text)
if err != nil {
return nil, fmt.Errorf("Fail to get ciphertext, %v", err)
}
pk, err := getPublicKey(stub)
if err != nil {
return nil, err
}
str, err := Decrypt(cipher, ak, pk)
if err != nil {
return nil, fmt.Errorf("Fail to decrypt encrypted key, %v", err)
}
success = true
return []byte(str), nil
}
func (t *OwnChaincode) appendTrans(stub shim.ChaincodeStubInterface, params []string) error {
if len(params) < 3 {
return errors.New("Not sufficient parameters")
}
from_department_id := params[0]
to_department_id := params[1]
transaction_id := params[2]
key := fmt.Sprintf("trans_%s_%s_%s", from_department_id, to_department_id, transaction_id)
if err := stub.PutState(key, []byte("yes")); err != nil {
return errors.New("Fail to put tranaction state")
}
return nil
}
func getPublicKey(stub shim.ChaincodeStubInterface) (*abe.FAMEPubKey, error) {
data, err := stub.GetState(CPABE_PUBLIC_KEY)
if err != nil || data == nil {
return nil, errors.New("Fail to get public key")
}
pk, err := DecodePubkey(data)
if err != nil {
return nil, errors.New("Fail to decode public key")
}
return pk, nil
}
func getPrivateKey(stub shim.ChaincodeStubInterface) (*abe.FAMESecKey, error) {
data, err := stub.GetState(CPABE_MASTER_KEY)
if err != nil || data == nil {
return nil, errors.New("Fail to get master key")
}
sk, err := DecodePrikey(data)
if err != nil {
return nil, errors.New("Fail to decode master key")
}
return sk, nil
}
func main() {
if err := shim.Start(&OwnChaincode{}); err != nil {
fmt.Printf("Fatal error %v", err)
}
}