passed getblockchain info
parent
342b986c57
commit
9bd2bb397a
|
@ -0,0 +1,148 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const {Endorser, Endpoint, Discoverer} = require('fabric-common');
|
||||||
|
const Client = require('fabric-common/lib/Client');
|
||||||
|
const User = require('fabric-common/lib/User');
|
||||||
|
const Signer = require('fabric-common/lib/Signer');
|
||||||
|
const Utils = require('fabric-common/lib/Utils');
|
||||||
|
const {common: commonProto} = require('fabric-protos');
|
||||||
|
const {Gateway} = require('fabric-network');
|
||||||
|
const IdentityContext = require('fabric-common/lib/IdentityContext');
|
||||||
|
|
||||||
|
const mspId = 'Org1MSP';
|
||||||
|
const peerURL = `grpcs://peer1.org1.example.com:7051`;
|
||||||
|
const channelName = 'businesschannel';
|
||||||
|
const tlsCAPath = path.resolve('../crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/tlscacerts/tlsca.org1.example.com-cert.pem');
|
||||||
|
const signCertPath = path.resolve('../crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem');
|
||||||
|
const signKeyPath = path.resolve('../crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/priv_sk');
|
||||||
|
|
||||||
|
const tlsCACert = fs.readFileSync(tlsCAPath).toString();
|
||||||
|
const signCert = fs.readFileSync(signCertPath).toString();
|
||||||
|
const signKey = fs.readFileSync(signKeyPath).toString();
|
||||||
|
|
||||||
|
class gwSigningIdentity {
|
||||||
|
constructor(signingIdentity) {
|
||||||
|
this.type = 'X.509';
|
||||||
|
this.mspId = signingIdentity._mspId;
|
||||||
|
this.credentials = {
|
||||||
|
certificate: signingIdentity._certificate.toString().trim(),
|
||||||
|
privateKey: signingIdentity._signer._key.toBytes().trim(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main entrance method
|
||||||
|
* @returns {Promise<*>}
|
||||||
|
*/
|
||||||
|
const main = async () => {
|
||||||
|
const options = {
|
||||||
|
url: peerURL,
|
||||||
|
'grpc-wait-for-ready-timeout': 30000,
|
||||||
|
pem: tlsCACert
|
||||||
|
//'grpc.ssl_target_name_override': peerHost
|
||||||
|
};
|
||||||
|
const endpoint = new Endpoint(options);
|
||||||
|
const endorser = new Endorser('myEndorser', {}, mspId);
|
||||||
|
endorser.setEndpoint(endpoint);
|
||||||
|
await endorser.connect();
|
||||||
|
|
||||||
|
// Only for test purpose, safe to remove following 3 lines
|
||||||
|
const discoverer = new Discoverer('myDiscoverer', {}, mspId);
|
||||||
|
discoverer.setEndpoint(endpoint);
|
||||||
|
await discoverer.connect();
|
||||||
|
|
||||||
|
const user = loadUser('test-Admin', signCert, signKey);
|
||||||
|
//---- query on system chaincode to getChainInfo
|
||||||
|
return getBlockchainInfo(channelName, endorser, user)
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the blockchain info from given channel
|
||||||
|
* @param {string} channelName Channel to fetch blockchain info
|
||||||
|
* @param {Endorser} endorser Endorser to send the request to
|
||||||
|
* @param {User} user Identity to use
|
||||||
|
* @returns {BlockchainInfo} Parsed blockchain info struct
|
||||||
|
*/
|
||||||
|
const getBlockchainInfo = async (channelName, endorser, user) => {
|
||||||
|
const chaincodeId = 'qscc';
|
||||||
|
const fcn = 'GetChainInfo';
|
||||||
|
const args = [channelName];
|
||||||
|
|
||||||
|
let result = await callChaincode(channelName, chaincodeId, fcn, args, endorser, user)
|
||||||
|
return parseBlockchainInfo(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call a chaincode and return the response in raw proto
|
||||||
|
* @param {string} channelName Name of the channel
|
||||||
|
* @param {string} chaincodeId Name of the chaincode
|
||||||
|
* @param {string} fcn Name of the chaincode method to call
|
||||||
|
* @param {string} args Parameters for the Chaincode method
|
||||||
|
* @param {Endorser} endorser Endorser to send the request to
|
||||||
|
* @param {User} user Identity to use
|
||||||
|
* @returns {Promise<Buffer>}
|
||||||
|
*/
|
||||||
|
const callChaincode = async (channelName, chaincodeId, fcn, args, endorser, user) => {
|
||||||
|
const gateWay = new Gateway();
|
||||||
|
const client = new Client(null);
|
||||||
|
const identity = new gwSigningIdentity(user._signingIdentity);
|
||||||
|
gateWay.identity = identity;
|
||||||
|
gateWay.identityContext = new IdentityContext(user, client);
|
||||||
|
|
||||||
|
const channel = client.newChannel(channelName);
|
||||||
|
client.channels.set(channelName, channel);
|
||||||
|
channel.getEndorsers = () => Array.from(channel.endorsers.values());
|
||||||
|
channel.endorsers.set(endorser.toString(), endorser);
|
||||||
|
|
||||||
|
await gateWay.connect(client, {
|
||||||
|
wallet: {}, discovery: {enabled: false}, identity
|
||||||
|
});
|
||||||
|
|
||||||
|
const network = await gateWay.getNetwork(channelName);
|
||||||
|
const contract = network.getContract(chaincodeId);
|
||||||
|
const tx = contract.createTransaction(fcn);
|
||||||
|
|
||||||
|
return tx.evaluate(...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse the result into the blockchain info structure
|
||||||
|
* @param {Buffer} _resultProto The original result from fabric network
|
||||||
|
* @returns {{previousBlockHash: string, currentBlockHash: string, height: number}}
|
||||||
|
*/
|
||||||
|
const parseBlockchainInfo = (_resultProto) => {
|
||||||
|
const {height, currentBlockHash, previousBlockHash} = commonProto.BlockchainInfo.decode(_resultProto);
|
||||||
|
return {
|
||||||
|
height: height.toInt(),
|
||||||
|
currentBlockHash: currentBlockHash.toString('hex'),
|
||||||
|
previousBlockHash: previousBlockHash.toString('hex'),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a user object based on given cert and key strings
|
||||||
|
* @param {string} name Name to assign to the user
|
||||||
|
* @param {string} signCert Certificate string to sign
|
||||||
|
* @param {string} signKey Private key string to sign
|
||||||
|
* @returns {User} User object
|
||||||
|
*/
|
||||||
|
const loadUser = (name, signCert, signKey) => {
|
||||||
|
const SigningIdentity = require('fabric-common/lib/SigningIdentity');
|
||||||
|
const user = new User(name);
|
||||||
|
user._cryptoSuite = Utils.newCryptoSuite();
|
||||||
|
const privateKey = user._cryptoSuite.createKeyFromRaw(signKey);
|
||||||
|
const {_cryptoSuite} = user;
|
||||||
|
const pubKey = _cryptoSuite.createKeyFromRaw(signCert);
|
||||||
|
user._signingIdentity = new SigningIdentity(signCert, pubKey, mspId, _cryptoSuite, new Signer(_cryptoSuite, privateKey));
|
||||||
|
user.getIdentity = () => {
|
||||||
|
return user._signingIdentity;
|
||||||
|
};
|
||||||
|
return user;
|
||||||
|
};
|
||||||
|
|
||||||
|
main()
|
||||||
|
.then(console.log)
|
||||||
|
.catch(console.error);
|
|
@ -346,6 +346,26 @@
|
||||||
"resolved": "https://registry.npmjs.org/jsrsasign/-/jsrsasign-8.0.24.tgz",
|
"resolved": "https://registry.npmjs.org/jsrsasign/-/jsrsasign-8.0.24.tgz",
|
||||||
"integrity": "sha512-u45jAyusqUpyGbFc2IbHoeE4rSkoBWQgLe/w99temHenX+GyCz4nflU5sjK7ajU1ffZTezl6le7u43Yjr/lkQg=="
|
"integrity": "sha512-u45jAyusqUpyGbFc2IbHoeE4rSkoBWQgLe/w99temHenX+GyCz4nflU5sjK7ajU1ffZTezl6le7u43Yjr/lkQg=="
|
||||||
},
|
},
|
||||||
|
"khala-fabric-formatter": {
|
||||||
|
"version": "2.2.0-alpha.8",
|
||||||
|
"resolved": "https://registry.npmjs.org/khala-fabric-formatter/-/khala-fabric-formatter-2.2.0-alpha.8.tgz",
|
||||||
|
"integrity": "sha512-4BxLqe7mOgY8kSETXYDXJwkc+kq133HgTx2NO3/F8vtB7mQ7BnOVL+ZzchbtX8YQOW+h7pCfrrIFxky0p+wl2A==",
|
||||||
|
"requires": {
|
||||||
|
"fabric-protos": "^2.2.8"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"fabric-protos": {
|
||||||
|
"version": "2.2.8",
|
||||||
|
"resolved": "https://registry.npmjs.org/fabric-protos/-/fabric-protos-2.2.8.tgz",
|
||||||
|
"integrity": "sha512-5e3MDLtXdsZpXs92kfTGRirIomaaQ3MaKQ59kp0y9QtYZGced4k9Donl1G3nREoBi0yy1bp45lkDnjRIOG9v+g==",
|
||||||
|
"requires": {
|
||||||
|
"@grpc/grpc-js": "^1.3.4",
|
||||||
|
"@grpc/proto-loader": "^0.6.2",
|
||||||
|
"protobufjs": "^6.11.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"lodash.camelcase": {
|
"lodash.camelcase": {
|
||||||
"version": "4.3.0",
|
"version": "4.3.0",
|
||||||
"resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz",
|
||||||
|
|
|
@ -10,7 +10,9 @@
|
||||||
"author": "Hyperledger",
|
"author": "Hyperledger",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"fabric-common": "^2.2.8",
|
||||||
"fabric-network": "^2.2.8",
|
"fabric-network": "^2.2.8",
|
||||||
"fabric-common": "^2.2.8"
|
"fabric-protos": "^2.2.8",
|
||||||
|
"khala-fabric-formatter": "^2.2.0-alpha.8"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
const fs = require('fs')
|
||||||
|
const {Wallets, Gateway, GatewayOptions} = require('fabric-network');
|
||||||
|
const {Client, Endorser, Endpoint, Discoverer} = require('fabric-common');
|
||||||
|
|
||||||
|
const connectionProfileFileName = "./connection-profile.json"
|
||||||
|
const org1AdminTLSClientCert = fs.readFileSync("/opt/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tls/client.crt").toString()
|
||||||
|
const org1AdminTLSClientKey = fs.readFileSync("/opt/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tls/client.key").toString()
|
||||||
|
const org1AdminCert = fs.readFileSync("/opt/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem").toString()
|
||||||
|
const org1AdminKey = fs.readFileSync("/opt/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/priv_sk").toString()
|
||||||
|
const connectionProfileJson = fs.readFile(connectionProfileFileName).toString();
|
||||||
|
const connectionProfile = JSON.parse(connectionProfileJson);
|
||||||
|
|
||||||
|
const mspId = "Org1MSP"
|
||||||
|
const adminUserId="Admin@org1"
|
||||||
|
const channelName = "businesschannel"
|
||||||
|
const chaincodeId="exp02"
|
||||||
|
|
||||||
|
// Connect to a gateway peer
|
||||||
|
const wallet = Wallets.newFileSystemWallet('/tmp/org1wallet');
|
||||||
|
|
||||||
|
const x509Identity = {
|
||||||
|
credentials: {
|
||||||
|
certificate: org1AdminCert,
|
||||||
|
privateKey: org1AdminKey.toBytes(),
|
||||||
|
},
|
||||||
|
mspId: mspId,
|
||||||
|
type: 'X.509',
|
||||||
|
};
|
||||||
|
wallet.put(adminUserId, x509Identity);
|
||||||
|
|
||||||
|
const gatewayOptions = {
|
||||||
|
identity: adminUserId, // Previously imported identity
|
||||||
|
wallet,
|
||||||
|
};
|
||||||
|
const gateway = new Gateway();
|
||||||
|
gateway.connect(connectionProfile, gatewayOptions);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
// Obtain the smart contract with which our application wants to interact
|
||||||
|
const network = gateway.getNetwork(channelName);
|
||||||
|
const contract = network.getContract(chaincodeId);
|
||||||
|
|
||||||
|
// Submit transactions for the smart contract
|
||||||
|
const args = [arg1, arg2];
|
||||||
|
const submitResult = contract.submitTransaction('transactionName', ...args);
|
||||||
|
|
||||||
|
// Evaluate queries for the smart contract
|
||||||
|
const evalResult = contract.evaluateTransaction('transactionName', ...args);
|
||||||
|
|
||||||
|
// Create and submit transactions for the smart contract with transient data
|
||||||
|
const transientResult = contract.createTransaction(transactionName)
|
||||||
|
.setTransient(privateData)
|
||||||
|
.submit(arg1, arg2);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
// Disconnect from the gateway peer when all work for this client identity is complete
|
||||||
|
gateway.disconnect();
|
||||||
|
}
|
|
@ -1,45 +0,0 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
// work with native fabric
|
|
||||||
|
|
||||||
const fs = require('fs')
|
|
||||||
|
|
||||||
const {Client, Endorser, Endpoint, Discoverer} = require('fabric-common');
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
const peer1TLSCACert=fs.readFileSync("/opt/crypto-config/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem").toString()
|
|
||||||
const org1AdminTLSClientCert=fs.readFileSync("/opt/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tls/client.crt").toString()
|
|
||||||
const org1AdminTLSClientKey=fs.readFileSync("/opt/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tls/client.key").toString()
|
|
||||||
|
|
||||||
const peerURL="grpcs://peer1.org1.example.com:7051"
|
|
||||||
const mspId="Org1MSP"
|
|
||||||
|
|
||||||
const client = new Client('myclient');
|
|
||||||
const endpoint = new Endpoint({url:peerURL, pem:peer1TLSCACert, clientCert:org1AdminTLSClientCert, clientKey:org1AdminTLSClientKey, requestTimeout: 30000, 'grpc-wait-for-ready-timeout': 30000});
|
|
||||||
|
|
||||||
// test using endorser
|
|
||||||
const endorser = new Endorser("myEndorser", client, mspId);
|
|
||||||
endorser.setEndpoint(endpoint);
|
|
||||||
await endorser.resetConnection()
|
|
||||||
try {
|
|
||||||
await endorser.connect(endpoint)
|
|
||||||
} catch (err) {
|
|
||||||
console.log(err)
|
|
||||||
console.log("is connectable = "+endorser.isConnectable())
|
|
||||||
await endorser.checkConnection()
|
|
||||||
if (endorser.hasChaincode("exp03")===true) {
|
|
||||||
console.log("Peer has chaincode")
|
|
||||||
} else {
|
|
||||||
console.log("Peer does not have chaincode")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// test using discoverer
|
|
||||||
const discoverer = new Discoverer("myDiscoverer", client, mspId);
|
|
||||||
discoverer.setEndpoint(endpoint);
|
|
||||||
await discoverer.resetConnection()
|
|
||||||
await discoverer.connect(endpoint)
|
|
||||||
await discoverer.checkConnection()
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
|
@ -1,52 +0,0 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
// work with native fabric
|
|
||||||
|
|
||||||
const fs = require('fs')
|
|
||||||
|
|
||||||
const {Client, Endorser, Endpoint, Discoverer} = require('fabric-common');
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
const peer1TLSCACert = fs.readFileSync("/opt/crypto-config/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem").toString()
|
|
||||||
const org1AdminTLSClientCert = fs.readFileSync("/opt/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tls/client.crt").toString()
|
|
||||||
const org1AdminTLSClientKey = fs.readFileSync("/opt/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/tls/client.key").toString()
|
|
||||||
|
|
||||||
const peerURL = "grpcs://peer1.org1.example.com:7051"
|
|
||||||
const mspId = "Org1MSP"
|
|
||||||
|
|
||||||
const client = new Client('myclient');
|
|
||||||
const endpoint = new Endpoint({
|
|
||||||
url: peerURL,
|
|
||||||
pem: peer1TLSCACert,
|
|
||||||
'grpc-wait-for-ready-timeout': 30000
|
|
||||||
});
|
|
||||||
|
|
||||||
// test using endorser
|
|
||||||
const endorser = new Endorser("myEndorser", {}, mspId);
|
|
||||||
endorser.setEndpoint(endpoint);
|
|
||||||
//await endorser.resetConnection()
|
|
||||||
let isConnected = await endorser.checkConnection()
|
|
||||||
console.log("before, isConnected=", isConnected)
|
|
||||||
|
|
||||||
//await endorser.connect(endpoint)
|
|
||||||
//isConnected = await endorser.checkConnection()
|
|
||||||
//console.log("after, isConnected=", isConnected)
|
|
||||||
|
|
||||||
// TODO: Not working well now, always return true.
|
|
||||||
if (endorser.hasChaincode("exp03") === true) {
|
|
||||||
console.log("Peer has chaincode")
|
|
||||||
} else {
|
|
||||||
console.log("Peer does not have chaincode")
|
|
||||||
}
|
|
||||||
|
|
||||||
// test using discoverer
|
|
||||||
const discoverer = new Discoverer("myDiscoverer", {}, mspId);
|
|
||||||
discoverer.setEndpoint(endpoint);
|
|
||||||
//await discoverer.resetConnection()
|
|
||||||
await discoverer.connect(endpoint)
|
|
||||||
|
|
||||||
isConnected = await discoverer.checkConnection()
|
|
||||||
console.log("discover isConnected=", isConnected)
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
Loading…
Reference in New Issue