Skip to content

Commit 41bc468

Browse files
Add test for Ethereum wallet (#31)
1 parent 98a4fcc commit 41bc468

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { expect, suite, test } from "vitest";
2+
import WalletsEthereum from "./WalletsEthereum";
3+
4+
const key1 = new Uint8Array([
5+
132, 205, 117, 44, 180, 113, 245, 200, 213, 19, 97, 187, 70, 157, 86, 111, 19,
6+
131, 14, 18, 196, 57, 9, 203, 64, 196, 19, 26, 73, 35, 126, 80,
7+
]);
8+
const key2 = new Uint8Array([
9+
133, 205, 117, 44, 180, 113, 245, 200, 213, 19, 97, 187, 70, 157, 86, 111, 19,
10+
131, 14, 18, 196, 57, 9, 203, 64, 196, 19, 26, 73, 35, 126, 80,
11+
]);
12+
13+
const notValidKey1 = new Uint8Array([
14+
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
15+
255, 19, 131, 14, 18, 196, 57, 9, 203, 64, 196, 19, 26, 73, 35, 126, 80,
16+
]);
17+
18+
const notValidKey2 = new Uint8Array([
19+
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,
20+
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,
21+
1, 1, 1, 1, 1, 1, 1,
22+
]);
23+
24+
suite("WalletsEthereum", () => {
25+
test("should generate wallet correctly", async () => {
26+
const walletGenerator = new WalletsEthereum();
27+
const wallet1 = await walletGenerator.makeWallet(null, key1);
28+
expect(JSON.stringify(wallet1)).toMatchSnapshot();
29+
30+
const wallet2 = await walletGenerator.makeWallet(null, key2);
31+
expect(JSON.stringify(wallet2)).toMatchSnapshot();
32+
});
33+
34+
test("should generate wallet correctly without initialPrivateKey", async () => {
35+
const walletGenerator = new WalletsEthereum();
36+
37+
const wallet = await walletGenerator.makeWallet(null);
38+
39+
expect(typeof wallet.address).toBe("string");
40+
expect(wallet.address.length).not.toBe(0);
41+
expect(typeof wallet.privateKey).toBe("string");
42+
expect(wallet.privateKey.length).not.toBe(0);
43+
});
44+
45+
test("should throw error for invalid initialPrivateKey", async () => {
46+
const walletGenerator = new WalletsEthereum();
47+
48+
await expect(
49+
walletGenerator.makeWallet(null, notValidKey1),
50+
).rejects.toThrow("Invalid private key");
51+
await expect(
52+
walletGenerator.makeWallet(null, notValidKey2),
53+
).rejects.toThrow("offset is out of bounds");
54+
});
55+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`WalletsEthereum > should generate wallet correctly 1`] = `"{"privateKey":"0x84cd752cb471f5c8d51361bb469d566f13830e12c43909cb40c4131a49237e50","address":"0x4a1bab2023db29cc924e475ceffafb81645f1ab5"}"`;
4+
5+
exports[`WalletsEthereum > should generate wallet correctly 2`] = `"{"privateKey":"0x85cd752cb471f5c8d51361bb469d566f13830e12c43909cb40c4131a49237e50","address":"0xd715914e3592c611d0614e4ad53bed7a09755326"}"`;

0 commit comments

Comments
 (0)