Skip to content

Commit

Permalink
Remove feature flag for Ledger support
Browse files Browse the repository at this point in the history
The feature is currently enabled in current distributed builds without any
observable regressions. Therefore removal of the flag is justifiable.
  • Loading branch information
Balint Ferencz committed Apr 1, 2022
1 parent 42b7752 commit d1bad36
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 138 deletions.
1 change: 0 additions & 1 deletion .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ HIDE_EARN_PAGE=true
HIDE_ADD_SEED=false
HIDE_IMPORT_DERIVATION_PATH=true
HIDE_CREATE_PHRASE=false
HIDE_IMPORT_LEDGER=false
GAS_PRICE_POOLING_FREQUENCY=120
ETHEREUM_NETWORK=mainnet
PERSIST_UI_LOCATION=true
Expand Down
1 change: 0 additions & 1 deletion background/features/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ export const HIDE_EARN_PAGE = process.env.HIDE_EARN_PAGE === "true"
export const HIDE_IMPORT_DERIVATION_PATH =
process.env.HIDE_IMPORT_DERIVATION_PATH === "true"
export const HIDE_CREATE_PHRASE = process.env.HIDE_CREATE_PHRASE === "true"
export const HIDE_IMPORT_LEDGER = process.env.HIDE_IMPORT_LEDGER === "true"
export const PERSIST_UI_LOCATION = process.env.PERSIST_UI_LOCATION === "true"
export const USE_MAINNET_FORK = process.env.USE_MAINNET_FORK === "true"
163 changes: 47 additions & 116 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ import {
setUsbDeviceCount,
} from "./redux-slices/ledger"
import { ETHEREUM } from "./constants"
import { HIDE_IMPORT_LEDGER } from "./features/features"
import { clearApprovalInProgress } from "./redux-slices/0x-swap"
import { SignatureResponse, TXSignatureResponse } from "./services/signing"

Expand Down Expand Up @@ -341,13 +340,13 @@ export default class Main extends BaseService<never> {

const telemetryService = TelemetryService.create()

const ledgerService = HIDE_IMPORT_LEDGER
? (Promise.resolve(null) as unknown as Promise<LedgerService>)
: LedgerService.create()
const ledgerService = LedgerService.create()

const signingService = HIDE_IMPORT_LEDGER
? (Promise.resolve(null) as unknown as Promise<SigningService>)
: SigningService.create(keyringService, ledgerService, chainService)
const signingService = SigningService.create(
keyringService,
ledgerService,
chainService
)

let savedReduxState = {}
// Setting READ_REDUX_CACHE to false will start the extension with an empty
Expand Down Expand Up @@ -506,13 +505,10 @@ export default class Main extends BaseService<never> {
this.internalEthereumProviderService.startService(),
this.providerBridgeService.startService(),
this.telemetryService.startService(),
this.ledgerService.startService(),
this.signingService.startService(),
]

if (!HIDE_IMPORT_LEDGER) {
servicesToBeStarted.push(this.ledgerService.startService())
servicesToBeStarted.push(this.signingService.startService())
}

await Promise.all(servicesToBeStarted)
}

Expand All @@ -527,13 +523,10 @@ export default class Main extends BaseService<never> {
this.internalEthereumProviderService.stopService(),
this.providerBridgeService.stopService(),
this.telemetryService.stopService(),
this.ledgerService.stopService(),
this.signingService.stopService(),
]

if (!HIDE_IMPORT_LEDGER) {
servicesToBeStopped.push(this.ledgerService.stopService())
servicesToBeStopped.push(this.signingService.stopService())
}

