diff --git a/background/main.ts b/background/main.ts index 6e8efd73d0..8a09f49ff0 100644 --- a/background/main.ts +++ b/background/main.ts @@ -141,7 +141,7 @@ import { REDUX_STATE_VERSION, } from "./redux-slices/migrations" import { PermissionMap } from "./services/provider-bridge/utils" -import { TALLY_INTERNAL_ORIGIN } from "./services/internal-ethereum-provider/constants" +import { TAHO_INTERNAL_ORIGIN } from "./services/internal-ethereum-provider/constants" import { ActivityDetail, addActivity, @@ -703,7 +703,7 @@ export default class Main extends BaseService { address: accounts[0].address, network: await this.internalEthereumProviderService.getCurrentOrDefaultNetworkForOrigin( - TALLY_INTERNAL_ORIGIN + TAHO_INTERNAL_ORIGIN ), }) ) @@ -1363,7 +1363,7 @@ export default class Main extends BaseService { this.internalEthereumProviderService.routeSafeRPCRequest( "wallet_switchEthereumChain", [{ chainId: network.chainID }], - TALLY_INTERNAL_ORIGIN + TAHO_INTERNAL_ORIGIN ) this.chainService.pollBlockPricesForNetwork(network.chainID) this.store.dispatch(clearCustomGas()) diff --git a/background/services/internal-ethereum-provider/constants.ts b/background/services/internal-ethereum-provider/constants.ts index 44f9c08978..7716397f9a 100644 --- a/background/services/internal-ethereum-provider/constants.ts +++ b/background/services/internal-ethereum-provider/constants.ts @@ -1,2 +1,2 @@ // eslint-disable-next-line import/prefer-default-export -export const TALLY_INTERNAL_ORIGIN = "@tally-internal" +export const TAHO_INTERNAL_ORIGIN = "@tally-internal" diff --git a/background/services/internal-ethereum-provider/db.ts b/background/services/internal-ethereum-provider/db.ts index aba98f5fac..eedbbb6116 100644 --- a/background/services/internal-ethereum-provider/db.ts +++ b/background/services/internal-ethereum-provider/db.ts @@ -1,7 +1,7 @@ import Dexie from "dexie" import { ETHEREUM } from "../../constants" import { EVMNetwork } from "../../networks" -import { TALLY_INTERNAL_ORIGIN } from "./constants" +import { TAHO_INTERNAL_ORIGIN } from "./constants" type NetworkForOrigin = { origin: string @@ -42,7 +42,7 @@ export class InternalEthereumProviderDatabase extends Dexie { this.on("populate", (tx) => tx.db .table("currentNetwork") - .add({ origin: TALLY_INTERNAL_ORIGIN, network: ETHEREUM }) + .add({ origin: TAHO_INTERNAL_ORIGIN, network: ETHEREUM }) ) } diff --git a/background/services/internal-ethereum-provider/index.ts b/background/services/internal-ethereum-provider/index.ts index eebff1ed8d..3f6bfc5091 100644 --- a/background/services/internal-ethereum-provider/index.ts +++ b/background/services/internal-ethereum-provider/index.ts @@ -33,7 +33,7 @@ import { parseSigningData, } from "../../utils/signing" import { getOrCreateDB, InternalEthereumProviderDatabase } from "./db" -import { TALLY_INTERNAL_ORIGIN } from "./constants" +import { TAHO_INTERNAL_ORIGIN } from "./constants" import { ROOTSTOCK } from "../../constants" import { EnrichedEVMTransactionRequest, @@ -145,7 +145,7 @@ export default class InternalEthereumProviderService extends BaseService result: await this.routeSafeRPCRequest( event.request.method, event.request.params, - TALLY_INTERNAL_ORIGIN + TAHO_INTERNAL_ORIGIN ), } logger.debug("internal response:", response) @@ -372,7 +372,7 @@ export default class InternalEthereumProviderService extends BaseService private async getCurrentInternalNetwork(): Promise { return this.db.getCurrentNetworkForOrigin( - TALLY_INTERNAL_ORIGIN + TAHO_INTERNAL_ORIGIN ) as Promise } @@ -398,7 +398,7 @@ export default class InternalEthereumProviderService extends BaseService origin: string ): Promise { const annotation = - origin === TALLY_INTERNAL_ORIGIN && + origin === TAHO_INTERNAL_ORIGIN && "annotation" in transactionRequest && transactionRequest.annotation !== undefined ? // We use `as` here as we know it's from a trusted source. diff --git a/background/services/provider-bridge/index.ts b/background/services/provider-bridge/index.ts index fa2737d367..528752b0be 100644 --- a/background/services/provider-bridge/index.ts +++ b/background/services/provider-bridge/index.ts @@ -8,7 +8,7 @@ import { EIP1193Error, RPCRequest, EIP1193_ERROR_CODES, - isTallyConfigPayload, + isTahoConfigPayload, } from "@tallyho/provider-bridge-shared" import { TransactionRequest as EthersTransactionRequest } from "@ethersproject/abstract-provider" import BaseService from "../base" @@ -35,7 +35,7 @@ import { parseRPCRequestParams, } from "./utils" import { toHexChainID } from "../../networks" -import { TALLY_INTERNAL_ORIGIN } from "../internal-ethereum-provider/constants" +import { TAHO_INTERNAL_ORIGIN } from "../internal-ethereum-provider/constants" type Events = ServiceLifecycleEvents & { requestPermission: PermissionRequest @@ -159,13 +159,13 @@ export default class ProviderBridgeService extends BaseService { ) const originPermission = await this.checkPermission(origin, network.chainID) - if (origin === TALLY_INTERNAL_ORIGIN) { + if (origin === TAHO_INTERNAL_ORIGIN) { // Explicitly disallow anyone who has managed to pretend to be the // internal provider. response.result = new EIP1193Error( EIP1193_ERROR_CODES.unauthorized ).toJSON() - } else if (isTallyConfigPayload(event.request)) { + } else if (isTahoConfigPayload(event.request)) { // let's start with the internal communication response.id = "tallyHo" response.result = { diff --git a/env.d.ts b/env.d.ts index 6d2987cd7c..82e5752648 100644 --- a/env.d.ts +++ b/env.d.ts @@ -42,6 +42,7 @@ type WalletProvider = BaseWalletProvider & { type TahoProvider = BaseWalletProvider & { isTally: true + isTaho: true tahoSetAsDefault: boolean send: (method: string, params: unknown[]) => void | Promise } @@ -49,7 +50,9 @@ type TahoProvider = BaseWalletProvider & { type WindowEthereum = WalletProvider & { isMetaMask?: boolean tallySetAsDefault?: boolean + tahoSetAsDefault?: boolean isTally?: boolean + isTaho?: boolean autoRefreshOnNetworkChange?: boolean } interface Window { @@ -57,7 +60,11 @@ interface Window { currentProvider: WalletProvider providers: WalletProvider[] shouldSetTallyForCurrentProvider: ( - shouldSetTally: boolean, + shouldSetTaho: boolean, + shouldReload?: boolean + ) => void + shouldSetTahoForCurrentProvider: ( + shouldSetTaho: boolean, shouldReload?: boolean ) => void routeToNewNonTahoDefault: (request: unknown) => Promise @@ -68,6 +75,7 @@ interface Window { addProvider: (newProvider: WalletProvider) => void } tally?: TahoProvider + taho?: TahoProvider ethereum?: WindowEthereum oldEthereum?: WindowEthereum } diff --git a/provider-bridge-shared/runtime-typechecks.ts b/provider-bridge-shared/runtime-typechecks.ts index e23f070e2f..39164c5121 100644 --- a/provider-bridge-shared/runtime-typechecks.ts +++ b/provider-bridge-shared/runtime-typechecks.ts @@ -2,9 +2,9 @@ import { RPCRequest, WindowResponseEvent, PortResponseEvent, - TallyConfigPayload, - TallyInternalCommunication, - TallyAccountPayload, + TahoConfigPayload, + TahoInternalCommunication, + TahoAccountPayload, } from "./types" export function getType(arg: unknown): string { @@ -80,19 +80,17 @@ export function isAllowedQueryParamPage( return Object.values(AllowedQueryParamPage).includes(url) } -export function isTallyInternalCommunication( +export function isTahoInternalCommunication( arg: unknown -): arg is TallyInternalCommunication { +): arg is TahoInternalCommunication { return isObject(arg) && arg.id === "tallyHo" } -export function isTallyConfigPayload(arg: unknown): arg is TallyConfigPayload { +export function isTahoConfigPayload(arg: unknown): arg is TahoConfigPayload { return isObject(arg) && arg.method === "tally_getConfig" } -export function isTallyAccountPayload( - arg: unknown -): arg is TallyAccountPayload { +export function isTahoAccountPayload(arg: unknown): arg is TahoAccountPayload { return ( isObject(arg) && arg.method === "tally_accountChanged" && diff --git a/provider-bridge-shared/types.ts b/provider-bridge-shared/types.ts index a426310f25..2304ebb180 100644 --- a/provider-bridge-shared/types.ts +++ b/provider-bridge-shared/types.ts @@ -57,12 +57,12 @@ export type PortTransport = { export type EthersSendCallback = (error: unknown, response: unknown) => void -export type TallyInternalCommunication = { +export type TahoInternalCommunication = { id: "tallyHo" - result: TallyConfigPayload | TallyAccountPayload + result: TahoConfigPayload | TahoAccountPayload } -export type TallyConfigPayload = { +export type TahoConfigPayload = { method: "tally_getConfig" defaultWallet: boolean chainId?: string @@ -70,7 +70,7 @@ export type TallyConfigPayload = { [prop: string]: unknown } -export type TallyAccountPayload = { +export type TahoAccountPayload = { method: "tally_accountChanged" address: Array } diff --git a/src/window-provider.ts b/src/window-provider.ts index ecc718d5b9..dad14ab677 100644 --- a/src/window-provider.ts +++ b/src/window-provider.ts @@ -181,6 +181,11 @@ Object.defineProperty(window, "tally", { writable: false, configurable: false, }) +Object.defineProperty(window, "taho", { + value: tahoWindowProvider, + writable: false, + configurable: false, +}) if (!window.walletRouter) { const existingProviders = @@ -202,21 +207,28 @@ if (!window.walletRouter) { Object.defineProperty(window, "walletRouter", { value: { - currentProvider: window.tally, + currentProvider: window.taho, lastInjectedProvider: wrappedLastInjectedProvider, - tallyProvider: window.tally, + tallyProvider: window.taho, + tahoProvider: window.taho, providers: dedupedProviders, shouldSetTallyForCurrentProvider( shouldSetTally: boolean, shouldReload = false ) { - if (shouldSetTally && this.currentProvider !== this.tallyProvider) { - this.currentProvider = this.tallyProvider + this.shouldSetTahoForCurrentProvider(shouldSetTally, shouldReload) + }, + shouldSetTahoForCurrentProvider( + shouldSetTaho: boolean, + shouldReload = false + ) { + if (shouldSetTaho && this.currentProvider !== this.tahoProvider) { + this.currentProvider = this.tahoProvider } else if ( - !shouldSetTally && - this.currentProvider === this.tallyProvider + !shouldSetTaho && + this.currentProvider === this.tahoProvider ) { - this.currentProvider = this.lastInjectedProvider ?? this.tallyProvider + this.currentProvider = this.lastInjectedProvider ?? this.tahoProvider } // Make the new "current provider" first in the provider list. This @@ -249,7 +261,7 @@ if (!window.walletRouter) { // where Taho is default, then default is turned off, but no other // provider is installed, so that we don't try to reroute back to Taho // as the only other provider. - if (this.currentProvider === this.tallyProvider) { + if (this.currentProvider === this.tahoProvider) { return Promise.reject( new Error("Only the Taho provider is installed.") ) @@ -267,7 +279,7 @@ if (!window.walletRouter) { }, reemitTahoEvent(event: string | symbol, ...args: unknown[]): boolean { if ( - this.currentProvider !== this.tallyProvider || + this.currentProvider !== this.tahoProvider || this.lastInjectedProvider === undefined || this.currentProvider === this.lastInjectedProvider ) { diff --git a/window-provider/index.ts b/window-provider/index.ts index 4086f3e76d..2d00b107f9 100644 --- a/window-provider/index.ts +++ b/window-provider/index.ts @@ -7,12 +7,12 @@ import { isPortResponseEvent, RequestArgument, EthersSendCallback, - isTallyConfigPayload, - TallyConfigPayload, + isTahoConfigPayload, + TahoConfigPayload, isEIP1193Error, - isTallyInternalCommunication, - TallyAccountPayload, - isTallyAccountPayload, + isTahoInternalCommunication, + TahoAccountPayload, + isTahoAccountPayload, } from "@tallyho/provider-bridge-shared" import { EventEmitter } from "events" import monitorForWalletConnectionPrompts from "./wallet-connection-handlers" @@ -36,6 +36,8 @@ export default class TahoWindowProvider extends EventEmitter { isTally: true = true + isTaho: true = true + isMetaMask = false tahoSetAsDefault = false @@ -65,17 +67,17 @@ export default class TahoWindowProvider extends EventEmitter { iconURL: "TODO", identityFlag: "isTally", checkIdentity: (provider: WalletProvider) => - !!provider && !!provider.isTally, + !!provider && !!provider.isTaho, } as const constructor(public transport: ProviderTransport) { super() const internalListener = (event: unknown) => { - let result: TallyConfigPayload | TallyAccountPayload + let result: TahoConfigPayload | TahoAccountPayload if ( isWindowResponseEvent(event) && - isTallyInternalCommunication(event.data) + isTahoInternalCommunication(event.data) ) { if ( event.origin !== this.transport.origin || // filter to messages claiming to be from the provider-bridge script @@ -88,17 +90,17 @@ export default class TahoWindowProvider extends EventEmitter { result = event.data.result } else if ( isPortResponseEvent(event) && - isTallyInternalCommunication(event) + isTahoInternalCommunication(event) ) { result = event.result } else { return } - if (isTallyConfigPayload(result)) { + if (isTahoConfigPayload(result)) { const wasTahoSetAsDefault = this.tahoSetAsDefault - window.walletRouter?.shouldSetTallyForCurrentProvider( + window.walletRouter?.shouldSetTahoForCurrentProvider( result.defaultWallet, false ) @@ -128,7 +130,7 @@ export default class TahoWindowProvider extends EventEmitter { if (result.chainId && result.chainId !== this.chainId) { this.handleChainIdChange(result.chainId) } - } else if (isTallyAccountPayload(result)) { + } else if (isTahoAccountPayload(result)) { this.handleAddressChange(result.address) } }