Skip to content

Commit 6a1a7ca

Browse files
committed
refactor: split apart methods file
1 parent 9632d21 commit 6a1a7ca

File tree

10 files changed

+97
-105
lines changed

10 files changed

+97
-105
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const PLUGIN_ID = "hardhat-keystore";

v-next/hardhat-keystore/src/hook-handlers/configuration-variables.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
HookContext,
55
} from "@ignored/hardhat-vnext/types/hooks";
66

7-
import { get } from "../methods.js";
7+
import get from "../tasks/get.js";
88
import { getKeystore } from "../utils.js";
99

1010
export default async (): Promise<Partial<ConfigurationVariableHooks>> => {
@@ -20,7 +20,8 @@ export default async (): Promise<Partial<ConfigurationVariableHooks>> => {
2020
return next(context, variable);
2121
}
2222

23-
const value = await get(variable.name);
23+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- this is temporary as part of a refactor
24+
const value = await get({ key: variable.name }, null as any);
2425

2526
return value ?? next(context, variable);
2627
},

v-next/hardhat-keystore/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const hardhatKeystorePlugin: HardhatPlugin = {
1313
},
1414
tasks: [
1515
task("keystore", "Store your keys in a secure way")
16-
.setAction(async (_, _hre) => {})
16+
.setAction(async () => {})
1717
.build(),
1818

1919
task(

v-next/hardhat-keystore/src/methods.ts

-93
This file was deleted.
+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { NewTaskActionFunction } from "@ignored/hardhat-vnext/types/tasks";
22

3-
import { remove } from "../methods.js";
3+
import { isAuthorized } from "../password-manager.js";
4+
import { showMsgNoKeystoreSet } from "../utils/show-msg-no-keystore-set.js";
5+
import { getKeystore, removeKey } from "../utils.js";
46

57
interface TaskDeleteArguments {
68
key: string;
@@ -9,7 +11,17 @@ interface TaskDeleteArguments {
911
const taskDelete: NewTaskActionFunction<TaskDeleteArguments> = async ({
1012
key,
1113
}) => {
12-
await remove(key);
14+
const keystore = await getKeystore();
15+
16+
if (keystore === undefined) {
17+
return showMsgNoKeystoreSet();
18+
}
19+
20+
if ((await isAuthorized()) === false) {
21+
return;
22+
}
23+
24+
await removeKey(key);
1325
};
1426

1527
export default taskDelete;
+24-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
import type { NewTaskActionFunction } from "@ignored/hardhat-vnext/types/tasks";
22

3-
import { get } from "../methods.js";
3+
import { io } from "../io.js";
4+
import { isAuthorized } from "../password-manager.js";
5+
import { showMsgNoKeystoreSet } from "../utils/show-msg-no-keystore-set.js";
6+
import { getKeystore } from "../utils.js";
47

58
interface TaskGetArguments {
69
key: string;
710
}
811

912
const taskGet: NewTaskActionFunction<TaskGetArguments> = async ({ key }) => {
10-
await get(key);
13+
const keystore = await getKeystore();
14+
15+
if (keystore === undefined) {
16+
showMsgNoKeystoreSet();
17+
return;
18+
}
19+
20+
if ((await isAuthorized()) === false) {
21+
return;
22+
}
23+
24+
if (keystore.keys[key] === undefined) {
25+
console.log(keystore);
26+
io.error(`Key "${key}" not found`);
27+
return;
28+
}
29+
30+
io.info(keystore.keys[key]);
31+
32+
return keystore.keys[key];
1133
};
1234

1335
export default taskGet;
+19-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
import type { NewTaskActionFunction } from "@ignored/hardhat-vnext/types/tasks";
22

3-
import { list } from "../methods.js";
3+
import { io } from "../io.js";
4+
import { showMsgNoKeystoreSet } from "../utils/show-msg-no-keystore-set.js";
5+
import { getKeystore } from "../utils.js";
46

57
const taskList: NewTaskActionFunction = async () => {
6-
await list();
8+
const keystore = await getKeystore();
9+
10+
if (keystore === undefined) {
11+
return showMsgNoKeystoreSet();
12+
}
13+
14+
// No authorization needed, it only shows the keys, not the secret values
15+
if (Object.keys(keystore.keys).length === 0) {
16+
io.info("The keystore does not contain any keys.");
17+
return;
18+
}
19+
20+
io.info("Keys:");
21+
for (const key of Object.keys(keystore.keys)) {
22+
io.info(key);
23+
}
724
};
825

926
export default taskList;

v-next/hardhat-keystore/src/tasks/set.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type { NewTaskActionFunction } from "@ignored/hardhat-vnext/types/tasks";
22

3-
import { set } from "../methods.js";
3+
import { io } from "../io.js";
4+
import { isAuthorized } from "../password-manager.js";
5+
import {
6+
addNewSecret,
7+
getKeystore,
8+
setupKeystore,
9+
validateKey,
10+
} from "../utils.js";
411

512
interface TaskGetArguments {
613
key: string;
@@ -11,7 +18,23 @@ const taskSet: NewTaskActionFunction<TaskGetArguments> = async ({
1118
key,
1219
force,
1320
}) => {
14-
await set(key, force);
21+
const keystore = await getKeystore();
22+
23+
if (keystore === undefined) {
24+
await setupKeystore();
25+
}
26+
27+
if (!validateKey(key)) {
28+
return;
29+
}
30+
31+
if ((await isAuthorized()) === false) {
32+
return;
33+
}
34+
35+
await addNewSecret(key, force);
36+
37+
io.info(`Key "${key}" set`);
1538
};
1639

1740
export default taskSet;

v-next/hardhat-keystore/src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
import chalk from "chalk";
1313
import envPaths from "env-paths";
1414

15+
import { PLUGIN_ID } from "./constants.js";
1516
import { io } from "./io.js";
16-
import { PLUGIN_ID } from "./methods.js";
1717
import { setUpPassword } from "./password-manager.js";
1818

1919
let keystoreCache: Keystore | undefined;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import chalk from "chalk";
2+
3+
import { io } from "../io.js";
4+
5+
export function showMsgNoKeystoreSet(): void {
6+
io.info(
7+
`No keystore found. Please set one up using ${chalk.blue.italic("npx hardhat keystore set {key}")} `,
8+
);
9+
}

0 commit comments

Comments
 (0)