forked from IBM/securitization_blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
94 lines (79 loc) · 4.04 KB
/
lib.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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package main
import (
"encoding/json"
"errors"
"strconv"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
// ============================================================================================================================
// Get Marble - get a marble asset from ledger
// ============================================================================================================================
func get_asset(stub shim.ChaincodeStubInterface, id string) (Asset, error) {
var asset Asset
assetAsBytes, err := stub.GetState(id) //getState retreives a key/value from the ledger
if err != nil { //this seems to always succeed, even if key didn't exist
return asset, errors.New("Failed to find marble - " + id)
}
json.Unmarshal(assetAsBytes, &asset) //un stringify it aka JSON.parse()
if asset.Id != id { //test if marble is actually here or just nil
return asset, errors.New("Asset does not exist - " + id)
}
return asset, nil
}
// ============================================================================================================================
// Get Owner - get the owner asset from ledger
// ============================================================================================================================
func get_originator(stub shim.ChaincodeStubInterface, id string) (Originator, error) {
var originator Originator
originatorAsBytes, err := stub.GetState(id) //getState retreives a key/value from the ledger
if err != nil { //this seems to always succeed, even if key didn't exist
return originator, errors.New("Failed to get Originator - " + id)
}
json.Unmarshal(originatorAsBytes, &originator) //un stringify it aka JSON.parse()
if len(originator.Username) == 0 { //test if owner is actually here or just nil
return originator, errors.New("Originator does not exist - " + id + ", '" + originator.Username + "' '" + originator.Company + "'")
}
return originator, nil
}
func get_investor(stub shim.ChaincodeStubInterface, id string) (Investor, error) {
var investor Investor
investorAsBytes, err := stub.GetState(id) //getState retreives a key/value from the ledger
if err != nil { //this seems to always succeed, even if key didn't exist
return investor, errors.New("Failed to get Investor - " + id)
}
json.Unmarshal(investorAsBytes, &investor) //un stringify it aka JSON.parse()
if len(investor.Username) == 0 { //test if owner is actually here or just nil
return investor, errors.New("Investor does not exist - " + id)
}
return investor, nil
}
// ========================================================
// Input Sanitation - dumb input checking, look for empty strings
// ========================================================
func sanitize_arguments(strs []string) error{
for i, val:= range strs {
if len(val) <= 0 {
return errors.New("Argument " + strconv.Itoa(i) + " must be a non-empty string")
}
if len(val) > 32 {
return errors.New("Argument " + strconv.Itoa(i) + " must be <= 32 characters")
}
}
return nil
}