Skip to content

Instantly share code, notes, and snippets.

@coreyphillips
Last active August 7, 2019 13:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreyphillips/9719d7e4b1f6042b993f548d2083cee8 to your computer and use it in GitHub Desktop.
Save coreyphillips/9719d7e4b1f6042b993f548d2083cee8 to your computer and use it in GitHub Desktop.
How To Add bitcoinjs-lib Version 5.0.5 To A React Native Project

Newer version available. Please see 5.1.1

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

Add/Link the following dependencies:

  • yarn add bitcoinjs-lib@5.0.5 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 base-x@3.0.4 && 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);
@osoese
Copy link

osoese commented May 30, 2019

any way to make this work on expo?
I get:
undefined is not an object (evaluating 'RNRandomBytes.seed')
I do not want to eject the project

thanks!

@coreyphillips
Copy link
Author

@osoese:
For this to work in Expo I believe you would need to find an alternative to react-native-randombytes that doesn't require you to react-native link it to your project.

After some digging it appears that this was brought up as a request here and it looks like you may be able to use this (expo-random) in place of react-native-randombytes.

Hope this helps!

@osoese
Copy link

osoese commented May 30, 2019

well, that is very helpful, thank you. I assume edit the ecpair.js file with the example kind of but I have not gotten it to work yet. I will edit this comment if I do. Thank you! By the way my approach is to import the Random from expo - turn the makeRandom function into an async and comment out the options lines and d = await Random.getRandomBytesAsync(32);
If that sounds ridiculous for some reason please let me know.

@coreyphillips
Copy link
Author

@osoese:
No, not at all. That appears to be a viable solution. If you manage to get it working please let me know. I'll be sure to update the instructions to include a section for Expo.

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