-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleAsset.go
84 lines (71 loc) · 2.04 KB
/
simpleAsset.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
package main
import ("fmt"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/peer"
"html/template"
"net/http"
)
type SimpleAsset struct{
}
//shim
//Chain code interface-Init, Invoke
//Chain code stub interface
//Init is to instantiate the chain code and to initalize any data
func(t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response{
//1)tranx proposal
args:= stub.GetStringArgs()
//Tranx will have key and value
if len(args)!=2{
return shim.Error("Expecting two value i.e. key and value")
}
//store the key and value to the ledger
err:= stub.PutState(args[0], []byte(args[1]))
if err!=nil{
return shim.Error(fmt.Sprintf("Falied to create the asset %s",args[0]))
}
return shim.Success(nil)
}
//Invoke the chain code
func(t *SimpleAsset) Invoke(stub shim.ChaincodeStubInterface) peer.Response{
var result string
var err error
fn, args:= stub.GetFunctionAndParameters()
if fn == "set"{
result,err = set(stub,args)
} else {
result,err = get(stub,args)
}
if err!=nil{
return shim.Error( err.Error())
}
return shim.Success([]byte(result))
}
//implementing our chain code
func set(stub shim.ChaincodeStubInterface, args []string) (string, error){
if len(args) !=2 {
return "",fmt.Errorf("Expecting key and value for the asset")
}
err := stub.PutState(args[0],[]byte(args[1]))
if err!=nil{
return "",fmt.Errorf("Falied to set the asset %s",args[0])
}
return args[1],nil
}
func get(stub shim.ChaincodeStubInterface,args []string) (string,error){
if len(args)!=2{
return "",fmt.Errorf("Expecting key and value for the asset")
}
value,err:=stub.GetState(args[0])
if err!=nil{
return "",fmt.Errorf("Failed to get the asset %s",args[0])
}
if value==nil{
return "",fmt.Errorf("No value was found.Please enter the correct key")
}
return string(value),nil
}
func main(){
if err:= shim.Start(new(SimpleAsset)); err!=nil{
fmt.Printf("Not able to run chaincode SimpleAsset")
}
}