Skip to content

Instantly share code, notes, and snippets.

@vitiko
vitiko / gist:4a5e1a9b92b4c669c0e35538e77e9d99
Last active August 31, 2017 07:12
Truffle compile solidity smart contract - fast alternative using solc binary, dumping results in truffle build file format
#!/usr/bin/env node
const fs = require('fs');
const { exec } = require('child_process');
const truffleBuildTemplate = {
contract_name: null,
abi: null,
unlinked_binary: null,
networks: {},
schema_version: "0.0.5",
@vitiko
vitiko / chaincode_interface.go
Last active May 20, 2018 14:15
Golang chaincode interface
type Chaincode interface {
// Init is called during Instantiate transaction
Init(stub ChaincodeStubInterface) pb.Response
// Invoke is called to update or query the ledger
Invoke(stub ChaincodeStubInterface) pb.Response
}
type ChaincodeStubInterface interface {
// GetArgs returns the arguments intended for the chaincode Init and Invoke
GetArgs() [][]byte
// InvokeChaincode locally calls the specified chaincode
InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response
// GetState returns the value of the specified `key` from the ledger.
GetState(key string) ([]byte, error)
// PutState puts the specified `key` and `value` into the transaction's writeset as a data-write proposal.
PutState(key string, value []byte) error
// DelState records the specified `key` to be deleted in the writeset of the transaction proposal.
@vitiko
vitiko / chaincode_example.go
Last active May 20, 2018 14:31
Chaincode example
func (t *SimpleAsset) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
fn, args := stub.GetFunctionAndParameters()
var result string
var err error
if fn == "set" {
result, err = set(stub, args)
} else { // assume 'get' even if fn is nil
result, err = get(stub, args)
}
if err != nil {
@vitiko
vitiko / cars_chaincode_example.go
Last active September 4, 2018 19:16
Cars chaincode example
// Simple CRUD chaincode for store information about cars
package cars
import (
"errors"
"time"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/protos/peer"
"github.com/s7techlab/cckit/extensions/owner"
@vitiko
vitiko / car_chaincode_test.go
Created May 20, 2018 14:53
Cars chaincode test example
package main
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
examplecert "github.com/s7techlab/cckit/examples/cert"
"github.com/s7techlab/cckit/extensions/owner"
"github.com/s7techlab/cckit/identity"
@vitiko
vitiko / chaincode_creation_func.go
Created May 20, 2018 15:04
Chaincode "constructor"
func New() *Chaincode {
r := router.New(`cars`) // also initialized logger with "cars" prefix
r.Group(`car`).
Query(`List`, cars). // chain code method name is carList
Query(`Get`, car, p.String(`id`)). // chain code method name is carGet, method has 1 string argument "id"
Invoke(`Register`, carRegister, p.Struct(`car`, &CarPayload{}), // 1 struct argument
owner.Only) // allow access to method only for chaincode owner (authority)
return &Chaincode{r}
@vitiko
vitiko / cars_chaincode_basemethods.go
Created May 20, 2018 19:11
Cars chaincode base methods
// Init initializes chain code - sets chaincode "owner"
func (cc *Chaincode) Init(stub shim.ChaincodeStubInterface) peer.Response {
// set owner of chain code with special permissions , based on tx creator certificate
// owner info stored in chaincode state as entry with key "OWNER" and content is serialized "Grant" structure
return owner.SetFromCreator(cc.router.Context(`init`, stub))
}
// Invoke - entry point for chain code invocations
func (cc *Chaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
// delegate handling to router
@vitiko
vitiko / cars_chaincode_asset.go
Created May 20, 2018 19:45
Cars chaincode asset
// Car struct for chaincode state
type Car struct {
Id string
Title string
Owner string
UpdatedAt time.Time // set by chaincode method
}
// CarPayload chaincode method argument
@vitiko
vitiko / cars_chaincode_methods_implementation.go
Created May 20, 2018 19:52
Cars chaincode methods implementation
// Key for car entry in chaincode state
func Key(id string) []string {
return []string{CarKeyPrefix, id}
}
// car get info chaincode method handler
func car(c router.Context) (interface{}, error) {
return c.State().Get( // get state entry
Key(c.ArgString(`id`)), // by composite key using CarKeyPrefix and car.Id
&Car{}) // and unmarshal from []byte to Car struct