Fabric Node SDK中CouchDB钱包怎么用

发布时间:2021-12-28 17:23:36 作者:小新
来源:亿速云 阅读:95

这篇文章将为大家详细讲解有关Fabric Node SDK中CouchDB钱包怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

1、Hyperledger Fabric钱包的类型

有三种类型的钱包:文件系统钱包、内存钱包和CouchDB钱包。

文件系统钱包

文件系统钱包就是一个简单的文件夹,在大多数情况下这是不错的默认选择。在fabric-samples/balance-transfer示例中,使用的就是文件系统钱包。当你运行这个示例代码时,它就会创建一个fabric-client-kv-orgName文件夹并在其中保存所有需要的Fabric身份资料。该配置定义可以参考orgname.yaml

内存钱包

顾名思义,内存钱包就是暂存在应用内存中的钱包。当运行在受限环境中或不需要访问文件系统时可以使用这种钱包,例如在浏览器中。需要提醒的是,这种钱包中的身份资料在应用终止后就没有了。内存钱包的具体文档可以查看这里。

CouchDB钱包

也就是使用CouchDB保存身份资料,在生产环境中这是最常见的选择。

2、Hyperledger Fabric CouchDB钱包

钱包使用两个库来保存证书和私钥:

状态库

状态库用于保存已登记身份的证书,它存储的是一个身份的基本信息:

{
  "name": "test",
  "mspid": "org1",
  "roles": null,
  "affiliation": "",
  "enrollmentSecret": "<ENROLLMENT_SECRET>",
  "enrollment": {
    "signingIdentity": "<PRIVATE_KEY_NAME>",
    "identity": {
      "certificate": "<SIGN_CERT>"
    }
  }
}

注意:signingIdentity指向私钥和公钥在密码学资料库中的保存地址。

密码学资料库

密码学资料库用于保存身份的私钥和公钥。

3、在Hyperledger Fabric应用中启用CouchDB钱包

首先引入Fabric Node SDK提供的CouchDBKeyValueStore库:

const CDBKVS = require("fabric-client/lib/impl/CouchDBKeyValueStore.js");

然后设置状态库:

let stateStore = await new CDBKVS({
  url: "https://<USERNAME>:<PASSWORD>@<URL>",
  name: "<DB_NAME>"
});

const Client = require("fabric-client");

const client = Client.loadFromConfig("path of network.yaml");

client.setStateStore(stateStore);

其中:

最后设置密码学资料库:

const cryptoSuite = Client.newCryptoSuite();

let cryptoKS = Client.newCryptoKeyStore(CDBKVS, {
  url: "https://<USERNAME>:<PASSWORD>@<URL>",
  name: "<DB_NAME>"
});

cryptoSuite.setCryptoKeyStore(cryptoKS);

client.setCryptoSuite(cryptoSuite);

4、在Balance Transfer示例中使用CouchDB钱包

Balance Transfer是Hyperledger Fabric官方提供的一个示例代码。

首先按照示例说明启动balance transfer网络,该网络包含:

启动couchdb的docker镜像:

~$ docker run --name couch-userdb -e COUCHDB_USER=admin \
                       -e COUCHDB_PASSWORD=password -p 5984:5984 -d couchdb

上面的命令会自动拉取docker hub上的couchdb镜像。

其中CouchDB详情如下:

CouchDB连接URL如下:

https://<USERNAME>:<PASSWORD>@<URL> https://admin:password@localhost:5984

然后更新balance-transfer示例中的client配置:打开文件/helper.js,更新其中的getClientForOrg函数:

'use strict';
var log4js = require('log4js');
var logger = log4js.getLogger('Helper');
logger.setLevel('DEBUG');

var path = require('path');
var util = require('util');

var hfc = require('fabric-client');
hfc.setLogger(logger);

// couchDB config 
const CDBKVS = require("fabric-client/lib/impl/CouchDBKeyValueStore.js");

async function getClientForOrg(userorg, username) {
    logger.debug('getClientForOrg - ****** START %s %s', userorg, username)
    // get a fabric client loaded with a connection profile for this org
    let config = '-connection-profile-path';

    // build a client context and load it with a connection profile
    // lets only load the network settings and save the client for later
    let client = hfc.loadFromConfig(hfc.getConfigSetting('network' + config));

    // This will load a connection profile over the top of the current one one
    // since the first one did not have a client section and the following one does
    // nothing will actually be replaced.
    // This will also set an admin identity because the organization defined in the
    // client section has one defined
    client.loadFromConfig(hfc.getConfigSetting(userorg + config));
    
    //********************** CouchDB configuration **************************************
    // set the state store
    let stateStore = await new CDBKVS({
        url: "https://<USERNAME>:<PASSWORD>@<URL>",
        name: "<DB_NAME>"
    });

    client.setStateStore(stateStore);

    // set the cryto store
    const cryptoSuite = hfc.newCryptoSuite();

    let cryptoKS = hfc.newCryptoKeyStore(CDBKVS, {
        url: "https://<USERNAME>:<PASSWORD>@<URL>",
        name: "<DB_NAME>"
    });

    cryptoSuite.setCryptoKeyStore(cryptoKS);

    client.setCryptoSuite(cryptoSuite);
    
    //********************** CouchDB configuration END **************************************
    
    if (username) {
        let user = await client.getUserContext(username, true);
        if (!user) {
            throw new Error(util.format('User was not found :', username));
        } else {
            logger.debug('User %s was found to be registered and enrolled', username);
        }
    }
    logger.debug('getClientForOrg - ****** END %s %s \n\n', userorg, username)

    return client;
}

我们的修改如下:

在上面的代码中还有一个小改动:

// Client variable is used as hfc
var hfc = require("fabric-client");

// Instead of Client
const Client = require("fabric-client");

然后在balance transfer中注册一个新的用户,一旦注册成功,就可以使用couchdb的api查看状态库和密码学资料库了。

例如,可以使用下面的参数注册一个用户。对于org1,我们使用同一个库org1db用于状态库和密码学资料库:

打开浏览器访问http://localhost:5369/org1db/_all_docs,可以看到 在org1db中保存的所有文档。

Fabric Node SDK中CouchDB钱包怎么用

其中0#、1#、2#是admin的证书。

3#是alice保存在状态库中的证书

4#和5#是alice保存在密码学资料库中的公钥和私钥

访问http://localhost:5369/org1db/alice,可以看到alice在状态库中的全部细节:

Fabric Node SDK中CouchDB钱包怎么用

查看signingIdentity

"signingIdentity":"d37a97a8c2377c21537801ec1a929d81905ae57963a2f6c8ba0308931a7fc791"

现在查看上图中的4#和5#,可以看到是一样的。

可能你还记得,signingIdentity字段是指向保存在密码学资料库中的私钥和公钥的。

关于“Fabric Node SDK中CouchDB钱包怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. Hyperledger Fabric启用CouchDB为状态数据库
  2. Hyperledger Fabric初识与环境搭建

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

fabric couchdb

上一篇:Fabric2.0如何创建通道

下一篇:Fabric链码开发的原则有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》