await Promise.all(servicesToBeStopped)
await super.internalStopService()
}
Expand All @@ -547,11 +540,8 @@ export default class Main extends BaseService<never> {
this.connectPreferenceService()
this.connectEnrichmentService()
this.connectTelemetryService()

if (!HIDE_IMPORT_LEDGER) {
this.connectLedgerService()
this.connectSigningService()
}
this.connectLedgerService()
this.connectSigningService()

await this.connectChainService()

Expand Down Expand Up @@ -715,47 +705,15 @@ export default class Main extends BaseService<never> {
transactionConstructionSliceEmitter.on(
"requestSignature",
async ({ transaction, method }) => {
if (HIDE_IMPORT_LEDGER) {
const network = this.chainService.resolveNetwork(transaction)
if (typeof network === "undefined") {
throw new Error(`Unknown chain ID ${transaction.chainID}.`)
}

const transactionWithNonce =
await this.chainService.populateEVMTransactionNonce(transaction)

try {
const signedTransactionResult =
await this.keyringService.signTransaction(
{
address: transaction.from,
network: this.chainService.ethereumNetwork,
},
transactionWithNonce
)
await this.store.dispatch(
transactionSigned(signedTransactionResult)
)
} catch (exception) {
logger.error(
"Error signing transaction; releasing nonce",
exception
)
this.chainService.releaseEVMTransactionNonce(transactionWithNonce)
}
} else {
try {
const signedTransactionResult =
await this.signingService.signTransaction(transaction, method)
await this.store.dispatch(
transactionSigned(signedTransactionResult)
)
} catch (exception) {
logger.error("Error signing transaction", exception)
this.store.dispatch(
clearTransactionState(TransactionConstructionStatus.Idle)
)
}
try {
const signedTransactionResult =
await this.signingService.signTransaction(transaction, method)
await this.store.dispatch(transactionSigned(signedTransactionResult))
} catch (exception) {
logger.error("Error signing transaction", exception)
this.store.dispatch(
clearTransactionState(TransactionConstructionStatus.Idle)
)
}
}
)
Expand Down Expand Up @@ -945,14 +903,10 @@ export default class Main extends BaseService<never> {
})

keyringSliceEmitter.on("deriveAddress", async (keyringID) => {
if (HIDE_IMPORT_LEDGER) {
await this.keyringService.deriveAddress(keyringID)
} else {
await this.signingService.deriveAddress({
type: "keyring",
accountID: keyringID,
})
}
await this.signingService.deriveAddress({
type: "keyring",
accountID: keyringID,
})
})

keyringSliceEmitter.on("generateNewKeyring", async () => {
Expand Down Expand Up @@ -985,14 +939,9 @@ export default class Main extends BaseService<never> {
this.store.dispatch(updateTransactionOptions(payload))

const clear = () => {
if (HIDE_IMPORT_LEDGER) {
// Ye olde mutual dependency.
// eslint-disable-next-line @typescript-eslint/no-use-before-define
this.keyringService.emitter.off("signedTx", resolveAndClear)
} else {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
this.signingService.emitter.off("signingTxResponse", handleAndClear)
}
// eslint-disable-next-line @typescript-eslint/no-use-before-define
this.signingService.emitter.off("signingTxResponse", handleAndClear)

transactionConstructionSliceEmitter.off(
"signatureRejected",
// eslint-disable-next-line @typescript-eslint/no-use-before-define
Expand Down Expand Up @@ -1024,11 +973,8 @@ export default class Main extends BaseService<never> {
rejecter()
}

if (HIDE_IMPORT_LEDGER) {
this.keyringService.emitter.on("signedTx", resolveAndClear)
} else {
this.signingService.emitter.on("signingTxResponse", handleAndClear)
}
this.signingService.emitter.on("signingTxResponse", handleAndClear)

transactionConstructionSliceEmitter.on(
"signatureRejected",
rejectAndClear
Expand All @@ -1051,17 +997,12 @@ export default class Main extends BaseService<never> {
this.store.dispatch(typedDataRequest(enrichedsignTypedDataRequest))

const clear = () => {
if (HIDE_IMPORT_LEDGER) {
// Ye olde mutual dependency.
this.signingService.emitter.off(
"signingDataResponse",
// eslint-disable-next-line @typescript-eslint/no-use-before-define
this.keyringService.emitter.off("signedData", resolveAndClear)
} else {
this.signingService.emitter.off(
"signingDataResponse",
// eslint-disable-next-line @typescript-eslint/no-use-before-define
handleAndClear
)
}
handleAndClear
)

signingSliceEmitter.off(
"signatureRejected",
// eslint-disable-next-line @typescript-eslint/no-use-before-define
Expand Down Expand Up @@ -1091,11 +1032,8 @@ export default class Main extends BaseService<never> {
rejecter()
}

if (HIDE_IMPORT_LEDGER) {
this.keyringService.emitter.on("signedData", resolveAndClear)
} else {
this.signingService.emitter.on("signingDataResponse", handleAndClear)
}
this.signingService.emitter.on("signingDataResponse", handleAndClear)

signingSliceEmitter.on("signatureRejected", rejectAndClear)
}
)
Expand All @@ -1113,16 +1051,12 @@ export default class Main extends BaseService<never> {
this.store.dispatch(signDataRequest(payload))

const clear = () => {
if (HIDE_IMPORT_LEDGER) {
this.signingService.emitter.off(
"personalSigningResponse",
// eslint-disable-next-line @typescript-eslint/no-use-before-define
this.keyringService.emitter.off("signedData", resolveAndClear)
} else {
this.signingService.emitter.off(
"personalSigningResponse",
// eslint-disable-next-line @typescript-eslint/no-use-before-define
handleAndClear
)
}
handleAndClear
)

signingSliceEmitter.off(
"signatureRejected",
// eslint-disable-next-line @typescript-eslint/no-use-before-define
Expand Down Expand Up @@ -1152,14 +1086,11 @@ export default class Main extends BaseService<never> {
rejecter()
}

if (HIDE_IMPORT_LEDGER) {
this.keyringService.emitter.on("signedData", resolveAndClear)
} else {
this.signingService.emitter.on(
"personalSigningResponse",
handleAndClear
)
}
this.signingService.emitter.on(
"personalSigningResponse",
handleAndClear
)

signingSliceEmitter.on("signatureRejected", rejectAndClear)
}
)
Expand Down
4 changes: 2 additions & 2 deletions background/redux-slices/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { combineReducers } from "redux"

import { HIDE_EARN_PAGE, HIDE_IMPORT_LEDGER } from "../features/features"
import { HIDE_EARN_PAGE } from "../features/features"

import accountsReducer from "./accounts"
import assetsReducer from "./assets"
Expand All @@ -26,7 +26,7 @@ const mainReducer = combineReducers({
ui: uiReducer,
dappPermission: dappPermissionReducer,
signing: signingReducer,
...(HIDE_IMPORT_LEDGER ? {} : { ledger: ledgerReducer }),
ledger: ledgerReducer,
...(HIDE_EARN_PAGE ? {} : { earn: earnReducer }),
})

Expand Down
14 changes: 4 additions & 10 deletions background/services/keyring/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { EIP1559TransactionRequest, SignedEVMTransaction } from "../../networks"
import BaseService from "../base"
import { ETH, FORK, MINUTE } from "../../constants"
import { ethersTransactionRequestFromEIP1559TransactionRequest } from "../chain/utils"
import { HIDE_IMPORT_LEDGER, USE_MAINNET_FORK } from "../../features/features"
import { USE_MAINNET_FORK } from "../../features/features"
import { AddressOnNetwork } from "../../accounts"
import logger from "../../lib/logger"

Expand Down Expand Up @@ -505,9 +505,7 @@ export default class KeyringService extends BaseService<Events> {
asset: ETH,
network: USE_MAINNET_FORK ? FORK : network,
}
if (HIDE_IMPORT_LEDGER) {
this.emitter.emit("signedTx", signedTx)
}

return signedTx
}
/**
Expand Down Expand Up @@ -538,9 +536,7 @@ export default class KeyringService extends BaseService<Events> {
typesForSigning,
message
)
if (HIDE_IMPORT_LEDGER) {
this.emitter.emit("signedData", signature)
}

return signature
} catch (error) {
throw new Error("Signing data failed")
Expand All @@ -567,9 +563,7 @@ export default class KeyringService extends BaseService<Events> {
const keyring = await this.#findKeyring(account)
try {
const signature = await keyring.signMessage(account, signingData)
if (HIDE_IMPORT_LEDGER) {
this.emitter.emit("signedData", signature)
}

return signature
} catch (error) {
throw new Error("Signing data failed")
Expand Down
4 changes: 1 addition & 3 deletions background/services/ledger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { getOrCreateDB, LedgerAccount, LedgerDatabase } from "./db"
import { ethersTransactionRequestFromEIP1559TransactionRequest } from "../chain/utils"
import { ETH } from "../../constants"
import { normalizeEVMAddress } from "../../lib/utils"
import { HIDE_IMPORT_LEDGER } from "../../features/features"

enum LedgerType {
UNKNOWN,
Expand All @@ -40,8 +39,7 @@ export const LedgerProductDatabase = {
LEDGER_NANO_X: { productId: 0x4015 },
}

export const isLedgerSupported =
!HIDE_IMPORT_LEDGER && typeof navigator.usb === "object"
export const isLedgerSupported = typeof navigator.usb === "object"

const TestedProductId = (productId: number): boolean => {
return Object.values(LedgerProductDatabase).some(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import { useHistory } from "react-router-dom"
import { ETHEREUM } from "@tallyho/tally-background/constants/networks"
import { AccountType } from "@tallyho/tally-background/redux-slices/accounts"
import { HIDE_IMPORT_LEDGER } from "@tallyho/tally-background/features/features"
import {
normalizeEVMAddress,
sameEVMAddress,
Expand Down Expand Up @@ -179,12 +178,9 @@ export default function AccountsNotificationPanelAccounts({
AccountType.Internal,
AccountType.Imported,
AccountType.ReadOnly,
AccountType.Ledger,
]

if (!HIDE_IMPORT_LEDGER) {
accountTypes.push(AccountType.Ledger)
}

return (
<div className="switcher_wrap">
{accountTypes
Expand Down

0 comments on commit d1bad36

Please sign in to comment.