Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from ethereumjs/feature/vanitygen
Browse files Browse the repository at this point in the history
Include vanity address generation
  • Loading branch information
axic committed Jun 9, 2016
2 parents 5a597bd + e55656b commit 21e7127
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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`)
* `generateVanityAddress(pattern)` - create an instance where the address is valid against the supplied pattern (**this will be very slow**)
* `fromPrivateKey(input)` - create an instance based on a raw private key
* `fromExtendedPrivateKey(input)` - create an instance based on a BIP32 extended private key (xprv)
* `fromPublicKey(input, [nonStrict])` - create an instance based on a public key (certain methods will not be available)
Expand Down
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ Wallet.generate = function (icapDirect) {
}
}

Wallet.generateVanityAddress = function (pattern) {
if (typeof pattern !== 'object') {
pattern = new RegExp(pattern)
}

while (true) {
var privKey = crypto.randomBytes(32)
var address = ethUtil.privateToAddress(privKey)

if (pattern.test(address.toString('hex'))) {
return new Wallet(privKey)
}
}
}

Wallet.prototype.getPrivateKey = function () {
return this.privKey
}
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ describe('.generate()', function () {
})
})

describe('.generateVanityAddress()', function () {
it('should generate an account with 000 prefix', function () {
var wallet = Wallet.generateVanityAddress(/^000/)
assert.equal(wallet.getPrivateKey().length, 32)
assert.equal(wallet.getAddress()[0], 0)
assert.equal(wallet.getAddress()[1] >>> 4, 0)
})
})

describe('.getV3Filename()', function () {
it('should work', function () {
assert.equal(fixturewallet.getV3Filename(1457917509265), 'UTC--2016-03-14T01-05-09.265Z--b14ab53e38da1c172f877dbc6d65e4a1b0474c3c')
Expand Down

0 comments on commit 21e7127

Please sign in to comment.