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
| package main
import ( "fmt" "log" "strconv" "time" "crypto/md5" "encoding/hex" "encoding/json" "github.com/hyperledger/fabric-contract-api-go/contractapi" )
type DonateContract struct { contractapi.Contract }
type Donator struct { ID string `json:"ID"` User string `json:"User"` Password string `json:"Password"` Account int `json:"Account"` }
type QueryHistoryResult struct { TxId string `json:"tx_id"` Value string `json:"value"` IsDel string `json:"is_del"` OnChainTime string `json:"on_chain_time"` }
func (s *DonateContract) InitLedger(ctx contractapi.TransactionContextInterface) error { donators := []Donator{ {ID: "8c8b6928", User: "mfwc",Password: "password1", Account: 0}, {ID: "bdb26a7e", User: "zhjy",Password: "password2", Account: 0}, {ID: "c1859b89", User: "jbzz",Password: "password3", Account: 0}, }
for _, donator := range donators { donatorJSON, err := json.Marshal(donator) if err != nil { return err }
err = ctx.GetStub().PutState(donator.ID, donatorJSON) if err != nil { return fmt.Errorf("failed to put to world state. %v", err) } }
return nil }
func (s *DonateContract) CreateDonator(ctx contractapi.TransactionContextInterface, id string, user string, password string, account int) error { exists, err := s.DonatorExists(ctx, id) if err != nil { return err } if exists { return fmt.Errorf("the donator %s already exists", id) }
donator := Donator{ ID: id, User: user, Password: password, Account: account, } donatorJSON, err := json.Marshal(donator) if err != nil { return err }
return ctx.GetStub().PutState(id, donatorJSON) }
func (s *DonateContract) ReadDonator(ctx contractapi.TransactionContextInterface, id string) (*Donator, error) { donatorJSON, err := ctx.GetStub().GetState(id) if err != nil { return nil, fmt.Errorf("failed to read from world state: %v", err) } if donatorJSON == nil { return nil, fmt.Errorf("the donator %s does not exist", id) }
var donator Donator err = json.Unmarshal(donatorJSON, &donator) if err != nil { return nil, err }
return &donator, nil }
func (s *DonateContract) UpdateDonator(ctx contractapi.TransactionContextInterface, id string, user string, password string, account int) error { exists, err := s.DonatorExists(ctx, id) if err != nil { return err } if !exists { return fmt.Errorf("the donator %s does not exist", id) }
donator := Donator{ ID: id, User: user, Password: password, Account: account, } donatorJSON, err := json.Marshal(donator) if err != nil { return err }
return ctx.GetStub().PutState(id, donatorJSON) }
func (s *DonateContract) DeleteDonator(ctx contractapi.TransactionContextInterface, id string) error { exists, err := s.DonatorExists(ctx, id) if err != nil { return err } if !exists { return fmt.Errorf("the donator %s does not exist", id) }
return ctx.GetStub().DelState(id) }
func (s *DonateContract) GetAllDonators(ctx contractapi.TransactionContextInterface) ([]*Donator, error) { resultsIterator, err := ctx.GetStub().GetStateByRange("", "") if err != nil { return nil, err } defer resultsIterator.Close()
var donators []*Donator for resultsIterator.HasNext() { queryResponse, err := resultsIterator.Next() if err != nil { return nil, err }
var donator Donator err = json.Unmarshal(queryResponse.Value, &donator) if err != nil { return nil, err } donators = append(donators, &donator) }
return donators, nil }
func (s *DonateContract) DonatorExists(ctx contractapi.TransactionContextInterface, id string) (bool, error) { donatorJSON, err := ctx.GetStub().GetState(id) if err != nil { return false, fmt.Errorf("failed to read from world state: %v", err) }
return donatorJSON != nil, nil }
func (s *DonateContract) Donate(ctx contractapi.TransactionContextInterface, donoid string, doneid string, money int) error { donor, err := s.ReadDonator(ctx, donoid) if err != nil { return err } donee, err := s.ReadDonator(ctx, doneid) if err != nil { return err } s.UpdateDonator(ctx,donor.ID,donor.User,donor.Password,donor.Account-money) s.UpdateDonator(ctx,donee.ID,donee.User,donee.Password,donee.Account+money) return nil }
func (s *DonateContract) signin(ctx contractapi.TransactionContextInterface, id string, user string, password string) error { s.CreateDonator(ctx,id,user,MD5(password),0) return nil }
func MD5(text string) string { ctx := md5.New() ctx.Write([]byte(text)) return hex.EncodeToString(ctx.Sum(nil)) }
func (s *DonateContract) Check(ctx contractapi.TransactionContextInterface, id string, password string) (bool, error) { user, err := s.ReadDonator(ctx, id) if err != nil { return false,err } if MD5(password)==user.Password { return true,nil } else{ return false,nil } }
func (s *DonateContract) Charge(ctx contractapi.TransactionContextInterface, donoid string, money int) error { donor, err := s.ReadDonator(ctx, donoid) if err != nil { return err } s.UpdateDonator(ctx,donor.ID,donor.User,donor.Password,donor.Account+money) return nil }
func (s *DonateContract) GetHistory(ctx contractapi.TransactionContextInterface, id string) ([]QueryHistoryResult, error) { resultsIterator, err := ctx.GetStub().GetHistoryForKey(id) if err != nil { return nil, err } defer resultsIterator.Close() results := make([]QueryHistoryResult, 0) for resultsIterator.HasNext() { if queryResponse, err := resultsIterator.Next();err==nil{ res := QueryHistoryResult{} res.TxId=queryResponse.TxId res.Value=string(queryResponse.Value) res.IsDel=strconv.FormatBool(queryResponse.IsDelete) res.OnChainTime=time.Unix(queryResponse.Timestamp.Seconds,0).Format("2006-01-02 15:04:05") results= append(results, res) } if err!=nil { return nil,err } } return results, nil }
func main() { donateChaincode, err := contractapi.NewChaincode(&DonateContract{}) if err != nil { log.Panicf("Error creating donate chaincode: %v", err) } if err := donateChaincode.Start(); err != nil { log.Panicf("Error starting donate chaincode: %v", err) } }
|