javascript - 编码为十六进制字符串:参数必须是字符串 node.js
问题描述
我正在尝试在超级账本锯齿中实现一个简单的交易流程,为了创建交易,它必须通过一些步骤
/*
* Create the transactions
*/
const createTransaction = function createTransaction(transactionHeaderBytes, payloadBytes) {
const signature = signer.sign(transactionHeaderBytes)
console.log(signature);
return transaction = protobuf.Transaction.create({
header: transactionHeaderBytes,
headerSignature:Buffer.from(signature, "hex"),
payload: payloadBytes
});
}
我需要编码headerSignature
为十六进制字符串,但出现以下错误
Argument must be a string
但是console.log(signature);
给出了以下结果a51d254f0c27f15abb016030eeb9e38b5ee06ee13d28d88ac5f5cc13a2520b42088090a1d1d19d321098996dc980b3f94cfc84ba0399a73ba7cd9ddc9b2a453d
更新
错误日志
TypeError: Argument must be a string
at Op.writeStringBuffer [as fn] (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/protobufjs/src/writer_buffer.js:61:13)
at BufferWriter.finish (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/protobufjs/src/writer.js:449:14)
at Object.createBatchHeader (/var/accubits-workspace/hypeerledger-sawtooth/tuts/helpers/private-key.js:82:8)
at app.get (/var/accubits-workspace/hypeerledger-sawtooth/tuts/index.js:24:32)
at Layer.handle [as handle_request] (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/layer.js:95:5)
at next (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/layer.js:95:5)
at /var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/var/accubits-workspace/hypeerledger-sawtooth/tuts/node_modules/express/lib/router/index.js:335:12)
解决方案
错误不在Buffer.from
但在protobuf.Transaction.create
headerSignature
需要是 a string
,而你正在通过 aBuffer
根据文档,它应该是这样的:
const signature = signer.sign(transactionHeaderBytes)
const transaction = protobuf.Transaction.create({
header: transactionHeaderBytes,
headerSignature: signature,
payload: payloadBytes
})
推荐阅读
- python - 如何获取不包括特定 RGB 值的区域的边界框
- python-3.x - Seaborn中分类xaxis的更改顺序
- apache - 如何阻止垃圾邮件机器人使用 htaccess 访问网站?
- excel - 获取每月更改位置的多个单元格值
- openshift - 什么组件正在解析 OpenShift 路由器中的域
- python - 修复 INIT AND QUIT 的 no 模块后,我的 Vs pygame 仍然不显示突出显示
- python - Dask - 新集群创建失败,“dask”用户欠 HDFS 文件
- python - 我如何理解这个 Python “def”代码?
- compiler-errors - OpenBugs:扫描了无效或意外的令牌
- mysql - mamp phpmyadmin 错误脚本导入数据库