From a1a844c1e671ce713d7ed44fe4c6bc7afb558b23 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Mon, 28 Apr 2014 20:03:51 -0300 Subject: [PATCH 1/2] add Address.forKey convenience method and example vanity address generator --- examples/VanityAddress.js | 28 ++++++++++++++++++++++++++++ lib/Address.js | 9 +++++++-- test/test.Address.js | 21 ++++++++++++++------- 3 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 examples/VanityAddress.js diff --git a/examples/VanityAddress.js b/examples/VanityAddress.js new file mode 100644 index 0000000..dfe4005 --- /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 5bc4abb..40e6cdc 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 a712cbe..80a87ee 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() { From ce00b77de020822a07aae90973996b4bf8a2e017 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 29 Apr 2014 11:48:59 -0300 Subject: [PATCH 2/2] name change forKey -> fromKey --- lib/Address.js | 2 +- test/test.Address.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Address.js b/lib/Address.js index 40e6cdc..9ca055b 100644 --- a/lib/Address.js +++ b/lib/Address.js @@ -27,7 +27,7 @@ Address.fromPubKey = function(pubKey, network) { }; // create an address from a Key object -Address.forKey = function(key, network) { +Address.fromKey = function(key, network) { return Address.fromPubKey(key.public, network); }; diff --git a/test/test.Address.js b/test/test.Address.js index 80a87ee..13f2f18 100644 --- a/test/test.Address.js +++ b/test/test.Address.js @@ -87,13 +87,13 @@ describe('Address', function() { addr.toString().should.equal(Address.fromPubKey(pubkey).toString()); }); }); - describe('#forKey', function() { + describe('#fromKey', 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); + var a = Address.fromKey(k); a.toString().should.equal('1L8k7WpWHMNkqVPTaZhzFU5VaWyjZEK7mD'); }); });