fix: validate new governance public key inputs

This commit is contained in:
Sebastian.Bor 2021-04-08 14:57:40 +01:00
parent be911a89f7
commit 501829d9fc
3 changed files with 43 additions and 2 deletions

View File

@ -163,6 +163,14 @@ export function fromLamports(
return (amount / precision) * rate;
}
export const tryParseKey = (key: string): PublicKey | null => {
try {
return new PublicKey(key);
} catch (error) {
return null;
}
};
var SI_SYMBOL = ['', 'k', 'M', 'G', 'T', 'P', 'E'];
const abbreviateNumber = (number: number, precision: number) => {

View File

@ -127,8 +127,16 @@ export const LABELS = {
SLOT_MUST_BE_NUMERIC: 'Slot can only be numeric',
SLOT_MUST_BE_GREATER_THAN: 'Slot must be greater than or equal to ',
DELAY: 'Slot Delay',
MIN_SLOT_MUST_BE_NUMERIC: 'Minimum Slot Waiting Period can only be numeric',
TIME_LIMIT_MUST_BE_NUMERIC: 'Time Limit can only be numeric',
PROGRAM_ID_IS_NOT_A_VALID_PUBLIC_KEY: (programId: string) =>
`Program ID: '${programId}' is not a valid public key`,
GOVERNANCE_MINT_IS_NOT_A_VALID_PUBLIC_KEY: (programId: string) =>
`Governance Mint ID: '${programId}' is not a valid public key`,
COUNCIL_MINT_IS_NOT_A_VALID_PUBLIC_KEY: (programId: string) =>
`Council Mint ID: '${programId}' is not a valid public key`,
TIME_LIMIT: 'Voting Time Limit',
THIS_CONFIG_LACKS_COUNCIL: 'This program does not have a council.',
GIT_CONTENT_EXCEEDED:

View File

@ -10,9 +10,8 @@ import {
VotingEntryRule,
ZERO_KEY,
} from '../../models/timelock';
import { Link } from 'react-router-dom';
import { LABELS } from '../../constants';
import { contexts, utils } from '@oyster/common';
import { contexts, utils, tryParseKey } from '@oyster/common';
import { registerProgramGovernance } from '../../actions/registerProgramGovernance';
import { Redirect } from 'react-router';
import BN from 'bn.js';
@ -96,6 +95,32 @@ export function NewForm({
});
return;
}
if (!tryParseKey(values.program)) {
notify({
message: LABELS.PROGRAM_ID_IS_NOT_A_VALID_PUBLIC_KEY(values.program),
type: 'error',
});
return;
}
if (values.governanceMint && !tryParseKey(values.governanceMint)) {
notify({
message: LABELS.GOVERNANCE_MINT_IS_NOT_A_VALID_PUBLIC_KEY(
values.governanceMint,
),
type: 'error',
});
return;
}
if (values.councilMint && !tryParseKey(values.councilMint)) {
notify({
message: LABELS.COUNCIL_MINT_IS_NOT_A_VALID_PUBLIC_KEY(
values.councilMint,
),
type: 'error',
});
return;
}
const uninitializedConfig = {
timelockType: values.timelockType,
executionType: values.executionType,