diff --git a/lib/bcoin/env.js b/lib/bcoin/env.js index 918253dd..f4f52a02 100644 --- a/lib/bcoin/env.js +++ b/lib/bcoin/env.js @@ -146,6 +146,7 @@ function Environment(options) { this.script = require('./script'); this.stack = this.script.Stack; this.witness = this.script.Witness; + this.address = require('./address'); this.input = require('./input'); this.output = require('./output'); this.coin = require('./coin'); @@ -169,7 +170,7 @@ function Environment(options) { this.mempoolentry = this.mempool.MempoolEntry; this.keypair = require('./keypair'); this.hd = require('./hd'); - this.address = require('./address'); + this.keyring = require('./keyring'); this.wallet = require('./wallet'); this.walletdb = require('./walletdb'); this.provider = this.walletdb.Provider; diff --git a/lib/bcoin/address.js b/lib/bcoin/keyring.js similarity index 76% rename from lib/bcoin/address.js rename to lib/bcoin/keyring.js index 6ceeafa1..1a90e162 100644 --- a/lib/bcoin/address.js +++ b/lib/bcoin/keyring.js @@ -14,7 +14,7 @@ var BufferReader = require('./reader'); /** * Represents a key ring which amounts to an address. Used for {@link Wallet}. - * @exports Address + * @exports KeyRing * @constructor * @param {Object} options * @param {String?} options.label @@ -30,13 +30,13 @@ var BufferReader = require('./reader'); * @param {Boolean?} options.witness - Whether witness programs are enabled. */ -function Address(options) { +function KeyRing(options) { var i; - if (!(this instanceof Address)) - return new Address(options); + if (!(this instanceof KeyRing)) + return new KeyRing(options); - if (options instanceof Address) + if (options instanceof KeyRing) return options; if (!options) @@ -75,12 +75,12 @@ function Address(options) { } /** - * Test an object to see if it is an Address. + * Test an object to see if it is an KeyRing. * @param {Object} obj * @returns {Boolean} */ -Address.isAddress = function isAddress(obj) { +KeyRing.isKeyRing = function isKeyRing(obj) { return obj && Array.isArray(obj.keys) && typeof obj._getAddressMap === 'function'; @@ -91,7 +91,7 @@ Address.isAddress = function isAddress(obj) { * @returns {Base58Address} */ -Address.prototype.getID = function getID() { +KeyRing.prototype.getID = function getID() { return this.getKeyAddress(); }; @@ -100,7 +100,7 @@ Address.prototype.getID = function getID() { * @param {Buffer} key */ -Address.prototype.addKey = function addKey(key) { +KeyRing.prototype.addKey = function addKey(key) { if (utils.indexOf(this.keys, key) !== -1) return; @@ -114,7 +114,7 @@ Address.prototype.addKey = function addKey(key) { * @param {Buffer} key */ -Address.prototype.removeKey = function removeKey(key) { +KeyRing.prototype.removeKey = function removeKey(key) { var index = utils.indexOf(this.keys, key); if (index === -1) @@ -131,7 +131,7 @@ Address.prototype.removeKey = function removeKey(key) { * @returns {Buffer} */ -Address.prototype.getPrivateKey = function getPrivateKey(enc) { +KeyRing.prototype.getPrivateKey = function getPrivateKey(enc) { return this.key.getPrivateKey(enc); }; @@ -141,7 +141,7 @@ Address.prototype.getPrivateKey = function getPrivateKey(enc) { * @returns {Buffer} */ -Address.prototype.getPublicKey = function getPublicKey(enc) { +KeyRing.prototype.getPublicKey = function getPublicKey(enc) { return this.key.getPublicKey(enc); }; @@ -150,7 +150,7 @@ Address.prototype.getPublicKey = function getPublicKey(enc) { * @returns {Script} */ -Address.prototype.getScript = function getScript() { +KeyRing.prototype.getScript = function getScript() { var redeem; if (this.type !== 'multisig') @@ -175,7 +175,7 @@ Address.prototype.getScript = function getScript() { * @returns {Buffer} */ -Address.prototype.getProgram = function getProgram() { +KeyRing.prototype.getProgram = function getProgram() { var hash, program; if (!this.witness) @@ -183,10 +183,10 @@ Address.prototype.getProgram = function getProgram() { if (!this._program) { if (this.type === 'pubkeyhash') { - hash = Address.hash160(this.getPublicKey()); + hash = KeyRing.hash160(this.getPublicKey()); program = bcoin.script.createWitnessProgram(0, hash); } else if (this.type === 'multisig') { - hash = Address.sha256(this.getScript().encode()); + hash = KeyRing.sha256(this.getScript().encode()); program = bcoin.script.createWitnessProgram(0, hash); } else { assert(false, 'Unknown address type.'); @@ -204,12 +204,12 @@ Address.prototype.getProgram = function getProgram() { * @returns {Buffer} */ -Address.prototype.getProgramHash = function getProgramHash(enc) { +KeyRing.prototype.getProgramHash = function getProgramHash(enc) { if (!this.witness) return; if (!this._programHash) - this._programHash = Address.hash160(this.getProgram().encode()); + this._programHash = KeyRing.hash160(this.getProgram().encode()); return enc === 'hex' ? this._programHash.toString('hex') @@ -221,7 +221,7 @@ Address.prototype.getProgramHash = function getProgramHash(enc) { * @returns {Base58Address} */ -Address.prototype.getProgramAddress = function getProgramAddress() { +KeyRing.prototype.getProgramAddress = function getProgramAddress() { var hash, address; if (!this.witness) @@ -242,7 +242,7 @@ Address.prototype.getProgramAddress = function getProgramAddress() { * @returns {Buffer} */ -Address.prototype.getScriptHash = function getScriptHash(enc) { +KeyRing.prototype.getScriptHash = function getScriptHash(enc) { if (this.witness) return this.getScriptHash256(enc); return this.getScriptHash160(enc); @@ -254,12 +254,12 @@ Address.prototype.getScriptHash = function getScriptHash(enc) { * @returns {Buffer} */ -Address.prototype.getScriptHash160 = function getScriptHash256(enc) { +KeyRing.prototype.getScriptHash160 = function getScriptHash256(enc) { if (this.type !== 'multisig') return; if (!this._scriptHash160) - this._scriptHash160 = Address.hash160(this.getScript().encode()); + this._scriptHash160 = KeyRing.hash160(this.getScript().encode()); return enc === 'hex' ? this._scriptHash160.toString('hex') @@ -272,12 +272,12 @@ Address.prototype.getScriptHash160 = function getScriptHash256(enc) { * @returns {Buffer} */ -Address.prototype.getScriptHash256 = function getScriptHash256(enc) { +KeyRing.prototype.getScriptHash256 = function getScriptHash256(enc) { if (this.type !== 'multisig') return; if (!this._scriptHash256) - this._scriptHash256 = Address.sha256(this.getScript().encode()); + this._scriptHash256 = KeyRing.sha256(this.getScript().encode()); return enc === 'hex' ? this._scriptHash256.toString('hex') @@ -289,7 +289,7 @@ Address.prototype.getScriptHash256 = function getScriptHash256(enc) { * @returns {Base58Address} */ -Address.prototype.getScriptAddress = function getScriptAddress() { +KeyRing.prototype.getScriptAddress = function getScriptAddress() { var hash, address; if (this.type !== 'multisig') @@ -315,9 +315,9 @@ Address.prototype.getScriptAddress = function getScriptAddress() { * @returns {Buffer} */ -Address.prototype.getKeyHash = function getKeyHash(enc) { +KeyRing.prototype.getKeyHash = function getKeyHash(enc) { if (!this._hash) - this._hash = Address.hash160(this.getPublicKey()); + this._hash = KeyRing.hash160(this.getPublicKey()); return enc === 'hex' ? this._hash.toString('hex') @@ -329,7 +329,7 @@ Address.prototype.getKeyHash = function getKeyHash(enc) { * @returns {Base58Address} */ -Address.prototype.getKeyAddress = function getKeyAddress() { +KeyRing.prototype.getKeyAddress = function getKeyAddress() { var hash, address; if (!this._address) { @@ -350,7 +350,7 @@ Address.prototype.getKeyAddress = function getKeyAddress() { * @returns {Buffer} */ -Address.prototype.getHash = function getHash(enc) { +KeyRing.prototype.getHash = function getHash(enc) { if (this.type === 'multisig') return this.getScriptHash(enc); return this.getKeyHash(enc); @@ -361,13 +361,13 @@ Address.prototype.getHash = function getHash(enc) { * @returns {Base58Address} */ -Address.prototype.getAddress = function getAddress() { +KeyRing.prototype.getAddress = function getAddress() { if (this.type === 'multisig') return this.getScriptAddress(); return this.getKeyAddress(); }; -Address.prototype._getAddressMap = function _getAddressMap() { +KeyRing.prototype._getAddressMap = function _getAddressMap() { if (!this.addressMap) { this.addressMap = {}; @@ -390,7 +390,7 @@ Address.prototype._getAddressMap = function _getAddressMap() { * @returns {Boolean} */ -Address.prototype.ownInput = function ownInput(tx, index) { +KeyRing.prototype.ownInput = function ownInput(tx, index) { var addressMap = this._getAddressMap(); if (tx instanceof bcoin.input) @@ -406,7 +406,7 @@ Address.prototype.ownInput = function ownInput(tx, index) { * @returns {Boolean} */ -Address.prototype.ownOutput = function ownOutput(tx, index) { +KeyRing.prototype.ownOutput = function ownOutput(tx, index) { var addressMap = this._getAddressMap(); if (tx instanceof bcoin.output) @@ -425,7 +425,7 @@ Address.prototype.ownOutput = function ownOutput(tx, index) { * @returns {Number} Total number of scripts built. */ -Address.prototype.scriptInputs = function scriptInputs(tx, index) { +KeyRing.prototype.scriptInputs = function scriptInputs(tx, index) { var total = 0; var i, input; @@ -461,7 +461,7 @@ Address.prototype.scriptInputs = function scriptInputs(tx, index) { * @returns {Number} Total number of inputs signed. */ -Address.prototype.signInputs = function signInputs(tx, index, type) { +KeyRing.prototype.signInputs = function signInputs(tx, index, type) { var total = 0; var i, input; @@ -500,7 +500,7 @@ Address.prototype.signInputs = function signInputs(tx, index, type) { * @returns {Number} Total number of inputs scripts built and signed. */ -Address.prototype.sign = function sign(tx, index, type) { +KeyRing.prototype.sign = function sign(tx, index, type) { var total = 0; var i, input; @@ -529,59 +529,59 @@ Address.prototype.sign = function sign(tx, index, type) { return total; }; -Address.prototype.__defineGetter__('privateKey', function() { +KeyRing.prototype.__defineGetter__('privateKey', function() { return this.getPrivateKey(); }); -Address.prototype.__defineGetter__('publicKey', function() { +KeyRing.prototype.__defineGetter__('publicKey', function() { return this.getPublicKey(); }); -Address.prototype.__defineGetter__('script', function() { +KeyRing.prototype.__defineGetter__('script', function() { return this.getScript(); }); -Address.prototype.__defineGetter__('scriptHash', function() { +KeyRing.prototype.__defineGetter__('scriptHash', function() { return this.getScriptHash(); }); -Address.prototype.__defineGetter__('scriptHash160', function() { +KeyRing.prototype.__defineGetter__('scriptHash160', function() { return this.getScriptHash160(); }); -Address.prototype.__defineGetter__('scriptHash256', function() { +KeyRing.prototype.__defineGetter__('scriptHash256', function() { return this.getScriptHash256(); }); -Address.prototype.__defineGetter__('scriptAddress', function() { +KeyRing.prototype.__defineGetter__('scriptAddress', function() { return this.getScriptAddress(); }); -Address.prototype.__defineGetter__('program', function() { +KeyRing.prototype.__defineGetter__('program', function() { return this.getProgram(); }); -Address.prototype.__defineGetter__('programHash', function() { +KeyRing.prototype.__defineGetter__('programHash', function() { return this.getProgramHash(); }); -Address.prototype.__defineGetter__('programAddress', function() { +KeyRing.prototype.__defineGetter__('programAddress', function() { return this.getProgramAddress(); }); -Address.prototype.__defineGetter__('keyHash', function() { +KeyRing.prototype.__defineGetter__('keyHash', function() { return this.getKeyHash(); }); -Address.prototype.__defineGetter__('keyAddress', function() { +KeyRing.prototype.__defineGetter__('keyAddress', function() { return this.getKeyAddress(); }); -Address.prototype.__defineGetter__('hash', function() { +KeyRing.prototype.__defineGetter__('hash', function() { return this.getHash(); }); -Address.prototype.__defineGetter__('address', function() { +KeyRing.prototype.__defineGetter__('address', function() { return this.getAddress(); }); @@ -591,7 +591,7 @@ Address.prototype.__defineGetter__('address', function() { * @returns {Buffer} */ -Address.hash160 = function hash160(key) { +KeyRing.hash160 = function hash160(key) { return utils.ripesha(key); }; @@ -600,7 +600,7 @@ Address.hash160 = function hash160(key) { * @returns {Buffer} */ -Address.sha256 = function sha256(key) { +KeyRing.sha256 = function sha256(key) { return utils.sha256(key); }; @@ -613,7 +613,7 @@ Address.sha256 = function sha256(key) { * @throws Error on bad hash/prefix. */ -Address.compileHash = function compileHash(hash, type, version, network) { +KeyRing.compileHash = function compileHash(hash, type, version, network) { return bcoin.script.Address.toBase58(hash, type, version, network); }; @@ -625,13 +625,13 @@ Address.compileHash = function compileHash(hash, type, version, network) { * @returns {Base58Address} */ -Address.compileData = function compileData(data, type, version, network) { +KeyRing.compileData = function compileData(data, type, version, network) { if (type === 'witnessscripthash') - data = Address.sha256(data); + data = KeyRing.sha256(data); else - data = Address.hash160(data); + data = KeyRing.hash160(data); - return Address.compileHash(data, type, version, network); + return KeyRing.compileHash(data, type, version, network); }; /** @@ -641,7 +641,7 @@ Address.compileData = function compileData(data, type, version, network) { * @throws Parse error */ -Address.parse = function parse(address) { +KeyRing.parse = function parse(address) { return bcoin.script.Address.parseBase58(address); }; @@ -652,21 +652,21 @@ Address.parse = function parse(address) { * @returns {Boolean} */ -Address.validate = function validate(address, type) { +KeyRing.validate = function validate(address, type) { return bcoin.script.Address.validate(address, type); }; -Address.prototype.compileHash = function compileHash(hash, type, version) { - return Address.compileHash(hash, type, version, this.network); +KeyRing.prototype.compileHash = function compileHash(hash, type, version) { + return KeyRing.compileHash(hash, type, version, this.network); }; /** - * Convert an Address to a more json-friendly object. - * @param {String?} passphrase - Address passphrase + * Convert an KeyRing to a more json-friendly object. + * @param {String?} passphrase - KeyRing passphrase * @returns {Object} */ -Address.prototype.toJSON = function toJSON(passphrase) { +KeyRing.prototype.toJSON = function toJSON(passphrase) { var key = this.key; if (!(key instanceof bcoin.keypair)) @@ -692,19 +692,19 @@ Address.prototype.toJSON = function toJSON(passphrase) { }; /** - * Instantiate an Address from a jsonified transaction object. + * Instantiate an KeyRing from a jsonified transaction object. * @param {Object} json - The jsonified transaction object. - * @param {String?} passphrase - Address passphrase - * @returns {Address} + * @param {String?} passphrase - KeyRing passphrase + * @returns {KeyRing} */ -Address.fromJSON = function fromJSON(json, passphrase) { +KeyRing.fromJSON = function fromJSON(json, passphrase) { var w; assert.equal(json.v, 1); assert.equal(json.name, 'address'); - w = new Address({ + w = new KeyRing({ label: json.label, change: json.change, derived: json.derived, @@ -721,4 +721,4 @@ Address.fromJSON = function fromJSON(json, passphrase) { return w; }; -module.exports = Address; +module.exports = KeyRing; diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 90d130b8..219c2392 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -781,7 +781,7 @@ MTX.prototype.addOutput = function addOutput(address, value) { assert(this.ts === 0, 'Cannot modify a confirmed tx.'); - if ((address instanceof bcoin.wallet) || (address instanceof bcoin.address)) + if ((address instanceof bcoin.wallet) || (address instanceof bcoin.keyring)) address = address.getAddress(); if (typeof address === 'string') { diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index b97937de..758f26cc 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.address.parse(address).hash; + var hash = bcoin.script.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.address.parse(address).hash; + var hash = bcoin.script.Address.getHash(address); this.unwatch(hash); }; diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 20cd629e..1ad0d0d1 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -2696,7 +2696,7 @@ Script.createOutputScript = function createOutputScript(options) { options = {}; if (options.address) { - address = bcoin.address.parse(options.address); + address = bcoin.script.Address.parseBase58(options.address); if (address.type === 'pubkeyhash') script = Script.createPubkeyhash(address.hash); diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 9b808181..24c7e011 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -373,7 +373,7 @@ Wallet.prototype.getID = function getID() { /** * Create a new receiving address (increments receiveDepth). - * @returns {Address} + * @returns {KeyRing} */ Wallet.prototype.createReceive = function createReceive() { @@ -382,7 +382,7 @@ Wallet.prototype.createReceive = function createReceive() { /** * Create a new change address (increments receiveDepth). - * @returns {Address} + * @returns {KeyRing} */ Wallet.prototype.createChange = function createChange() { @@ -392,7 +392,7 @@ Wallet.prototype.createChange = function createChange() { /** * Create a new address (increments depth). * @param {Boolean} change - * @returns {Address} + * @returns {KeyRing} */ Wallet.prototype.createAddress = function createAddress(change) { @@ -424,7 +424,7 @@ Wallet.prototype.createAddress = function createAddress(change) { /** * Derive a receiving address at `index`. Do not increment depth. * @param {Number} index - * @returns {Address} + * @returns {KeyRing} */ Wallet.prototype.deriveReceive = function deriveReceive(index) { @@ -437,7 +437,7 @@ Wallet.prototype.deriveReceive = function deriveReceive(index) { /** * Derive a change address at `index`. Do not increment depth. * @param {Number} index - * @returns {Address} + * @returns {KeyRing} */ Wallet.prototype.deriveChange = function deriveChange(index) { @@ -451,7 +451,7 @@ Wallet.prototype.deriveChange = function deriveChange(index) { * Derive an address at `index`. Do not increment depth. * @param {Boolean} change - Whether the address on the change branch. * @param {Number} index - * @returns {Address} + * @returns {KeyRing} */ Wallet.prototype.deriveAddress = function deriveAddress(change, index) { @@ -506,7 +506,7 @@ Wallet.prototype.deriveAddress = function deriveAddress(change, index) { options.keys.push(key.publicKey); }, this); - address = new bcoin.address(options); + address = new bcoin.keyring(options); this.addressMap[address.getKeyHash('hex')] = data.path; @@ -830,7 +830,7 @@ Wallet.prototype.createTX = function createTX(options, outputs, callback) { * Derive an address for a single transaction input. * @param {TX} tx * @param {Number} index - * @returns {Address} + * @returns {KeyRing} */ Wallet.prototype.deriveInput = function deriveInput(tx, index) { @@ -841,7 +841,7 @@ Wallet.prototype.deriveInput = function deriveInput(tx, index) { * Derive necessary addresses for signing a transaction. * @param {TX|Input} tx * @param {Number?} index - Input index. - * @returns {Address[]} + * @returns {KeyRing[]} */ Wallet.prototype.deriveInputs = function deriveInputs(tx, index) {