注册管理员账号

在上一节中执行query和invote前,必须要先注册管理员,因此本节我们重点来讲解下注册管理员的代码

打开$GOPATH/src/github.com/hyperledger/fabric-samples/fabcar/javascript/enrollAdmin.js 文件,我们来看看代码。

开启javascript的严格模式,目的是指定代码在严格条件下执行。严格模式下你不能使用未声明的变量

'use strict';

加载fabric证书服务

const FabricCAServices = require('fabric-ca-client');

加载fabric的network网络服务

const { Wallets } = require('fabric-network');

加载fs文件系统模块,实现对文件或目录的创建,修改和删除等操作

const fs = require('fs');

加载path模块,它提供了一些用于处理文件路径的小工具

const path = require('path');

定义一个异步主函数,异步函数是指通过事件循环异步执行的函数,它会通过一个隐式的 Promise 返回其结果

async function main() {
    try {
        ...
    } catch (error) {
        console.error(`Failed to enroll admin user "admin": ${error}`);
        process.exit(1);
    }
}

接下来我们看看try部分里面的业务代码

第一部分:加载test-network网络中组织1的的连接配置信息

path.resolve的可理解为是当前目录执行一个cd操作,从左到右执行,返回的是最后的当前目录
如下面的语句就是:

cd __dirname  //返回:$GOPATH/src/github.com/hyperledger/fabric-samples/fabcar/javascript
cd ..  //返回:$GOPATH/src/github.com/hyperledger/fabric-samples/fabcar
cd ..  //返回:$GOPATH/src/github.com/hyperledger/fabric-samples
cd test-network  //返回:$GOPATH/src/github.com/hyperledger/fabric-samples/test-network

其它的类推,最终得到的ccpPath的值是:
$GOPATH/src/github.com/hyperledger/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/connection-org1.json

const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json');

获取到connection-org1.json的参数并转为json格式,方便后面使用,这个文件的参数会在最后说明

const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));

第二部分:创建一个新的CA客户端以与CA服务进行交互.

先获取配置文件中的ca.org1.example.com选项的参数,caInfo得到的值为:
{"url": "https://localhost:7054","caName": "ca-org1","tlsCACerts": {"pem": "内容太多,这是省略..."},"httpOptions": {"verify": false}}

const caInfo = ccp.certificateAuthorities['ca.org1.example.com'];

获取配置文件中ca公钥

const caTLSCACerts = caInfo.tlsCACerts.pem;

实例化一个CA服务

const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);

第三部分:创建一个新的基于文件系统的钱包来管理身份。

生成钱包的路径。

process.cwd() 是获取当前node命令执行时所在的文件夹目录,与上面的__dirname不同,它指被执行js文件所在的文件夹目录。当然我们这次就是在enrollAdmin.js根目录下执行的,两者得到的值是一样的。

path.join是将路径片段使用特定的分隔符(window:\)连接起来形成路径,并规范化生成的路径。

最终walletPath的值是:$GOPATH/src/github.com/hyperledger/fabric-samples/fabcar/javascript/wallet

const walletPath = path.join(process.cwd(), 'wallet');

根据钱包路径生成钱包对应的目录

const wallet = await Wallets.newFileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);

如下图我们看到它已经生成了wallet这个目录

第四部分:查看我们是否已经注册了管理员用户

如果wallet目录下已经存在admin.id这个文件就表示已经注册过

const identity = await wallet.get('admin');
if (identity) {
      console.log('An identity for the admin user "admin" already exists in the wallet');
      return;
}

由此我们知道,注册管理员只需要一次,后面不再需要重复注册,如果要重新注册,需要先删除admin.id这个文件才行。

第五部分:注册管理员用户,并将新身份导入钱包

注册账号名为admin,密码为adminpw的管理员账号

const enrollment = await ca.enroll({ enrollmentID: 'admin', enrollmentSecret: 'adminpw' });

定义证书的参数,credentials是证书信息,mspId 组织ID,type要使用的证书类型

        const x509Identity = {
            credentials: {
                certificate: enrollment.certificate,
                privateKey: enrollment.key.toBytes(),
            },
            mspId: 'Org1MSP',
            type: 'X.509',
        };

通过同步的方式把admin管理员账号注册好并写入到钱包目录中

        await wallet.put('admin', x509Identity);
        console.log('Successfully enrolled admin user "admin" and imported it into the wallet');

// 执行主函数

main();

本文由小韦云原创,转载请注明出处:https://www.bctos.cn/doc/4/1831,否则追究其法律责任

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章