From 148b6f8e74f6bc2145d0b84c86c1dafbd71bc171 Mon Sep 17 00:00:00 2001 From: mixmix Date: Tue, 1 Oct 2024 17:04:46 +1300 Subject: [PATCH] WIP: QA file-restructure --- src/account/command.ts | 12 ++++++++-- src/balance/command.ts | 19 ++++++++++----- src/common/utils-cli.ts | 7 +++--- src/config/encoding.ts | 12 +++++++--- src/sign/command.ts | 4 ++-- src/transfer/command.ts | 21 ++++++++++++---- tests/cli.test.sh | 53 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 107 insertions(+), 21 deletions(-) create mode 100755 tests/cli.test.sh diff --git a/src/account/command.ts b/src/account/command.ts index a502a850..59fa8745 100644 --- a/src/account/command.ts +++ b/src/account/command.ts @@ -13,6 +13,9 @@ export function entropyAccountCommand () { .addCommand(entropyAccountImport()) .addCommand(entropyAccountList()) .addCommand(entropyAccountRegister()) + // .addCommand(entropyAccountAlias()) + // IDEA: support aliases for remote accounts (those we don't have seeds for) + // this would make transfers safer/ easier from CLI } function entropyAccountCreate () { @@ -84,9 +87,9 @@ function entropyAccountList () { function entropyAccountRegister () { return new Command('register') .description('Register an entropy account with a program') - .addOption(passwordOption()) - .addOption(endpointOption()) .addOption(accountOption()) + .addOption(endpointOption()) + .addOption(passwordOption()) // Removing these options for now until we update the design to accept program configs // .addOption( // new Option( @@ -101,11 +104,16 @@ function entropyAccountRegister () { // ) // ) .action(async (opts) => { + console.log('here 0') + console.log(opts) // NOTE: loadEntropy throws if it can't find opts.account const entropy: Entropy = await loadEntropy(opts.account, opts.endpoint) const accountService = new EntropyAccount(entropy, opts.endpoint) + console.log('here 1') + const verifyingKey = await accountService.register() + console.log('here 2') await addVerifyingKeyToAccountAndSelect(verifyingKey, opts.account) cliWrite(verifyingKey) diff --git a/src/balance/command.ts b/src/balance/command.ts index 4d0b023a..ad60000a 100644 --- a/src/balance/command.ts +++ b/src/balance/command.ts @@ -1,22 +1,29 @@ import { Command } from "commander"; import Entropy from "@entropyxyz/sdk"; -import { cliWrite, endpointOption, loadEntropy, passwordOption } from "src/common/utils-cli"; + import { EntropyBalance } from "./main"; +import { cliWrite, endpointOption, loadEntropy, passwordOption } from "../common/utils-cli"; +import { findAccountByAddressOrName } from "../common/utils"; +import * as config from "../config"; export function entropyBalanceCommand () { const balanceCommand = new Command('balance') balanceCommand .description('Command to retrieive the balance of an account on the Entropy Network') - .argument('address', 'Account address whose balance you want to query') - .addOption(passwordOption()) + .argument('account ', 'Account address whose balance you want to query') .addOption(endpointOption()) - .action(async (address, opts) => { - const entropy: Entropy = await loadEntropy(address, opts.endpoint) + .addOption(passwordOption()) + .action(async (account, opts) => { + const entropy: Entropy = await loadEntropy(account, opts.endpoint) const BalanceService = new EntropyBalance(entropy, opts.endpoint) + + const { accounts } = await config.get() + const address = findAccountByAddressOrName(accounts, account)?.address + const balance = await BalanceService.getBalance(address) cliWrite(`${balance.toLocaleString('en-US')} BITS`) process.exit(0) }) - + return balanceCommand } diff --git a/src/common/utils-cli.ts b/src/common/utils-cli.ts index 3aec350b..f185cd05 100644 --- a/src/common/utils-cli.ts +++ b/src/common/utils-cli.ts @@ -20,7 +20,7 @@ function getConfigOrNull () { export function endpointOption () { return new Option( - '-e, --endpoint ', + '-e, --endpoint ', [ 'Runs entropy with the given endpoint and ignores network endpoints in config.', 'Can also be given a stored endpoint name from config eg: `entropy --endpoint test-net`.' @@ -44,16 +44,17 @@ export function endpointOption () { export function passwordOption (description?: string) { return new Option( - '-p, --password ', + '-p, --password', description || 'Password for the account' ) + .hideHelp() // TEMP } export function accountOption () { const storedConfig = getConfigOrNull() return new Option( - '-a, --account ', + '-a, --account ', [ 'Sets the account for the session.', 'Defaults to the last set account (or the first account if one has not been set before).' diff --git a/src/config/encoding.ts b/src/config/encoding.ts index cb580655..e29422d6 100644 --- a/src/config/encoding.ts +++ b/src/config/encoding.ts @@ -1,12 +1,18 @@ const PREFIX = 'data:application/UI8A;base64,' // was a UInt8Array, but is stored as base64 -export function serialize (config) { +export function serialize (config: object) { return JSON.stringify(config, replacer, 2) } -export function deserialize (config) { - return JSON.parse(config, reviver) +export function deserialize (config: string) { + try { + return JSON.parse(config, reviver) + } catch (err) { + console.log('broken config:', config) + // WIP here: nothing being passed in?!! + throw err + } } function replacer (_key: string, value: any) { diff --git a/src/sign/command.ts b/src/sign/command.ts index fc574228..87c2ed70 100644 --- a/src/sign/command.ts +++ b/src/sign/command.ts @@ -6,9 +6,9 @@ export function entropySignCommand () { const signCommand = new Command('sign') .description('Sign a message using the Entropy network. Output is a JSON { verifyingKey, signature }') .argument('msg', 'Message you would like to sign (string)') - .addOption(passwordOption('Password for the source account (if required)')) - .addOption(endpointOption()) .addOption(accountOption()) + .addOption(endpointOption()) + .addOption(passwordOption('Password for the source account (if required)')) // .addOption( // new Option( // '-r, --raw', diff --git a/src/transfer/command.ts b/src/transfer/command.ts index b814e2f9..db3bf157 100644 --- a/src/transfer/command.ts +++ b/src/transfer/command.ts @@ -1,20 +1,31 @@ import { Command } from "commander" -import { accountOption, endpointOption, loadEntropy, passwordOption } from "src/common/utils-cli" +import { accountOption, endpointOption, loadEntropy, passwordOption } from "../common/utils-cli" import { EntropyTransfer } from "./main" export function entropyTransferCommand () { - const transferCommand = new Command('tranfer') + const transferCommand = new Command('transfer') transferCommand .description('Transfer funds between two Entropy accounts.') // TODO: name the output .argument('destination', 'Account address funds will be sent to') - .argument('amount', 'Amount of funds to be moved') - .addOption(passwordOption('Password for the source account (if required)')) - .addOption(endpointOption()) + .argument('amount', 'Amount of funds to be moved (in "tokens")') .addOption(accountOption()) + .addOption(endpointOption()) + .addOption(passwordOption('Password for the source account (if required)')) .action(async (destination, amount, opts) => { + console.log({ destination, amount, opts }) + + // TODO: destination as ? const entropy = await loadEntropy(opts.account, opts.endpoint) + .catch(err => { + // WIP here. SOMETHING is wrecking the config upstream + console.error("loadEntropy failed", err) + throw err + }) + const transferService = new EntropyTransfer(entropy, opts.endpoint) + await transferService.transfer(destination, amount) + // cliWrite(??) // TODO: write the output process.exit(0) }) diff --git a/tests/cli.test.sh b/tests/cli.test.sh new file mode 100755 index 00000000..0e2a0374 --- /dev/null +++ b/tests/cli.test.sh @@ -0,0 +1,53 @@ +#! /usr/bin/bash + +ENTROPY_ENDPOINT=ws://127.0.0.1:9944 + +rm ~/.config/entropy-cryptography/entropy-cli.json +# backup config +# mv ~/.config/entropy-cryptography/entropy-cli{.json,.backup.json} + +print () { + COLOR='\033[0;35m' + RESET='\033[0m' + echo "" + echo -e "${COLOR}> $1${RESET}" +} + +print "// ACCOUNT /////////////////////////////////////////////////" + +# Errors (correct, but messy?) +# print "account ls:" +# entropy account ls | jq + +print "account create" +entropy account create naynay | jq + +print "account import" +entropy account import faucet 0x358f394d157e31be23313a1500f5e2c8871e514e530a35aa5c05334be7a39ba6 | jq + +print "account list" +entropy account list | jq + + + +print "// BALANCE ///////////////////////////////////////////////// " + +print "balance naynay" +entropy balance naynay + +print "balance 5CqJyjALDFz4sKjQgK8NXBQGHCWAiV63xXn2Dye393Y6Vghz" +# entropy balance faucet +entropy balance 5CqJyjALDFz4sKjQgK8NXBQGHCWAiV63xXn2Dye393Y6Vghz + + + +print "// TRANSFER ////////////////////////////////////////////////" + +print "entropy transfer" +NAYNAY_ADDRESS=`entropy account ls | jq --raw-output ".[0].address"` +# NOTE: --raw-output is needed to drop the quotes +entropy transfer -a faucet ${NAYNAY_ADDRESS} 2.5 +entropy balance naynay + +# restore config +# mv ~/.config/entropy-cryptography/entropy-cli{.backup.json,.json}