Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warmup Filecoin Ledger account before transaction (uplift to 1.44.x) #14802

Merged
merged 2 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ test('Check locks for device', () => {
test('Extract accounts from locked device', () => {
const ledgerHardwareKeyring = new FilecoinLedgerKeyring()
ledgerHardwareKeyring.unlock = async function () {
return { success: false, error: 'braveWalletUnlockError' }
return { success: false, error: 'braveWalletUnlockError', code: 'unlockError' }
}
return expect(ledgerHardwareKeyring.getAccounts(-2, 1, CoinType.TEST))
.resolves.toStrictEqual({ error: 'braveWalletUnlockError', success: false })
.resolves.toStrictEqual({ error: 'braveWalletUnlockError', success: false, code: 'unlockError' })
})

test('Sign transaction locked device, unlock error', () => {
const ledgerHardwareKeyring = new FilecoinLedgerKeyring()
ledgerHardwareKeyring.unlock = async function () {
return { success: false, error: 'braveWalletUnlockError' }
return { success: false, error: 'braveWalletUnlockError', code: 'unlockError' }
}
ledgerHardwareKeyring.provider = new MockApp() as LedgerProvider
const message = {
Expand All @@ -118,7 +118,7 @@ test('Sign transaction locked device, unlock error', () => {
}

return expect(ledgerHardwareKeyring.signTransaction(JSON.stringify(message)))
.resolves.toStrictEqual({ success: false, error: 'braveWalletUnlockError' })
.resolves.toStrictEqual({ success: false, error: 'braveWalletUnlockError', code: 'unlockError' })
})

test('Sign transaction locked device, success', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class FilecoinLedgerKeyring implements LedgerFilecoinKeyring {
getAccounts = async (from: number, to: number, network: FilecoinNetwork): Promise<GetAccountsHardwareOperationResult> => {
const unlocked = await this.unlock()
if (!unlocked.success || !this.provider) {
return unlocked
return { success: false, error: unlocked.error, code: unlocked.code }
}
from = (from < 0) ? 0 : from
const app: LedgerProvider = this.provider
Expand Down Expand Up @@ -89,14 +89,14 @@ export default class FilecoinLedgerKeyring implements LedgerFilecoinKeyring {
}
try {
if (!await this.makeApp() || !this.provider) {
return { success: false }
return { success: false, code: 'unlockError' }
}
const app: LedgerProvider = this.provider
const address = await app.getAccounts(0, 1, CoinType.TEST)
this.deviceId = address[0]
return { success: this.isUnlocked() }
} catch (e) {
return { success: false, error: getLocale('braveWalletLedgerFilecoinUnlockError') }
return { success: false, error: getLocale('braveWalletLedgerFilecoinUnlockError'), code: 'unlockError' }
}
}

Expand All @@ -107,7 +107,7 @@ export default class FilecoinLedgerKeyring implements LedgerFilecoinKeyring {
signTransaction = async (message: string): Promise<SignHardwareOperationResult> => {
const unlocked = await this.unlock()
if (!unlocked.success || !this.provider) {
return { success: false, error: unlocked.error }
return { success: false, error: unlocked.error, code: unlocked.code }
}
try {
const parsed = JSON.parse(message)
Expand All @@ -122,6 +122,10 @@ export default class FilecoinLedgerKeyring implements LedgerFilecoinKeyring {
Method: parsed.Method,
Params: parsed.Params
}

// Accounts must be warmed up before signing.
await this.provider?.getAccounts()

const signed: SignedLotusMessage = await this.provider?.sign(parsed.From, lotusMessage)
return { success: true, payload: signed }
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ export interface Props {
onClickInstructions: () => void
}

function getAppName (coinType: BraveWallet.CoinType): string {
switch (coinType) {
case BraveWallet.CoinType.SOL:
return 'Solana'
case BraveWallet.CoinType.FIL:
return 'Filecoin'
default:
return 'Ethereum'
}
}

export const ConnectHardwareWalletPanel = ({
onCancel,
walletName,
Expand All @@ -60,7 +71,7 @@ export const ConnectHardwareWalletPanel = ({
}

if (hardwareWalletCode === 'openLedgerApp') {
let network = (coinType === BraveWallet.CoinType.SOL) ? 'Solana' : 'Ethereum'
let network = getAppName(coinType)
return getLocale('braveWalletConnectHardwarePanelOpenApp')
.replace('$1', network)
.replace('$2', walletName)
Expand Down