Introduce .generate() for creating new wallets

This commit is contained in:
Alex Beregszaszi 2016-02-24 12:55:36 +00:00
parent fed7bcde9a
commit 4cf1f60e7f
2 changed files with 14 additions and 0 deletions

View File

@ -21,6 +21,7 @@ Features not supported:
Constructors:
* `generate([icap])` - create an instance based on a new random key (setting `icap` to true will generate an address suitable for the `ICAP Direct mode`)
* `fromPrivateKey(input)` - create an instance based on a raw key
* `fromV1(input, password)` - import a wallet (Version 1 of the Ethereum wallet format)
* `fromV3(input, password)` - import a wallet (Version 3 of the Ethereum wallet format)

View File

@ -17,6 +17,19 @@ var Wallet = function (priv) {
this.privKey = priv
}
Wallet.generate = function (icapDirect) {
if (icapDirect) {
while (true) {
var privKey = crypto.randomBytes(32)
if (ethUtil.privateToAddress(privKey)[0] === 0) {
return new Wallet(privKey)
}
}
} else {
return new Wallet(crypto.randomBytes(32))
}
}
Wallet.prototype.getPrivateKey = function () {
return this.privKey
}