Skip to content

Commit

Permalink
move legacy to classic package
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkhala committed Feb 1, 2024
1 parent c4e89b7 commit e5bc29f
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 41 deletions.
19 changes: 7 additions & 12 deletions cid/cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@ package cid
import (
"crypto/x509"
"fmt"
"github.com/davidkhala/fabric-common/golang/format"
. "github.com/davidkhala/goutils"
. "github.com/davidkhala/goutils/crypto"
"github.com/davidkhala/goutils/crypto"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-chaincode-go/pkg/attrmgr"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/msp"
)

// MSPID Hyperledger Fabric Member Service Provider ID
type MSPID = string

// CertificateID a unique ID associated with the x509 certificate identity
type CertificateID = string

// ClientIdentity alternative of creator starting from 1.1
type ClientIdentity struct {
MspID MSPID
MspID format.MSPID
CertificatePem string
Attrs map[string]string `json:"attrs"`
Attrs map[string]string `json:"Attrs,omitempty"`
}

func NewClientIdentity(stub shim.ChaincodeStubInterface) (c ClientIdentity) {
Expand All @@ -47,15 +42,15 @@ func (c ClientIdentity) GetAttributeValue(attrName string) string {
}

// GetID returns a unique ID associated with the invoking identity.
func (c ClientIdentity) GetID() CertificateID {
func (c ClientIdentity) GetID() format.CertificateID {
// The leading "x509::" distinguishes this as an X509 certificate, and
// the subject and issuer DNs uniquely identify the X509 certificate.
// The resulting ID will remain the same if the certificate is renewed.
var certificate = c.GetCertificate()
id := fmt.Sprintf("x509::%s::%s", GetDN(certificate.Subject), GetDN(certificate.Issuer))
id := fmt.Sprintf("x509::%s::%s", crypto.GetDN(certificate.Subject), crypto.GetDN(certificate.Issuer))
return id
}

func (c ClientIdentity) GetCertificate() *x509.Certificate {
return ParseCertPemOrPanic([]byte(c.CertificatePem))
return crypto.ParseCertPemOrPanic([]byte(c.CertificatePem))
}
23 changes: 23 additions & 0 deletions cid/cid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cid

import (
"github.com/davidkhala/goutils"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)

func TestSyntax(t *testing.T) {
t.Run("cid to json", func(t *testing.T) {
var id_nil = ClientIdentity{
MspID: "msp",
CertificatePem: "cert",
Attrs: nil,
}
assert.False(t, strings.Contains(string(goutils.ToJson(id_nil)), "Attrs"))
id_nil.Attrs = map[string]string{}
assert.False(t, strings.Contains(string(goutils.ToJson(id_nil)), "Attrs"))
id_nil.Attrs["foo"] = "bar"
assert.True(t, strings.Contains(string(goutils.ToJson(id_nil)), "Attrs"))
})
}
2 changes: 1 addition & 1 deletion mock.go → classic/args.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golang
package classic

type Args struct {
buff [][]byte
Expand Down
12 changes: 11 additions & 1 deletion classic.go → classic/response.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golang
package classic

import (
"fmt"
Expand Down Expand Up @@ -28,3 +28,13 @@ var DeferHandlerPeerResponse = func(errString string, params ...interface{}) boo
debug.PrintStack()
return true
}

// PeerResponse a readable structure of peer.response
type PeerResponse struct {
// A status code that should follow the HTTP status codes.
Status int32 `json:"status,omitempty"`
// A message associated with the response code.
Message string `json:"message,omitempty"`
// A payload that can be used to include metadata with this response.
Payload string `json:"payload,omitempty"`
}
4 changes: 2 additions & 2 deletions struct_test.go → classic/struct_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package golang
package classic

import (
"fmt"
"testing"
)

func TestArgsBuilder_Constructor(t *testing.T) {
func TestArgsBuilder(t *testing.T) {
var args = ArgsBuilder("abc")
fmt.Println("args", args)
args.AppendArg("cde")
Expand Down
3 changes: 2 additions & 1 deletion fabric.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package golang

import (
"github.com/davidkhala/fabric-common-chaincode-golang/classic"
. "github.com/davidkhala/goutils"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/hyperledger/fabric-chaincode-go/shim"
Expand All @@ -9,7 +10,7 @@ import (

func (cc CommonChaincode) InvokeChaincode(chaincodeName string, args [][]byte, channel string) peer.Response {
var resp = cc.CCAPI.InvokeChaincode(chaincodeName, args, channel)
PanicPeerResponse(resp)
classic.PanicPeerResponse(resp)
return resp
}

Expand Down
12 changes: 7 additions & 5 deletions fabric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package golang

import (
"github.com/davidkhala/fabric-common-chaincode-golang/cid"
"github.com/davidkhala/fabric-common-chaincode-golang/classic"
. "github.com/davidkhala/goutils"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-chaincode-go/shimtest"
Expand All @@ -28,7 +29,7 @@ func (t *TestChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response {
// Transaction makes payment of X units from A to B
func (t *TestChaincode) Invoke(stub shim.ChaincodeStubInterface) (response peer.Response) {
logger.Info("Invoke")
defer Deferred(DeferHandlerPeerResponse, &response)
defer Deferred(classic.DeferHandlerPeerResponse, &response)
var fcn, _ = stub.GetFunctionAndParameters()
var responseBytes []byte
switch fcn {
Expand All @@ -43,13 +44,13 @@ func (t *TestChaincode) Invoke(stub shim.ChaincodeStubInterface) (response peer.
var cc = new(TestChaincode)
var mock = shimtest.NewMockStub(name, cc)

//initialize mocker
// initialize mocker
func TestCommonChaincode_Prepare(t *testing.T) {
cc.Prepare(mock)
}

func TestTestChaincode_Init(t *testing.T) {
var args = ArgsBuilder("Initfcn")
var args = classic.ArgsBuilder("Initfcn")
var TxID = "ob"

var response = mock.MockInit(TxID, args.Get())
Expand All @@ -58,7 +59,7 @@ func TestTestChaincode_Init(t *testing.T) {
}
func TestTestChaincode_Invoke(t *testing.T) {

var args = ArgsBuilder("Invokefcn")
var args = classic.ArgsBuilder("Invokefcn")

var TxID = "oa"
var response = mock.MockInvoke(TxID, args.Get())
Expand All @@ -83,7 +84,8 @@ func TestCreateCompositeKey(t *testing.T) {
mock.MockTransactionEnd(TxID)
}

/**
/*
*
2018-07-09 12:46:27.277 HKT [mock] HasNext -> ERRO 001 HasNext() couldn't get Current
mockstub.go line 410: mockLogger.Error("HasNext() couldn't get Current")
*/
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/davidkhala/fabric-common-chaincode-golang
go 1.20

require (
github.com/davidkhala/fabric-common/golang v0.0.10
github.com/davidkhala/goutils v1.6.0
github.com/golang/protobuf v1.5.3
github.com/hyperledger/fabric-chaincode-go v0.0.0-20240124143825-7dec3c7e7d45
Expand All @@ -19,8 +20,8 @@ require (
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 7 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davidkhala/fabric-common/golang v0.0.10 h1:S6l2ZgIaTHWA25MKe8oNUAC5gUa0GPMdDBFPL02cS+o=
github.com/davidkhala/fabric-common/golang v0.0.10/go.mod h1:hgDOy75CL/vJBxgoql5gw9n+ZkqnjZPizX8GlDjddvI=
github.com/davidkhala/goutils v1.6.0 h1:uDjfyEiRUf/FQSfX3W5xUrQuNrX95iAb41r+/6FGqzI=
github.com/davidkhala/goutils v1.6.0/go.mod h1:vUEFyO8rG7HzRhaL413vZZfngC8146L4Qqf75Buvw2M=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hyperledger/fabric-chaincode-go v0.0.0-20240124143825-7dec3c7e7d45 h1:tZeJCTwbAE3cwi6XId+dYd/gTtfTKzZ3uEb1ksvQf7I=
github.com/hyperledger/fabric-chaincode-go v0.0.0-20240124143825-7dec3c7e7d45/go.mod h1:YZBt6/ZlJCzyPoWecbfFp34G+ZIYKodTQA46c0sxHIk=
Expand Down Expand Up @@ -56,10 +58,10 @@ golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 h1:AB/lmRny7e2pLhFEYIbl5qkDAUt2h0ZRO4wGPhZf+ik=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405/go.mod h1:67X1fPuzjcrkymZzZV1vvkFeTn2Rvc6lYF9MYFGCcwE=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 h1:/jFB8jK5R3Sq3i/lmeZO0cATSzFfZaJq1J2Euan3XKU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0/go.mod h1:FUoWkonphQm3RhTS+kOEhF8h0iDpm4tdXolVCeZ9KKA=
google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0=
google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
Expand Down
4 changes: 2 additions & 2 deletions shim.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package golang

import (
"github.com/davidkhala/fabric-common-chaincode-golang/cid"
"github.com/davidkhala/fabric-common/golang/format"
"github.com/davidkhala/goutils"
"github.com/hyperledger/fabric-chaincode-go/shim"
)

// GetMSPID From https://github.com/hyperledger/fabric-chaincode-go/commit/2d899240a7ed642a381ba9df2f6b0c303cb149dc
func GetMSPID() cid.MSPID {
func GetMSPID() format.MSPID {
var mspId, err = shim.GetMSPID()
goutils.PanicError(err)
return mspId
Expand Down
10 changes: 0 additions & 10 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,3 @@ func ParseStates(iterator shim.StateQueryIteratorInterface, filter func(StateKV)
}
return kvs
}

// PeerResponse a readable structure of peer.response
type PeerResponse struct {
// A status code that should follow the HTTP status codes.
Status int32 `json:"status,omitempty"`
// A message associated with the response code.
Message string `json:"message,omitempty"`
// A payload that can be used to include metadata with this response.
Payload string `json:"payload,omitempty"`
}

0 comments on commit e5bc29f

Please sign in to comment.