diff --git a/.changeset/wise-deers-press.md b/.changeset/wise-deers-press.md new file mode 100644 index 00000000000..d3d6657a5ba --- /dev/null +++ b/.changeset/wise-deers-press.md @@ -0,0 +1,6 @@ +--- +"@fuel-ts/crypto": patch +"@fuel-ts/errors": patch +--- + +chore: removed redundant crypto functionality diff --git a/apps/docs/src/guide/errors/index.md b/apps/docs/src/guide/errors/index.md index 927962f80d4..5e7e7783aa8 100644 --- a/apps/docs/src/guide/errors/index.md +++ b/apps/docs/src/guide/errors/index.md @@ -306,11 +306,6 @@ When the workspace is not detected in the directory indicated in the message. Ensure that the workspace is present in the directory specified. -### `HASHER_LOCKED` - -The hashing algorithm is currently locked, any subsequent attempts to register a new implementation will throw this error. -The purpose of the lock function is to provide a way to ensure that the implementation of the specific hashing algorithm cannot be changed once it is locked. This can be useful in scenarios where you want to guarantee the integrity and consistency of the hashing function throughout your application. - ### `UNKNOWN` In cases where the error hasn't been mapped yet, this code will be used. diff --git a/packages/crypto/src/node/hmac.ts b/packages/crypto/src/node/hmac.ts index 17280f28b79..7f7e9b542e1 100644 --- a/packages/crypto/src/node/hmac.ts +++ b/packages/crypto/src/node/hmac.ts @@ -1,18 +1,6 @@ -import { FuelError, ErrorCode } from '@fuel-ts/errors'; -import type { BytesLike } from '@fuel-ts/interfaces'; import { arrayify, hexlify } from '@fuel-ts/utils'; import { createHmac } from 'crypto'; -let locked = false; - -const COMPUTEHMAC = ( - algorithm: 'sha256' | 'sha512', - key: Uint8Array, - data: Uint8Array -): BytesLike => createHmac(algorithm, key).update(data).digest(); - -let computeHMAC = COMPUTEHMAC; - export function computeHmac( algorithm: 'sha256' | 'sha512', _key: Uint8Array, @@ -20,18 +8,5 @@ export function computeHmac( ): string { const key = arrayify(_key, 'key'); const data = arrayify(_data, 'data'); - return hexlify(computeHMAC(algorithm, key, data)); + return hexlify(createHmac(algorithm, key).update(data).digest()); } -computeHmac._ = COMPUTEHMAC; -computeHmac.lock = () => { - locked = true; -}; -computeHmac.register = ( - func: (algorithm: 'sha256' | 'sha512', key: Uint8Array, data: Uint8Array) => BytesLike -) => { - if (locked) { - throw new FuelError(ErrorCode.HASHER_LOCKED, 'computeHmac is locked'); - } - computeHMAC = func; -}; -Object.freeze(computeHmac); diff --git a/packages/crypto/src/node/pbkdf2.ts b/packages/crypto/src/node/pbkdf2.ts index 86e65ef47db..cca0e4b83a1 100644 --- a/packages/crypto/src/node/pbkdf2.ts +++ b/packages/crypto/src/node/pbkdf2.ts @@ -1,20 +1,7 @@ -import { ErrorCode, FuelError } from '@fuel-ts/errors'; import type { BytesLike } from '@fuel-ts/interfaces'; import { arrayify, hexlify } from '@fuel-ts/utils'; import { pbkdf2Sync } from 'crypto'; -let locked = false; - -const PBKDF2 = ( - password: Uint8Array, - salt: Uint8Array, - iterations: number, - keylen: number, - algo: 'sha256' | 'sha512' -): BytesLike => pbkdf2Sync(password, salt, iterations, keylen, algo); - -let pBkdf2 = PBKDF2; - export function pbkdf2( _password: BytesLike, _salt: BytesLike, @@ -24,24 +11,5 @@ export function pbkdf2( ): string { const password = arrayify(_password, 'password'); const salt = arrayify(_salt, 'salt'); - return hexlify(pBkdf2(password, salt, iterations, keylen, algo)); + return hexlify(pbkdf2Sync(password, salt, iterations, keylen, algo)); } -pbkdf2._ = PBKDF2; -pbkdf2.lock = (): void => { - locked = true; -}; -pbkdf2.register = ( - func: ( - password: Uint8Array, - salt: Uint8Array, - iterations: number, - keylen: number, - algo: 'sha256' | 'sha512' - ) => BytesLike -) => { - if (locked) { - throw new FuelError(ErrorCode.HASHER_LOCKED, 'pbkdf2 is locked'); - } - pBkdf2 = func; -}; -Object.freeze(pbkdf2); diff --git a/packages/crypto/src/shared/ripemd160.ts b/packages/crypto/src/shared/ripemd160.ts index 2e94855e23a..116206561e8 100644 --- a/packages/crypto/src/shared/ripemd160.ts +++ b/packages/crypto/src/shared/ripemd160.ts @@ -1,26 +1,8 @@ -import { ErrorCode, FuelError } from '@fuel-ts/errors'; import type { BytesLike } from '@fuel-ts/interfaces'; import { arrayify } from '@fuel-ts/utils'; import { ripemd160 as noble_ripemd160 } from '@noble/hashes/ripemd160'; -let locked = false; - -const helper = (data: Uint8Array): Uint8Array => noble_ripemd160(data); - -let ripemd: (data: Uint8Array) => Uint8Array = helper; - export function ripemd160(_data: BytesLike): Uint8Array { const data = arrayify(_data, 'data'); - return ripemd(data); + return noble_ripemd160(data); } -ripemd160._ = helper; -ripemd160.lock = (): void => { - locked = true; -}; -ripemd160.register = (func: (data: Uint8Array) => Uint8Array) => { - if (locked) { - throw new FuelError(ErrorCode.HASHER_LOCKED, 'ripemd160 is locked'); - } - ripemd = func; -}; -Object.freeze(ripemd160); diff --git a/packages/crypto/test/hmac.node.test.ts b/packages/crypto/test/hmac.node.test.ts deleted file mode 100644 index 15dde359f4b..00000000000 --- a/packages/crypto/test/hmac.node.test.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { arrayify } from '@fuel-ts/utils'; - -import { computeHmac } from '..'; -/** - * @group node - */ -describe('computeHmac node', () => { - it('should use the registered HMAC function', () => { - const key = '0x0102030405060708090a0b0c0d0e0f10'; - const data = '0x11121314151617181920212223242526'; - const expectedHmac = '0x1234567890abcdef'; - - computeHmac.register((algorithm, _key, _data) => { - expect(algorithm).toBe('sha256'); - expect(arrayify(_key)).toEqual(arrayify(key)); - expect(arrayify(_data)).toEqual(arrayify(data)); - return arrayify(expectedHmac); - }); - - expect(computeHmac('sha256', key, data)).toBe(expectedHmac); - }); - - it('should lock the computeHmac function', () => { - computeHmac.lock(); - expect(() => computeHmac.register((_, __, ___) => arrayify('0x1234'))).toThrowError( - 'computeHmac is locked' - ); - }); -}); diff --git a/packages/crypto/test/pbkdf2.node.test.ts b/packages/crypto/test/pbkdf2.node.test.ts deleted file mode 100644 index 2e95fe2f5e8..00000000000 --- a/packages/crypto/test/pbkdf2.node.test.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { bufferFromString, pbkdf2 } from '..'; - -/** - * @group node - */ -describe('pbkdf2 node', () => { - it('should use the registered function for PBKDF2 computation', () => { - const expectedResult = '0x90eceedd899d5cdcdfd9b315ad6e2c3391bf95cc131b6f0f016339db5ee60494'; - const passwordBuffer = bufferFromString(String('password123').normalize('NFKC'), 'utf-8'); - const saltBuffer = bufferFromString(String('salt456').normalize('NFKC'), 'utf-8'); - const iterations = 1000; - const keylen = 32; - const algo = 'sha256'; - - pbkdf2.register((password, salt, iter, kl, al) => { - expect(password).toBeInstanceOf(Uint8Array); - expect(salt).toBeInstanceOf(Uint8Array); - expect(iter).toBe(1000); - expect(keylen).toBe(32); - expect(al).toBe('sha256'); - return expectedResult; - }); - - expect(pbkdf2(passwordBuffer, saltBuffer, iterations, keylen, algo)).toBe(expectedResult); - }); - - it('should throw an error when registering a function after locking', () => { - pbkdf2.lock(); - - expect(() => { - pbkdf2.register(() => {}); - }).toThrowError('pbkdf2 is locked'); - }); - - it('should have a frozen object', () => { - expect(Object.isFrozen(pbkdf2)).toBe(true); - }); -}); diff --git a/packages/errors/src/error-codes.ts b/packages/errors/src/error-codes.ts index 8c3938ce463..24da1de11a5 100644 --- a/packages/errors/src/error-codes.ts +++ b/packages/errors/src/error-codes.ts @@ -60,6 +60,8 @@ export enum ErrorCode { // crypto INVALID_CREDENTIALS = 'invalid-credentials', + + /** @deprecated This error code is no longer used */ HASHER_LOCKED = 'hasher-locked', // transaction