Ethereum RangeError:私钥长度无效:secp256k1.sign(msgHash,privateKey);

Ethereum RangeError:私钥长度无效:secp256k1.sign(msgHash,privateKey);,ethereum,web3js,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\ethereum

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

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”,但没有任何效果

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)
    })

}) 

任何人都可以提出建议,因为我是Web3新手,并尽我所能理解它。请尝试发送私钥,删除
'0x'

也许是这样:

process.env.PRIVATE_KEY_1.substr(2);

希望有帮助

我知道这很晚了,但是对于搜索这个的任何人,请尝试:
const privateKey=Buffer.from(process.env.PRIVATE_KEY,“hex”)

它对我有用;)

您是否尝试在没有任何“十六进制”转换的情况下发送普通私钥并删除
'0x'
?我看不到这行
var sig=secp256k1.sign(msgHash,privateKey)在您的代码中。我遗漏了什么吗?这是来自web3的引用,它是在签署tx时来自web3的。也许可以尝试通过以下方法广播它:
web3.eth.sendRawTransaction(raw,(err,txHash)=>{console.log('err:',err,'txHash:',txHash)})
我刚刚检查了npm上的ethereumjs tx包,发现声明存在差异,也许这就是问题所在。您已将其声明为
const-Tx=require('ethereumjs-Tx')
,并在npm.js上声明为
const-EthereumTx=require('ethereumjs-Tx')。事务