Skip to content

Commit

Permalink
fix Ledger hid
Browse files Browse the repository at this point in the history
- Fix error on hw usb connection
- Add timer to dispatch error by time
LedgerHQ/ledger-live#7786
  • Loading branch information
nicolas-meilan committed Sep 23, 2024
1 parent fb8d1cc commit 891622c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
38 changes: 38 additions & 0 deletions patches/@ledgerhq/react-native-hid+6.32.3.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
diff --git a/node_modules/@ledgerhq/react-native-hid/android/src/main/java/com/ledgerwallet/hid/ReactHIDModule.java b/node_modules/@ledgerhq/react-native-hid/android/src/main/java/com/ledgerwallet/hid/ReactHIDModule.java
index 349bf3982c..7ead607ea7 100755
--- a/node_modules/@ledgerhq/react-native-hid/android/src/main/java/com/ledgerwallet/hid/ReactHIDModule.java
+++ b/node_modules/@ledgerhq/react-native-hid/android/src/main/java/com/ledgerwallet/hid/ReactHIDModule.java
@@ -7,6 +7,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
+import android.os.Build;

import androidx.annotation.NonNull;

@@ -58,7 +59,11 @@ public class ReactHIDModule extends ReactContextBaseJavaModule {
.emit(event, buildMapFromDevice(device));
}
};
- getReactApplicationContext().registerReceiver(receiver, filter);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+ getReactApplicationContext().registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED);
+ } else {
+ getReactApplicationContext().registerReceiver(receiver, filter);
+ }
}

private WritableMap buildMapFromDevice(UsbDevice device) {
@@ -213,7 +218,11 @@ public class ReactHIDModule extends ReactContextBaseJavaModule {
unregisterReceiver(this);
}
};
- getReactApplicationContext().registerReceiver(receiver, intFilter);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+ getReactApplicationContext().registerReceiver(receiver, intFilter, Context.RECEIVER_NOT_EXPORTED);
+ } else {
+ getReactApplicationContext().registerReceiver(receiver, intFilter);
+ }
}

private void unregisterReceiver(BroadcastReceiver receiver) {
25 changes: 16 additions & 9 deletions src/web3/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
isBeEnabled,
BE_DISABLED,
} from '@system/bluetooth';
import { delay } from '@utils/time';
import { erc20BlockchainsConfigurationPropagation } from '@utils/web3';

// export const BTC_DERIVATION_PATH = "m/44'/0'/0'/0"; // m/purpose'/coin_type'/account'/change/index
Expand All @@ -30,7 +31,8 @@ export const BASE_ADDRESS_INDEX = 0;
const SEED_24_WORDS_STRENGTH = 256;
const SEED_12_WORDS_STRENGTH = 128;

const HW_BLUETOOTH_MAX_TIME = 15000;
const HW_BLUETOOTH_MAX_TIME = 15;
const HW_USB_MAX_TIME = 5;

type BlockchainWalletConfig = {
[blockchain in Blockchains]: {
Expand Down Expand Up @@ -176,33 +178,38 @@ const getBluetoothHw = async () => {
}
},
});

const timeRef = setTimeout(() => {
delay(HW_BLUETOOTH_MAX_TIME).then(() => {
if (procesingConnection) {
timeExceeded = true;
clearTimeout(timeRef);
return;
}

try {
suscription.unsubscribe();
} catch (error) { }
clearTimeout(timeRef);
resolve(null);
}, HW_BLUETOOTH_MAX_TIME);
});
});

return [selectedHw];
};

const usbHwTimerErrorHandler = async (): Promise<TransportBLE | TransportHID> => {
await delay(HW_USB_MAX_TIME);

return new Promise((_, reject) => reject(HW_USB_MAX_TIME));
};

export const connectHw = async (bluetoothConnection: boolean = false): Promise<TransportBLE | TransportHID> => {
const transportToUse = bluetoothConnection ? TransportBLE : TransportHID;
const [firstHw] = await (bluetoothConnection ? getBluetoothHw() : transportToUse.list());

if (!firstHw) throw new Error(NO_LEDGER_CONNECTED_ERROR);
let transport = null;
let transport: TransportBLE | TransportHID | null = null;
try {
transport = await transportToUse.open(firstHw);
transport = await (bluetoothConnection
? transportToUse.open(firstHw)
: Promise.race([transportToUse.open(firstHw), usbHwTimerErrorHandler()]));
} catch (_) { // Retry connection one time
try {
transport = await transportToUse.open(firstHw);
Expand Down Expand Up @@ -231,7 +238,7 @@ export const getHwWalletAddress = async (
const walletIndex = (index || BASE_ADDRESS_INDEX) > 0 ? index : 0;
const derivationPath = `${walletConfig.derivationPath}/${walletIndex}`;
const transport = await connectHw(bluetoothConnection);

try {
const ledgerApp = new (walletConfig.ledgerApp)(transport);
const { address } = await ledgerApp.getAddress(derivationPath, true);
Expand Down

0 comments on commit 891622c

Please sign in to comment.