Commit 60a844e5 authored by Zhenzhen Zhan's avatar Zhenzhen Zhan
Browse files

Modify `Address` class

parent 66ed8cf8
......@@ -97,7 +97,10 @@ Address.prototype._classifyArguments = function(data, network, type) {
// transform and validate input data
if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 20) {
return Address._transformHash(data);
} else if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 21) {
} else if (
(data instanceof Buffer || data instanceof Uint8Array) &&
(data.length === 23 || data.length === 24)
) {
return Address._transformBuffer(data, network, type);
} else if (data instanceof PublicKey) {
return Address._transformPublicKey(data);
......@@ -162,8 +165,10 @@ Address._transformObject = function(data) {
Address._classifyFromVersion = function(buffer) {
var version = {};
var pubkeyhashNetwork = Networks.get(buffer[0], 'pubkeyhash');
var scripthashNetwork = Networks.get(buffer[0], 'scripthash');
var pubkeyhashNetwork = Networks.get(buffer.slice(0, 3), 'pubkeyAddressPrefix') ||
Networks.get(buffer.slice(0, 4), 'pubkeyAddressPrefix');
var scripthashNetwork = Networks.get(buffer.slice(0, 3), 'scriptAddressPrefix') ||
Networks.get(buffer.slice(0, 4), 'scriptAddressPrefix');
if (pubkeyhashNetwork) {
version.network = pubkeyhashNetwork;
......@@ -191,8 +196,8 @@ Address._transformBuffer = function(buffer, network, type) {
if (!(buffer instanceof Buffer) && !(buffer instanceof Uint8Array)) {
throw new TypeError('Address supplied is not a buffer.');
}
if (buffer.length !== 1 + 20) {
throw new TypeError('Address buffers must be exactly 21 bytes.');
if (buffer.length !== 3 + 20 && buffer.length !== 4 + 20) {
throw new TypeError('Address buffers must be 23 or 24 bytes.');
}
network = Networks.get(network);
......@@ -206,7 +211,7 @@ Address._transformBuffer = function(buffer, network, type) {
throw new TypeError('Address has mismatched type.');
}
info.hashBuffer = buffer.slice(1);
info.hashBuffer = buffer.slice(bufferVersion.network.name === 'livenet' ? 3 : 4);
info.network = bufferVersion.network;
info.type = bufferVersion.type;
return info;
......
......@@ -22,7 +22,7 @@ Network.prototype.toString = function toString() {
* @function
* @member Networks#get
* Retrieves the network associated with a magic number or string.
* @param {string|number|Network} arg
* @param {string|number|Buffer|Network} arg
* @param {string|Array} keys - if set, only check if the magic number associated with this name matches
* @return Network
*/
......@@ -35,7 +35,12 @@ function get(arg, keys) {
keys = [keys];
}
var containsArg = function(key) {
return networks[index][key] === arg;
if (arg instanceof Buffer && networks[index][key] instanceof Buffer) {
return networks[index][key].equals(arg);
}
else {
return networks[index][key] === arg;
}
};
for (var index in networks) {
if (_.any(keys, containsArg)) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment