Using with React NativeΒΆ

To use @herajs/crypto with React Native, you need to shim a few Node internal packages.

Otherwise, you may get an error like Module `crypto does not exist in the Haste module map`.

The following guide uses rn-nodeify.

1. Installation

When using Yarn:

// Install dependencies
yarn add react-native-crypto react-native-randombytes

// Fix integration
react-native link react-native-randombytes
yarn add -D tradle/rn-nodeify
./node_modules/.bin/rn-nodeify --install --hack --yarn

When using NPM:

// Install dependencies
npm install --save react-native-crypto react-native-randombytes

// Fix integration
react-native link react-native-randombytes
npm install --save-dev tradle/rn-nodeify
./node_modules/.bin/rn-nodeify --install --hack

Note

You have to run the final command every time you add packages. It is a good idea to add it as a post-install script to your package.json:

"scripts": {
    "postinstall": "rn-nodeify --install --hack"
}

2. Add shim to index.js

Import these at the top of the file.

import './shim.js'
import crypto from 'crypto'

If you are using a simulator, you may also need to add this line to shim.js:

self = undefined

3. Use normally

Now you can use @herajs/crypto normally. Add the dependency @herajs/crypto and use it, for example:

import { createIdentity } from '@herajs/crypto';

const identity = createIdentity();