Skip to content

Commit

Permalink
BE-745 Migrate wallet implementation to v2.1 (#107)
Browse files Browse the repository at this point in the history
Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>
  • Loading branch information
nekia authored May 27, 2020
1 parent dd00ce6 commit 8600c89
Show file tree
Hide file tree
Showing 13 changed files with 1,315 additions and 453 deletions.
19 changes: 9 additions & 10 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,6 @@
"react/prefer-stateless-function": ["off"],
"react/sort-comp": ["off"],
"react/jsx-one-expression-per-line": ["off"],

"overrides": [
{
"files": ["*.spec.js", "**/app/test/**"],
"rules": {
"no-unused-expressions": "off"
}
}
],
"padded-blocks": "off",
"camelcase": ["off"],
"no-magic-numbers": ["off"],
Expand Down Expand Up @@ -445,5 +436,13 @@
"minLength": 3
}
]
}
},
"overrides": [
{
"files": ["*.spec.js", "**/app/test/**"],
"rules": {
"no-unused-expressions": "off"
}
}
]
}
91 changes: 7 additions & 84 deletions app/platform/fabric/FabricClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
*/

const Fabric_Client = require('fabric-client');
const { BlockDecoder, User } = require('fabric-common');
const path = require('path');

const Constants = require('fabric-client/lib/Constants.js');

const BlockDecoder = require('fabric-client/lib/BlockDecoder');
const User = require('fabric-client/lib/User.js');
const ExplorerError = require('../../common/ExplorerError');
const FabricUtils = require('./utils/FabricUtils.js');
const FabricGateway = require('../../platform/fabric/gateway/FabricGateway');
Expand All @@ -17,8 +14,6 @@ const helper = require('../../common/helper');

const logger = helper.getLogger('FabricClient');

const ROLES = Constants.NetworkConfig.ROLES;

const explorer_mess = require('../../common/ExplorerMessage').explorer;

/**
Expand All @@ -44,13 +39,9 @@ class FabricClient {
this.channelsGenHash = new Map();
this.client_config = null;
this.config = null;
this.peerroles = {};
this.status = false;
this.tls = false;
this.asLocalhost = null;
for (const role of ROLES) {
this.peerroles[role] = role;
}
this.asLocalhost = false;
}

/**
Expand Down Expand Up @@ -79,13 +70,7 @@ class FabricClient {
// Use Gateway to connect to fabric network
this.fabricGateway = new FabricGateway(configPath);
await this.fabricGateway.initialize();
this.hfc_client = await this.fabricGateway.getClient();
this.hfc_client.setConfigSetting('initialize-with-discovery', true);
/* eslint-disable */
this.asLocalhost =
String(
this.hfc_client.getConfigSetting('discovery-as-localhost', 'true')
) === 'true';
this.hfc_client = Fabric_Client.loadFromConfig(this.fabricGateway.config);
/* eslint-enable */
const channel_name = this.fabricGateway.getDefaultChannelName();
this.defaultPeer = this.fabricGateway.getDefaultPeer();
Expand All @@ -104,6 +89,10 @@ class FabricClient {
} else {
this.hfc_client.setConfigSetting('discovery-protocol', 'grpcs');
}
this.asLocalhost =
String(
this.hfc_client.getConfigSetting('discovery-as-localhost', 'true')
) === 'true';
// Enable discover service
await this.defaultChannel.initialize({
discover: true,
Expand Down Expand Up @@ -289,72 +278,6 @@ class FabricClient {
}
}

/**
*
*
* @param {*} client_config
* @returns
* @memberof FabricClient
*/
async getRegisteredUser(client_config) {
let username = null;
try {
username = Fabric_Client.getConfigSetting('enroll-id', 'dflt_hlbeuser');
const userOrg = client_config.client.organization;

logger.debug('Successfully initialized the credential stores');

/*
* Client can now act as an agent for the specified organization
* First check to see if the user is already enrolled
*/
let user = await this.hfc_client.getUserContext(username, true);
if (user && user.isEnrolled()) {
logger.info('Successfully loaded member from persistence [%s]', username);
} else {
// User was not enrolled, so we will need an admin user object to register
logger.info(
'User %s was not enrolled, so we will need an admin user object to register',
username
);

const adminUserObj = await this.hfc_client.setUserContext({
username: Fabric_Client.getConfigSetting('admin-username', 'admin'),
password: Fabric_Client.getConfigSetting('admin-secret', 'adminpw')
});
const caClient = this.hfc_client.getCertificateAuthority();
const secret = await caClient.register(
{
enrollmentID: username,
affiliation:
userOrg.toLowerCase() +
Fabric_Client.getConfigSetting('enroll-affiliation', '')
},
adminUserObj
);
logger.debug('Successfully got the secret for user %s', username);
user = await this.hfc_client.setUserContext({
username: username,
password: secret
});
logger.debug(
'Successfully enrolled username %s and setUserContext on the client object',
username
);
}
if (!user || !user.isEnrolled) {
throw new Error('User was not enrolled ');
}
} catch (error) {
logger.error(
'Failed to get registered user: %s with error: %s',
username,
error.toString()
);
return 'failed ' + error.toString();
}
}

/**
*
*
Expand Down
11 changes: 11 additions & 0 deletions app/platform/fabric/FabricConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ class FabricConfig {
return { orgMsp, adminPrivateKeyPath, signedCertPath };
}

/**
*
*
* @returns
* @memberof FabricConfig
*/
getMspId() {
const organization = this.config.organizations[this.getOrganization()];
return organization.mspid;
}

/**
*
*
Expand Down
1 change: 1 addition & 0 deletions app/platform/fabric/connection-profile/first-network.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"adminPrivateKey": {
"path": "/fabric-path/fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/1bebc656f198efb4b5bed08ef42cf3b2d89ac86f0a6b928e7a172fd823df0a48_sk"
},
"peers": ["peer0.org1.example.com"],
"signedCert": {
"path": "/fabric-path/fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"adminPrivateKey": {
"path": "./app/platform/fabric/e2e-test/specs/crypto-config/peerOrganizations/org1/users/Admin@org1/msp/keystore/priv_sk"
},
"peers": ["peer0-org1"],
"certificateAuthorities": ["ca0"],
"signedCert": {
"path": "./app/platform/fabric/e2e-test/specs/crypto-config/peerOrganizations/org1/users/Admin@org1/msp/signcerts/Admin@org1-cert.pem"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"adminPrivateKey": {
"path": "./app/platform/fabric/e2e-test/specs/crypto-config/peerOrganizations/org2/users/Admin@org2/msp/keystore/priv_sk"
},
"peers": ["peer0-org2"],
"certificateAuthorities": ["ca0"],
"signedCert": {
"path": "./app/platform/fabric/e2e-test/specs/crypto-config/peerOrganizations/org2/users/Admin@org2/msp/signcerts/Admin@org2-cert.pem"
Expand Down
Loading

0 comments on commit 8600c89

Please sign in to comment.