ethereum - RangeError : private key length is invalid: secp256k1. sign(msgHash, privateKey);

标签 ethereum web3js

在以太坊网络上签署交易时出现以下错误:

E:\Web3\node_modules\ethereumjs-util\dist\index.js:369
  var sig = secp256k1.sign(msgHash, privateKey);
                      ^

RangeError: private key length is invalid
    at Object.exports.ecsign (E:\Web3\node_modules\ethereumjs-util\dist\index.js:369:23)
    at Transaction.sign (E:\Web3\node_modules\ethereumjs-tx\es5\index.js:252:23)
    at Object.web3.eth.getTransactionCount [as callback] (E:\Web3\app3.js:264:8)
    at sendTxCallback (E:\Web3\node_modules\web3-core-method\src\index.js:484:29)
    at E:\Web3\node_modules\web3-core-requestmanager\src\index.js:147:9

这是导致此错误的 web3 代码。我正在将私钥值转换为“十六进制”表示,但它仍然无法解决。
const Tx = require('ethereumjs-tx')

const Web3 = require('web3')
const web3 = new Web3('https://ropsten.infura.io/tBIZU6erdu0roIzShVDM')

const account1='0xceAbcE5eE63212e7d4fAf9eB522d2B7b5886bF1F'
const account2='0x5F16088a3dec5c07E02317B403472c9ff5335912'

console.log(process.env.PRIVATE_KEY_1)

const privateKey1 = Buffer.from(process.env.PRIVATE_KEY_1, 'hex')
const privateKey2 = Buffer.from(process.env.PRIVATE_KEY_2, 'hex')

console.log(privateKey1)
console.log(privateKey2)

contractABI = [
    {
        "constant": true,
        "inputs": [],
        "name": "name",
        "outputs": [
            {
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_spender",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "approve",
        "outputs": [
            {
                "name": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "totalSupply",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_from",
                "type": "address"
            },
            {
                "name": "_to",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "transferFrom",
        "outputs": [
            {
                "name": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "standard",
        "outputs": [
            {
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "",
                "type": "address"
            }
        ],
        "name": "balanceOf",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "symbol",
        "outputs": [
            {
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "constant": false,
        "inputs": [
            {
                "name": "_to",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "transfer",
        "outputs": [
            {
                "name": "success",
                "type": "bool"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [
            {
                "name": "",
                "type": "address"
            },
            {
                "name": "",
                "type": "address"
            }
        ],
        "name": "allowance",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "_initialSupply",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    },
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": true,
                "name": "_from",
                "type": "address"
            },
            {
                "indexed": true,
                "name": "_to",
                "type": "address"
            },
            {
                "indexed": false,
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "Transfer",
        "type": "event"
    },
    {
        "anonymous": false,
        "inputs": [
            {
                "indexed": true,
                "name": "_owner",
                "type": "address"
            },
            {
                "indexed": true,
                "name": "_spender",
                "type": "address"
            },
            {
                "indexed": false,
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "Approval",
        "type": "event"
    }
]

const contractAddress = '0x30a8999Cb4c766fD6BA21723679466169710f053'

const contract = new web3.eth.Contract(contractABI, contractAddress)

const data = contract.methods.transfer(account2, 1000).encodeABI()

web3.eth.getTransactionCount(account1, (err, txCount) => {
    //Build Tx
    const txObject = {
        nonce: web3.utils.toHex(txCount),
        gasLimit: web3.utils.toHex(800000),
        gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
        to: contractAddress,
        data: data
    }

    //sign the Tx
    const tx = new Tx(txObject)
    tx.sign(privateKey1)

    const serializedTransaction = tx.serialize()
    const raw = '0x' + serializedTransaction.toString('hex')


    //Broadcast Tx
    web3.eth.sendSignedTransaction(raw, (err, txHash) => {
        console.log('err: ',err,'txHash:', txHash)
    })

}) 

我什至尝试不使用私钥的“十六进制”转换,也尝试从私钥值中删除“0x”,但没有任何效果。

任何人都可以建议,这里有什么问题,因为我是 Web3 的新手,并尽我所能去理解它。

最佳答案

我知道已经很晚了,但对于搜索此内容的任何人,请尝试:const privateKey = Buffer.from(process.env.PRIVATE_KEY, "hex");它对我有用;)

关于ethereum - RangeError : private key length is invalid: secp256k1. sign(msgHash, privateKey);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53591170/

相关文章:

c# - Poloniex C# API - 获取交易

ethereum - 从新创建的帐户中使用 web3 调用合约方法

javascript - 未处理的拒绝错误 : Invalid JSON RPC response: ""

javascript - 在发送交易之前如何估计当前的gas limit?

web3js - 如何查询 RSK 交易的内部交易?

javascript - 如何在 nodejs 中处理多个 web3 事务

javascript - 如何使用 Joi 验证以太坊地址

ethereum - 智能合约区 block 链历史

javascript - 如何使用 web3js 创建公钥和私钥?

ethereum - Ropsten与web3.js的网络连接(版本1.0.0-beta.34)