forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wallet-ext: dapp <-> content script messaging
* use window message events to pass messages between the web page and the content script
- Loading branch information
1 parent
dc47252
commit 393d212
Showing
16 changed files
with
239 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { WindowMessageStream } from '_messaging/WindowMessageStream'; | ||
|
||
export function setupMessagesProxy() { | ||
const windowMsgStream = new WindowMessageStream( | ||
'sui_content-script', | ||
'sui_in-page' | ||
); | ||
windowMsgStream.messages.subscribe((msg) => { | ||
// TODO implement | ||
// eslint-disable-next-line no-console | ||
console.log('[ContentScriptProxy] message from inPage', msg); | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,10 +1,56 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { filter, lastValueFrom, map, take } from 'rxjs'; | ||
|
||
import { createMessage } from '_messages'; | ||
import { WindowMessageStream } from '_messaging/WindowMessageStream'; | ||
import { isErrorPayload } from '_payloads'; | ||
|
||
import type { SuiAddress } from '@mysten/sui.js'; | ||
import type { Payload } from '_payloads'; | ||
import type { GetAccount } from '_payloads/account/GetAccount'; | ||
import type { GetAccountResponse } from '_payloads/account/GetAccountResponse'; | ||
import type { Observable } from 'rxjs'; | ||
|
||
export class DAppInterface { | ||
private _window: Window; | ||
private _messagesStream: WindowMessageStream; | ||
|
||
constructor() { | ||
this._messagesStream = new WindowMessageStream( | ||
'sui_in-page', | ||
'sui_content-script' | ||
); | ||
} | ||
|
||
public getAccounts(): Promise<SuiAddress[]> { | ||
const stream = this.send<GetAccount, GetAccountResponse>({ | ||
type: 'get-account', | ||
}).pipe( | ||
take(1), | ||
map((response) => { | ||
if (isErrorPayload(response)) { | ||
// TODO: throw proper error | ||
throw new Error(response.message); | ||
} | ||
return response.accounts; | ||
}) | ||
); | ||
return lastValueFrom(stream); | ||
} | ||
|
||
constructor(theWindow: Window) { | ||
this._window = window; | ||
private send< | ||
RequestPayload extends Payload, | ||
ResponsePayload extends Payload | void = void | ||
>( | ||
payload: RequestPayload, | ||
responseForID?: string | ||
): Observable<ResponsePayload> { | ||
const msg = createMessage(payload, responseForID); | ||
this._messagesStream.send(msg); | ||
return this._messagesStream.messages.pipe( | ||
filter(({ id }) => id === msg.id), | ||
map((msg) => msg.payload as ResponsePayload) | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { filter, fromEvent, map } from 'rxjs'; | ||
|
||
import type { Message } from '_messages'; | ||
import type { Observable } from 'rxjs'; | ||
|
||
export type ClientType = 'sui_in-page' | 'sui_content-script'; | ||
|
||
type WindowMessage = { | ||
target: ClientType; | ||
payload: Message; | ||
}; | ||
|
||
export class WindowMessageStream { | ||
public readonly messages: Observable<Message>; | ||
private _name: ClientType; | ||
private _target: ClientType; | ||
|
||
constructor(name: ClientType, target: ClientType) { | ||
if (name === target) { | ||
throw new Error( | ||
'[WindowMessageStream] name and target must be different' | ||
); | ||
} | ||
this._name = name; | ||
this._target = target; | ||
this.messages = fromEvent<MessageEvent<WindowMessage>>( | ||
window, | ||
'message' | ||
).pipe( | ||
filter( | ||
(message) => | ||
message.source === window && | ||
message.data.target === this._name | ||
), | ||
map((message) => message.data.payload) | ||
); | ||
} | ||
|
||
public send(payload: Message) { | ||
const msg: WindowMessage = { | ||
target: this._target, | ||
payload, | ||
}; | ||
window.postMessage(msg); | ||
} | ||
} |
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,21 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { v4 as uuidV4 } from 'uuid'; | ||
|
||
import type { Payload } from './payloads/Payload'; | ||
|
||
export type Message = { | ||
id: string; | ||
payload: Payload; | ||
}; | ||
|
||
export function createMessage<MsgPayload extends Payload>( | ||
payload: MsgPayload, | ||
id?: string | ||
): Message { | ||
return { | ||
id: id || uuidV4(), | ||
payload, | ||
}; | ||
} |
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,4 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export * from './Message'; |
19 changes: 19 additions & 0 deletions
19
wallet/src/shared/messaging/messages/payloads/BasePayload.ts
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,19 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { Payload } from './Payload'; | ||
|
||
export type PayloadType = | ||
| 'permission-request' | ||
| 'permission-response' | ||
| 'get-permission-requests' | ||
| 'get-account' | ||
| 'get-account-response'; | ||
|
||
export interface BasePayload { | ||
type: PayloadType; | ||
} | ||
|
||
export function isBasePayload(payload: Payload): payload is BasePayload { | ||
return 'type' in payload && typeof payload.type !== 'undefined'; | ||
} |
14 changes: 14 additions & 0 deletions
14
wallet/src/shared/messaging/messages/payloads/ErrorPayload.ts
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,14 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { Payload } from './Payload'; | ||
|
||
export interface ErrorPayload { | ||
error: true; | ||
code: number; | ||
message: string; | ||
} | ||
|
||
export function isErrorPayload(payload: Payload): payload is ErrorPayload { | ||
return 'error' in payload && payload.error === true; | ||
} |
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,7 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { BasePayload } from './BasePayload'; | ||
import type { ErrorPayload } from './ErrorPayload'; | ||
|
||
export type Payload = BasePayload | ErrorPayload; |
8 changes: 8 additions & 0 deletions
8
wallet/src/shared/messaging/messages/payloads/account/GetAccount.ts
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,8 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { BasePayload } from '_payloads'; | ||
|
||
export interface GetAccount extends BasePayload { | ||
type: 'get-account'; | ||
} |
10 changes: 10 additions & 0 deletions
10
wallet/src/shared/messaging/messages/payloads/account/GetAccountResponse.ts
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,10 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { SuiAddress } from '@mysten/sui.js'; | ||
import type { BasePayload } from '_payloads'; | ||
|
||
export interface GetAccountResponse extends BasePayload { | ||
type: 'get-account-response'; | ||
accounts: SuiAddress[]; | ||
} |
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,6 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export * from './BasePayload'; | ||
export * from './ErrorPayload'; | ||
export * from './Payload'; |