From a3b6ab5bd8e362d1b025bc6b0bd9b7b2e5657114 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 10 Feb 2020 10:53:38 +0100 Subject: [PATCH] Improve codec handling --- packages/faucet/src/actions/start/start.ts | 19 +++++++++++++------ packages/faucet/src/addresses.ts | 17 +++++++++++++++++ packages/faucet/src/codec.spec.ts | 1 - packages/faucet/src/codec.ts | 14 -------------- packages/faucet/src/constants.ts | 1 + packages/faucet/src/debugging.ts | 4 ++-- packages/faucet/src/faucet.ts | 6 +++--- packages/faucet/src/multichainhelpers.ts | 9 ++++----- packages/faucet/src/profile.ts | 4 ++-- 9 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 packages/faucet/src/addresses.ts delete mode 100644 packages/faucet/src/codec.spec.ts delete mode 100644 packages/faucet/src/codec.ts diff --git a/packages/faucet/src/actions/start/start.ts b/packages/faucet/src/actions/start/start.ts index 13ade74d..deb80177 100644 --- a/packages/faucet/src/actions/start/start.ts +++ b/packages/faucet/src/actions/start/start.ts @@ -1,9 +1,10 @@ import { UserProfile } from "@iov/keycontrol"; import cors = require("@koa/cors"); +import { createCosmWasmConnector } from "@cosmwasm/bcp"; import Koa from "koa"; import bodyParser from "koa-bodyparser"; -import { codecImplementation, establishConnection } from "../../codec"; +import { isValidAddress } from "../../addresses"; import * as constants from "../../constants"; import { logAccountsState, logSendJob } from "../../debugging"; import { Faucet } from "../../faucet"; @@ -41,8 +42,14 @@ export async function start(args: ReadonlyArray): Promise { if (!constants.mnemonic) { throw new Error("The FAUCET_MNEMONIC environment variable is not set"); } + + const connector = createCosmWasmConnector( + blockchainBaseUrl, + constants.addressPrefix, + constants.tokenConfig, + ); console.info(`Connecting to blockchain ${blockchainBaseUrl} ...`); - const connection = await establishConnection(blockchainBaseUrl); + const connection = await connector.establishConnection(); const connectedChainId = connection.chainId(); console.info(`Connected to network: ${connectedChainId}`); @@ -67,8 +74,8 @@ export async function start(args: ReadonlyArray): Promise { const faucet = new Faucet(constants.tokenConfig); - await faucet.refill(profile, connection); - setInterval(async () => faucet.refill(profile, connection), 60_000); // ever 60 seconds + await faucet.refill(profile, connection, connector.codec); + setInterval(async () => faucet.refill(profile, connection, connector.codec), 60_000); // ever 60 seconds console.info("Creating webserver ..."); const api = new Koa(); @@ -112,7 +119,7 @@ export async function start(args: ReadonlyArray): Promise { const requestBody = context.request.body; const { address, ticker } = RequestParser.parseCreditBody(requestBody); - if (!codecImplementation().isValidAddress(address)) { + if (!isValidAddress(address)) { throw new HttpError(400, "Address is not in the expected format for this chain."); } @@ -131,7 +138,7 @@ export async function start(args: ReadonlyArray): Promise { tokenTicker: ticker, }; logSendJob(job); - await send(profile, connection, job); + await send(profile, connection, connector.codec, job); } catch (e) { console.error(e); throw new HttpError(500, "Sending tokens failed"); diff --git a/packages/faucet/src/addresses.ts b/packages/faucet/src/addresses.ts new file mode 100644 index 00000000..2b749e62 --- /dev/null +++ b/packages/faucet/src/addresses.ts @@ -0,0 +1,17 @@ +import { CosmWasmCodec } from "@cosmwasm/bcp"; +import { Address, Identity, TxCodec } from "@iov/bcp"; + +import * as constants from "./constants"; + +const noTokensCodec: Pick = new CosmWasmCodec( + constants.addressPrefix, + [], +); + +export function identityToAddress(identity: Identity): Address { + return noTokensCodec.identityToAddress(identity); +} + +export function isValidAddress(input: string): boolean { + return noTokensCodec.isValidAddress(input); +} diff --git a/packages/faucet/src/codec.spec.ts b/packages/faucet/src/codec.spec.ts deleted file mode 100644 index 624bf357..00000000 --- a/packages/faucet/src/codec.spec.ts +++ /dev/null @@ -1 +0,0 @@ -describe("codec", () => {}); diff --git a/packages/faucet/src/codec.ts b/packages/faucet/src/codec.ts deleted file mode 100644 index cffbfd67..00000000 --- a/packages/faucet/src/codec.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { CosmWasmCodec, CosmWasmConnection } from "@cosmwasm/bcp"; -import { TxCodec } from "@iov/bcp"; - -import { tokenConfig } from "./constants"; - -const prefix = "cosmos"; - -export async function establishConnection(url: string): Promise { - return CosmWasmConnection.establish(url, prefix, tokenConfig); -} - -export function codecImplementation(): TxCodec { - return new CosmWasmCodec(prefix, tokenConfig.bankTokens); -} diff --git a/packages/faucet/src/constants.ts b/packages/faucet/src/constants.ts index 1faaa975..6eb5c861 100644 --- a/packages/faucet/src/constants.ts +++ b/packages/faucet/src/constants.ts @@ -5,6 +5,7 @@ export const concurrency: number = Number.parseInt(process.env.FAUCET_CONCURRENC export const port: number = Number.parseInt(process.env.FAUCET_PORT || "", 10) || 8000; export const mnemonic: string | undefined = process.env.FAUCET_MNEMONIC; +export const addressPrefix = "cosmos"; export const tokenConfig: TokenConfiguration = { bankTokens: [ { diff --git a/packages/faucet/src/debugging.ts b/packages/faucet/src/debugging.ts index a5d7cf64..97a50f5f 100644 --- a/packages/faucet/src/debugging.ts +++ b/packages/faucet/src/debugging.ts @@ -1,7 +1,7 @@ import { Account, Amount } from "@iov/bcp"; import { Decimal } from "@iov/encoding"; -import { codecImplementation } from "./codec"; +import { identityToAddress } from "./addresses"; import { SendJob } from "./types"; /** A string representation of a coin in a human-readable format that can change at any time */ @@ -31,7 +31,7 @@ export function logAccountsState(accounts: ReadonlyArray): void { } export function logSendJob(job: SendJob): void { - const from = codecImplementation().identityToAddress(job.sender); + const from = identityToAddress(job.sender); const to = job.recipient; const amount = debugAmount(job.amount); console.info(`Sending ${amount} from ${from} to ${to} ...`); diff --git a/packages/faucet/src/faucet.ts b/packages/faucet/src/faucet.ts index b0427fe1..42d6cec8 100644 --- a/packages/faucet/src/faucet.ts +++ b/packages/faucet/src/faucet.ts @@ -1,5 +1,5 @@ import { TokenConfiguration } from "@cosmwasm/bcp"; -import { Account, Amount, BlockchainConnection, TokenTicker } from "@iov/bcp"; +import { Account, Amount, BlockchainConnection, TokenTicker, TxCodec } from "@iov/bcp"; import { Decimal, Uint53 } from "@iov/encoding"; import { UserProfile } from "@iov/keycontrol"; import { sleep } from "@iov/utils"; @@ -53,7 +53,7 @@ export class Faucet { return this.creditAmount(token, factor); } - public async refill(profile: UserProfile, connection: BlockchainConnection): Promise { + public async refill(profile: UserProfile, connection: BlockchainConnection, codec: TxCodec): Promise { console.info(`Connected to network: ${connection.chainId()}`); console.info(`Tokens on network: ${(await loadTokenTickers(connection)).join(", ")}`); @@ -87,7 +87,7 @@ export class Faucet { if (jobs.length > 0) { for (const job of jobs) { logSendJob(job); - await send(profile, connection, job); + await send(profile, connection, codec, job); await sleep(50); } diff --git a/packages/faucet/src/multichainhelpers.ts b/packages/faucet/src/multichainhelpers.ts index 3628e627..7791c6b4 100644 --- a/packages/faucet/src/multichainhelpers.ts +++ b/packages/faucet/src/multichainhelpers.ts @@ -6,10 +6,11 @@ import { isBlockInfoPending, SendTransaction, TokenTicker, + TxCodec, } from "@iov/bcp"; import { UserProfile } from "@iov/keycontrol"; -import { codecImplementation } from "./codec"; +import { identityToAddress } from "./addresses"; import { SendJob } from "./types"; export function identitiesOfFirstWallet(profile: UserProfile): ReadonlyArray { @@ -21,8 +22,7 @@ export async function loadAccounts( profile: UserProfile, connection: BlockchainConnection, ): Promise> { - const codec = codecImplementation(); - const addresses = identitiesOfFirstWallet(profile).map(identity => codec.identityToAddress(identity)); + const addresses = identitiesOfFirstWallet(profile).map(identity => identityToAddress(identity)); const out: Account[] = []; for (const address of addresses) { @@ -55,10 +55,9 @@ export async function loadTokenTickers( export async function send( profile: UserProfile, connection: BlockchainConnection, + codec: TxCodec, job: SendJob, ): Promise { - const codec = codecImplementation(); - const sendWithFee = await connection.withDefaultFee({ kind: "bcp/send", chainId: connection.chainId(), diff --git a/packages/faucet/src/profile.ts b/packages/faucet/src/profile.ts index b5c4b766..dea697b7 100644 --- a/packages/faucet/src/profile.ts +++ b/packages/faucet/src/profile.ts @@ -1,7 +1,7 @@ import { ChainId } from "@iov/bcp"; import { HdPaths, Secp256k1HdWallet, UserProfile } from "@iov/keycontrol"; -import { codecImplementation } from "./codec"; +import { identityToAddress } from "./addresses"; import * as constants from "./constants"; import { debugPath } from "./hdpaths"; @@ -24,7 +24,7 @@ export async function setSecretAndCreateIdentities( // log const role = i === 0 ? "token holder " : `distributor ${i}`; - const address = codecImplementation().identityToAddress(identity); + const address = identityToAddress(identity); console.info(`Created ${role} (${debugPath(path)}): ${address}`); } }