Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Use the SHA-256 implementation built into JavaScript engines #7361

Merged
merged 3 commits into from
Oct 17, 2024

Conversation

steveluscher
Copy link
Contributor

@steveluscher steveluscher commented Oct 16, 2024

This is a breaking change which removes the need for browsers to introduce a polyfill for createHash. Engines now have a built-in SubtleCrypto#digest which serves this purpose, polyfill-free, with good support.

The reason this is a breaking change is that createHash is synchronous and SubtleCrypto#digest is async.

Related to #7351.

@@ -1,7 +1,7 @@
{
"name": "@solana/spl-type-length-value",
"description": "SPL Type Length Value Library",
"version": "0.1.0",
"version": "0.2.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Breaking change.

export async function splDiscriminate(discriminator: string, length = 8): Promise<Uint8Array> {
assertDigestCapabilityIsAvailable();
const bytes = new TextEncoder().encode(discriminator);
const digest = await crypto.subtle.digest('SHA-256', bytes);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ugh. crypto wasn't added as a global until Node 19. Node 20 is presently LTS.

Our choices are:

  1. Elevate the requirements to Node 19, or
  2. Introduce a dual-build Node/Browser system like we have in @solana/web3.js to solana-program-library.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can keep it simple and elevate the requirement for this package to Node 19. Considering it'll become a dev dependency for the two packages that use it, I can't see any drawbacks. Are there any other potential negative consequences that I'm missing?

Copy link
Contributor

Choose a reason for hiding this comment

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

As part of that change, can you also update CI to only run the 'libraries' step for node v20?

node-version: [16.x, 18.x, 20.x]
package:
[
account-compression,
libraries,
memo,
name-service,
stake-pool,
token,
token-group,
token-metadata,
token-swap,
]
include:
# Restrict single-pool and token-lending to supported Node.js versions.
- package: single-pool
node-version: 20.5
- package: token-lending
node-version: 18.5

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally.

@steveluscher steveluscher force-pushed the for-sha branch 4 times, most recently from d4f790a to fa04fe7 Compare October 17, 2024 15:12
Copy link
Contributor

@joncinque joncinque left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

@steveluscher steveluscher merged commit a2de58f into master Oct 17, 2024
33 checks passed
@steveluscher steveluscher deleted the for-sha branch October 17, 2024 16:23
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants