cosmic-interchain-cli
Version:
A command-line utility for Cosmic Wire's interchain messaging protocol
77 lines • 2.61 kB
JavaScript
import { input } from '@inquirer/prompts';
import { ethers } from 'ethers';
import { impersonateAccount } from '@hyperlane-xyz/sdk';
import { ensure0x } from '@hyperlane-xyz/utils';
const ETHEREUM_ADDRESS_LENGTH = 42;
/**
* Retrieves a signer for the current command-context.
* @returns the signer
*/
export async function getSigner({ key, skipConfirmation, }) {
key ||= await retrieveKey(skipConfirmation);
const signer = privateKeyToSigner(key);
return { key, signer };
}
/**
* Retrieves an impersonated signer for the current command-context.
* @returns the impersonated signer
*/
export async function getImpersonatedSigner({ fromAddress, key, skipConfirmation, }) {
if (!fromAddress) {
const { signer } = await getSigner({ key, skipConfirmation });
fromAddress = signer.address;
}
return {
impersonatedKey: fromAddress,
impersonatedSigner: await addressToImpersonatedSigner(fromAddress),
};
}
/**
* Verifies the specified signer is valid.
* @param signer the signer to verify
*/
export function assertSigner(signer) {
if (!signer || !ethers.Signer.isSigner(signer))
throw new Error('Signer is invalid');
}
/**
* Generates a signer from an address.
* @param address an EOA address
* @returns a signer for the address
*/
async function addressToImpersonatedSigner(address) {
if (!address)
throw new Error('No address provided');
const formattedKey = address.trim().toLowerCase();
if (address.length != ETHEREUM_ADDRESS_LENGTH)
throw new Error('Invalid address length.');
else if (ethers.utils.isHexString(ensure0x(formattedKey)))
return impersonateAccount(address);
else
throw new Error('Invalid address format');
}
/**
* Generates a signer from a private key.
* @param key a private key
* @returns a signer for the private key
*/
function privateKeyToSigner(key) {
if (!key)
throw new Error('No private key provided');
const formattedKey = key.trim().toLowerCase();
if (ethers.utils.isHexString(ensure0x(formattedKey)))
return new ethers.Wallet(ensure0x(formattedKey));
else if (formattedKey.split(' ').length >= 6)
return ethers.Wallet.fromMnemonic(formattedKey);
else
throw new Error('Invalid private key format');
}
async function retrieveKey(skipConfirmation) {
if (skipConfirmation)
throw new Error(`No private key provided`);
else
return input({
message: `Please enter private key or use the HYP_KEY environment variable.`,
});
}
//# sourceMappingURL=keys.js.map