UNPKG

@codegame.dev/wallet-cli

Version:

A CLI tool for managing wallets across multiple blockchains, supporting operations like wallet creation, balance checking, transfers, and fee estimation.

133 lines (120 loc) 4.61 kB
import { Command } from "commander" import { coinGeckoPlatformToTrustWalletCoinMap } from "../assets/coingecko"; import type { WalletCore } from "@trustwallet/wallet-core"; export const getCoin = (trustCore: WalletCore, coinId?: string, coingeckoPlatform?: string, required: boolean = true) => { const { CoinType } = trustCore if (required && !coinId && !coingeckoPlatform) { return [ null, { success: false, code: 'empty-coin', message: "Set coin name" } ] as const } const coinFromCoinGecko = coingeckoPlatform ? coinGeckoPlatformToTrustWalletCoinMap[coingeckoPlatform] : null if (coingeckoPlatform && !coinFromCoinGecko) { return [ null, { success: false, code: 'invalid-coingecko-platform', message: "This coingecko platform not supported" } ] as const } const coin = Object.keys(CoinType).slice(1).find(v => v == (coinFromCoinGecko || coinId)) as keyof typeof CoinType | null if (coinId && !coin) { return [ null, { success: false, code: 'invalid-coin', message: "This coin is not supported" } ] as const } return [coin || null, null] as const } export default (program: Command, trustCore: WalletCore) => { const { HDWallet, CoinType } = trustCore program.command('coin-list') .description('show list of all supported coins') .option('-s <text>', 'search in the list') .option('--json', 'output will be in json format') .action((values) => { const list = Object.keys(CoinType).slice(1).filter(v => v.toLowerCase().includes(values.s || '')) if (values.json) { console.log(JSON.stringify(list)) } else { console.log(list.join('\n')) } }) program.command('get-address') .description('get public address of a coin') .option('--mnemonic <mnemonic>', 'set an existing mnemonic') .option('--coin <coin>', 'set coin name based on trustwallet core') .option('--coingecko-platform <platform>', 'set platform name based on coingecko api') .option('--json', 'output will be in json format') .action((values) => { const [coin, error] = getCoin(trustCore, values.coin, values.coingeckoPlatform, true) if (error || !coin) { program.error(JSON.stringify({ ...error, json: values.json })) } let mnemonic = values.mnemonic || '' let address: string const wallet = HDWallet.createWithMnemonic(mnemonic, "") mnemonic = wallet.mnemonic() address = wallet.getAddressForCoin(CoinType[coin]) if (values.json) { console.log(JSON.stringify({ success: true, coin, mnemonic, address })) } else { console.log('Address:', address) } }) program.command('create-wallet') .description('create a new mnemonic') .option('--coin <coin>', 'set coin name based on trustwallet core') .option('--coingecko-platform <platform>', 'set platform name based on coingecko api') .option('--json', 'output will be in json format') .action((values) => { const [coin, error] = getCoin(trustCore, values.coin, values.coingeckoPlatform, false) if (error) { program.error(JSON.stringify({ ...error, json: values.json })) } let mnemonic: string let address: string | null = null const wallet = HDWallet.create(128, "") mnemonic = wallet.mnemonic() if (coin) { address = wallet.getAddressForCoin(CoinType[coin]) } if (values.json) { console.log(JSON.stringify({ success: true, coin, mnemonic, address })) } else { console.log('Mnemonic:', mnemonic) if (address) { console.log('Address:', address) } } }) }