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

Commit a2de58f

Browse files
authored
Use the SHA-256 implementation built into JavaScript engines (#7361)
* Use the SHA-256 implementation built into JavaScript engines * Elevate requirements to Node >=19 * Lock to 0.1.0 of tlv package for now
1 parent 933ae6c commit a2de58f

File tree

6 files changed

+35
-27
lines changed

6 files changed

+35
-27
lines changed

.github/workflows/pull-request-js.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ jobs:
4444
package:
4545
[
4646
account-compression,
47-
libraries,
4847
memo,
4948
name-service,
5049
stake-pool,
@@ -54,7 +53,9 @@ jobs:
5453
token-swap,
5554
]
5655
include:
57-
# Restrict single-pool and token-lending to supported Node.js versions.
56+
# Restrict certain packages to supported Node.js versions.
57+
- package: libraries
58+
node-version: 20.x
5859
- package: single-pool
5960
node-version: 20.5
6061
- package: token-lending

libraries/type-length-value/js/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Library with utilities for working with Type-Length-Value structures in js.
88
import { TlvState, SplDiscriminator } from '@solana/spl-type-length-value';
99

1010
const tlv = new TlvState(tlvData, discriminatorSize, lengthSize);
11-
const discriminator = splDiscriminate("<discriminator-hash-input>", discriminatorSize);
11+
const discriminator = await splDiscriminate("<discriminator-hash-input>", discriminatorSize);
1212

1313
const firstValue = tlv.firstBytes(discriminator);
1414

libraries/type-length-value/js/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "@solana/spl-type-length-value",
33
"description": "SPL Type Length Value Library",
4-
"version": "0.1.0",
4+
"version": "0.2.0",
55
"author": "Solana Labs Maintainers <[email protected]>",
66
"repository": "https://github.com/solana-labs/solana-program-library",
77
"license": "Apache-2.0",
88
"type": "module",
99
"sideEffects": false,
1010
"engines": {
11-
"node": ">=16"
11+
"node": ">=19"
1212
},
1313
"files": [
1414
"lib",
@@ -43,6 +43,7 @@
4343
"watch": "tsc --build --verbose --watch tsconfig.all.json"
4444
},
4545
"dependencies": {
46+
"@solana/assertions": "^2.0.0-rc.1",
4647
"buffer": "^6.0.3"
4748
},
4849
"devDependencies": {
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { createHash } from 'crypto';
1+
import { assertDigestCapabilityIsAvailable } from '@solana/assertions';
22

3-
export const splDiscriminate = (discriminator: string, length = 8): Buffer => {
4-
const digest = createHash('sha256').update(discriminator).digest();
5-
return digest.subarray(0, length);
6-
};
3+
export async function splDiscriminate(discriminator: string, length = 8): Promise<Uint8Array> {
4+
assertDigestCapabilityIsAvailable();
5+
const bytes = new TextEncoder().encode(discriminator);
6+
const digest = await crypto.subtle.digest('SHA-256', bytes);
7+
return new Uint8Array(digest).subarray(0, length);
8+
}
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import { expect } from 'chai';
22
import { splDiscriminate } from '../src/splDiscriminate';
3-
import { createHash } from 'crypto';
43

5-
describe('splDiscrimintor', () => {
6-
const testVectors = [
7-
'hello',
8-
'this-is-a-test',
9-
'test-namespace:this-is-a-test',
10-
'test-namespace:this-is-a-test:with-a-longer-name',
11-
];
4+
const testVectors = [
5+
'hello',
6+
'this-is-a-test',
7+
'test-namespace:this-is-a-test',
8+
'test-namespace:this-is-a-test:with-a-longer-name',
9+
];
1210

13-
const testExpectedBytes = testVectors.map(x => {
14-
return createHash('sha256').update(x).digest();
15-
});
11+
const testExpectedBytes = await Promise.all(
12+
testVectors.map(x =>
13+
crypto.subtle.digest('SHA-256', new TextEncoder().encode(x)).then(digest => new Uint8Array(digest)),
14+
),
15+
);
1616

17-
const testSplDiscriminator = (length: number) => {
17+
describe('splDiscrimintor', () => {
18+
const testSplDiscriminator = async (length: number) => {
1819
for (let i = 0; i < testVectors.length; i++) {
19-
const discriminator = splDiscriminate(testVectors[i], length);
20+
const discriminator = await splDiscriminate(testVectors[i], length);
2021
const expectedBytes = testExpectedBytes[i].subarray(0, length);
2122
expect(discriminator).to.have.length(length);
2223
expect(discriminator).to.deep.equal(expectedBytes);
@@ -29,9 +30,9 @@ describe('splDiscrimintor', () => {
2930
testSplDiscriminator(2);
3031
});
3132

32-
it('should produce the same bytes as rust library', () => {
33+
it('should produce the same bytes as rust library', async () => {
3334
const expectedBytes = Buffer.from([105, 37, 101, 197, 75, 251, 102, 26]);
34-
const discriminator = splDiscriminate('spl-transfer-hook-interface:execute');
35+
const discriminator = await splDiscriminate('spl-transfer-hook-interface:execute');
3536
expect(discriminator).to.deep.equal(expectedBytes);
3637
});
3738
});

pnpm-lock.yaml

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)