All files / fabric-shim/lib/cmds serverCommand.js

100% Statements 41/41
100% Branches 28/28
100% Functions 4/4
100% Lines 41/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107                1x   1x 1x   1x                                     1x   1x 6x   6x   6x 5x     4x 1x   3x 1x     3x     6x     1x 1x   1x     1x 4x   4x 64x       4x     3x   3x 1x   2x     3x 1x   2x       3x 1x 2x 1x     3x       4x 4x   4x 4x   4x    
/*
# Copyright Hitachi America, Ltd. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
*/
 
'use strict';
 
const fs = require('fs');
 
exports.command = 'server [options]';
exports.desc = 'Start the chaincode as a server';
 
const validOptions = {
    'chaincode-address': {type: 'string', required: true},
    'grpc.max_send_message_length': {type: 'number', default: -1},
    'grpc.max_receive_message_length': {type: 'number', default: -1},
    'grpc.keepalive_time_ms': {type: 'number', default: 110000},
    'grpc.http2.min_time_between_pings_ms': {type: 'number', default: 110000},
    'grpc.keepalive_timeout_ms': {type: 'number', default: 20000},
    'grpc.http2.max_pings_without_data': {type: 'number', default: 0},
    'grpc.keepalive_permit_without_calls': {type: 'number', default: 1},
    'chaincode-id': {type: 'string', required: true},
    'chaincode-tls-cert-file': {type: 'string', conflicts: 'chaincode-tls-cert-path'},
    'chaincode-tls-cert-path': {type: 'string', conflicts: 'chaincode-tls-cert-file'},
    'chaincode-tls-key-file': {type: 'string', conflicts: 'chaincode-tls-key-path'},
    'chaincode-tls-key-path': {type: 'string', conflicts: 'chaincode-tls-key-file'},
    'chaincode-tls-client-cacert-file': {type: 'string', conflicts: 'chaincode-tls-client-cacert-path'},
    'chaincode-tls-client-cacert-path': {type: 'string', conflicts: 'chaincode-tls-client-cacert-file'},
    'module-path': {type: 'string', default: process.cwd()}
};
 
exports.validOptions = validOptions;
 
exports.builder = function (yargs) {
    yargs.options(validOptions);
 
    yargs.usage('fabric-chaincode-node server --chaincode-address 0.0.0.0:9999 --chaincode-id mycc_v0:abcdef12345678...');
 
    yargs.check((argv) => {
        if (argv['chaincode-tls-key-file'] || argv['chaincode-tls-key-path'] ||
            argv['chaincode-tls-cert-file'] || argv['chaincode-tls-cert-path']) {
            // TLS should be enabled
            if (!argv['chaincode-tls-key-file'] && !argv['chaincode-tls-key-path']) {
                throw new Error('A TLS option is set but no key is specified');
            }
            if (!argv['chaincode-tls-cert-file'] && !argv['chaincode-tls-cert-path']) {
                throw new Error('A TLS option is set but no cert is specified');
            }
        }
        return true;
    });
 
    return yargs;
};
 
exports.handler = async function (argv) {
    const Bootstrap = require('../contract-spi/bootstrap');
 
    await Bootstrap.bootstrap(true);
};
 
exports.getArgs = function (yargs) {
    const argv = {};
 
    for (const name in validOptions) {
        argv[name] = yargs.argv[name];
    }
 
    // Load the cryptographic files if TLS is enabled
    if (argv['chaincode-tls-key-file'] || argv['chaincode-tls-key-path'] ||
        argv['chaincode-tls-cert-file'] || argv['chaincode-tls-cert-path']) {
 
        const tlsProps = {};
 
        if (argv['chaincode-tls-key-file']) {
            tlsProps.key = fs.readFileSync(argv['chaincode-tls-key-file']);
        } else {
            tlsProps.key = Buffer.from(fs.readFileSync(argv['chaincode-tls-key-path']).toString(), 'base64');
        }
 
        if (argv['chaincode-tls-cert-file']) {
            tlsProps.cert = fs.readFileSync(argv['chaincode-tls-cert-file']);
        } else {
            tlsProps.cert = Buffer.from(fs.readFileSync(argv['chaincode-tls-cert-path']).toString(), 'base64');
        }
 
        // If cacert option is specified, enable client certificate validation
        if (argv['chaincode-tls-client-cacert-file']) {
            tlsProps.clientCACerts = fs.readFileSync(argv['chaincode-tls-client-cacert-file']);
        } else if (argv['chaincode-tls-client-cacert-path']) {
            tlsProps.clientCACerts =  Buffer.from(fs.readFileSync(argv['chaincode-tls-client-cacert-path']).toString(), 'base64');
        }
 
        argv.tlsProps = tlsProps;
    }
 
    // Translate the options to server options
    argv.ccid = argv['chaincode-id'];
    argv.address = argv['chaincode-address'];
 
    delete argv['chaincode-id'];
    delete argv['chaincode-address'];
 
    return argv;
};