add Address.forKey convenience method and example vanity address generator

This commit is contained in:
Manuel Araoz 2014-04-28 20:03:51 -03:00
parent 1959ce953c
commit a1a844c1e6
3 changed files with 49 additions and 9 deletions

28
examples/VanityAddress.js Normal file
View File

@ -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();
}

View File

@ -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)

View File

@ -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() {