0

I am using ganache which creates an ethereum blockchain with 10 accounts.

enter image description here

The blockchain created by ganache is linked with truffle's pet-shop box using web3. I want to add a new account in that blockchain. I used web3.eth.accounts.create() which returns public and private key of the newly created account.

truffle(development)> web3.eth.accounts.create()

{ address: '0x53811349479671B8230BD853184AFf1e589f5A07',
  privateKey:
   '0x68e18d97f62a803cd6f4da1f1e00cb360d217a33c67ed4120fa3947e121b5b03',
  signTransaction: [Function: signTransaction],
  sign: [Function: sign],
  encrypt: [Function: encrypt] }

But the new account doesn't appear in the ganache. I also used web3.eth.getAccounts() to show all the accounts in the blockchain but I didn't get the new account.

truffle(development)> web3.eth.getAccounts()

[ '0x7771248F9Df904f5Ee684D325E9a2B4d7A413bA0',
  '0x9a46234628ab69516C43826A967312ABeB36DcE7',
  '0x1392BC4cdc0aB295FD18bb720218079Da3f0c73E',
  '0x4459Ffc957406B682DC8Cee556c2aDf17126A147',
  '0x25Ec4525E472EAf52D2bc289F5768dd57fc0FaE5',
  '0xf7A0B489BeE0ee7F195e64d991802f65a6B5AdDA',
  '0x77175215D7A5eedb1B5551c6bfedb8d38A415388',
  '0x25F4719D0eE8cc0eBf33a7AD61C35724Bc8E1740',
  '0xb6880212A8F43D4872789f8a2aEbaf8358b32f89',
  '0x5D51F0fB4bEa6fec0334fA0c7BBC5C3a8dD42Db1',
  '0xb037075B163a6d3772AA8a401FC8A1361b970c43' ]

I can use the new account by using its private key. But where the new account is created? How I will know that the new account is part of ethereum blockchain or not which is created by ganache?

4

Ganache doesn't sync with the new accounts you generate.

Ganache gets you started with 10 accounts to choose from.

Why don't you just use one of them?

1

When you start ganache it creates you 10 accounts by default, pre-funds it with 100 ETH per account and groups those 10 accounts in a Object (let's call this accounts array because it looks similar to an array :p)

When you run the command web3.eth.accounts.create() , you are creating a new account part of the same ganache network (of course same blockchain) but it not part of the initial accounts array that ganache gave you when you started the network. So it is not pre funded (But hey, for all testing purposes 1000 ETH is way too generous)

But if you want to group all your accounts for your use case, run these commands in your truffle console:

truffle(development)> accountsArray = await web3.eth.getAccounts() // you are getting 10 pre-funded accounts

truffle(development)> newAccount = web3.eth.accounts.create() //creating a new account

truffle(development)> accountsArray.push(newAccount.address) //adding your new account to the accounts array

truffle(development)> accountsArray //you can now see your new account in this list

I hope this answer gives you a general understanding of what happens in the background :)

  • push() function was not working for me. The method above will only put publick key in the accountsArray but i want to link my new account with actual blockchain. – Zeeshan Ahmad Khalil Sep 3 at 6:11
  • I would like to know what error msg you get while executing push function. And yes, this will only add new account to the array. I do not understand what you mean by linking because the new account is part of actual blockchain. – raj chakravarthy Sep 3 at 18:35
  • sorry, the push is not giving any error now. – Zeeshan Ahmad Khalil Sep 4 at 12:07
  • I want to add the new account in the accounts created by ganache so that whenever I write web3.eth.getAccounts() it should show me public key of 10 accounts created by ganache + the public key of new account. The above answer is only creating a copy of public keys of these11 accounts in accountsArray – Zeeshan Ahmad Khalil Sep 4 at 12:16

Your Answer

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

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