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

chore: replace ethers implementations of sha256, scrypt and keccak #1786

Merged
merged 24 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fc71471
chore: replace ethers sha256 in hasher
danielbate Feb 21, 2024
06e3c45
chore: replace ethers sha256 in transactions
danielbate Feb 21, 2024
0fdd55d
chore: replace ethers sha256 in abi-coder
danielbate Feb 21, 2024
a4535f9
chore: replace ethers sha256 in address
danielbate Feb 21, 2024
187ebdb
chore: replace ethers sha256 in merkle
danielbate Feb 21, 2024
88922c8
chore: regen lock
danielbate Feb 21, 2024
1c3b941
Merge branch 'master' into db/chore/replace-ethers-hashing-algos
danielbate Feb 22, 2024
9bd1adc
Merge branch 'master' into db/chore/replace-ethers-hashing-algos
danielbate Feb 22, 2024
9459f4b
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
danielbate Mar 4, 2024
24ab61c
chore: replace ethers in merkle and hash package
danielbate Mar 5, 2024
b7fd45b
chore: replace ethers in crypto
danielbate Mar 5, 2024
6a91965
chore: replace ethers in contract
danielbate Mar 5, 2024
91f89b1
chore: replace ethers BytesLike in abi coder
danielbate Mar 5, 2024
bfa2d8f
chore: fix hasher teest
danielbate Mar 5, 2024
fbe2fb1
feat: replace sha256 usages in account
danielbate Mar 5, 2024
7387399
feat: replace scrypt usage
danielbate Mar 5, 2024
684e411
feat: replace keccak
danielbate Mar 5, 2024
2b1c648
chore: regen lock
danielbate Mar 5, 2024
bec01a4
chore: add missing utils dep
danielbate Mar 5, 2024
5a51d3f
Merge branch 'master' of https://github.com/FuelLabs/fuels-ts into db…
danielbate Mar 5, 2024
d2309f8
chore: changeset
danielbate Mar 5, 2024
9ad94b2
chore: regen lock
danielbate Mar 5, 2024
c877ac3
Merge branch 'master' into db/chore/replace-ethers-hashing-algos
danielbate Mar 5, 2024
75600a5
Merge branch 'master' into db/chore/replace-ethers-hashing-algos
nedsalk Mar 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/orange-lions-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@fuel-ts/abi-coder": patch
"@fuel-ts/account": patch
"@fuel-ts/address": patch
"@fuel-ts/contract": patch
"@fuel-ts/crypto": patch
"@fuel-ts/errors": patch
"@fuel-ts/hasher": patch
"@fuel-ts/merkle": patch
"@fuel-ts/transactions": patch
---

Migrate implementations of `sha256`, `keccak` and `scrypt` to `@noble/hashes`
5 changes: 3 additions & 2 deletions packages/abi-coder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
"license": "Apache-2.0",
"dependencies": {
"@fuel-ts/crypto": "workspace:*",
"@fuel-ts/errors": "workspace:*",
"@fuel-ts/hasher": "workspace:^",
"@fuel-ts/interfaces": "workspace:*",
"@fuel-ts/math": "workspace:*",
"@fuel-ts/versions": "workspace:*",
"@fuel-ts/utils": "workspace:*",
"@fuel-ts/errors": "workspace:*",
"@fuel-ts/versions": "workspace:*",
"ethers": "^6.7.1",
"type-fest": "^3.1.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/abi-coder/src/FunctionFragment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { bufferFromString } from '@fuel-ts/crypto';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { sha256 } from '@fuel-ts/hasher';
import type { BytesLike } from '@fuel-ts/interfaces';
import { bn } from '@fuel-ts/math';
import { arrayify } from '@fuel-ts/utils';
import { sha256 } from 'ethers';

import { AbiCoder } from './AbiCoder';
import { ResolvedAbiType } from './ResolvedAbiType';
Expand Down
2 changes: 1 addition & 1 deletion packages/abi-coder/src/encoding/coders/AbstractCoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BytesLike } from '@fuel-ts/interfaces';
import type { BN } from '@fuel-ts/math';
import type { BytesLike } from 'ethers';

import type { Option } from './v0/OptionCoder';

