From 579a98a5316a51b8bf59be7e15653ffd9a06c49d Mon Sep 17 00:00:00 2001 From: Nodar Chkuaselidze Date: Thu, 15 Nov 2018 17:39:25 +0400 Subject: [PATCH] address: minor fixes --- lib/net/pool.js | 2 +- lib/primitives/address.js | 9 ++++++--- lib/wallet/http.js | 12 ++++++------ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/net/pool.js b/lib/net/pool.js index 607d05811..cc3854c48 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -3152,7 +3152,7 @@ class Pool extends EventEmitter { watchAddress(address) { if (typeof address === 'string') - address = Address.fromString(address); + address = Address.fromString(address, this.network); const hash = Address.getHash(address); this.watch(hash); diff --git a/lib/primitives/address.js b/lib/primitives/address.js index 5f2bb315d..c7c63dc49 100644 --- a/lib/primitives/address.js +++ b/lib/primitives/address.js @@ -384,6 +384,7 @@ class Address { const addr = bech32.decode(data); + // make sure HRP is correct. Network.fromBech32(addr.hrp, network); return this.fromHash(addr.hash, type, addr.version); @@ -793,12 +794,11 @@ class Address { /** * Get the hash of a base58 address or address-related object. * @param {String|Address|Hash} data - * @param {String?} enc - * @param {Network?} network + * @param {String?} enc - Can be `"hex"` or `null`. * @returns {Hash} */ - static getHash(data, network) { + static getHash(data, enc) { if (!data) throw new Error('Object is not an address.'); @@ -814,6 +814,9 @@ class Address { throw new Error('Object is not an address.'); } + if (enc === 'hex') + return hash.toString('hex'); + return hash; } diff --git a/lib/wallet/http.js b/lib/wallet/http.js index 26a16540b..5ca1056bc 100644 --- a/lib/wallet/http.js +++ b/lib/wallet/http.js @@ -377,7 +377,7 @@ class HTTP extends Server { const passphrase = valid.str('passphrase'); const pub = valid.buf('publicKey'); const priv = valid.str('privateKey'); - const b58 = valid.str('address'); + const address = valid.str('address'); if (pub) { const key = KeyRing.fromPublic(pub); @@ -393,8 +393,8 @@ class HTTP extends Server { return; } - if (b58) { - const addr = Address.fromString(b58, this.network); + if (address) { + const addr = Address.fromString(address, this.network); await req.wallet.importAddress(acct, addr); res.json(200, { success: true }); return; @@ -627,12 +627,12 @@ class HTTP extends Server { // Get private key this.get('/wallet/:id/wif/:address', async (req, res) => { const valid = Validator.fromRequest(req); - const b58 = valid.str('address'); + const address = valid.str('address'); const passphrase = valid.str('passphrase'); - enforce(b58, 'Address is required.'); + enforce(address, 'Address is required.'); - const addr = Address.fromString(b58, this.network); + const addr = Address.fromString(address, this.network); const key = await req.wallet.getPrivateKey(addr, passphrase); if (!key) {