首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@ethereumjs/tx: toBuffer只支持0x前缀十六进制字符串

@ethereumjs/tx: toBuffer只支持0x前缀十六进制字符串
EN

Ethereum用户
提问于 2023-03-28 17:25:56
回答 1查看 56关注 0票数 0

注:我见过这个堆栈交换问题,它的解决方案不起作用。

我试图在Goerli上部署一份合同,但得到了以下错误:

代码语言:javascript
复制
throw new Error(`Cannot convert string to buffer. toBuffer 
only supports 0x-prefixed hex strings and this string was 
given: ${v}`);

Error: Cannot convert string to buffer. toBuffer only 
supports 0x-prefixed hex strings and this string was given: 
0x60806...
000...0002F
    at toBuffer (/home/kevin/coding/dao-voting/besu-deploy/node_modules/@ethereumjs/util/dist/bytes.js:147:19)
    at new BaseTransaction (/home/kevin/coding/dao-voting/besu-deploy/node_modules/@ethereumjs/tx/dist/baseTransaction.js:53:41)
    at new Transaction (/home/kevin/coding/dao-voting/besu-deploy/node_modules/@ethereumjs/tx/dist/legacyTransaction.js:28:9)
    at main (/home/kevin/coding/dao-voting/besu-deploy/public_tx.js:35:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.7.0

当我尝试执行以下操作时,就会发生这种情况:

代码语言:javascript
复制
const rawTxOptions = {
  nonce: web3.utils.numberToHex(txnCount),
  from: account.address,
  to: null, //public tx
  value: "0x00",
  data: '0x'+contractBin+contractConstructorInit, // contract binary appended with initialization value
  gasPrice: "0x0", //ETH per unit of gas
  gasLimit: "0x24A22" //max number of gas units the tx is allowed to use
};
const tx = new Tx(rawTxOptions);

我知道产生错误的字符串来自data字段,因为它与'0x'+contractBin (contractBin来自这个文件)匹配。因此,从错误中可以看到,给定的data和字符串确实以0x开头。那么,我为什么要犯这个错误呢?我创建了一个简单存储库来复制这个错误以及运行它的步骤

EN

回答 1

Ethereum用户

发布于 2023-03-28 17:37:43

我在my @ethereumjs/tx发行上得到了一个答案:

问题似乎在于您如何构造事务数据字段。在您的示例回购中,您正在连接data: '0x'+contractBin+contractConstructorInit,其中contractBin是一个buffer,而不是一个字符串。如果用data: '0x'+contractBin.toString('hex')+contractConstructorInit替换它,这个错误就会消失。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/148126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档