bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.72k stars 2.11k forks source link

Help needed in React Native Integration #1927

Open theDevSoham opened 1 year ago

theDevSoham commented 1 year ago

Hi, I'm trying to integrate bitcoinjs-lib@6.0.2 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.

ChrisCho-H commented 1 year ago
  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;


3. import global.js in index.js
```javascript
/**
 * @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 commented 1 year ago

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

ChrisCho-H commented 1 year ago

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 commented 1 year ago

And u probably need to install events as well

theDevSoham commented 1 year ago

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

junderw commented 1 year ago

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)

divyangkhatri commented 1 year ago

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.

theDevSoham commented 1 year ago

@divyangkhatri happy to help 😊