Skip to content

Instantly share code, notes, and snippets.

@coreyphillips
Last active October 24, 2024 02:55
Show Gist options
  • Save coreyphillips/4d45160fed016417a5f583f179c2cbdb to your computer and use it in GitHub Desktop.
Save coreyphillips/4d45160fed016417a5f583f179c2cbdb to your computer and use it in GitHub Desktop.
How To Add bitcoinjs-lib To A React Native v0.60+ Project

How To Add bitcoinjs-lib To A React Native v0.60+ Project

Add/Link the following dependencies:

  • yarn add bitcoinjs-lib ecpair @bitcoinerlab/secp256k1 react-native-randombytes buffer readable-stream
  • yarn add --dev rn-nodeify
  • cd ios && pod install && cd ..
  • Add the following postinstall to your script in package.json: "postinstall": "rn-nodeify --install buffer,stream,assert,events,crypto,vm,process --hack"

Install any remaining dependencies and run postinstall.

NOTE: (If you receive an error about "shim.js" not existing just run yarn install again):

  • yarn install

  • Uncomment require('crypto') at the bottom of "shim.js". Or add require('crypto') to the bottom of "shim.js" if it doesn't exist.

  • And finally, yarn install && cd ios && pod install && cd .. one more time.

To test if everything is working as expected place the following in render:

import './shim';
import ECPairFactory from 'ecpair';
import ecc from '@bitcoinerlab/secp256k1';
const bitcoin = require('bitcoinjs-lib');
const ECPair = ECPairFactory(ecc);
const keyPair = ECPair.makeRandom();
const {address} = bitcoin.payments.p2pkh({pubkey: keyPair.publicKey});
console.log(address);

An unedited video walkthrough of this guide can be found here:

@coreyphillips
Copy link
Author

Thanks again @junderw!

I've updated the guide to use @bitcoin-js/tiny-secp256k1-asmjs.

Regarding RN/bitcoinjs-lib trouble, the first-time setup for a project can be tricky and some devs tend to default to Expo which can bring some additional complications. Once a project is setup successfully on a particular version it's not an issue to maintain. However, as seen with tiny-secp256k1, any change/upgrade to a crypto library can be difficult in RN.

@parthindia47
Copy link

parthindia47 commented Mar 5, 2024

Hi,
I followed all this steps but I am getting following error, any idea ?

``Error: While trying to resolve module @bitcoin-js/tiny-secp256k1-asmjs from file `C:\Users\Parth\Documents\Development\crypto-wallet-app\LCRN14-crypto-wallet-app-master\utils\SimBSC.js`, the package `C:\Users\Parth\Documents\Development\crypto-wallet-app\LCRN14-crypto-wallet-app-master\node_modules@bitcoin-js\tiny-secp256k1-asmjs\package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`C:\Users\Parth\Documents\Development\crypto-wallet-app\LCRN14-crypto-wallet-app-master\node_modules@bitcoin-js\tiny-secp256k1-asmjs\lib\cjs\index.cjs`. Indeed, none of these files exist:`
`

@coreyphillips
Copy link
Author

@parthindia47, do you have any luck swapping out @bitcoin-js/tiny-secp256k1-asmjs for @bitcoinerlab/secp256k1?

@techiejd
Copy link

Hi everyone! I'd like to let you know that there is a process for including these shims through using metro.config.js instead of rn-nodeify. I find it easier, plus you don't need to eject from expo.

PTAL if you wish - https://gist.github.com/techiejd/a14442315b5bfe0cba4ede7d2971a52f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment