Search code examples
blockchainethereum

How to create an ethereum account using truffle ( not in the console mode )


i m using truffle for compiling and deploying a smart contract in ethereum and for that i need to create some account to interact with the smart contract .

the creation of the account is always made with the command line in truffle like this :

    truffle console
 web3.personal.newAccount('verystrongpassword')
web3.eth.getBalance('0x95a94979d86d9c32d1d2ab5ace2dcc8d1b446fa1')
web3.personal.unlockAccount('0x95a94979d86d9c32d1d2ab5ace2dcc8d1b446fa1', 'verystrongpassword', 15000) 

is there a way that i can put this code in a script (javascript) and run it with truflle without the command line

thanks !


Solution

  • The Truffle console is an interactive JavaScript console. Anything you can do in the console can be done in another JavaScript environment. The implementation details change depending on if you're running within a browser/Node environment and the Ethereum node provider you're connecting to.

    It's a little tricky with web3js 0.20.x because the web3.personal library is not always available in your client for security reasons (it depends on if you're connecting to a local node via IPC or RPC and which APIs that node has enabled. See may the Geth Management API documentation for details).

    It's much easier if you're using web3js 1.0.0. In that version, they introduced functionality in web3.eth.accounts that allows you to create new accounts.