-
Notifications
You must be signed in to change notification settings - Fork 94
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
Conversation
There was a problem hiding this 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.
samples/go/sample.go
Outdated
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) | ||
} |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, typo
samples/node/src/sample.ts
Outdated
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import {connect, ConnectOptions, Identity, Signer, signers} from "fabric-gateway"; |
There was a problem hiding this comment.
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
samples/node/src/sample.ts
Outdated
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'); |
There was a problem hiding this comment.
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
samples/node/src/sample.ts
Outdated
let gateway; | ||
try { |
There was a problem hiding this comment.
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()
}
samples/node/src/sample.ts
Outdated
try { | ||
const privateKeyPem = await fs.promises.readFile(keyPath); | ||
const privateKey = crypto.createPrivateKey(privateKeyPem); | ||
const signer: Signer = signers.newPrivateKeySigner(privateKey); |
There was a problem hiding this comment.
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()
samples/node/src/sample.ts
Outdated
}; | ||
|
||
const tlsRootCert = fs.readFileSync(tlsCertPath); | ||
const grpcOptions = { |
There was a problem hiding this comment.
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
samples/node/src/sample.ts
Outdated
} catch (e) { | ||
console.log(e); | ||
} |
There was a problem hiding this comment.
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>
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