-
-
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.
"wip: dummy POC 'popup in suite-desktop'"
- Loading branch information
Showing
5 changed files
with
216 additions
and
5 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,132 @@ | ||
import EventEmitter from 'events'; | ||
|
||
// NOTE: @trezor/connect part is intentionally not imported from the index so we do include the whole library. | ||
import { | ||
IFRAME, | ||
UiResponseEvent, | ||
CallMethodPayload, | ||
CallMethodAnyResponse, | ||
} from '@trezor/connect/src/events'; | ||
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 { parseConnectSettings } from '../connectSettings'; | ||
import { Login } from '@trezor/connect/src/types/api/requestLogin'; | ||
// import { createDeferred } from '@trezor/utils'; | ||
|
||
const ws = new WebSocket('ws://localhost:8090'); | ||
|
||
ws.addEventListener('error', console.error); | ||
|
||
ws.addEventListener('open', function open() {}); | ||
|
||
/** | ||
* 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 CoreInSuiteDesktop implements ConnectFactoryDependencies { | ||
public eventEmitter = new EventEmitter(); | ||
protected _settings: ConnectSettings; | ||
|
||
public constructor() { | ||
this._settings = parseConnectSettings(); | ||
} | ||
|
||
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> { | ||
const newSettings = parseConnectSettings({ | ||
...this._settings, | ||
...settings, | ||
}); | ||
|
||
// defaults | ||
if (!newSettings.transports?.length) { | ||
newSettings.transports = ['BridgeTransport', 'WebUsbTransport']; | ||
} | ||
this._settings = newSettings; | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
/** | ||
* 1. opens popup | ||
* 2. sends request to popup where the request is handled by core | ||
* 3. returns response | ||
*/ | ||
public async call(params: CallMethodPayload): Promise<CallMethodAnyResponse> { | ||
ws.send( | ||
JSON.stringify({ | ||
type: IFRAME.CALL, | ||
payload: params, | ||
}), | ||
); | ||
|
||
return new Promise(resolve => { | ||
ws.addEventListener('message', function message(event) { | ||
console.log('received: %s', event.data); | ||
resolve(JSON.parse(event.data)); | ||
}); | ||
}); | ||
} | ||
|
||
uiResponse(_response: UiResponseEvent) { | ||
// this shouldn't be needed, ui response should be handled in suite-desktop | ||
throw ERRORS.TypedError('Method_InvalidPackage'); | ||
} | ||
|
||
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 CoreInSuiteDesktop(); | ||
|
||
// 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
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
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
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