diff --git a/examples/VanityAddress.js b/examples/VanityAddress.js new file mode 100644 index 000000000..dfe400522 --- /dev/null +++ b/examples/VanityAddress.js @@ -0,0 +1,28 @@ +'use strict'; + + +var run = function() { + // Replace '../bitcore' with 'bitcore' if you use this code elsewhere. + var bitcore = require('../bitcore'); + var Key = bitcore.Key; + var Address = bitcore.Address; + + // config your regular expression + var re = /[0-9]{6}$/; // ends in 6 digits + + var a,k,m; + while (true) { + k = Key.generateSync(); + a = Address.forKey(k); + m = a.toString().match(re); + if (m) break; + } + console.log('Address: '+a.toString()); + console.log('Private Key: '+k.private.toString('hex')); + +}; + +module.exports.run = run; +if (require.main === module) { + run(); +} diff --git a/lib/Address.js b/lib/Address.js index 5bc4abb22..40e6cdc2e 100644 --- a/lib/Address.js +++ b/lib/Address.js @@ -12,12 +12,12 @@ function Address() { Address.parent = parent; parent.applyEncodingsTo(Address); -//create a pubKeyHash address +// create a pubKeyHash address Address.fromPubKey = function(pubKey, network) { if (!network) network = 'livenet'; - if (pubKey.length != 33 && pubKey.length != 65) + if (pubKey.length !== 33 && pubKey.length !== 65) throw new Error('Invalid public key'); var version = networks[network].addressVersion; @@ -26,6 +26,11 @@ Address.fromPubKey = function(pubKey, network) { return new Address(version, hash); }; +// create an address from a Key object +Address.forKey = function(key, network) { + return Address.fromPubKey(key.public, network); +}; + //create a p2sh m-of-n multisig address Address.fromPubKeys = function(mReq, pubKeys, network, opts) { if (!network) diff --git a/test/test.Address.js b/test/test.Address.js index a712cbe2d..80a87eede 100644 --- a/test/test.Address.js +++ b/test/test.Address.js @@ -5,15 +5,11 @@ var bitcore = bitcore || require('../bitcore'); var should = chai.should(); -var AddressModule = bitcore.Address; -var Address; +var Address = bitcore.Address; +var Key = bitcore.Key; describe('Address', function() { - it('should initialze the main object', function() { - should.exist(AddressModule); - }); it('should be able to create class', function() { - Address = AddressModule; should.exist(Address); }); it('should be able to create instance', function() { @@ -84,13 +80,24 @@ describe('Address', function() { }); describe('#fromPubKey', function() { - it('should make this pubkeyhash address from uncompressed this public key', function() { + it('should make pubkeyhash address from an uncompressed public key', function() { var pubkey = new Buffer('04fa05ce8b25010cb6e17a30e0b66668bf083c40687547748ec330ee77adf53a42abd3d26148cbacfcf79c907ddefeb2c37f8bebc0a695ba79d634449d871de218', 'hex'); var hash = bitcore.util.sha256ripe160(pubkey); var addr = new Address(0, hash); addr.toString().should.equal(Address.fromPubKey(pubkey).toString()); }); }); + describe('#forKey', function() { + it('should make this pubkeyhash address from uncompressed this public key', function() { + var k = new Key(); + k.private = new Buffer('43532455C88590A594D552F76DDB70EC1CFD7746F05C10CBB70B1EA9552EDF87', 'hex'); + k.compressed = true; + k.regenerateSync(); + var a = Address.forKey(k); + a.toString().should.equal('1L8k7WpWHMNkqVPTaZhzFU5VaWyjZEK7mD'); + }); + }); + describe('#fromPubKeys', function() { it('should make this p2sh multisig address from these pubkeys', function() {