-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from rsksmart/feat/DV-762-address-book
Feat/DV-762 address book
- Loading branch information
Showing
5 changed files
with
199 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import inquirer from "inquirer"; | ||
import chalk from "chalk"; | ||
import { loadWallets } from "../utils/index.js"; | ||
import { walletFilePath } from "../utils/constants.js"; | ||
import { writeWalletData } from "./wallet.js"; | ||
|
||
export async function addressBookCommand() { | ||
try { | ||
const walletsDataString = loadWallets(); | ||
const walletsData = JSON.parse(walletsDataString); | ||
walletsData.addressBook = walletsData.addressBook || {}; | ||
|
||
const actions = [ | ||
"➕ Add Address", | ||
"📖 View Address Book", | ||
"✏️ Update Address", | ||
"🗑️ Delete Address", | ||
]; | ||
|
||
const { action } = await inquirer.prompt([ | ||
{ | ||
type: "list", | ||
name: "action", | ||
message: "What would you like to do with your address book?", | ||
choices: actions, | ||
}, | ||
]); | ||
|
||
if (action === "➕ Add Address") { | ||
const { label, address } = await inquirer.prompt([ | ||
{ | ||
type: "input", | ||
name: "label", | ||
message: "Enter a label for the address:", | ||
}, | ||
{ | ||
type: "input", | ||
name: "address", | ||
message: "Enter the address:", | ||
}, | ||
]); | ||
|
||
if (walletsData.addressBook[label]) { | ||
console.log(chalk.red(`❌ Label "${label}" already exists.`)); | ||
return; | ||
} | ||
|
||
walletsData.addressBook[label] = address; | ||
console.log(chalk.green(`✅ Address added under label "${label}".`)); | ||
} | ||
|
||
if (action === "📖 View Address Book") { | ||
const addressBook = walletsData.addressBook; | ||
if (Object.keys(addressBook).length === 0) { | ||
console.log(chalk.red("❌ Address book is empty.")); | ||
return; | ||
} | ||
|
||
console.log(chalk.green("📖 Address Book:")); | ||
Object.entries(addressBook).forEach(([label, address]) => { | ||
console.log(chalk.blue(`- ${label}: ${address}`)); | ||
}); | ||
} | ||
|
||
if (action === "✏️ Update Address") { | ||
const labels = Object.keys(walletsData.addressBook); | ||
if (labels.length === 0) { | ||
console.log(chalk.red("❌ Address book is empty.")); | ||
return; | ||
} | ||
|
||
const { label } = await inquirer.prompt([ | ||
{ | ||
type: "list", | ||
name: "label", | ||
message: "Select the address you want to update:", | ||
choices: labels, | ||
}, | ||
]); | ||
|
||
const { newAddress } = await inquirer.prompt([ | ||
{ | ||
type: "input", | ||
name: "newAddress", | ||
message: `Enter the new address for "${label}":`, | ||
}, | ||
]); | ||
|
||
walletsData.addressBook[label] = newAddress; | ||
console.log(chalk.green(`✅ Address for "${label}" updated.`)); | ||
} | ||
|
||
if (action === "🗑️ Delete Address") { | ||
const labels = Object.keys(walletsData.addressBook); | ||
if (labels.length === 0) { | ||
console.log(chalk.red("❌ Address book is empty.")); | ||
return; | ||
} | ||
|
||
const { label } = await inquirer.prompt([ | ||
{ | ||
type: "list", | ||
name: "label", | ||
message: "Select the address you want to delete:", | ||
choices: labels, | ||
}, | ||
]); | ||
|
||
delete walletsData.addressBook[label]; | ||
console.log(chalk.red(`🗑️ Address with label "${label}" deleted.`)); | ||
} | ||
|
||
writeWalletData(walletFilePath, walletsData); | ||
} catch (error: any) { | ||
console.error(chalk.red("❌ Error managing address book:"), error.message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { loadWallets } from "../utils/index.js"; | ||
import inquirer from "inquirer"; | ||
import chalk from "chalk"; | ||
import { Address } from "viem"; | ||
|
||
export async function selectAddress(): Promise<Address> { | ||
try { | ||
const walletsDataString = loadWallets(); | ||
const walletsData = JSON.parse(walletsDataString); | ||
walletsData.addressBook = walletsData.addressBook || {}; | ||
|
||
const addressBook = walletsData.addressBook; | ||
const addressBookLabels = Object.keys(addressBook); | ||
|
||
const addressChoices = [ | ||
...addressBookLabels.map((label) => ({ | ||
name: `${label} (${addressBook[label]})`, | ||
value: addressBook[label], | ||
})), | ||
{ name: "Enter a custom address", value: "custom" }, | ||
]; | ||
|
||
const { selectedAddress } = await inquirer.prompt([ | ||
{ | ||
type: "list", | ||
name: "selectedAddress", | ||
message: "Select an address:", | ||
choices: addressChoices, | ||
}, | ||
]); | ||
|
||
if (selectedAddress === "custom") { | ||
const { customAddress } = await inquirer.prompt([ | ||
{ | ||
type: "input", | ||
name: "customAddress", | ||
message: "Enter the address:", | ||
}, | ||
]); | ||
return customAddress; | ||
} | ||
|
||
return selectedAddress; | ||
} catch (error: any) { | ||
console.error( | ||
chalk.red("❌ Error selecting address:"), | ||
chalk.yellow(error.message || error) | ||
); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters