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

Commit 21e7127

Browse files
committed
Merge pull request #5 from ethereumjs/feature/vanitygen
Include vanity address generation
2 parents 5a597bd + e55656b commit 21e7127

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Features not supported:
2828
Constructors:
2929

3030
* `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`)
31+
* `generateVanityAddress(pattern)` - create an instance where the address is valid against the supplied pattern (**this will be very slow**)
3132
* `fromPrivateKey(input)` - create an instance based on a raw private key
3233
* `fromExtendedPrivateKey(input)` - create an instance based on a BIP32 extended private key (xprv)
3334
* `fromPublicKey(input, [nonStrict])` - create an instance based on a public key (certain methods will not be available)

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ Wallet.generate = function (icapDirect) {
6060
}
6161
}
6262

63+
Wallet.generateVanityAddress = function (pattern) {
64+
if (typeof pattern !== 'object') {
65+
pattern = new RegExp(pattern)
66+
}
67+
68+
while (true) {
69+
var privKey = crypto.randomBytes(32)
70+
var address = ethUtil.privateToAddress(privKey)
71+
72+
if (pattern.test(address.toString('hex'))) {
73+
return new Wallet(privKey)
74+
}
75+
}
76+
}
77+
6378
Wallet.prototype.getPrivateKey = function () {
6479
return this.privKey
6580
}

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ describe('.generate()', function () {
108108
})
109109
})
110110

111+
describe('.generateVanityAddress()', function () {
112+
it('should generate an account with 000 prefix', function () {
113+
var wallet = Wallet.generateVanityAddress(/^000/)
114+
assert.equal(wallet.getPrivateKey().length, 32)
115+
assert.equal(wallet.getAddress()[0], 0)
116+
assert.equal(wallet.getAddress()[1] >>> 4, 0)
117+
})
118+
})
119+
111120
describe('.getV3Filename()', function () {
112121
it('should work', function () {
113122
assert.equal(fixturewallet.getV3Filename(1457917509265), 'UTC--2016-03-14T01-05-09.265Z--b14ab53e38da1c172f877dbc6d65e4a1b0474c3c')

0 commit comments

Comments
 (0)