如何添加私钥以将智能合约部署到 Ropsten?

How to add private keys to deploy a smart contract to Roptsen?

我正在尝试将智能合约部署到 Ropsten 测试网。

我尝试将私钥助记符添加到 .secret 文件,但在终端 运行 truffle migrate --network ropsten

中出现以下错误

Error: Private key does not satisfy the curve requirements (ie. it is invalid)

infura api 密钥通过使用点环境导入它来工作。

助记词私钥来自metamask钱包

现在 truffle.config 文件中的内容是:

require('babel-polyfill');
require('dotenv').config();
const HDWalletProvider = require('truffle-hdwallet-provider-privkey');
const MNEMONIC = './.secret';
const infuraKey = process.env.INFURA_API_KEY


module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*" // Match any network id
    },

    ropsten: {
      provider: () => new HDWalletProvider(MNEMONIC, `https://ropsten.infura.io/v3/${infuraKey}`),
      network_id: 3,       // Ropsten's id
      gas: 5500000,        // Ropsten has a lower block limit than mainnet
      confirmations: 2,    // # of confs to wait between deployments. (default: 0)
      timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
    }
  },
  contracts_directory: './src/contracts/',
  contracts_build_directory: './src/abis/',
  compilers: {
    solc: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  }
}

其他人遇到了这个问题here

有人建议使用 Buffer.from,即 Buffer.from('<PRIVATE_KEY>', 'hex') 首先将您的私钥转换为缓冲区。

可以尝试使用以下方法:

const privateKey = Buffer.from('PRIVATEKEYFROMMETAMASK', 'hex');
const provider = new HDWalletProvider(privateKey, "https://ropsten.infura.io/v3/INFURATOKEN");
const web3 = new Web3(provider);