小编给大家分享一下Hyperledger Fabric中register和reroll一个user的过程分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
“专业、务实、高效、创新、把客户的事当成自己的事”是我们每一个人一直以来坚持追求的企业文化。 创新互联公司是您可以信赖的网站建设服务商、专业的互联网服务提供商! 专注于成都做网站、网站建设、软件开发、设计服务业务。我们始终坚持以客户需求为导向,结合用户体验与视觉传达,提供有针对性的项目解决方案,提供专业性的建议,创新互联建站将不断地超越自我,追逐市场,引领市场!
这一步骤介绍register & reroll 一个user的过程, 这一部需要使用上一步注册的admin用户登录, 然后注册一个普通的user, user 的role指定为client. 全部代码如下:
'use strict'; /* * Copyright IBM Corp All Rights Reserved * * SPDX-License-Identifier: Apache-2.0 */ /* * Register and Enroll a user */ var Fabric_Client = require('fabric-client'); var Fabric_CA_Client = require('fabric-ca-client'); var path = require('path'); var util = require('util'); var os = require('os'); // var fabric_client = new Fabric_Client(); var fabric_ca_client = null; var admin_user = null; var member_user = null; var store_path = path.join(__dirname, 'hfc-key-store'); console.log(' Store path:'+store_path); // create the key value store as defined in the fabric-client/config/default.json 'key-value-store' setting Fabric_Client.newDefaultKeyValueStore({ path: store_path }).then((state_store) => { // assign the store to the fabric client fabric_client.setStateStore(state_store); var crypto_suite = Fabric_Client.newCryptoSuite(); // use the same location for the state store (where the users' certificate are kept) // and the crypto store (where the users' keys are kept) var crypto_store = Fabric_Client.newCryptoKeyStore({path: store_path}); crypto_suite.setCryptoKeyStore(crypto_store); fabric_client.setCryptoSuite(crypto_suite); var tlsOptions = { trustedRoots: [], verify: false }; // be sure to change the http to https when the CA is running TLS enabled fabric_ca_client = new Fabric_CA_Client('http://localhost:7054', null , '', crypto_suite); // first check to see if the admin is already enrolled return fabric_client.getUserContext('admin', true); }).then((user_from_store) => { if (user_from_store && user_from_store.isEnrolled()) { console.log('Successfully loaded admin from persistence'); admin_user = user_from_store; } else { throw new Error('Failed to get admin.... run enrollAdmin.js'); } // at this point we should have the admin user // first need to register the user with the CA server return fabric_ca_client.register({enrollmentID: 'user1', affiliation: 'org1.department1',role: 'client'}, admin_user); }).then((secret) => { // next we need to enroll the user with CA server console.log('Successfully registered user1 - secret:'+ secret); return fabric_ca_client.enroll({enrollmentID: 'user1', enrollmentSecret: secret}); }).then((enrollment) => { console.log('Successfully enrolled member user "user1" '); return fabric_client.createUser( {username: 'user1', mspid: 'Org1MSP', cryptoContent: { privateKeyPEM: enrollment.key.toBytes(), signedCertPEM: enrollment.certificate } }); }).then((user) => { member_user = user; return fabric_client.setUserContext(member_user); }).then(()=>{ console.log('User1 was successfully registered and enrolled and is ready to interact with the fabric network'); }).catch((err) => { console.error('Failed to register: ' + err); if(err.toString().indexOf('Authorization') > -1) { console.error('Authorization failures may be caused by having admin credentials from a previous CA instance.\n' + 'Try again after deleting the contents of the store directory '+store_path); } });
以上是“Hyperledger Fabric中register和reroll一个user的过程分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!