Skip to content

Instantly share code, notes, and snippets.

@vitiko
vitiko / state_mapping_test.go
Created February 20, 2019 21:45
Chaincode state mapping test
package mapping_test
import (
"testing"
"github.com/hyperledger/fabric/protos/peer"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/proto"
@vitiko
vitiko / commercial_paper_chaincode.go
Last active February 21, 2019 13:36
Commecial paper chaincode
package cpaper
import (
"fmt"
"github.com/pkg/errors"
"github.com/s7techlab/cckit/examples/cpaper/schema"
"github.com/s7techlab/cckit/extensions/debug"
"github.com/s7techlab/cckit/extensions/encryption"
"github.com/s7techlab/cckit/extensions/owner"
@vitiko
vitiko / commercial_maper_state_mapping.go
Created February 20, 2019 21:35
Commercial paper state mapping
var (
// State mappings
StateMappings = m.StateMappings{}.
//key namespace will be <`CommercialPaper`, Issuer, PaperNumber>
Add(&schema.CommercialPaper{}, m.PKeySchema(&schema.CommercialPaperId{}))
// EventMappings
EventMappings = m.EventMappings{}.
// event name will be `IssueCommercialPaper`, payload - same as issue payload
Add(&schema.IssueCommercialPaper{}).
@vitiko
vitiko / commercial_paper.proto
Created February 20, 2019 21:26
Commercial paper proto definition
syntax = "proto3";
package schema;
import "google/protobuf/timestamp.proto";
message CommercialPaper {
enum State {
ISSUED = 0;
TRADING = 1;
@vitiko
vitiko / keyer_interface.go
Created February 20, 2019 20:59
Keyer interface.go
type (
// Key type
Key []string
// Keyer interface for entity containing logic of its key creation
Keyer interface {
Key() (Key, error)
}
)
@vitiko
vitiko / chaincode_key_funcs.go
Last active February 20, 2019 20:54
Chaincode key functions from ChaincodeStubInterface
interface ChaincodeStubInterface {
// The function creates a key by combining the attributes into a single string.
// The arguments must be valid utf8 strings and must not contain U+0000 (nil byte) and U+10FFFF charactres.
func CreateCompositeKey(objectType string, attributes []string) (string, error)
// The function splits the compositeKey into attributes from which the key was formed.
// This function is useful for extracting attributes from keys returned by range queries.
func SplitCompositeKey(compositeKey string) (string, []string, error)
}
@vitiko
vitiko / tofrom_byte_interface.go
Created February 20, 2019 05:34
ToByter / From byter interfaces
type (
  // FromByter interface supports FromBytes func for converting from slice of bytes to target type
FromByter interface {
FromBytes([]byte) (interface{}, error)
}
// ToByter interface supports ToBytes func for converting to slice of bytes from source type
ToByter interface {
ToBytes() ([]byte, error)
}
@vitiko
vitiko / cckit_state_interface.go
Created February 20, 2019 05:30
CCKit state interface
type State interface {
// Get returns value from state, converted to target type
// entry can be Key (string or []string) or type implementing Keyer interface
Get(entry interface{}, target ...interface{}) (result interface{}, err error)
// Get returns value from state, converted to int
// entry can be Key (string or []string) or type implementing Keyer interface
GetInt(entry interface{}, defaultValue int) (result int, err error)
// GetHistory returns slice of history records for entry, with values converted to target type
@vitiko
vitiko / working_with_cc_state.go
Last active February 20, 2019 05:23
Working with chaincode state using ChaincodeStubeInterface
ct := ContractType{}
err := json.Unmarshal([]byte(args[0]), &req)
if err != nil {
return shim.Error(err.Error())
}
key, err := stub.CreateCompositeKey(prefixContractType, []string{req.UUID})
if err != nil {
return shim.Error(err.Error())
@vitiko
vitiko / erc20test.go
Created November 11, 2018 19:04
erc20test.go
Describe("ERC-20 transfer", func() {
It("Disallow to transfer token to same account", func() {
expectcc.ResponseError(
erc20fs.From(actors[`token_owner`]).Invoke(
`transfer`, actors[`token_owner`].GetMSPID(), actors[`token_owner`].GetID(), 100),
ErrForbiddenToTransferToSameAccount)
})
It("Disallow token holder with zero balance to transfer tokens", func() {
expectcc.ResponseError(