-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(aepp-wallet)!: rewrite to ts all except rpc
BREAKING CHANGE: BrowserRuntimeConnection, BrowserWindowMessageConnection are classes Create instances using new. BREAKING CHANGE: ContentScriptBridge, WalletDetector rewrited to plain functions Use `connectionProxy`, `walletDetector` respectively
- Loading branch information
Showing
21 changed files
with
386 additions
and
609 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* ISC License (ISC) | ||
* Copyright (c) 2022 aeternity developers | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
* PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
import BrowserConnection from './connection/Browser' | ||
|
||
/** | ||
* Browser connection proxy | ||
* Provide functionality to easily forward messages from one connection to another and back | ||
* @param con1 first connection | ||
* @param con2 second connection | ||
* @returns a function to stop proxying | ||
*/ | ||
export default (con1: BrowserConnection, con2: BrowserConnection): () => void => { | ||
con1.connect((msg: any) => con2.sendMessage(msg), () => con2.disconnect()) | ||
con2.connect((msg: any) => con1.sendMessage(msg), () => con1.disconnect()) | ||
|
||
return () => { | ||
con1.disconnect() | ||
con2.disconnect() | ||
} | ||
} |
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,68 @@ | ||
/* | ||
* ISC License (ISC) | ||
* Copyright (c) 2022 aeternity developers | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
* PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
import { AlreadyConnectedError, NoWalletConnectedError } from '../../errors' | ||
|
||
/** | ||
* Browser connection base interface | ||
*/ | ||
export default abstract class BrowserConnection { | ||
debug: boolean | ||
|
||
protected constructor ({ debug = false }: { debug?: boolean }) { | ||
this.debug = debug | ||
} | ||
|
||
/** | ||
* Connect | ||
* @param onMessage - Message handler | ||
* @param onDisconnect - trigger when runtime connection in closed | ||
*/ | ||
connect ( | ||
onMessage: (message: any, origin: string, source: any) => void, | ||
onDisconnect: () => void | ||
): void { | ||
if (this.isConnected()) throw new AlreadyConnectedError('You already connected') | ||
} | ||
|
||
/** | ||
* Disconnect | ||
*/ | ||
disconnect (): void { | ||
if (!this.isConnected()) throw new NoWalletConnectedError('You dont have connection. Please connect before') | ||
} | ||
|
||
/** | ||
* Send message | ||
*/ | ||
protected receiveMessage (message: any): void { | ||
if (this.debug) console.log('Receive message:', message) | ||
} | ||
|
||
/** | ||
* Send message | ||
*/ | ||
sendMessage (message: any): void { | ||
if (this.debug) console.log('Send message:', message) | ||
} | ||
|
||
/** | ||
* Check if connected | ||
* @return Is connected | ||
*/ | ||
abstract isConnected (): boolean | ||
} |
58 changes: 58 additions & 0 deletions
58
src/utils/aepp-wallet-communication/connection/BrowserRuntime.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,58 @@ | ||
/* | ||
* ISC License (ISC) | ||
* Copyright (c) 2022 aeternity developers | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
* PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
import { Runtime } from 'webextension-polyfill' | ||
import BrowserConnection from './Browser' | ||
|
||
/** | ||
* BrowserRuntimeConnection | ||
* Handle browser runtime communication | ||
*/ | ||
export default class BrowserRuntimeConnection extends BrowserConnection { | ||
port: Runtime.Port | ||
|
||
constructor ({ port, ...options }: { port: Runtime.Port, debug: boolean }) { | ||
super(options) | ||
this.port = port | ||
} | ||
|
||
disconnect (): void { | ||
super.disconnect() | ||
this.port.disconnect() | ||
} | ||
|
||
connect ( | ||
onMessage: (message: any, origin: string, source: Runtime.Port) => void, | ||
onDisconnect: () => void | ||
): void { | ||
super.connect(onMessage, onDisconnect) | ||
this.port.onMessage.addListener((message, port) => { | ||
this.receiveMessage(message) | ||
onMessage(message, port.name, port) | ||
}) | ||
this.port.onDisconnect.addListener(onDisconnect) | ||
} | ||
|
||
sendMessage (message: any): void { | ||
super.sendMessage(message) | ||
this.port.postMessage(message) | ||
} | ||
|
||
isConnected (): boolean { | ||
return this.port.onMessage.hasListeners() | ||
} | ||
} |
Oops, something went wrong.