Skip to content

Commit

Permalink
Second pass at updating tally -> taho in provider bridge/related
Browse files Browse the repository at this point in the history
Anything that is public API is duplicated as tally and taho, everything
else is renamed. Some string constants still say tally_ as checking for
breakage requires some extra diligence.:
  • Loading branch information
Shadowfiend committed Aug 11, 2023
1 parent 8537e3e commit 96b805a
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 49 deletions.
6 changes: 3 additions & 3 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -703,7 +703,7 @@ export default class Main extends BaseService<never> {
address: accounts[0].address,
network:
await this.internalEthereumProviderService.getCurrentOrDefaultNetworkForOrigin(
TALLY_INTERNAL_ORIGIN
TAHO_INTERNAL_ORIGIN
),
})
)
Expand Down Expand Up @@ -1363,7 +1363,7 @@ export default class Main extends BaseService<never> {
this.internalEthereumProviderService.routeSafeRPCRequest(
"wallet_switchEthereumChain",
[{ chainId: network.chainID }],
TALLY_INTERNAL_ORIGIN
TAHO_INTERNAL_ORIGIN
)
this.chainService.pollBlockPricesForNetwork(network.chainID)
this.store.dispatch(clearCustomGas())
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 2 additions & 2 deletions background/services/internal-ethereum-provider/db.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 })
)
}

Expand Down
8 changes: 4 additions & 4 deletions background/services/internal-ethereum-provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -145,7 +145,7 @@ export default class InternalEthereumProviderService extends BaseService<Events>
result: await this.routeSafeRPCRequest(
event.request.method,
event.request.params,
TALLY_INTERNAL_ORIGIN
TAHO_INTERNAL_ORIGIN
),
}
logger.debug("internal response:", response)
Expand Down Expand Up @@ -372,7 +372,7 @@ export default class InternalEthereumProviderService extends BaseService<Events>

private async getCurrentInternalNetwork(): Promise<EVMNetwork> {
return this.db.getCurrentNetworkForOrigin(
TALLY_INTERNAL_ORIGIN
TAHO_INTERNAL_ORIGIN
) as Promise<EVMNetwork>
}

Expand All @@ -398,7 +398,7 @@ export default class InternalEthereumProviderService extends BaseService<Events>
origin: string
): Promise<SignedTransaction> {
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.
Expand Down
8 changes: 4 additions & 4 deletions background/services/provider-bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -159,13 +159,13 @@ export default class ProviderBridgeService extends BaseService<Events> {
)

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 = {
Expand Down
10 changes: 9 additions & 1 deletion env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,29 @@ type WalletProvider = BaseWalletProvider & {

type TahoProvider = BaseWalletProvider & {
isTally: true
isTaho: true
tahoSetAsDefault: boolean
send: (method: string, params: unknown[]) => void | Promise<unknown>
}

type WindowEthereum = WalletProvider & {
isMetaMask?: boolean
tallySetAsDefault?: boolean
tahoSetAsDefault?: boolean
isTally?: boolean
isTaho?: boolean
autoRefreshOnNetworkChange?: boolean
}
interface Window {
walletRouter?: {
currentProvider: WalletProvider
providers: WalletProvider[]
shouldSetTallyForCurrentProvider: (
shouldSetTally: boolean,
shouldSetTaho: boolean,
shouldReload?: boolean
) => void
shouldSetTahoForCurrentProvider: (
shouldSetTaho: boolean,
shouldReload?: boolean
) => void
routeToNewNonTahoDefault: (request: unknown) => Promise<unknown>
Expand All @@ -68,6 +75,7 @@ interface Window {
addProvider: (newProvider: WalletProvider) => void
}
tally?: TahoProvider
taho?: TahoProvider
ethereum?: WindowEthereum
oldEthereum?: WindowEthereum
}
Expand Down
16 changes: 7 additions & 9 deletions provider-bridge-shared/runtime-typechecks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
RPCRequest,
WindowResponseEvent,
PortResponseEvent,
TallyConfigPayload,
TallyInternalCommunication,
TallyAccountPayload,
TahoConfigPayload,
TahoInternalCommunication,
TahoAccountPayload,
} from "./types"

export function getType(arg: unknown): string {
Expand Down Expand Up @@ -80,19 +80,17 @@ export function isAllowedQueryParamPage(
return Object.values<unknown>(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" &&
Expand Down
8 changes: 4 additions & 4 deletions provider-bridge-shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ 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
shouldReload?: boolean
[prop: string]: unknown
}

export type TallyAccountPayload = {
export type TahoAccountPayload = {
method: "tally_accountChanged"
address: Array<string>
}
30 changes: 21 additions & 9 deletions src/window-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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
Expand Down Expand Up @@ -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.")
)
Expand All @@ -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
) {
Expand Down
26 changes: 14 additions & 12 deletions window-provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -36,6 +36,8 @@ export default class TahoWindowProvider extends EventEmitter {

isTally: true = true

isTaho: true = true

isMetaMask = false

tahoSetAsDefault = false
Expand Down Expand Up @@ -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
Expand All @@ -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
)
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 96b805a

Please sign in to comment.