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 peerEndpoint = "peer.example.org: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) peer := chaincode.NewPeer(connection, id) gateway := chaincode.NewGateway(connection, id) // 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 = peer.Install(ctx, 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 = gateway.Approve(ctx, chaincodeDefinition) panicOnError(err) // Commit approved chaincode definition. This can be carried out by any organization once enough approvals have // been recorded. err = gateway.Commit(ctx, chaincodeDefinition) panicOnError(err) } func newGrpcConnection() *grpc.ClientConn { caCertificate := readCertificate() certPool := x509.NewCertPool() certPool.AddCert(caCertificate) transportCredentials := credentials.NewClientTLSFromCert(certPool, "") connection, err := grpc.NewClient(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 GetPackageID(label string, ccInstallPkg []byte) string
- 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 SignaturePolicyEnvelopeToString(policy *cb.SignaturePolicyEnvelope) (string, error)
- func ValidateLabel(label string) error
- type ChaincodePackageMetadata
- type Connection
- type Definition
- type Gateway
- func (g *Gateway) Approve(ctx context.Context, chaincodeDef *Definition) error
- func (g *Gateway) CheckCommitReadiness(ctx context.Context, chaincodeDef *Definition) (*lifecycle.CheckCommitReadinessResult, error)
- func (g *Gateway) ClientIdentity() identity.SigningIdentity
- func (g *Gateway) Commit(ctx context.Context, chaincodeDef *Definition) error
- func (g *Gateway) QueryApproved(ctx context.Context, channelName string, chaincodeName string, sequence int64) (*lifecycle.QueryApprovedChaincodeDefinitionResult, error)
- func (g *Gateway) QueryCommitted(ctx context.Context, channelName string) (*lifecycle.QueryChaincodeDefinitionsResult, error)
- func (g *Gateway) QueryCommittedWithName(ctx context.Context, channelName string, chaincodeName string) (*lifecycle.QueryChaincodeDefinitionResult, error)
- type Metadata
- type Peer
- func (p *Peer) ClientIdentity() identity.SigningIdentity
- func (p *Peer) GetInstalled(ctx context.Context, packageID string) ([]byte, error)
- func (p *Peer) Install(ctx context.Context, packageReader io.Reader) (*lifecycle.InstallChaincodeResult, error)
- func (p *Peer) QueryInstalled(ctx context.Context) (*lifecycle.QueryInstalledChaincodesResult, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetPackageID ¶
GetPackageID returns the package ID with the label and hash of the chaincode install package
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 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.
type Gateway ¶ added in v0.1.0
type Gateway struct {
// contains filtered or unexported fields
}
Gateway peer belonging to a specific organization to which you want to target requests.
func NewGateway ¶ added in v0.1.0
func NewGateway(connection grpc.ClientConnInterface, id identity.SigningIdentity) *Gateway
NewGateway creates a new Gateway instance.
func (*Gateway) Approve ¶ added in v0.1.0
func (g *Gateway) Approve(ctx context.Context, chaincodeDef *Definition) error
Approve a chaincode package for the user's own organization.
func (*Gateway) CheckCommitReadiness ¶ added in v0.1.0
func (g *Gateway) CheckCommitReadiness(ctx context.Context, chaincodeDef *Definition) (*lifecycle.CheckCommitReadinessResult, error)
CheckCommitReadiness for a chaincode and return all approval records.
func (*Gateway) ClientIdentity ¶ added in v0.1.0
func (g *Gateway) ClientIdentity() identity.SigningIdentity
ClientIdentity used to interact with the gateway peer.
func (*Gateway) Commit ¶ added in v0.1.0
func (g *Gateway) Commit(ctx context.Context, chaincodeDef *Definition) error
Commit a chaincode definition to the channel.
func (*Gateway) QueryApproved ¶ added in v0.1.0
func (g *Gateway) QueryApproved(ctx context.Context, channelName string, chaincodeName string, sequence int64) (*lifecycle.QueryApprovedChaincodeDefinitionResult, error)
QueryApproved chaincode definition for the user's own organization.
func (*Gateway) QueryCommitted ¶ added in v0.1.0
func (g *Gateway) QueryCommitted(ctx context.Context, channelName string) (*lifecycle.QueryChaincodeDefinitionsResult, error)
QueryCommitted returns the definitions of all committed chaincode for a given channel.
func (*Gateway) QueryCommittedWithName ¶ added in v0.1.0
func (g *Gateway) QueryCommittedWithName(ctx context.Context, channelName string, chaincodeName string) (*lifecycle.QueryChaincodeDefinitionResult, error)
QueryCommittedWithName returns the definition of the named chaincode for a given channel.
type Peer ¶ added in v0.1.0
type Peer struct {
// contains filtered or unexported fields
}
Peer in a Fabric network.
func NewPeer ¶ added in v0.1.0
func NewPeer(connection grpc.ClientConnInterface, id identity.SigningIdentity) *Peer
NewPeer creates a new Peer instance.
func (*Peer) ClientIdentity ¶ added in v0.1.0
func (p *Peer) ClientIdentity() identity.SigningIdentity
ClientIdentity used to interact with the peer.
func (*Peer) GetInstalled ¶ added in v0.1.0
GetInstalled chaincode package from a specific peer.
func (*Peer) Install ¶ added in v0.1.0
func (p *Peer) Install(ctx context.Context, packageReader io.Reader) (*lifecycle.InstallChaincodeResult, error)
Install a chaincode package to specific peer.
func (*Peer) QueryInstalled ¶ added in v0.1.0
func (p *Peer) QueryInstalled(ctx context.Context) (*lifecycle.QueryInstalledChaincodesResult, error)
QueryInstalled chaincode on a specific peer.