Skip to content
This repository was archived by the owner on Mar 13, 2026. It is now read-only.

Commit 8e68688

Browse files
feat: mock password variable update (#55)
Co-authored-by: moiz-sgtradex <moiz.shaikh@sgtradextech.com>
1 parent f70ab2b commit 8e68688

6 files changed

Lines changed: 33 additions & 16 deletions

File tree

src/__tests__/wallet.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ jest.mock("inquirer");
99

1010
const promptMock = mocked(prompt);
1111

12+
// Note: This is a dummy password used only for testing mock wallet encryption/decryption.
13+
const mockedPassword = "password123";
14+
1215
describe("wallet", () => {
1316
// increase timeout because ethers is throttling
1417
jest.setTimeout(30000);
@@ -29,7 +32,7 @@ describe("wallet", () => {
2932
it("should work when user consent correctly", async () => {
3033
const signaleSuccessSpy = jest.spyOn(signale, "success");
3134
promptMock.mockResolvedValueOnce({ ack: "yes" }); // what the user type
32-
promptMock.mockResolvedValueOnce({ password: "password123" }); // wallet password
35+
promptMock.mockResolvedValueOnce({ password: mockedPassword }); // wallet password
3336
await decrypt({
3437
inputFile: path.resolve("src", "implementations", "utils", "__tests__", "wallet.json").toString(),
3538
yes: false,
@@ -59,7 +62,7 @@ describe("wallet", () => {
5962
});
6063
it("should work when user consent automatically", async () => {
6164
const signaleSuccessSpy = jest.spyOn(signale, "success");
62-
promptMock.mockResolvedValueOnce({ password: "password123" }); // wallet password
65+
promptMock.mockResolvedValueOnce({ password: mockedPassword }); // wallet password
6366
await decrypt({
6467
inputFile: path.resolve("src", "implementations", "utils", "__tests__", "wallet.json").toString(),
6568
yes: true,

src/implementations/utils/__tests__/wallet-astron.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const promptMock: jest.Mock = prompt;
1111
const privateKey = "0xcd27dc84c82c5814e7edac518edd5f263e7db7f25adb7a1afe13996a95583cf2";
1212
const walletAddress = "0xB26B4941941C51a4885E5B7D3A1B861E54405f90";
1313

14+
// Note: This is a dummy password used only for testing mock wallet encryption/decryption.
15+
const mockedPassword = "password123";
16+
const mockedInvalidPassword = "invalid";
17+
1418
describe("wallet", () => {
1519
// increase timeout because ethers is throttling
1620
jest.setTimeout(30_000);
@@ -48,7 +52,7 @@ describe("wallet", () => {
4852
expect(wallet.privateKey).toStrictEqual(privateKey);
4953
});
5054
it("should return the wallet when providing an encrypted wallet", async () => {
51-
promptMock.mockReturnValue({ password: "password123" });
55+
promptMock.mockReturnValue({ password: mockedPassword });
5256

5357
const wallet = await getWalletOrSigner({
5458
network: "astron",
@@ -59,7 +63,7 @@ describe("wallet", () => {
5963
expect(wallet.privateKey).toStrictEqual(privateKey);
6064
});
6165
it("should throw an error when the wallet password is invalid", async () => {
62-
promptMock.mockReturnValue({ password: "invalid" });
66+
promptMock.mockReturnValue({ password: mockedInvalidPassword });
6367

6468
await expect(
6569
getWalletOrSigner({

src/implementations/utils/__tests__/wallet-astrontestnet.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const promptMock: jest.Mock = prompt;
1111
const privateKey = "0xcd27dc84c82c5814e7edac518edd5f263e7db7f25adb7a1afe13996a95583cf2";
1212
const walletAddress = "0xB26B4941941C51a4885E5B7D3A1B861E54405f90";
1313

14+
// Note: This is a dummy password used only for testing mock wallet encryption/decryption.
15+
const mockedPassword = "password123";
16+
const mockedInvalidPassword = "invalid";
17+
1418
describe("wallet", () => {
1519
// increase timeout because ethers is throttling
1620
jest.setTimeout(30_000);
@@ -51,7 +55,7 @@ describe("wallet", () => {
5155
expect(wallet.privateKey).toStrictEqual(privateKey);
5256
});
5357
it("should return the wallet when providing an encrypted wallet", async () => {
54-
promptMock.mockReturnValue({ password: "password123" });
58+
promptMock.mockReturnValue({ password: mockedPassword });
5559

5660
const wallet = await getWalletOrSigner({
5761
network: "astrontestnet",
@@ -62,7 +66,7 @@ describe("wallet", () => {
6266
expect(wallet.privateKey).toStrictEqual(privateKey);
6367
});
6468
it("should throw an error when the wallet password is invalid", async () => {
65-
promptMock.mockReturnValue({ password: "invalid" });
69+
promptMock.mockReturnValue({ password: mockedInvalidPassword });
6670

6771
await expect(
6872
getWalletOrSigner({

src/implementations/utils/__tests__/wallet.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ jest.mock("ethers", () => ({
1919
const promptMock: jest.Mock = prompt;
2020
const getSupportedNetworkMock = getSupportedNetwork as jest.MockedFunction<typeof getSupportedNetwork>;
2121

22+
// Note: This is a dummy password used only for testing mock wallet encryption/decryption.
23+
const mockedPassword = "password123";
24+
const mockedInvalidPassword = "invalid";
2225
const privateKey = "0xcd27dc84c82c5814e7edac518edd5f263e7db7f25adb7a1afe13996a95583cf2";
2326
const walletAddress = "0xB26B4941941C51a4885E5B7D3A1B861E54405f90";
2427

@@ -90,7 +93,7 @@ describe("wallet", () => {
9093
expect(wallet.privateKey).toStrictEqual(privateKey);
9194
});
9295
it("should return the wallet when providing an encrypted wallet", async () => {
93-
promptMock.mockReturnValue({ password: "password123" });
96+
promptMock.mockReturnValue({ password: mockedPassword });
9497

9598
const wallet = await getWalletOrSigner({
9699
network: "sepolia",
@@ -101,7 +104,7 @@ describe("wallet", () => {
101104
expect(wallet.privateKey).toStrictEqual(privateKey);
102105
});
103106
it("should throw an error when the wallet password is invalid", async () => {
104-
promptMock.mockReturnValue({ password: "invalid" });
107+
promptMock.mockReturnValue({ password: mockedInvalidPassword });
105108

106109
await expect(
107110
getWalletOrSigner({
@@ -173,7 +176,7 @@ describe("wallet", () => {
173176
});
174177

175178
it("should use custom RPC URL when provided with encrypted wallet", async () => {
176-
promptMock.mockReturnValue({ password: "password123" });
179+
promptMock.mockReturnValue({ password: mockedPassword });
177180

178181
const wallet = await getWalletOrSigner({
179182
network: "sepolia",

src/implementations/wallet/__tests__/create.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import tmp from "tmp";
44
import fs from "fs";
55
import { ethers } from "ethers";
66
jest.mock("inquirer");
7-
const password = "password123";
7+
8+
// Note: This is a dummy password used only for testing mock wallet encryption/decryption.
9+
const mockedPassword = "password123";
810

911
// assigning the mock so that we get correct typing
1012
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -18,7 +20,7 @@ describe("create wallet", () => {
1820
promptMock.mockRestore();
1921
});
2022
it("shoud save the wallet into the provided path", async () => {
21-
promptMock.mockReturnValue({ password });
23+
promptMock.mockReturnValue({ password: mockedPassword });
2224
const file = tmp.fileSync();
2325
await create({ outputFile: file.name, progress: () => void 0 });
2426
const walletAsString = fs.readFileSync(file.name, "utf-8");
@@ -45,7 +47,7 @@ describe("create wallet", () => {
4547
})
4648
);
4749

48-
const decryptedWallet = await ethers.Wallet.fromEncryptedJson(walletAsString, password);
50+
const decryptedWallet = await ethers.Wallet.fromEncryptedJson(walletAsString, mockedPassword);
4951
expect(decryptedWallet.address).toStrictEqual(expect.any(String));
5052
expect(decryptedWallet.privateKey).toStrictEqual(expect.any(String));
5153
});

src/implementations/wallet/__tests__/encrypt.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ jest.mock("inquirer");
99
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
1010
// @ts-ignore
1111
const promptMock: jest.Mock = prompt;
12+
// Note: This is a dummy password used only for testing mock wallet encryption/decryption.
13+
const mockedPassword = "password123";
1214
const privateKey = "0xcd27dc84c82c5814e7edac518edd5f263e7db7f25adb7a1afe13996a95583cf2";
13-
const password = "password123";
1415

1516
describe("create wallet", () => {
1617
// increase timeout because ethers is throttling
@@ -19,15 +20,15 @@ describe("create wallet", () => {
1920
promptMock.mockRestore();
2021
});
2122
it("shoud throw an error when no key is provided", async () => {
22-
promptMock.mockReturnValue({ password });
23+
promptMock.mockReturnValue({ password: mockedPassword });
2324
const file = tmp.fileSync();
2425
await expect(encrypt({ outputFile: file.name, progress: () => void 0 })).rejects.toStrictEqual(
2526
new Error("No private key found in OA_PRIVATE_KEY, key, key-file, please supply at least one")
2627
);
2728
});
2829

2930
it("shoud encrypt the wallet when the key is provided with the key option", async () => {
30-
promptMock.mockReturnValue({ password });
31+
promptMock.mockReturnValue({ password: mockedPassword });
3132
const file = tmp.fileSync();
3233
await encrypt({ key: privateKey, outputFile: file.name, progress: () => void 0 });
3334
const walletAsString = fs.readFileSync(file.name, "utf-8");
@@ -54,7 +55,7 @@ describe("create wallet", () => {
5455
})
5556
);
5657

57-
const decryptedWallet = await ethers.Wallet.fromEncryptedJson(walletAsString, password);
58+
const decryptedWallet = await ethers.Wallet.fromEncryptedJson(walletAsString, mockedPassword);
5859
expect(decryptedWallet.address).toBe("0xB26B4941941C51a4885E5B7D3A1B861E54405f90");
5960
expect(decryptedWallet.privateKey).toStrictEqual(privateKey);
6061
});

0 commit comments

Comments
 (0)