0

I wrote a small PoC for JOSE which works on Expo web but fails on iOS because it's looking for crypto and it's showing can't find variable: crypto

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265

1 Answers1

0

A few other packages need to be installed

npx expo install expo-random text-encoding buffer expo-standard-web-crypto

Then add file that would initialize the polyfills that are needed.

// initializePolyfills.ts
global.Buffer = require("buffer").Buffer;
import { polyfillWebCrypto } from "expo-standard-web-crypto";
polyfillWebCrypto();
const TextEncodingPolyfill = require("text-encoding");

Object.assign(global, {
  TextEncoder: TextEncodingPolyfill.TextEncoder,
  TextDecoder: TextEncodingPolyfill.TextDecoder,
});

In app.tsx, import the file as the first line

import './importPolyfills'
...
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
  • well, `expo-standard-web-crypto` only has `crypto.getRandomValues()`, so even if it installed the global correctly it wouldn't be enough to do any JOSE operation. –  Oct 13 '22 at 12:06
  • your best bet at a working ios webcrypto is to use the webkit's webcrypto through a hidden webview. see [1](https://github.com/webview-crypto/react-native-webview-crypto), [2](https://github.com/webview-crypto/webview-crypto) for the approach. Unfortunately, even with this in place the jose lib will not work for you because it doesn't bring in the CryptoKey global. You can however get inspired by the approach and do both SubtleCrypto and jose from within the webview, that will, in theory, work. –  Oct 13 '22 at 12:10
  • It seems enough for signature verification. But may be insufficient to do encryption which I haven't tested yet. – Archimedes Trajano Oct 13 '22 at 16:13