From 58f79ecefe8a773af4ba1b926fee0c7772e0aff2 Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 11 Aug 2023 15:19:40 +0800 Subject: [PATCH] feat: adapt lowlevel transport --- packages/core/src/core/index.ts | 14 +++++++---- .../core/src/data-manager/TransportManager.ts | 19 +++++++++++++-- packages/core/src/types/api/init.ts | 4 +++- packages/core/src/types/settings.ts | 2 +- packages/core/src/utils/logger.ts | 2 ++ packages/hd-common-connect-sdk/package.json | 3 ++- packages/hd-common-connect-sdk/src/index.ts | 24 ++++++++++++++++--- packages/hd-transport-lowlevel/package.json | 2 +- packages/hd-transport-lowlevel/src/index.ts | 11 ++++++--- packages/hd-transport-lowlevel/src/types.ts | 12 ---------- packages/hd-transport/src/index.ts | 2 ++ packages/hd-transport/src/types/transport.ts | 18 +++++++++++++- 12 files changed, 83 insertions(+), 30 deletions(-) diff --git a/packages/core/src/core/index.ts b/packages/core/src/core/index.ts index cf9e7f86f..4c1f0f4e2 100644 --- a/packages/core/src/core/index.ts +++ b/packages/core/src/core/index.ts @@ -1,6 +1,6 @@ import semver from 'semver'; import EventEmitter from 'events'; -import { Features, OneKeyDeviceInfo } from '@onekeyfe/hd-transport'; +import { Features, LowlevelTransportSharedPlugin, OneKeyDeviceInfo } from '@onekeyfe/hd-transport'; import { createDeferred, Deferred, @@ -765,15 +765,19 @@ export const initConnector = () => { return _connector; }; -const initTransport = (Transport: any) => { - TransportManager.setTransport(Transport); +const initTransport = (Transport: any, plugin?: LowlevelTransportSharedPlugin) => { + TransportManager.setTransport(Transport, plugin); }; -export const init = async (settings: ConnectSettings, Transport: any) => { +export const init = async ( + settings: ConnectSettings, + Transport: any, + plugin?: LowlevelTransportSharedPlugin +) => { try { try { await DataManager.load(settings); - initTransport(Transport); + initTransport(Transport, plugin); } catch { Log.error('DataManager.load error'); } diff --git a/packages/core/src/data-manager/TransportManager.ts b/packages/core/src/data-manager/TransportManager.ts index 688e83a2b..bc1ff4531 100644 --- a/packages/core/src/data-manager/TransportManager.ts +++ b/packages/core/src/data-manager/TransportManager.ts @@ -1,4 +1,4 @@ -import { Transport } from '@onekeyfe/hd-transport'; +import { LowlevelTransportSharedPlugin, Transport } from '@onekeyfe/hd-transport'; import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared'; import DataManager, { MessageVersion } from './DataManager'; import { getLogger, LoggerNames } from '../utils'; @@ -10,6 +10,7 @@ import { Features } from '../types'; const Log = getLogger(LoggerNames.Transport); const BleLogger = getLogger(LoggerNames.HdBleTransport); const HttpLogger = getLogger(LoggerNames.HdTransportHttp); +const LowLevelLogger = getLogger(LoggerNames.HdTransportLowLevel); /** * transport 在同一个环境中只会存在一个 @@ -27,6 +28,8 @@ export default class TransportManager { static messageVersion: MessageVersion = 'latest'; + static plugin: LowlevelTransportSharedPlugin | null = null; + static load() { Log.debug('transport manager load'); this.defaultMessages = DataManager.getProtobufMessages(); @@ -45,6 +48,14 @@ export default class TransportManager { } else { Log.debug('React Native Do Not Initializing transports'); } + } else if (env === 'lowlevel') { + if (!this.plugin) { + throw ERRORS.TypedError( + HardwareErrorCode.TransportNotConfigured, + 'Lowlevel transport mast have plugin' + ); + } + await this.transport.init(LowLevelLogger, DevicePool.emitter, this.plugin); } else { await this.transport.init(HttpLogger); } @@ -81,7 +92,7 @@ export default class TransportManager { } } - static setTransport(TransportConstructor: any) { + static setTransport(TransportConstructor: any, plugin?: LowlevelTransportSharedPlugin) { const env = DataManager.getSettings('env'); if (env === 'react-native') { /** Actually initializes the ReactNativeTransport */ @@ -90,6 +101,10 @@ export default class TransportManager { /** Actually initializes the HttpTransport */ this.transport = new TransportConstructor() as unknown as Transport; } + if (plugin) { + this.plugin = plugin; + Log.debug('set transport plugin: ', this.plugin); + } Log.debug('set transport: ', this.transport); } diff --git a/packages/core/src/types/api/init.ts b/packages/core/src/types/api/init.ts index c63de81da..f742d5e0d 100644 --- a/packages/core/src/types/api/init.ts +++ b/packages/core/src/types/api/init.ts @@ -1,9 +1,11 @@ +import type { LowlevelTransportSharedPlugin } from '@onekeyfe/hd-transport'; import { LowLevelCoreApi } from '../../lowLevelInject'; import type { ConnectSettings } from '../settings'; export declare function init( settings: Partial, - lowLevelApi?: LowLevelCoreApi + lowLevelApi?: LowLevelCoreApi, + pulgin?: LowlevelTransportSharedPlugin ): Promise; export declare function updateSettings(settings: Partial): Promise; diff --git a/packages/core/src/types/settings.ts b/packages/core/src/types/settings.ts index ad5951dd6..33e3c433b 100644 --- a/packages/core/src/types/settings.ts +++ b/packages/core/src/types/settings.ts @@ -14,7 +14,7 @@ export type ConnectSettings = { priority: number; trustedHost: boolean; supportedBrowser?: boolean; - env: 'node' | 'web' | 'webextension' | 'electron' | 'react-native' | 'webusb'; + env: 'node' | 'web' | 'webextension' | 'electron' | 'react-native' | 'webusb' | 'lowlevel'; timestamp: number; isFrame?: boolean; preRelease?: boolean; diff --git a/packages/core/src/utils/logger.ts b/packages/core/src/utils/logger.ts index 46c1154b6..858e3764f 100644 --- a/packages/core/src/utils/logger.ts +++ b/packages/core/src/utils/logger.ts @@ -155,6 +155,7 @@ export enum LoggerNames { HdCommonConnectSdk = '@onekey/common-connect-sdk', HdBleSdk = '@onekey/hd-ble-sdk', HdTransportHttp = '@onekey/hd-transport-http', + HdTransportLowLevel = '@onekey/hd-transport-lowlevel', HdBleTransport = '@onekey/hd-ble-transport', Connect = '@onekey/connect', Iframe = 'IFrame', @@ -173,6 +174,7 @@ export const LoggerMap = { [LoggerNames.HdBleSdk]: initLog(LoggerNames.HdBleSdk), [LoggerNames.HdTransportHttp]: initLog(LoggerNames.HdTransportHttp), [LoggerNames.HdBleTransport]: initLog(LoggerNames.HdBleTransport), + [LoggerNames.HdTransportLowLevel]: initLog(LoggerNames.HdTransportLowLevel), [LoggerNames.Connect]: initLog(LoggerNames.Connect), [LoggerNames.Iframe]: initLog(LoggerNames.Iframe), [LoggerNames.SendMessage]: initLog(LoggerNames.SendMessage), diff --git a/packages/hd-common-connect-sdk/package.json b/packages/hd-common-connect-sdk/package.json index 0f5239e96..34e2f5705 100644 --- a/packages/hd-common-connect-sdk/package.json +++ b/packages/hd-common-connect-sdk/package.json @@ -23,6 +23,7 @@ "@onekeyfe/hd-core": "^0.3.22", "@onekeyfe/hd-shared": "^0.3.22", "@onekeyfe/hd-transport-http": "^0.3.22", - "@onekeyfe/hd-transport-webusb": "^0.3.22" + "@onekeyfe/hd-transport-webusb": "^0.3.22", + "@onekeyfe/hd-transport-lowlevel": "^0.3.22" } } diff --git a/packages/hd-common-connect-sdk/src/index.ts b/packages/hd-common-connect-sdk/src/index.ts index a9da9ac80..713e937b2 100644 --- a/packages/hd-common-connect-sdk/src/index.ts +++ b/packages/hd-common-connect-sdk/src/index.ts @@ -18,10 +18,13 @@ import HardwareSdk, { FIRMWARE_EVENT, DEVICE_EVENT, DEVICE, + LowLevelCoreApi, } from '@onekeyfe/hd-core'; import { ERRORS, createDeferred, Deferred, HardwareErrorCode } from '@onekeyfe/hd-shared'; +import type { LowlevelTransportSharedPlugin } from '@onekeyfe/hd-transport'; import HttpTransport from '@onekeyfe/hd-transport-http'; import WebusbTransport from '@onekeyfe/hd-transport-webusb'; +import LowlevelTransport from '@onekeyfe/hd-transport-lowlevel'; const eventEmitter = new EventEmitter(); const Log = getLogger(LoggerNames.HdCommonConnectSdk); @@ -100,7 +103,11 @@ async function postMessage(message: CoreMessage, usePromise = true) { _core.handleMessage(message); } -const init = async (settings: Partial) => { +const init = async ( + settings: Partial, + _?: LowLevelCoreApi, + plugin?: LowlevelTransportSharedPlugin +) => { _settings = { ..._settings, ...settings, env: settings.env ?? 'node' }; enableLog(!!settings.debug); @@ -109,8 +116,19 @@ const init = async (settings: Partial) => { try { console.log(_settings.env); - const Transport = _settings.env === 'webusb' ? WebusbTransport : HttpTransport; - _core = await initCore(_settings, Transport); + // const Transport = _settings.env === 'webusb' ? WebusbTransport : HttpTransport; + let Transport: any; + switch (_settings.env) { + case 'webusb': + Transport = WebusbTransport; + break; + case 'lowlevel': + Transport = LowlevelTransport; + break; + default: + Transport = HttpTransport; + } + _core = await initCore(_settings, Transport, plugin); _core?.on(CORE_EVENT, handleMessage); setLoggerPostMessage(handleMessage); diff --git a/packages/hd-transport-lowlevel/package.json b/packages/hd-transport-lowlevel/package.json index b8c55a73e..febc380e3 100644 --- a/packages/hd-transport-lowlevel/package.json +++ b/packages/hd-transport-lowlevel/package.json @@ -1,5 +1,5 @@ { - "name": "hd-transport-lowlevel", + "name": "@onekeyfe/hd-transport-lowlevel", "version": "0.3.22", "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme", "license": "MIT", diff --git a/packages/hd-transport-lowlevel/src/index.ts b/packages/hd-transport-lowlevel/src/index.ts index b23377cc2..e291abad1 100644 --- a/packages/hd-transport-lowlevel/src/index.ts +++ b/packages/hd-transport-lowlevel/src/index.ts @@ -1,6 +1,8 @@ import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared'; import transport from '@onekeyfe/hd-transport'; -import type { LowlevelTransportSharedPlugin, LowLevelAcquireInput } from './types'; +import type EventEmitter from 'events'; +import type { LowlevelTransportSharedPlugin } from '@onekeyfe/hd-transport'; +import type { LowLevelAcquireInput } from './types'; const { check, buildBuffers, receiveOne, parseConfigure } = transport; @@ -11,10 +13,13 @@ export default class LowlevelTransport { Log?: any; + emitter?: EventEmitter; + plugin: LowlevelTransportSharedPlugin = {} as LowlevelTransportSharedPlugin; - init(logger: any, plugin: LowlevelTransportSharedPlugin) { + init(logger: any, emitter: EventEmitter, plugin: LowlevelTransportSharedPlugin) { this.Log = logger; + this.emitter = emitter; this.plugin = plugin; this.plugin.init(); } @@ -29,7 +34,7 @@ export default class LowlevelTransport { // empty } - async enumerate() { + enumerate() { return this.plugin.enumerate(); } diff --git a/packages/hd-transport-lowlevel/src/types.ts b/packages/hd-transport-lowlevel/src/types.ts index 114165fe4..8a1e8ed00 100644 --- a/packages/hd-transport-lowlevel/src/types.ts +++ b/packages/hd-transport-lowlevel/src/types.ts @@ -1,15 +1,3 @@ -export type Device = { id: string; name: string }; -export type LowlevelTransportSharedPlugin = { - enumerate: () => Promise; - send: (uuid: string, data: string) => Promise; - receive: () => Promise; - connect: (uuid: string) => Promise; - disconnect: (uuid: string) => Promise; - - init: () => Promise; - version: string; -}; - export type LowLevelAcquireInput = { uuid: string; }; diff --git a/packages/hd-transport/src/index.ts b/packages/hd-transport/src/index.ts index 47eb9bca5..c91e8edd1 100644 --- a/packages/hd-transport/src/index.ts +++ b/packages/hd-transport/src/index.ts @@ -20,6 +20,8 @@ export type { OneKeyMobileDeviceInfo, OneKeyDeviceInfoWithSession, MessageFromOneKey, + LowlevelTransportSharedPlugin, + LowLevelDevice, } from './types'; export { Messages } from './types'; diff --git a/packages/hd-transport/src/types/transport.ts b/packages/hd-transport/src/types/transport.ts index 5c3b4f862..c08d5ec89 100644 --- a/packages/hd-transport/src/types/transport.ts +++ b/packages/hd-transport/src/types/transport.ts @@ -26,7 +26,11 @@ export type AcquireInput = { export type MessageFromOneKey = { type: string; message: Record }; -type ITransportInitFn = (logger?: any, emitter?: EventEmitter) => Promise; +type ITransportInitFn = ( + logger?: any, + emitter?: EventEmitter, + plugin?: LowlevelTransportSharedPlugin +) => Promise; export type Transport = { enumerate(): Promise>; @@ -55,3 +59,15 @@ export type Transport = { isOutdated: boolean; }; + +export type LowLevelDevice = { id: string; name: string }; +export type LowlevelTransportSharedPlugin = { + enumerate: () => Promise; + send: (uuid: string, data: string) => Promise; + receive: () => Promise; + connect: (uuid: string) => Promise; + disconnect: (uuid: string) => Promise; + + init: () => Promise; + version: string; +};