From b156d68dea5458ec2f343f77ebf009a75f20341f Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 22 Jul 2021 17:18:13 +0200 Subject: [PATCH] Make FAUCET_PATH_PATTERN configurable --- CHANGELOG.md | 4 ++++ packages/faucet/README.md | 4 ++++ packages/faucet/src/actions/generate.ts | 6 +++++- packages/faucet/src/actions/help.ts | 4 ++++ packages/faucet/src/actions/start.ts | 3 +++ packages/faucet/src/constants.ts | 1 + packages/faucet/src/faucet.spec.ts | 22 +++++++++++++++++++++- packages/faucet/src/faucet.ts | 11 ++++++++++- packages/faucet/src/profile.ts | 6 ++++-- 9 files changed, 56 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index edcf771656..5b4ca7e337 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,10 @@ and this project adheres to - @cosmjs/cosmwasm-stargate: Copy symbols `Code`, `CodeDetails`, `Contract`, `ContractCodeHistoryEntry` and `JsonObject` from @cosmjs/cosmwasm-launchpad and remove dependency on @cosmjs/cosmwasm-launchpad. +- @cosmjs/faucet: Add new configuration variable `FAUCET_PATH_PATTERN` to + configure the HD path of the faucet accounts ([#832]). + +[#832]: https://github.com/cosmos/cosmjs/issues/832 ### Changed diff --git a/packages/faucet/README.md b/packages/faucet/README.md index a58ef6adab..1af4b071a8 100644 --- a/packages/faucet/README.md +++ b/packages/faucet/README.md @@ -52,6 +52,10 @@ FAUCET_GAS_PRICE Gas price for transactions as a comma separated list. FAUCET_GAS_LIMIT Gas limit for send transactions. Defaults to 80000. FAUCET_MNEMONIC Secret mnemonic that serves as the base secret for the faucet HD accounts +FAUCET_PATH_PATTERN The pattern of BIP32 paths for the faucet accounts. + Must contain one "a" placeholder that is replaced with + the account index. + Defaults to the Cosmos Hub path "m/44'/118'/0'/0/a". FAUCET_ADDRESS_PREFIX The bech32 address prefix. Defaults to "cosmos". FAUCET_TOKENS A comma separated list of token denoms, e.g. "uatom" or "ucosm, mstake". diff --git a/packages/faucet/src/actions/generate.ts b/packages/faucet/src/actions/generate.ts index a7720b7589..5f90494d96 100644 --- a/packages/faucet/src/actions/generate.ts +++ b/packages/faucet/src/actions/generate.ts @@ -1,6 +1,7 @@ import { Bip39, Random } from "@cosmjs/crypto"; import * as constants from "../constants"; +import { makePathBuilder } from "../pathbuilder"; import { createWallets } from "../profile"; export async function generate(args: readonly string[]): Promise { @@ -13,6 +14,9 @@ export async function generate(args: readonly string[]): Promise { const mnemonic = Bip39.encode(Random.getBytes(16)).toString(); console.info(`FAUCET_MNEMONIC="${mnemonic}"`); + const pathBuilder = makePathBuilder(constants.pathPattern); + console.info(`FAUCET_PATH_PATTERN="${constants.pathPattern}"`); + // Log the addresses - await createWallets(mnemonic, constants.addressPrefix, constants.concurrency, true); + await createWallets(mnemonic, pathBuilder, constants.addressPrefix, constants.concurrency, true); } diff --git a/packages/faucet/src/actions/help.ts b/packages/faucet/src/actions/help.ts index 74cc23b919..31834d2e7d 100644 --- a/packages/faucet/src/actions/help.ts +++ b/packages/faucet/src/actions/help.ts @@ -25,6 +25,10 @@ FAUCET_GAS_PRICE Gas price for transactions as a comma separated list. FAUCET_GAS_LIMIT Gas limit for send transactions. Defaults to 80000. FAUCET_MNEMONIC Secret mnemonic that serves as the base secret for the faucet HD accounts +FAUCET_PATH_PATTERN The pattern of BIP32 paths for the faucet accounts. + Must contain one "a" placeholder that is replaced with + the account index. + Defaults to the Cosmos Hub path "m/44'/118'/0'/0/a". FAUCET_ADDRESS_PREFIX The bech32 address prefix. Defaults to "cosmos". FAUCET_TOKENS A comma separated list of token denoms, e.g. "uatom" or "ucosm, mstake". diff --git a/packages/faucet/src/actions/start.ts b/packages/faucet/src/actions/start.ts index 6d0d6e6e4e..71497687bf 100644 --- a/packages/faucet/src/actions/start.ts +++ b/packages/faucet/src/actions/start.ts @@ -5,6 +5,7 @@ import { Webserver } from "../api/webserver"; import * as constants from "../constants"; import { logAccountsState } from "../debugging"; import { Faucet } from "../faucet"; +import { makePathBuilder } from "../pathbuilder"; export async function start(args: readonly string[]): Promise { if (args.length < 1) { @@ -29,11 +30,13 @@ export async function start(args: readonly string[]): Promise { // Faucet if (!constants.mnemonic) throw new Error("The FAUCET_MNEMONIC environment variable is not set"); const logging = true; + const pathBuilder = makePathBuilder(constants.pathPattern); const faucet = await Faucet.make( blockchainBaseUrl, constants.addressPrefix, constants.tokenConfig, constants.mnemonic, + pathBuilder, constants.concurrency, stargate, logging, diff --git a/packages/faucet/src/constants.ts b/packages/faucet/src/constants.ts index e8be3084d9..a88b65a920 100644 --- a/packages/faucet/src/constants.ts +++ b/packages/faucet/src/constants.ts @@ -13,6 +13,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 = process.env.FAUCET_ADDRESS_PREFIX || "cosmos"; +export const pathPattern = process.env.FAUCET_PATH_PATTERN || "m/44'/118'/0'/0/a"; export const tokenConfig: TokenConfiguration = { bankTokens: parseBankTokens(process.env.FAUCET_TOKENS || "ucosm, ustake"), }; diff --git a/packages/faucet/src/faucet.spec.ts b/packages/faucet/src/faucet.spec.ts index 14f285adf1..1311534703 100644 --- a/packages/faucet/src/faucet.spec.ts +++ b/packages/faucet/src/faucet.spec.ts @@ -1,7 +1,7 @@ import { Random } from "@cosmjs/crypto"; import { Bech32 } from "@cosmjs/encoding"; import { CosmosClient } from "@cosmjs/launchpad"; -import { StargateClient } from "@cosmjs/stargate"; +import { makeCosmoshubPath, StargateClient } from "@cosmjs/stargate"; import { assert } from "@cosmjs/utils"; import { Faucet } from "./faucet"; @@ -32,6 +32,8 @@ const faucetMnemonic = "economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone"; describe("Faucet", () => { + const pathBuilder = makeCosmoshubPath; + describe("launchpad", () => { const apiUrl = "http://localhost:1317"; const stargate = false; @@ -44,6 +46,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -59,6 +62,7 @@ describe("Faucet", () => { defaultAddressPrefix, { bankTokens: [] }, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -73,6 +77,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -89,6 +94,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -122,6 +128,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -150,6 +157,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -174,6 +182,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -200,6 +209,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -216,6 +226,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 1, stargate, ); @@ -262,6 +273,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -277,6 +289,7 @@ describe("Faucet", () => { defaultAddressPrefix, { bankTokens: [] }, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -291,6 +304,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -307,6 +321,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -340,6 +355,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -368,6 +384,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -392,6 +409,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -418,6 +436,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 3, stargate, ); @@ -434,6 +453,7 @@ describe("Faucet", () => { defaultAddressPrefix, defaultTokenConfig, faucetMnemonic, + pathBuilder, 1, stargate, ); diff --git a/packages/faucet/src/faucet.ts b/packages/faucet/src/faucet.ts index 354a5d06c4..79c143cf54 100644 --- a/packages/faucet/src/faucet.ts +++ b/packages/faucet/src/faucet.ts @@ -13,6 +13,7 @@ import { sleep } from "@cosmjs/utils"; import * as constants from "./constants"; import { debugAccount, logAccountsState, logSendJob } from "./debugging"; +import { PathBuilder } from "./pathbuilder"; import { createClients, createWallets } from "./profile"; import { TokenConfiguration, TokenManager } from "./tokenmanager"; import { MinimalAccount, SendJob } from "./types"; @@ -27,11 +28,19 @@ export class Faucet { addressPrefix: string, config: TokenConfiguration, mnemonic: string, + pathBuilder: PathBuilder, numberOfDistributors: number, stargate = true, logging = false, ): Promise { - const wallets = await createWallets(mnemonic, addressPrefix, numberOfDistributors, stargate, logging); + const wallets = await createWallets( + mnemonic, + pathBuilder, + addressPrefix, + numberOfDistributors, + stargate, + logging, + ); const clients = await createClients(apiUrl, wallets); const readonlyClient = stargate ? await StargateClient.connect(apiUrl) : new CosmosClient(apiUrl); return new Faucet(addressPrefix, config, clients, readonlyClient, logging); diff --git a/packages/faucet/src/profile.ts b/packages/faucet/src/profile.ts index 60ba24e149..396e359eb8 100644 --- a/packages/faucet/src/profile.ts +++ b/packages/faucet/src/profile.ts @@ -1,12 +1,14 @@ import { pathToString } from "@cosmjs/crypto"; -import { makeCosmoshubPath, Secp256k1HdWallet, SigningCosmosClient } from "@cosmjs/launchpad"; +import { Secp256k1HdWallet, SigningCosmosClient } from "@cosmjs/launchpad"; import { DirectSecp256k1HdWallet, isOfflineDirectSigner, OfflineSigner } from "@cosmjs/proto-signing"; import { SigningStargateClient } from "@cosmjs/stargate"; import * as constants from "./constants"; +import { PathBuilder } from "./pathbuilder"; export async function createWallets( mnemonic: string, + pathBuilder: PathBuilder, addressPrefix: string, numberOfDistributors: number, stargate = true, @@ -18,7 +20,7 @@ export async function createWallets( // first account is the token holder const numberOfIdentities = 1 + numberOfDistributors; for (let i = 0; i < numberOfIdentities; i++) { - const path = makeCosmoshubPath(i); + const path = pathBuilder(i); const wallet = await createWallet(mnemonic, { hdPaths: [path], prefix: addressPrefix }); const [{ address }] = await wallet.getAccounts(); if (logging) {