Skip to content

Commit

Permalink
Merge pull request #22 from bitcoinerlab/react-native
Browse files Browse the repository at this point in the history
feat: Improve React Native compatibility and bump version to 1.1.1
  • Loading branch information
landabaso authored Sep 12, 2023
2 parents 5945e78 + b62cd7d commit d5dbf16
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.1] - 2023-10-12

### Changed

- **React Native Compatibility**:
- Adjusted the way the library imports `ledger-bitcoin` in `src/ledger.ts` to improve compatibility with React Native projects using the Metro bundler.
- Previously, the library utilized dynamic imports (`await import('ledger-bitcoin')`), but this approach caused issues with Metro bundler. The bundler tried to unconditionally require `ledger-bitcoin` even if it wasn't installed (since it's an optional peerDependency).
- The new approach bypasses this by using a direct `require` statement. For more details on the underlying issue, refer to [this React Native discussion](https://github.com/react-native-community/discussions-and-proposals/issues/120).
- This update ensures smoother integration for developers using this library in React Native projects.

## [1.1.0] - 2023-10-7

### Changed
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@bitcoinerlab/descriptors",
"description": "This library parses and creates Bitcoin Miniscript Descriptors and generates Partially Signed Bitcoin Transactions (PSBTs). It provides PSBT finalizers and signers for single-signature, BIP32 and Hardware Wallets.",
"homepage": "https://github.com/bitcoinerlab/descriptors",
"version": "1.1.0",
"version": "1.1.1",
"author": "Jose-Luis Landabaso",
"license": "MIT",
"repository": {
Expand Down
17 changes: 16 additions & 1 deletion src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,22 @@ export async function importAndValidateLedgerBitcoin(
): Promise<unknown> {
let ledgerBitcoinModule;
try {
ledgerBitcoinModule = await import('ledger-bitcoin');
// Originally, the code used dynamic imports:
// ledgerBitcoinModule = await import('ledger-bitcoin');

// However, in React Native with the Metro bundler, there's an issue with
// recognizing dynamic imports inside try-catch blocks. For details, refer to:
// https://github.com/react-native-community/discussions-and-proposals/issues/120

// The dynamic import gets transpiled to:
// ledgerBitcoinModule = Promise.resolve().then(() => __importStar(require('ledger-bitcoin')));

// Metro bundler fails to recognize the above as conditional. Hence, it tries
// to require 'ledger-bitcoin' unconditionally, leading to potential errors if
// 'ledger-bitcoin' is not installed (given it's an optional peerDependency).

// To bypass this, we directly use require:
ledgerBitcoinModule = require('ledger-bitcoin');
} catch (error) {
throw new Error(
'Could not import "ledger-bitcoin". This is a peer dependency and needs to be installed explicitly. Please run "npm install ledger-bitcoin" to use Ledger Hardware Wallet functionality.'
Expand Down

0 comments on commit d5dbf16

Please sign in to comment.