Skip to content

Commit 5dbba7c

Browse files
[DDW-1179] Introduce bip32StrToPath function
1 parent 804c02d commit 5dbba7c

File tree

7 files changed

+58
-37
lines changed

7 files changed

+58
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@
203203
"dependencies": {
204204
"@cardano-foundation/ledgerjs-hw-app-cardano": "6.0.0",
205205
"@iohk-jormungandr/wallet-js": "0.5.0-pre7",
206-
"@ledgerhq/hw-transport-node-hid": "6.27.12",
206+
"@ledgerhq/hw-transport-node-hid": "6.27.1",
207207
"@trezor/connect": "9.0.2",
208208
"aes-js": "3.1.2",
209209
"bech32": "2.0.0",

source/common/utils/helper.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
1+
import { HARDENED } from '@cardano-foundation/ledgerjs-hw-app-cardano';
2+
13
export const toJS = (object: any | null | undefined): any =>
24
typeof object === 'object' ? JSON.parse(JSON.stringify(object)) : object;
5+
6+
export const parseBIP32Index = (str: string): number => {
7+
let base = 0;
8+
if (str.endsWith("'")) {
9+
str = str.slice(0, -1);
10+
base = HARDENED;
11+
}
12+
const i = parseInt(str, 10);
13+
if (i < 0 || i > HARDENED) throw new Error('Invalid path');
14+
return base + i;
15+
};
16+
17+
export const bip32StrToPath = (str: string): number[] => {
18+
return str.split('/').map((i: string): number => {
19+
return parseBIP32Index(i);
20+
});
21+
};

source/main/ipc/getHardwareWalletChannel.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { HardwareWalletChannels } from './createHardwareWalletIPCChannels';
3232
import { Device } from './hardwareWallets/ledger/deviceDetection/types';
3333
import { DeviceDetectionPayload } from './hardwareWallets/ledger/deviceDetection/deviceDetection';
3434
import { initTrezorConnect, reinitTrezorConnect } from '../trezor/connection';
35+
import { bip32StrToPath } from '../../common/utils/helper';
3536

3637
type ListenerType = {
3738
unsubscribe: (...args: Array<any>) => any;
@@ -474,10 +475,8 @@ export const handleHardwareWalletRequests = async (
474475
networkId,
475476
protocolMagic,
476477
} = params;
477-
const spendingPath = utils.str_to_path(spendingPathStr);
478-
const stakingPath = stakingPathStr
479-
? utils.str_to_path(stakingPathStr)
480-
: null;
478+
const spendingPath = bip32StrToPath(spendingPathStr);
479+
const stakingPath = stakingPathStr ? bip32StrToPath(stakingPathStr) : null;
481480

482481
deviceConnection = get(devicesMemo, [devicePath, 'AdaConnection']);
483482

@@ -542,10 +541,8 @@ export const handleHardwareWalletRequests = async (
542541
networkId,
543542
protocolMagic,
544543
} = params;
545-
const spendingPath = utils.str_to_path(spendingPathStr);
546-
const stakingPath = stakingPathStr
547-
? utils.str_to_path(stakingPathStr)
548-
: null;
544+
const spendingPath = bip32StrToPath(spendingPathStr);
545+
const stakingPath = stakingPathStr ? bip32StrToPath(stakingPathStr) : null;
549546

550547
try {
551548
deviceConnection = get(devicesMemo, [devicePath, 'AdaConnection']);
@@ -804,9 +801,10 @@ export const handleHardwareWalletRequests = async (
804801
if (!deviceConnection) {
805802
throw new Error('Ledger device not connected');
806803
}
807-
804+
console.debug('>>> EXPORT - start: ', path);
805+
console.debug('>>> DERIVED: ', bip32StrToPath(path));
808806
const extendedPublicKey = await deviceConnection.getExtendedPublicKey({
809-
path: utils.str_to_path(path),
807+
path: bip32StrToPath(path),
810808
});
811809
const deviceSerial = await deviceConnection.getSerial();
812810
return Promise.resolve({
@@ -815,6 +813,7 @@ export const handleHardwareWalletRequests = async (
815813
deviceId: deviceSerial.serialHex,
816814
});
817815
} catch (error) {
816+
console.debug('>>> EXPORT - ERROR: ', error);
818817
logger.info('[HW-DEBUG] EXPORT KEY ERROR', error);
819818
throw error;
820819
}

source/renderer/app/components/wallet/receive/WalletReceiveDialog.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { Component } from 'react';
22
import { observer } from 'mobx-react';
3-
import { utils } from '@cardano-foundation/ledgerjs-hw-app-cardano';
43
import { map, filter } from 'lodash';
54
import { defineMessages, intlShape, FormattedHTMLMessage } from 'react-intl';
65
import classnames from 'classnames';
@@ -25,6 +24,8 @@ import { HW_SHELLEY_CONFIG } from '../../../config/hardwareWalletsConfig';
2524
import { hardenedPathToDerivationPath } from '../../../utils/hardwareWalletUtils';
2625
import { AddressVerificationCheckStatuses } from '../../../stores/HardwareWalletsStore';
2726
import LoadingSpinner from '../../widgets/LoadingSpinner';
27+
import { bip32StrToPath } from '../../../../../common/utils/helper';
28+
2829
import type { AddressVerificationCheckStatus } from '../../../stores/HardwareWalletsStore';
2930
import type { HwDeviceStatus } from '../../../domains/Wallet';
3031

@@ -196,7 +197,7 @@ class WalletReceiveDialog extends Component<Props, State> {
196197
field.value = field.value.replace(/\n/g, '');
197198
};
198199
constructPaths = (address: WalletAddress) => {
199-
const hardenedSpendingPath = utils.str_to_path(address.spendingPath);
200+
const hardenedSpendingPath = bip32StrToPath(address.spendingPath);
200201
const derivationSpendingPath = hardenedPathToDerivationPath(
201202
hardenedSpendingPath
202203
);

source/renderer/app/utils/hardwareWalletUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ from 'lodash';
22
import { bech32 } from 'bech32';
3-
import { utils } from '@cardano-foundation/ledgerjs-hw-app-cardano';
43
import { HARDENED } from '../config/hardwareWalletsConfig';
4+
import { bip32StrToPath } from '../../../common/utils/helper';
55
// Types
66
import type { CoinSelectionAssetsType } from '../api/transactions/types';
77
import type { AddressType } from '../../../common/types/address-introspection.types';
@@ -73,7 +73,7 @@ export const derivationPathToLedgerPath = (derivationPath: Array<string>) => {
7373

7474
const constructedPath = _.join(transformedPath, '/');
7575

76-
return utils.str_to_path(constructedPath);
76+
return bip32StrToPath(constructedPath);
7777
};
7878
export const getParamsFromPath = (derivationPath: Array<string>) => {
7979
const pathParams = _.takeRight(derivationPath, 2);

source/renderer/app/utils/shelleyLedger.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from './hardwareWalletUtils';
1717
import { deriveXpubChannel } from '../ipc/getHardwareWalletChannel';
1818
import { AddressStyles } from '../domains/WalletAddress';
19+
import { bip32StrToPath } from '../../../common/utils/helper';
1920
// Types
2021
import type {
2122
CoinSelectionInput,
@@ -443,7 +444,7 @@ export const prepareLedgerOutput = (
443444
type: AddressType.BASE_PAYMENT_KEY_STAKE_KEY,
444445
params: {
445446
spendingPath: derivationPathToLedgerPath(output.derivationPath),
446-
stakingPath: utils.str_to_path("1852'/1815'/0'/2/0"),
447+
stakingPath: bip32StrToPath("1852'/1815'/0'/2/0"),
447448
},
448449
},
449450
},

yarn.lock

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,15 @@
14341434
"@jridgewell/resolve-uri" "^3.0.3"
14351435
"@jridgewell/sourcemap-codec" "^1.4.10"
14361436

1437+
"@ledgerhq/devices@^6.27.1":
1438+
version "6.27.1"
1439+
resolved "https://registry.yarnpkg.com/@ledgerhq/devices/-/devices-6.27.1.tgz#3b13ab1d1ba8201e9e74a08f390560483978c962"
1440+
dependencies:
1441+
"@ledgerhq/errors" "^6.10.0"
1442+
"@ledgerhq/logs" "^6.10.0"
1443+
rxjs "6"
1444+
semver "^7.3.5"
1445+
14371446
"@ledgerhq/devices@^8.0.0":
14381447
version "8.0.0"
14391448
resolved "https://registry.yarnpkg.com/@ledgerhq/devices/-/devices-8.0.0.tgz#8fe9f9e442e28b7a20bcdf4c2eed06ce7b8f76ae"
@@ -1443,11 +1452,11 @@
14431452
rxjs "6"
14441453
semver "^7.3.5"
14451454

1446-
"@ledgerhq/errors@^6.12.3":
1455+
"@ledgerhq/errors@^6.10.0", "@ledgerhq/errors@^6.12.3":
14471456
version "6.12.3"
14481457
resolved "https://registry.yarnpkg.com/@ledgerhq/errors/-/errors-6.12.3.tgz#a610caae1eeeb7cb038525e5212fe03217dda683"
14491458

1450-
"@ledgerhq/hw-transport-node-hid-noevents@^6.27.12":
1459+
"@ledgerhq/hw-transport-node-hid-noevents@^6.27.1":
14511460
version "6.27.12"
14521461
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-transport-node-hid-noevents/-/hw-transport-node-hid-noevents-6.27.12.tgz#5dacbaf7e146018a73d18d16ce975925b5795732"
14531462
dependencies:
@@ -1457,36 +1466,28 @@
14571466
"@ledgerhq/logs" "^6.10.1"
14581467
node-hid "^2.1.2"
14591468

1460-
"@ledgerhq/[email protected].12":
1461-
version "6.27.12"
1462-
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-transport-node-hid/-/hw-transport-node-hid-6.27.12.tgz#8a628ea574c51bd727aa1d7b7a4ddaf9a5042df1"
1469+
"@ledgerhq/[email protected].1":
1470+
version "6.27.1"
1471+
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-transport-node-hid/-/hw-transport-node-hid-6.27.1.tgz#550b56b741fbdeb07ceb66082cf9500844f3d0a1"
14631472
dependencies:
1464-
"@ledgerhq/devices" "^8.0.0"
1465-
"@ledgerhq/errors" "^6.12.3"
1466-
"@ledgerhq/hw-transport" "^6.28.1"
1467-
"@ledgerhq/hw-transport-node-hid-noevents" "^6.27.12"
1468-
"@ledgerhq/logs" "^6.10.1"
1473+
"@ledgerhq/devices" "^6.27.1"
1474+
"@ledgerhq/errors" "^6.10.0"
1475+
"@ledgerhq/hw-transport" "^6.27.1"
1476+
"@ledgerhq/hw-transport-node-hid-noevents" "^6.27.1"
1477+
"@ledgerhq/logs" "^6.10.0"
14691478
lodash "^4.17.21"
1470-
node-hid "^2.1.2"
1479+
node-hid "2.1.1"
14711480
usb "^1.7.0"
14721481

1473-
"@ledgerhq/hw-transport@^6.27.10":
1474-
version "6.28.1"
1475-
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-transport/-/hw-transport-6.28.1.tgz#cb22fe9bc23af4682c30f2aac7fe6f7ab13ed65a"
1476-
dependencies:
1477-
"@ledgerhq/devices" "^8.0.0"
1478-
"@ledgerhq/errors" "^6.12.3"
1479-
events "^3.3.0"
1480-
1481-
"@ledgerhq/hw-transport@^6.28.1":
1482+
"@ledgerhq/hw-transport@^6.27.1", "@ledgerhq/hw-transport@^6.27.10", "@ledgerhq/hw-transport@^6.28.1":
14821483
version "6.28.1"
14831484
resolved "https://registry.yarnpkg.com/@ledgerhq/hw-transport/-/hw-transport-6.28.1.tgz#cb22fe9bc23af4682c30f2aac7fe6f7ab13ed65a"
14841485
dependencies:
14851486
"@ledgerhq/devices" "^8.0.0"
14861487
"@ledgerhq/errors" "^6.12.3"
14871488
events "^3.3.0"
14881489

1489-
"@ledgerhq/logs@^6.10.1":
1490+
"@ledgerhq/logs@^6.10.0", "@ledgerhq/logs@^6.10.1":
14901491
version "6.10.1"
14911492
resolved "https://registry.yarnpkg.com/@ledgerhq/logs/-/logs-6.10.1.tgz#5bd16082261d7364eabb511c788f00937dac588d"
14921493

0 commit comments

Comments
 (0)