Skip to content

Commit

Permalink
fix(cli): fix signer init strategy (#4987)
Browse files Browse the repository at this point in the history
### Description

Probably fixes an issue due to a recent pr merge that changes how
signers are initialized based on the command to be executed

### Drive-by changes

- Updates the key info message log level from info to debug to avoid the
following

![image](https://github.com/user-attachments/assets/3ad0d4f8-36b9-4e2f-8c28-199858f8ef0e)


### Related issues

### Backward compatibility

- Yes

### Testing

- Manual
  • Loading branch information
xeno097 authored Dec 12, 2024
1 parent 657ac92 commit c2ca849
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 44 deletions.
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()],
[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();
}
}
76 changes: 41 additions & 35 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,12 @@ 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 @@ -54,13 +54,11 @@ export class MultiChainResolver implements ChainResolver {
return this.resolveAgentChains(argv);
case ChainSelectionMode.STRATEGY:
return this.resolveStrategyChains(argv);
case ChainSelectionMode.RELAYER:
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);
}
}

Expand Down Expand Up @@ -119,28 +117,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 +127,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)]),
);
}

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

private async getWarpRouteConfigChains(
Expand Down Expand Up @@ -219,16 +217,20 @@ 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 {
return new MultiChainResolver(ChainSelectionMode.RELAYER);
return new MultiChainResolver(ChainSelectionMode.DEFAULT);
}

static forStrategyConfig(): MultiChainResolver {
Expand All @@ -246,4 +248,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

0 comments on commit c2ca849

Please sign in to comment.