I want to create new accounts through my Dapp web interface and unlock a specific account so that transactions can be processed. I didn't see it in the web3.js documentation so wonder if that's on the web3.js roadmap?
Updated answer per 4 Jan 2018
Web3.js has full-fledged support for accounts management starting at v1.0.0, including creating a new account. v1.0.0 is still in beta, but is close to production and is already the version released through npm
. Thanks Thom Ives for pointing this out.
Full example sourced from web3.js documentation:
web3.eth.accounts.create();
> {
address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709",
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
web3.eth.accounts.create('2435@#@#@±±±±!!!!678543213456764321§34567543213456785432134567');
> {
address: "0xF2CD2AA0c7926743B1D4310b2BC984a0a453c3d4",
privateKey: "0xd7325de5c2c1cf0009fac77d3d04a9c004b038883446b065871bc3e831dcd098",
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
web3.eth.accounts.create(web3.utils.randomHex(32));
> {
address: "0xe78150FaCD36E8EB00291e251424a0515AA1FF05",
privateKey: "0xcc505ee6067fba3f6fc2050643379e190e087aeffe5d958ab9f2f3ed3800fa4e",
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
Old answer from 2016
Web3.js doesn't have this capability built in and likely won't ever have. However there are custom solutions like https://github.com/SilentCicero/ethereumjs-accounts/.
ethereumjs-accounts
allows you to create accounts in the browser, then use these addresses for web3.js
transactions. Private keys are kept in localStorage
and can be encrypted. Be sure to read the security warnings and understand what you are doing.
https://metamask.io/ is a broader approach to make DApps accessible through a normal browser. It contains identity management (e.g. accounts) too.
In my view solutions like ethereumjs-accounts
and Metamask can be a good on-ramp to make DApps accessible to a wide audience, for low-value transactions or even just read-only access of DApps.
It is unlikely functionality regarding accounts will be added to web3.js.
Mist is a browser designed to run DApps and manage accounts. It has mist.requestAccount
which appears to be the expected API DApps should use.
Probably the main reason why Mist and not web3.js, is that a browser itself is able to provide more security than Javascript. For example, a modal dialog in Mist that prompts a user for their account and password, is more difficult to spoof, tamper, or intercept than an HTML/CSS/JS dialog.
-
1Right now mist requires you to run a full node. But that is not too user friendly, considering it the blockchain is already at 10 GB. If I want to develop some end user applications we need to find a way for users to avoid having to use mist, or make mist less resource intensive, don't you think? – Bilthon Apr 4 '16 at 0:32
-
2Once the LES protocol is finished, Mist will be able to work without downloading the full blockchain. – Tjaden Hess♦ Apr 18 '16 at 0:46
After requesting an update in my comments to Aron's answer, I did a search using a modification of this title and found that web3.js does now in fact allow you to create new accounts. SEE HERE. From reading these web3.js docs on create, it appears to create accounts in a way that is as secure as MetaMask.
-
Indeed that looks like full-fledged accounts management functionality within web3.js. Happy to have been proven wrong on this :) – AronVanAmmers Jan 4 '18 at 15:46
-
@AronVanAmmers, I think when you answered originally it was not available. However, learning to manage others addresses along with their private keys without knowing their private keys will require us to learn and share a new set of best practices. – Thom Ives Jan 5 '18 at 16:41
You can check out ethers.js
Ethers.js has full wallet management, seperated from geth (like it should be since it's just crypto). You can then attach a provider to your wallet (web3 does it the other way around basicly). You then attach this wallet+provider to the contract artifact and you have a contract instance you can call.