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

Commit e55656b

Browse files
committed
Include vanity address generation
1 parent a317c50 commit e55656b

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
@@ -61,6 +61,21 @@ Wallet.generate = function (icapDirect) {
6161
}
6262
}
6363

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

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)