diff --git a/CHANGELOG.md b/CHANGELOG.md index 42b1a0e..bbbd4ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package-lock.json b/package-lock.json index dfa428e..00f638e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@bitcoinerlab/descriptors", - "version": "1.0.3", + "version": "1.1.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@bitcoinerlab/descriptors", - "version": "1.0.3", + "version": "1.1.1", "license": "MIT", "dependencies": { "@bitcoinerlab/miniscript": "^1.2.1", diff --git a/package.json b/package.json index ed5e262..fe58da0 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/ledger.ts b/src/ledger.ts index b14ed6f..1031523 100644 --- a/src/ledger.ts +++ b/src/ledger.ts @@ -51,7 +51,22 @@ export async function importAndValidateLedgerBitcoin( ): Promise { 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.'