From 501829d9fc860a6be4a9872e6233abb761ab651c Mon Sep 17 00:00:00 2001 From: "Sebastian.Bor" Date: Thu, 8 Apr 2021 14:57:40 +0100 Subject: [PATCH] fix: validate new governance public key inputs --- packages/common/src/utils/utils.ts | 8 +++++ packages/proposals/src/constants/labels.ts | 8 +++++ .../src/views/governance/register.tsx | 29 +++++++++++++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/packages/common/src/utils/utils.ts b/packages/common/src/utils/utils.ts index 40975c0..85c3d25 100644 --- a/packages/common/src/utils/utils.ts +++ b/packages/common/src/utils/utils.ts @@ -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) => { diff --git a/packages/proposals/src/constants/labels.ts b/packages/proposals/src/constants/labels.ts index f0e3d71..34a1902 100644 --- a/packages/proposals/src/constants/labels.ts +++ b/packages/proposals/src/constants/labels.ts @@ -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: diff --git a/packages/proposals/src/views/governance/register.tsx b/packages/proposals/src/views/governance/register.tsx index 63a48bf..c25fb39 100644 --- a/packages/proposals/src/views/governance/register.tsx +++ b/packages/proposals/src/views/governance/register.tsx @@ -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,