Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-native support #80

Open
dyegolara opened this issue Jan 7, 2023 · 24 comments
Open

react-native support #80

dyegolara opened this issue Jan 7, 2023 · 24 comments

Comments

@dyegolara
Copy link

I have tried to polyfill my react-native app so it could use the crypto native module, but then I realized that this library is meant to be used by clients, a lot of which doesn't support node's native crypto module.

May I suggest using some library instead, like https://github.com/entronad/crypto-es ?
Do you think it would be possible?

@aussedatlo
Copy link

With Expo and react-native, you can patch the new version of @noble/secp256k1 to use expo-random instead of node's native crypto module.

It's not the best solution but it work.

@fiatjaf
Copy link
Collaborator

fiatjaf commented Jan 17, 2023

I forgot to answer this, but that was fine because I didn't have an answer, and @aussedatlo has provided us with a potential solution here that I don't understand either.

@KoalaSat any insight here?

@KoalaSat
Copy link

KoalaSat commented Jan 17, 2023

Yes, I hacked it for Nostros by forking the cipher-base library and changing this dependency browserify/cipher-base@master...KoalaSat:cipher-base:master

At that point I'm just downloading it on my package https://github.com/KoalaSat/nostros/blob/main/package.json#L22

As a little bit of background, the issue is that nostr-tools depends on an ECMA version that Android still does not implements (can't remember the number it was like 19 vs 21 or something) but this only relays on a 3rd level dependency, so by changing that library you just make it compatible with Android's ECMA version

@KoalaSat
Copy link

you might also have problems with Buffer, let me know if it's your case @dyegolara

@fiatjaf
Copy link
Collaborator

fiatjaf commented Jan 17, 2023

In the newest nostr-tools version there is no usage of Buffer anymore.

@KoalaSat
Copy link

KoalaSat commented Jan 18, 2023

I wasn't aware of that then maybe I'll try to get rid of the hack for Nostros 👍

@facinick
Copy link

facinick commented Feb 1, 2023

what's the status of this issue, does nostr-tools support react native now?

@Eosxx
Copy link
Contributor

Eosxx commented May 1, 2023

Try this.

import * as secp256k1 from "@noble/secp256k1";
import { randomBytes  } from 'react-native-randombytes'

secp256k1.utils.randomBytes = function (bytesLength  = 32) {
  return Uint8Array.from(randomBytes(bytesLength));
}

@earonesty
Copy link

earonesty commented Jun 7, 2023

@Eosxx even with randomBytes working, crypto.subtle isn't going to work on RN. not sure how this is a solution. need a polyfill for subtle for nostr-tools to work on RN reliably.

@earonesty
Copy link

looks like other projects just don't use the libs from nostr-tools instead.... https://github.com/KoalaSat/nostros/blob/main/frontend/lib/nostr/Nip04/index.ts. im having some luck with isomorphic-webcrypto. everything works but the randomBytes. going to patch it up soon, and then release a new version (maintainer unresponsive). don't like "rolling my own". nostr-tools has more eyeballs.

@Saleciani
Copy link

Any news on this? trying to implement a client with expo react-native and functions such as getSignature and generatePrivateKey do not seem to work.

I have had a quick fix for generatePrivate key such as

async function generatePrivateKey(): Promise<string> {
  const privateKeyBytes = await Crypto.digestStringAsync(Crypto.CryptoDigestAlgorithm.SHA256, Math.random().toString());
  return privateKeyBytes.substring(0, 64);
}

Any tips on getting around this?

@earonesty
Copy link

earonesty commented Jun 22, 2023

i use this: https://github.com/earonesty/isomorphic-webcrypto + expo-crypto

works fine to get you all the stuff you need. working on a faster one. will have it up in a week or so

@Saleciani
Copy link

So you are currently working on a PR to add react native support?

Or the solution you say is to use those libraries and emulate the functions from nostr tools?

@thevikas
Copy link

thevikas commented Jul 9, 2023

I am getting Property TextDecoder doesn't exists on my RN usage. Any way around it? any polyfills that work to implement this method in RN?

@earonesty
Copy link

@thevikas
Copy link

Is there any react native proj using nostr-tools? the getRandomValues is also undefined.

@minibits-cash
Copy link

I use nostr-tools in my RN project the following way:

  1. Install react-native-quick-crypto following their readme. It exposes part of the node crypto functions using C++ implementation and JSI bridge - fast and without the need for shim.js
  2. There are still some functions missing that are used by nostr-tools, notably crypto.subtle used e.g. for encrypting NIP04 messages. However encryption/decryption can be easily implemented using examples in nip04 docs that does not use subtle, just replace node crypto functions with their Quickcrypto implementation.

@SamSamskies
Copy link
Contributor

I used react-native-get-random-values to polyfill crypto.getRandomValues. It's recommended in the README for @noble/curves. Haven't tested much, but signing events and generating private keys are now working for me.

@SamSamskies
Copy link
Contributor

Using a SimplePool instance doesn't work for me in react native though. I haven't been able to figure out why. I've tried both pool.publish and pool.get and both don't work. Promises all get rejected. Works fine if I use relay.publish and relay.get.

@gasparnd
Copy link

Someone can use nostr-tools in React Native? I have the next problems:
Error: crypto.getRandomValues must be defined
Error: crypto.getRandomValues must be defined

@gasparnd
Copy link

With Expo and react-native, you can patch the new version of @noble/secp256k1 to use expo-random instead of node's native crypto module.

It's not the best solution but it work.

Can your share the code please, i have a bare react native app with Expo modules and it doesn't work for me

@SamSamskies
Copy link
Contributor

With Expo and react-native, you can patch the new version of @noble/secp256k1 to use expo-random instead of node's native crypto module.
It's not the best solution but it work.

Can your share the code please, i have a bare react native app with Expo modules and it doesn't work for me

Try using react-native-get-random-values. That's I used for the Wavlake mobile app.

https://github.com/wavlake/mobile/blob/467b0824c5adde4da556e86e478ee9a6a1b4cb61/utils/nostr.ts#L4-L5

@gasparnd
Copy link

gasparnd commented Dec 7, 2023

With Expo and react-native, you can patch the new version of @noble/secp256k1 to use expo-random instead of node's native crypto module.
It's not the best solution but it work.

Can your share the code please, i have a bare react native app with Expo modules and it doesn't work for me

Try using react-native-get-random-values. That's I used for the Wavlake mobile app.

https://github.com/wavlake/mobile/blob/467b0824c5adde4da556e86e478ee9a6a1b4cb61/utils/nostr.ts#L4-L5

It works fine, but the moment I set up React navigation it stopped working. What can i do? I'm presenting the same problem that i received before

@Egge21M
Copy link
Contributor

Egge21M commented Dec 29, 2023

It works fine, but the moment I set up React navigation it stopped working. What can i do? I'm presenting the same problem that i received before

Did you upgrade nostr-tools? Later versions are even less compatible with react-native.

Here is what works for all the projects that I work on:

  • [email protected] (pre curves and MessageQueue
  • patch @noble packages to use expo-crypto for entropy
  • polyfill TextEncoder and TextDecoder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests