From 97aca6c7cfab13015307e91c0b07e58fdbdb0580 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 3 Aug 2020 07:49:26 +0200 Subject: [PATCH 1/3] Test example scripts --- .circleci/config.yml | 12 ++++++++++++ packages/cli/examples/faucet_addresses.ts | 2 +- packages/cli/examples/generate_address.ts | 2 +- packages/cli/src/cli.ts | 14 +++++++++++++- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index df705c05..2ff4af33 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -139,6 +139,18 @@ jobs: environment: SKIP_BUILD: 1 command: yarn selftest + - run: + name: Run CLI examples + working_directory: packages/cli + environment: + SKIP_BUILD: 1 + command: | + ./bin/cosmwasm-cli --init examples/delegate.ts --code "process.exit(0)" + ./bin/cosmwasm-cli --init examples/faucet_addresses.ts --code "process.exit(0)" + ./bin/cosmwasm-cli --init examples/generate_address.ts --code "process.exit(0)" + ./bin/cosmwasm-cli --init examples/helpers.ts --code "process.exit(0)" + ./bin/cosmwasm-cli --init examples/local_faucet.ts --code "process.exit(0)" + ./bin/cosmwasm-cli --init examples/mask.ts --code "process.exit(0)" - run: command: ./scripts/wasmd/stop.sh - run: diff --git a/packages/cli/examples/faucet_addresses.ts b/packages/cli/examples/faucet_addresses.ts index 211ff61f..2e080c11 100644 --- a/packages/cli/examples/faucet_addresses.ts +++ b/packages/cli/examples/faucet_addresses.ts @@ -5,5 +5,5 @@ for (let i of [0, 1, 2, 3, 4]) { const wallet = await Secp256k1Wallet.fromMnemonic(mnemonic, makeCosmoshubPath(i), "cosmos"); const [{ address, pubkey }] = await wallet.getAccounts(); console.info(`Address ${i}: ${address}`); - console.info(`Pubkey ${i}: ${pubkey}`); + console.info(`Pubkey ${i}: ${toBase64(pubkey)}`); } diff --git a/packages/cli/examples/generate_address.ts b/packages/cli/examples/generate_address.ts index cbf7ae95..9e403284 100644 --- a/packages/cli/examples/generate_address.ts +++ b/packages/cli/examples/generate_address.ts @@ -3,5 +3,5 @@ const wallet = await Secp256k1Wallet.fromMnemonic(mnemonic); const [{ address, pubkey }] = await wallet.getAccounts(); console.info("mnemonic:", mnemonic); -console.info("pubkey:", pubkey); +console.info("pubkey:", encodeSecp256k1Pubkey(pubkey)); console.info("address:", address); diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index a19172c8..adc81a25 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -14,6 +14,11 @@ export function main(originalArgs: readonly string[]): void { describe: "Read initial TypeScript code from files", type: "array", }, + code: { + describe: + "Add initial TypeScript code from the argument. All code arguments are processed after all init arguments.", + type: "array", + }, // Maintainer options debug: { describe: "Enable debugging", @@ -24,7 +29,7 @@ export function main(originalArgs: readonly string[]): void { type: "boolean", }, }) - .group(["init", "help", "version"], "User options") + .group(["init", "code", "help", "version"], "User options") .group(["debug", "selftest"], "Maintainer options") .parse(originalArgs); @@ -162,6 +167,13 @@ export function main(originalArgs: readonly string[]): void { } } + if (args.code) { + for (const code of args.code.map((arg) => arg.toString())) { + if (args.debug) console.info(`Adding code: '${code}' ...`); + init += `${code}\n`; + } + } + const tsconfigPath = join(__dirname, "..", "tsconfig_repl.json"); const installationDir = join(__dirname, ".."); new TsRepl(tsconfigPath, init, !!args.debug, installationDir).start().catch((error) => { From ce1f0f7f1f1c2bf4aaddf35c5da228b1cb341671 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 3 Aug 2020 10:33:50 +0200 Subject: [PATCH 2/3] Add support for HTTPs URLs in --init --- CHANGELOG.md | 1 + packages/cli/src/cli.ts | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f054fab1..0726f44c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## HEAD +- @cosmjs/cli: Now supports HTTPs URLs for `--init` code sources. - @cosmjs/cosmwasm: Rename `CosmWasmClient.getNonce` method to `.getSequence`. - @cosmjs/cosmwasm: Remove `RestClient` class in favour of new modular `LcdClient` class from @cosmjs/sdk38. diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index adc81a25..22cd44d0 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -1,3 +1,4 @@ +import axios from "axios"; import * as fs from "fs"; import { join } from "path"; import yargs from "yargs"; @@ -6,12 +7,13 @@ import { TsRepl } from "./tsrepl"; import colors = require("colors/safe"); -export function main(originalArgs: readonly string[]): void { +export async function main(originalArgs: readonly string[]): Promise { const args = yargs .options({ // User options (we get --help and --version for free) init: { - describe: "Read initial TypeScript code from files", + describe: + "Read initial TypeScript code from the given sources. This can be URLs (supported schemes: https) or local file paths.", type: "array", }, code: { @@ -161,9 +163,14 @@ export function main(originalArgs: readonly string[]): void { } if (args.init) { - for (const path of args.init.map((arg) => arg.toString())) { - if (args.debug) console.info(`Adding file: '${path}' ...`); - init += fs.readFileSync(path, "utf8") + "\n"; + for (const source of args.init.map((arg) => arg.toString())) { + if (args.debug) console.info(`Adding code from: '${source}' ...`); + if (source.startsWith("https://")) { + const response = await axios.get(source); + init += response.data + "\n"; + } else { + init += fs.readFileSync(source, "utf8") + "\n"; + } } } From 420feeb4b802d146c90e26f6b42acbbd0d531918 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 3 Aug 2020 10:34:18 +0200 Subject: [PATCH 3/3] Document new --code argument in CLI --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0726f44c..7d67e29f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## HEAD - @cosmjs/cli: Now supports HTTPs URLs for `--init` code sources. +- @cosmjs/cli: Now supports adding code directly via `--code`. - @cosmjs/cosmwasm: Rename `CosmWasmClient.getNonce` method to `.getSequence`. - @cosmjs/cosmwasm: Remove `RestClient` class in favour of new modular `LcdClient` class from @cosmjs/sdk38.