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AEzB,MAAM,EAAE,GAAG,CAAC,KAAK;YACjB,IAAI,EAAE,GAAG,CAAC,GAAG;SACd,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;QACjB,OAAO,2BAAM,CAAC,IAAI,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAA;IACtC,CAAC;CACF;AAlMD,2BAkMC","sourcesContent":["import reduct = require('reduct')\nimport compat from 'ilp-compat-plugin'\nimport Store from '../services/store'\nimport Config from './config'\nimport { EventEmitter } from 'events'\nimport { AccountInfo } from '../types/accounts'\nimport {\n ConnectOptions,\n PluginInstance\n} from '../types/plugin'\nimport ILDCP = require('ilp-protocol-ildcp')\n\nimport { create as createLogger } from '../common/log'\nconst log = createLogger('accounts')\n\nexport interface AccountEntry {\n plugin: PluginInstance,\n info: AccountInfo\n}\n\nexport default class Accounts extends EventEmitter {\n protected config: Config\n protected store: Store\n\n protected address: string\n protected accounts: Map<string, AccountEntry>\n\n constructor (deps: reduct.Injector) {\n super()\n\n this.config = deps(Config)\n this.store = deps(Store)\n\n this.address = this.config.ilpAddress || 'unknown'\n this.accounts = new Map()\n }\n\n async loadIlpAddress () {\n const inheritFrom = this.config.ilpAddressInheritFrom ||\n // Get account id of first parent\n [...this.accounts]\n .filter(([key, value]) => value.info.relation === 'parent')\n .map(([key]) => key)[0]\n\n if (this.config.ilpAddress === 'unknown' && !inheritFrom) {\n throw new Error('When there is no parent, ILP address must be specified in configuration.')\n } else if (this.config.ilpAddress === 'unknown' && inheritFrom) {\n const parent = this.getPlugin(inheritFrom)\n\n log.trace('connecting to parent. accountId=%s', inheritFrom)\n await parent.connect({})\n\n const ildcpInfo = await ILDCP.fetch(parent.sendData.bind(parent))\n\n this.setOwnAddress(ildcpInfo.clientAddress)\n\n if (this.address === 'unknown') {\n log.error('could not get ilp address from parent.')\n throw new Error('no ilp address configured.')\n }\n }\n }\n\n async connect (options: ConnectOptions) {\n const unconnectedAccounts = Array.from(this.accounts.values())\n .filter(account => !account.plugin.isConnected())\n return Promise.all(unconnectedAccounts.map(account => account.plugin.connect(options)))\n }\n\n async disconnect () {\n const connectedAccounts = Array.from(this.accounts.values())\n .filter(account => account.plugin.isConnected())\n return Promise.all(connectedAccounts.map(account => account.plugin.disconnect()))\n }\n\n getOwnAddress () {\n return this.address\n }\n\n setOwnAddress (newAddress: string) {\n log.trace('setting ilp address. oldAddress=%s newAddress=%s', this.address, newAddress)\n this.address = newAddress\n }\n\n getPlugin (accountId: string) {\n const account = this.accounts.get(accountId)\n\n if (!account) {\n log.error('could not find plugin for account id. accountId=%s', accountId)\n throw new Error('unknown account id. accountId=' + accountId)\n }\n\n return account.plugin\n }\n\n exists (accountId: string) {\n return this.accounts.has(accountId)\n }\n\n getAccountIds () {\n return Array.from(this.accounts.keys())\n }\n\n getAssetCode (accountId: string) {\n const account = this.accounts.get(accountId)\n\n if (!account) {\n log.error('no currency found. account=%s', accountId)\n return undefined\n }\n\n return account.info.assetCode\n }\n\n add (accountId: string, creds: any) {\n log.info('add account. accountId=%s', accountId)\n\n // Although cloning the options object that comes in from\n // code that includes ilp-connector is good practice,\n // this breaks for instance when the plugin options\n // contain for instance a https server like in `wsOpts` in\n // https://github.com/interledgerjs/ilp-plugin-mini-accounts/blob/a77f1a6b984b6816856a0948dfa57fe95e7ddd8b/README.md#example\n //\n // creds = cloneDeep(creds)\n\n try {\n this.config.validateAccount(accountId, creds)\n } catch (err) {\n if (err.name === 'InvalidJsonBodyError') {\n log.error('validation error in account config. id=%s', accountId)\n err.debugPrint(log.warn.bind(log))\n throw new Error('error while adding account, see error log for details.')\n }\n\n throw err\n }\n\n const plugin = this.getPluginFromCreds(accountId, creds)\n this.accounts.set(accountId, {\n info: creds,\n plugin\n })\n\n this.emit('add', accountId, plugin)\n }\n\n remove (accountId: string) {\n const plugin = this.getPlugin(accountId)\n if (!plugin) {\n return undefined\n }\n log.info('remove account. accountId=' + accountId)\n\n this.emit('remove', accountId, plugin)\n\n this.accounts.delete(accountId)\n return plugin\n }\n\n getInfo (accountId: string) {\n const account = this.accounts.get(accountId)\n\n if (!account) {\n throw new Error('unknown account id. accountId=' + accountId)\n }\n\n return account.info\n }\n\n getChildAddress (accountId: string) {\n const info = this.getInfo(accountId)\n\n if (info.relation !== 'child') {\n throw new Error('Can\\'t generate child address for account that is isn\\'t a child')\n }\n\n const ilpAddressSegment = info.ilpAddressSegment || accountId\n\n return this.address + '.' + ilpAddressSegment\n }\n\n getStatus () {\n const accounts = {}\n this.accounts.forEach((account, accountId) => {\n accounts[accountId] = {\n // Set info.options to undefined so that credentials aren't exposed.\n info: Object.assign({}, account.info, { options: undefined }),\n connected: account.plugin.isConnected(),\n adminInfo: !!account.plugin.getAdminInfo\n }\n })\n return {\n address: this.address,\n accounts\n }\n }\n\n private getPluginFromCreds (accountId: string, creds: any): PluginInstance {\n // Use a plugin instance directly.\n if (typeof creds.plugin === 'object') return creds.plugin\n const Plugin = require(creds.plugin)\n\n const api: any = {\n store: this.store.getPluginStore(accountId),\n log: createLogger(`${creds.plugin}[${accountId}]`)\n }\n\n const opts = Object.assign({\n // Provide old deprecated _store and _log properties\n _store: api.store,\n _log: api.log\n }, creds.options)\n return compat(new Plugin(opts, api))\n }\n}\n"]}
\No newline at end of file