Skip to content

Instantly share code, notes, and snippets.

@cody0704
Forked from coreyphillips/bitcoinjs-lib_Setup.md
Created February 15, 2024 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cody0704/11690fd95503544e6950d1d18353f6dd to your computer and use it in GitHub Desktop.
Save cody0704/11690fd95503544e6950d1d18353f6dd 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 @bitcoin-js/tiny-secp256k1-asmjs 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 * as ecc from '@bitcoin-js/tiny-secp256k1-asmjs';
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:

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