2

I'm using Truffle, web3js and Metamask to build a webapp. I'm using ganache for local testing. I'm trying to deploy a contract with

import TruffleDeployer from 'truffle-deployer';
import TruffleContract from 'truffle-contract';
import BaseDaoData from './build/contracts/BaseDao.json';

const provider = window.web3.currentProvider; // MetaMaskInpageProvider
const web3 = window.web3;
var deployer = null;
web3.version.getNetwork(function(err, id) {
      const network_id = id;

      deployer = new TruffleDeployer({
        contracts: [BaseDaoData],
        network: "test",
        network_id: network_id,
        provider: provider
      });

      deployer.start();
});

const deployHelper = (contract, ...args) => {
    var acc = window.web3.eth.accounts[0];
    return deployer.deploy(contract, ...args, {from: acc});
};

deployHelper(TruffleContract(BaseDaoData), "my", "args");

I manually set Ganache to use the same mnemonic, nose foot asset wage radar canyon online bread hurry train absent bachelor on each start so that the accounts are the same. I've imported the private key (f49e1216edac9a5b0fab36f28037bfe8d5eb104b13f049b59decfac446e56ab3) of one of the accounts (0x1f9945a4DBe3851beDc787434d2A63dd44303f77) into metamask and have confirmed it's presence. If I set a breakpoint in my code and inspect web3.eth.accounts[0] I see the address of the private key which I imported. Everything is dandy. However, when I run this JS I get the following error:

RangeError: private key length is invalid
    at Object.t.isBufferLength (/node_modules/ganache-cli/build/webpack:/ganache/node_modules/secp256k1/lib/assert.js:31:1)
    at Object.sign (/node_modules/ganache-cli/build/webpack:/ganache/node_modules/secp256k1/lib/index.js:173:1)
    at Object.t.ecsign (/node_modules/ganache-cli/build/webpack:/ganache/node_modules/ethereumjs-util/dist/index.js:374:1)
    at e.exports.sign (/node_modules/ganache-cli/build/webpack:/ganache/node_modules/ethereumjs-tx/index.js:233:1)
    at e.exports.hash (/node_modules/ganache-cli/build/webpack:/ganache/node_modules/ethereumjs-tx/fake.js:64:1)
    at new c (/node_modules/ganache-cli/build/webpack:/ganache/node_modules/ganache-core/lib/utils/receipt.js:16:1)
    at /node_modules/ganache-cli/build/webpack:/ganache/node_modules/ganache-core/lib/database/receiptserializer.js:28:1
    at /node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:4094:1
    at /node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:480:1
    at o (/node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:1006:1)
    at /node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:977:1
    at /node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:4093:1
    at /node_modules/ganache-cli/build/webpack:/ganache/node_modules/ganache-core/lib/database/blocklogsserializer.js:43:1
    at /node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:1219:1
    at /node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:480:1
    at i (/node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:1116:1)
    at /node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:977:1
    at /node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:1219:1
    at /node_modules/ganache-cli/build/webpack:/ganache/node_modules/ganache-core/lib/database/blocklogsserializer.js:31:1
    at /node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:1218:1
    at ke (/node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:1121:1)
    at Ee (/node_modules/ganache-cli/build/webpack:/ganache/node_modules/async/dist/async.js:1126:1)

When I use metamask to export the private key, I see that what's exported is correct except it's missing the 0x prefix which I suspect is why this RangeError is happening. I'm not sure how to ask metamask to prepend the private key it provides with 0x or how to intercept the private key and prepend 0x myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.