|
| 1 | +import prompts from 'prompts'; |
| 2 | + |
| 3 | +import { ProtocolType } from '../../../utils/dist/types.js'; |
| 4 | +import { getChain } from '../../config/registry.js'; |
1 | 5 | import { getAllCloudAgentKeys } from '../../src/agents/key-utils.js'; |
2 | 6 | import { getArgs, withContext, withProtocol } from '../agent-utils.js'; |
3 | 7 | import { getConfigsBasedOnArgs } from '../core-utils.js'; |
4 | 8 |
|
| 9 | +function getKeyArgs() { |
| 10 | + return withProtocol(withContext(getArgs())) |
| 11 | + .alias('p', 'protocol') |
| 12 | + .option('bech32Prefix', { |
| 13 | + type: 'string', |
| 14 | + description: 'The bech32 prefix for the Cosmos address', |
| 15 | + }) |
| 16 | + .alias('b', 'bech32Prefix'); |
| 17 | +} |
| 18 | + |
5 | 19 | async function main() { |
6 | | - const argv = await withProtocol(withContext(getArgs())).argv; |
| 20 | + const argv = await getKeyArgs().argv; |
| 21 | + const { agentConfig, envConfig } = await getConfigsBasedOnArgs(argv); |
7 | 22 |
|
8 | | - const { agentConfig } = await getConfigsBasedOnArgs(argv); |
| 23 | + if (argv.protocol === 'cosmos' || argv.protocol === 'cosmosnative') { |
| 24 | + if (!argv.bech32Prefix) { |
| 25 | + const bech32PrefixMap = envConfig.supportedChainNames.reduce< |
| 26 | + Record<string, string> |
| 27 | + >((acc, chainName) => { |
| 28 | + const chain = getChain(chainName); |
| 29 | + if ( |
| 30 | + chain && |
| 31 | + (chain.protocol === ProtocolType.Cosmos || |
| 32 | + chain.protocol === ProtocolType.CosmosNative) && |
| 33 | + chain.bech32Prefix |
| 34 | + ) { |
| 35 | + acc[chainName] = chain.bech32Prefix; |
| 36 | + } |
| 37 | + return acc; |
| 38 | + }, {}); |
| 39 | + |
| 40 | + const response = await prompts({ |
| 41 | + type: 'select', |
| 42 | + name: 'bech32Prefix', |
| 43 | + message: 'Select the bech32 prefix for Cosmos address:', |
| 44 | + choices: Object.entries(bech32PrefixMap).map(([chainName, prefix]) => ({ |
| 45 | + title: chainName, |
| 46 | + value: prefix, |
| 47 | + })), |
| 48 | + }); |
| 49 | + argv.bech32Prefix = response.bech32Prefix; |
| 50 | + } |
| 51 | + } |
9 | 52 |
|
10 | 53 | const keys = getAllCloudAgentKeys(agentConfig); |
11 | 54 | const keyInfoPromises = keys.map(async (key) => { |
12 | 55 | let address = undefined; |
13 | 56 | try { |
14 | 57 | await key.fetch(); |
15 | | - address = key.addressForProtocol(argv.protocol); |
| 58 | + address = key.addressForProtocol(argv.protocol, argv.bech32Prefix); |
16 | 59 | } catch (e) { |
17 | 60 | // Swallow error |
18 | 61 | console.error('Error getting address', { key: key.identifier, e }); |
|
0 commit comments