Skip to content

Commit

Permalink
feat(VerifierFactory): add connection feature to both of socket.io-ty…
Browse files Browse the repository at this point in the history
…pe Validator and http-type Validator

Signed-off-by: Takuma TAKEUCHI <takeuchi.takuma@fujitsu.com>
  • Loading branch information
takeutak committed Mar 18, 2021
1 parent 17fdd94 commit d2825cc
Show file tree
Hide file tree
Showing 63 changed files with 1,313 additions and 54 deletions.
100 changes: 100 additions & 0 deletions examples/cartrade/AssetManagement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* AssetManagement.ts
*/

import { LPInfoHolder } from '../../packages/routing-interface/util/LPInfoHolder';
import { VerifierBase } from '../../packages/ledger-plugin/VerifierBase';
import { ContractInfoHolder } from '../../packages/routing-interface/util/ContractInfoHolder';
import { ConfigUtil } from '../../packages/routing-interface/util/ConfigUtil';

const fs = require('fs');
const path = require('path');
const config: any = ConfigUtil.getConfig();
import { getLogger } from "log4js";
const moduleName = 'AssetManagement';
const logger = getLogger(`${moduleName}`);
logger.level = config.logLevel;

export class AssetManagement {
private connectInfo: LPInfoHolder = null; // connection information
private contractInfoholder: ContractInfoHolder = null; // contract information
private verifierEthereum: VerifierBase = null;

constructor() {
this.connectInfo = new LPInfoHolder();
this.contractInfoholder = new ContractInfoHolder();
}

addAsset(amount: string): Promise<any> {
return new Promise((resolve, reject) => {
if (this.verifierEthereum === null) {
logger.debug("create verifierEthereum");
const ledgerPluginInfo: string = this.connectInfo.getLegerPluginInfo("84jUisrs");
this.verifierEthereum = new VerifierBase(ledgerPluginInfo);
}

// for Neo
const contractInfo: string = this.contractInfoholder.getContractInfo("AssetContract");
const contractInfoObj: {} = JSON.parse(contractInfo);
const coinbase = contractInfoObj['_eth']['coinbase'];
const address = contractInfoObj['address'];
const abi = contractInfoObj['abi'];
const contract = {
"address": address,
"abi": abi
};
const method = {type: "contract", command: "addAsset", function: "sendTransaction"};
const args = {"args": [amount, {from: coinbase}]};

this.verifierEthereum.execSyncFunctionNeo(contract, method, args).then(result => {
const response = {
"status": result.status,
"Transaction hash": result.data
}
resolve(response);
}).catch((err) => {
logger.error(err);
reject(err);
});

});
}

getAsset(): Promise<any> {
return new Promise((resolve, reject) => {
if (this.verifierEthereum === null) {
logger.debug("create verifierEthereum");
const ledgerPluginInfo: string = this.connectInfo.getLegerPluginInfo("84jUisrs");
this.verifierEthereum = new VerifierBase(ledgerPluginInfo);
}

// for Neo
const contractInfo: string = this.contractInfoholder.getContractInfo("AssetContract");
const contractInfoObj: {} = JSON.parse(contractInfo);
const address = contractInfoObj['address'];
const abi = contractInfoObj['abi'];
const contract = {
"address": address,
"abi": abi
};
const method = {type: "contract", command: "getAsset", function: "call"};
const args = {"args": []};

this.verifierEthereum.execSyncFunctionNeo(contract, method, args).then(result => {
const response = {
"status": result.status,
"asset": parseFloat(result.data)
}
resolve(response);
}).catch((err) => {
logger.error(err);
reject(err);
});

});
}

}
59 changes: 59 additions & 0 deletions examples/cartrade/BalanceManagement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* BalanceManagement.ts
*/

import { LPInfoHolder } from '../../packages/routing-interface/util/LPInfoHolder';
import { VerifierBase } from '../../packages/ledger-plugin/VerifierBase';
import { ConfigUtil } from '../../packages/routing-interface/util/ConfigUtil';

const fs = require('fs');
const path = require('path');
const config: any = ConfigUtil.getConfig();
import { getLogger } from "log4js";
const moduleName = 'BalanceManagement';
const logger = getLogger(`${moduleName}`);
logger.level = config.logLevel;

