Hyperledger Fabric学习笔记——Wallet

wallet包含一组用户身份,用户运行的应用程序在连接到通道时会选择这些身份之一,结合MSP使用此身份确定对诸如ledger之类的通道资源的访问权限。

1、类型

wallet存储有三种不同的形式:

  • File system:最普通的,最好理解的方式,是很好的默认选择
  • In-memory:当饮用程序在受限环境中运行而无法访问文件系统时,使用这种wallet,通常是网络浏览器。这种wallet是不稳定的,应用程序正常结束或者崩溃后,身份将丢失
  • CouchDB:这是用的最少的形式,但是对于那些想要使用数据库备份和还原机制的用户,CouchDBwallet可以提供简化灾难恢复的选择

使用Wallets类提供的factory funciton来创建wallets。

2、结构

如下图所示,一个wallet可以保存多个身份,每个身份由特定的ca颁发,每个身份具有一个标准的结构,包括描述性标签、包含公钥的X.509证书、私钥和某些特定于Fabric的元数据。不同的wallet类型将此结构适当地映射到其存储机制。

有一些关键的类方法可以简化管理wallets和身份:

  1.  
    const identity: X509Identity = {
  2.  
    credentials: {
  3.  
    certificate: certificatePEM,
  4.  
    privateKey: privateKeyPEM,
  5.  
    },
  6.  
    mspId: 'Org1MSP',
  7.  
    type: 'X.509',
  8.  
    };
  9.  
    await wallet.put(identityLabel, identity);

上面建立了一个包含元数据Org1MSP、X.509证书和私钥的身份,然后将这个身份加入到wallet中。

网关类只需要通过mspId和type元数据来设定一个身份——上面例子中的Org1MSP和X.509。网关当前使用MSPID值来表示特定的peers(connection profile中),如下面所示:

  1.  
    organizations:
  2.  
    Org1:
  3.  
    mspid: Org1MSP
  4.  
     
  5.  
    peers:
  6.  
    - peer0.org1.example.com

3、操作方式

  1.  
    const wallet = await Wallets.newFileSystemWallet('../identity/user/isabella/wallet');
  2.  
     
  3.  
    const cert = fs.readFileSync(path.join(credPath, '.../User1@org1.example.com-cert.pem')).toString();
  4.  
    const key = fs.readFileSync(path.join(credPath, '.../_sk')).toString();
  5.  
     
  6.  
    const identityLabel = 'User1@org1.example.com';
  7.  
    const identity = {
  8.  
    credentials: {
  9.  
    certificate: cert,
  10.  
    privateKey: key,
  11.  
    },
  12.  
    mspId: 'Org1MSP',
  13.  
    type: 'X.509',
  14.  
    };
  15.  
     
  16.  
    await wallet.put(identityLabel, identity);
  • 程序第一次运行时,将会在本地文件系统创建一个wallet
  • 从文件系统导入cert和key
  • 通过cert、key和Org1MSP建立一个新的X.509身份
  • 将身份通过描述性标签加入wallet中

https://blog.csdn.net/Nemoosi/article/details/104796585

原文地址:https://www.cnblogs.com/yuluoxingkong/p/13558542.html