Expand Down
11 changes: 2 additions & 9 deletions packages/account/src/hdwallet/hdwallet.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { sha256 } from '@fuel-ts/hasher';
import type { BytesLike } from '@fuel-ts/interfaces';
import { bn, toBytes, toHex } from '@fuel-ts/math';
import { arrayify, hexlify, concat } from '@fuel-ts/utils';
import {
toBeHex,
dataSlice,
encodeBase58,
decodeBase58,
sha256,
computeHmac,
ripemd160,
} from 'ethers';
import { toBeHex, dataSlice, encodeBase58, decodeBase58, computeHmac, ripemd160 } from 'ethers';

import { Mnemonic } from '../mnemonic';
import { Signer } from '../signer';
Expand Down
3 changes: 2 additions & 1 deletion packages/account/src/mnemonic/mnemonic.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { randomBytes } from '@fuel-ts/crypto';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { sha256 } from '@fuel-ts/hasher';
import type { BytesLike } from '@fuel-ts/interfaces';
import { arrayify, hexlify, concat } from '@fuel-ts/utils';
import { dataSlice, pbkdf2, sha256, computeHmac, encodeBase58 } from 'ethers';
import { dataSlice, pbkdf2, computeHmac, encodeBase58 } from 'ethers';

import { english } from '../wordlists';

Expand Down
2 changes: 1 addition & 1 deletion packages/account/src/mnemonic/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { sha256 } from '@fuel-ts/hasher';
import type { BytesLike } from '@fuel-ts/interfaces';
import { arrayify } from '@fuel-ts/utils';
import { sha256 } from 'ethers';

/* Mnemonic phrase composed by words from the provided wordlist it can be a text or a array of words */
export type MnemonicPhrase = string | Array<string>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ZeroBytes32 } from '@fuel-ts/address/configs';
import { uint64ToBytesBE } from '@fuel-ts/hasher';
import { uint64ToBytesBE, sha256 } from '@fuel-ts/hasher';
import { bn } from '@fuel-ts/math';
import { TransactionType, InputType, OutputType, TransactionCoder } from '@fuel-ts/transactions';
import { concat } from '@fuel-ts/utils';
import { sha256 } from 'ethers';
import { clone } from 'ramda';

import type { TransactionRequest } from './types';
Expand Down
2 changes: 1 addition & 1 deletion packages/account/src/signer/signer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sha256 } from '@fuel-ts/hasher';
import { arrayify } from '@fuel-ts/utils';
import { sha256 } from 'ethers';

import { Signer } from './signer';

Expand Down
4 changes: 2 additions & 2 deletions packages/address/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@fuel-ts/interfaces": "workspace:*",
"@fuel-ts/utils": "workspace:^",
"@fuel-ts/versions": "workspace:*",
"bech32": "^2.0.0",
"ethers": "^6.7.1"
"@noble/hashes": "^1.3.3",
"bech32": "^2.0.0"
}
}
4 changes: 2 additions & 2 deletions packages/address/src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FuelError } from '@fuel-ts/errors';
import { AbstractAddress } from '@fuel-ts/interfaces';
import type { Bech32Address, B256Address, EvmAddress, AssetId } from '@fuel-ts/interfaces';
import { arrayify, hexlify } from '@fuel-ts/utils';
import { sha256 } from 'ethers';
import { sha256 } from '@noble/hashes/sha256';

