How do I get the public address & key (not address or account) for one of the accounts that Ganache displays on startup using Web3.js? I need it for a unit test I'm creating that tests transaction signing on the Ganache network, but without using Metamask. When Ganache starts up I see 10 accounts and 10 private keys along with the Mnemonic words, but no public addresses. I am already retrieving the accounts list from Ganache using web3.eth.getAccounts(), but I don't know how to get the public address & key associated with a particular account.

  • According to this post (question and answers), there is no practical use for the public-key (as far as the user - you, in this case - is concerned). The address, which you already know how to retrieve, is sufficient for all your needs. Commented Sep 24, 2018 at 8:06

1 Answer 1

  1. start ganache-cli with --acctKeys ganache-accounts.json parameter it will save all the data about accounts/keys

then use this script to extract public key :

#!/usr/bin/env python
import json
from pprint import pprint
import sys
file = sys.argv[1]
print file
data = json.loads(json_data)
for a in data['addresses']:
    for pbk in pubkeyArray:
        pubkey = "%s%02x" % (pubkey,pbk)
    print "Address:%s" % a
    print "Public Key:0x%s" % pubkey 
    print "Private Key:0x%s" % data['private_keys'][a]
  1. pass file name ganache-accounts.json to the script

  2. it will produce output as follows :

./scripts/ganache-keys.py ./keystore/ganache-accounts.json 
Public Key:0xf93e5a7cbd7b76cb3e2c1b362039397fbba00a0cbea1eccdf00e0e1e64d575411ae32bc6daf297aba31707fea7f84f20adb82c325fc33d347b54cf5fcd2bd71a
Private Key:0x30963a4383bfb78f0ecf290b1f3879a9c70903481e865f9e5d2df66ced64395d
Public Key:0xdcaa05bb5a82e0d1675d7f9b12b4d1948122500fc35ca7dee2edc99f2a3af2c5d6498f80dfbc0ab7c624793919aee8e4651adeefa7951c96438cad7838fbb9ea
Private Key:0x5641128d7d895f185d91fa2b83dffe646eede097d1a85c3247debc2b75219d92

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.