Documentation
¶
Overview ¶
Package chaincode provides functions for driving chaincode lifecycle.
Example ¶
package main
import (
"context"
"crypto"
"crypto/x509"
"encoding/pem"
"os"
"time"
"github.com/hyperledger/fabric-admin-sdk/pkg/chaincode"
"github.com/hyperledger/fabric-admin-sdk/pkg/identity"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
const peerName = "peer.example.org"
const peerEndpoint = peerName + ":7051"
const mspID = "Org1"
const chaincodePackageFile = "basic.tar.gz"
func main() {
// gRPC connection a target peer.
connection := newGrpcConnection()
defer connection.Close()
// Client identity used to carry out deployment tasks.
id, err := identity.NewPrivateKeySigningIdentity(mspID, readCertificate(), readPrivateKey())
panicOnError(err)
// Context used to manage Fabric invocations.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
// Read existing chaincode package file.
chaincodePackage, err := os.Open(chaincodePackageFile)
panicOnError(err)
// Install chaincode package. This must be performed for each peer on which the chaincode is to be installed.
_, err = chaincode.Install(ctx, connection, id, chaincodePackage)
panicOnError(err)
// Definition of the chaincode as it should appear on the channel.
chaincodeDefinition := &chaincode.Definition{
ChannelName: "mychannel",
Name: "basic",
Version: "1.0",
Sequence: 1,
}
// Approve chaincode definition. This must be performed using client identities from sufficient organizations to
// satisfy the approval policy.
err = chaincode.Approve(ctx, connection, id, chaincodeDefinition)
panicOnError(err)
// Commit approved chaincode definition. This can be carried out by any organization once enough approvals have
// been recorded.
err = chaincode.Commit(ctx, connection, id, chaincodeDefinition)
panicOnError(err)
}
func newGrpcConnection() *grpc.ClientConn {
caCertificate := readCertificate()
certPool := x509.NewCertPool()
certPool.AddCert(caCertificate)
transportCredentials := credentials.NewClientTLSFromCert(certPool, "")
connection, err := grpc.Dial(peerEndpoint, grpc.WithTransportCredentials(transportCredentials))
panicOnError(err)
return connection
}
func readCertificate() *x509.Certificate {
certificatePEM, err := os.ReadFile("certificate.pem")
panicOnError(err)
block, _ := pem.Decode([]byte(certificatePEM))
if block == nil {
panic("failed to parse certificate PEM")
}
certificate, err := x509.ParseCertificate(block.Bytes)
if err != nil {
panic("failed to parse certificate: " + err.Error())
}
return certificate
}
func readPrivateKey() crypto.PrivateKey {
privateKeyPEM, err := os.ReadFile("privateKey.pem")
panicOnError(err)
block, _ := pem.Decode(privateKeyPEM)
if block == nil {
panic("failed to parse private key PEM")
}
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
panic("failed to parse PKCS8 encoded private key: " + err.Error())
}
return privateKey
}
func panicOnError(err error) {
if err != nil {
panic(err)
}
}
Output:
Index ¶
- func Approve(ctx context.Context, connection grpc.ClientConnInterface, ...) error
- func CheckCommitReadiness(ctx context.Context, connection grpc.ClientConnInterface, ...) (*lifecycle.CheckCommitReadinessResult, error)
- func Commit(ctx context.Context, connection grpc.ClientConnInterface, ...) error
- func GetInstalled(ctx context.Context, connection grpc.ClientConnInterface, ...) ([]byte, error)
- func GetPackageID(label string, ccInstallPkg []byte) string
- func Install(ctx context.Context, connection grpc.ClientConnInterface, ...) (*lifecycle.InstallChaincodeResult, error)
- func NewApplicationPolicy(signaturePolicy, channelConfigPolicy string) (*peer.ApplicationPolicy, error)
- func PackageCCAAS(connection Connection, metadata Metadata, tmpPath, filename string) error
- func PackageID(packageReader io.Reader) (string, error)
- func QueryApproved(ctx context.Context, connection grpc.ClientConnInterface, ...) (*lifecycle.QueryApprovedChaincodeDefinitionResult, error)
- func QueryCommitted(ctx context.Context, connection grpc.ClientConnInterface, ...) (*lifecycle.QueryChaincodeDefinitionsResult, error)
- func QueryCommittedWithName(ctx context.Context, connection grpc.ClientConnInterface, ...) (*lifecycle.QueryChaincodeDefinitionResult, error)
- func QueryInstalled(ctx context.Context, connection grpc.ClientConnInterface, ...) (*lifecycle.QueryInstalledChaincodesResult, error)
- func SignaturePolicyEnvelopeToString(policy *cb.SignaturePolicyEnvelope) (string, error)
- func ValidateLabel(label string) error
- type ChaincodePackageMetadata
- type Connection
- type Definition
- type Metadata
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Approve ¶
func Approve(ctx context.Context, connection grpc.ClientConnInterface, id identity.SigningIdentity, chaincodeDef *Definition) error
Approve a chaincode package for the user's own organization. The connection may be to any Gateway peer that is a member of the channel.
func CheckCommitReadiness ¶
func CheckCommitReadiness(ctx context.Context, connection grpc.ClientConnInterface, id identity.SigningIdentity, chaincodeDef *Definition) (*lifecycle.CheckCommitReadinessResult, error)
CheckCommitReadiness for a chaincode and return all approval records. The connection may be to any Gateway peer that is a member of the channel.
func Commit ¶
func Commit(ctx context.Context, connection grpc.ClientConnInterface, id identity.SigningIdentity, chaincodeDef *Definition) error
Commit a chaincode definition to the channel. This requires that sufficient organizations have approved the chaincode definition. The connection may be to any Gateway peer that is a member of the channel.
func GetInstalled ¶
func GetInstalled(ctx context.Context, connection grpc.ClientConnInterface, id identity.SigningIdentity, packageID string) ([]byte, error)
GetInstalled chaincode package from a specific peer. The connection must be to the specific peer where the chaincode is installed.
func GetPackageID ¶
GetPackageID returns the package ID with the label and hash of the chaincode install package
func Install ¶
func Install(ctx context.Context, connection grpc.ClientConnInterface, id identity.SigningIdentity, packageReader io.Reader) (*lifecycle.InstallChaincodeResult, error)
Install a chaincode package to specific peer. The connection must be to the specific peer where the chaincode is to be installed.
func NewApplicationPolicy ¶
func NewApplicationPolicy(signaturePolicy, channelConfigPolicy string) (*peer.ApplicationPolicy, error)
func PackageCCAAS ¶
func PackageCCAAS(connection Connection, metadata Metadata, tmpPath, filename string) error
PackageCCAAS requires that you start a container by yourself. as sample as ${CONTAINER_CLI} run --rm -d --name peer0org1_${CC_NAME}_ccaas \ --network fabric_test \ -e CHAINCODE_SERVER_ADDRESS=0.0.0.0:${CCAAS_SERVER_PORT} \ -e CHAINCODE_ID=$PACKAGE_ID -e CORE_CHAINCODE_ID_NAME=$PACKAGE_ID \ ${CC_NAME}_ccaas_image:latest
func QueryApproved ¶
func QueryApproved(ctx context.Context, connection grpc.ClientConnInterface, id identity.SigningIdentity, channelID string, chaincodeName string, sequence int64) (*lifecycle.QueryApprovedChaincodeDefinitionResult, error)
QueryApproved chaincode definition for the user's own organization. The connection may be to any Gateway peer that is a member of the channel.
func QueryCommitted ¶
func QueryCommitted(ctx context.Context, connection grpc.ClientConnInterface, id identity.SigningIdentity, channelID string) (*lifecycle.QueryChaincodeDefinitionsResult, error)
QueryCommitted returns the definitions of all committed chaincode for a given channel. The connection may be to any Gateway peer that is a member of the channel.
func QueryCommittedWithName ¶
func QueryCommittedWithName(ctx context.Context, connection grpc.ClientConnInterface, id identity.SigningIdentity, channelID, chaincodeName string) (*lifecycle.QueryChaincodeDefinitionResult, error)
QueryCommittedWithName returns the definition of the named chaincode for a given channel. The connection may be to any Gateway peer that is a member of the channel.
func QueryInstalled ¶
func QueryInstalled(ctx context.Context, connection grpc.ClientConnInterface, id identity.SigningIdentity) (*lifecycle.QueryInstalledChaincodesResult, error)
QueryInstalled chaincode on a specific peer. The connection must be to the specific peer where the chaincode is installed.
func SignaturePolicyEnvelopeToString ¶
func SignaturePolicyEnvelopeToString(policy *cb.SignaturePolicyEnvelope) (string, error)
SignaturePolicyEnvelopeToString parse a SignaturePolicyEnvelope to human readable expression the returned expression is GATE(P[, P])
where:
- GATE is either "and" or "or" or "outof"
- P is either a principal or another nested call to GATE
A principal is defined as:
ORG.ROLE ¶
where:
- ORG is a string (representing the MSP identifier)
- ROLE takes the value of any of the RoleXXX constants representing the required role
func ValidateLabel ¶
ValidateLabel return an error if the provided label contains any invalid characters, as determined by LabelRegexp.
Types ¶
type ChaincodePackageMetadata ¶
type ChaincodePackageMetadata struct {
Type string `json:"type"`
Path string `json:"path"`
Label string `json:"label"`
}
ChaincodePackageMetadata contains the information necessary to understand the embedded code package.
func ParseChaincodePackage ¶
func ParseChaincodePackage(source []byte) (*ChaincodePackageMetadata, []byte, error)
ParseChaincodePackage parses a set of bytes as a chaincode package and returns the parsed package as a metadata struct and a code package
type Connection ¶
type Connection struct {
Address string `json:"address"`
DialTimeout string `json:"dial_timeout"`
TLSRequired bool `json:"tls_required"`
}
Connection setting used in connection.json
type Definition ¶
type Definition struct {
// ChannelName on which the chaincode is deployed.
ChannelName string
// PackageID is a unique identifier for a chaincode package, combining the package label with a hash of the package.
PackageID string
// Name used when invoking the chaincode.
Name string
// Version associated with a given chaincode package.
Version string
// EndorsementPlugin used by the chaincode. May be omitted unless a custom plugin is required.
EndorsementPlugin string
// ValidationPlugin used by the chaincode. May be omitted unless a custom plugin is required.
ValidationPlugin string
// Sequence number indicating the number of times the chaincode has been defined on a channel, and used to keep
// track of chaincode upgrades.
Sequence int64
// ApplicationPolicy defines the endorsement policy for the chaincode. This can be an explicit endorsement policy
// it follows fabric-protos-go-apiv2/peer format and it will convert to validationParameter during validation phase.
ApplicationPolicy *peer.ApplicationPolicy
// InitRequired is true only if the chaincode defines an Init function using the low-level shim API, which must be
// invoked before other transaction functions may be invoked; otherwise false. It is not recommended to rely on
// functionality.
InitRequired bool
// Collections configuration for private data collections accessed by the chaincode.
Collections *peer.CollectionConfigPackage
}
Definition of a chaincode.