Skip to content

Commit

Permalink
Refactor version checking
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBenc committed Jan 15, 2021
1 parent 99455ac commit ab5f589
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 54 deletions.
3 changes: 2 additions & 1 deletion app/frontend/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
NETWORKS,
PREMIUM_MEMBER_BALANCE_TRESHOLD,
BIG_DELEGATOR_THRESHOLD,
CryptoProviderFeatures,
} from './wallet/constants'
import NamedError from './helpers/NamedError'
import {exportWalletSecretDef} from './wallet/keypass-json'
Expand Down Expand Up @@ -864,7 +865,7 @@ export default ({setState, getState}: {setState: SetStateFn; getState: GetStateF
const plan = await prepareTxPlan({rewards, txType: 'withdraw'})
const withdrawalValidationError =
withdrawalPlanValidator(rewards, getSourceAccountInfo(state).balance, plan) ||
wallet.checkCryptoProviderVersion('WITHDRAWAL')
wallet.ensureFeatureIsSupported(CryptoProviderFeatures.WITHDRAWAL)
if (withdrawalValidationError) {
setErrorState('transactionSubmissionError', withdrawalValidationError, {
shouldShowTransactionErrorModal: true,
Expand Down
26 changes: 16 additions & 10 deletions app/frontend/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import printAda from './helpers/printAda'
import debugLog from './helpers/debugLog'
import {ADALITE_CONFIG} from './config'
import {Lovelace} from './state'
import {LEDGER_VERSIONS} from './wallet/constants'
import {LEDGER_VERSIONS, CryptoProviderFeatures} from './wallet/constants'

const {ADALITE_MIN_DONATION_VALUE} = ADALITE_CONFIG

Expand Down Expand Up @@ -86,18 +86,24 @@ const translations = {
'NetworkError: Request to our servers has failed. Please check your network connection and if the problem persists, contact us.',
ServerError: () =>
'ServerError: Our servers are probably down. Please try again later and if the problem persists, contact us.',
OutdatedCardanoAppError: ({message}) =>
`OutdatedCardanoAppError: Your cardano application is running on an outdated version ${message}. Please update your cardano application to the version ${
LEDGER_VERSIONS.MINIMAL.major
}.${LEDGER_VERSIONS.MINIMAL.minor}.${
LEDGER_VERSIONS.MINIMAL.patch
LedgerOutdatedCardanoAppError: ({message}) =>
`LedgerOutdatedCardanoAppError: Your cardano application is running on an outdated version ${message}. Please update your cardano application to the version ${
LEDGER_VERSIONS[CryptoProviderFeatures.MINIMAL].major
}.${LEDGER_VERSIONS[CryptoProviderFeatures.MINIMAL].minor}.${
LEDGER_VERSIONS[CryptoProviderFeatures.MINIMAL].patch
} or later. See https://support.ledger.com/hc/en-us/articles/360006523674-Install-uninstall-and-update-apps for more information.`,
NotRecommendedCardanoAppVerion: ({message}) =>
LedgerWithdrawalNotSupported: ({message}) =>
`RewardsWithdrawalNotSupported: There was a bug in Ledger Cardano app 2.0.3 that didn't allow rewards withdrawals. To withdraw rewards, you need to update your Ledger firmware and your Ledger Cardano app. You need to update to firmware version 1.6.1 for Ledger Nano S and to firmware version 1.2.4-4 for Nano X. For more information how to do this please refer to https://support.ledger.com/hc/en-us/articles/360005885733-Update-device-firmware. After your ledger firmware is updated please install the latest version of the the Ledger Cardano app. Your current version is ${message} and the required version is ${
LEDGER_VERSIONS.WITHDRAWAL.major
}.${LEDGER_VERSIONS.WITHDRAWAL.minor}.${
LEDGER_VERSIONS.WITHDRAWAL.patch
LEDGER_VERSIONS[CryptoProviderFeatures.WITHDRAWAL].major
}.${LEDGER_VERSIONS[CryptoProviderFeatures.WITHDRAWAL].minor}.${
LEDGER_VERSIONS[CryptoProviderFeatures.WITHDRAWAL].patch
}. For more information how to do this, please refer to https://support.ledger.com/hc/en-us/articles/360006523674-Install-uninstall-and-update-apps`,
LedgerPoolRegNotSupported: ({message}) =>
`Pool registration is not supported on this device. Your current version is ${message} and the required version is ${
LEDGER_VERSIONS[CryptoProviderFeatures.WITHDRAWAL].major
}.${LEDGER_VERSIONS[CryptoProviderFeatures.WITHDRAWAL].minor}.${
LEDGER_VERSIONS[CryptoProviderFeatures.WITHDRAWAL].patch
}`,
PoolRegInvalidNumCerts: () =>
'The transaction must include exactly one certificate, being the pool registration.',
PoolRegInvalidType: () => 'The certificate in transaction is not a pool registration.',
Expand Down
2 changes: 1 addition & 1 deletion app/frontend/wallet/cardano-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ const CardanoWallet = (options) => {
return {}
}

function checkCryptoProviderVersion() {
function checkCryptoProviderVersion(feature) {
return {code: 'UnsupportedOperation', message: ''}
}

Expand Down
53 changes: 36 additions & 17 deletions app/frontend/wallet/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,38 +104,57 @@ export const CRYPTO_PROVIDER_TYPES = {
export const SENTRY_USER_FEEDBACK_API =
'https://sentry.io/api/0/projects/vacuumlabs-sro/adalite-frontend/user-feedback/'

export const UNKNOWN_POOL_NAME = '<Unknown pool>'

export const PREMIUM_MEMBER_BALANCE_TRESHOLD = 2500000000000
export const BIG_DELEGATOR_THRESHOLD = 10000000000000

export const SATURATION_POINT = 62224967000000

export const MAX_BULK_EXPORT_AMOUNT = 5

export const enum CERTIFICATES_ENUM {
STAKING_KEY_REGISTRATION = 0,
STAKING_KEY_DEREGISTRATION = 1,
DELEGATION = 2,
STAKEPOOL_REGISTRATION = 3,
}

export const enum CryptoProviderFeatures {
MINIMAL,
WITHDRAWAL,
BULK_EXPORT,
POOL_OWNER,
}

export const LEDGER_VERSIONS = {
MINIMAL: {
[CryptoProviderFeatures.MINIMAL]: {
major: 2,
minor: 0,
patch: 2,
},
WITHDRAWAL: {
[CryptoProviderFeatures.WITHDRAWAL]: {
major: 2,
minor: 0,
patch: 4,
},
BULK_EXPORT: {
[CryptoProviderFeatures.BULK_EXPORT]: {
major: 2,
minor: 1,
patch: 0,
},
[CryptoProviderFeatures.POOL_OWNER]: {
major: 2,
minor: 1,
patch: 0,
},
}

export const UNKNOWN_POOL_NAME = '<Unknown pool>'

export const PREMIUM_MEMBER_BALANCE_TRESHOLD = 2500000000000
export const BIG_DELEGATOR_THRESHOLD = 10000000000000

export const SATURATION_POINT = 62224967000000

export const MAX_BULK_EXPORT_AMOUNT = 5

export const enum CERTIFICATES_ENUM {
STAKING_KEY_REGISTRATION = 0,
STAKING_KEY_DEREGISTRATION = 1,
DELEGATION = 2,
STAKEPOOL_REGISTRATION = 3,
export const LEDGER_ERRORS = {
[CryptoProviderFeatures.MINIMAL]: 'LedgerOutdatedCardanoAppError',
[CryptoProviderFeatures.WITHDRAWAL]: 'LedgerWithdrawalNotSupported',
[CryptoProviderFeatures.BULK_EXPORT]: 'LedgerBulkExportNotSupported',
[CryptoProviderFeatures.POOL_OWNER]: 'LedgerPoolRegNotSupported',
}

export const MAX_ACCOUNT_COUNT = 100
9 changes: 5 additions & 4 deletions app/frontend/wallet/shelley-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BlockchainExplorer from './blockchain-explorer'
import {AccountManager} from './account-manager'
import {AccountInfo} from '../types'
import {CryptoProviderFeatures} from './constants'

const ShelleyWallet = ({config, cryptoProvider}) => {
const blockchainExplorer = BlockchainExplorer(config)
Expand Down Expand Up @@ -35,11 +36,11 @@ const ShelleyWallet = ({config, cryptoProvider}) => {
return await blockchainExplorer.fetchTxInfo(txHash)
}

function checkCryptoProviderVersion(featureName: string) {
function ensureFeatureIsSupported(feature: CryptoProviderFeatures) {
try {
cryptoProvider.ensureFeatureIsSupported(featureName)
cryptoProvider.ensureFeatureIsSupported(feature)
} catch (e) {
return {code: e.name, message: e.message}
return {code: e.name, params: {message: e.message}}
}
return null
}
Expand All @@ -59,7 +60,7 @@ const ShelleyWallet = ({config, cryptoProvider}) => {
submitTx,
getWalletSecretDef,
fetchTxInfo,
checkCryptoProviderVersion,
ensureFeatureIsSupported,
getAccountsInfo,
getValidStakepools,
getAccount: accountManager.getAccount,
Expand Down
10 changes: 6 additions & 4 deletions app/frontend/wallet/shelley/helpers/version-check.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import {LEDGER_VERSIONS} from '../../constants'

interface Version {
major: string
minor: string
patch: string
}

export const hasRequiredVersion = (currentVersionStr: Version, requiredVersionType: string) => {
export const hasRequiredVersion = (currentVersionStr: Version, requiredVersionStr) => {
const current = {
major: parseInt(currentVersionStr.major, 10),
minor: parseInt(currentVersionStr.minor, 10),
patch: parseInt(currentVersionStr.patch, 10),
}

const required = LEDGER_VERSIONS[requiredVersionType]
const required = {
major: parseInt(requiredVersionStr.major, 10),
minor: parseInt(requiredVersionStr.minor, 10),
patch: parseInt(requiredVersionStr.patch, 10),
}

return (
current.major > required.major ||
Expand Down
9 changes: 7 additions & 2 deletions app/frontend/wallet/shelley/shelley-js-crypto-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ const ShelleyJsCryptoProvider = ({
return ShelleySignedTransactionStructured(txAux, witnesses, meta)
}

function ensureFeatureIsSupported(featureName: string) {
return
function isFeatureSupported(feature) {
return true
}

function ensureFeatureIsSupported(feature) {
return true
}

return {
Expand All @@ -146,6 +150,7 @@ const ShelleyJsCryptoProvider = ({
_deriveHdNodeFromRoot: deriveHdNode,
_deriveChildHdNode: deriveChildHdNode,
ensureFeatureIsSupported,
isFeatureSupported,
cleanXpubCache,
}
}
Expand Down
29 changes: 17 additions & 12 deletions app/frontend/wallet/shelley/shelley-ledger-crypto-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
import * as platform from 'platform'
import {hasRequiredVersion} from './helpers/version-check'
import {PoolParams} from './helpers/poolCertificateUtils'
import {CERTIFICATES_ENUM} from '../constants'
import {
CERTIFICATES_ENUM,
LEDGER_VERSIONS,
CryptoProviderFeatures,
LEDGER_ERRORS,
} from '../constants'

import {
bechAddressToHex,
Expand Down Expand Up @@ -63,13 +68,13 @@ const ShelleyLedgerCryptoProvider = async ({network, config, forceWebUsb}) => {

const version = await ledger.getVersion()

ensureFeatureIsSupported('MINIMAL')
ensureFeatureIsSupported(CryptoProviderFeatures.MINIMAL)

const isHwWallet = () => true
const getWalletName = () => 'Ledger'

const exportPublicKeys = async (derivationPaths) => {
if (hasRequiredVersion(version, 'BULK_EXPORT')) {
if (isFeatureSupported(CryptoProviderFeatures.BULK_EXPORT)) {
return await ledger.getExtendedPublicKeys(derivationPaths)
}
const response = []
Expand All @@ -88,14 +93,13 @@ const ShelleyLedgerCryptoProvider = async ({network, config, forceWebUsb}) => {
}
)

function ensureFeatureIsSupported(featureName: string) {
if (!hasRequiredVersion(version, featureName)) {
const versionErrors = {
MINIMAL: 'OutdatedCardanoAppError',
WITHDRAWAL: 'NotRecommendedCardanoAppVerion',
BULK_EXPORT: 'BulkExportNotSupported',
}
throw NamedError(versionErrors[featureName], {
function isFeatureSupported(feature: CryptoProviderFeatures) {
return hasRequiredVersion(version, LEDGER_VERSIONS[feature])
}

function ensureFeatureIsSupported(feature: CryptoProviderFeatures) {
if (!isFeatureSupported(feature)) {
throw NamedError(LEDGER_ERRORS[feature], {
message: `${version.major}.${version.minor}.${version.patch}`,
})
}
Expand Down Expand Up @@ -294,8 +298,9 @@ const ShelleyLedgerCryptoProvider = async ({network, config, forceWebUsb}) => {
getWalletName,
_sign: sign,
_deriveHdNode: deriveHdNode,
ensureFeatureIsSupported,
cleanXpubCache,
isFeatureSupported,
ensureFeatureIsSupported,
}
}

Expand Down
11 changes: 8 additions & 3 deletions app/frontend/wallet/shelley/shelley-trezor-crypto-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line import/no-unresolved
import CachedDeriveXpubFactory from '../helpers/CachedDeriveXpubFactory'
import {ADALITE_SUPPORT_EMAIL} from '../constants'
import {ADALITE_SUPPORT_EMAIL, CryptoProviderFeatures} from '../constants'
import derivationSchemes from '../helpers/derivation-schemes'
import NamedError from '../../helpers/NamedError'
import debugLog from '../../helpers/debugLog'
Expand Down Expand Up @@ -252,8 +252,12 @@ const CardanoTrezorCryptoProvider = ({network, config}) => {
}
}

function ensureFeatureIsSupported(featureName: string) {
return
function isFeatureSupported(feature: CryptoProviderFeatures) {
return true
}

function ensureFeatureIsSupported(features: CryptoProviderFeatures) {
return true
}

return {
Expand All @@ -269,6 +273,7 @@ const CardanoTrezorCryptoProvider = ({network, config}) => {
_deriveHdNode: deriveHdNode,
network,
ensureFeatureIsSupported,
isFeatureSupported,
}
}

Expand Down

0 comments on commit ab5f589

Please sign in to comment.