You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Hyperledger Fabric:如何将channel.tx制品转换为JSON文件?

解决Hyperledger Fabric中channel.tx转JSON失败的问题

嘿,我明白你现在的困扰——能搞定genesis.pb转JSON,却卡在channel.tx上对吧?问题出在两者的结构差异上:genesis.pb的结构相对简单,而channel.tx是一个完整的交易信封(Envelope),包含多层嵌套结构,直接用common.Message类型解码肯定行不通。

下面是咱们一步步来的正确解码流程,都是在Fabric CLI里就能执行的:

第一步:先把channel.tx解码成Envelope类型的JSON

首先得把原始的channel.tx文件解码为最外层的common.Envelope结构,命令是:

configtxlator proto_decode --input channel.tx --type common.Envelope > channel_envelope.json

第二步:提取Payload里的ConfigUpdateEnvelope内容

打开刚生成的channel_envelope.json,找到payload -> data字段的值(这是一段Base64编码的内容),把它复制出来存成一个新文件,比如config_update_env.base64

如果你的CLI里装了jq工具(很多Fabric环境自带),可以直接用命令提取,省得手动复制:

jq -r '.payload.data' channel_envelope.json > config_update_env.base64

第三步:解码ConfigUpdateEnvelope为可读JSON

把Base64内容转成二进制,再用configtxlator解码成JSON:

base64 -d config_update_env.base64 > config_update_env.bin
configtxlator proto_decode --input config_update_env.bin --type common.ConfigUpdateEnvelope > channel_config.json

到这一步,你就能得到channel.tx对应的JSON文件了。如果还想深挖里面的具体配置更新细节,可以继续第四步:

第四步(可选):提取具体的ConfigUpdate内容

如果要查看最核心的配置更新项,继续提取config_update字段的Base64内容,重复类似步骤:

jq -r '.config_update' channel_config.json > config_update.base64
base64 -d config_update.base64 > config_update.bin
configtxlator proto_decode --input config_update.bin --type common.ConfigUpdate > config_update_details.json

为啥你的原始命令没用?

简单说,genesis.pb本质是common.Config类型,有时候用common.Message能蒙对是因为它的封装层级少,但channel.tx是标准的交易信封,包含签名、Payload等多层嵌套,必须按层级解码才能拿到正确的可读内容。

要是你嫌分步麻烦,也可以用一条命令链搞定(前提是CLI支持管道和jq):

configtxlator proto_decode --input channel.tx --type common.Envelope | jq -r '.payload.data' | base64 -d | configtxlator proto_decode --type common.ConfigUpdateEnvelope > channel_final.json

内容的提问来源于stack exchange,提问作者Thinker

火山引擎 最新活动