Skip to content

Commit

Permalink
Simplify the add ledger account message
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Oct 14, 2024
1 parent 413e1db commit 6b37bd8
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 43 deletions.
6 changes: 3 additions & 3 deletions packages/extension/src/background/accountMessaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ export const handleAccountMessage: HandleMessage<AccountMessage> = async ({
throw Error("you need an open session")
}

const { account, hdIndex, networkId } = msg.data
const { account } = msg.data
try {
const baseAccount = await wallet.importLedgerAccount(account, hdIndex, networkId)
const baseAccount = await wallet.importLedgerAccount(account)
return sendMessageToUi({
type: "ALPH_NEW_LEDGER_ACCOUNT_RES",
data: {
Expand All @@ -105,7 +105,7 @@ export const handleAccountMessage: HandleMessage<AccountMessage> = async ({

analytics.track("createAccount", {
status: "failure",
networkId: networkId,
networkId: account.networkId,
errorMessage: error,
})

Expand Down
14 changes: 1 addition & 13 deletions packages/extension/src/background/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,7 @@ export class Wallet extends AccountDiscovery {
}
}

public async importLedgerAccount(account: Account, hdIndex: number, networkId: string): Promise<BaseWalletAccount> {
const walletAccount: WalletAccount = {
address: account.address,
networkId: networkId,
signer: {
type: "ledger" as const,
publicKey: account.publicKey,
keyType: account.keyType,
derivationIndex: hdIndex,
group: groupOfAddress(account.address)
},
type: "alephium",
}
public async importLedgerAccount(walletAccount: WalletAccount): Promise<BaseWalletAccount> {
await this.walletStore.push([walletAccount])
await this.selectAccount(walletAccount)
return walletAccount
Expand Down
4 changes: 2 additions & 2 deletions packages/extension/src/shared/messages/AccountMessage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyType, Account } from "@alephium/web3";
import { KeyType } from "@alephium/web3";
import {
ArgentAccountType,
BaseWalletAccount,
Expand All @@ -15,7 +15,7 @@ export type AccountMessage =
}
}
| { type: "ALPH_NEW_ACCOUNT_REJ"; data: { error: string } }
| { type: "ALPH_NEW_LEDGER_ACCOUNT"; data: { account: Account; hdIndex: number, networkId: string } }
| { type: "ALPH_NEW_LEDGER_ACCOUNT"; data: { account: WalletAccount } }
| {
type: "ALPH_NEW_LEDGER_ACCOUNT_RES"
data: {
Expand Down
9 changes: 5 additions & 4 deletions packages/extension/src/ui/features/accounts/useAddAccount.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyType, Account } from "@alephium/web3"
import { KeyType } from "@alephium/web3"
import { useCallback } from "react"
import { useNavigate } from "react-router-dom"

Expand All @@ -7,6 +7,7 @@ import { getAccounts, selectAccount } from "../../services/backgroundAccounts"
import { recover } from "../recovery/recovery.service"
import { createAccount } from "./accounts.service"
import { importNewLedgerAccount } from '../../services/backgroundAccounts'
import { WalletAccount } from "../../../shared/wallet.model"

export const useAddAccount = () => {
const navigate = useNavigate()
Expand All @@ -22,10 +23,10 @@ export const useAddAccount = () => {
return { addAccount }
}

export const addLedgerAccount = async (networkId: string, account: Account, hdIndex: number) => {
await importNewLedgerAccount(account, hdIndex, networkId)
export const addLedgerAccount = async (account: WalletAccount) => {
await importNewLedgerAccount(account)
// switch background wallet to the account that was selected
await selectAccount({ address: account.address, networkId: networkId })
await selectAccount({ address: account.address, networkId: account.networkId })
}

export const getAllLedgerAccounts = async (networkId: string) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/extension/src/ui/features/ledger/start.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ export const LedgerStartScreen: FC = () => {

setDetecting(true)
try {
const [account, hdIndex] = await LedgerAlephium
const account = await LedgerAlephium
.create()
.then((ledger) => ledger.createNewAccount(networkId, addressGroup, keyType ?? "default"))
addLedgerAccount(networkId, account, hdIndex)
addLedgerAccount(account)
navigate(routes.ledgerDone())
} catch (e) {
console.error(e)
Expand Down
14 changes: 3 additions & 11 deletions packages/extension/src/ui/features/ledger/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ export class LedgerAlephium extends AccountDiscovery {
const existingLedgerAccounts = await getAllLedgerAccounts(networkId)
var index = 0
while (true) {
const [newAccount, hdIndex] = await this.getAccount(index, targetAddressGroup, keyType)
const newAccount = await this.deriveAccount(networkId, index, keyType, targetAddressGroup)
if (existingLedgerAccounts.find((account) => account.address === newAccount.address) === undefined) {
await this.app.close()
return [newAccount, hdIndex] as const
return newAccount
}
index = hdIndex + 1
index = newAccount.signer.derivationIndex + 1
}
}

Expand Down Expand Up @@ -118,11 +118,3 @@ export class LedgerAlephium extends AccountDiscovery {
return getNextPathIndex(indexes)
}
}

const isAddressGroupMatched = (account: WalletAccount, targetAddressGroup: number | undefined) => {
if (targetAddressGroup === undefined) {
return true
} else {
return account.signer.group === targetAddressGroup
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ const DeveloperSettings: FC = () => {
const result = await LedgerAlephium.create().then((ledger) => ledger.discoverActiveAccounts(currentNetwork.id))
const discoveredAccounts = mapWalletAccountsToAccounts(result)
for (const account of discoveredAccounts) {
await addLedgerAccount(
currentNetwork.id,
{ keyType: account.signer.keyType, address: account.address, group: account.signer.group, publicKey: account.signer.publicKey },
account.signer.derivationIndex
)
await addLedgerAccount(account)
setAccountName(account.networkId, account.address, getDefaultAccountNameByIndex(account, accountIndex))
accountIndex++
}
Expand Down
6 changes: 3 additions & 3 deletions packages/extension/src/ui/services/backgroundAccounts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyType, Account } from "@alephium/web3"
import { KeyType } from "@alephium/web3"
import { sendMessage, waitForMessage } from "../../shared/messages"
import {
ArgentAccountType,
Expand Down Expand Up @@ -33,8 +33,8 @@ export const discoverAccounts = async (networkId: string) => {
}
}

export const importNewLedgerAccount = async (account: Account, hdIndex: number, networkId: string) => {
sendMessage({ type: "ALPH_NEW_LEDGER_ACCOUNT", data: { account, hdIndex, networkId } })
export const importNewLedgerAccount = async (account: WalletAccount) => {
sendMessage({ type: "ALPH_NEW_LEDGER_ACCOUNT", data: { account } })
try {
return await Promise.race([
waitForMessage("ALPH_NEW_LEDGER_ACCOUNT_RES"),
Expand Down

0 comments on commit 6b37bd8

Please sign in to comment.