Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): fix signer init strategy #4987

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hot-spies-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperlane-xyz/cli': minor
---

fix signer strategy init for broken cli commands
3 changes: 2 additions & 1 deletion typescript/cli/src/commands/signCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const SIGN_COMMANDS = [
export function isSignCommand(argv: any): boolean {
//TODO: fix reading and checking warp without signer, and remove this
const temporarySignCommandsCheck =
argv._[0] === 'warp' && (argv._[1] === 'read' || argv._[1] === 'check');
argv._[0] === 'warp' &&
(argv._[1] === 'read' || argv._[1] === 'check' || argv._[1] === 'verify');
return (
SIGN_COMMANDS.includes(argv._[0]) ||
(argv._.length > 1 && SIGN_COMMANDS.includes(argv._[1])) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CommandType } from '../../../commands/signCommands.js';

import { MultiChainResolver } from './MultiChainResolver.js';
import { SingleChainResolver } from './SingleChainResolver.js';
import { ChainResolver } from './types.js';

/**
Expand All @@ -11,13 +10,16 @@ import { ChainResolver } from './types.js';
export class ChainResolverFactory {
private static strategyMap: Map<CommandType, () => ChainResolver> = new Map([
[CommandType.WARP_DEPLOY, () => MultiChainResolver.forWarpRouteConfig()],
[CommandType.WARP_SEND, () => MultiChainResolver.forOriginDestination()],
// Using the forRelayer resolver because warp send allows the user to self relay the tx
[CommandType.WARP_SEND, () => MultiChainResolver.forRelayer()],
xeno097 marked this conversation as resolved.
Show resolved Hide resolved
[CommandType.WARP_APPLY, () => MultiChainResolver.forWarpRouteConfig()],
[CommandType.WARP_READ, () => MultiChainResolver.forWarpCoreConfig()],
[CommandType.WARP_CHECK, () => MultiChainResolver.forWarpCoreConfig()],
[CommandType.SEND_MESSAGE, () => MultiChainResolver.forOriginDestination()],
// Using the forRelayer resolver because send allows the user to self relay the tx
[CommandType.SEND_MESSAGE, () => MultiChainResolver.forRelayer()],
[CommandType.AGENT_KURTOSIS, () => MultiChainResolver.forAgentKurtosis()],
[CommandType.STATUS, () => MultiChainResolver.forOriginDestination()],
// Using the forRelayer resolver because status allows the user to self relay the tx
[CommandType.STATUS, () => MultiChainResolver.forRelayer()],
[CommandType.SUBMIT, () => MultiChainResolver.forStrategyConfig()],
[CommandType.RELAYER, () => MultiChainResolver.forRelayer()],
[CommandType.CORE_APPLY, () => MultiChainResolver.forCoreApply()],
Expand All @@ -30,7 +32,7 @@ export class ChainResolverFactory {
static getStrategy(argv: Record<string, any>): ChainResolver {
const commandKey = `${argv._[0]}:${argv._[1] || ''}`.trim() as CommandType;
const createStrategy =
this.strategyMap.get(commandKey) || (() => new SingleChainResolver());
this.strategyMap.get(commandKey) || (() => MultiChainResolver.default());
return createStrategy();
}
}
71 changes: 40 additions & 31 deletions typescript/cli/src/context/strategies/chain/MultiChainResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
DeployedCoreAddresses,
DeployedCoreAddressesSchema,
EvmCoreModule,
MultiProvider,
} from '@hyperlane-xyz/sdk';
import { assert } from '@hyperlane-xyz/utils';
import { ProtocolType, assert } from '@hyperlane-xyz/utils';

import { DEFAULT_WARP_ROUTE_DEPLOYMENT_CONFIG_PATH } from '../../../commands/options.js';
import { readCoreDeployConfigs } from '../../../config/core.js';
Expand All @@ -26,13 +27,13 @@ import { getWarpCoreConfigOrExit } from '../../../utils/warp.js';
import { ChainResolver } from './types.js';

enum ChainSelectionMode {
ORIGIN_DESTINATION,
AGENT_KURTOSIS,
WARP_CONFIG,
WARP_READ,
STRATEGY,
RELAYER,
CORE_APPLY,
DEFAULT,
}

// This class could be broken down into multiple strategies
Expand All @@ -58,9 +59,9 @@ export class MultiChainResolver implements ChainResolver {
return this.resolveRelayerChains(argv);
case ChainSelectionMode.CORE_APPLY:
return this.resolveCoreApplyChains(argv);
case ChainSelectionMode.ORIGIN_DESTINATION:
case ChainSelectionMode.DEFAULT:
default:
return this.resolveOriginDestinationChains(argv);
return this.resolveRelayerChains(argv);
xeno097 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -119,28 +120,6 @@ export class MultiChainResolver implements ChainResolver {
return [argv.origin, ...argv.targets];
}

private async resolveOriginDestinationChains(
argv: Record<string, any>,
): Promise<ChainName[]> {
const { chainMetadata } = argv.context;

argv.origin =
argv.origin ??
(await runSingleChainSelectionStep(
chainMetadata,
'Select the origin chain',
));

argv.destination =
argv.destination ??
(await runSingleChainSelectionStep(
chainMetadata,
'Select the destination chain',
));

return [argv.origin, argv.destination];
}

private async resolveStrategyChains(
argv: Record<string, any>,
): Promise<ChainName[]> {
Expand All @@ -151,7 +130,29 @@ export class MultiChainResolver implements ChainResolver {
private async resolveRelayerChains(
argv: Record<string, any>,
): Promise<ChainName[]> {
return argv.chains.split(',').map((item: string) => item.trim());
const { multiProvider } = argv.context;
const chains = [];

if (argv.origin) {
chains.push(argv.origin);
}

if (argv.destination) {
chains.push(argv.destination);
}

if (!argv.chains) {
return Array.from(
new Set([...chains, ...this.getEvmChains(multiProvider)]),
);
}
xeno097 marked this conversation as resolved.
Show resolved Hide resolved

return Array.from(
new Set([
...chains,
...argv.chains.split(',').map((item: string) => item.trim()),
]),
);
}

private async getWarpRouteConfigChains(
Expand Down Expand Up @@ -219,12 +220,16 @@ export class MultiChainResolver implements ChainResolver {
}
}

static forAgentKurtosis(): MultiChainResolver {
return new MultiChainResolver(ChainSelectionMode.AGENT_KURTOSIS);
private getEvmChains(multiProvider: MultiProvider): ChainName[] {
const chains = multiProvider.getKnownChainNames();

return chains.filter(
(chain) => multiProvider.getProtocol(chain) === ProtocolType.Ethereum,
);
}

static forOriginDestination(): MultiChainResolver {
return new MultiChainResolver(ChainSelectionMode.ORIGIN_DESTINATION);
static forAgentKurtosis(): MultiChainResolver {
return new MultiChainResolver(ChainSelectionMode.AGENT_KURTOSIS);
}

static forRelayer(): MultiChainResolver {
Expand All @@ -246,4 +251,8 @@ export class MultiChainResolver implements ChainResolver {
static forCoreApply(): MultiChainResolver {
return new MultiChainResolver(ChainSelectionMode.CORE_APPLY);
}

static default(): MultiChainResolver {
return new MultiChainResolver(ChainSelectionMode.DEFAULT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ export class MultiProtocolSignerManager {
let privateKey: string;

if (this.options.key) {
this.logger.info(
this.logger.debug(
`Using private key passed via CLI --key flag for chain ${chain}`,
);
privateKey = this.options.key;
} else if (ENV.HYP_KEY) {
this.logger.info(`Using private key from .env for chain ${chain}`);
this.logger.debug(`Using private key from .env for chain ${chain}`);
privateKey = ENV.HYP_KEY;
} else {
privateKey = await this.extractPrivateKey(chain, signerStrategy);
Expand All @@ -145,7 +145,7 @@ export class MultiProtocolSignerManager {
`No private key found for chain ${chain}`,
);

this.logger.info(
this.logger.debug(
`Extracting private key from strategy config/user prompt for chain ${chain}`,
);
return strategyConfig.privateKey;
Expand Down
Loading