Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample code for Node and Go #71

Merged
merged 1 commit into from
Mar 29, 2021
Merged

Conversation

andrew-coleman
Copy link
Member

Very basic samples demonstrating how to write a client application using the gateway SDKs
Go and Node for now.

Signed-off-by: andrew-coleman andrew_coleman@uk.ibm.com

Copy link
Member

@bestbeforetoday bestbeforetoday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice simple examples 👍

In fine Fabric tradition, I've made a load of comments that I think are worth addressing as they are going to be end user samples so we want the code to look nice. Nothing is a show-stopper though.

Comment on lines 102 to 109
func loadX509Cert(certFile string) (*x509.Certificate, error) {
cf, e := ioutil.ReadFile(certFile)
if e != nil {
return nil, e
}
cpb, _ := pem.Decode(cf)
return x509.ParseCertificate(cpb.Bytes)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functionally duplicates the first 5 lines of the newIdentity() function so might be good to factor that into common code.

Note that cpb can be nil for bad input, which is checked for you using identity.CertificateFromPEM()

Makefile Outdated
@@ -65,6 +65,9 @@ lint:
staticcheck:
staticcheck $(base_dir)/pkg/... $(scenario_dir)/go

sample-network:: vendor-chaincode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the double : is supposed to be there or makes a difference?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, typo

SPDX-License-Identifier: Apache-2.0
*/

import {connect, ConnectOptions, Identity, Signer, signers} from "fabric-gateway";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth consistently using either " or ' around strings

import * as grpc from '@grpc/grpc-js';

const mspId = "Org1MSP"
const cryptoPath = path.resolve(__dirname, '../', '..', '..', 'scenario', 'fixtures', 'crypto-material', 'crypto-config', 'peerOrganizations', 'org1.example.com');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the / should be there in the path components. path.resolve() should be putting the correct separators in there

Comment on lines 21 to 45
let gateway;
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You definitely don't have a gateway until the connect completes successfully so could start the try block just after the connect and have:

const gateway = connect(options);
try {
    ...
} finally {
    gateway.close()
}

try {
const privateKeyPem = await fs.promises.readFile(keyPath);
const privateKey = crypto.createPrivateKey(privateKeyPem);
const signer: Signer = signers.newPrivateKeySigner(privateKey);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally wouldn't specify the type of signer as it's inferred from the return type of newPrivateKeySigner()

};

const tlsRootCert = fs.readFileSync(tlsCertPath);
const grpcOptions = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally would explicitly specify the type as Partial<ChannelOptions> so the compiler can ensure you're using valid property names/types. Or embed it straight into the new GrpcClient() call so the compiler can infer the type. I think then you'll see any error at the point of definition rather than the point of use

Comment on lines 53 to 55
} catch (e) {
console.log(e);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd ditch this catch block and invoke the main() function with main().catch(console.error); so anything thrown gets logged to stderr

Very basic samples demonstrating how to write a client application using the gateway SDKs
Go and Node for now.

Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
@andrew-coleman andrew-coleman merged commit 243bb23 into hyperledger:main Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants