application-typescript code refactor

Signed-off-by: sapthasurendran <saptha.surendran@ibm.com>
This commit is contained in:
sapthasurendran 2022-07-11 16:14:59 +05:30
parent 318b4cc250
commit efeb603139

View file

@ -27,34 +27,37 @@ const peerEndpoint = 'localhost:7051';
async function main() { async function main() {
console.log('\nRunning the Node HSM sample'); console.log('\nRunning the Node HSM sample');
let client;
let gateway;
let hsmSignerFactory;
let hsmSigner;
try {
// The gRPC client connection should be shared by all Gateway connections to this endpoint // The gRPC client connection should be shared by all Gateway connections to this endpoint
const client = await newGrpcConnection(); client = await newGrpcConnection();
// get an HSMSigner Factory. You only need to do this once for the application // get an HSMSigner Factory. You only need to do this once for the application
const hsmSignerFactory = signers.newHSMSignerFactory(findSoftHSMPKCS11Lib()); hsmSignerFactory = signers.newHSMSignerFactory(findSoftHSMPKCS11Lib());
const credentials = await fs.promises.readFile(certPath); const credentials = await fs.promises.readFile(certPath);
// Get the signer function and a close function. The close function closes the signer // Get the signer function and a close function. The close function closes the signer
// once there is no further need for it. // once there is no further need for it.
const {signer, close} = await newHSMSigner(hsmSignerFactory, credentials.toString()); hsmSigner = await newHSMSigner(hsmSignerFactory, credentials.toString());
gateway = connect({
const gateway = connect({
client, client,
identity: { mspId, credentials }, identity: { mspId, credentials },
signer, signer:hsmSigner.signer,
}); });
try {
await exampleTransaction(gateway); await exampleTransaction(gateway);
console.log(); console.log();
console.log('Node HSM sample completed successfully'); console.log('Node HSM sample completed successfully');
} finally { } finally {
gateway.close(); gateway?.close();
client.close(); client?.close();
hsmSignerFactory?.dispose();
// close the HSM Signer // close the HSM Signer
close(); hsmSigner?.close();
} }
} }