From 86bc2227a4030eacd210f309d09773092bb36dbe Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 13 May 2016 13:33:18 -0700 Subject: [PATCH] get address working. --- lib/bcoin/address.js | 347 +++++++++++++++++++++++++++++++++++++++++++ lib/bcoin/chaindb.js | 4 +- lib/bcoin/input.js | 2 +- lib/bcoin/keyring.js | 112 ++++---------- lib/bcoin/output.js | 2 +- lib/bcoin/pool.js | 8 +- lib/bcoin/script.js | 297 +----------------------------------- test/wallet-test.js | 12 +- 8 files changed, 391 insertions(+), 393 deletions(-) create mode 100644 lib/bcoin/address.js diff --git a/lib/bcoin/address.js b/lib/bcoin/address.js new file mode 100644 index 00000000..709c79eb --- /dev/null +++ b/lib/bcoin/address.js @@ -0,0 +1,347 @@ +/*! + * address.js - address object for bcoin + * Copyright (c) 2014-2015, Fedor Indutny (MIT License) + * Copyright (c) 2014-2016, Christopher Jeffrey (MIT License). + * https://github.com/indutny/bcoin + */ + +var bcoin = require('./env'); +var bn = require('bn.js'); +var constants = bcoin.protocol.constants; +var networks = bcoin.protocol.network; +var utils = require('./utils'); +var assert = utils.assert; +var BufferWriter = require('./writer'); +var BufferReader = require('./reader'); +var Script = bcoin.script; + +/** + * Represents an address. + * @exports Address + * @constructor + * @param {Object} options + * @param {String?} options.hash + * @param {String?} options.type + * @param {String?} options.version + * @param {String?} options.network + */ + +function Address(options) { + if (!(this instanceof Address)) + return new Address(options); + + this.hash = options.hash; + this.type = options.type || 'pubkeyhash'; + this.version = options.version == null ? -1 : options.version; + this.network = bcoin.network.get(options.network).type; + + if (!Buffer.isBuffer(this.hash)) + this.hash = new Buffer(this.hash, 'hex'); +} + +Address.prototype.getHash = function getHash(enc) { + if (enc === 'hex') + return this.hash.toString(enc); + return this.hash; +}; + +Address.prototype.toBase58 = function toBase58(network) { + if (!network) + network = this.network; + + return Address.toBase58(this.hash, this.type, this.version, network); +}; + +/** + * Compile a hash to an address. + * @param {Hash|Buffer} hash + * @param {AddressType?} type + * @param {Number?} version - Witness version. + * @returns {Base58Address} + * @throws Error on bad hash/prefix. + */ + +Address.toBase58 = function toBase58(hash, type, version, network) { + var p, prefix; + + if (!Buffer.isBuffer(hash)) + hash = new Buffer(hash, 'hex'); + + if (!type) + type = 'pubkeyhash'; + + network = bcoin.network.get(network); + + prefix = network.address.prefixes[type]; + + if (!(version >= 0)) + version = network.address.versions[type]; + + assert(prefix != null, 'Not a valid address prefix.'); + + if (!(version >= 0)) + assert(hash.length === 20, 'Hash is the wrong size.'); + else if (version === 0 && type === 'witnesspubkeyhash') + assert(hash.length === 20, 'Hash is the wrong size.'); + else if (version === 0 && type === 'witnessscripthash') + assert(hash.length === 32, 'Hash is the wrong size.'); + + p = new BufferWriter(); + + p.writeU8(prefix); + if (version != null) { + p.writeU8(version); + p.writeU8(0) + } + p.writeBytes(hash); + p.writeChecksum(); + + return utils.toBase58(p.render()); +}; + +/** + * Parse a base58 address. + * @param {Base58Address} address + * @returns {ParsedAddress} + * @throws Parse error + */ + +Address.parseBase58 = function parseBase58(address) { + var i, prefix, type, version, hash, network; + + if (!Buffer.isBuffer(address)) + address = utils.fromBase58(address); + + p = new BufferReader(address, true); + prefix = p.readU8(); + + for (i = 0; i < networks.types.length; i++) { + network = networks[networks.types[i]]; + type = network.address.prefixesByVal[prefix]; + if (type != null) + break; + } + + assert(type != null, 'Unknown address prefix.'); + + version = network.address.versions[type]; + + if (version != null) { + version = p.readU8(); + assert(version >= 0 && version <= 16, 'Bad program version.'); + assert(p.readU8() === 0, 'Address version padding is non-zero.'); + } + + if (type === 'witnessscripthash') + hash = p.readBytes(32); + else + hash = p.readBytes(20); + + p.verifyChecksum(); + + return { + network: network.type, + type: type, + hash: hash, + version: version == null ? -1 : version + }; +}; + +Address.fromBase58 = function fromBase58(addr) { + return new Address(Address.parseBase58(addr)); +}; + +Address.parseScript = function parseScript(script) { + var program; + + if (script.isWitnessProgram()) { + program = script.getWitnessProgram(); + if (!program.type || program.type === 'unknown') + return; + return { + hash: program.data, + type: program.type, + version: program.version + }; + } + + if (script.isPubkey()) { + hash = utils.ripesha(script.code[0]); + return { hash: hash, type: 'pubkeyhash', version: -1 }; + } + + if (script.isPubkeyhash()) { + hash = script.code[2]; + return { hash: hash, type: 'pubkeyhash', version: -1 }; + } + + if (script.isMultisig()) { + hash = utils.ripesha(script.encode()); + return { hash: hash, type: 'scripthash', version: -1 }; + } + + if (script.isScripthash()) { + hash = script.code[1]; + return { hash: hash, type: 'scripthash', version: -1 }; + } +}; + +Address.parseInput = function parseInput(code, witness) { + var hash; + + if (Script.isPubkeyInput(code)) + return; + + if (Script.isPubkeyhashInput(code)) { + hash = utils.ripesha(code[1]); + if (witness) + return { hash: hash, type: 'witnesspubkeyhash', version: 0 }; + return { hash: hash, type: 'pubkeyhash', version: -1 }; + } + + if (Script.isMultisigInput(code, witness)) + return; + + if (Script.isScripthashInput(code)) { + if (witness) { + hash = utils.sha256(code[code.length - 1]); + return { hash: hash, type: 'witnessscripthash', version: 0 }; + } + hash = utils.ripesha(code[code.length - 1]); + return { hash: hash, type: 'scripthash', version: -1 }; + } +}; + +Address.parseWitness = function parseWitness(witness) { + return Address.parseInput(witness.items, true); +}; + +Address.parseInputScript = function parseInputScript(script) { + return Address.parseInput(script.code, false); +}; + +Address.fromWitness = function fromWitness(witness) { + var data = Address.parseWitness(witness); + + if (!data) + return; + + return new Address(data); +}; + +Address.fromInputScript = function fromInputScript(script) { + var data = Address.parseInputScript(script); + + if (!data) + return; + + return new Address(data); +}; + +Address.fromScript = function fromScript(script) { + var data = Address.parseScript(script); + + if (!data) + return; + + return new Address(data); +}; + +Address.parseHash = function parseHash(hash, type, version) { + return { + hash: hash, + type: type || 'pubkeyhash', + version: version == null ? -1 : version + }; +}; + +Address.fromHash = function fromHash(hash, type, version) { + return new Address(Address.parseHash(hash, type, version)); +}; + +/** + * Hash data and compile hash to an address. + * @param {Hash|Buffer} hash + * @param {AddressType?} type + * @param {Number?} version - Witness program version. + * @returns {Base58Address} + */ + +Address.parseData = function parseData(data, type, version) { + if (type === 'witnessscripthash') + data = utils.sha256(data); + else + data = utils.ripesha(data); + return Address.parseHash(data, type, version); +}; + +Address.fromData = function fromData(data, type, version) { + return new Address(Address.parseData(data, type, version)); +}; + +Address.toScript = function toScript() { + if (this.type === 'pubkeyhash') + return Script.createPubkeyhash(this.hash); + if (this.type === 'scripthash') + return Script.createScripthash(this.hash); + if (this.version !== -1) + return Script.createWitnessProgram(this.version, this.hash); + assert(false, 'Bad type.'); +}; + +/** + * Validate an address, optionally test against a type. + * @param {String} address - Can be of any type in reality. + * @param {AddressType} + * @returns {Boolean} + */ + +Address.validate = function validate(address, type) { + if (!address) + return false; + + if (!Buffer.isBuffer(address) && typeof address !== 'string') + return false; + + try { + address = Address.parseBase58(address); + } catch (e) { + return false; + } + + if (type && address.type !== type) + return false; + + return true; +}; + +Address.getHash = function getHash(data) { + var hash; + + if (data instanceof Address) { + hash = data.hash; + } else { + try { + hash = Address.parseBase58(data).hash; + } catch (e) { + return; + } + } + + return hash.toString('hex'); +}; + +// Address.prototype.toString = function toString() { +// return this.toBase58(); +// }; + +Address.prototype.inspect = function inspect() { + return { + hash: this.getHash('hex'), + type: this.type, + version: this.version, + address: this.toBase58() + }; +}; + +module.exports = Address; diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 449b6ee9..74ef4228 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -1110,7 +1110,7 @@ ChainDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, call addresses = utils.uniq(addresses); utils.forEachSerial(addresses, function(address, next) { - address = bcoin.script.Address.getHash(address); + address = bcoin.address.getHash(address); if (!address) return next(); self.db.lookup({ @@ -1153,7 +1153,7 @@ ChainDB.prototype.getTXByAddress = function getTXByAddress(addresses, callback) addresses = utils.uniq(addresses); utils.forEachSerial(addresses, function(address, next) { - address = bcoin.script.Address.getHash(address); + address = bcoin.address.getHash(address); if (!address) return next(); self.db.lookup({ diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index fb7a1f0f..e4ab3487 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -148,7 +148,7 @@ Input.prototype.getSubtype = function getSubtype() { * Get the previous output script's address. Will "guess" * based on the input script and/or witness if coin * is not available. - * @returns {ScriptAddress?} address + * @returns {Address?} address */ Input.prototype.getAddress = function getAddress() { diff --git a/lib/bcoin/keyring.js b/lib/bcoin/keyring.js index 1a90e162..28da7658 100644 --- a/lib/bcoin/keyring.js +++ b/lib/bcoin/keyring.js @@ -1,5 +1,5 @@ /*! - * address.js - address object for bcoin + * keyring.js - keyring object for bcoin * Copyright (c) 2014-2015, Fedor Indutny (MIT License) * Copyright (c) 2014-2016, Christopher Jeffrey (MIT License). * https://github.com/indutny/bcoin @@ -183,10 +183,10 @@ KeyRing.prototype.getProgram = function getProgram() { if (!this._program) { if (this.type === 'pubkeyhash') { - hash = KeyRing.hash160(this.getPublicKey()); + hash = utils.ripesha(this.getPublicKey()); program = bcoin.script.createWitnessProgram(0, hash); } else if (this.type === 'multisig') { - hash = KeyRing.sha256(this.getScript().encode()); + hash = utils.sha256(this.getScript().encode()); program = bcoin.script.createWitnessProgram(0, hash); } else { assert(false, 'Unknown address type.'); @@ -209,7 +209,7 @@ KeyRing.prototype.getProgramHash = function getProgramHash(enc) { return; if (!this._programHash) - this._programHash = KeyRing.hash160(this.getProgram().encode()); + this._programHash = utils.ripesha(this.getProgram().encode()); return enc === 'hex' ? this._programHash.toString('hex') @@ -229,7 +229,7 @@ KeyRing.prototype.getProgramAddress = function getProgramAddress() { if (!this._programAddress) { hash = this.getProgramHash(); - address = this.compileHash(hash, 'scripthash'); + address = this.compile(hash, 'scripthash'); this._programAddress = address; } @@ -259,7 +259,7 @@ KeyRing.prototype.getScriptHash160 = function getScriptHash256(enc) { return; if (!this._scriptHash160) - this._scriptHash160 = KeyRing.hash160(this.getScript().encode()); + this._scriptHash160 = utils.ripesha(this.getScript().encode()); return enc === 'hex' ? this._scriptHash160.toString('hex') @@ -277,7 +277,7 @@ KeyRing.prototype.getScriptHash256 = function getScriptHash256(enc) { return; if (!this._scriptHash256) - this._scriptHash256 = KeyRing.sha256(this.getScript().encode()); + this._scriptHash256 = utils.sha256(this.getScript().encode()); return enc === 'hex' ? this._scriptHash256.toString('hex') @@ -298,10 +298,10 @@ KeyRing.prototype.getScriptAddress = function getScriptAddress() { if (!this._scriptAddress) { if (this.witness) { hash = this.getScriptHash256(); - address = this.compileHash(hash, 'witnessscripthash', 0); + address = this.compile(hash, 'witnessscripthash', 0); } else { hash = this.getScriptHash160(); - address = this.compileHash(hash, 'scripthash'); + address = this.compile(hash, 'scripthash'); } this._scriptAddress = address; } @@ -317,7 +317,7 @@ KeyRing.prototype.getScriptAddress = function getScriptAddress() { KeyRing.prototype.getKeyHash = function getKeyHash(enc) { if (!this._hash) - this._hash = KeyRing.hash160(this.getPublicKey()); + this._hash = utils.ripesha(this.getPublicKey()); return enc === 'hex' ? this._hash.toString('hex') @@ -335,15 +335,28 @@ KeyRing.prototype.getKeyAddress = function getKeyAddress() { if (!this._address) { hash = this.getKeyHash(); if (this.witness) - address = this.compileHash(hash, 'witnesspubkeyhash', 0); + address = this.compile(hash, 'witnesspubkeyhash', 0); else - address = this.compileHash(hash, 'pubkeyhash'); + address = this.compile(hash, 'pubkeyhash'); this._address = address; } return this._address; }; +/** + * Compile a hash to an address. + * @param {Hash|Buffer} hash + * @param {AddressType?} type + * @param {Number?} version - Witness version. + * @returns {Base58Address} + * @throws Error on bad hash/prefix. + */ + +KeyRing.prototype.compile = function compile(hash, type, version) { + return bcoin.address.toBase58(hash, type, version, this.network); +}; + /** * Get hash. * @param {String?} enc - `"hex"` or `null`. @@ -585,81 +598,6 @@ KeyRing.prototype.__defineGetter__('address', function() { return this.getAddress(); }); -/** - * Perform a hash160 (sha256 + ripemd160) - * @param {Buffer} key - * @returns {Buffer} - */ - -KeyRing.hash160 = function hash160(key) { - return utils.ripesha(key); -}; - -/** - * @param {Buffer} key - * @returns {Buffer} - */ - -KeyRing.sha256 = function sha256(key) { - return utils.sha256(key); -}; - -/** - * Compile a hash to an address. - * @param {Hash|Buffer} hash - * @param {AddressType?} type - * @param {Number?} version - Witness version. - * @returns {Base58Address} - * @throws Error on bad hash/prefix. - */ - -KeyRing.compileHash = function compileHash(hash, type, version, network) { - return bcoin.script.Address.toBase58(hash, type, version, network); -}; - -/** - * Hash data and compile hash to an address. - * @param {Hash|Buffer} hash - * @param {AddressType?} type - * @param {Number?} version - Witness program version. - * @returns {Base58Address} - */ - -KeyRing.compileData = function compileData(data, type, version, network) { - if (type === 'witnessscripthash') - data = KeyRing.sha256(data); - else - data = KeyRing.hash160(data); - - return KeyRing.compileHash(data, type, version, network); -}; - -/** - * Parse a base58 address. - * @param {Base58Address} address - * @returns {ParsedAddress} - * @throws Parse error - */ - -KeyRing.parse = function parse(address) { - return bcoin.script.Address.parseBase58(address); -}; - -/** - * Validate an address, optionally test against a type. - * @param {String} address - Can be of any type in reality. - * @param {AddressType} - * @returns {Boolean} - */ - -KeyRing.validate = function validate(address, type) { - return bcoin.script.Address.validate(address, type); -}; - -KeyRing.prototype.compileHash = function compileHash(hash, type, version) { - return KeyRing.compileHash(hash, type, version, this.network); -}; - /** * Convert an KeyRing to a more json-friendly object. * @param {String?} passphrase - KeyRing passphrase diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index 8b0450b0..9f068c3d 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -70,7 +70,7 @@ Output.prototype.getType = function getType() { /** * Get the address. - * @returns {ScriptAddress} address + * @returns {Address} address */ Output.prototype.getAddress = function getAddress() { diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 758f26cc..86412658 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -1392,7 +1392,7 @@ Pool.prototype.removeWallet = function removeWallet(wallet) { */ Pool.prototype.watchAddress = function watchAddress(address) { - var hash = bcoin.script.Address.getHash(address); + var hash = bcoin.address.getHash(address); this.watch(hash); }; @@ -1402,7 +1402,7 @@ Pool.prototype.watchAddress = function watchAddress(address) { */ Pool.prototype.unwatchAddress = function unwatchAddress(address) { - var hash = bcoin.script.Address.getHash(address); + var hash = bcoin.address.getHash(address); this.unwatch(hash); }; @@ -1413,7 +1413,7 @@ Pool.prototype.unwatchAddress = function unwatchAddress(address) { Pool.prototype.watchWallet = function watchWallet(wallet) { Object.keys(wallet.addressMap).forEach(function(address) { - this.watchAddress(address); + this.watch(new Buffer(address, 'hex')); }, this); }; @@ -1424,7 +1424,7 @@ Pool.prototype.watchWallet = function watchWallet(wallet) { Pool.prototype.unwatchWallet = function unwatchWallet(wallet) { Object.keys(wallet.addressMap).forEach(function(address) { - this.unwatchAddress(address); + this.unwatch(new Buffer(address, 'hex')); }, this); }; diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 1ad0d0d1..fc3b4bae 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -8,7 +8,6 @@ var bcoin = require('./env'); var bn = require('bn.js'); var constants = bcoin.protocol.constants; -var networks = bcoin.protocol.network; var utils = require('./utils'); var assert = utils.assert; var BufferWriter = require('./writer'); @@ -118,7 +117,7 @@ Witness.prototype.getInputType = function getInputType() { */ Witness.prototype.getInputAddress = function getInputAddress(network) { - return Address.fromWitness(this); + return bcoin.address.fromWitness(this); }; /** @@ -2356,7 +2355,7 @@ Script.prototype.getSize = function getSize() { */ Script.prototype.getInputAddress = function getInputAddress() { - return Address.fromInputScript(this); + return bcoin.address.fromInputScript(this); }; /** @@ -2367,7 +2366,7 @@ Script.prototype.getInputAddress = function getInputAddress() { */ Script.prototype.getAddress = function getAddress() { - return Address.fromScript(this); + return bcoin.address.fromScript(this); }; /** @@ -2696,7 +2695,8 @@ Script.createOutputScript = function createOutputScript(options) { options = {}; if (options.address) { - address = bcoin.script.Address.parseBase58(options.address); + if (typeof options.address === 'string') + address = bcoin.address.parseBase58(options.address); if (address.type === 'pubkeyhash') script = Script.createPubkeyhash(address.hash); @@ -4214,290 +4214,3 @@ Script.Witness = Witness; Script.Stack = Stack; module.exports = Script; - -function Address(options) { - if (!(this instanceof Address)) - return new Address(options); - - this.hash = options.hash; - this.type = options.type || 'pubkeyhash'; - this.version = options.version == null ? -1 : options.version; - this.network = options.network; - - if (!Buffer.isBuffer(this.hash)) - this.hash = new Buffer(this.hash, 'hex'); -} - -Address.prototype.getHash = function getHash(enc) { - if (enc === 'hex') - return this.hash.toString(enc); - return this.hash; -}; - -Address.prototype.toBase58 = function toBase58(network) { - if (!network) - network = this.network; - - return Address.toBase58(this.hash, this.type, this.version, network); -}; - -Address.toBase58 = function toBase58(hash, type, version, network) { - var p, prefix; - - if (!Buffer.isBuffer(hash)) - hash = new Buffer(hash, 'hex'); - - if (!type) - type = 'pubkeyhash'; - - network = bcoin.network.get(network); - - prefix = network.address.prefixes[type]; - - if (!(version >= 0)) - version = network.address.versions[type]; - - assert(prefix != null, 'Not a valid address prefix.'); - - if (!(version >= 0)) - assert(hash.length === 20, 'Hash is the wrong size.'); - else if (version === 0 && type === 'witnesspubkeyhash') - assert(hash.length === 20, 'Hash is the wrong size.'); - else if (version === 0 && type === 'witnessscripthash') - assert(hash.length === 32, 'Hash is the wrong size.'); - - p = new BufferWriter(); - - p.writeU8(prefix); - if (version != null) { - p.writeU8(version); - p.writeU8(0) - } - p.writeBytes(hash); - p.writeChecksum(); - - return utils.toBase58(p.render()); -}; - -Address.parseBase58 = function parseBase58(address) { - var i, prefix, type, version, hash, network; - - if (!Buffer.isBuffer(address)) - address = utils.fromBase58(address); - - p = new BufferReader(address, true); - prefix = p.readU8(); - - for (i = 0; i < networks.types.length; i++) { - network = networks[networks.types[i]]; - type = network.address.prefixesByVal[prefix]; - if (type != null) - break; - } - - assert(type != null, 'Unknown address prefix.'); - - version = network.address.versions[type]; - - if (version != null) { - version = p.readU8(); - assert(version >= 0 && version <= 16, 'Bad program version.'); - assert(p.readU8() === 0, 'Address version padding is non-zero.'); - } - - if (type === 'witnessscripthash') - hash = p.readBytes(32); - else - hash = p.readBytes(20); - - p.verifyChecksum(); - - return { - network: network.type, - type: type, - hash: hash, - version: version == null ? -1 : version - }; -}; - -Address.fromBase58 = function fromBase58(addr) { - return new Address(Address.parseBase58(addr)); -}; - -Address.parseScript = function parseScript(script) { - var program; - - if (script.isWitnessProgram()) { - program = script.getWitnessProgram(); - if (!program.type || program.type === 'unknown') - return; - return { - hash: program.data, - type: program.type, - version: program.version - }; - } - - if (script.isPubkey()) { - hash = utils.ripesha(script.code[0]); - return { hash: hash, type: 'pubkeyhash', version: -1 }; - } - - if (script.isPubkeyhash()) { - hash = script.code[2]; - return { hash: hash, type: 'pubkeyhash', version: -1 }; - } - - if (script.isMultisig()) { - hash = utils.ripesha(script.encode()); - return { hash: hash, type: 'scripthash', version: -1 }; - } - - if (script.isScripthash()) { - hash = script.code[1]; - return { hash: hash, type: 'scripthash', version: -1 }; - } -}; - -Address.parseInput = function parseInput(code, witness) { - var hash; - - if (Script.isPubkeyInput(code)) - return; - - if (Script.isPubkeyhashInput(code)) { - hash = utils.ripesha(code[1]); - if (witness) - return { hash: hash, type: 'witnesspubkeyhash', version: 0 }; - return { hash: hash, type: 'pubkeyhash', version: -1 }; - } - - if (Script.isMultisigInput(code, witness)) - return; - - if (Script.isScripthashInput(code)) { - if (witness) { - hash = utils.sha256(code[code.length - 1]); - return { hash: hash, type: 'witnessscripthash', version: 0 }; - } - hash = utils.ripesha(code[code.length - 1]); - return { hash: hash, type: 'scripthash', version: -1 }; - } -}; - -Address.parseWitness = function parseWitness(witness) { - return Address.parseInput(witness.items, true); -}; - -Address.parseInputScript = function parseInputScript(script) { - return Address.parseInput(script.code, false); -}; - -Address.fromWitness = function fromWitness(witness) { - var data = Address.parseWitness(witness); - - if (!data) - return; - - return new Address(data); -}; - -Address.fromInputScript = function fromInputScript(script) { - var data = Address.parseInputScript(script); - - if (!data) - return; - - return new Address(data); -}; - -Address.fromScript = function fromScript(script) { - var data = Address.parseScript(script); - - if (!data) - return; - - return new Address(data); -}; - -Address.parseHash = function parseHash(hash, type, version) { - return { - hash: hash, - type: type || 'pubkeyhash', - version: version == null ? -1 : version - }; -}; - -Address.fromHash = function fromHash(hash, type, version) { - return new Address(Address.parseHash(hash, type, version)); -}; - -Address.parseData = function parseData(data, type, version) { - if (type === 'witnessscripthash') - data = utils.sha256(data); - else - data = utils.ripesha(data); - return Address.parseHash(data, type, version); -}; - -Address.fromData = function fromData(data, type, version) { - return new Address(Address.parseData(data, type, version)); -}; - -Address.toScript = function toScript() { - if (this.type === 'pubkeyhash') - return Script.createPubkeyhash(this.hash); - if (this.type === 'scripthash') - return Script.createScripthash(this.hash); - assert(false, 'Bad type.'); -}; - -Address.validate = function validate(address, type) { - if (!address) - return false; - - if (!Buffer.isBuffer(address) && typeof address !== 'string') - return false; - - try { - address = Address.parseBase58(address); - } catch (e) { - return false; - } - - if (type && address.type !== type) - return false; - - return true; -}; - -Address.getHash = function getHash(data) { - var hash; - - if (data instanceof Address) { - hash = data.hash; - } else { - try { - hash = Address.parseBase58(data).hash; - } catch (e) { - return; - } - } - - return hash.toString('hex'); -}; - -// Address.prototype.toString = function toString() { -// return this.toBase58(); -// }; - -Address.prototype.inspect = function inspect() { - return { - hash: this.getHash('hex'), - type: this.type, - version: this.version, - address: this.toBase58() - }; -}; - -Script.Address = Address; diff --git a/test/wallet-test.js b/test/wallet-test.js index 9d12358f..3526899c 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -61,9 +61,9 @@ describe('Wallet', function() { assert.ifError(err); if (witness) - assert(bcoin.address.parse(w.getAddress()).type === 'witnesspubkeyhash'); + assert(bcoin.address.parseBase58(w.getAddress()).type === 'witnesspubkeyhash'); else - assert(bcoin.address.parse(w.getAddress()).type === 'pubkeyhash'); + assert(bcoin.address.parseBase58(w.getAddress()).type === 'pubkeyhash'); // Input transcation var src = bcoin.mtx({ @@ -74,7 +74,7 @@ describe('Wallet', function() { : w.getAddress() }, { value: 5460 * 2, - address: bcoin.address.compileData(new Buffer([])) + address: bcoin.address.fromData(new Buffer([])).toBase58() }] }); @@ -124,7 +124,7 @@ describe('Wallet', function() { keys: [ w.getPublicKey(), k2.derive('m/0/0').publicKey ] }, { value: 5460 * 2, - address: bcoin.address.compileData(new Buffer([])) + address: bcoin.address.fromData(new Buffer([])).toBase58() }] }); src.addInput(dummyInput); @@ -432,9 +432,9 @@ describe('Wallet', function() { var addr = w1.getAddress(); if (witness) - assert(bcoin.address.parse(addr).type === 'witnessscripthash'); + assert(bcoin.address.parseBase58(addr).type === 'witnessscripthash'); else - assert(bcoin.address.parse(addr).type === 'scripthash'); + assert(bcoin.address.parseBase58(addr).type === 'scripthash'); assert.equal(w1.getAddress(), addr); assert.equal(w2.getAddress(), addr);