3 changed files with 109 additions and 153 deletions
@ -1,78 +1,64 @@ |
|||||
var base58check = require('./base58check'); |
var base58check = require('./base58check'); |
||||
var constants = require('./constants'); |
var constants = require('./constants'); |
||||
|
|
||||
function Address(str) { |
function Address(hash, network, type) { |
||||
if (!str) { |
this.hash = hash; |
||||
this.buf = undefined; |
this.network = network; |
||||
return; |
this.type = type; |
||||
} |
|
||||
if (typeof str !== 'string') |
|
||||
throw new Error('address: Input must be a string, or undefined'); |
|
||||
this.fromString(str); |
|
||||
}; |
|
||||
|
|
||||
Address.prototype.getNetwork = function() { |
|
||||
if (this.buf[0] === constants.mainnet.pubkeyHash || this.buf[0] === constants.mainnet.p2sh) |
|
||||
return 'mainnet'; |
|
||||
else if (this.buf[0] === constants.testnet.pubkeyHash || this.buf[0] === constants.testnet.p2sh) |
|
||||
return 'testnet'; |
|
||||
else |
|
||||
return 'unknown'; |
|
||||
}; |
}; |
||||
|
|
||||
Address.prototype.getHash = function() { |
Address.prototype.fromString = function(str) { |
||||
var pubkeyHash = this.buf.slice(1); |
var buf = base58check.decode(str); |
||||
if (pubkeyHash.length === 20) |
if (buf.length !== 1 + 20) |
||||
return pubkeyHash; |
throw new Error('address: Address buffers must be exactly 21 bytes'); |
||||
else |
var version = buf[0]; |
||||
throw new Error('address: Hash must be exactly 20 bytes'); |
if (version === constants['mainnet']['pubkeyhash']) { |
||||
}; |
this.network = 'mainnet'; |
||||
|
this.type = 'pubkeyhash'; |
||||
|
} else if (version === constants['mainnet']['p2sh']) { |
||||
|
this.network = 'mainnet'; |
||||
|
this.type = 'p2sh'; |
||||
|
} else if (version === constants['testnet']['pubkeyhash']) { |
||||
|
this.network = 'testnet'; |
||||
|
this.type = 'pubkeyhash'; |
||||
|
} else if (version === constants['testnet']['p2sh']) { |
||||
|
this.network = 'testnet'; |
||||
|
this.type = 'p2sh'; |
||||
|
} else { |
||||
|
this.network = 'unknown'; |
||||
|
this.type = 'unknown'; |
||||
|
} |
||||
|
|
||||
Address.prototype.getType = function() { |
this.hash = buf.slice(1); |
||||
if (this.buf[0] === constants.mainnet.pubkeyHash || this.buf[0] === constants.testnet.pubkeyHash) |
} |
||||
return 'pubkeyHash'; |
|
||||
else if (this.buf[0] === constants.mainnet.p2sh || this.buf[0] === constants.testnet.p2sh) |
|
||||
return 'p2sh'; |
|
||||
else |
|
||||
return 'unknown'; |
|
||||
}; |
|
||||
|
|
||||
Address.prototype.isValid = function() { |
Address.prototype.isValid = function() { |
||||
if (Buffer.isBuffer(this.buf) && this.buf.length === 1 + 20) |
try { |
||||
|
this.validate(); |
||||
return true; |
return true; |
||||
else |
} catch (e) { |
||||
return false; |
return false; |
||||
|
} |
||||
}; |
}; |
||||
|
|
||||
Address.prototype.setBuf = function(buf, network, type) { |
Address.prototype.toBuffer = function() { |
||||
var version; |
version = new Buffer([constants[this.network][this.type]]); |
||||
if (!Buffer.isBuffer(buf)) |
var buf = Buffer.concat([version, this.hash]); |
||||
throw new Error('address: buf must be a buffer'); |
return buf; |
||||
if (buf.length !== 20) |
|
||||
throw new Error('address: buf must be 20 bytes'); |
|
||||
if (typeof network === 'undefined') |
|
||||
throw new Error('address: Must specify network ("mainnet" or "testnet")'); |
|
||||
if (typeof type === 'undefined') |
|
||||
throw new Error('address: Must specify type ("pubkeyHash" or "p2sh")'); |
|
||||
if (network !== 'mainnet' && network !== 'testnet') |
|
||||
throw new Error('address: Unknown network'); |
|
||||
if (type !== 'pubkeyHash' && type !== 'p2sh') |
|
||||
throw new Error('address: Unknown type'); |
|
||||
|
|
||||
version = new Buffer([constants[network][type]]); |
|
||||
|
|
||||
this.buf = Buffer.concat([version, buf]); |
|
||||
}; |
}; |
||||
|
|
||||
Address.prototype.fromString = function(str) { |
|
||||
var buf = base58check.decode(str); |
|
||||
if (buf.length !== 1 + 20) |
|
||||
throw new Error('address: Addresses must be exactly 21 bytes'); |
|
||||
this.buf = buf; |
|
||||
} |
|
||||
|
|
||||
Address.prototype.toString = function() { |
Address.prototype.toString = function() { |
||||
return base58check.encode(this.buf); |
return base58check.encode(this.toBuffer()); |
||||
|
}; |
||||
|
|
||||
|
Address.prototype.validate = function() { |
||||
|
if (!Buffer.isBuffer(this.hash) || this.hash.length !== 20) |
||||
|
throw new Error('address: hash must be a buffer of 20 bytes'); |
||||
|
if (this.network !== 'mainnet' && this.network !== 'testnet') |
||||
|
throw new Error('address: network must be "mainnet" or "testnet"'); |
||||
|
if (this.type !== 'pubkeyhash' && this.type !== 'p2sh') |
||||
|
throw new Error('address: type must be "pubkeyhash" or "p2sh"'); |
||||
|
return this; |
||||
}; |
}; |
||||
|
|
||||
module.exports = Address; |
module.exports = Address; |
||||
|
Loading…
Reference in new issue