-
Notifications
You must be signed in to change notification settings - Fork 272
/
bondasset.go
333 lines (286 loc) · 9.84 KB
/
bondasset.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
/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"time"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/cacti/weaver/common/protos-go/v2/common"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
)
type BondAsset struct {
Type string `json:"type"`
ID string `json:"id"`
Owner string `json:"owner"`
Issuer string `json:"issuer"`
FaceValue int `json:"facevalue"`
MaturityDate time.Time `json:"maturitydate"`
}
func getBondAssetKey(assetType string, assetId string) string {
return assetType + assetId
}
func getBondAsset(ctx contractapi.TransactionContextInterface, assetType, id string) (*BondAsset, error) {
assetJSON, err := ctx.GetStub().GetState(getBondAssetKey(assetType, id))
if err != nil {
return nil, fmt.Errorf("failed to read asset record from world state: %v", err)
}
if assetJSON == nil {
return nil, fmt.Errorf("the asset %s does not exist", id)
}
var asset BondAsset
err = json.Unmarshal(assetJSON, &asset)
if err != nil {
return nil, err
}
return &asset, nil
}
// InitBondAssetLedger adds a base set of assets to the ledger
func (s *SmartContract) InitBondAssetLedger(ctx contractapi.TransactionContextInterface) error {
assets := []BondAsset{
{Type: "t1", ID: "a01", Issuer: "Treasury" , Owner: "", FaceValue: 300,
MaturityDate: time.Date(2022, time.April, 1, 12, 0, 0, 0, time.UTC)},
{Type: "t1", ID: "a02", Issuer: "Treasury" , Owner: "", FaceValue: 400,
MaturityDate: time.Date(2022, time.July, 1, 12, 0, 0, 0, time.UTC)},
}
for _, asset := range assets {
assetJSON, err := json.Marshal(asset)
if err != nil {
return err
}
err = ctx.GetStub().PutState(getBondAssetKey(asset.Type, asset.ID), assetJSON)
if err != nil {
return fmt.Errorf("failed to put to world state. %v", err)
}
}
return nil
}
// CreateAsset issues a new asset to the world state with given details.
func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface, assetType, id, owner, issuer string, faceValue int, maturityDate string) error {
if assetType == "" {
return fmt.Errorf("Asset type cannot be blank")
}
if id == "" {
return fmt.Errorf("Asset ID cannot be blank")
}
if owner == "" && issuer == "" {
return fmt.Errorf("Asset Owner and Issuer cannot both be blank")
}
exists, err := s.AssetExists(ctx, assetType, id)
if err != nil {
return err
}
if exists {
return fmt.Errorf("the asset %s already exists", id)
}
md_time, err := time.Parse(time.RFC822, maturityDate)
if err != nil {
return fmt.Errorf("maturity date provided is not in correct format, please use this format: %s", time.RFC822)
}
if md_time.Before(time.Now()) {
return fmt.Errorf("maturity date can not be in past.")
}
asset := BondAsset{
Type: assetType,
ID: id,
Owner: owner,
Issuer: issuer,
FaceValue: faceValue,
MaturityDate: md_time,
}
assetJSON, err := json.Marshal(asset)
if err != nil {
return err
}
return ctx.GetStub().PutState(getBondAssetKey(assetType, id), assetJSON)
}
// ReadAsset returns the asset stored in the world state with given id.
// This function is called with the parameter inUpdateOwnerContext value as false, except in the update-owner context
func (s *SmartContract) ReadAsset(ctx contractapi.TransactionContextInterface, assetType, id string) (*BondAsset, error) {
asset, err := getBondAsset(ctx, assetType, id)
if err != nil {
return nil, err
}
if !checkAccessToAsset(s, ctx, asset) {
return nil, fmt.Errorf("cannot access Bond Asset %s", id)
}
return asset, nil
}
// DeleteAsset deletes an given asset from the world state.
func (s *SmartContract) DeleteAsset(ctx contractapi.TransactionContextInterface, assetType, id string) error {
// Read the asset (which internally check access)
asset, err := s.ReadAsset(ctx, assetType, id)
if err != nil {
return err
}
return ctx.GetStub().DelState(getBondAssetKey(asset.Type, asset.ID))
}
// isCallerAssetOwner returns true only if the invoker of the transaction is also the asset owner
func isCallerAssetOwner(ctx contractapi.TransactionContextInterface, asset *BondAsset) bool {
caller, err := getECertOfTxCreatorBase64(ctx)
if err != nil {
fmt.Println(err.Error())
return false
}
return (asset.Owner == caller)
}
// isBondAssetLocked returns true only if the asset is presently locked
func isBondAssetLocked(s *SmartContract, ctx contractapi.TransactionContextInterface, asset *BondAsset) bool {
bondAssetAgreement := &common.AssetExchangeAgreement{
AssetType: asset.Type,
Id: asset.ID,
Recipient: "*",
Locker: asset.Owner,
}
bondAssetAgreementProtoSerialized, err := proto.Marshal(bondAssetAgreement)
if err != nil {
fmt.Println(err.Error())
return false
}
bondAssetAgreementProto64 := base64.StdEncoding.EncodeToString(bondAssetAgreementProtoSerialized)
locked, err := s.IsAssetLocked(ctx, bondAssetAgreementProto64)
if err != nil {
fmt.Println(err.Error())
return false
}
return locked
}
// isBondAssetLockedForMe returns true only if the asset is presently locked for me
func isBondAssetLockedForMe(s *SmartContract, ctx contractapi.TransactionContextInterface, asset *BondAsset) bool {
bondAssetAgreement := &common.AssetExchangeAgreement{
AssetType: asset.Type,
Id: asset.ID,
Recipient: "",
Locker: asset.Owner,
}
bondAssetAgreementProtoSerialized, err := proto.Marshal(bondAssetAgreement)
if err != nil {
fmt.Println(err.Error())
return false
}
bondAssetAgreementProto64 := base64.StdEncoding.EncodeToString(bondAssetAgreementProtoSerialized)
locked, err := s.IsAssetLocked(ctx, bondAssetAgreementProto64)
if err != nil {
fmt.Println(err.Error())
return false
}
return locked
}
// checkAccessToAsset checks several conditions under which an asset can be put on hold (i.e., not available for regular business operation)
func checkAccessToAsset(s *SmartContract, ctx contractapi.TransactionContextInterface, asset *BondAsset) bool {
// Ensure that the client is the owner of the asset
if !isCallerAssetOwner(ctx, asset) {
fmt.Printf("Illegal update: caller is not owner of asset %s\n", asset.ID)
return false
}
// Ensure that the asset is not locked
if isBondAssetLocked(s, ctx, asset) {
fmt.Printf("Cannot update attributes of locked asset %s\n", asset.ID)
return false
}
return true
}
// AssetExists returns true when asset with given ID exists in world state
func (s *SmartContract) AssetExists(ctx contractapi.TransactionContextInterface, assetType, id string) (bool, error) {
assetJSON, err := ctx.GetStub().GetState(getBondAssetKey(assetType, id))
if err != nil {
return false, fmt.Errorf("failed to read asset record from world state: %v", err)
}
return assetJSON != nil, nil
}
// IsAssetReleased returns true if asset maturity date elapses
func (s *SmartContract) IsAssetReleased(ctx contractapi.TransactionContextInterface, assetType, id string) (bool, error) {
asset, err := s.ReadAsset(ctx, assetType, id)
if err != nil {
return false, err
}
currDate := time.Now()
if (currDate.After(asset.MaturityDate)) {
return true, nil
}
return false, nil
}
// UpdateOwner sets the owner of an asset to a new owner.
func (s *SmartContract) UpdateOwner(ctx contractapi.TransactionContextInterface, assetType, id string, newOwner string) error {
// Read asset (which internally checks access if it is free to modified)
asset, err := s.ReadAsset(ctx, assetType, id)
if err != nil {
return err
}
asset.Owner = newOwner
assetJSON, err := json.Marshal(asset)
if err != nil {
return err
}
return ctx.GetStub().PutState(getBondAssetKey(assetType, id), assetJSON)
}
// UpdateMaturityDate sets the maturity date of the asset to an updated date as passed in the parameters.
func (s *SmartContract) UpdateMaturityDate(ctx contractapi.TransactionContextInterface, assetType, id string, newMaturityDate time.Time) error {
// Read asset (which internally checks access if it is free to modified)
asset, err := s.ReadAsset(ctx, assetType, id)
if err != nil {
return err
}
asset.MaturityDate = newMaturityDate
assetJSON, err := json.Marshal(asset)
if err != nil {
return err
}
return ctx.GetStub().PutState(getBondAssetKey(assetType, id), assetJSON)
}
// UpdateFaceValue sets the face value of an asset to the new value passed.
func (s *SmartContract) UpdateFaceValue(ctx contractapi.TransactionContextInterface, assetType, id string, newFaceValue int) error {
// Read asset (which internally checks access if it is free to modified)
asset, err := s.ReadAsset(ctx, assetType, id)
if err != nil {
return err
}
asset.FaceValue = newFaceValue
assetJSON, err := json.Marshal(asset)
if err != nil {
return err
}
return ctx.GetStub().PutState(getBondAssetKey(assetType, id), assetJSON)
}
// GetMyAssets returns the assets owner by the caller
func (s *SmartContract) GetMyAssets(ctx contractapi.TransactionContextInterface) ([]*BondAsset, error) {
assets, err := s.GetAllAssets(ctx)
if err != nil {
return nil, err
}
var myassets []*BondAsset
for _, asset := range assets {
if checkAccessToAsset(s, ctx, asset) {
myassets = append(myassets, asset)
}
}
return myassets, nil
}
// GetAllAssets returns all assets found in world state
func (s *SmartContract) GetAllAssets(ctx contractapi.TransactionContextInterface) ([]*BondAsset, error) {
// range query with empty string for startKey and endKey does an
// open-ended query of all assets in the chaincode namespace.
resultsIterator, err := ctx.GetStub().GetStateByRange("", "")
if err != nil {
return nil, err
}
defer resultsIterator.Close()
var assets []*BondAsset
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return nil, err
}
var asset BondAsset
err = json.Unmarshal(queryResponse.Value, &asset)
if err != nil {
return nil, err
}
assets = append(assets, &asset)
}
return assets, nil
}