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

时间:2018-12-03 09:51:37

标签: 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)
    })

}) 

我什至尝试不使用私钥的'hex'转换,我也尝试从私钥值中删除'0x',但是没有任何结果。

任何人都可以建议,因为我是Web3的新手,请尽我最大的努力来理解它。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

尝试发送私钥以删除'0x'

也许是这样:

process.env.PRIVATE_KEY_1.substr(2);

希望有帮助!