Gaëtan Renaudeau
7 years ago
committed by
GitHub
6 changed files with 108 additions and 29 deletions
@ -0,0 +1,54 @@ |
|||
// @flow
|
|||
|
|||
import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies' |
|||
|
|||
import { createCommand, Command } from 'helpers/ipc' |
|||
import { fromPromise } from 'rxjs/observable/fromPromise' |
|||
|
|||
import { withDevice } from 'helpers/deviceAccess' |
|||
import getBitcoinLikeInfo from 'helpers/devices/getBitcoinLikeInfo' |
|||
import getAddress from 'helpers/getAddressForCurrency' |
|||
import { standardDerivation } from 'helpers/derivations' |
|||
|
|||
type Input = { |
|||
devicePath: string, |
|||
currencyId: string, |
|||
} |
|||
|
|||
type Result = boolean |
|||
|
|||
const cmd: Command<Input, Result> = createCommand( |
|||
'isCurrencyAppOpened', |
|||
({ devicePath, currencyId }) => |
|||
fromPromise( |
|||
withDevice(devicePath)(async transport => { |
|||
const currency = getCryptoCurrencyById(currencyId) |
|||
|
|||
// First, we check if the app can derivates on the currency
|
|||
try { |
|||
await getAddress(currencyId)( |
|||
transport, |
|||
currencyId, |
|||
standardDerivation({ currency, segwit: false, x: 0 }), |
|||
{ segwit: false }, |
|||
) |
|||
|
|||
// then, just in case of BTC, we need to make sure we are on the correct BTC fork
|
|||
const { bitcoinLikeInfo } = currency |
|||
if (bitcoinLikeInfo) { |
|||
const { P2SH, P2PKH } = await getBitcoinLikeInfo(transport) |
|||
return P2SH === bitcoinLikeInfo.P2SH && P2PKH === bitcoinLikeInfo.P2PKH |
|||
} |
|||
|
|||
// in case of ETH / XRP, the address derivation is enough
|
|||
return true |
|||
} catch (e) { |
|||
console.log(e) |
|||
// if anything failed, it does not pass
|
|||
return false |
|||
} |
|||
}), |
|||
), |
|||
) |
|||
|
|||
export default cmd |
@ -0,0 +1,21 @@ |
|||
// @flow
|
|||
|
|||
import type Transport from '@ledgerhq/hw-transport' |
|||
|
|||
const getBitcoinLikeInfo = ( |
|||
transport: Transport<any>, |
|||
): Promise<{ |
|||
P2PKH: number, |
|||
P2SH: number, |
|||
message: Buffer, |
|||
short: Buffer, |
|||
}> => |
|||
transport.send(0xe0, 0x16, 0x00, 0x00).then(res => { |
|||
const P2PKH = res.readUInt16BE(0) |
|||
const P2SH = res.readUInt16BE(2) |
|||
const message = res.slice(5, res.readUInt8(4)) |
|||
const short = res.slice(5 + message.length + 1, res.readUInt8(5 + message.length)) |
|||
return { P2PKH, P2SH, message, short } |
|||
}) |
|||
|
|||
export default getBitcoinLikeInfo |
Loading…
Reference in new issue