Doing some ERC-735 tests on truffle+ganache I got stuck when creating claim signatures. They need to be signed from my ganache account, but I don't know its private key. The code to be used is this:

const messageToSign = "hello world";
const privateKey = "43f2ee33c522046e80b67e96ceb84a05b60b9434b0ee2e3ae4b1311b9f5dcc46";
var msgHash = EthUtil.hashPersonalMessage(new Buffer(messageToSign));
var sig = EthUtil.ecsign(msgHash, new Buffer(privateKey, 'hex')); 
var signatureRPC = EthUtil.toRpcSig(sig.v, sig.r, sig.s)

As explained on Sign data with private key inside a truffle test file there was a method to sign messages from Truffle using:


but it is now deprecated, it throws "Method personal_sign not supported" error, so a solution seems to be an explicit signing using private key (code above).

How can I get the private key from ganache? or even better, can I get the private key from truffle?

  • 1
    signing using private key is actually not the best approach when developing, not because it doesn't work but it is risky (first blockchain transactions were done manually), in any ETH node the private keys are stored in a safe spot and only read by the node client itself. so trying to read it from js is really not going to happen, just look for the new signing method from web3 if that is deprecated. – Kaki Master Of Time Oct 22 '18 at 16:30
  • 1
    @KakiMasterOfTime , the only way to sign a transaction is using a private key, so would he use plain private key, or encrypted private key, anyway he has to store one of both (either private key or the password) in the code. – Nulik Oct 22 '18 at 22:40

You can start ganache with a mnemonic as a parameter ( -m 'jar boss sister abuse equal ....'). Doing this, you'll:

  • be guaranteed to have the same addresses generated with each run
  • be able to derive the private keys from that mnemonic

    const bip39 = require('bip39');
    const wallet = require('ethereumjs-wallet');
    const seed = bip39.mnemonicToSeed(mnemonic); // mnemonic is the string containing the words
    const hdk = hdkey.fromMasterSeed(seed);
    const addr_node = hdk.derivePath("m/44'/60'/0'/0/0"); //m/44'/60'/0'/0/0 is derivation path for the first account. m/44'/60'/0'/0/1 is the derivation path for the second account and so on
    const addr = addr_node.getWallet().getAddressString(); //check that this is the same with the address that ganache list for the first account to make sure the derivation is correct
    const private_key = addr_node.getWallet().getPrivateKey();

Your Answer

