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

Commit 6bc04b5

Browse files
authored
fix: replace js-sha3 with @noble/hashes/sha3 (#27630)
1 parent 466e994 commit 6bc04b5

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

web3.js/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
"buffer": "6.0.1",
7070
"fast-stable-stringify": "^1.0.0",
7171
"jayson": "^3.4.4",
72-
"js-sha3": "^0.8.0",
7372
"node-fetch": "2",
7473
"rpc-websockets": "^7.5.0",
7574
"superstruct": "^0.14.2"

web3.js/rollup.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ function generateConfig(configType, format) {
9797
/@babel\/runtime/,
9898
'@noble/hashes/hmac',
9999
'@noble/hashes/sha256',
100+
'@noble/hashes/sha3',
100101
'@noble/hashes/sha512',
101102
'@noble/ed25519',
102103
'@noble/secp256k1',
@@ -108,7 +109,6 @@ function generateConfig(configType, format) {
108109
'buffer',
109110
'crypto-hash',
110111
'jayson/lib/client/browser',
111-
'js-sha3',
112112
'node-fetch',
113113
'rpc-websockets',
114114
'superstruct',
@@ -164,6 +164,7 @@ function generateConfig(configType, format) {
164164
'@solana/buffer-layout',
165165
'@noble/hashes/hmac',
166166
'@noble/hashes/sha256',
167+
'@noble/hashes/sha3',
167168
'@noble/hashes/sha512',
168169
'@noble/ed25519',
169170
'@noble/secp256k1',
@@ -176,7 +177,6 @@ function generateConfig(configType, format) {
176177
'http',
177178
'https',
178179
'jayson/lib/client/browser',
179-
'js-sha3',
180180
'node-fetch',
181181
'react-native-url-polyfill',
182182
'rpc-websockets',

web3.js/src/programs/secp256k1.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Buffer} from 'buffer';
22
import * as BufferLayout from '@solana/buffer-layout';
3-
import sha3 from 'js-sha3';
3+
import {keccak_256} from '@noble/hashes/sha3';
44

55
import {PublicKey} from '../publickey';
66
import {TransactionInstruction} from '../transaction';
@@ -98,9 +98,9 @@ export class Secp256k1Program {
9898
);
9999

100100
try {
101-
return Buffer.from(
102-
sha3.keccak_256.update(toBuffer(publicKey)).digest(),
103-
).slice(-ETHEREUM_ADDRESS_BYTES);
101+
return Buffer.from(keccak_256(toBuffer(publicKey))).slice(
102+
-ETHEREUM_ADDRESS_BYTES,
103+
);
104104
} catch (error) {
105105
throw new Error(`Error constructing Ethereum address: ${error}`);
106106
}
@@ -211,9 +211,7 @@ export class Secp256k1Program {
211211
privateKey,
212212
false /* isCompressed */,
213213
).slice(1); // throw away leading byte
214-
const messageHash = Buffer.from(
215-
sha3.keccak_256.update(toBuffer(message)).digest(),
216-
);
214+
const messageHash = Buffer.from(keccak_256(toBuffer(message)));
217215
const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
218216

219217
return this.createInstructionWithPublicKey({

web3.js/test/program-tests/secp256k1.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Buffer} from 'buffer';
2-
import {keccak_256} from 'js-sha3';
2+
import {keccak_256} from '@noble/hashes/sha3';
33

44
import {
55
ecdsaSign,
@@ -43,7 +43,7 @@ if (process.env.TEST_LIVE) {
4343

4444
it('create secp256k1 instruction with string address', async () => {
4545
const message = Buffer.from('string address');
46-
const messageHash = Buffer.from(keccak_256.update(message).digest());
46+
const messageHash = Buffer.from(keccak_256(message));
4747
const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
4848
const transaction = new Transaction().add(
4949
Secp256k1Program.createInstructionWithEthAddress({
@@ -59,7 +59,7 @@ if (process.env.TEST_LIVE) {
5959

6060
it('create secp256k1 instruction with 0x prefix string address', async () => {
6161
const message = Buffer.from('0x string address');
62-
const messageHash = Buffer.from(keccak_256.update(message).digest());
62+
const messageHash = Buffer.from(keccak_256(message));
6363
const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
6464
const transaction = new Transaction().add(
6565
Secp256k1Program.createInstructionWithEthAddress({
@@ -75,7 +75,7 @@ if (process.env.TEST_LIVE) {
7575

7676
it('create secp256k1 instruction with buffer address', async () => {
7777
const message = Buffer.from('buffer address');
78-
const messageHash = Buffer.from(keccak_256.update(message).digest());
78+
const messageHash = Buffer.from(keccak_256(message));
7979
const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
8080
const transaction = new Transaction().add(
8181
Secp256k1Program.createInstructionWithEthAddress({
@@ -91,7 +91,7 @@ if (process.env.TEST_LIVE) {
9191

9292
it('create secp256k1 instruction with public key', async () => {
9393
const message = Buffer.from('public key');
94-
const messageHash = Buffer.from(keccak_256.update(message).digest());
94+
const messageHash = Buffer.from(keccak_256(message));
9595
const [signature, recoveryId] = ecdsaSign(messageHash, privateKey);
9696
const transaction = new Transaction().add(
9797
Secp256k1Program.createInstructionWithPublicKey({

0 commit comments

Comments
 (0)