Hyperledger Fabric API返回一个缓冲区类型的数据,如何在javascript中将缓冲区转换为该数据的



我正在超级账本结构(node.js SDK(上构建区块链。

https://hyperledger.github.io/fabric-sdk-node/release-1.4/global.html#BlockchainInfo__anchor

我调用apiBlockchainInfo并获得json格式响应,其中包括currentBlockHash

官方文档显示currentBlockHash的响应数据类型Array<byte>

例如以下数据。

我想把这个缓冲区转换成字符串,但不知道该怎么办。

谢谢你阅读这个问题。

{ buffer:
{ type: 'Buffer',
data:
[ 8,
207,
230,
17,
18,
32,
124,
143,
73,
40,
171,
42,
251,
237,
193,
138,
36,
92,
58,
57,
254,
56,
144,
96,
54,
201,
242,
64,
10,
111,
150,
28,
198,
187,
196,
118,
97,
160,
26,
32,
16,
160,
154,
19,
11,
179,
147,
11,
38,
16,
150,
190,
126,
17,
121,
123,
200,
7,
71,
27,
241,
103,
54,
188,
196,
248,
178,
88,
48,
115,
186,
133 ] },
offset: 6,
markedOffset: -1,
limit: 38,
littleEndian: true,
noAssert: false }

这是始发地应答数据

{"height":{"low":291663,"high":0,"unsigned":true},"currentBlockHash":{"buffer":{"type":"Buffer","data":[8,207,230,17,18,32,124,143,73,40,171,42,251,237,193,138,36,92,58,57,254,56,144,96,54,201,242,64,10,111,150,28,198,187,196,118,97,160,26,32,16,160,154,19,11,179,147,11,38,16,150,190,126,17,121,123,200,7,71,27,241,103,54,188,196,248,178,88,48,115,186,133]},"offset":6,"markedOffset":-1,"limit":38,"littleEndian":true,"noAssert":false},"previousBlockHash":{"buffer":{"type":"Buffer","data":[8,207,230,17,18,32,124,143,73,40,171,42,251,237,193,138,36,92,58,57,254,56,144,96,54,201,242,64,10,111,150,28,198,187,196,118,97,160,26,32,16,160,154,19,11,179,147,11,38,16,150,190,126,17,121,123,200,7,71,27,241,103,54,188,196,248,178,88,48,115,186,133]},"offset":40,"markedOffset":-1,"limit":72,"littleEndian":true,"noAssert":false}}

我认为JSDoc在这种情况下具有误导性。我认为currentBlockHash实际上是一个Node.js缓冲区(或者可能是一个模仿缓冲区行为的protobuf实现(。您可以通过调用currentBlockHash.toString()将缓冲区转换为字符串。

我观察到一些情况,在protobufBuffer实现上调用toString((会给出一个调试字符串,而不是缓冲区内容的字符串表示,因此为了安全起见,我倾向于指定一个编码参数。

在散列的情况下,将其视为十六进制字符串可能更方便,而不是utf8(这将是默认值(,所以我会尝试currentBlockHash.toString('hex')

有关缓冲区的更多信息,请参阅此处:https://nodejs.org/docs/latest-v10.x/api/buffer.html

相关内容

  • 没有找到相关文章

最新更新