export class BalanceManagement {
private connectInfo: LPInfoHolder = null; // connection information
private verifierEthereum: VerifierBase = null;

constructor() {
this.connectInfo = new LPInfoHolder();
}

getBalance(account: string): Promise<any> {
return new Promise((resolve, reject) => {
if (this.verifierEthereum === null) {
logger.debug("create verifierEthereum");
const ledgerPluginInfo: string = this.connectInfo.getLegerPluginInfo("84jUisrs");
this.verifierEthereum = new VerifierBase(ledgerPluginInfo);
}

// for LedgerOperation
// const execData = {"referedAddress": account};
// const ledgerOperation: LedgerOperation = new LedgerOperation("getNumericBalance", "", execData);

// for Neo
const contract = {}; // NOTE: Since contract does not need to be specified, specify an empty object.
const method = {type: "web3Eth", command: "getBalance"};
const args = {"args": [account]};

this.verifierEthereum.execSyncFunctionNeo(contract, method, args).then(result => {
const response = {
"status": result.status,
"amount": parseFloat(result.data)
}
resolve(response);
}).catch((err) => {
logger.error(err);
reject(err);
});

});
}

}
12 changes: 8 additions & 4 deletions examples/cartrade/BusinessLogicCartrade.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Hyperledger Cactus Contributors
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* BusinessLogicCartrade.ts
Expand All @@ -14,6 +14,7 @@ import { TransactionData } from './TransactionData';
import { BusinessLogicInquireCartradeStatus } from './BusinessLogicInquireCartradeStatus';
import { TxInfoData } from './TxInfoData';
import { transactionManagement } from '../../packages/routing-interface/routes/index';
import { verifierFactory } from '../../packages/routing-interface/routes/index';
import { LedgerOperation } from '../../packages/business-logic-plugin/LedgerOperation';
import { BusinessLogicBase } from '../../packages/business-logic-plugin/BusinessLogicBase';
import { makeRawTransaction } from './TransactionEthereum'
Expand Down Expand Up @@ -106,7 +107,8 @@ export class BusinessLogicCartrade extends BusinessLogicBase {
// Get Verifier Instance
logger.debug(`##firstTransaction(): businessLogicID: ${tradeInfo.businessLogicID}`);
const useValidator = JSON.parse(transactionManagement.getValidatorToUse(tradeInfo.businessLogicID));
const verifierEthereum = transactionManagement.getVerifier(useValidator['validatorID'][0]);
// const verifierEthereum = transactionManagement.getVerifier(useValidator['validatorID'][0]);
const verifierEthereum = verifierFactory.getVerifier(useValidator['validatorID'][0]);
logger.debug("getVerifierEthereum");

// TODO: get private key from
Expand Down Expand Up @@ -156,7 +158,8 @@ export class BusinessLogicCartrade extends BusinessLogicBase {
// Get Verifier Instance
logger.debug(`##secondTransaction(): businessLogicID: ${tradeInfo.businessLogicID}`);
const useValidator = JSON.parse(transactionManagement.getValidatorToUse(tradeInfo.businessLogicID));
const verifierFabric = transactionManagement.getVerifier(useValidator['validatorID'][1]);
// const verifierFabric = transactionManagement.getVerifier(useValidator['validatorID'][1]);
const verifierFabric = verifierFactory.getVerifier(useValidator['validatorID'][1]);
logger.debug("getVerifierFabric");

// Generate parameters for sendSignedProposal(changeCarOwner)
Expand Down Expand Up @@ -195,7 +198,8 @@ export class BusinessLogicCartrade extends BusinessLogicBase {
// Get Verifier Instance
logger.debug(`##thirdTransaction(): businessLogicID: ${tradeInfo.businessLogicID}`);
const useValidator = JSON.parse(transactionManagement.getValidatorToUse(tradeInfo.businessLogicID));
const verifierEthereum = transactionManagement.getVerifier(useValidator['validatorID'][0]);
// const verifierEthereum = transactionManagement.getVerifier(useValidator['validatorID'][0]);
const verifierEthereum = verifierFactory.getVerifier(useValidator['validatorID'][0]);
logger.debug("getVerifierEthereum");

// TODO: Get address of escrow and set parameter
Expand Down
2 changes: 1 addition & 1 deletion examples/cartrade/BusinessLogicInquireCartradeStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Hyperledger Cactus Contributors
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* BusinessLogicInquireCartradeStatus.ts
Expand Down
72 changes: 72 additions & 0 deletions examples/cartrade/CarsManagement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* CarsManagement.ts
*/

import { LPInfoHolder } from '../../packages/routing-interface/util/LPInfoHolder';
import { VerifierBase } from '../../packages/ledger-plugin/VerifierBase';
import { ConfigUtil } from '../../packages/routing-interface/util/ConfigUtil';

const fs = require('fs');
const path = require('path');
const config: any = ConfigUtil.getConfig();
import { getLogger } from "log4js";
const moduleName = 'CarsManagement';
const logger = getLogger(`${moduleName}`);
logger.level = config.logLevel;

export class CarsManagement {
private connectInfo: LPInfoHolder = null; // connection information
private verifierFabric: VerifierBase = null;

constructor() {
this.connectInfo = new LPInfoHolder();
}

queryCar(carID: string): Promise<any> {
return new Promise((resolve, reject) => {
if (this.verifierFabric === null) {
logger.debug("create verifierFabric");
const ledgerPluginInfo: string = this.connectInfo.getLegerPluginInfo("r9IS4dDf");
this.verifierFabric = new VerifierBase(ledgerPluginInfo);
}

const contract = {"channelName": "mychannel", "contractName": "fabcar"};
const method = {type: "evaluateTransaction", command: "queryCar"};
const args = {"args": [carID]};

this.verifierFabric.execSyncFunctionNeo(contract, method, args).then(result => {
resolve(result);
}).catch((err) => {
logger.error(err);
reject(err);
});

});
}

queryAllCars(): Promise<any> {
return new Promise((resolve, reject) => {
if (this.verifierFabric === null) {
logger.debug("create verifierFabric");
const ledgerPluginInfo: string = this.connectInfo.getLegerPluginInfo("r9IS4dDf");
this.verifierFabric = new VerifierBase(ledgerPluginInfo);
}

const contract = {"channelName": "mychannel", "contractName": "fabcar"};
const method = {type: "evaluateTransaction", command: "queryAllCars"};
const args = {"args": []};

this.verifierFabric.execSyncFunctionNeo(contract, method, args).then(result => {
resolve(result);
}).catch((err) => {
logger.error(err);
reject(err);
});

});
}

}
2 changes: 1 addition & 1 deletion examples/cartrade/ResultTransactionStatusData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Hyperledger Cactus Contributors
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* ResultTransactionStatusData.ts
Expand Down
2 changes: 1 addition & 1 deletion examples/cartrade/TransactionData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Hyperledger Cactus Contributors
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* TransactionData.ts
Expand Down
2 changes: 1 addition & 1 deletion examples/cartrade/TransactionEthereum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Hyperledger Cactus Contributors
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* TransactionEthereum.ts
Expand Down
2 changes: 1 addition & 1 deletion examples/cartrade/TransactionFabric.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Hyperledger Cactus Contributors
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* TransactionFabric.ts
Expand Down
2 changes: 1 addition & 1 deletion examples/cartrade/TransactionInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Hyperledger Cactus Contributors
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* TransactionInfo.ts
Expand Down
2 changes: 1 addition & 1 deletion examples/cartrade/TransactionInfoManagement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Hyperledger Cactus Contributors
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* TransactionInfoManagement.ts
Expand Down
2 changes: 1 addition & 1 deletion examples/cartrade/TransactionStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Hyperledger Cactus Contributors
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* TransactionStatus.ts
Expand Down
2 changes: 1 addition & 1 deletion examples/cartrade/TxInfoData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Hyperledger Cactus Contributors
* Copyright 2020-2021 Hyperledger Cactus Contributors
* SPDX-License-Identifier: Apache-2.0
*
* TxInfoData.ts
Expand Down
Loading

0 comments on commit d2825cc

Please sign in to comment.