Browse Source

Merge pull request #412 from gre/differenciate-currency-apps

Differenciate currency apps
master
Gaëtan Renaudeau 7 years ago
committed by GitHub
parent
commit
61325c19d9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      package.json
  2. 2
      src/commands/index.js
  3. 54
      src/commands/isCurrencyAppOpened.js
  4. 52
      src/components/EnsureDeviceApp/index.js
  5. 21
      src/helpers/devices/getBitcoinLikeInfo.js
  6. 6
      yarn.lock

2
package.json

@ -42,7 +42,7 @@
"@ledgerhq/hw-transport": "^4.12.0",
"@ledgerhq/hw-transport-node-hid": "^4.12.0",
"@ledgerhq/ledger-core": "1.4.1",
"@ledgerhq/live-common": "2.15.0",
"@ledgerhq/live-common": "2.16.1",
"axios": "^0.18.0",
"babel-runtime": "^6.26.0",
"bcryptjs": "^2.4.3",

2
src/commands/index.js

@ -12,6 +12,7 @@ import installApp from 'commands/installApp'
import installFinalFirmware from 'commands/installFinalFirmware'
import installMcu from 'commands/installMcu'
import installOsuFirmware from 'commands/installOsuFirmware'
import isCurrencyAppOpened from 'commands/isCurrencyAppOpened'
import libcoreGetVersion from 'commands/libcoreGetVersion'
import libcoreScanAccounts from 'commands/libcoreScanAccounts'
import libcoreSignAndBroadcast from 'commands/libcoreSignAndBroadcast'
@ -34,6 +35,7 @@ const all: Array<Command<any, any>> = [
installFinalFirmware,
installMcu,
installOsuFirmware,
isCurrencyAppOpened,
libcoreGetVersion,
libcoreScanAccounts,
libcoreSignAndBroadcast,

54
src/commands/isCurrencyAppOpened.js

@ -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

52
src/components/EnsureDeviceApp/index.js

@ -1,7 +1,6 @@
// @flow
import { PureComponent } from 'react'
import { connect } from 'react-redux'
import { standardDerivation } from 'helpers/derivations'
import type { Account, CryptoCurrency } from '@ledgerhq/live-common/lib/types'
import type { Device } from 'types/common'
@ -9,6 +8,7 @@ import type { Device } from 'types/common'
import { getDevices } from 'reducers/devices'
import type { State as StoreState } from 'reducers/index'
import getAddress from 'commands/getAddress'
import isCurrencyAppOpened from 'commands/isCurrencyAppOpened'
type OwnProps = {
currency?: ?CryptoCurrency,
@ -50,6 +50,8 @@ const mapStateToProps = (state: StoreState) => ({
devices: getDevices(state),
})
// TODO we want to split into <EnsureDeviceCurrency/> and <EnsureDeviceAccount/>
// and minimize the current codebase AF
class EnsureDeviceApp extends PureComponent<Props, State> {
state = {
appStatus: 'progress',
@ -104,39 +106,39 @@ class EnsureDeviceApp extends PureComponent<Props, State> {
return
}
let appOptions
if (account) {
appOptions = {
devicePath: deviceSelected.path,
currencyId: account.currency.id,
path: account.freshAddressPath,
accountAddress: account.freshAddress,
segwit: !!account.isSegwit,
}
} else if (currency) {
appOptions = {
devicePath: deviceSelected.path,
currencyId: currency.id,
path: standardDerivation({ currency, x: 0, segwit: false }),
}
}
try {
if (appOptions) {
const { address } = await getAddress.send(appOptions).toPromise()
if (account && account.freshAddress !== address) {
console.log(account)
console.warn(account.freshAddress, address)
if (account) {
const { address } = await getAddress
.send({
devicePath: deviceSelected.path,
currencyId: account.currency.id,
path: account.freshAddressPath,
segwit: !!account.isSegwit,
})
.toPromise()
const { freshAddress } = account
if (account && freshAddress !== address) {
console.warn({ freshAddress, address })
throw new Error('Account address is different than device address')
}
} else if (currency) {
const pass = await isCurrencyAppOpened
.send({
devicePath: deviceSelected.path,
currencyId: currency.id,
})
.toPromise()
if (!pass) {
throw new Error(`${currency.name} app is not opened on the device`)
}
} else {
// TODO: real check if user is on the device dashboard
if (!deviceSelected) {
throw new Error('No device')
}
await sleep(1)
await sleep(1) // WTF
}
this.handleStatusChange(this.state.deviceStatus, 'success')
if (withGenuineCheck && appStatus !== 'success') {

21
src/helpers/devices/getBitcoinLikeInfo.js

@ -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

6
yarn.lock

@ -1464,9 +1464,9 @@
npm "^5.7.1"
prebuild-install "^2.2.2"
"@ledgerhq/live-common@2.15.0":
version "2.15.0"
resolved "https://registry.yarnpkg.com/@ledgerhq/live-common/-/live-common-2.15.0.tgz#9677c223446c996e5d5f13658fc6af09e966c701"
"@ledgerhq/live-common@2.16.1":
version "2.16.1"
resolved "https://registry.yarnpkg.com/@ledgerhq/live-common/-/live-common-2.16.1.tgz#f3e1d2a616d5181a71c3982158a5d17d3c562ca1"
dependencies:
axios "^0.18.0"
invariant "^2.2.2"

Loading…
Cancel
Save