Skip to content

Commit 3539db3

Browse files
committed
refactor(sdk/js): use @noble/hashes instead of node crypto
replace crypto.createHash('sha384'|'sha256') calls with @noble/hashes sha384/sha256, which work identically in node and browsers without needing a polyfill. @noble/hashes is already a peer dependency. this prepares for removing crypto-browserify, whose upstream chain (elliptic, create-ecdh, browserify-sign) carries unpatched advisories.
1 parent dc5fc21 commit 3539db3

5 files changed

Lines changed: 16 additions & 32 deletions

File tree

sdk/js/src/get-compose-hash.browser.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
import crypto from 'crypto';
5+
import { sha256 } from '@noble/hashes/sha256';
6+
import { bytesToHex } from '@noble/hashes/utils';
67

78
type SortableValue = string | number | boolean | null | undefined | SortableObject | SortableArray;
89
interface SortableObject {
@@ -27,15 +28,8 @@ function sortObjectKeys(obj: SortableValue): SortableValue {
2728
return sortedObj;
2829
}
2930

30-
/**
31-
* Browser-compatible SHA-256 hash using crypto-browserify
32-
* @param data - Data to hash
33-
* @returns Promise resolving to hex-encoded hash
34-
*/
3531
async function sha256Hash(data: string): Promise<string> {
36-
const hash = crypto.createHash('sha256');
37-
hash.update(data, 'utf8');
38-
return hash.digest('hex');
32+
return bytesToHex(sha256(new TextEncoder().encode(data)));
3933
}
4034

4135
/**

sdk/js/src/get-compose-hash.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
import crypto from "crypto";
5+
import { sha256 } from "@noble/hashes/sha256";
6+
import { bytesToHex } from "@noble/hashes/utils";
67

78
type SortableValue = string | number | boolean | null | undefined | SortableObject | SortableArray;
89
interface SortableObject {
@@ -104,5 +105,5 @@ export function getComposeHash(app_compose: AppCompose, normalize: boolean = fal
104105
app_compose = preprocessAppCompose(app_compose);
105106
}
106107
const manifest_str = toDeterministicJson(app_compose);
107-
return crypto.createHash("sha256").update(manifest_str, "utf8").digest("hex");
108+
return bytesToHex(sha256(new TextEncoder().encode(manifest_str)));
108109
}

sdk/js/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
import fs from 'fs'
6-
import crypto from 'crypto'
6+
import { sha384 } from '@noble/hashes/sha512'
77
import { send_rpc_request } from './send-rpc-request'
88
export { getComposeHash } from './get-compose-hash'
99
export { verifyEnvEncryptPublicKey, verifyEnvEncryptPublicKeyLegacy } from './verify-env-encrypt-public-key'
@@ -154,9 +154,7 @@ function replay_rtmr(history: string[]): string {
154154
const padding = Buffer.alloc(48 - contentBuffer.length, 0)
155155
contentBuffer = Buffer.concat([contentBuffer, padding])
156156
}
157-
mr = Buffer.from(crypto.createHash('sha384')
158-
.update(Buffer.concat([mr, contentBuffer]))
159-
.digest())
157+
mr = Buffer.from(sha384(Buffer.concat([mr, contentBuffer])))
160158
}
161159
return mr.toString('hex')
162160
}

sdk/js/src/solana.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
import crypto from 'crypto'
5+
import { sha256 } from '@noble/hashes/sha256'
66
import { type GetKeyResponse, type GetTlsKeyResponse } from './index'
77
import { Keypair } from '@solana/web3.js'
88

@@ -28,14 +28,9 @@ export function toKeypair(keyResponse: GetTlsKeyResponse | GetKeyResponse) {
2828
export function toKeypairSecure(keyResponse: GetTlsKeyResponse | GetKeyResponse) {
2929
// Keep legacy behavior for GetTlsKeyResponse, but with warning.
3030
if (keyResponse.__name__ === 'GetTlsKeyResponse') {
31-
try {
32-
console.warn('toKeypairSecure: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
33-
// Get supported hash algorithm by `openssl list -digest-algorithms`, but it's not guaranteed to be supported by node.js
34-
const buf = crypto.createHash('sha256').update(keyResponse.asUint8Array()).digest()
35-
return Keypair.fromSeed(buf)
36-
} catch (err) {
37-
throw new Error('toKeypairSecure: missing sha256 support, please upgrade your openssl and node.js')
38-
}
31+
console.warn('toKeypairSecure: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
32+
const buf = sha256(keyResponse.asUint8Array())
33+
return Keypair.fromSeed(buf)
3934
}
4035
return Keypair.fromSeed(keyResponse.key)
4136
}

sdk/js/src/viem.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
import crypto from 'crypto'
5+
import { sha256 } from '@noble/hashes/sha256'
6+
import { bytesToHex } from '@noble/hashes/utils'
67
import { type GetKeyResponse, type GetTlsKeyResponse } from './index'
78
import { privateKeyToAccount } from 'viem/accounts'
89

@@ -29,13 +30,8 @@ export function toViemAccountSecure(keyResponse: GetKeyResponse | GetTlsKeyRespo
2930
// Keep legacy behavior for GetTlsKeyResponse, but with warning.
3031
if (keyResponse.__name__ === 'GetTlsKeyResponse') {
3132
console.warn('toViemAccountSecure: Please don\'t use `deriveKey` method to get key, use `getKey` instead.')
32-
try {
33-
// Get supported hash algorithm by `openssl list -digest-algorithms`, but it's not guaranteed to be supported by node.js
34-
const hex = crypto.createHash('sha256').update(keyResponse.asUint8Array()).digest('hex')
35-
return privateKeyToAccount(`0x${hex}`)
36-
} catch (err) {
37-
throw new Error('toViemAccountSecure: missing sha256 support, please upgrade your openssl and node.js')
38-
}
33+
const hex = bytesToHex(sha256(keyResponse.asUint8Array()))
34+
return privateKeyToAccount(`0x${hex}`)
3935
}
4036
const hex = Array.from(keyResponse.key).map(b => b.toString(16).padStart(2, '0')).join('')
4137
return privateKeyToAccount(`0x${hex}`)

0 commit comments

Comments
 (0)