Skip to content

Instantly share code, notes, and snippets.

@coreyphillips
Last active March 12, 2019 16:45
Show Gist options
  • Save coreyphillips/928ae27ccea69cd0b494d13ad2b3f27d to your computer and use it in GitHub Desktop.
Save coreyphillips/928ae27ccea69cd0b494d13ad2b3f27d to your computer and use it in GitHub Desktop.
How To Add bitcoinjs-lib Version 4.0.2 To A React Native Project

Newer version available. Please see 4.0.3

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

Add/Link the following dependencies:

  • yarn add [email protected] 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 && cd node_modules/bs58 && yarn add [email protected] && cd ../../"

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'

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);
@Vanclief
Copy link

Following this gist, I get the following error:

undefined is not a function(evaluating 'BASE_MAP.fill(255)')

@coreyphillips
Copy link
Author

coreyphillips commented Jan 30, 2019

Hey @Vanclief,

Sorry for the late response here. I ran into this same issue as I was trying to bump the bitcoinjs-lib version to 4.0.3. I did some digging and it looks like this is an issue with the base-x package. Running npm ls base-x in the intended repo should produce something similar to the following:

└─┬ [email protected]
  └─┬ [email protected]
    └─┬ [email protected]
      └── [email protected] 

Based on this Github issue, bringing the base-x version down to 3.0.4 resolves the issue. And there's discussion on why this is so here.

Unfortunately, there's no easy one-off fix I can think of at the moment, but it appears that this issue may be easier to resolve once the following change is published to npm.

I'll provide additional updates here as they come.

@coreyphillips
Copy link
Author

@Vanclief, I've updated the postinstall script in the instructions above to:

"postinstall": "rn-nodeify --install buffer,stream,assert,events,crypto,vm --hack && cd node_modules/bs58 && yarn add [email protected] && cd ../../"

After updating the postinstall script simply run yarn install again.

This will ensure the base-x version is always set to 3.0.4 to avoid the breaking changes in 3.0.5. This is not ideal, but until the change mentioned above is implemented or the base-x package becomes stable through some other means this solution should get you up and running. If it continues to give you any issues just let me know, I'll be happy to help.

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