/*! * lib/remote-importer/sources.js * Copyright © 2019 – Katana Cryptographic Ltd. All Rights Reserved. */ 'use strict' const network = require('../bitcoin/network') const Logger = require('../logger') const keys = require('../../keys')[network.key] /** * Base class defining data source for imports/rescans of HD accounts and addresses */ class Sources { /** * Constructor */ constructor() { this.source = null } /** * Retrieve information for a given address * @param {string} address - bitcoin address * @param {boolean} filterAddr - True if an upper bound should be used * for #transactions associated to the address, False otherwise * @returns {Promise} returns an object * { address: , txids: , ntx: } */ async getAddress(address, filterAddr) { const ret = { address, txids: [], ntx: 0 } try { const result = await this.source.getAddress(address, filterAddr) if (result.ntx) ret.ntx = result.ntx else if (result.txids) ret.ntx = result.txids.length if (result.txids) ret.txids = result.txids } catch(e) { Logger.error(null, `Importer : Sources.getAddress() : ${address} from ${this.source.base}`) } finally { return ret } } /** * Retrieve information for a list of addresses * @param {string[]} addresses - array of bitcoin address * @param {boolean} filterAddr - True if an upper bound should be used * for #transactions associated to the address, False otherwise * @returns {Promise} returns an object * { address: , txids: , ntx: } */ async getAddresses(addresses, filterAddr) { const ret = [] try { const results = await this.source.getAddresses(addresses, filterAddr) for (let r of results) { // Filter addresses with too many txs if (!filterAddr || (r.ntx <= keys.addrFilterThreshold)) ret.push(r) } } catch(e) { Logger.error(null, `Importer : Sources.getAddresses() : Error while requesting ${addresses} from ${this.source.base}`) } finally { return ret } } /** * Retrieve the height of the chaintip * @returns {Promise} returns an object * {chainTipHeight: } */ async getChainTipHeight() { let ret = {'chainTipHeight': null} try { ret = await this.source.getChainTipHeight() } catch(e) { Logger.error(e, `Importer : Sources.getChainTipHeight() : Error while retrieving the chaintip`) } finally { return ret } } } module.exports = Sources