diff --git a/README.md b/README.md index 0249515..3f4934f 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,14 @@ Use the `-t` or `--testnet` flag to check the balance on the Rootstock testnet. rsk-cli balance -t ``` +#### Wallet + +Use the `--wallet` flag to dynamically select wallet + +```bash +rsk-cli balance --wallet +``` + Output example: ``` @@ -166,6 +174,14 @@ Use the `-t` or `--testnet` flag to execute the transfer on the Rootstock testne rsk-cli transfer --testnet --address 0x0x08C4E4BdAb2473E454B8B2a4400358792786d341 --value 0.001 ``` +#### Wallet + +Use the `--wallet` flag to dynamically select wallet + +```bash +rsk-cli transfer --wallet --address 0x0x08C4E4BdAb2473E454B8B2a4400358792786d341 --value 0.001 +``` + Output example: ``` @@ -222,6 +238,12 @@ rsk-cli deploy --abi --bytecode --args < rsk-cli deploy --testnet --abi --bytecode --args ... ``` +#### Wallet + +```bash +rsk-cli deploy --wallet --abi --bytecode --args ... +``` + Output example: ``` @@ -326,6 +348,12 @@ rsk-cli bridge rsk-cli bridge --testnet ``` +#### Wallet + +```bash +rsk-cli bridge --wallet +``` + Output example: ``` diff --git a/bin/index.ts b/bin/index.ts index f240d42..c4359ec 100644 --- a/bin/index.ts +++ b/bin/index.ts @@ -22,6 +22,7 @@ interface CommandOptions { json?: any; name?: string; decodedArgs?: any; + wallet?: string; } const orange = chalk.rgb(255, 165, 0); @@ -56,14 +57,16 @@ program .command("balance") .description("Check the balance of the saved wallet") .option("-t, --testnet", "Check the balance on the testnet") + .option("--wallet ", "Name of the wallet") .action(async (options: CommandOptions) => { - await balanceCommand(!!options.testnet); + await balanceCommand(!!options.testnet, options.wallet!); }); program .command("transfer") .description("Transfer rBTC to the provided address") .option("-t, --testnet", "Transfer on the testnet") + .option("--wallet ", "Name of the wallet") .requiredOption("-a, --address
", "Recipient address") .requiredOption("-v, --value ", "Amount to transfer in rBTC") .action(async (options: CommandOptions) => { @@ -75,7 +78,8 @@ program await transferCommand( !!options.testnet, address, - parseFloat(options.value!) + parseFloat(options.value!), + options.wallet! ); } catch (error) { console.error(chalk.red("Error during transfer:"), error); @@ -100,6 +104,7 @@ program .description("Deploy a contract") .requiredOption("--abi ", "Path to the ABI file") .requiredOption("--bytecode ", "Path to the bytecode file") + .option("--wallet ", "Name of the wallet") .option("--args ", "Constructor arguments (space-separated)") .option("-t, --testnet", "Deploy on the testnet") .action(async (options: CommandOptions) => { @@ -108,7 +113,8 @@ program options.abi!, options.bytecode!, !!options.testnet, - args + args, + options.wallet! ); }); @@ -147,8 +153,9 @@ program .command("bridge") .description("Interact with RSK bridge") .option("-t, --testnet", "Deploy on the testnet") + .option("--wallet ", "Name of the wallet") .action(async (options: CommandOptions) => { - await bridgeCommand(!!options.testnet); + await bridgeCommand(!!options.testnet, options.wallet!); }); program.parse(process.argv); diff --git a/src/commands/balance.ts b/src/commands/balance.ts index ab6717d..7371e91 100644 --- a/src/commands/balance.ts +++ b/src/commands/balance.ts @@ -3,7 +3,7 @@ import fs from "fs"; import chalk from "chalk"; import { walletFilePath } from "../utils/constants.js"; -export async function balanceCommand(testnet: boolean) { +export async function balanceCommand(testnet: boolean, name: string) { try { if (!fs.existsSync(walletFilePath)) { console.log( @@ -24,8 +24,20 @@ export async function balanceCommand(testnet: boolean) { } const { currentWallet, wallets } = walletsData; + let wallet = wallets[currentWallet]; + + if (name) { + if (!wallets[name]) { + console.log( + chalk.red("⚠️ Wallet with the provided name does not exist.") + ); + + throw new Error(); + } else { + wallet = wallets[name]; + } + } - const wallet = wallets[currentWallet]; const { address } = wallet; if (!address) { diff --git a/src/commands/bridge.ts b/src/commands/bridge.ts index 7a1771b..d3edc63 100644 --- a/src/commands/bridge.ts +++ b/src/commands/bridge.ts @@ -12,7 +12,7 @@ type InquirerAnswers = { args?: string[]; }; -export async function bridgeCommand(testnet: boolean) { +export async function bridgeCommand(testnet: boolean, name: string) { try { const spinner = ora(); console.log( @@ -100,7 +100,7 @@ export async function bridgeCommand(testnet: boolean) { } if (selectedType === "write") { - const walletClient = await provider.getWalletClient(); + const walletClient = await provider.getWalletClient(name); const account = walletClient.account; console.log(chalk.blue(`🔑 Wallet account: ${account?.address}`)); diff --git a/src/commands/deploy.ts b/src/commands/deploy.ts index 89db662..c08045c 100644 --- a/src/commands/deploy.ts +++ b/src/commands/deploy.ts @@ -7,7 +7,8 @@ export async function deployCommand( abiPath: string, bytecodePath: string, testnet: boolean, - args: any[] = [] + args: any[] = [], + name: string ): Promise { try { console.log( @@ -16,7 +17,7 @@ export async function deployCommand( ) ); const provider = new ViemProvider(testnet); - const walletClient = await provider.getWalletClient(); + const walletClient = await provider.getWalletClient(name); if (!walletClient.account) { console.error( diff --git a/src/commands/transfer.ts b/src/commands/transfer.ts index 106a2cf..81b0442 100644 --- a/src/commands/transfer.ts +++ b/src/commands/transfer.ts @@ -8,7 +8,8 @@ import { walletFilePath } from "../utils/constants.js"; export async function transferCommand( testnet: boolean, toAddress: Address, - value: number + value: number, + name?: string ) { try { if (!fs.existsSync(walletFilePath)) { @@ -31,7 +32,19 @@ export async function transferCommand( const { currentWallet, wallets } = walletsData; - const wallet = wallets[currentWallet]; + let wallet = wallets[currentWallet]; + + if (name) { + if (!wallets[name]) { + console.log( + chalk.red("⚠️ Wallet with the provided name does not exist.") + ); + + throw new Error(); + } else { + wallet = wallets[name]; + } + } const { address: walletAddress } = wallet; if (!walletAddress) { @@ -64,7 +77,7 @@ export async function transferCommand( return; } - const walletClient = await provider.getWalletClient(); + const walletClient = await provider.getWalletClient(name); const account = walletClient.account; if (!account) { diff --git a/src/utils/viemProvider.ts b/src/utils/viemProvider.ts index caff679..d342ad7 100644 --- a/src/utils/viemProvider.ts +++ b/src/utils/viemProvider.ts @@ -27,8 +27,10 @@ class ViemProvider { }); } - public async getWalletClient(): Promise { - const { account } = await this.decryptPrivateKey(); + public async getWalletClient( + name: string | undefined + ): Promise { + const { account } = await this.decryptPrivateKey(name ? name : undefined); return createWalletClient({ chain: this.chain, @@ -37,7 +39,7 @@ class ViemProvider { }); } - private async decryptPrivateKey(): Promise<{ + private async decryptPrivateKey(name: string | undefined): Promise<{ account: ReturnType; }> { if (!fs.existsSync(walletFilePath)) { @@ -58,6 +60,19 @@ class ViemProvider { } const { currentWallet, wallets } = walletsData; + let wallet = wallets[currentWallet]; + + if (name) { + if (!wallets[name]) { + console.log( + chalk.red("⚠️ Wallet with the provided name does not exist.") + ); + + throw new Error(); + } else { + wallet = wallets[name]; + } + } const passwordQuestion: any = [ { @@ -70,7 +85,6 @@ class ViemProvider { const { password } = await inquirer.prompt(passwordQuestion); - const wallet = wallets[currentWallet]; const { encryptedPrivateKey, iv } = wallet; try {