Skip to content

Instantly share code, notes, and snippets.

@coreyphillips
Last active March 12, 2019 16:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save coreyphillips/f33d86f3d2307e58f0a5686e6cfc8a7e to your computer and use it in GitHub Desktop.
Save coreyphillips/f33d86f3d2307e58f0a5686e6cfc8a7e to your computer and use it in GitHub Desktop.
How To Add bitcoinjs-lib Version 4.0.1 To A React Native Project

Newer version available. Please see 4.0.2

How To Add bitcoinjs-lib Version 4.0.1 To A React Native Project

Add/Link the following dependencies:

  • yarn add bitcoinjs-lib@4.0.1 react-native-randombytes buffer-reverse buffer@5
  • yarn add --dev rn-nodeify
  • react-native link react-native-randombytes
  • Add the following postinstall to your script in package.json: "postinstall": "rn-nodeify --install buffer,stream,assert,events,crypto,vm --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

  • Add the following to shim.js:

if (typeof Buffer.prototype.reverse === 'undefined') {
  var bufferReverse = require('buffer-reverse');

  Buffer.prototype.reverse = function () {
    return bufferReverse(this);
  };
}
  • Uncomment require('crypto') at the bottom of "shim.js". Or add require('crypto') to the bottom of "shim.js" if it doesn't exist.

Resolve Item In ecpair.js

  • Open ecpair.js in .../bitcoinjs-lib/src/ecpair.js and replace const randomBytes = require('randombytes') with the following: import { randomBytes } from 'react-native-randombytes'

Resolve Item In bitcoinjs-lib/src/payments/package.json

  • Open package.json in .../bitcoinjs-lib/src/payments/package.json and replace "main": "_testnet.js", with the following: "main": "index.js",

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

import "./shim";
const bitcoin = require("bitcoinjs-lib");
const keyPair = bitcoin.ECPair.makeRandom();
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey });
console.log(address);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment