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 addrequire('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:
- https://youtu.be/pKRM5eAZl8c (outdated)
Hi guys.
I tried everything with this gist but I can't get it running. The error I get now is the following:
TypeError: undefined is not an object (evaluating '_bitcoinjsLib.default.ECPair')
I tried with newer packages, maybe that is the problem:
"dependencies": {
"assert": "^1.5.0",
"bitcoinjs-lib": "^6.0.1",
"buffer": "^6.0.3",
"ecpair": "^2.0.1",
"events": "^1.1.1",
"process": "^0.11.10",
"react": "17.0.2",
"react-native": "0.68.2",
"react-native-crypto": "^2.2.0",
"react-native-randombytes": "^3.6.1",
"readable-stream": "^4.0.0",
"stream-browserify": "^1.0.0",
"vm-browserify": "^0.0.4"
},
Any ideas? Thanks!