diff --git a/lib/publickey.js b/lib/publickey.js index dd1b60fe0..2c25d1279 100644 --- a/lib/publickey.js +++ b/lib/publickey.js @@ -2,6 +2,7 @@ var Point = require('./crypto/point'); var BN = require('./crypto/bn'); +var Address = require('./address'); /** * @@ -316,6 +317,16 @@ PublicKey.prototype.toDER = function(compressed) { } }; +/** + * + * Will return an address for the public key + * + * @returns {Address} An address generated from the public key + */ +PublicKey.prototype.toAddress = function(network) { + return Address.fromPublicKey(this, network || 'livenet'); +}; + /** * * Will output the PublicKey to a DER encoded hex string diff --git a/test/publickey.js b/test/publickey.js index fe54c8ad7..299eef008 100644 --- a/test/publickey.js +++ b/test/publickey.js @@ -256,6 +256,22 @@ describe('PublicKey', function() { }); + describe('#toAddress', function() { + + it('should output this known mainnet address correctly', function() { + var pk = new PublicKey('03c87bd0e162f26969da8509cafcb7b8c8d202af30b928c582e263dd13ee9a9781'); + var address = pk.toAddress('mainnet'); + address.toString().should.equal('1A6ut1tWnUq1SEQLMr4ttDh24wcbJ5o9TT'); + }); + + it('should output this known testnet address correctly', function() { + var pk = new PublicKey('0293126ccc927c111b88a0fe09baa0eca719e2a3e087e8a5d1059163f5c566feef'); + var address = pk.toAddress('testnet'); + address.toString().should.equal('mtX8nPZZdJ8d3QNLRJ1oJTiEi26Sj6LQXS'); + }); + + }); + describe('#toString', function() { it('should print this known public key', function() {