Guest User

Untitled

a guest
Jun 20th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.63 KB | None | 0 0
  1. cli
  2. peer1.org1.example.com
  3. peer0.org2.example.com
  4. peer1.org2.example.com
  5. peer0.org1.example.com
  6. orderer.example.com
  7. couchdb3
  8. couchdb2
  9. couchdb1
  10. ca.org1.example.com
  11. couchdb0
  12. ca.org2.example.com
  13.  
  14. ./byfn.sh -m down
  15.  
  16. # clean the keystore
  17. rm -rf ./hfc-key-store
  18. rm -rf ./hfc-key-store2
  19.  
  20. #Generate artifacts
  21. ../bin/cryptogen generate --config=./crypto-config.yaml
  22. export FABRIC_CFG_PATH=$PWD
  23. ../bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
  24. export CHANNEL_NAME=mychannel && ../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
  25. ../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
  26. ../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
  27.  
  28. # Copyright IBM Corp. All Rights Reserved.
  29. #
  30. # SPDX-License-Identifier: Apache-2.0
  31. #
  32.  
  33. version: '2'
  34.  
  35. volumes:
  36. orderer.example.com:
  37. peer0.org1.example.com:
  38. peer1.org1.example.com:
  39. peer0.org2.example.com:
  40. peer1.org2.example.com:
  41.  
  42. networks:
  43. byfn:
  44.  
  45. services:
  46.  
  47. ca0:
  48. image: hyperledger/fabric-ca:$IMAGE_TAG
  49. environment:
  50. - CORE_LOGGING_LEVEL=DEBUG
  51. - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
  52. - FABRIC_CA_SERVER_CA_NAME=ca-org1
  53. - FABRIC_CA_SERVER_CA_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
  54. - FABRIC_CA_SERVER_CA_KEYFILE=/etc/hyperledger/fabric-ca-server-config/f6aa4b99b3177c86e00e55e4ed5dea88ec0c67f27327955eb978b9bf80d6116e_sk
  55. ports:
  56. - "7054:7054"
  57. command: sh -c 'fabric-ca-server start -b admin:adminpw -d'
  58. volumes:
  59. - ./crypto-config/peerOrganizations/org1.example.com/ca/:/etc/hyperledger/fabric-ca-server-config
  60. container_name: ca.org1.example.com
  61. networks:
  62. - byfn
  63.  
  64. ca1:
  65. image: hyperledger/fabric-ca:$IMAGE_TAG
  66. environment:
  67. - CORE_LOGGING_LEVEL=DEBUG
  68. - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
  69. - FABRIC_CA_SERVER_CA_NAME=ca-org2
  70. - FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org2.example.com-cert.pem
  71. - FABRIC_CA_SERVER_TLS_KEYFILE=/etc/hyperledger/fabric-ca-server-config/e6a77b168161b5032d393f9590ad6e61c9d4cde51ca6cfeb8b9dc4d1932b2be9_sk
  72. ports:
  73. - "8054:7054"
  74. command: sh -c 'fabric-ca-server start -b admin:adminpw -d'
  75. volumes:
  76. - ./crypto-config/peerOrganizations/org2.example.com/ca/:/etc/hyperledger/fabric-ca-server-config
  77. container_name: ca.org2.example.com
  78. networks:
  79. - byfn
  80.  
  81. orderer.example.com:
  82. extends:
  83. file: base/docker-compose-base.yaml
  84. service: orderer.example.com
  85. container_name: orderer.example.com
  86. networks:
  87. - byfn
  88.  
  89. peer0.org1.example.com:
  90. container_name: peer0.org1.example.com
  91. extends:
  92. file: base/docker-compose-base.yaml
  93. service: peer0.org1.example.com
  94. networks:
  95. - byfn
  96.  
  97. peer1.org1.example.com:
  98. container_name: peer1.org1.example.com
  99. extends:
  100. file: base/docker-compose-base.yaml
  101. service: peer1.org1.example.com
  102. networks:
  103. - byfn
  104.  
  105. peer0.org2.example.com:
  106. container_name: peer0.org2.example.com
  107. extends:
  108. file: base/docker-compose-base.yaml
  109. service: peer0.org2.example.com
  110. networks:
  111. - byfn
  112.  
  113. peer1.org2.example.com:
  114. container_name: peer1.org2.example.com
  115. extends:
  116. file: base/docker-compose-base.yaml
  117. service: peer1.org2.example.com
  118. networks:
  119. - byfn
  120.  
  121. cli:
  122. container_name: cli
  123. image: hyperledger/fabric-tools:$IMAGE_TAG
  124. tty: true
  125. stdin_open: true
  126. environment:
  127. - GOPATH=/opt/gopath
  128. - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
  129. - CORE_LOGGING_LEVEL=DEBUG
  130. #- CORE_LOGGING_LEVEL=INFO
  131. - CORE_PEER_ID=cli
  132. - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
  133. - CORE_PEER_LOCALMSPID=Org1MSP
  134. #- CORE_PEER_TLS_ENABLED=true
  135. #- CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
  136. #- CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
  137. #- CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
  138. - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
  139. working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
  140. command: /bin/bash
  141. volumes:
  142. - /var/run/:/host/var/run/
  143. - ./../chaincode/:/opt/gopath/src/github.com/chaincode
  144. - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
  145. - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
  146. - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
  147. depends_on:
  148. - orderer.example.com
  149. - peer0.org1.example.com
  150. - peer1.org1.example.com
  151. - peer0.org2.example.com
  152. - peer1.org2.example.com
  153. networks:
  154. - byfn
  155.  
  156. #Startup dockers
  157. docker-compose -f docker-compose-cli.yaml -f docker-compose-couch.yaml up -d
  158. sleep 30
  159.  
  160. docker exec cli peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx
  161. sleep 10
  162. docker exec cli peer channel join -b mychannel.block
  163.  
  164. docker exec -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp" -e "CORE_PEER_ADDRESS=peer0.org2.example.com:7051" -e "CORE_PEER_LOCALMSPID=Org2MSP" cli peer channel join -b mychannel.block
  165.  
  166. docker exec cli peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx
  167.  
  168. docker exec -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp" -e "CORE_PEER_ADDRESS=peer0.org2.example.com:7051" -e "CORE_PEER_LOCALMSPID=Org2MSP" cli peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx
  169.  
  170. #Install chaincode
  171. docker exec cli peer chaincode install -n fabcar -v 1.0 -p github.com/chaincode/fabcar/go
  172.  
  173. docker exec -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp" -e "CORE_PEER_ADDRESS=peer0.org2.example.com:7051" -e "CORE_PEER_LOCALMSPID=Org2MSP" cli peer chaincode install -n fabcar -v 1.0 -p github.com/chaincode/fabcar/go
  174.  
  175. docker exec cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n fabcar -v 1.0 -c '{"Args":[""]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
  176. sleep 10
  177. docker exec cli peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n fabcar -c '{"function":"initLedger","Args":[""]}'
  178.  
  179. 'use strict';
  180. /*
  181. * Copyright IBM Corp All Rights Reserved
  182. *
  183. * SPDX-License-Identifier: Apache-2.0
  184. */
  185. /*
  186. * Enroll the admin user
  187. */
  188.  
  189. var Fabric_Client = require('fabric-client');
  190. var Fabric_CA_Client = require('fabric-ca-client');
  191.  
  192. var path = require('path');
  193. var util = require('util');
  194. var os = require('os');
  195.  
  196. //
  197. var fabric_client = new Fabric_Client();
  198. var fabric_ca_client = null;
  199. var admin_user = null;
  200. var member_user = null;
  201. var store_path = path.join(__dirname, 'hfc-key-store2');
  202. console.log(' Store path:'+store_path);
  203.  
  204. // create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting
  205. Fabric_Client.newDefaultKeyValueStore({ path: store_path
  206. }).then((state_store) => {
  207. // assign the store to the fabric client
  208. fabric_client.setStateStore(state_store);
  209. var crypto_suite = Fabric_Client.newCryptoSuite();
  210. // use the same location for the state store (where the users' certificate are kept)
  211. // and the crypto store (where the users' keys are kept)
  212. var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});
  213. crypto_suite.setCryptoKeyStore(crypto_store);
  214. fabric_client.setCryptoSuite(crypto_suite);
  215. var tlsOptions = {
  216. trustedRoots: [],
  217. verify: false
  218. };
  219. // be sure to change the http to https when the CA is running TLS enabled
  220. fabric_ca_client = new Fabric_CA_Client('http://localhost:8054', tlsOptions , 'ca-org2', crypto_suite);
  221.  
  222. // first check to see if the admin is already enrolled
  223. return fabric_client.getUserContext('admin', true);
  224. }).then((user_from_store) => {
  225. if (user_from_store && user_from_store.isEnrolled()) {
  226. console.log('Successfully loaded admin from persistence');
  227. admin_user = user_from_store;
  228. return null;
  229. } else {
  230. // need to enroll it with CA server
  231. return fabric_ca_client.enroll({
  232. enrollmentID: 'admin',
  233. enrollmentSecret: 'adminpw'
  234. }).then((enrollment) => {
  235. console.log('Successfully enrolled admin user "admin"');
  236. return fabric_client.createUser(
  237. {username: 'admin',
  238. mspid: 'Org2MSP',
  239. cryptoContent: { privateKeyPEM: enrollment.key.toBytes(), signedCertPEM: enrollment.certificate }
  240. });
  241. }).then((user) => {
  242. admin_user = user;
  243. return fabric_client.setUserContext(admin_user);
  244. }).catch((err) => {
  245. console.error('Failed to enroll and persist admin. Error: ' + err.stack ? err.stack : err);
  246. throw new Error('Failed to enroll admin');
  247. });
  248. }
  249. }).then(() => {
  250. console.log('Assigned the admin user to the fabric client ::' + admin_user.toString());
  251. }).catch((err) => {
  252. console.error('Failed to enroll admin: ' + err);
  253. });
  254.  
  255. 'use strict';
  256. /*
  257. * Copyright IBM Corp All Rights Reserved
  258. *
  259. * SPDX-License-Identifier: Apache-2.0
  260. */
  261. /*
  262. * Register and Enroll a user
  263. */
  264.  
  265. var Fabric_Client = require('fabric-client');
  266. var Fabric_CA_Client = require('fabric-ca-client');
  267.  
  268. var path = require('path');
  269. var util = require('util');
  270. var os = require('os');
  271.  
  272. //
  273. var fabric_client = new Fabric_Client();
  274. var fabric_ca_client = null;
  275. var admin_user = null;
  276. var member_user = null;
  277. var store_path = path.join(__dirname, 'hfc-key-store2');
  278. console.log(' Store path:'+store_path);
  279.  
  280. // create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting
  281. Fabric_Client.newDefaultKeyValueStore({ path: store_path
  282. }).then((state_store) => {
  283. // assign the store to the fabric client
  284. fabric_client.setStateStore(state_store);
  285. var crypto_suite = Fabric_Client.newCryptoSuite();
  286. // use the same location for the state store (where the users' certificate are kept)
  287. // and the crypto store (where the users' keys are kept)
  288. var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});
  289. crypto_suite.setCryptoKeyStore(crypto_store);
  290. fabric_client.setCryptoSuite(crypto_suite);
  291. var tlsOptions = {
  292. trustedRoots: [],
  293. verify: false
  294. };
  295. // be sure to change the http to https when the CA is running TLS enabled
  296. fabric_ca_client = new Fabric_CA_Client('http://localhost:8054', null , '', crypto_suite);
  297.  
  298. // first check to see if the admin is already enrolled
  299. return fabric_client.getUserContext('admin', true);
  300. }).then((user_from_store) => {
  301. if (user_from_store && user_from_store.isEnrolled()) {
  302. console.log('Successfully loaded admin from persistence');
  303. admin_user = user_from_store;
  304. } else {
  305. throw new Error('Failed to get admin.... run enrollAdmin.js');
  306. }
  307.  
  308. // at this point we should have the admin user
  309. // first need to register the user with the CA server
  310. return fabric_ca_client.register({enrollmentID: 'User1', affiliation: 'org2.department1',role: 'client'}, admin_user);
  311. }).then((secret) => {
  312. // next we need to enroll the user with CA server
  313. console.log('Successfully registered user2 - secret:'+ secret);
  314.  
  315. return fabric_ca_client.enroll({enrollmentID: 'User1', enrollmentSecret: secret});
  316. }).then((enrollment) => {
  317. console.log('Successfully enrolled member user "User1" ');
  318. return fabric_client.createUser(
  319. {username: 'User1',
  320. mspid: 'Org2MSP',
  321. cryptoContent: { privateKeyPEM: enrollment.key.toBytes(), signedCertPEM: enrollment.certificate }
  322. });
  323. }).then((user) => {
  324. member_user = user;
  325.  
  326. return fabric_client.setUserContext(member_user);
  327. }).then(()=>{
  328. console.log('User1 was successfully registered and enrolled and is ready to intreact with the fabric network');
  329.  
  330. }).catch((err) => {
  331. console.error('Failed to register: ' + err);
  332. if(err.toString().indexOf('Authorization') > -1) {
  333. console.error('Authorization failures may be caused by having admin credentials from a previous CA instance.n' +
  334. 'Try again after deleting the contents of the store directory '+store_path);
  335. }
  336. });
  337.  
  338. 'use strict';
  339. /*
  340. * Copyright IBM Corp All Rights Reserved
  341. *
  342. * SPDX-License-Identifier: Apache-2.0
  343. */
  344. /*
  345. * Chaincode query
  346. */
  347.  
  348. var Fabric_Client = require('fabric-client');
  349. var path = require('path');
  350. var util = require('util');
  351. var os = require('os');
  352.  
  353. //
  354. var fabric_client = new Fabric_Client();
  355.  
  356. // setup the fabric network
  357. var channel = fabric_client.newChannel('mychannel');
  358. var peer = fabric_client.newPeer('grpc://localhost:9051');
  359. channel.addPeer(peer);
  360.  
  361. //
  362. var member_user = null;
  363. var store_path = path.join(__dirname, 'hfc-key-store2');
  364. console.log('Store path:'+store_path);
  365. var tx_id = null;
  366.  
  367. // create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting
  368. Fabric_Client.newDefaultKeyValueStore({ path: store_path
  369. }).then((state_store) => {
  370. // assign the store to the fabric client
  371. fabric_client.setStateStore(state_store);
  372. var crypto_suite = Fabric_Client.newCryptoSuite();
  373. // use the same location for the state store (where the users' certificate are kept)
  374. // and the crypto store (where the users' keys are kept)
  375. var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path});
  376. crypto_suite.setCryptoKeyStore(crypto_store);
  377. fabric_client.setCryptoSuite(crypto_suite);
  378.  
  379. // get the enrolled user from persistence, this user will sign all requests
  380. return fabric_client.getUserContext('User1', true);
  381. }).then((user_from_store) => {
  382. if (user_from_store && user_from_store.isEnrolled()) {
  383. console.log('Successfully loaded User1 from persistence');
  384. member_user = user_from_store;
  385. } else {
  386. throw new Error('Failed to get User1.... run registerUser.js');
  387. }
  388. console.log('1');
  389. // queryCar chaincode function - requires 1 argument, ex: args: ['CAR4'],
  390. // queryAllCars chaincode function - requires no arguments , ex: args: [''],
  391. const request = {
  392. //targets : --- letting this default to the peers assigned to the channel
  393. chaincodeId: 'fabcar',
  394. fcn: 'queryAllCars',
  395. args: ['']
  396. };
  397. console.log('2');
  398. // send the query proposal to the peer
  399. return channel.queryByChaincode(request);
  400. }).then((query_responses) => {
  401. console.log("Query has completed, checking results");
  402. // query_responses could have more than one results if there multiple peers were used as targets
  403. if (query_responses && query_responses.length == 1) {
  404. if (query_responses[0] instanceof Error) {
  405. console.error("error from query = ", query_responses[0]);
  406. } else {
  407. console.log("Response is ", query_responses[0].toString());
  408. }
  409. } else {
  410. console.log("No payloads were returned from query");
  411. }
  412. }).catch((err) => {
  413. console.log('3');
  414. console.error('Failed to query successfully :: ' + err);
  415. });
Add Comment
Please, Sign In to add comment