genlayer 0.19.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +2 -4
- package/CHANGELOG.md +12 -0
- package/dist/index.js +110 -57
- package/docker-compose.yml +10 -27
- package/package.json +1 -1
- package/src/commands/general/index.ts +1 -1
- package/src/commands/general/init.ts +6 -1
- package/src/commands/keygen/create.ts +6 -5
- package/src/commands/keygen/index.ts +4 -4
- package/src/lib/actions/BaseAction.ts +108 -17
- package/src/lib/config/simulator.ts +1 -1
- package/src/lib/interfaces/ISimulatorService.ts +1 -0
- package/src/lib/interfaces/KeystoreData.ts +5 -0
- package/src/lib/services/simulator.ts +18 -0
- package/tests/actions/create.test.ts +15 -37
- package/tests/actions/init.test.ts +48 -4
- package/tests/commands/init.test.ts +10 -0
- package/tests/libs/baseAction.test.ts +229 -24
- package/tests/services/simulator.test.ts +60 -0
- package/src/lib/accounts/KeypairManager.ts +0 -43
- package/tests/libs/accounts/KeypairManager.test.ts +0 -110
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { describe, test, vi, beforeEach, afterEach, expect } from "vitest";
|
|
2
|
-
import { writeFileSync, existsSync, readFileSync } from "fs";
|
|
3
|
-
import { ethers } from "ethers";
|
|
4
|
-
import { KeypairManager } from "../../../src/lib/accounts/KeypairManager";
|
|
5
|
-
|
|
6
|
-
vi.mock("fs");
|
|
7
|
-
vi.mock("ethers");
|
|
8
|
-
|
|
9
|
-
describe("KeypairManager", () => {
|
|
10
|
-
let keypairManager: KeypairManager;
|
|
11
|
-
const mockPrivateKey = "0xMockedPrivateKey";
|
|
12
|
-
const mockAddress = "0xMockedAddress";
|
|
13
|
-
const mockKeypairPath = "/mocked/path/keypair.json";
|
|
14
|
-
const mockKeypairData = {
|
|
15
|
-
address: mockAddress,
|
|
16
|
-
privateKey: mockPrivateKey,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
beforeEach(() => {
|
|
20
|
-
vi.clearAllMocks();
|
|
21
|
-
keypairManager = new KeypairManager();
|
|
22
|
-
vi.spyOn(keypairManager as any, "getConfigByKey").mockReturnValue(mockKeypairPath);
|
|
23
|
-
vi.spyOn(keypairManager as any, "writeConfig").mockImplementation(() => {});
|
|
24
|
-
vi.spyOn(keypairManager as any, "getFilePath").mockReturnValue(mockKeypairPath);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
afterEach(() => {
|
|
28
|
-
vi.restoreAllMocks();
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
describe("getPrivateKey", () => {
|
|
32
|
-
test("should return private key when keypair file exists and contains private key", () => {
|
|
33
|
-
vi.mocked(existsSync).mockReturnValue(true);
|
|
34
|
-
vi.mocked(readFileSync).mockReturnValue(JSON.stringify(mockKeypairData));
|
|
35
|
-
|
|
36
|
-
const result = keypairManager.getPrivateKey();
|
|
37
|
-
|
|
38
|
-
expect(result).toBe(mockPrivateKey);
|
|
39
|
-
expect(existsSync).toHaveBeenCalledWith(mockKeypairPath);
|
|
40
|
-
expect(readFileSync).toHaveBeenCalledWith(mockKeypairPath, "utf-8");
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test("should return empty string when keypair file does not exist", () => {
|
|
44
|
-
vi.mocked(existsSync).mockReturnValue(false);
|
|
45
|
-
|
|
46
|
-
const result = keypairManager.getPrivateKey();
|
|
47
|
-
|
|
48
|
-
expect(result).toBe("");
|
|
49
|
-
expect(existsSync).toHaveBeenCalledWith(mockKeypairPath);
|
|
50
|
-
expect(readFileSync).not.toHaveBeenCalled();
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test("should return empty string when keypair file exists but has no private key", () => {
|
|
54
|
-
vi.mocked(existsSync).mockReturnValue(true);
|
|
55
|
-
vi.mocked(readFileSync).mockReturnValue(JSON.stringify({ address: mockAddress }));
|
|
56
|
-
|
|
57
|
-
const result = keypairManager.getPrivateKey();
|
|
58
|
-
|
|
59
|
-
expect(result).toBe("");
|
|
60
|
-
expect(existsSync).toHaveBeenCalledWith(mockKeypairPath);
|
|
61
|
-
expect(readFileSync).toHaveBeenCalledWith(mockKeypairPath, "utf-8");
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
describe("createKeypair", () => {
|
|
66
|
-
test("should create new keypair and save it to file", () => {
|
|
67
|
-
const mockWallet = {
|
|
68
|
-
address: mockAddress,
|
|
69
|
-
privateKey: mockPrivateKey,
|
|
70
|
-
};
|
|
71
|
-
vi.mocked(ethers.Wallet.createRandom).mockReturnValue(mockWallet as any);
|
|
72
|
-
vi.mocked(existsSync).mockReturnValue(false);
|
|
73
|
-
|
|
74
|
-
keypairManager.createKeypair();
|
|
75
|
-
|
|
76
|
-
expect(ethers.Wallet.createRandom).toHaveBeenCalled();
|
|
77
|
-
expect(writeFileSync).toHaveBeenCalledWith(
|
|
78
|
-
mockKeypairPath,
|
|
79
|
-
JSON.stringify(mockKeypairData, null, 2)
|
|
80
|
-
);
|
|
81
|
-
expect(keypairManager["writeConfig"]).toHaveBeenCalledWith("keyPairPath", mockKeypairPath);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test("should throw error when file exists and overwrite is false", () => {
|
|
85
|
-
vi.mocked(existsSync).mockReturnValue(true);
|
|
86
|
-
|
|
87
|
-
expect(() => keypairManager.createKeypair()).toThrow(
|
|
88
|
-
`The file at ${mockKeypairPath} already exists. Use the '--overwrite' option to replace it.`
|
|
89
|
-
);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test("should overwrite existing file when overwrite is true", () => {
|
|
93
|
-
const mockWallet = {
|
|
94
|
-
address: mockAddress,
|
|
95
|
-
privateKey: mockPrivateKey,
|
|
96
|
-
};
|
|
97
|
-
vi.mocked(ethers.Wallet.createRandom).mockReturnValue(mockWallet as any);
|
|
98
|
-
vi.mocked(existsSync).mockReturnValue(true);
|
|
99
|
-
|
|
100
|
-
keypairManager.createKeypair("./keypair.json", true);
|
|
101
|
-
|
|
102
|
-
expect(ethers.Wallet.createRandom).toHaveBeenCalled();
|
|
103
|
-
expect(writeFileSync).toHaveBeenCalledWith(
|
|
104
|
-
mockKeypairPath,
|
|
105
|
-
JSON.stringify(mockKeypairData, null, 2)
|
|
106
|
-
);
|
|
107
|
-
expect(keypairManager["writeConfig"]).toHaveBeenCalledWith("keyPairPath", mockKeypairPath);
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
});
|