Skip to content

Commit

Permalink
Merge branch 'tallycash:main' into idriss-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
idrisssystem committed Apr 2, 2022
2 parents 294ee6f + 017aaf5 commit 79b4554
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 224 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"
207 changes: 74 additions & 133 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {

import { EIP712TypedData, HexString, KeyringTypes } from "./types"
import { SignedEVMTransaction } from "./networks"
import { AddressOnNetwork, NameOnNetwork } from "./accounts"
import { AccountBalance, AddressOnNetwork, NameOnNetwork } from "./accounts"

import rootReducer from "./redux-slices"
import {
Expand Down 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"
import {ResolvedIdrissAddressState} from "./redux-slices/idriss-resolver";
Expand Down Expand Up @@ -342,13 +341,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 @@ -507,13 +506,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 @@ -528,13 +524,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 @@ -548,11 +541,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 @@ -644,10 +634,13 @@ export default class Main extends BaseService<never> {

async connectChainService(): Promise<void> {
// Wire up chain service to account slice.
this.chainService.emitter.on("accountBalance", (accountWithBalance) => {
// The first account balance update will transition the account to loading.
this.store.dispatch(updateAccountBalance(accountWithBalance))
})
this.chainService.emitter.on(
"accountsWithBalances",
(accountWithBalance) => {
// The first account balance update will transition the account to loading.
this.store.dispatch(updateAccountBalance(accountWithBalance))
}
)

this.chainService.emitter.on("block", (block) => {
this.store.dispatch(blockSeen(block))
Expand Down Expand Up @@ -716,47 +709,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 @@ -836,21 +797,28 @@ export default class Main extends BaseService<never> {

async connectIndexingService(): Promise<void> {
this.indexingService.emitter.on(
"accountBalance",
async (accountWithBalance) => {
"accountsWithBalances",
async (accountsWithBalances) => {
const assetsToTrack = await this.indexingService.getAssetsToTrack()

// TODO support multi-network assets
const doesThisBalanceHaveAnAlreadyTrackedAsset = !!assetsToTrack.filter(
(t) => t.symbol === accountWithBalance.assetAmount.asset.symbol
)[0]
const filteredBalancesToDispatch: AccountBalance[] = []

if (
accountWithBalance.assetAmount.amount > 0 ||
doesThisBalanceHaveAnAlreadyTrackedAsset
) {
this.store.dispatch(updateAccountBalance(accountWithBalance))
}
accountsWithBalances.forEach((balance) => {
// TODO support multi-network assets
const doesThisBalanceHaveAnAlreadyTrackedAsset =
!!assetsToTrack.filter(
(t) => t.symbol === balance.assetAmount.asset.symbol
)[0]

if (
balance.assetAmount.amount > 0 ||
doesThisBalanceHaveAnAlreadyTrackedAsset
) {
filteredBalancesToDispatch.push(balance)
}
})

this.store.dispatch(updateAccountBalance(filteredBalancesToDispatch))
}
)

Expand Down Expand Up @@ -946,14 +914,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 @@ -986,14 +950,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 @@ -1025,11 +984,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 @@ -1052,17 +1008,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 @@ -1092,11 +1043,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 @@ -1114,16 +1062,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 @@ -1153,14 +1097,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
Loading

0 comments on commit 79b4554

Please sign in to comment.