From 642b19bd1b14c5f683df6a18e12bf71d5a86eb89 Mon Sep 17 00:00:00 2001 From: Abhijit Roy Date: Mon, 10 Jun 2024 13:43:10 +0530 Subject: [PATCH] Add blake2_256 function and its tests to auto-utils --- packages/auto-utils/__test__/crypto.test.ts | 16 +++++++++++ packages/auto-utils/src/crypto.ts | 31 +++++++++++++++++++++ packages/auto-utils/src/index.ts | 1 + 3 files changed, 48 insertions(+) create mode 100644 packages/auto-utils/__test__/crypto.test.ts create mode 100644 packages/auto-utils/src/crypto.ts diff --git a/packages/auto-utils/__test__/crypto.test.ts b/packages/auto-utils/__test__/crypto.test.ts new file mode 100644 index 00000000..8fff6ba9 --- /dev/null +++ b/packages/auto-utils/__test__/crypto.test.ts @@ -0,0 +1,16 @@ +import { blake2b_256, stringToUint8Array } from '../src/crypto' + +describe('Verify crypto functions', () => { + test('Check blake2b_256 return the hash of the data', async () => { + const message = 'Alice transfer money to Bob' + const data = stringToUint8Array(message) + const hash = blake2b_256(data) + expect(hash).toMatch('0x') + }) + + test('Check stringToUint8Array return the byte array of the string', async () => { + const message = 'Hello, world!' + const byteArray = stringToUint8Array(message) + expect(byteArray).toBeInstanceOf(Uint8Array) + }) +}) diff --git a/packages/auto-utils/src/crypto.ts b/packages/auto-utils/src/crypto.ts new file mode 100644 index 00000000..1dce5c74 --- /dev/null +++ b/packages/auto-utils/src/crypto.ts @@ -0,0 +1,31 @@ +import { blake2AsHex } from '@polkadot/util-crypto' + +/** + * Hashes the given data using BLAKE2b-256. + * + * @param data Uint8Array - The data to be hashed. + * @returns string - The BLAKE2b-256 hash of the data as a hex string. + */ +export function blake2b_256(data: Uint8Array): string { + return blake2AsHex(data, 256) +} + +/** + * Converts a string to a Uint8Array using UTF-8 encoding. + * + * This function uses the TextEncoder API to convert a plain string into its equivalent byte array + * representation in UTF-8 format. It is useful for scenarios where string data needs to be processed + * in a binary format, such as hashing or cryptographic operations. + * + * @param text The string to be converted into a byte array. + * @returns Uint8Array - The UTF-8 encoded byte array representation of the input string. + * + * @example + * const text = "Hello, world!"; + * const byteArray = stringToUint8Array(text); + * console.log(byteArray); // Outputs the byte array of the string + */ +export function stringToUint8Array(text: string): Uint8Array { + const encoder = new TextEncoder() // Create a new TextEncoder instance + return encoder.encode(text) // Encode the string to a Uint8Array using UTF-8 encoding +} diff --git a/packages/auto-utils/src/index.ts b/packages/auto-utils/src/index.ts index d38eecfc..520949a3 100644 --- a/packages/auto-utils/src/index.ts +++ b/packages/auto-utils/src/index.ts @@ -1,4 +1,5 @@ export * from './api' +export * from './crypto' export * from './network' export * from './read' export * from './save'