Fix key funder (#1048)

pull/1058/head
Trevor Porter 2 years ago committed by GitHub
parent a067a6235f
commit 4620d2ae88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      typescript/infra/scripts/funding/fund-keys-from-deployer.ts
  2. 44
      typescript/infra/src/agents/agent.ts
  3. 51
      typescript/infra/src/agents/keys.ts

@ -3,6 +3,7 @@ import { Gauge, Registry } from 'prom-client';
import { format } from 'util';
import {
AllChains,
ChainConnection,
ChainName,
CompleteChainMap,
@ -11,6 +12,7 @@ import {
import { error, log } from '@abacus-network/utils';
import { Contexts } from '../../config/contexts';
import { parseKeyIdentifier } from '../../src/agents/agent';
import { getAllCloudAgentKeys } from '../../src/agents/key-utils';
import {
BaseCloudAgentKey,
@ -187,12 +189,23 @@ class ContextFunder {
path,
});
const idsAndAddresses = readJSONAtPath(path);
const keys: BaseCloudAgentKey[] = idsAndAddresses.map((idAndAddress: any) =>
ReadOnlyCloudAgentKey.fromSerializedAddress(
idAndAddress.identifier,
idAndAddress.address,
),
);
const keys: BaseCloudAgentKey[] = idsAndAddresses
.filter((idAndAddress: any) => {
const parsed = parseKeyIdentifier(idAndAddress.identifier);
// Filter out any invalid chain names. This can happen if we're running an old
// version of this script but the list of identifiers (expected to be stored in GCP secrets)
// references newer chains.
return (
parsed.chainName === undefined ||
AllChains.includes(parsed.chainName as ChainName)
);
})
.map((idAndAddress: any) =>
ReadOnlyCloudAgentKey.fromSerializedAddress(
idAndAddress.identifier,
idAndAddress.address,
),
);
// TODO: Why do we need to cast here?
const context = keys[0].context as Contexts;

@ -52,3 +52,47 @@ export function userIdentifier(
) {
return identifier(false, environment, context, role, chainName, index);
}
// Doesn't perform any checks on whether the parsed values are valid,
// this is left to the caller.
export function parseKeyIdentifier(identifier: string): {
environment: string;
context: string;
role: string;
chainName?: string;
index?: number;
} {
const regex =
/(alias\/)?([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-key-([a-zA-Z0-9]+)-?([a-zA-Z0-9]+)?-?([0-9]+)?/g;
const matches = regex.exec(identifier);
if (!matches) {
throw Error('Invalid identifier');
}
const context = matches[2];
const environment = matches[3];
// If matches[5] is undefined, this key doesn't have a chainName, and matches[4]
// is the role name.
if (matches[5] === undefined) {
return {
environment,
context,
role: matches[4],
};
} else if (matches[6] === undefined) {
// If matches[6] is undefined, this key doesn't have an index.
return {
environment,
context,
role: matches[5],
chainName: matches[4],
};
}
return {
environment,
context,
role: matches[5],
chainName: matches[4],
index: parseInt(matches[6]),
};
}

@ -5,6 +5,7 @@ import { ChainName } from '@abacus-network/sdk';
import { Contexts } from '../../config/contexts';
import { assertChain, assertContext, assertRole } from '../utils/utils';
import { parseKeyIdentifier } from './agent';
import { KEY_ROLE_ENUM } from './roles';
// Base class to represent keys used to run Abacus agents.
@ -100,45 +101,17 @@ export class ReadOnlyCloudAgentKey extends BaseCloudAgentKey {
identifier: string,
address: string,
): ReadOnlyCloudAgentKey {
const regex =
/(alias\/)?([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-key-([a-zA-Z0-9]+)-?([a-zA-Z0-9]+)?-?([0-9]+)?/g;
const matches = regex.exec(identifier);
if (!matches) {
throw Error('Invalid identifier');
}
const context = assertContext(matches[2]);
const environment = matches[3];
const { environment, context, role, chainName, index } =
parseKeyIdentifier(identifier);
// If matches[5] is undefined, this key doesn't have a chainName, and matches[4]
// is the role name.
if (matches[5] === undefined) {
return new ReadOnlyCloudAgentKey(
environment,
context,
assertRole(matches[4]),
identifier,
address,
);
} else if (matches[6] === undefined) {
// If matches[6] is undefined, this key doesn't have an index.
return new ReadOnlyCloudAgentKey(
environment,
context,
assertRole(matches[5]),
identifier,
address,
assertChain(matches[4]),
);
} else {
return new ReadOnlyCloudAgentKey(
environment,
context,
assertRole(matches[5]),
identifier,
address,
assertChain(matches[4]),
parseInt(matches[6]),
);
}
return new ReadOnlyCloudAgentKey(
environment,
assertContext(context),
assertRole(role),
identifier,
address,
chainName ? assertChain(chainName) : undefined,
index,
);
}
}

Loading…
Cancel
Save