UNPKG

@onekeyfe/blockchain-link

Version:

High-level javascript interface for blockchain communication

208 lines (207 loc) 8 kB
"use strict"; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); var constants_1 = require("../constants"); var errors_1 = require("../constants/errors"); var WorkerCommon = (function () { function WorkerCommon(postFn) { this.addresses = []; this.accounts = []; this.subscription = {}; this.settings = { name: 'unknown', worker: 'unknown', server: [], }; this.debugPrefix = '[UnknownWorker]'; this.post = function () { return console.warn('BlockchainLink:workers.common: postMessage method is not set'); }; if (typeof postFn !== 'undefined') { this.post = postFn; } } WorkerCommon.prototype.handshake = function () { this.post.call(null, { id: -1, type: constants_1.MESSAGES.HANDSHAKE, }); }; WorkerCommon.prototype.setSettings = function (s) { this.settings = s; this.debugPrefix = "[Worker \"" + s.name + "\"]:"; }; WorkerCommon.prototype.getSettings = function () { return this.settings; }; WorkerCommon.prototype.errorHandler = function (_a) { var id = _a.id, error = _a.error; var errorCode = 'blockchain_link/unknown'; var message = error.message; if (error.code) { errorCode = error.code; } this.post.call(null, { id: id, type: constants_1.RESPONSES.ERROR, payload: { code: errorCode, message: message, }, }); }; WorkerCommon.prototype.response = function (data) { this.post.call(null, this.removeEmpty(data)); }; WorkerCommon.prototype.validateAddresses = function (addr) { if (!Array.isArray(addr)) throw new errors_1.CustomError('invalid_param', '+addresses'); var seen = []; return addr.filter(function (a) { if (typeof a !== 'string') return false; if (seen.indexOf(a) >= 0) return false; seen.push(a); return true; }); }; WorkerCommon.prototype.addAddresses = function (addr) { var _this = this; var unique = this.validateAddresses(addr).filter(function (a) { return _this.addresses.indexOf(a) < 0; }); this.addresses = this.addresses.concat(unique); return unique; }; WorkerCommon.prototype.getAddresses = function () { return this.addresses; }; WorkerCommon.prototype.removeAddresses = function (addr) { var unique = this.validateAddresses(addr); this.addresses = this.addresses.filter(function (a) { return unique.indexOf(a) < 0; }); return this.addresses; }; WorkerCommon.prototype.validateAccounts = function (acc) { if (!Array.isArray(acc)) throw new errors_1.CustomError('invalid_param', '+accounts'); var seen = []; return acc.filter(function (a) { if (a && typeof a === 'object' && typeof a.descriptor === 'string') { if (seen.indexOf(a.descriptor) >= 0) return false; seen.push(a.descriptor); return true; } return false; }); }; WorkerCommon.prototype.getAccountAddresses = function (acc) { if (acc.addresses) { var _a = acc.addresses, change = _a.change, used = _a.used, unused = _a.unused; return change.concat(used, unused).map(function (a) { return a.address; }); } return [acc.descriptor]; }; WorkerCommon.prototype.addAccounts = function (acc) { var _this = this; var valid = this.validateAccounts(acc); var others = this.accounts.filter(function (a) { return !valid.find(function (b) { return b.descriptor === a.descriptor; }); }); this.accounts = others.concat(valid); var addresses = this.accounts.reduce(function (addr, a) { return addr.concat(_this.getAccountAddresses(a)); }, []); this.addAddresses(addresses); return valid; }; WorkerCommon.prototype.getAccount = function (address) { return this.accounts.find(function (a) { if (a.descriptor === address) return true; if (a.addresses) { var _a = a.addresses, change = _a.change, used = _a.used, unused = _a.unused; if (change.find(function (ad) { return ad.address === address; })) return true; if (used.find(function (ad) { return ad.address === address; })) return true; if (unused.find(function (ad) { return ad.address === address; })) return true; } return false; }); }; WorkerCommon.prototype.getAccounts = function () { return this.accounts; }; WorkerCommon.prototype.removeAccounts = function (acc) { var _this = this; var valid = this.validateAccounts(acc); var accountsToRemove = this.accounts.filter(function (a) { return valid.find(function (b) { return b.descriptor === a.descriptor; }); }); var addressesToRemove = accountsToRemove.reduce(function (addr, acc) { return addr.concat(_this.getAccountAddresses(acc)); }, []); this.accounts = this.accounts.filter(function (a) { return accountsToRemove.indexOf(a) < 0; }); this.removeAddresses(addressesToRemove); return this.accounts; }; WorkerCommon.prototype.addSubscription = function (type) { this.subscription[type] = true; }; WorkerCommon.prototype.getSubscription = function (type) { return this.subscription[type]; }; WorkerCommon.prototype.hasSubscriptions = function () { return Object.keys(this.subscription).length > 0; }; WorkerCommon.prototype.removeSubscription = function (type) { delete this.subscription[type]; }; WorkerCommon.prototype.clearSubscriptions = function () { var _this = this; Object.keys(this.subscription).forEach(function (key) { delete _this.subscription[key]; }); }; WorkerCommon.prototype.shuffleEndpoints = function (a) { var _a; for (var i = a.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); _a = [a[j], a[i]], a[i] = _a[0], a[j] = _a[1]; } return a; }; WorkerCommon.prototype.removeEmpty = function (obj) { var _this = this; Object.keys(obj).forEach(function (key) { if (Array.isArray(obj[key])) obj[key].map(function (o) { return _this.removeEmpty(o); }); if (obj[key] && typeof obj[key] === 'object') _this.removeEmpty(obj[key]); else if (obj[key] === undefined) delete obj[key]; }); return obj; }; WorkerCommon.prototype.debug = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } if (this.settings && this.settings.debug) { if (args[0] === 'warn' || args[0] === 'error') { console[args[0]].apply(console, __spreadArrays([this.debugPrefix], args.slice(1))); } else { console.log.apply(console, __spreadArrays([this.debugPrefix], args)); } } }; return WorkerCommon; }()); exports.default = WorkerCommon;