import { UI_REQUEST } from '../../constants/ui-request'; import { serializedPath, validatePath } from '../helpers/pathUtils'; import { BaseMethod } from '../BaseMethod'; import { validateParams, validateResult } from '../helpers/paramsValidator'; import type { ScdoGetAddress as HardwareScdoGetAddress } from '@onekeyfe/hd-transport'; import type { DeviceFirmwareRange, ScdoAddress, ScdoGetAddressParams } from '../../types'; export default class ScdoGetAddress extends BaseMethod { hasBundle = false; init() { this.checkDeviceId = true; this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.NOT_INITIALIZE]; this.hasBundle = !!this.payload?.bundle; const payload = this.hasBundle ? this.payload : { bundle: [this.payload] }; // check payload validateParams(payload, [{ name: 'bundle', type: 'array' }]); // init params this.params = []; payload.bundle.forEach((batch: ScdoGetAddressParams) => { const addressN = validatePath(batch.path, 3); validateParams(batch, [ { name: 'path', required: true }, { name: 'showOnOneKey', type: 'boolean' }, ]); const showOnOneKey = batch.showOnOneKey ?? true; this.params.push({ address_n: addressN, show_display: showOnOneKey, }); }); } getVersionRange(): DeviceFirmwareRange { return { model_touch: { min: '4.10.0', }, }; } async run() { const responses: ScdoAddress[] = []; for (let i = 0; i < this.params.length; i++) { const param = this.params[i]; const res = await this.device.commands.typedCall('ScdoGetAddress', 'ScdoAddress', { ...param, }); const { address } = res.message; const result = { path: serializedPath(param.address_n), address, }; responses.push(result); this.postPreviousAddressMessage(result); } validateResult(responses, ['address'], { expectedLength: this.params.length, }); return Promise.resolve(this.hasBundle ? responses : responses[0]); } }