动机
使用node-sdk进行了简单交易,有一些问题没有解决。
- cryptogen模块生成的证书如何给node-sdk进行使用。
- node-sdk中的主要模块包含哪些可用函数,可以完成哪些工作。
架构设计
sdk在fabric中扮演的角色:
- 通过节点的gRPC协议访问安装在peer节点上的链码
- 将peer返回的背书,封装成交易,发送给orderer,作排序
- 分为以下三大模块:
- fabric-ca-client
- fabric-client
- fabric-network
fabric-ca-client
与fabric-ca 模块交互,主要负责证书管理,如向ca申请证书 。
1 | // 镜像 |
fabric-client
负责与fabric网络交互,如发送提案,发送交易监听区块等,试用场景偏向与特殊业务定制。
1 | // 创建身份标识 |
fabric-network
负责与fabric网络交互,如发送交易,监听区块等,是fabric-client的封装,适用场景偏向普通业务直接集成。
1 | // 导入相关对象 |
connection-org1.json*
连接文件,内容如下:
1 | { |
wallet*
目录结构:
admin是客户端从ca处申请的证书。
User1@org1.example.com是cryptogen生成证书。
.
├── admin
│ ├── 6efed24e38799a56532210c985bf9ac7f0367bdf2c094529408ba06bb8ad3824-priv //私钥
│ ├── 6efed24e38799a56532210c985bf9ac7f0367bdf2c094529408ba06bb8ad3824-pub //公钥
│ └── admin // 用户相关信息
└── User1@org1.example.com
├── msp
│ ├── admincerts // 组织管理员证书
│ │ └── User1@org1.example.com-cert.pem
│ ├── cacerts // 组织根证书
│ │ └── ca.org1.example.com-cert.pem
│ ├── keystore // 私钥
│ │ └── 7365294a1793d88ab82ce6d825bb5fd8e828deed402e25e37f0e2999e9d68278_sk
│ ├── signcerts // 证书
│ │ └── User1@org1.example.com-cert.pem
│ └── tlscacerts // TLS通信证书(客户端使用)
│ └── tlsca.org1.example.com-cert.pem
└── tls
├── ca.crt // 组织根证书
├── client.crt // 证书
└── client.key // 私钥admin
1 {"name":"admin","mspid":"OwnMSP","roles":null,"affiliation":"","enrollmentSecret":"","enrollment":{"signingIdentity":"6efed24e38799a56532210c985bf9ac7f0367bdf2c094529408ba06bb8ad3824","identity":{"certificate":"-----BEGIN CERTIFICATE-----\nMIICADCCAaagAwIBAgIUcG/93ySJHKn3WeNLxAPZ5R6RZo0wCgYIKoZIzj0EAwIw\ncTELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNh\nbiBGcmFuY2lzY28xGDAWBgNVBAoTD293bi5leGFtcGxlLmNvbTEbMBkGA1UEAxMS\nY2Eub3duLmV4YW1wbGUuY29tMB4XDTIwMDExMjA3MDkwMFoXDTIxMDExMTA3MTQw\nMFowITEPMA0GA1UECxMGY2xpZW50MQ4wDAYDVQQDEwVhZG1pbjBZMBMGByqGSM49\nAgEGCCqGSM49AwEHA0IABAJsM0RFBuRTCE63qdnIqAxloJJCzpGtYWX8OZQ/e0RL\nPcwVYm3g5EaPfO6ibVyO02wqTSMkWhZmEq5YsirV8PSjbDBqMA4GA1UdDwEB/wQE\nAwIHgDAMBgNVHRMBAf8EAjAAMB0GA1UdDgQWBBTK3G8uVQqxI5E+65v3RnXkWKBN\nxTArBgNVHSMEJDAigCChgxTzM7uvN9514LK8agb2vMflWvGE9QuHT72cM6bUdDAK\nBggqhkjOPQQDAgNIADBFAiEAi1o7+Zfj0jYl/OcI+Js9fjfcxlDDiQDrLYWvYgWQ\ntagCIBv1goo/6IYHhN4xL+Tn66qbwqaDljAkCrhm5WNdihhT\n-----END CERTIFICATE-----\n"}}}lable:admin。
MSPID:OwnMSP。
roles:角色。
affiliation:从属关系,指组织中的部门。
enrollmentSecret:登记密码。
enrollment:登记信息。
signingIdentity:签名身份 。
certificate:证书。
相关实验
cryptogen模块改造为fabric-network可以使用的格式
cryptogen模块生成的证书无法被fabric-network直接使用。下面是处理代码。
1 | // 导入相关类 |
查块命令
fabric-network中给的示例代码并没有给查块的示例代码。
1 | // 创建网关对象 |
代码和示例
1 | const { FileSystemWallet, Gateway } = require('fabric-network'); |
结果
1 | ########### getChannelPeers() ############ |
queryBlock(blockNumber, target, useAdmin, skipDecode)
Queries the ledger on the target peer for Block by block number.
Parameters:
| Name | Type | Description |
|---|---|---|
blockNumber |
number | The number of the Block in question. |
target |
Peer | Optional. The peer to send this query to. If no target is passed, the query is sent to the first peer that was added to the channel object. |
useAdmin |
boolean | Optional. Indicates that the admin credentials should be used in making this call to the peer. |
skipDecode |
boolean | Optional. If true, this function returns an encoded block. |
Returns:
A Promise for a Block at the blockNumber slot in the ledger, fully decoded into an object.
Type
Promise
queryBlockByHash(blockHash, target, useAdmin, skipDecode)
Queries the ledger on the target peer for a Block by block hash.
Parameters:
| Name | Type | Description |
|---|---|---|
blockHash |
Array. |
of the Block in question. |
target |
Peer | Optional. The peer to send the query to. If no target is passed, the query is sent to the first peer that was added to the channel object. |
useAdmin |
boolean | Optional. Indicates that the admin credentials should be used in making this call to the peer. |
skipDecode |
boolean | Optional. If true, this function returns an encoded block. |
Returns:
A Promise for a Block matching the hash, fully decoded into an object.
Type
Promise
queryBlockByTxID(tx_id, target, useAdmin, skipDecode)
Queries the ledger on the target peer for a Block TransactionID.
Parameters:
| Name | Type | Description |
|---|---|---|
tx_id |
string | The TransactionID of the Block in question. |
target |
Peer | Optional. The peer to send the query to. If no target is passed, the query is sent to the first peer that was added to the channel object. |
useAdmin |
boolean | Optional. Indicates that the admin credentials should be used in making this call to the peer. |
skipDecode |
boolean | Optional. If true, this function returns an encoded block. |
Returns:
A Promise for a Block matching the tx_id, fully decoded into an object.
Type
Promise
官网
https://hyperledger.github.io/fabric-sdk-node/release-1.4/Channel.html