[Ethereum] How to make new account by JSON-RPC

go-ethereumjson-rpcSecurity

I'd like to make new account by JSON-RPC.I've checked the wiki of Ethereum, however I could not find out how to do that.

I've found call existed accounts, but it isn't what I want.

eth_accounts

Could you tell me how to do this?

Best Answer

First, a note on safety:

You should not make the personal API available over RPC

If you are on a local, trusted machine, you should use IPC instead of RPC. Otherwise, anyone who can connect to your node via RPC can try to brute-force your passwords and steal your Ether.

All administrative APIs are available by default over IPC, so no need to use any flags with geth

To connect via IPC:

Install my library: npm install web3_extended

var web3_extended = require('web3_extended');

var options = {
  host: '/home/user/.ethereum/geth.ipc',
  ipc:true,
  personal: true, 
  admin: false,
  debug: false
};

var web3 = web3_extended.create(options);

web3.personal.newAccount("password",function(error,result){
    if(!error){
        console.log(result);
    }
});

Replace the host variable with the proper path for your system.

Note: All requests via IPC must be asynchronous.


Some Alternatives:

I don't know why you want to create new accounts via web3, but it's likely not the best way to do what you're trying to achieve. It is much safer and more modular to use a hooked web3 provider with a client-side light wallet or to simply use the Mist browser which handles all accounts for you.

Now for the technique (don't do this)

You need to enable the personal API over RPC. Do this by starting geth with

geth --rpc --rpcapi "db,eth,net,web3,personal"

Then you can use the personal_newAccount method via RPC. It's not implemented in web3.js, so you need to manually issue the RPC request. For example with curl:

curl -X POST --data '{"jsonrpc":"2.0","method":"personal_newAccount","params":["password"],"id":1}' localhost:8545

creates a new account with password password and returns the address:

{"id":1,"jsonrpc":"2.0","result":"0x05ca0ddf7e7506672f745b2b567f1d33b7b55f4f"} There is some basic documentation

Alternatively: Use the unofficial extended web3.js

  • this allows you to use the personal, admin and miner APIs via a standard web3.js interface.
Related Topic