bitcoinjs-lib icon indicating copy to clipboard operation
bitcoinjs-lib copied to clipboard

Help needed in React Native Integration

Open theDevSoham opened this issue 1 year ago • 8 comments

Hi, I'm trying to integrate [email protected] in react-native project. The errors I'm facing are pretty much understandable. For eg:

  1. ReferenceError: Property 'Buffer' doesn't exist, js engine: hermes . OR
  2. fs cannot be found in node modules etc.

It seems some core node modules are not available in react-native. Possible solutions I tried:

  1. Installing rn-nodeify and shimming the libraries not present in react native (Problem: some modules like react-native-udp uses compile method in it's gradle file which is deprecated.)
  2. Using alternative libraries like react-native-bitcoinjs-lib or rn-bitcoinjs-lib (Problem: They require rn-nodeify which recurs the previous problem)
  3. Following this thread: https://gist.github.com/coreyphillips/4d45160fed016417a5f583f179c2cbdb (Problem: It's showing process not found although process is inbuilt in react native or rather it extends something like NodeProcess still it's showing error. Couldn't really understand what's the issue).

Please let me know if it's possible to suggest a workaround because the client needs this library only. Thanks a lot in advance.

theDevSoham avatar May 30 '23 16:05 theDevSoham

  1. install buffer and react-native-get-random-values dependency
  2. make file global.js in root directory(where index.js is in)
import {Buffer} from 'buffer';
import 'react-native-get-random-values';

global.crypto.getRandomValues;
global.Buffer = Buffer;
  1. import global.js in index.js
/**
 * @format
 */
import './global.js';
import { AppRegistry } from 'react-native';
import App from './App';

AppRegistry.registerComponent(appName, () => App);

I don't think fs error coming from this library

ChrisCho-H avatar Jun 20 '23 09:06 ChrisCho-H

And u can refer this if cipher-base error happens if u install stream https://github.com/bitcoinjs/bitcoinjs-lib/issues/1865

ChrisCho-H avatar Jun 20 '23 09:06 ChrisCho-H

if u install tiny-secp256k1 as well, u need to use version with 1.0.0~1.1.6 unless u r gonna integrate WASM in mobile.

ChrisCho-H avatar Jun 20 '23 09:06 ChrisCho-H

And u probably need to install events as well

ChrisCho-H avatar Jun 20 '23 09:06 ChrisCho-H

Thanks @ChrisCho-H . Yes this fixes the issue for me. By the way created a template repo for integrating bitcoinjs-lib and ethers into react native v0.71.x so that others don't have to go through this hassle already 🙂. Feel free to check this out: https://github.com/theDevSoham/react_native_crypto_wallet_template

theDevSoham avatar Jun 20 '23 09:06 theDevSoham

https://www.npmjs.com/package/@bitcoin-js/tiny-secp256k1-asmjs

Released asmjs version of tiny-secp256k1.

Also, @bitcoinerlab/secp256k1 also implements the same interface and is slightly faster (but it uses native JS BigInt, so if your build environment can't handle that, you can't use it)

junderw avatar Jul 22 '23 03:07 junderw

Thanks @ChrisCho-H . Yes this fixes the issue for me. By the way created a template repo for integrating bitcoinjs-lib and ethers into react native v0.71.x so that others don't have to go through this hassle already 🙂. Feel free to check this out: https://github.com/theDevSoham/react_native_crypto_wallet_template

It is working fine.

divyangkhatri avatar Sep 16 '23 07:09 divyangkhatri

@divyangkhatri happy to help 😊

theDevSoham avatar Oct 05 '23 08:10 theDevSoham