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

fix: remove dependency on tweetnacl #73

Merged
merged 9 commits into from
Jun 13, 2024
Merged

fix: remove dependency on tweetnacl #73

merged 9 commits into from
Jun 13, 2024

Conversation

JustinBeckwith
Copy link
Contributor

@JustinBeckwith JustinBeckwith commented May 18, 2024

This change drops the dependency on tweetnacl for key verification, instead relying on the node.js built-in Ed25519 implementation. This seems to work locally with node 18+, but needs to be tested in other environments.

I'm not sure this is a good idea. The ed225519 support in node 22 is still experimental, and this will drop a console warning on startup:
https://nodejs.org/api/webcrypto.html#ed25519ed448x25519x448-key-pairs

  • Set a correct minimum version of node.js in engines
  • Verify it works as expected with Cloudflare workers
  • Ship a preview version of the package before 4.x to let people try it out first
  • Make sure to update the changelog to call out the breaking change for making verifyKey async

Fixes #30 and generally resolves #54

@JustinBeckwith JustinBeckwith marked this pull request as draft May 18, 2024 16:06
@JustinBeckwith JustinBeckwith marked this pull request as ready for review May 22, 2024 17:06
@JustinBeckwith
Copy link
Contributor Author

src/util.ts Outdated Show resolved Hide resolved
): Uint8Array {
if (value == null) {
return new Uint8Array();
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is illegal in the types; are we doing this to protect against JS users?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume as such, yes. This function was copypasta'd from index.ts, in it's exact same form. I didn't change of it.

Comment on lines +44 to +48
const matches = value.match(/.{1,2}/g);
if (matches == null) {
throw new Error('Value is not a valid hex string');
}
const hexVal = matches.map((byte: string) => Number.parseInt(byte, 16));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of insane. Is this really the best approach 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dude ... if you can find a better approach that works I'm all ears. This is a copy/paste from the original code, just shifted over. I tried doing some other clever things with btoa and TextEncoder, but the whole "hex string" thing threw me for a loop.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually ran into this recently on a separate project and ended up resorting to buffers since the native web APIs didn't support this very well. I suppose we're stuck with this for now.

return new Uint8Array(value);
}
} catch (ex) {
// Runtime doesn't have Buffer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do a typeof Buffer !== 'undefined' check here in case we're swallowing exceptions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't imagine what other kind of exception could happen in this very narrow use case

@JustinBeckwith JustinBeckwith merged commit 69499c9 into main Jun 13, 2024
4 checks passed
@JustinBeckwith JustinBeckwith deleted the nodeps branch June 13, 2024 19:42
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

Successfully merging this pull request may close these issues.

Replace tweetnacl with discord-verify Suggestion: Change verifyKey function to be pure JS friendly
4 participants