-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for Brave wallet (#828)
Signed-off-by: Logan Nguyen <[email protected]> Signed-off-by: Eric Le Ponner <[email protected]> Co-authored-by: Logan Nguyen <[email protected]>
- Loading branch information
1 parent
66b4e82
commit 873d342
Showing
5 changed files
with
119 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/*- | ||
* | ||
* Hedera Mirror Node Explorer | ||
* | ||
* Copyright (C) 2021 - 2023 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import {BaseProvider} from '@metamask/providers'; | ||
import {WalletDriver_Ethereum} from "@/utils/wallet/WalletDriver_Ethereum"; | ||
import {WalletDriverError} from "@/utils/wallet/WalletDriverError"; | ||
|
||
export class WalletDriver_Brave extends WalletDriver_Ethereum { | ||
|
||
private braveProvider: BaseProvider|null = null | ||
|
||
// | ||
// Public | ||
// | ||
|
||
public constructor() { | ||
super("Brave Wallet", | ||
"https://brave.com/static-assets/images/optimized/brave-branding-assets/images/brave-logo-color-RGB_reversed.png", | ||
"https://brave.com/static-assets/images/brave-logo-no-shadow.png") | ||
} | ||
|
||
// | ||
// WalletDriver_Ethereum | ||
// | ||
|
||
public async isExpectedProvider(provider: object): Promise<boolean> { | ||
// https://wallet-docs.brave.com/ethereum/wallet-detection/ | ||
const clientVersion = await (provider as any).request({method: "web3_clientVersion"}) | ||
const isBraveWallet = clientVersion.split('/')[0] === 'BraveWallet' | ||
this.braveProvider = isBraveWallet ? provider as BaseProvider : null | ||
return this.braveProvider !== null | ||
} | ||
|
||
public async connect(network: string): Promise<string[]> { | ||
const result = await super.connect(network) | ||
this.braveProvider?.on('chainChanged', this.handleDisconnect) | ||
return Promise.resolve(result) | ||
} | ||
|
||
public async disconnect(): Promise<void> { | ||
this.braveProvider?.removeListener("chainChanged", this.handleDisconnect) | ||
this.braveProvider = null | ||
return super.disconnect() | ||
} | ||
|
||
// | ||
// WalletDriver | ||
// | ||
|
||
public extensionNotFound(): WalletDriverError { | ||
// Brave wallet is builtin to Brave browser => we adapt wording | ||
const message = "Cannot access to " + this.name | ||
const extra = "Make sure to use Brave browser and enable wallet in browser settings." | ||
return new WalletDriverError(message, extra) | ||
} | ||
|
||
|
||
// | ||
// Private | ||
// | ||
|
||
private readonly handleDisconnect = () => this.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
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