- Added Node sdk test examples to interact with bityoga fabric set up

- Refer README for more instructions
parent 457a39fa
# Hyperledger Fabric NodeSDK Tester
Node sdk test examples to interact with bityoga fabric set up
# Requirements
1. Bityoga fabric sdk should be up and running
2. Node version
- Supports node version >=8
- Tested with v8.9.0
# Run Instructions
1. ## Clone this repository
- git clone https://github.com/bityoga/fabric_node_sdk_tester.git
2. ## Run npm install
- cd fabric_node_sdk_tester
- #### Set node version
- nvm use node v8.9.0 (using nvm)
- npm install
3. ## Update ip address in 'network_profile.json'
- update the url ip addresses of orderer, peer2, orgca, tlsca (4 places).
- update it with your prime manager's ip address
4. ## Retrieve hyperledger fabric tls certificates of 'orderer' and 'peer2'
#### (Replace ip address with your fabric prime manager's ip address)
- scp -r root@178.62.207.235:/root/hlft-store/tlsca/orderer/tls-msp/tlscacerts/tls-tlsca-7054.pem ./hlft-store/tlsca/orderer/tls-msp/tlscacerts/tls-tlsca-7054.pem
- scp -r /root/hlft-store/tlsca/peer2/tls-msp/tlscacerts/tls-tlsca-7054.pem ./hlft-store/tlsca/peer2/tls-msp/tlscacerts/tls-tlsca-7054.pem
(or)
- run shell script **" bash get_tls_certificates.sh "**
5. ## Enroll admin
- #### Command : node enrollAdmin.js
- check wallet directory
- a directory named 'admin' will be available
- certificates for admin will be available under this directory.
6. ## Register user
- If needed, edit the following lines in registerUser.js
- const user_name = "lion";
- const user_password = "lion";
- const user_role = "client";
- #### Command : node registerUser.js
- check wallet directory
- a directory with the name of the 'registerd user name' will be available
- certificates for the registerd user will be available under this directory.
7. ## Query a chaincode
- If needed, edit the following lines in query.js
- const user_name = "lion";
- const CHANNEL_NAME = "appchannel";
- const CHAIN_CODE_NAME = "carcc";
- const CHAIN_CODE_FUNCTION_NAME = "listCars";
- const result = await contract.evaluateTransaction(CHAIN_CODE_FUNCTION_NAME);
- const result = await contract.evaluateTransaction(CHAIN_CODE_FUNCTION_NAME,"b");
- #### Command : node query.js
8. ## Invoke a chaincode
- If needed, edit the following lines in invoke.js
- const user_name = "lion";
- const CHANNEL_NAME = "appchannel";
- const CHAIN_CODE_NAME = "carcc";
- const CHAIN_CODE_FUNCTION_NAME = "createCar";
- await contract.submitTransaction(CHAIN_CODE_FUNCTION_NAME, "TR8800","Opel","Corsa","Light Blue","7","2050","1");
- await contract.submitTransaction(CHAIN_CODE_FUNCTION_NAME, "b","a","1");
- #### Command : node invoke.js
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const FabricCAServices = require('fabric-ca-client');
const { FileSystemWallet, X509WalletMixin } = require('fabric-network');
const fs = require('fs');
const path = require('path');
const ccpPath = path.resolve(__dirname, '.', 'network_profile.json');
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);
const admin_username = "ca-admin-orgca";
const admin_password = "orgcapw";
const ORGANISATION_MSP = "hlfMSP";
const CA_ORGANISATION_NAME = "orgca";
async function main() {
try {
// Create a new CA client for interacting with the CA.
const caInfo = ccp.certificateAuthorities[CA_ORGANISATION_NAME];
console.log(caInfo);
const caTLSCACerts = [];
//const caTLSCACerts = caInfo.tlsCACerts.pem;
const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the admin user.
const adminExists = await wallet.exists('admin');
if (adminExists) {
console.log('An identity for the admin user "admin" already exists in the wallet');
return;
}
// Enroll the admin user, and import the new identity into the wallet.
const enrollment = await ca.enroll({ enrollmentID: admin_username, enrollmentSecret: admin_password });
const identity = X509WalletMixin.createIdentity(ORGANISATION_MSP, enrollment.certificate, enrollment.key.toBytes());
await wallet.import('admin', identity);
console.log('Successfully enrolled admin user "admin" and imported it into the wallet');
} catch (error) {
console.error(`Failed to enroll admin user "admin": ${error}`);
process.exit(1);
}
}
main();
#!/bin/bash
set -x #echo on
IP_ADDRESS="164.90.214.237"
REMOTE_MACHINE_TLS_CERT_FILE="/root/hlft-store/hlfMSP/tlscacerts/tls-tlsca-7054.pem"
LOCAL_TLS_CERT_FILE="./hlft-store/hlfMSP/tlscacerts/tls-tlsca-7054.pem"
scp -r root@$IP_ADDRESS:$REMOTE_MACHINE_TLS_CERT_FILE $LOCAL_TLS_CERT_FILE
\ No newline at end of file
-----BEGIN CERTIFICATE-----
MIICATCCAaegAwIBAgIUP/fI4qZbd8HE2ZTS4OHbB9ntc7gwCgYIKoZIzj0EAwIw
XTELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENhcm9saW5hMRQwEgYDVQQK
EwtIeXBlcmxlZGdlcjEPMA0GA1UECxMGRmFicmljMQ4wDAYDVQQDEwV0bHNjYTAe
Fw0yMDA4MTMwODU4MDBaFw0zNTA4MTAwODU4MDBaMF0xCzAJBgNVBAYTAlVTMRcw
FQYDVQQIEw5Ob3J0aCBDYXJvbGluYTEUMBIGA1UEChMLSHlwZXJsZWRnZXIxDzAN
BgNVBAsTBkZhYnJpYzEOMAwGA1UEAxMFdGxzY2EwWTATBgcqhkjOPQIBBggqhkjO
PQMBBwNCAASXGzTNSh2D/Fi2NaLTsygpLQshZppkQO9Wrq2km9VC5XFNUeaAtUSx
hGepwgKuQ1ofcZVLBRh3IZ3So9MDfftko0UwQzAOBgNVHQ8BAf8EBAMCAQYwEgYD
VR0TAQH/BAgwBgEB/wIBATAdBgNVHQ4EFgQUu7SghRZCw0VDPRR0CdUKoyfsMMkw
CgYIKoZIzj0EAwIDSAAwRQIhAOlDaWS4YC7GN9UVqW5WJR4mOcHAaeXEmsXDM1yN
CRUYAiAM+yZwJe11TbLSsmA0IaCN4tqU7DC4skR86CMcwhcT9A==
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
MIICADCCAaegAwIBAgIUaWBQK+8YCdldiOXOAc/1O/mR4UUwCgYIKoZIzj0EAwIw
XTELMAkGA1UEBhMCVVMxFzAVBgNVBAgTDk5vcnRoIENh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9ov++WEANsqR3+00xqEWGMCCIy4qm
7NRq5mVlLyZwN/gIKi3QB78hYln5KxqMo0UwQzAOBgNVHQ8BAf8EBAMCAQYwEgYD
VR0TAQH/BAgwBgEB/wIBATAdBgNVHQ4EFgQUWDuGHUYguQE2PFFzBRn4Xy4qgm4w
CgYIKoZIzj0EAwIDRwAwRAIgXLkp85Fxrr+yKJ2Ger4h+EDH/C617+it7+65ZaVh
GxQCIHAekGPEy91asdd7tnp+5De+IPzL0BVpgp4W9Y+KX+5E
-----END CERTIFICATE-----
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { FileSystemWallet, Gateway } = require('fabric-network');
const path = require('path');
const ccpPath = path.resolve(__dirname, '.', 'network_profile.json');
const user_name = "lion";
const CHANNEL_NAME = "appchannel";
const CHAIN_CODE_NAME = "carcc";
const CHAIN_CODE_FUNCTION_NAME = "createCar";
// const user_name = "lion";
// const CHANNEL_NAME = "appchannel";
// const CHAIN_CODE_NAME = "testcc";
// const CHAIN_CODE_FUNCTION_NAME = "invoke";
async function main() {
try {
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
const userExists = await wallet.exists(user_name);
if (!userExists) {
console.log('An identity for the user "user1" does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
return;
}
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccpPath, { wallet, identity: user_name, discovery: { enabled: false, asLocalhost: false } });
// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork(CHANNEL_NAME);
// Get the contract from the network.
const contract = network.getContract(CHAIN_CODE_NAME);
// Submit the specified transaction.
// createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom')
// changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR10', 'Dave')
await contract.submitTransaction(CHAIN_CODE_FUNCTION_NAME, "BE8800","Opel","Corsa","Light Blue","7","2050","1");
//await contract.submitTransaction(CHAIN_CODE_FUNCTION_NAME, "b","a","1");
console.log('Transaction has been submitted');
// Disconnect from the gateway.
await gateway.disconnect();
} catch (error) {
console.error(`Failed to submit transaction: ${error}`);
process.exit(1);
}
}
main();
{
"name": "hlf-bityoga",
"version": "1.0.0",
"client": {
"organization": "hlf",
"connection": {
"timeout": {
"peer": {
"endorser": "300"
}
}
}
},
"channels": {
"appchannel": {
"orderers": ["orderer"],
"peers": {
"peer2": {
"endorsingPeer": "true",
"chaincodeQuery": "true",
"ledgerQuery": "true",
"eventSource": "true",
"discover": "true"
},
"peer1": {
"endorsingPeer": "false",
"chaincodeQuery": "false",
"ledgerQuery": "true",
"eventSource": "false",
"discover": "true"
}
}
}
},
"organizations": {
"hlf": {
"mspid": "hlfMSP",
"peers": ["peer2", "peer1"],
"certificateAuthorities": ["orgca", "tlsca"]
}
},
"orderers": {
"orderer": {
"url": "grpcs://164.90.214.237:8053",
"tlsCACerts": {
"path": "./hlft-store/hlfMSP/tlscacerts/tls-tlsca-7054.pem"
},
"grpcOptions": {
"ssl-target-name-override": "orderer"
}
}
},
"peers": {
"peer2": {
"url": "grpcs://164.90.214.237:8055",
"tlsCACerts": {
"path": "./hlft-store/hlfMSP/tlscacerts/tls-tlsca-7054.pem"
},
"grpcOptions": {
"ssl-target-name-override": "peer2"
}
}
},
"certificateAuthorities": {
"orgca": {
"caName": "orgca",
"url": "https://164.90.214.237:8052",
"httpOptions": {
"verify": false
}
},
"tlsca": {
"caName": "tlsca",
"url": "https://164.90.214.237:8081",
"httpOptions": {
"verify": false
}
}
}
}
{
"engines": {
"node": ">=8",
"npm": ">=5"
},
"scripts": {
"lint": "eslint .",
"pretest": "npm run lint",
"test": "nyc mocha --recursive"
},
"engineStrict": true,
"author": "Hyperledger",
"license": "Apache-2.0",
"dependencies": {
"fabric-ca-client": "~1.4.3",
"fabric-network": "~1.4.3"
},
"devDependencies": {
"chai": "^4.2.0",
"eslint": "^5.9.0",
"mocha": "^5.2.0",
"nyc": "^15.0.0",
"sinon": "^7.1.1",
"sinon-chai": "^3.3.0"
},
"nyc": {
"exclude": [
"coverage/**",
"test/**"
],
"reporter": [
"text-summary",
"html"
],
"all": true,
"check-coverage": true,
"statements": 100,
"branches": 100,
"functions": 100,
"lines": 100
}
}
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { FileSystemWallet, Gateway } = require('fabric-network');
const path = require('path');
const ccpPath = path.resolve(__dirname, '.', 'network_profile.json');
const user_name = "lion";
const CHANNEL_NAME = "appchannel";
const CHAIN_CODE_NAME = "carcc";
const CHAIN_CODE_FUNCTION_NAME = "listCars";
// const user_name = "lion";
// const CHANNEL_NAME = "appchannel";
// const CHAIN_CODE_NAME = "testcc";
// const CHAIN_CODE_FUNCTION_NAME = "query";
async function main() {
try {
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
const userExists = await wallet.exists(user_name);
if (!userExists) {
console.log('An identity for the user "user1" does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
return;
}
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccpPath, { wallet, identity: user_name, discovery: { enabled: false, asLocalhost: false } });
// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork(CHANNEL_NAME);
// Get the contract from the network.
const contract = network.getContract(CHAIN_CODE_NAME);
// Evaluate the specified transaction.
// queryCar transaction - requires 1 argument, ex: ('queryCar', 'CAR4')
// queryAllCars transaction - requires no arguments, ex: ('queryAllCars')
const result = await contract.evaluateTransaction(CHAIN_CODE_FUNCTION_NAME);
//const result = await contract.evaluateTransaction(CHAIN_CODE_FUNCTION_NAME,"b");
//const result = await contract.evaluateTransaction(CHAIN_CODE_FUNCTION_NAME,"a");
console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
process.exit(1);
} catch (error) {
console.error(`Failed to evaluate transaction: ${error}`);
//process.exit(1);
}
}
main();
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { FileSystemWallet, Gateway, X509WalletMixin } = require('fabric-network');
const path = require('path');
const ccpPath = path.resolve(__dirname, '.', 'network_profile.json');
const user_name = "lion";
const user_password = "lion";
const user_role = "client";
async function main() {
try {
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
const userExists = await wallet.exists(user_name);
if (userExists) {
console.log('An identity for the user "'+user_name+'" already exists in the wallet');
return;
}
// Check to see if we've already enrolled the admin user.
const adminExists = await wallet.exists('admin');
if (!adminExists) {
console.log('An identity for the admin user "admin" does not exist in the wallet');
console.log('Run the enrollAdmin.js application before retrying');
return;
}
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccpPath, { wallet, identity: 'admin', discovery: { enabled: true, asLocalhost: false } });
// Get the CA client object from the gateway for interacting with the CA.
const ca = gateway.getClient().getCertificateAuthority();
const adminIdentity = gateway.getCurrentIdentity();
// Register the user, enroll the user, and import the new identity into the wallet.
const secret = await ca.register({ enrollmentID: user_name,role: user_role,enrollmentSecret:user_password }, adminIdentity);
const enrollment = await ca.enroll({ enrollmentID: user_name, enrollmentSecret: secret });
const userIdentity = X509WalletMixin.createIdentity('hlfMSP', enrollment.certificate, enrollment.key.toBytes());
await wallet.import(user_name, userIdentity);
console.log('Successfully registered and enrolled user "'+user_name+'" with role "'+user_role+'" and imported it into the wallet');
} catch (error) {
console.error(`Failed to register user "user_name": ${error}`);
process.exit(1);
}
}
main();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment