-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
578 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import EventEmitter from 'events'; | ||
|
||
import { | ||
POPUP, | ||
IFRAME, | ||
UI_EVENT, | ||
createErrorMessage, | ||
UiResponseEvent, | ||
CallMethodPayload, | ||
CallMethodAnyResponse, | ||
} from '@trezor/connect'; | ||
import { Login } from '@trezor/connect/src/types/api/requestLogin'; | ||
|
||
import * as ERRORS from '@trezor/connect/src/constants/errors'; | ||
import type { | ||
ConnectSettings, | ||
ConnectSettingsPublic, | ||
Manifest, | ||
Response, | ||
} from '@trezor/connect/src/types'; | ||
import { ConnectFactoryDependencies, factory } from '@trezor/connect/src/factory'; | ||
import { initLog, setLogWriter, LogMessage, LogWriter, Log } from '@trezor/connect/src/utils/debug'; | ||
|
||
import { parseConnectSettings } from '../connectSettings'; | ||
import { createDeferred } from '@trezor/utils'; | ||
|
||
import { coreManager } from '@trezor/connect/src/core'; | ||
/** | ||
* Base class for CoreInPopup methods for TrezorConnect factory. | ||
* This implementation is directly used here in connect-web, but it is also extended in connect-webextension. | ||
*/ | ||
export class CoreInModule implements ConnectFactoryDependencies { | ||
public eventEmitter = new EventEmitter(); | ||
protected _settings: ConnectSettings; | ||
|
||
protected logger: Log; | ||
|
||
public constructor() { | ||
this._settings = parseConnectSettings(); | ||
this.logger = initLog('@trezor/connect-web'); | ||
} | ||
|
||
private logWriterFactory(): LogWriter { | ||
return { | ||
add: (message: LogMessage) => { | ||
// popupManager.channel.postMessage( | ||
// { | ||
// event: UI_EVENT, | ||
// type: IFRAME.LOG, | ||
// payload: message, | ||
// }, | ||
// { usePromise: false, useQueue: true }, | ||
// ); | ||
}, | ||
}; | ||
} | ||
|
||
public manifest(data: Manifest) { | ||
this._settings = parseConnectSettings({ | ||
...this._settings, | ||
manifest: data, | ||
}); | ||
} | ||
|
||
public dispose() { | ||
this.eventEmitter.removeAllListeners(); | ||
this._settings = parseConnectSettings(); | ||
|
||
return Promise.resolve(undefined); | ||
} | ||
|
||
public cancel(error?: string) {} | ||
|
||
public init(settings: Partial<ConnectSettingsPublic> = {}): Promise<void> { | ||
this._settings = parseConnectSettings({ | ||
settings, | ||
}); | ||
|
||
this.logger.enabled = !!settings.debug; | ||
|
||
if (!this._settings.manifest) { | ||
throw ERRORS.TypedError('Init_ManifestMissing'); | ||
} | ||
|
||
this.logger.debug('initiated'); | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
/** | ||
*/ | ||
public async call(params: CallMethodPayload): Promise<CallMethodAnyResponse> { | ||
this.logger.debug('call', params); | ||
} | ||
|
||
uiResponse(response: UiResponseEvent) { | ||
const { type, payload } = response; | ||
} | ||
|
||
renderWebUSBButton() {} | ||
|
||
requestLogin(): Response<Login> { | ||
// todo: not supported yet | ||
throw ERRORS.TypedError('Method_InvalidPackage'); | ||
} | ||
|
||
disableWebUSB() { | ||
// todo: not supported yet, probably not needed | ||
throw ERRORS.TypedError('Method_InvalidPackage'); | ||
} | ||
|
||
requestWebUSBDevice() { | ||
// not needed - webusb pairing happens in popup | ||
throw ERRORS.TypedError('Method_InvalidPackage'); | ||
} | ||
} | ||
|
||
const impl = new CoreInModule(); | ||
|
||
// Exported to enable using directly | ||
export const TrezorConnect = factory({ | ||
// Bind all methods due to shadowing `this` | ||
eventEmitter: impl.eventEmitter, | ||
init: impl.init.bind(impl), | ||
call: impl.call.bind(impl), | ||
manifest: impl.manifest.bind(impl), | ||
requestLogin: impl.requestLogin.bind(impl), | ||
uiResponse: impl.uiResponse.bind(impl), | ||
renderWebUSBButton: impl.renderWebUSBButton.bind(impl), | ||
disableWebUSB: impl.disableWebUSB.bind(impl), | ||
requestWebUSBDevice: impl.requestWebUSBDevice.bind(impl), | ||
cancel: impl.cancel.bind(impl), | ||
dispose: impl.dispose.bind(impl), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.