Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:dynamic wallet selection #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>
```

Output example:

```
Expand All @@ -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 <name> --address 0x0x08C4E4BdAb2473E454B8B2a4400358792786d341 --value 0.001
```

Output example:

```
Expand Down Expand Up @@ -222,6 +238,12 @@ rsk-cli deploy --abi <path_to_abi> --bytecode <path_to_bytecode> --args <arg1> <
rsk-cli deploy --testnet --abi <path_to_abi> --bytecode <path_to_bytecode> --args <arg1> <arg2> ...
```

#### Wallet

```bash
rsk-cli deploy --wallet <name> --abi <path_to_abi> --bytecode <path_to_bytecode> --args <arg1> <arg2> ...
```

Output example:

```
Expand Down Expand Up @@ -326,6 +348,12 @@ rsk-cli bridge
rsk-cli bridge --testnet
```

#### Wallet

```bash
rsk-cli bridge --wallet <name>
```

Output example:

```
Expand Down
15 changes: 11 additions & 4 deletions bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface CommandOptions {
json?: any;
name?: string;
decodedArgs?: any;
wallet?: string;
}

const orange = chalk.rgb(255, 165, 0);
Expand Down Expand Up @@ -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 <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 <wallet>", "Name of the wallet")
.requiredOption("-a, --address <address>", "Recipient address")
.requiredOption("-v, --value <value>", "Amount to transfer in rBTC")
.action(async (options: CommandOptions) => {
Expand All @@ -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);
Expand All @@ -100,6 +104,7 @@ program
.description("Deploy a contract")
.requiredOption("--abi <path>", "Path to the ABI file")
.requiredOption("--bytecode <path>", "Path to the bytecode file")
.option("--wallet <wallet>", "Name of the wallet")
.option("--args <args...>", "Constructor arguments (space-separated)")
.option("-t, --testnet", "Deploy on the testnet")
.action(async (options: CommandOptions) => {
Expand All @@ -108,7 +113,8 @@ program
options.abi!,
options.bytecode!,
!!options.testnet,
args
args,
options.wallet!
);
});

Expand Down Expand Up @@ -147,8 +153,9 @@ program
.command("bridge")
.description("Interact with RSK bridge")
.option("-t, --testnet", "Deploy on the testnet")
.option("--wallet <wallet>", "Name of the wallet")
.action(async (options: CommandOptions) => {
await bridgeCommand(!!options.testnet);
await bridgeCommand(!!options.testnet, options.wallet!);
});

program.parse(process.argv);
16 changes: 14 additions & 2 deletions src/commands/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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}`));
Expand Down
5 changes: 3 additions & 2 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export async function deployCommand(
abiPath: string,
bytecodePath: string,
testnet: boolean,
args: any[] = []
args: any[] = [],
name: string
): Promise<void> {
try {
console.log(
Expand All @@ -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(
Expand Down
19 changes: 16 additions & 3 deletions src/commands/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 18 additions & 4 deletions src/utils/viemProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ class ViemProvider {
});
}

public async getWalletClient(): Promise<WalletClient> {
const { account } = await this.decryptPrivateKey();
public async getWalletClient(
name: string | undefined
): Promise<WalletClient> {
const { account } = await this.decryptPrivateKey(name ? name : undefined);

return createWalletClient({
chain: this.chain,
Expand All @@ -37,7 +39,7 @@ class ViemProvider {
});
}

private async decryptPrivateKey(): Promise<{
private async decryptPrivateKey(name: string | undefined): Promise<{
account: ReturnType<typeof privateKeyToAccount>;
}> {
if (!fs.existsSync(walletFilePath)) {
Expand All @@ -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 = [
{
Expand All @@ -70,7 +85,6 @@ class ViemProvider {

const { password } = await inquirer.prompt(passwordQuestion);

const wallet = wallets[currentWallet];
const { encryptedPrivateKey, iv } = wallet;

try {
Expand Down