Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/__tests__/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ jest.mock("inquirer");

const promptMock = mocked(prompt);

// Note: This is a dummy password used only for testing mock wallet encryption/decryption.
const mockedPassword = "password123";

describe("wallet", () => {
// increase timeout because ethers is throttling
jest.setTimeout(30000);
Expand All @@ -29,7 +32,7 @@ describe("wallet", () => {
it("should work when user consent correctly", async () => {
const signaleSuccessSpy = jest.spyOn(signale, "success");
promptMock.mockResolvedValueOnce({ ack: "yes" }); // what the user type
promptMock.mockResolvedValueOnce({ password: "password123" }); // wallet password
promptMock.mockResolvedValueOnce({ password: mockedPassword }); // wallet password
await decrypt({
inputFile: path.resolve("src", "implementations", "utils", "__tests__", "wallet.json").toString(),
yes: false,
Expand Down Expand Up @@ -59,7 +62,7 @@ describe("wallet", () => {
});
it("should work when user consent automatically", async () => {
const signaleSuccessSpy = jest.spyOn(signale, "success");
promptMock.mockResolvedValueOnce({ password: "password123" }); // wallet password
promptMock.mockResolvedValueOnce({ password: mockedPassword }); // wallet password
await decrypt({
inputFile: path.resolve("src", "implementations", "utils", "__tests__", "wallet.json").toString(),
yes: true,
Expand Down
8 changes: 6 additions & 2 deletions src/implementations/utils/__tests__/wallet-astron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const promptMock: jest.Mock = prompt;
const privateKey = "0xcd27dc84c82c5814e7edac518edd5f263e7db7f25adb7a1afe13996a95583cf2";
const walletAddress = "0xB26B4941941C51a4885E5B7D3A1B861E54405f90";

// Note: This is a dummy password used only for testing mock wallet encryption/decryption.
const mockedPassword = "password123";
const mockedInvalidPassword = "invalid";

describe("wallet", () => {
// increase timeout because ethers is throttling
jest.setTimeout(30_000);
Expand Down Expand Up @@ -48,7 +52,7 @@ describe("wallet", () => {
expect(wallet.privateKey).toStrictEqual(privateKey);
});
it("should return the wallet when providing an encrypted wallet", async () => {
promptMock.mockReturnValue({ password: "password123" });
promptMock.mockReturnValue({ password: mockedPassword });

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

await expect(
getWalletOrSigner({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const promptMock: jest.Mock = prompt;
const privateKey = "0xcd27dc84c82c5814e7edac518edd5f263e7db7f25adb7a1afe13996a95583cf2";
const walletAddress = "0xB26B4941941C51a4885E5B7D3A1B861E54405f90";

// Note: This is a dummy password used only for testing mock wallet encryption/decryption.
const mockedPassword = "password123";
const mockedInvalidPassword = "invalid";

describe("wallet", () => {
// increase timeout because ethers is throttling
jest.setTimeout(30_000);
Expand Down Expand Up @@ -51,7 +55,7 @@ describe("wallet", () => {
expect(wallet.privateKey).toStrictEqual(privateKey);
});
it("should return the wallet when providing an encrypted wallet", async () => {
promptMock.mockReturnValue({ password: "password123" });
promptMock.mockReturnValue({ password: mockedPassword });

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

await expect(
getWalletOrSigner({
Expand Down
9 changes: 6 additions & 3 deletions src/implementations/utils/__tests__/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ jest.mock("ethers", () => ({
const promptMock: jest.Mock = prompt;
const getSupportedNetworkMock = getSupportedNetwork as jest.MockedFunction<typeof getSupportedNetwork>;

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

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

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

await expect(
getWalletOrSigner({
Expand Down Expand Up @@ -173,7 +176,7 @@ describe("wallet", () => {
});

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

const wallet = await getWalletOrSigner({
network: "sepolia",
Expand Down
8 changes: 5 additions & 3 deletions src/implementations/wallet/__tests__/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import tmp from "tmp";
import fs from "fs";
import { ethers } from "ethers";
jest.mock("inquirer");
const password = "password123";

// Note: This is a dummy password used only for testing mock wallet encryption/decryption.
const mockedPassword = "password123";

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

const decryptedWallet = await ethers.Wallet.fromEncryptedJson(walletAsString, password);
const decryptedWallet = await ethers.Wallet.fromEncryptedJson(walletAsString, mockedPassword);
expect(decryptedWallet.address).toStrictEqual(expect.any(String));
expect(decryptedWallet.privateKey).toStrictEqual(expect.any(String));
});
Expand Down
9 changes: 5 additions & 4 deletions src/implementations/wallet/__tests__/encrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ jest.mock("inquirer");
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const promptMock: jest.Mock = prompt;
// Note: This is a dummy password used only for testing mock wallet encryption/decryption.
const mockedPassword = "password123";
const privateKey = "0xcd27dc84c82c5814e7edac518edd5f263e7db7f25adb7a1afe13996a95583cf2";
const password = "password123";

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

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

const decryptedWallet = await ethers.Wallet.fromEncryptedJson(walletAsString, password);
const decryptedWallet = await ethers.Wallet.fromEncryptedJson(walletAsString, mockedPassword);
expect(decryptedWallet.address).toBe("0xB26B4941941C51a4885E5B7D3A1B861E54405f90");
expect(decryptedWallet.privateKey).toStrictEqual(privateKey);
});
Expand Down