All files / src/components/fabric/services cc.service.ts

36.11% Statements 13/36
0% Branches 0/13
33.33% Functions 1/3
36.11% Lines 13/36

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 108 109 110 1111x 1x 1x 1x 1x   1x 1x   1x   1x       1x   1x     3499x               3499x                                                                                                                                                                    
import {inject} from '@loopback/core';
import * as dotenv from 'dotenv';
import {Gateway} from 'fabric-network';
import {CAService} from '.';
import {LogChainConfigs} from '../../../configs';
import {AnyObject} from '../../../types';
import {AppUtil} from '../app-util';
import {FabricServiceBindings} from '../keys';
import {RequestParams} from '../types';
import { StringUtils } from '@logchain/utils';
 
dotenv.config();
/**
 * Chaincode Service
 */
export class CCService {
  @inject(FabricServiceBindings.CA_SERVICE)
  public fabric_ca_service: CAService;
 
  // default caller is org1 identity.
  public caller: AnyObject = {
    identifier: process.env.HLF_ORG1_ADMIN_IDENTITY,
    company_id: LogChainConfigs.LogChainCompanyId,
  };
 
  gateway: Gateway;
 
  constructor() {
    this.gateway = new Gateway();
  }
 
  /**
   *
   * @param fnc_name
   * @param params
   */
  async invoke(
    fnc_name: string,
    params: RequestParams,
    options: AnyObject = {is_transient: true},
    caller?: AnyObject,
  ): Promise<string> {
    if (process.env.HLF_BY_PASS === 'true') {
      return StringUtils.randomFabricTxId();
    }
 
    if (!caller) {
      caller = this.caller;
    }
    // if identifier not provided. it will be identity of admin.
    const {wallet, identity} = await this.fabric_ca_service.getIdentityOfCaller(
      caller.identifier,
    );
 
    const configs = AppUtil.buildOrgConfigsBasedOnCompany(caller.company_id);
    console.log(
      `\n--> Fabric client user & Gateway init: Using ${configs.org} identity`,
    );
 
    const cpp = await AppUtil.buildCCPOrg(
      configs.org,
      configs.connectionConfigFile,
    );
 
    await this.gateway.connect(cpp, {
      wallet,
      identity: identity,
      discovery: {
        enabled: true,
        asLocalhost: process.env.HLF_DISCOVERY_AS_LOCALHOST === 'true',
      },
    });
 
    // Get the network (channel) our contract is deployed to.
    const network = await this.gateway.getNetwork(configs.channelName);
 
    // Get the contract from the network.
    const contract = network.getContract(configs.chaincodeName);
    console.log(`\n**************** As ${configs.org} Client ****************`);
    const stateful_txn = contract.createTransaction(fnc_name);
    //if you need to customize endorsement to specific set of Orgs, use setEndorsingOrganizations
    console.log('configs.mspOrg', configs.mspOrg);
 
    if (options?.is_transient) {
      stateful_txn.setTransient({
        transient_input: Buffer.from(JSON.stringify(params)),
      });
 
      await stateful_txn.submit();
    } else {
      if (typeof params !== 'string') {
        params = JSON.stringify(params);
      }
 
      await stateful_txn.submit(params);
    }
    console.log(`Transaction has been submitted`);
    // Disconnect from the gateway.
    this.gateway.disconnect();
 
    return stateful_txn.getTransactionId();
  }
 
  /**
   *
   * @param fnc_name
   * @param params
   */
  async query(fnc_name: string, params: RequestParams) {}
}