import {
normalizeBech32,
Expand Down Expand Up @@ -150,7 +150,7 @@ export default class Address extends AbstractAddress {
throw new FuelError(FuelError.CODES.INVALID_PUBLIC_KEY, `Invalid Public Key: ${publicKey}.`);
}

const b256Address = sha256(hexlify(arrayify(publicKey)));
const b256Address = hexlify(sha256(arrayify(publicKey)));
return new Address(toBech32(b256Address));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@fuel-ts/versions": "workspace:*",
"@fuel-ts/account": "workspace:*",
"@fuel-ts/errors": "workspace:*",
"ethers": "^6.7.1"
"@fuel-ts/hasher": "workspace:*"
},
"devDependencies": {
"@fuel-ts/forc": "workspace:*"
Expand Down
2 changes: 1 addition & 1 deletion packages/contract/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { FuelError } from '@fuel-ts/errors';
import { sha256 } from '@fuel-ts/hasher';
import type { BytesLike } from '@fuel-ts/interfaces';
import { calcRoot, SparseMerkleTree } from '@fuel-ts/merkle';
import type { StorageSlot } from '@fuel-ts/transactions';
import { chunkAndPadBytes, hexlify, concat, arrayify } from '@fuel-ts/utils';
import { sha256 } from 'ethers';

/**
* @hidden
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"dependencies": {
"@fuel-ts/errors": "workspace:*",
"@fuel-ts/utils": "workspace:^",
"ethereum-cryptography": "^2.1.2",
"@noble/hashes": "^1.3.3",
"ethers": "^6.7.1"
}
}
1 change: 0 additions & 1 deletion packages/crypto/src/browser/aes-ctr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const keyFromPassword: CryptoApi['keyFromPassword'] = (
): Uint8Array => {
const passBuffer = bufferFromString(String(password).normalize('NFKC'), 'utf-8');
const key = pbkdf2(passBuffer, saltBuffer, 100000, 32, 'sha256');

return arrayify(key);
};

Expand Down
4 changes: 2 additions & 2 deletions packages/crypto/src/shared/keccak256.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { keccak256 as keccak } from './keccak256';

const data = bufferFromString('hashedKey');

vi.mock('ethereum-cryptography/keccak', () => ({
keccak256: vi.fn(() => data),
vi.mock('@noble/hashes/sha3', () => ({
keccak_256: vi.fn(() => data),
}));

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/crypto/src/shared/keccak256.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { keccak256 as keccak } from 'ethereum-cryptography/keccak';
import { keccak_256 } from '@noble/hashes/sha3';

export const keccak256 = (data: Uint8Array): Uint8Array => keccak(data);
export const keccak256 = (data: Uint8Array): Uint8Array => keccak_256(data);
4 changes: 2 additions & 2 deletions packages/crypto/src/shared/scrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { scrypt } from './scrypt';

const data = bufferFromString('hashedKey');

vi.mock('ethereum-cryptography/scrypt', () => ({
scryptSync: vi.fn(() => data),
vi.mock('@noble/hashes/scrypt', () => ({
scrypt: vi.fn(() => data),
}));

/**
Expand Down
5 changes: 2 additions & 3 deletions packages/crypto/src/shared/scrypt.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { scryptSync as ethCryScrypt } from 'ethereum-cryptography/scrypt';
import { scrypt as scryptNoble } from '@noble/hashes/scrypt';

import type { IScryptParams } from '../types';

export const scrypt = (params: IScryptParams): Uint8Array => {
const { password, salt, n, p, r, dklen } = params;

const derivedKey = ethCryScrypt(password, salt, n, r, p, dklen);
const derivedKey = scryptNoble(password, salt, { N: n, r, p, dkLen: dklen });

return derivedKey;
};
2 changes: 1 addition & 1 deletion packages/hasher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@fuel-ts/interfaces": "workspace:^",
"@fuel-ts/math": "workspace:*",
"@fuel-ts/utils": "workspace:*",
"ethers": "^6.7.1",
"@noble/hashes": "^1.3.3",
"ramda": "^0.29.0"
},
"devDependencies": {
Expand Down
11 changes: 10 additions & 1 deletion packages/hasher/src/hasher.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { hashMessage, hash, uint64ToBytesBE } from './hasher';
import { bufferFromString } from '@fuel-ts/crypto';

import { hashMessage, hash, uint64ToBytesBE, sha256 } from './hasher';

/**
* @group node
* @group browser
*/
describe('Hasher', () => {
it('Hash "hello world"', () => {
const bytes = bufferFromString('hello world', 'utf-8');
expect(sha256(bytes)).toEqual(
'0xb94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'
);
});

it('Hash message', () => {
const message = 'my message';
const hashedMessage = '0xea38e30f75767d7e6c21eba85b14016646a3b60ade426ca966dac940a5db1bab';
Expand Down
31 changes: 20 additions & 11 deletions packages/hasher/src/hasher.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { bufferFromString } from '@fuel-ts/crypto';
import type { BytesLike } from '@fuel-ts/interfaces';
import { sha256 } from 'ethers';
import { arrayify, hexlify } from '@fuel-ts/utils';
import { sha256 as sha256AsBytes } from '@noble/hashes/sha256';

/**
* hash string messages with sha256
* @param data - The data to be hashed
* @returns A sha256 hash of the data in hex format
*/
export function sha256(data: BytesLike): string {
return hexlify(sha256AsBytes(arrayify(data)));
}

/**
* wrap sha256
*
* @param msg - The string message to be hashed
* @returns A sha256 hash of the message
* @param data - The data to be hash
* @returns A sha256 hash of the data
*/
export function hashMessage(msg: string) {
return sha256(bufferFromString(msg, 'utf-8'));
export function hash(data: BytesLike): string {
return sha256(data);
}

/**
Expand All @@ -24,11 +33,11 @@ export function uint64ToBytesBE(value: number): Uint8Array {
}

/**
* wrap sha256
* hash string messages with sha256
*
* @param data - The data to be hash
* @returns A sha256 hash of the data
* @param msg - The string message to be hashed
* @returns A sha256 hash of the message
*/
export function hash(data: BytesLike) {
return sha256(data);
export function hashMessage(msg: string) {
return hash(bufferFromString(msg, 'utf-8'));
}
4 changes: 2 additions & 2 deletions packages/merkle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
},
"license": "Apache-2.0",
"dependencies": {
"@fuel-ts/hasher": "workspace:^",
"@fuel-ts/interfaces": "workspace:^",
"@fuel-ts/math": "workspace:*",
"ethers": "^6.7.1"
"@fuel-ts/math": "workspace:*"
}
}
4 changes: 3 additions & 1 deletion packages/merkle/src/binary/binaryMerkleTree.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// A set of useful helper methods for testing binary Merkle trees.
import { EMPTY, hash } from '../common';
import { hash } from '@fuel-ts/hasher';

import { EMPTY } from '../common';

import Node from './types/node';

Expand Down
10 changes: 0 additions & 10 deletions packages/merkle/src/common/cryptography.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/merkle/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './common';
export * from './cryptography';
2 changes: 1 addition & 1 deletion packages/merkle/src/sparse/proofs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hash } from '../common';
import { hash } from '@fuel-ts/hasher';

import { hashLeaf, hashNode, parseLeaf } from './treeHasher';
import SparseCompactMerkleProof from './types/sparseCompactMerkleProof';
Expand Down
4 changes: 2 additions & 2 deletions packages/merkle/src/sparse/sparseMerkleTree.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { hash } from '@fuel-ts/hasher';
import { toHex } from '@fuel-ts/math';

import { hash } from '../common';

import { DeepSparseMerkleSubTree } from './deepSparseMerkleSubTree';
import { SparseMerkleTree } from './sparseMerkleTree';

/**
* @group node
* @group browser
*/
describe('Sparse Merkle Tree', () => {
it('Update and delete', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/merkle/src/sparse/sparseMerkleTree.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// @dev The Fuel testing Merkle trees.
/// A set of useful helper methods for testing and deploying Merkle trees.
import { hash } from '../common';
import { hash } from '@fuel-ts/hasher';

import { compactProof } from './proofs';
import { isLeaf, hashLeaf, hashNode, parseLeaf, parseNode } from './treeHasher';
Expand Down
2 changes: 1 addition & 1 deletion packages/merkle/src/sparse/treeHasher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hash } from '../common';
import { hash } from '@fuel-ts/hasher';

export const leafPrefix = '0x00';
export const nodePrefix = '0x01';
Expand Down
3 changes: 1 addition & 2 deletions packages/merkle/src/sum/sumMerkleTree.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/// @dev The Fuel testing Merkle trees.
/// A set of useful helper methods for testing and deploying Merkle trees.
import { hash } from '@fuel-ts/hasher';
import { bn, toHex } from '@fuel-ts/math';

import { hash } from '../common';

import Node from './types/node';
import Proof from './types/proof';

Expand Down
4 changes: 2 additions & 2 deletions packages/transactions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
"@fuel-ts/abi-coder": "workspace:*",
"@fuel-ts/address": "workspace:*",
"@fuel-ts/errors": "workspace:*",
"@fuel-ts/hasher": "workspace:^",
"@fuel-ts/interfaces": "workspace:^",
"@fuel-ts/math": "workspace:*",
"@fuel-ts/utils": "workspace:*",
"ethers": "^6.7.1"
"@fuel-ts/utils": "workspace:*"
}
}
2 changes: 1 addition & 1 deletion packages/transactions/src/coders/input.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable max-classes-per-file */
import { Coder, B256Coder, NumberCoder, BigNumberCoder } from '@fuel-ts/abi-coder';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { sha256 } from '@fuel-ts/hasher';
import type { BytesLike } from '@fuel-ts/interfaces';
import type { BN } from '@fuel-ts/math';
import { concat, arrayify } from '@fuel-ts/utils';
import { sha256 } from 'ethers';

import { ByteArrayCoder } from './byte-array';
import type { TxPointer } from './tx-pointer';
Expand Down
Loading
Loading