Skip to content

Commit

Permalink
add and list wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
yu23ki14 committed Sep 29, 2024
1 parent 052ef32 commit 5f66bb3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
5 changes: 4 additions & 1 deletion pkgs/cli/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
dist/
.env
.env

profiles.json
/**/profiles.json
26 changes: 16 additions & 10 deletions pkgs/cli/src/commands/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Command } from "commander";
import * as donenv from "dotenv";
import { getEthAddress, sendEth } from "../modules/viem";

donenv.config();
import { listProfiles, saveProfile } from "../services/wallet";

export const walletCommands = new Command();

Expand All @@ -14,17 +12,25 @@ walletCommands
.version("1.0.0");

/**
* ETHアドレスを取得
* ウォレット一覧
*/
walletCommands
.command("getEthAddress")
.description("show wallet address")
.command("list")
.description("show wallets")
.action(async () => {
console.log("Start getting the eth address");

const ethAddress = getEthAddress(TOBAN_PRIVATE_KEY as `0x${string}`);
listProfiles();
});

console.log("EthAddress:", ethAddress);
/**
* 新しいウォレットを追加
*/
walletCommands
.command("add")
.description("add a new wallet")
.requiredOption("--privateKey <privateKey>", "Private key to be saved")
.requiredOption("--name <name>", "Wallet name")
.action(({ name, privateKey }) => {
saveProfile({ name, privateKey });
});

/**
Expand Down
57 changes: 57 additions & 0 deletions pkgs/cli/src/services/wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { existsSync, readFileSync, writeFileSync } from "fs";
import path from "path";
import { Hex } from "viem";
import { privateKeyToAccount, privateKeyToAddress } from "viem/accounts";
const profilesPath = path.join(__dirname, "profiles.json");

export interface Profile {
name: string;
privateKey: Hex;
}

export const getProfiles = () => {
if (!existsSync(profilesPath)) {
writeFileSync(profilesPath, JSON.stringify([]));
}
const data = readFileSync(profilesPath, "utf8");
return JSON.parse(data) as Profile[];
};

export const getWalletByName = (name: string) => {
const profiles = getProfiles();
const profile = profiles.find((p) => p.name === name);

if (!profile) throw "Profile not found.";

return privateKeyToAccount(profile.privateKey);
};

export const saveProfile = (params: Profile) => {
if (!params.privateKey.match(/^0x[0-9a-f]{64}$/)) {
console.log("Invalid private key.");
return;
}

const profiles: Profile[] = getProfiles();
if (
profiles.find(
(p) => p.privateKey.toLowerCase() === params.privateKey.toLowerCase()
) ||
profiles.find((p) => p.name === params.name)
) {
console.log("Already exists.");
return;
}

profiles.push(params);

writeFileSync(profilesPath, JSON.stringify(profiles, null, 2));
console.log(`Profile ${params.name} with private key has been saved.`);
};

export const listProfiles = () => {
const profiles = getProfiles();
for (const profile of profiles) {
console.log(`${profile.name}: ${privateKeyToAddress(profile.privateKey)}`);
}
};

0 comments on commit 5f66bb3

Please sign in to comment.