diff --git a/README.md b/README.md index aef4431..429b2b2 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/index.js b/index.js index 3e1abb0..7aa41a3 100644 --- a/index.js +++ b/index.js @@ -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 } diff --git a/test/index.js b/test/index.js index 388616c..4f38a7c 100644 --- a/test/index.js +++ b/test/index.js @@ -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')