Fabric SDK
- 与Fabric网络交互
- fabric-client:已淘汰,v1.4中使用它开发的客户端仍可与 v2.x 进行交互但无法使用 v2.x的新功能
- fabric-network:推荐使用
- 管理和治理
- fabric-client 中的 tx api 由 fabric-common 取代
- fabric-client 中的链码管理 api 由fabric-admin 取代
jwt: https://jwt.io/
header+payload+signature
本文中的代码参见:https://github.com/alanhou/blockchain/tree/main/fabric-network
api-v1.4
1 2 |
cd api-v1.4 npm install |
注册用户
通过向 http://localhost:4000/users 提交 post 请求即可注册用户(首次注册会默认注册一个 admin),并在crypto和fabric-client-kv-xxx 中生成相应的文件:
此时获取的 token 用作稍后请求时携带的 JWT 参数
交易调用
示例:POST http://192.168.xx.xxx:4000/channels/mychannel/chaincodes/fabcar
BODY(通过修改参数中最后一个字段即相关将所有权转让)
1 2 3 4 5 6 7 |
{ "fcn": "createCar", "peers": ["peer0.org1.alanhou.org", "peer0.org2.alanhou.org"], "chaincodeName": "fabcar", "channelName": "mychannel", "args": ["SuperCar", "Tesla", "F1-Reloaded", "White", "John"] } |
RESPONSE
1 2 3 4 5 6 7 |
{ "result": { "tx_id": "8a0f304a92f81c3411c1ae5019982f4e79a318a4d4efe788277e156fb1590882" }, "error": null, "errorData": null } |
根据 id 查询内容
RESPONSE
1 2 3 4 5 6 |
{ "make": "Tesla", "model": "F1-Reloaded", "colour": "White", "owner": "John" } |
交易更新操作
示例:POST http://192.168.xx.xxx:4000/channels/mychannel/chaincodes/fabcar
BODY
1 2 3 4 5 6 7 |
{ "fcn": "changeCarOwner", "peers": ["peer0.org1.alanhou.org", "peer0.org2.alanhou.org"], "chaincodeName": "fabcar", "channelName": "mychannel", "args": ["SuperCar", "Jasmine"] } |
RESPONSE
1 2 3 4 5 6 7 |
{ "result": { "tx_id": "921c7a68cb2355b5c30e3d22f27ab9d51c0a2dadbeae28225f94b3e624995f8e" }, "error": null, "errorData": null } |
根据交易id 查询交易内容
RESPONSE
根据区块编号查询区块内容
示例: GET http://192.168.xx.xxx:4000/channels/mychannel/blocks/6?peer=peer0.org1.alanhou.org
RESPONSE
查询资产历史交易
RESPONSE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[ { "TxId": "5abf48773fbcc6a4d148ab71b326234041636cebfe2b9de1e2ad20717bf51b2f", "Value": { "make": "Tesla", "model": "F1-Reloaded", "colour": "White", "owner": "Jasmine" }, "Timestamp": "2021-05-10 13:07:00.941 +0000 UTC", "IsDelete": "false" }, { "TxId": "9761cb2c6648ce8557cfae3db840870e2dddae6399e6a62e06447d7e5cee9a54", "Value": { "make": "Tesla", "model": "F1-Reloaded", "colour": "White", "owner": "John" }, "Timestamp": "2021-05-10 13:03:33.06 +0000 UTC", "IsDelete": "false" } ] |
Composite Key
首先使用同一个人多次调用 createCar(之后再调用 changeCarOwner 不受影响)
RESPONSE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[ { "make": "Tesla", "model": "F1-Reloaded", "colour": "White", "owner": "Jasmine" }, { "colour": "White", "make": "Tesla", "model": "F1-Reloaded", "owner": "John" }, { "colour": "White", "make": "Tesla", "model": "F1-Reloaded", "owner": "John" } ] |
区块链浏览器
1 2 |
cd explorer docker-compose up -d |