Skip to content

Commit

Permalink
Add sha256 Address from pubkey functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
rynomster committed Nov 12, 2017
1 parent 4487087 commit 612c7b7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
18 changes: 10 additions & 8 deletions lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function Address(data, network, type) {
Address.prototype._classifyArguments = function(data, network, type) {
/* jshint maxcomplexity: 10 */
// transform and validate input data
if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 20) {
if ((data instanceof Buffer || data instanceof Uint8Array) && [20,32].includes(data.length)) {
return Address._transformHash(data);
} else if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 21) {
return Address._transformBuffer(data, network, type);
Expand Down Expand Up @@ -132,8 +132,8 @@ Address._transformHash = function(hash) {
if (!(hash instanceof Buffer) && !(hash instanceof Uint8Array)) {
throw new TypeError('Address supplied is not a buffer.');
}
if (hash.length !== 20) {
throw new TypeError('Address hashbuffers must be exactly 20 bytes.');
if (hash.length !== 20 && hash.length !== 32) {
throw new TypeError('Address hashbuffers must be exactly 20 or 32 bytes.');
}
info.hashBuffer = hash;
return info;
Expand Down Expand Up @@ -251,16 +251,17 @@ Address._transformBuffer = function(buffer, network, type) {
* Internal function to transform a {@link PublicKey}
*
* @param {PublicKey} pubkey - An instance of PublicKey
* @param {Boolean} sha256 - Hash with sha256
* @returns {Object} An object with keys: hashBuffer, type
* @private
*/
Address._transformPublicKey = function(pubkey) {
Address._transformPublicKey = function(pubkey, sha256) {
var info = {};
if (!(pubkey instanceof PublicKey)) {
throw new TypeError('Address must be an instance of PublicKey.');
}
info.hashBuffer = Hash.sha256ripemd160(pubkey.toBuffer());
info.type = Address.PayToPublicKeyHash;
info.hashBuffer = Hash[sha256 ? 'sha256' : 'sha256ripemd160'](pubkey.toBuffer());
info.type = Address[sha256 ? 'PayToPublicKeyHash256' : 'PayToPublicKeyHash'];
return info;
};

Expand Down Expand Up @@ -321,10 +322,11 @@ Address._transformString = function(data, network, type) {
*
* @param {PublicKey} data
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @param {Boolean} sha256
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.fromPublicKey = function(data, network) {
var info = Address._transformPublicKey(data);
Address.fromPublicKey = function(data, network, sha256) {
var info = Address._transformPublicKey(data, sha256);
network = network || Networks.defaultNetwork;
return new Address(info.hashBuffer, network, info.type);
};
Expand Down
5 changes: 3 additions & 2 deletions lib/publickey.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,12 @@ PublicKey.prototype._getID = function _getID() {
* Will return an address for the public key
*
* @param {String|Network=} network - Which network should the address be for
* @param {Boolean=} sha256 - Return a sha256 hashed address
* @returns {Address} An address generated from the public key
*/
PublicKey.prototype.toAddress = function(network) {
PublicKey.prototype.toAddress = function(network, sha256) {
var Address = require('./address');
return Address.fromPublicKey(this, network || this.network);
return Address.fromPublicKey(this, network || this.network, sha256);
};

/**
Expand Down

0 comments on commit 612c7b7

Please sign in to comment.