|
|
@ -1,11 +1,10 @@ |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
var base58check = require('./encoding/base58check'); |
|
|
|
var networks = require('./networks'); |
|
|
|
var Networks = require('./networks'); |
|
|
|
var Hash = require('./crypto/hash'); |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Instantiate an address from an address String or Buffer, a public key or script hash Buffer, |
|
|
|
* or an instance of PublicKey or Script. |
|
|
|
* |
|
|
@ -40,7 +39,7 @@ function Address(data, network, type) { |
|
|
|
throw new TypeError('First argument is required, please include address data.'); |
|
|
|
} |
|
|
|
|
|
|
|
if (network && !networks.get(network)) { |
|
|
|
if (network && !Networks.get(network)) { |
|
|
|
throw new TypeError('Second argument must be "livenet" or "testnet".'); |
|
|
|
} |
|
|
|
|
|
|
@ -68,7 +67,7 @@ function Address(data, network, type) { |
|
|
|
} |
|
|
|
|
|
|
|
// set defaults if not set
|
|
|
|
info.network = info.network || network || networks.defaultNetwork.name; |
|
|
|
info.network = info.network || Networks.get(network) || Networks.defaultNetwork; |
|
|
|
info.type = info.type || type || Address.PayToPublicKeyHash; |
|
|
|
|
|
|
|
Object.defineProperty(this, 'hashBuffer', { |
|
|
@ -93,7 +92,6 @@ Address.PayToPublicKeyHash = 'pubkeyhash'; |
|
|
|
Address.PayToScriptHash = 'scripthash'; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Internal function to transform a hash buffer |
|
|
|
* |
|
|
|
* @param {Buffer} hash - An instance of a hash Buffer |
|
|
@ -113,7 +111,39 @@ Address._transformHash = function(hash){ |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Internal function to discover the network and type |
|
|
|
* |
|
|
|
* @param {Buffer} buffer - An instance of a hex encoded address Buffer |
|
|
|
* @returns {Object} An object with keys: network and type |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
Address._classifyFromVersion = function(buffer){ |
|
|
|
var version = {}; |
|
|
|
switch(buffer[0]){ // the version byte
|
|
|
|
case Networks.livenet.pubkeyhash: |
|
|
|
version.network = Networks.livenet; |
|
|
|
version.type = Address.PayToPublicKeyHash; |
|
|
|
break; |
|
|
|
|
|
|
|
case Networks.livenet.scripthash: |
|
|
|
version.network = Networks.livenet; |
|
|
|
version.type = Address.PayToScriptHash; |
|
|
|
break; |
|
|
|
|
|
|
|
case Networks.testnet.pubkeyhash: |
|
|
|
version.network = Networks.testnet; |
|
|
|
version.type = Address.PayToPublicKeyHash; |
|
|
|
break; |
|
|
|
|
|
|
|
case Networks.testnet.scripthash: |
|
|
|
version.network = Networks.testnet; |
|
|
|
version.type = Address.PayToScriptHash; |
|
|
|
break; |
|
|
|
} |
|
|
|
return version; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Internal function to transform a bitcoin address buffer |
|
|
|
* |
|
|
|
* @param {Buffer} buffer - An instance of a hex encoded address Buffer |
|
|
@ -131,47 +161,24 @@ Address._transformBuffer = function(buffer, network, type){ |
|
|
|
throw new TypeError('Address buffers must be exactly 21 bytes.'); |
|
|
|
} |
|
|
|
|
|
|
|
var bufNetwork = false; |
|
|
|
var bufType = false; |
|
|
|
network = Networks.get(network); |
|
|
|
var bufferVersion = Address._classifyFromVersion(buffer); |
|
|
|
|
|
|
|
switch(buffer[0]){ // the version byte
|
|
|
|
case networks.livenet.pubkeyhash: |
|
|
|
bufNetwork = 'livenet'; |
|
|
|
bufType = 'pubkeyhash'; |
|
|
|
break; |
|
|
|
|
|
|
|
case networks.livenet.scripthash: |
|
|
|
bufNetwork = 'livenet'; |
|
|
|
bufType = 'scripthash'; |
|
|
|
break; |
|
|
|
|
|
|
|
case networks.testnet.pubkeyhash: |
|
|
|
bufNetwork = 'testnet'; |
|
|
|
bufType = 'pubkeyhash'; |
|
|
|
break; |
|
|
|
|
|
|
|
case networks.testnet.scripthash: |
|
|
|
bufNetwork = 'testnet'; |
|
|
|
bufType = 'scripthash'; |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
if (!bufNetwork || (network && network !== bufNetwork)) { |
|
|
|
if (!bufferVersion.network || (network && network !== bufferVersion.network)) { |
|
|
|
throw new TypeError('Address has mismatched network type.'); |
|
|
|
} |
|
|
|
|
|
|
|
if (!bufType || ( type && type !== bufType )) { |
|
|
|
if (!bufferVersion.type || ( type && type !== bufferVersion.type )) { |
|
|
|
throw new TypeError('Address has mismatched type.'); |
|
|
|
} |
|
|
|
|
|
|
|
info.hashBuffer = buffer.slice(1); |
|
|
|
info.network = bufNetwork; |
|
|
|
info.type = bufType; |
|
|
|
info.network = bufferVersion.network; |
|
|
|
info.type = bufferVersion.type; |
|
|
|
return info; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Internal function to transform a PublicKey |
|
|
|
* |
|
|
|
* @param {PublicKey} pubkey - An instance of PublicKey |
|
|
@ -189,7 +196,6 @@ Address._transformPublicKey = function(pubkey){ |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Internal function to transform a Script |
|
|
|
* |
|
|
|
* @param {Script} script - An instance of Script |
|
|
@ -207,7 +213,6 @@ Address._transformScript = function(script){ |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Internal function to transform a bitcoin address string |
|
|
|
* |
|
|
|
* @param {String} data - An instance of PublicKey |
|
|
@ -226,7 +231,6 @@ Address._transformString = function(data, network, type){ |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Instantiate an address from a PublicKey instance |
|
|
|
* |
|
|
|
* @param {PublicKey} data - An instance of PublicKey |
|
|
@ -235,12 +239,11 @@ Address._transformString = function(data, network, type){ |
|
|
|
*/ |
|
|
|
Address.fromPublicKey = function(data, network){ |
|
|
|
var info = Address._transformPublicKey(data); |
|
|
|
network = network || networks.defaultNetwork.name; |
|
|
|
network = network || Networks.defaultNetwork; |
|
|
|
return new Address(info.hashBuffer, network, info.type); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Instantiate an address from a ripemd160 public key hash |
|
|
|
* |
|
|
|
* @param {Buffer} hash - An instance of buffer of the hash |
|
|
@ -253,7 +256,6 @@ Address.fromPublicKeyHash = function(hash, network) { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Instantiate an address from a ripemd160 script hash |
|
|
|
* |
|
|
|
* @param {Buffer} hash - An instance of buffer of the hash |
|
|
@ -266,7 +268,6 @@ Address.fromScriptHash = function(hash, network) { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Instantiate an address from a Script |
|
|
|
* |
|
|
|
* @param {Script} script - An instance of Script |
|
|
@ -279,7 +280,6 @@ Address.fromScript = function(script, network) { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Instantiate an address from a buffer of the address |
|
|
|
* |
|
|
|
* @param {Buffer} buffer - An instance of buffer of the address |
|
|
@ -293,7 +293,6 @@ Address.fromBuffer = function(buffer, network, type) { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Instantiate an address from an address string |
|
|
|
* |
|
|
|
* @param {String} str - An string of the bitcoin address |
|
|
@ -307,7 +306,6 @@ Address.fromString = function(str, network, type) { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Will return a validation error if exists |
|
|
|
* |
|
|
|
* @example |
|
|
@ -331,7 +329,6 @@ Address.getValidationError = function(data, network, type) { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Will return a boolean if an address is valid |
|
|
|
* |
|
|
|
* @example |
|
|
@ -365,19 +362,17 @@ Address.prototype.isPayToScriptHash = function() { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Will return a buffer representation of the address |
|
|
|
* |
|
|
|
* @returns {Buffer} Bitcoin address buffer |
|
|
|
*/ |
|
|
|
Address.prototype.toBuffer = function() { |
|
|
|
var version = new Buffer([networks[this.network][this.type]]); |
|
|
|
var version = new Buffer([this.network[this.type]]); |
|
|
|
var buf = Buffer.concat([version, this.hashBuffer]); |
|
|
|
return buf; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Will return a the string representation of the address |
|
|
|
* |
|
|
|
* @returns {String} Bitcoin address |
|
|
@ -387,7 +382,6 @@ Address.prototype.toString = function() { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* Will return a string formatted for the console |
|
|
|
* |
|
|
|
* @returns {String} Bitcoin address |
|
|
|