scripts/caliper-workspace/node_modules/fabric-network/lib/gateway.js
/**
     * Connect to the Gateway with a connection profile or a prebuilt Client instance.
     * @async
     * @param {(string|object|Client)} config The configuration for this Gateway which can be:
     * <ul>
     *   <li>A fully qualified common connection profile file path (String)</li>
     *   <li>A common connection profile JSON (Object)</li>
     *   <li>A pre-configured client instance</li>
     * </ul>
     * @param {module:fabric-network.Gateway~GatewayOptions} options - specific options
      * for creating this Gateway instance
     * @example
     * const gateway = new Gateway();
     * const wallet = new FileSystemWallet('./WALLETS/wallet');
     * const ccpFile = fs.readFileSync('./network.json');
     * const ccp = JSON.parse(ccpFile.toString());
     * await gateway.connect(ccp, {
     *   identity: 'admin',
     *   wallet: wallet
     * });
     */
Connect to the Gateway with a connection profile or a prebuilt Client instance.
Connect to the Gateway with a connection profile or a prebuilt Client instance .
async connect(config, options) {
        const method = 'connect';
        logger.debug('%s - start', method);
        if (!options || !options.wallet) {
            logger.error('%s - A wallet must be assigned to a Gateway instance', method);
            throw new Error('A wallet must be assigned to a Gateway instance');
        }
        Gateway._mergeOptions(this.options, options);
        logger.debug('connection options: %j', options);
        let load_ccp = false;
        if (config && config.type === 'Client') {
            logger.debug('%s - using existing client object', method);
            this.client = config;
        }
        else {
            this.client = new Client('gateway client');
            load_ccp = true;
        }
        if (options.identity) {
            logger.debug('%s - setting identity', method);
            this.identity = await this._getWalletIdentity(options.identity);
            const provider = options.wallet.getProviderRegistry().getProvider(this.identity.type);
            const user = await provider.getUserContext(this.identity, options.identity);
            this.identityContext = this.client.newIdentityContext(user);
        }
        if (options.clientTlsIdentity) {
            logger.debug('%s - setting tlsIdentity', method);
            const tlsIdentity = await this._getWalletIdentity(options.clientTlsIdentity);
            this.client.setTlsClientCertAndKey(tlsIdentity.credentials.certificate, tlsIdentity.credentials.privateKey);
        }
        else if (options.tlsInfo) {
            logger.debug('%s - setting tlsInfo', method);
            this.client.setTlsClientCertAndKey(options.tlsInfo.certificate, options.tlsInfo.key);
        }
        else {
            logger.debug('%s - using self signed setting for tls', method);
            this.client.setTlsClientCertAndKey();
        }
        if (load_ccp) {
            logger.debug('%s - NetworkConfig loading client from ccp', method);
            await NetworkConfig.loadFromConfig(this.client, config);
        }
        else {
            logger.debug('%s - no connection profile - using client instance', method);
        }
        if (options['connection-options']) {
            this.client.centralized_options = options['connection-options'];
            logger.debug('%s - assigned connection options');
        }
        logger.debug('%s - end', method);
    }
async connect ( config , options ) { const method = 'connect' ; logger . debug ( '%s - start' , method ) ; if ( ! options || ! options . wallet ) { logger . error ( '%s - A wallet must be assigned to a Gateway instance' , method ) ; throw new Error ( 'A wallet must be assigned to a Gateway instance' ) ; } Gateway . _mergeOptions ( this . options , options ) ; logger . debug ( 'connection options: %j' , options ) ; let load_ccp = false ; if ( config && config . type === 'Client' ) { logger . debug ( '%s - using existing client object' , method ) ; this . client = config ; } else { this . client = new Client ( 'gateway client' ) ; load_ccp = true ; } if ( options . identity ) { logger . debug ( '%s - setting identity' , method ) ; this . identity = await this . _getWalletIdentity ( options . identity ) ; const provider = options . wallet . getProviderRegistry ( ) . getProvider ( this . identity . type ) ; const user = await provider . getUserContext ( this . identity , options . identity ) ; this . identityContext = this . client . newIdentityContext ( user ) ; } if ( options . clientTlsIdentity ) { logger . debug ( '%s - setting tlsIdentity' , method ) ; const tlsIdentity = await this . _getWalletIdentity ( options . clientTlsIdentity ) ; this . client . setTlsClientCertAndKey ( tlsIdentity . credentials . certificate , tlsIdentity . credentials . privateKey ) ; } else if ( options . tlsInfo ) { logger . debug ( '%s - setting tlsInfo' , method ) ; this . client . setTlsClientCertAndKey ( options . tlsInfo . certificate , options . tlsInfo . key ) ; } else { logger . debug ( '%s - using self signed setting for tls' , method ) ; this . client . setTlsClientCertAndKey ( ) ; } if ( load_ccp ) { logger . debug ( '%s - NetworkConfig loading client from ccp' , method ) ; await NetworkConfig . loadFromConfig ( this . client , config ) ; } else { logger . debug ( '%s - no connection profile - using client instance' , method ) ; } if ( options [ 'connection-options' ] ) { this . client . centralized_options = options [ 'connection-options' ] ; logger . debug ( '%s - assigned connection options' ) ; } logger . debug ( '%s - end' , method ) ; }
Connect to the Gateway with a connection profile or a prebuilt Client instance.
Connect to the Gateway with a connection profile or a prebuilt Client instance .
// initialize from an existing Client object instance // build a new client and once it has been configured with // the passed in options it will be loaded with the ccp // setup an initial identity for the Gateway // apply any connection options to the client instance for use // internally by the client instance when building a complete set // of connection options for an endpoint // these will be merged with those from the config (default.json)