From 2b19df73366c6e97f4a2c4173cb9f9cb78c350ba Mon Sep 17 00:00:00 2001 From: getCryptoAddress <125769450+getCryptoAddress@users.noreply.github.com> Date: Thu, 7 Nov 2024 18:56:53 +0200 Subject: [PATCH] Add test for Ethereum wallet --- .../walletsEthereum/WalletsEthereum.test.ts | 55 +++++++++++++++++++ .../WalletsEthereum.test.ts.snap | 5 ++ 2 files changed, 60 insertions(+) create mode 100644 src/entities/CryptoWallets/lib/Wallets/walletsEthereum/WalletsEthereum.test.ts create mode 100644 src/entities/CryptoWallets/lib/Wallets/walletsEthereum/__snapshots__/WalletsEthereum.test.ts.snap diff --git a/src/entities/CryptoWallets/lib/Wallets/walletsEthereum/WalletsEthereum.test.ts b/src/entities/CryptoWallets/lib/Wallets/walletsEthereum/WalletsEthereum.test.ts new file mode 100644 index 0000000..6d6789b --- /dev/null +++ b/src/entities/CryptoWallets/lib/Wallets/walletsEthereum/WalletsEthereum.test.ts @@ -0,0 +1,55 @@ +import { expect, suite, test } from "vitest"; +import WalletsEthereum from "./WalletsEthereum"; + +const key1 = new Uint8Array([ + 132, 205, 117, 44, 180, 113, 245, 200, 213, 19, 97, 187, 70, 157, 86, 111, 19, + 131, 14, 18, 196, 57, 9, 203, 64, 196, 19, 26, 73, 35, 126, 80, +]); +const key2 = new Uint8Array([ + 133, 205, 117, 44, 180, 113, 245, 200, 213, 19, 97, 187, 70, 157, 86, 111, 19, + 131, 14, 18, 196, 57, 9, 203, 64, 196, 19, 26, 73, 35, 126, 80, +]); + +const notValidKey1 = new Uint8Array([ + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 19, 131, 14, 18, 196, 57, 9, 203, 64, 196, 19, 26, 73, 35, 126, 80, +]); + +const notValidKey2 = new Uint8Array([ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, +]); + +suite("WalletsEthereum", () => { + test("should generate wallet correctly", async () => { + const walletGenerator = new WalletsEthereum(); + const wallet1 = await walletGenerator.makeWallet(null, key1); + expect(JSON.stringify(wallet1)).toMatchSnapshot(); + + const wallet2 = await walletGenerator.makeWallet(null, key2); + expect(JSON.stringify(wallet2)).toMatchSnapshot(); + }); + + test("should generate wallet correctly without initialPrivateKey", async () => { + const walletGenerator = new WalletsEthereum(); + + const wallet = await walletGenerator.makeWallet(null); + + expect(typeof wallet.address).toBe("string"); + expect(wallet.address.length).not.toBe(0); + expect(typeof wallet.privateKey).toBe("string"); + expect(wallet.privateKey.length).not.toBe(0); + }); + + test("should throw error for invalid initialPrivateKey", async () => { + const walletGenerator = new WalletsEthereum(); + + await expect( + walletGenerator.makeWallet(null, notValidKey1), + ).rejects.toThrow("Invalid private key"); + await expect( + walletGenerator.makeWallet(null, notValidKey2), + ).rejects.toThrow("offset is out of bounds"); + }); +}); diff --git a/src/entities/CryptoWallets/lib/Wallets/walletsEthereum/__snapshots__/WalletsEthereum.test.ts.snap b/src/entities/CryptoWallets/lib/Wallets/walletsEthereum/__snapshots__/WalletsEthereum.test.ts.snap new file mode 100644 index 0000000..dbd5f29 --- /dev/null +++ b/src/entities/CryptoWallets/lib/Wallets/walletsEthereum/__snapshots__/WalletsEthereum.test.ts.snap @@ -0,0 +1,5 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`WalletsEthereum > should generate wallet correctly 1`] = `"{"privateKey":"0x84cd752cb471f5c8d51361bb469d566f13830e12c43909cb40c4131a49237e50","address":"0x4a1bab2023db29cc924e475ceffafb81645f1ab5"}"`; + +exports[`WalletsEthereum > should generate wallet correctly 2`] = `"{"privateKey":"0x85cd752cb471f5c8d51361bb469d566f13830e12c43909cb40c4131a49237e50","address":"0xd715914e3592c611d0614e4ad53bed7a09755326"}"`;