Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Provide HDMode where needed (almost everywhere) #2379

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
18 changes: 10 additions & 8 deletions src/main/api/ledger/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import * as E from 'fp-ts/Either'

import { IPCLedgerAdddressParams, LedgerError, LedgerErrorId } from '../../../shared/api/types'
import { isError } from '../../../shared/utils/guard'
import { isError, isEthHDMode } from '../../../shared/utils/guard'
import { WalletAddress } from '../../../shared/wallet/types'
import { getAddress as getBNBAddress, verifyAddress as verifyBNBAddress } from './binance/address'
import { getAddress as getBTCAddress, verifyAddress as verifyBTCAddress } from './bitcoin/address'
Expand All @@ -27,7 +27,7 @@ export const getAddress = async ({
chain,
network,
walletIndex,
ethDerivationMode
hdMode
}: IPCLedgerAdddressParams): Promise<E.Either<LedgerError, WalletAddress>> => {
try {
let res: E.Either<LedgerError, WalletAddress>
Expand All @@ -52,13 +52,13 @@ export const getAddress = async ({
res = await getDOGEAddress(transport, network, walletIndex)
break
case ETHChain: {
if (!ethDerivationMode) {
if (!isEthHDMode(hdMode)) {
res = E.left({
errorId: LedgerErrorId.INVALID_ETH_DERIVATION_MODE,
msg: `'ethDerivationMode' is needed for ETH to get Ledger address`
msg: `Invaid 'EthHDMode' - needed for ETH to get Ledger address`
})
} else {
res = await getETHAddress(transport, walletIndex, ethDerivationMode)
res = await getETHAddress({ transport, walletIndex, ethHdMode: hdMode })
}
break
}
Expand All @@ -81,7 +81,7 @@ export const getAddress = async ({
}
}

export const verifyLedgerAddress = async ({ chain, network, walletIndex }: IPCLedgerAdddressParams) => {
export const verifyLedgerAddress = async ({ chain, network, walletIndex, hdMode }: IPCLedgerAdddressParams) => {
const transport = await TransportNodeHidSingleton.open()
let result = false
switch (chain) {
Expand All @@ -103,9 +103,11 @@ export const verifyLedgerAddress = async ({ chain, network, walletIndex }: IPCLe
case DOGEChain:
result = await verifyDOGEAddress({ transport, network, walletIndex })
break
case ETHChain:
result = await verifyETHAddress(transport, walletIndex)
case ETHChain: {
if (!isEthHDMode(hdMode)) throw Error(`Invaid 'EthHDMode' - needed for ETH to verify Ledger address`)
result = await verifyETHAddress({ transport, walletIndex, ethHdMode: hdMode })
break
}
case CosmosChain:
result = await verifyCOSMOSAddress(transport, walletIndex)
break
Expand Down
2 changes: 1 addition & 1 deletion src/main/api/ledger/binance/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const getAddress = async (
if (pk) {
// get address from pubkey
const address = crypto.getAddressFromPublicKey(pk.toString('hex'), prefix)
return E.right({ address, chain: BNBChain, type: 'ledger', walletIndex })
return E.right({ address, chain: BNBChain, type: 'ledger', walletIndex, hdMode: 'default' })
} else {
return E.left({
errorId: LedgerErrorId.INVALID_PUBKEY,
Expand Down
2 changes: 1 addition & 1 deletion src/main/api/ledger/bitcoin/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const getAddress = async (
const { bitcoinAddress } = await app.getWalletPublicKey(derivePath, {
format: 'bech32' // bech32 format with 84' paths
})
return E.right({ address: bitcoinAddress, chain: BTCChain, type: 'ledger', walletIndex })
return E.right({ address: bitcoinAddress, chain: BTCChain, type: 'ledger', walletIndex, hdMode: 'default' })
} catch (error) {
return E.left({
errorId: LedgerErrorId.GET_ADDRESS_FAILED,
Expand Down
2 changes: 1 addition & 1 deletion src/main/api/ledger/bitcoincash/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const getAddress = async (
// @see https://github.com/LedgerHQ/ledgerjs/blob/master/packages/hw-app-btc/README.md#parameters-2
format: 'cashaddr'
})
return E.right({ address: bchAddress, chain: BCHChain, type: 'ledger', walletIndex })
return E.right({ address: bchAddress, chain: BCHChain, type: 'ledger', walletIndex, hdMode: 'default' })
} catch (error) {
return E.left({
errorId: LedgerErrorId.GET_ADDRESS_FAILED,
Expand Down
2 changes: 1 addition & 1 deletion src/main/api/ledger/cosmos/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const getAddress = async (
msg: `Getting 'address' from Ledger's Cosmos app failed`
})
}
return E.right({ address, chain: CosmosChain, type: 'ledger', walletIndex })
return E.right({ address, chain: CosmosChain, type: 'ledger', walletIndex, hdMode: 'default' })
} catch (error) {
return E.left({
errorId: LedgerErrorId.GET_ADDRESS_FAILED,
Expand Down
2 changes: 1 addition & 1 deletion src/main/api/ledger/doge/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const getAddress = async (
// @see https://github.com/LedgerHQ/ledgerjs/blob/master/packages/hw-app-btc/README.md#parameters-2
format: 'legacy'
})
return E.right({ address, chain: DOGEChain, type: 'ledger', walletIndex })
return E.right({ address, chain: DOGEChain, type: 'ledger', walletIndex, hdMode: 'default' })
} catch (error) {
return E.left({
errorId: LedgerErrorId.GET_ADDRESS_FAILED,
Expand Down
34 changes: 22 additions & 12 deletions src/main/api/ledger/ethereum/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ import * as E from 'fp-ts/Either'

import { LedgerError, LedgerErrorId } from '../../../../shared/api/types'
import { getDerivationPath } from '../../../../shared/ethereum/ledger'
import { EthDerivationMode } from '../../../../shared/ethereum/types'
import { EthHDMode } from '../../../../shared/ethereum/types'
import { isError } from '../../../../shared/utils/guard'
import { WalletAddress } from '../../../../shared/wallet/types'
import { getDerivationMode } from './common'

export const getAddress = async (
transport: Transport,
walletIndex: number,
mode: EthDerivationMode
): Promise<E.Either<LedgerError, WalletAddress>> => {
export const getAddress = async ({
transport,
walletIndex,
ethHdMode
}: {
transport: Transport
walletIndex: number
ethHdMode: EthHDMode
}): Promise<E.Either<LedgerError, WalletAddress>> => {
try {
const app = new EthApp(transport)
const path = getDerivationPath(walletIndex, mode)
const path = getDerivationPath(walletIndex, ethHdMode)
const { address } = await app.getAddress(path)

if (address) {
return E.right({ address, chain: ETHChain, type: 'ledger', walletIndex })
return E.right({ address, chain: ETHChain, type: 'ledger', walletIndex, hdMode: ethHdMode })
} else {
return E.left({
errorId: LedgerErrorId.INVALID_PUBKEY,
Expand All @@ -36,10 +39,17 @@ export const getAddress = async (
}
}

export const verifyAddress = async (transport: Transport, walletIndex: number) => {
export const verifyAddress = async ({
transport,
walletIndex,
ethHdMode
}: {
transport: Transport
walletIndex: number
ethHdMode: EthHDMode
}) => {
const app = new EthApp(transport)
const mode = await getDerivationMode()
const path = getDerivationPath(walletIndex, mode)
const path = getDerivationPath(walletIndex, ethHdMode)
const _ = await app.getAddress(path, true)
return true
}
7 changes: 3 additions & 4 deletions src/main/api/ledger/ethereum/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import { IPCLedgerApproveERC20TokenParams } from '../../../../shared/api/io'
import { DEFAULT_APPROVE_GAS_LIMIT_FALLBACK, FEE_BOUNDS } from '../../../../shared/ethereum/const'
import { getDerivationPath } from '../../../../shared/ethereum/ledger'
import { toClientNetwork } from '../../../../shared/utils/client'
import { getDerivationMode } from './common'
import { LedgerSigner } from './LedgerSigner'

export const approveLedgerERC20Token = async ({
network,
contractAddress,
spenderAddress,
walletIndex
walletIndex,
ethHdMode
}: IPCLedgerApproveERC20TokenParams): Promise<TxHash> => {
const { ethplorerApiKey, ethplorerUrl } = getEthplorerCreds()

Expand All @@ -35,8 +35,7 @@ export const approveLedgerERC20Token = async ({

const transport = await TransportNodeHidSingleton.open()
const app = new EthApp(transport)
const mode = await getDerivationMode()
const path = getDerivationPath(walletIndex, mode)
const path = getDerivationPath(walletIndex, ethHdMode)
const provider = client.getProvider()
const signer = new LedgerSigner({ provider, path, app })

Expand Down
9 changes: 0 additions & 9 deletions src/main/api/ledger/ethereum/common.ts

This file was deleted.

16 changes: 9 additions & 7 deletions src/main/api/ledger/ethereum/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { getInfuraCreds } from '../../../../shared/api/infura'
import { LedgerError, LedgerErrorId, Network } from '../../../../shared/api/types'
import { FEE_BOUNDS } from '../../../../shared/ethereum/const'
import { getDerivationPath } from '../../../../shared/ethereum/ledger'
import { EthHDMode } from '../../../../shared/ethereum/types'
import { toClientNetwork } from '../../../../shared/utils/client'
import { isError } from '../../../../shared/utils/guard'
import { getDerivationMode } from './common'
import { LedgerSigner } from './LedgerSigner'

/**
Expand All @@ -28,7 +28,8 @@ export const send = async ({
memo,
recipient,
feeOption,
walletIndex
walletIndex,
ethHDMode
}: {
asset: Asset
transport: Transport
Expand All @@ -38,6 +39,7 @@ export const send = async ({
memo?: string
feeOption: FeeOption
walletIndex: number
ethHDMode: EthHDMode
}): Promise<E.Either<LedgerError, TxHash>> => {
try {
const { ethplorerApiKey, ethplorerUrl } = getEthplorerCreds()
Expand All @@ -56,8 +58,7 @@ export const send = async ({
})

const app = new EthApp(transport)
const mode = await getDerivationMode()
const path = getDerivationPath(walletIndex, mode)
const path = getDerivationPath(walletIndex, ethHDMode)
const provider = client.getProvider()
const signer = new LedgerSigner({ provider, path, app })

Expand Down Expand Up @@ -98,7 +99,8 @@ export const deposit = async ({
memo,
recipient,
walletIndex,
feeOption
feeOption,
ethHDMode
}: {
asset: Asset
router: Address
Expand All @@ -109,6 +111,7 @@ export const deposit = async ({
memo?: string
walletIndex: number
feeOption: FeeOption
ethHDMode: EthHDMode
}): Promise<E.Either<LedgerError, TxHash>> => {
try {
const address = ETH.getAssetAddress(asset)
Expand Down Expand Up @@ -138,8 +141,7 @@ export const deposit = async ({
})

const app = new EthApp(transport)
const mode = await getDerivationMode()
const path = getDerivationPath(walletIndex, mode)
const path = getDerivationPath(walletIndex, ethHDMode)
const provider = client.getProvider()
const signer = new LedgerSigner({ provider, path, app })

Expand Down
2 changes: 1 addition & 1 deletion src/main/api/ledger/litecoin/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const getAddress = async (
const { bitcoinAddress: ltcAddress } = await app.getWalletPublicKey(derivePath, {
format: 'bech32' // bech32 format with 84' paths
})
return E.right({ address: ltcAddress, chain: LTCChain, type: 'ledger', walletIndex })
return E.right({ address: ltcAddress, chain: LTCChain, type: 'ledger', walletIndex, hdMode: 'default' })
} catch (error) {
return E.left({
errorId: LedgerErrorId.GET_ADDRESS_FAILED,
Expand Down
2 changes: 1 addition & 1 deletion src/main/api/ledger/thorchain/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const getAddress = async (
msg: `Getting 'bech32Address' from Ledger's THORChain App failed`
})
}
return E.right({ address: bech32Address, chain: THORChain, type: 'ledger', walletIndex })
return E.right({ address: bech32Address, chain: THORChain, type: 'ledger', walletIndex, hdMode: 'default' })
} catch (error) {
return E.left({
errorId: LedgerErrorId.GET_ADDRESS_FAILED,
Expand Down
24 changes: 19 additions & 5 deletions src/main/api/ledger/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as E from 'fp-ts/Either'

import { IPCLedgerDepositTxParams, IPCLedgerSendTxParams } from '../../../shared/api/io'
import { LedgerError, LedgerErrorId } from '../../../shared/api/types'
import { isError } from '../../../shared/utils/guard'
import { isError, isEthHDMode } from '../../../shared/utils/guard'
import * as BNB from './binance/transaction'
import * as BTC from './bitcoin/transaction'
import * as BCH from './bitcoincash/transaction'
Expand All @@ -37,7 +37,8 @@ export const sendTx = async ({
feeRate,
feeOption,
walletIndex,
nodeUrl
nodeUrl,
hdMode
}: IPCLedgerSendTxParams): Promise<E.Either<LedgerError, TxHash>> => {
try {
const transport = await TransportNodeHidSingleton.open()
Expand Down Expand Up @@ -123,6 +124,11 @@ export const sendTx = async ({
errorId: LedgerErrorId.INVALID_DATA,
msg: `Fee option needs to be set to send Ledger transaction on ${chainToString(chain)}`
})
} else if (!isEthHDMode(hdMode)) {
res = E.left({
errorId: LedgerErrorId.INVALID_DATA,
msg: `Invalid EthHDMode set - needed to send Ledger transaction on ${chainToString(chain)}`
})
} else {
res = await ETH.send({
asset,
Expand All @@ -132,7 +138,8 @@ export const sendTx = async ({
amount,
memo,
walletIndex,
feeOption
feeOption,
ethHDMode: hdMode
})
}
break
Expand Down Expand Up @@ -186,7 +193,8 @@ export const deposit = async ({
memo,
walletIndex,
feeOption,
nodeUrl
nodeUrl,
hdMode
}: IPCLedgerDepositTxParams): Promise<E.Either<LedgerError, TxHash>> => {
try {
const transport = await TransportNodeHidSingleton.open()
Expand Down Expand Up @@ -223,6 +231,11 @@ export const deposit = async ({
errorId: LedgerErrorId.INVALID_DATA,
msg: `Fee option needs to be defined to send Ledger transaction on ${chainToString(chain)}`
})
} else if (!isEthHDMode(hdMode)) {
res = E.left({
errorId: LedgerErrorId.INVALID_DATA,
msg: `Invalid EthHDMode set - needed to send Ledger transaction on ${chainToString(chain)}`
})
} else {
res = await ETH.deposit({
asset,
Expand All @@ -233,7 +246,8 @@ export const deposit = async ({
memo,
walletIndex,
recipient,
feeOption
feeOption,
ethHDMode: hdMode
})
}
break
Expand Down
Loading