2014-04-17 15:31:45 +02:00
|
|
|
var assert = require('assert')
|
2014-04-03 20:10:12 +02:00
|
|
|
var base58check = require('./base58check')
|
2014-05-05 07:31:40 +02:00
|
|
|
var networks = require('./networks')
|
2014-06-12 13:14:22 +02:00
|
|
|
var templates = require('./templates')
|
2014-05-05 07:31:40 +02:00
|
|
|
|
|
|
|
function findScriptTypeByVersion(queryVersion) {
|
|
|
|
for (var networkName in networks) {
|
|
|
|
var network = networks[networkName]
|
|
|
|
|
|
|
|
for (var versionName in network) {
|
|
|
|
var version = network[versionName]
|
|
|
|
|
|
|
|
if (version === queryVersion) {
|
2014-06-13 01:33:58 +02:00
|
|
|
return versionName.toLowerCase()
|
2014-05-05 07:31:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-17 06:39:15 +01:00
|
|
|
|
2014-04-17 15:31:45 +02:00
|
|
|
function Address(hash, version) {
|
2014-05-31 03:48:03 +02:00
|
|
|
assert(Buffer.isBuffer(hash), 'Expected Buffer, got ' + hash)
|
2014-04-17 22:48:40 +02:00
|
|
|
assert.strictEqual(hash.length, 20, 'Invalid hash length')
|
|
|
|
assert.strictEqual(version & 0xFF, version, 'Invalid version byte')
|
2013-10-21 20:00:31 +02:00
|
|
|
|
2014-04-17 15:31:45 +02:00
|
|
|
this.hash = hash
|
|
|
|
this.version = version
|
2014-03-25 20:57:19 +01:00
|
|
|
}
|
|
|
|
|
2014-04-19 20:38:33 +02:00
|
|
|
// Import functions
|
2014-04-17 15:31:45 +02:00
|
|
|
Address.fromBase58Check = function(string) {
|
2014-06-03 09:02:59 +02:00
|
|
|
var payload = base58check.decode(string)
|
|
|
|
var version = payload.readUInt8(0)
|
|
|
|
var hash = payload.slice(1)
|
2014-04-17 15:31:45 +02:00
|
|
|
|
2014-06-03 09:02:59 +02:00
|
|
|
return new Address(hash, version)
|
2014-03-25 20:57:19 +01:00
|
|
|
}
|
2013-10-07 14:21:00 +02:00
|
|
|
|
2014-05-05 07:31:40 +02:00
|
|
|
Address.fromScriptPubKey = function(script, network) {
|
|
|
|
network = network || networks.bitcoin
|
|
|
|
|
2014-06-12 13:14:22 +02:00
|
|
|
var type = templates.classifyScriptPubKey(script)
|
2014-05-05 07:31:40 +02:00
|
|
|
|
|
|
|
if (type === 'pubkeyhash') {
|
2014-06-12 13:14:22 +02:00
|
|
|
return new Address(script.chunks[2], network.pubKeyHash)
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2014-06-12 13:14:22 +02:00
|
|
|
} else if (type === 'scripthash') {
|
|
|
|
return new Address(script.chunks[1], network.scriptHash)
|
2014-05-05 07:31:40 +02:00
|
|
|
}
|
|
|
|
|
2014-05-28 05:17:04 +02:00
|
|
|
assert(false, type + ' has no matching Address')
|
2014-05-05 07:31:40 +02:00
|
|
|
}
|
|
|
|
|
2014-04-17 15:31:45 +02:00
|
|
|
// Export functions
|
|
|
|
Address.prototype.toBase58Check = function () {
|
2014-06-03 09:02:59 +02:00
|
|
|
var payload = new Buffer(21)
|
|
|
|
payload.writeUInt8(this.version, 0)
|
|
|
|
this.hash.copy(payload, 1)
|
|
|
|
|
|
|
|
return base58check.encode(payload)
|
2014-03-25 20:57:19 +01:00
|
|
|
}
|
2014-05-05 07:31:40 +02:00
|
|
|
|
|
|
|
Address.prototype.toScriptPubKey = function() {
|
|
|
|
var scriptType = findScriptTypeByVersion(this.version)
|
|
|
|
|
2014-06-13 01:33:58 +02:00
|
|
|
if (scriptType === 'pubkeyhash') {
|
2014-06-12 13:14:22 +02:00
|
|
|
return templates.createPubKeyHashScriptPubKey(this.hash)
|
2014-05-05 07:31:40 +02:00
|
|
|
|
2014-06-13 01:33:58 +02:00
|
|
|
} else if (scriptType === 'scripthash') {
|
2014-06-12 13:14:22 +02:00
|
|
|
return templates.createP2SHScriptPubKey(this.hash)
|
2014-05-05 07:31:40 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 01:33:58 +02:00
|
|
|
assert(false, this.toString() + ' has no matching Script')
|
2014-05-05 07:31:40 +02:00
|
|
|
}
|
|
|
|
|
2014-04-17 15:31:45 +02:00
|
|
|
Address.prototype.toString = Address.prototype.toBase58Check
|
2011-05-04 18:02:56 +02:00
|
|
|
|
2014-03-25 20:57:19 +01:00
|
|
|
module.exports = Address
|