Skip to content

Commit

Permalink
CSUB-589: changed sensitive input to interactive or env vars (#1151)
Browse files Browse the repository at this point in the history
* changed sensitive input to interactive or env vars

* improved error handling in getSeedFromEnvOrPrompt

* yarn format

* eslint

* changed prompt from invisible to password

* centralized the regular seed

* rectified stash vs bond key elicitation

* set_keys actor rectification

* make pretty

* unbound to controller seed

* appropriate names

* prettify

---------

Co-authored-by: Jon Zlotnik <jon.zlotnik@gluwa.com>
  • Loading branch information
jonZlotnik and jonZlotnik authored Jun 21, 2023
1 parent b23422d commit 6dc67c3
Show file tree
Hide file tree
Showing 14 changed files with 420 additions and 471 deletions.
11 changes: 3 additions & 8 deletions scripts/cc-cli/src/commands/bond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@ import { Command, OptionValues } from "commander";
import { newApi } from "../api";
import {
checkAddress,
getSeedFromOptions,
getStashSeedFromEnvOrPrompt,
initKeyringPair,
} from "../utils/account";
import { Balance, getBalance, toMicrounits } from "../utils/balance";
import { bond, parseRewardDestination } from "../utils/bond";
import { promptContinue } from "../utils/promptContinue";
import { Balance, getBalance, toMicrounits } from "../utils/balance";

export function makeBondCommand() {
const cmd = new Command("bond");
cmd.description("Bond CTC from a Stash account");
cmd.option("-a, --amount [amount]", "Amount to bond");
cmd.option("-s, --seed [seed phrase]", "Specify seed phrase to bond from");
cmd.option(
"-f, --file [file-name]",
"Specify file with seed phrase to bond from"
);
cmd.option("-c, --controller [controller]", "Specify controller address");
cmd.option(
"-r, --reward-destination [reward-destination]",
Expand All @@ -43,7 +38,7 @@ async function bondAction(options: OptionValues) {
process.exit(1);
}

const stashSeed = getSeedFromOptions(options);
const stashSeed = await getStashSeedFromEnvOrPrompt();

// Check balance
const stashKeyring = initKeyringPair(stashSeed);
Expand Down
12 changes: 2 additions & 10 deletions scripts/cc-cli/src/commands/chill.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import { Command, OptionValues } from "commander";
import { newApi } from "../api";
import { getSeedFromOptions } from "../utils/account";
import { getControllerSeedFromEnvOrPrompt } from "../utils/account";
import { chill } from "../utils/validate";

export function makeChillCommand() {
const cmd = new Command("chill");
cmd.description(
"Signal intention to stop validating from a Controller account"
);
cmd.option(
"-s, --seed [seed phrase]",
"Specify seed phrase of controller account"
);
cmd.option(
"-f, --file [file-name]",
"Specify file with seed phrase of controller account"
);
cmd.action(chillAction);
return cmd;
}

async function chillAction(options: OptionValues) {
const { api } = await newApi(options.url);

const controllerSeed = getSeedFromOptions(options);
const controllerSeed = await getControllerSeedFromEnvOrPrompt();

console.log("Creating chill transaction...");

Expand Down
16 changes: 7 additions & 9 deletions scripts/cc-cli/src/commands/distributeRewards.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Command, OptionValues } from "commander";
import { newApi } from "../api";
import { getSeedFromOptions, initKeyringPair } from "../utils/account";
import {
getCallerSeedFromEnvOrPrompt,
initKeyringPair,
} from "../utils/account";

export function makeDistributeRewardsCommand() {
const cmd = new Command("distribute-rewards");
cmd.description("Distribute all pending rewards for all validators");
cmd.option("-s, --seed [mnemonic]", "Specify mnemonic phrase to use");
cmd.option(
"-f, --file [file-name]",
"Specify file with mnemonic phrase to use"
);
cmd.option(
"-v, --validator-id [validator-id]",
"Specify validator to distribute rewards for"
Expand All @@ -22,8 +20,6 @@ export function makeDistributeRewardsCommand() {
async function distributeRewardsAction(options: OptionValues) {
const { api } = await newApi(options.url);

const signerSeed = getSeedFromOptions(options);

if (!options.validatorId) {
console.log("Must specify a validator to distribute rewards for");
process.exit(1);
Expand All @@ -34,12 +30,14 @@ async function distributeRewardsAction(options: OptionValues) {
process.exit(1);
}

// Any account can call the distribute_rewards extrinsic
const callerSeed = await getCallerSeedFromEnvOrPrompt();
const distributeTx = api.tx.staking.payoutStakers(
options.validatorId,
options.era
);

const hash = await distributeTx.signAndSend(initKeyringPair(signerSeed));
const hash = await distributeTx.signAndSend(initKeyringPair(callerSeed));

console.log("Payout stakers transaction sent with hash:", hash.toHex());
process.exit(0);
Expand Down
12 changes: 2 additions & 10 deletions scripts/cc-cli/src/commands/newSeed.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Command, OptionValues } from "commander";
import { mnemonicGenerate } from "@polkadot/util-crypto";
import { writeFileSync } from "fs";
import { Command, OptionValues } from "commander";

export function makeNewSeedCommand() {
const cmd = new Command("new");
cmd.description("Create new seed phrase");
cmd.option("-l, --length [word-length]", "Specify the amount of words");
cmd.option("-s, --save [file-name]", "Save the new seed to a file");
cmd.action(newSeedAction);
return cmd;
}
Expand All @@ -16,12 +14,6 @@ function newSeedAction(options: OptionValues) {
const seedPhrase = options.length
? mnemonicGenerate(options.length)
: mnemonicGenerate();
if (options.save) {
console.log("Saving seed to file:", options.save);
// Write seed phrase to a file
writeFileSync(options.save, seedPhrase);
} else {
console.log("Seed phrase:", seedPhrase);
}
console.log("Seed phrase:", seedPhrase);
process.exit(0);
}
26 changes: 9 additions & 17 deletions scripts/cc-cli/src/commands/send.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import { Command, OptionValues } from "commander";
import { ApiPromise } from "creditcoin-js";
import { newApi } from "../api";
import {
checkAddress,
getSeedFromOptions,
getCallerSeedFromEnvOrPrompt,
initECDSAKeyringPairFromPK,
initKeyringPair,
} from "../utils/account";
import { toMicrounits } from "../utils/balance";
import { ApiPromise } from "creditcoin-js";

export function makeSendCommand() {
const cmd = new Command("send");
cmd.description("Send CTC from an account");
cmd.option(
"-s, --seed [mnemonic]",
"Specify mnemonic phrase to use for sending CTC"
);
cmd.option(
"-f, --file [file-name]",
"Specify file with mnemonic phrase to use for sending CTC"
);
cmd.option(
"--use-ecdsa",
"Use ECDSA signature scheme and a private key instead of a mnemonic phrase"
Expand Down Expand Up @@ -57,29 +49,29 @@ function checkAmount(options: OptionValues) {

async function sendFromSr25519(options: OptionValues, api: ApiPromise) {
// Build account
const seed = getSeedFromOptions(options);
const stash = initKeyringPair(seed);
const callerSeed = await getCallerSeedFromEnvOrPrompt();
const caller = initKeyringPair(callerSeed);

// Send transaction
const tx = api.tx.balances.transfer(
options.to,
toMicrounits(options.amount).toString()
);
const hash = await tx.signAndSend(stash);
const hash = await tx.signAndSend(caller);
return hash;
}

async function sendFromECDSA(options: OptionValues, api: ApiPromise) {
// Build account
const seed = getSeedFromOptions(options);
const stash = initECDSAKeyringPairFromPK(seed);
console.log(stash.address);
const callerSeed = await getCallerSeedFromEnvOrPrompt();
const caller = initECDSAKeyringPairFromPK(callerSeed);
console.log(caller.address);

// Send transaction
const tx = api.tx.balances.transfer(
options.to,
toMicrounits(options.amount).toString()
);
const hash = await tx.signAndSend(stash);
const hash = await tx.signAndSend(caller);
return hash;
}
19 changes: 7 additions & 12 deletions scripts/cc-cli/src/commands/setKeys.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { Command, OptionValues } from "commander";
import { newApi } from "../api";
import { getSeedFromOptions, initKeyringPair } from "../utils/account";
import {
getControllerSeedFromEnvOrPrompt,
initKeyringPair,
} from "../utils/account";

export function makeSetKeysCommand() {
const cmd = new Command("set-keys");
cmd.description("Set session keys for a Controller account");
cmd.option(
"-s, --seed [mnemonic]",
"Specify mnemonic phrase to set keys from"
);
cmd.option(
"-f, --file [file-name]",
"Specify file with mnemonic phrase to set keys from"
);
cmd.option("-k, --keys [keys]", "Specify keys to set");
cmd.option("-r, --rotate", "Rotate and set new keys");

Expand All @@ -24,8 +19,8 @@ async function setKeysAction(options: OptionValues) {
const { api } = await newApi(options.url);

// Build account
const seed = getSeedFromOptions(options);
const stash = initKeyringPair(seed);
const controllerSeed = await getControllerSeedFromEnvOrPrompt();
const controller = initKeyringPair(controllerSeed);

let keys;
if (!options.keys && !options.rotate) {
Expand All @@ -40,7 +35,7 @@ async function setKeysAction(options: OptionValues) {
}

const tx = api.tx.session.setKeys(keys, []);
const hash = await tx.signAndSend(stash);
const hash = await tx.signAndSend(controller);

console.log("Set keys transaction hash: " + hash.toHex());

Expand Down
21 changes: 8 additions & 13 deletions scripts/cc-cli/src/commands/showAddress.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import { Command, OptionValues } from "commander";
import { getSeedFromOptions, initKeyringPair } from "../utils/account";
import { cryptoWaitReady } from "@polkadot/util-crypto";
import { Command } from "commander";
import {
getCallerSeedFromEnvOrPrompt,
initKeyringPair,
} from "../utils/account";

export function makeShowAddressCommand() {
const cmd = new Command("show-address");
cmd.description("Show account address");
cmd.option(
"-s, --seed [mnemonic]",
"Specify mnemonic phrase to use of account"
);
cmd.option(
"-f, --file [file-name]",
"Specify file with mnemonic phrase of account"
);
cmd.action(showAddressAction);
return cmd;
}

async function showAddressAction(options: OptionValues) {
async function showAddressAction() {
await cryptoWaitReady();
const seed = getSeedFromOptions(options);
const pair = initKeyringPair(seed);
const callerSeed = await getCallerSeedFromEnvOrPrompt();
const pair = initKeyringPair(callerSeed);
const address = pair.address;

console.log("Account address:", address);
Expand Down
16 changes: 7 additions & 9 deletions scripts/cc-cli/src/commands/unbond.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { Command, OptionValues } from "commander";
import { newApi } from "../api";
import { getSeedFromOptions, initKeyringPair } from "../utils/account";
import {
getControllerSeedFromEnvOrPrompt,
initKeyringPair,
} from "../utils/account";
import { toMicrounits } from "../utils/balance";

export function makeUnbondCommand() {
const cmd = new Command("unbond");
cmd.description("Schedule a portion of the stash to be unlocked");
cmd.option("-s, --seed [mnemonic]", "Specify mnemonic phrase to use");
cmd.option(
"-f, --file [file-name]",
"Specify file with mnemonic phrase to use"
);
cmd.option("-a, --amount [amount]", "Amount to send");
cmd.action(unbondAction);
return cmd;
Expand All @@ -23,13 +21,13 @@ async function unbondAction(options: OptionValues) {
checkAmount(options);

// Build account
const seed = getSeedFromOptions(options);
const stash = initKeyringPair(seed);
const controllerSeed = await getControllerSeedFromEnvOrPrompt();
const controller = initKeyringPair(controllerSeed);

// Unbond transaction
const tx = api.tx.staking.unbond(toMicrounits(options.amount).toString());

const hash = await tx.signAndSend(stash);
const hash = await tx.signAndSend(controller);

console.log("Unbond transaction hash: " + hash.toHex());
process.exit(0);
Expand Down
16 changes: 4 additions & 12 deletions scripts/cc-cli/src/commands/validate.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import { Command, OptionValues } from "commander";
import { newApi } from "../api";
import { getSeedFromOptions } from "../utils/account";
import { StakingPalletValidatorPrefs, validate } from "../utils/validate";
import { getControllerSeedFromEnvOrPrompt } from "../utils/account";
import { perbillFromPercent } from "../utils/perbill";
import { StakingPalletValidatorPrefs, validate } from "../utils/validate";

export function makeValidateCommand() {
const cmd = new Command("validate");
cmd.description("Signal intention to validate from a Controller account");
cmd.option(
"-s, --seed [mnemonic]",
"Specify mnemonic phrase to use for new account"
);
cmd.option(
"-f, --file [file-name]",
"Specify file with mnemonic phrase to use for new account"
);
cmd.option(
"--commission [commission]",
"Specify commission for validator in percent"
Expand All @@ -30,7 +22,7 @@ export function makeValidateCommand() {
async function validateAction(options: OptionValues) {
const { api } = await newApi(options.url);

const stashSeed = getSeedFromOptions(options);
const controllerSeed = await getControllerSeedFromEnvOrPrompt();

const commission = options.commission
? perbillFromPercent(options.commission)
Expand All @@ -40,7 +32,7 @@ async function validateAction(options: OptionValues) {

console.log("Creating validate transaction...");

const validateTxHash = await validate(stashSeed, preferences, api);
const validateTxHash = await validate(controllerSeed, preferences, api);

console.log("Validate transaction sent with hash:", validateTxHash.toHex());
process.exit(0);
Expand Down
Loading

0 comments on commit 6dc67c3

Please sign in to comment.