Skip to content

Commit 7fb4903

Browse files
[DDW-1179] Revert str_to_path changes and add proper hex_to_buf type
1 parent b6f160c commit 7fb4903

File tree

5 files changed

+21
-43
lines changed

5 files changed

+21
-43
lines changed

source/common/utils/helper.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,2 @@
1-
import { HARDENED } from '@cardano-foundation/ledgerjs-hw-app-cardano';
2-
31
export const toJS = (object: any | null | undefined): any =>
42
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: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import TransportNodeHid, {
33
getDevices,
44
} from '@ledgerhq/hw-transport-node-hid-noevents';
55
import AppAda, { utils } from '@cardano-foundation/ledgerjs-hw-app-cardano';
6+
import { str_to_path } from '@cardano-foundation/ledgerjs-hw-app-cardano/dist/utils/address';
7+
import { HexString } from '@cardano-foundation/ledgerjs-hw-app-cardano/dist/types/internal';
68
import TrezorConnect, {
79
DEVICE,
810
DEVICE_EVENT,
@@ -32,7 +34,6 @@ import { HardwareWalletChannels } from './createHardwareWalletIPCChannels';
3234
import { Device } from './hardwareWallets/ledger/deviceDetection/types';
3335
import { DeviceDetectionPayload } from './hardwareWallets/ledger/deviceDetection/deviceDetection';
3436
import { initTrezorConnect, reinitTrezorConnect } from '../trezor/connection';
35-
import { bip32StrToPath } from '../../common/utils/helper';
3637

3738
type ListenerType = {
3839
unsubscribe: (...args: Array<any>) => any;
@@ -456,8 +457,7 @@ export const handleHardwareWalletRequests = async (
456457
});
457458
deriveXpubChannel.onRequest(async (params) => {
458459
const { parentXpubHex, lastIndex, derivationScheme } = params;
459-
// @ts-ignore
460-
const parentXpub = utils.hex_to_buf(parentXpubHex);
460+
const parentXpub = utils.hex_to_buf(parentXpubHex as HexString);
461461

462462
try {
463463
const xpub = deriveChildXpub(parentXpub, lastIndex, derivationScheme);
@@ -476,8 +476,8 @@ export const handleHardwareWalletRequests = async (
476476
networkId,
477477
protocolMagic,
478478
} = params;
479-
const spendingPath = bip32StrToPath(spendingPathStr);
480-
const stakingPath = stakingPathStr ? bip32StrToPath(stakingPathStr) : null;
479+
const spendingPath = str_to_path(spendingPathStr);
480+
const stakingPath = stakingPathStr ? str_to_path(stakingPathStr) : null;
481481

482482
deviceConnection = get(devicesMemo, [devicePath, 'AdaConnection']);
483483

@@ -542,8 +542,8 @@ export const handleHardwareWalletRequests = async (
542542
networkId,
543543
protocolMagic,
544544
} = params;
545-
const spendingPath = bip32StrToPath(spendingPathStr);
546-
const stakingPath = stakingPathStr ? bip32StrToPath(stakingPathStr) : null;
545+
const spendingPath = str_to_path(spendingPathStr);
546+
const stakingPath = stakingPathStr ? str_to_path(stakingPathStr) : null;
547547

548548
try {
549549
deviceConnection = get(devicesMemo, [devicePath, 'AdaConnection']);
@@ -802,10 +802,8 @@ export const handleHardwareWalletRequests = async (
802802
if (!deviceConnection) {
803803
throw new Error('Ledger device not connected');
804804
}
805-
console.debug('>>> EXPORT - start: ', path);
806-
console.debug('>>> DERIVED: ', bip32StrToPath(path));
807805
const extendedPublicKey = await deviceConnection.getExtendedPublicKey({
808-
path: bip32StrToPath(path),
806+
path: str_to_path(path),
809807
});
810808
const deviceSerial = await deviceConnection.getSerial();
811809
return Promise.resolve({
@@ -814,7 +812,6 @@ export const handleHardwareWalletRequests = async (
814812
deviceId: deviceSerial.serialHex,
815813
});
816814
} catch (error) {
817-
console.debug('>>> EXPORT - ERROR: ', error);
818815
logger.info('[HW-DEBUG] EXPORT KEY ERROR', error);
819816
throw error;
820817
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { PopOver } from 'react-polymorph/lib/components/PopOver';
1111
import QRCode from 'qrcode.react';
1212
import { Checkbox } from 'react-polymorph/lib/components/Checkbox';
1313
import { CheckboxSkin } from 'react-polymorph/lib/skins/simple/CheckboxSkin';
14+
import { str_to_path } from '@cardano-foundation/ledgerjs-hw-app-cardano/dist/utils/address';
1415
import RadioSet from '../../widgets/RadioSet';
1516
import Dialog from '../../widgets/Dialog';
1617
import DialogCloseButton from '../../widgets/DialogCloseButton';
@@ -24,7 +25,6 @@ import { HW_SHELLEY_CONFIG } from '../../../config/hardwareWalletsConfig';
2425
import { hardenedPathToDerivationPath } from '../../../utils/hardwareWalletUtils';
2526
import { AddressVerificationCheckStatuses } from '../../../stores/HardwareWalletsStore';
2627
import LoadingSpinner from '../../widgets/LoadingSpinner';
27-
import { bip32StrToPath } from '../../../../../common/utils/helper';
2828

2929
import type { AddressVerificationCheckStatus } from '../../../stores/HardwareWalletsStore';
3030
import type { HwDeviceStatus } from '../../../domains/Wallet';
@@ -197,7 +197,7 @@ class WalletReceiveDialog extends Component<Props, State> {
197197
field.value = field.value.replace(/\n/g, '');
198198
};
199199
constructPaths = (address: WalletAddress) => {
200-
const hardenedSpendingPath = bip32StrToPath(address.spendingPath);
200+
const hardenedSpendingPath = str_to_path(address.spendingPath);
201201
const derivationSpendingPath = hardenedPathToDerivationPath(
202202
hardenedSpendingPath
203203
);

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 { str_to_path } from '@cardano-foundation/ledgerjs-hw-app-cardano/dist/utils/address';
34
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 bip32StrToPath(constructedPath);
76+
return str_to_path(constructedPath);
7777
};
7878
export const getParamsFromPath = (derivationPath: Array<string>) => {
7979
const pathParams = _.takeRight(derivationPath, 2);

source/renderer/app/utils/shelleyLedger.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import {
66
StakeCredentialParamsType,
77
CIP36VoteRegistrationFormat,
88
} from '@cardano-foundation/ledgerjs-hw-app-cardano';
9+
import {
10+
str_to_path,
11+
base58_decode,
12+
} from '@cardano-foundation/ledgerjs-hw-app-cardano/dist/utils/address';
13+
import { HexString } from '@cardano-foundation/ledgerjs-hw-app-cardano/dist/types/internal';
914
import { encode } from 'borc';
1015
import blakejs from 'blakejs';
1116
import _ from 'lodash';
@@ -16,7 +21,6 @@ import {
1621
} from './hardwareWalletUtils';
1722
import { deriveXpubChannel } from '../ipc/getHardwareWalletChannel';
1823
import { AddressStyles } from '../domains/WalletAddress';
19-
import { bip32StrToPath } from '../../../common/utils/helper';
2024
// Types
2125
import type {
2226
CoinSelectionInput,
@@ -31,8 +35,6 @@ import type {
3135
} from '../../../common/types/hardware-wallets.types';
3236
import type { AddressStyle } from '../api/addresses/types';
3337

34-
const bs58 = require('bs58');
35-
3638
export const CATALYST_VOTING_REGISTRATION_TYPE = 'CATALYST_VOTING';
3739
export type ShelleyTxInputType = {
3840
coins: number;
@@ -177,7 +179,7 @@ export function ShelleyTxOutput(
177179
const addressBuff =
178180
addressStyle === AddressStyles.ADDRESS_SHELLEY
179181
? utils.bech32_decodeAddress(address)
180-
: bs58.decode(address);
182+
: base58_decode(address);
181183
return encoder.pushAny([addressBuff, coins]);
182184
}
183185

@@ -401,15 +403,13 @@ export const CachedDeriveXpubFactory = (
401403
const parentXpub = await deriveXpub(derivationPath.slice(0, -1), null);
402404

403405
try {
404-
// @ts-ignore
405406
const parentXpubHex = utils.buf_to_hex(parentXpub);
406407
const derivedXpub = await deriveXpubChannel.request({
407408
parentXpubHex,
408409
lastIndex,
409410
derivationScheme: derivationScheme.ed25519Mode,
410411
});
411-
// @ts-ignore
412-
return utils.hex_to_buf(derivedXpub);
412+
return utils.hex_to_buf(derivedXpub as HexString);
413413
} catch (e) {
414414
throw e;
415415
}
@@ -447,7 +447,7 @@ export const prepareLedgerOutput = (
447447
type: AddressType.BASE_PAYMENT_KEY_STAKE_KEY,
448448
params: {
449449
spendingPath: derivationPathToLedgerPath(output.derivationPath),
450-
stakingPath: bip32StrToPath("1852'/1815'/0'/2/0"),
450+
stakingPath: str_to_path("1852'/1815'/0'/2/0"),
451451
},
452452
},
453453
},
@@ -463,7 +463,7 @@ export const prepareLedgerOutput = (
463463
addressHex:
464464
addressStyle === AddressStyles.ADDRESS_SHELLEY
465465
? utils.buf_to_hex(utils.bech32_decodeAddress(output.address))
466-
: utils.buf_to_hex(bs58.decode(output.address)),
466+
: utils.buf_to_hex(base58_decode(output.address)),
467467
},
468468
},
469469
amount: output.amount.quantity.toString(),

0 commit comments

Comments
 (0)