|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package enclave_go |
| 8 | + |
| 9 | +import ( |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + |
| 13 | + "github.com/hyperledger/fabric-chaincode-go/shim" |
| 14 | + pb "github.com/hyperledger/fabric-protos-go/peer" |
| 15 | +) |
| 16 | + |
| 17 | +const SKVSKey = "SKVS" |
| 18 | + |
| 19 | +type SkvsStubInterface struct { |
| 20 | + *FpcStubInterface |
| 21 | + allDataOld map[string][]byte |
| 22 | + allDataNew map[string][]byte |
| 23 | + key string |
| 24 | +} |
| 25 | + |
| 26 | +func NewSkvsStubInterface(stub shim.ChaincodeStubInterface, input *pb.ChaincodeInput, rwset *readWriteSet, sep StateEncryptionFunctions) *SkvsStubInterface { |
| 27 | + fpcStub := NewFpcStubInterface(stub, input, rwset, sep) |
| 28 | + skvsStub := &SkvsStubInterface{ |
| 29 | + FpcStubInterface: fpcStub, |
| 30 | + allDataOld: make(map[string][]byte), |
| 31 | + allDataNew: make(map[string][]byte), |
| 32 | + key: SKVSKey, |
| 33 | + } |
| 34 | + err := skvsStub.initSKVS() |
| 35 | + if err != nil { |
| 36 | + panic(fmt.Sprintf("Initializing SKVS failed, err: %v", err)) |
| 37 | + } |
| 38 | + return skvsStub |
| 39 | +} |
| 40 | + |
| 41 | +func (s *SkvsStubInterface) initSKVS() error { |
| 42 | + |
| 43 | + // get current state, this will only operate once |
| 44 | + encValue, err := s.GetPublicState(s.key) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + // return if the key initially does not exist |
| 50 | + if len(encValue) == 0 { |
| 51 | + logger.Warningf("SKVS is empty, Initiating.") |
| 52 | + return nil |
| 53 | + } |
| 54 | + |
| 55 | + value, err := s.sep.DecryptState(encValue) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + logger.Debug("SKVS has default value, loading current value.") |
| 60 | + |
| 61 | + err = json.Unmarshal(value, &s.allDataOld) |
| 62 | + if err != nil { |
| 63 | + logger.Errorf("SKVS Json unmarshal error: %s", err) |
| 64 | + return err |
| 65 | + } |
| 66 | + err = json.Unmarshal(value, &s.allDataNew) |
| 67 | + if err != nil { |
| 68 | + logger.Errorf("SKVS Json unmarshal error: %s", err) |
| 69 | + return err |
| 70 | + } |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func (s *SkvsStubInterface) GetState(key string) ([]byte, error) { |
| 75 | + value, found := s.allDataOld[key] |
| 76 | + if !found { |
| 77 | + logger.Errorf("skvs allDataOld key: %s, not found", key) |
| 78 | + return nil, nil |
| 79 | + } |
| 80 | + return value, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (s *SkvsStubInterface) PutState(key string, value []byte) error { |
| 84 | + |
| 85 | + s.allDataNew[key] = value |
| 86 | + byteAllData, err := json.Marshal(s.allDataNew) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + encValue, err := s.sep.EncryptState(byteAllData) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + return s.PutPublicState(s.key, encValue) |
| 96 | +} |
| 97 | + |
| 98 | +func (s *SkvsStubInterface) DelState(key string) error { |
| 99 | + delete(s.allDataNew, key) |
| 100 | + byteAllData, err := json.Marshal(s.allDataNew) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + encValue, err := s.sep.EncryptState(byteAllData) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + return s.PutPublicState(s.key, encValue) |
| 109 | +} |
| 110 | + |
| 111 | +func (s *SkvsStubInterface) GetStateByRange(startKey string, endKey string) (shim.StateQueryIteratorInterface, error) { |
| 112 | + panic("not implemented") // TODO: Implement |
| 113 | +} |
| 114 | + |
| 115 | +func (s *SkvsStubInterface) GetStateByRangeWithPagination(startKey string, endKey string, pageSize int32, bookmark string) (shim.StateQueryIteratorInterface, *pb.QueryResponseMetadata, error) { |
| 116 | + panic("not implemented") // TODO: Implement |
| 117 | +} |
0 commit comments