Skip to content

Commit

Permalink
Update chaincode package example code (#205)
Browse files Browse the repository at this point in the history
Make use of the current identity package implementation to read
certificate and private key instead of the lower level operations
provided by the Go standard packages.

Signed-off-by: Mark S. Lewis <Mark.S.Lewis@outlook.com>
  • Loading branch information
bestbeforetoday authored Sep 20, 2024
1 parent b0d7acf commit 7048277
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 48 deletions.
7 changes: 6 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
},
"customizations": {
"vscode": {
"extensions": ["esbenp.prettier-vscode", "github.vscode-github-actions", "ms-vscode.makefile-tools"]
"extensions": [
"esbenp.prettier-vscode",
"github.vscode-github-actions",
"ms-vscode.makefile-tools",
"streetsidesoftware.code-spell-checker"
]
}
},
"postStartCommand": ["make", "generate"]
Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ For information on using the fabric-admin-sdk, please visit the [Go API document

## Overview

The current API for client application to transact with Fabric networks (the [Fabric Gateway client API](https://hyperledger.github.io/fabric-gateway/)) does not provide the administrative capabilities of the legcacy client application SDKs. The fabric-admin-sdk aims to provide this programmatic administrative capability for use-cases where the [Fabric CLI commands](https://hyperledger-fabric.readthedocs.io/en/latest/command_ref.html) are not the best fit. A specific objective is to support the development of Kubernetes operators.
The current API for client applications to transact with Fabric networks (the [Fabric Gateway client API](https://hyperledger.github.io/fabric-gateway/)) does not provide the administrative capabilities of the legacy client application SDKs. The fabric-admin-sdk aims to provide this programmatic administrative capability for use-cases where the [Fabric CLI commands](https://hyperledger-fabric.readthedocs.io/en/latest/command_ref.html) are not the best fit. A specific objective is to support the development of Kubernetes operators.

More detailed information on the motivation and objectives of the fabric-admin-sdk can be found in the associated [Fabric Admin SDK RFC](https://hyperledger.github.io/fabric-rfcs/text/0000-admin_sdk.html).

Expand Down Expand Up @@ -51,7 +51,7 @@ The initial submission and implementation of fabric-admin-sdk was driven by memb

## Contribute

Here is steps in short for any contribution.
Here are steps in short for any contribution.

1. check license and code of conduct
1. fork this project
Expand All @@ -61,6 +61,4 @@ Here is steps in short for any contribution.

## Code of Conduct guidelines

Please review the Hyperledger [Code of
Conduct](https://wiki.hyperledger.org/community/hyperledger-project-code-of-conduct)
before participating. It is important that we keep things civil.
Please review the Hyperledger [Code of Conduct](https://wiki.hyperledger.org/community/hyperledger-project-code-of-conduct) before participating. It is important that we keep things civil.
57 changes: 15 additions & 42 deletions pkg/chaincode/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ package chaincode_test

import (
"context"
"crypto"
"crypto/x509"
"encoding/pem"
"os"
"time"

Expand All @@ -29,8 +27,13 @@ func Example() {
connection := newGrpcConnection()
defer connection.Close()

certificate, err := identity.ReadCertificate("client-certificate.pem")
panicOnError(err)
privateKey, err := identity.ReadPrivateKey("client-private-key.pem")
panicOnError(err)

// Client identity used to carry out deployment tasks.
id, err := identity.NewPrivateKeySigningIdentity(mspID, readCertificate(), readPrivateKey())
id, err := identity.NewPrivateKeySigningIdentity(mspID, certificate, privateKey)
panicOnError(err)

peer := chaincode.NewPeer(connection, id)
Expand All @@ -44,7 +47,8 @@ func Example() {
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.
// 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)

Expand All @@ -56,19 +60,21 @@ func Example() {
Sequence: 1,
}

// Approve chaincode definition. This must be performed using client identities from sufficient organizations to
// satisfy the approval policy.
// 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.
// 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()
caCertificate, err := identity.ReadCertificate("ca-certificate.pem")
panicOnError(err)

certPool := x509.NewCertPool()
certPool.AddCert(caCertificate)
transportCredentials := credentials.NewClientTLSFromCert(certPool, "")
Expand All @@ -79,39 +85,6 @@ func newGrpcConnection() *grpc.ClientConn {
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)
Expand Down

0 comments on commit 7048277

Please sign in to comment.