Skip to content

Commit

Permalink
basic ui and deps
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloCastellano committed Mar 20, 2022
1 parent e890bfc commit 20d119b
Show file tree
Hide file tree
Showing 13 changed files with 894 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .env.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ HIDE_SWAP=false
HIDE_IMPORT_DERIVATION_PATH=true
HIDE_CREATE_PHRASE=false
HIDE_IMPORT_LEDGER=false
HIDE_IMPORT_TREZOR=true
GAS_PRICE_POOLING_FREQUENCY=120
ETHEREUM_NETWORK=mainnet
PERSIST_UI_LOCATION=false
USE_MAINNET_FORK=false
MAINNET_FORK_URL="http://127.0.0.1:8545"
MAINNET_FORK_CHAIN_ID=1337
MAINNET_FORK_CHAIN_ID=1337
1 change: 1 addition & 0 deletions background/features/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ 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 HIDE_IMPORT_TREZOR = process.env.HIDE_IMPORT_TREZOR === "true"
export const PERSIST_UI_LOCATION = process.env.PERSIST_UI_LOCATION === "true"
export const USE_MAINNET_FORK = process.env.USE_MAINNET_FORK === "true"
61 changes: 58 additions & 3 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
TelemetryService,
ServiceCreatorFunction,
LedgerService,
TrezorService,
SigningService,
} from "./services"

Expand Down Expand Up @@ -86,7 +87,7 @@ import {
setUsbDeviceCount,
} from "./redux-slices/ledger"
import { ETHEREUM } from "./constants"
import { HIDE_IMPORT_LEDGER } from "./features/features"
import { HIDE_IMPORT_LEDGER, HIDE_IMPORT_TREZOR } from "./features/features"
import { clearApprovalInProgress } from "./redux-slices/0x-swap"
import { SignatureResponse, TXSignatureResponse } from "./services/signing"

Expand Down Expand Up @@ -338,6 +339,10 @@ export default class Main extends BaseService<never> {
? (Promise.resolve(null) as unknown as Promise<SigningService>)
: SigningService.create(keyringService, ledgerService, chainService)

const trezorService = HIDE_IMPORT_TREZOR
? (Promise.resolve(null) as unknown as Promise<TrezorService>)
: TrezorService.create()

let savedReduxState = {}
// Setting READ_REDUX_CACHE to false will start the extension with an empty
// initial state, which can be useful for development
Expand Down Expand Up @@ -375,7 +380,8 @@ export default class Main extends BaseService<never> {
await providerBridgeService,
await telemetryService,
await ledgerService,
await signingService
await signingService,
await trezorService
)
}

Expand Down Expand Up @@ -440,7 +446,14 @@ export default class Main extends BaseService<never> {
* A promise to the signing service which will route operations between the UI
* and the exact signing services.
*/
private signingService: SigningService
private signingService: SigningService,

/**
* A promise to the Trezor service, handling the communication
* with attached Trezor device
*/
private trezorService: TrezorService

) {
super({
initialLoadWaitExpired: {
Expand Down Expand Up @@ -502,6 +515,10 @@ export default class Main extends BaseService<never> {
servicesToBeStarted.push(this.signingService.startService())
}

if (!HIDE_IMPORT_TREZOR) {
servicesToBeStarted.push(this.trezorService.startService())
}

await Promise.all(servicesToBeStarted)
}

Expand All @@ -523,6 +540,10 @@ export default class Main extends BaseService<never> {
servicesToBeStopped.push(this.signingService.stopService())
}

if (!HIDE_IMPORT_TREZOR) {
servicesToBeStopped.push(this.trezorService.stopService())
}

await Promise.all(servicesToBeStopped)
await super.internalStopService()
}
Expand All @@ -542,6 +563,10 @@ export default class Main extends BaseService<never> {
this.connectSigningService()
}

if (!HIDE_IMPORT_TREZOR) {
this.connectTrezorService()
}

await this.connectChainService()

// FIXME Should no longer be necessary once transaction queueing enters the
Expand Down Expand Up @@ -624,6 +649,10 @@ export default class Main extends BaseService<never> {
return this.ledgerService.refreshConnectedLedger()
}

async connectTrezor(): Promise<string | null> {
return this.trezorService.refreshConnectedTrezor()
}

async getAccountEthBalanceUncached(address: string): Promise<bigint> {
const amountBigNumber =
await this.chainService.providers.ethereum.getBalance(address)
Expand Down Expand Up @@ -882,6 +911,32 @@ export default class Main extends BaseService<never> {
})
}

async connectTrezorService(): Promise<void> {
//this.store.dispatch(resetLedgerState())

this.trezorService.emitter.on("connected", ({ id, metadata }) => {
console.log("Got Trezor connected event " + id + " | " + metadata)
this.store.dispatch(
setDeviceConnectionStatus({
deviceID: id,
status: "available",
isBlindSigner: metadata.ethereumBlindSigner,
})
)
})

this.trezorService.emitter.on("disconnected", ({ id }) => {
console.log("Got Trezor disconnected event " + id)
this.store.dispatch(
setDeviceConnectionStatus({ deviceID: id, status: "disconnected" })
)
})

this.trezorService.emitter.on("usbDeviceCount", (usbDeviceCount) => {
console.log("Got Trezor usbDeviceCount event " + usbDeviceCount)
})
}

async connectKeyringService(): Promise<void> {
this.keyringService.emitter.on("keyrings", (keyrings) => {
this.store.dispatch(updateKeyrings(keyrings))
Expand Down
1 change: 1 addition & 0 deletions background/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"ethers": "^5.5.1",
"node-fetch": "^2.6.1",
"siwe": "^1.1.0",
"trezor-connect": "^8.2.7",
"util": "^0.12.4",
"webextension-polyfill": "^0.8.0"
},
Expand Down
1 change: 1 addition & 0 deletions background/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export { default as ProviderBridgeService } from "./provider-bridge"
export { default as InternalEthereumProviderService } from "./internal-ethereum-provider"
export { default as TelemetryService } from "./telemetry"
export { default as LedgerService } from "./ledger"
export { default as TrezorService } from "./trezor"
export { default as SigningService } from "./signing"
42 changes: 42 additions & 0 deletions background/services/trezor/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Dexie from "dexie"
import { normalizeEVMAddress } from "../../lib/utils"
import { HexString } from "../../types"

export interface TrezorAccount {
trezorId: string
address: HexString
path: string
}

export class TrezorDatabase extends Dexie {
private trezor!: Dexie.Table<TrezorAccount, number>

constructor() {
super("tally/trezor")

this.version(1).stores({
ledger: "&address,trezorId",
})
}

async addAccount(account: TrezorAccount): Promise<void> {
await this.trezor.add(account)
}

async getAccountByAddress(address: HexString): Promise<TrezorAccount | null> {
return (
(await this.trezor
.where("address")
.equals(normalizeEVMAddress(address))
.first()) ?? null
)
}

async getAllAccountsByTrezorId(trezorId: string): Promise<TrezorAccount[]> {
return this.trezor.where("trezorId").equals(trezorId).toArray()
}
}

export async function getOrCreateDB(): Promise<TrezorDatabase> {
return new TrezorDatabase()
}

0 comments on commit 20d119b

Please sign in to comment.