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

You should be able to time when to polyfill Ed25519 crypto methods #2898

Closed
steveluscher opened this issue Jun 30, 2024 · 2 comments · Fixed by #2902
Closed

You should be able to time when to polyfill Ed25519 crypto methods #2898

steveluscher opened this issue Jun 30, 2024 · 2 comments · Fixed by #2902
Assignees
Labels
enhancement New feature or request released

Comments

@steveluscher
Copy link
Collaborator

Motivation

Presently, for environments that don't support Ed25519 natively (React Native, old browsers) we need a polyfill. We would benefit from being able to time when that polyfill installs.

Example use case

Our current implementation of that is to do a raw require of this:

import '@solana/webcrypto-ed25519-polyfill';

There comes a problem when you're building a React Native application. RN applications themselves need a polyfill of the entire SubtleCrypto API because it doesn't exist to begin with.

One example of this is https://github.com/margelo/react-native-quick-crypto. This doesn't work with our solution because doing this:

import { install } from 'react-native-quick-crypto';
install();
import '@solana/webcrypto-ed25519-polyfill';

Results in this sequence of events:

  1. Our polyfill's module factory runs first and creates globalThis.crypto.subtle
  2. The install() method of react-native-quick-crypto runs second and overwrites globalThis.crypto

And you're left without the Ed25519 polyfill. Womp womp.

Details

Either

  1. Stop polyfilling in the module factory and instead export an install() method that people have to call. Update all the docs and example apps, or
  2. Export two versions of @solana/webcrypto-ed25519-polyfill (eg. export a @solana/webcrypto-ed25519-polyfill/installer) so that you can either bare-import the automatic-install version or import an install() method that you can call at the right time.

The second would be elegant, but subpackage exports might be a huge pain in the ass so don't kill yourself trying to make it work.

@steveluscher steveluscher added the enhancement New feature or request label Jun 30, 2024
@mcintyre94 mcintyre94 self-assigned this Jul 1, 2024
steveluscher pushed a commit that referenced this issue Jul 2, 2024
#2902)

This PR refactors the webcrypto polyfill so that all its functionality is in an `install` function. This must be called to enable the polyfill and it is no longer automatically installed by just importing. This gives apps more control over when the polyfill is called.

Example:

```ts
import { install } from '@solana/webcrypto-ed25519-polyfill';

async function main() {
    try {
        await crypto.subtle.generateKey('Ed25519', false, ['sign']);
    } catch (e) {
        console.log('unable to create keypair first time');
    }
    install();
    const keyPair = await crypto.subtle.generateKey('Ed25519', false, ['sign']); 
    console.log(keyPair);
}

main().then(() => console.log('done!'));
```

This will first log "unable to create keypair first time", as the polyfill is not yet installed. It will then log an Ed25519 keypair object created after installing the polyfill. 

Fixes #2898
Copy link
Contributor

github-actions bot commented Jul 8, 2024

🎉 This issue has been resolved in version 1.95.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link
Contributor

Because there has been no activity on this issue for 7 days since it was closed, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants