Skip to content

Commit

Permalink
improve bytes32 error message
Browse files Browse the repository at this point in the history
  • Loading branch information
gsteenkamp89 committed Apr 10, 2024
1 parent 5aa9fa7 commit 4954ca9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/plugins/oSnap/components/Input/MethodParameter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isAddress } from '@ethersproject/address';
import { isBigNumberish } from '@ethersproject/bignumber/lib/bignumber';
import AddressInput from './Address.vue';
import { hexZeroPad, isBytesLike } from '@ethersproject/bytes';
import { isBytesLikeSafe } from '../../utils';

const props = defineProps<{
parameter: ParamType;
Expand Down Expand Up @@ -53,7 +54,7 @@ const errorMessageForDisplay = computed(() => {
});

const allowQuickFixForBytes32 = computed(() => {
if (!errorMessageForDisplay?.value?.includes('long')) {
if (errorMessageForDisplay?.value?.includes('short')) {
return true;
}
return false;
Expand Down Expand Up @@ -97,20 +98,24 @@ function validateBytes32Input(value: string) {
const data = value?.slice(2) || '';

if (data.length < 64) {
validationErrorMessage.value = 'Value too short';
throw new Error('Less than 32 bytes');
const padded = hexZeroPad(value, 32);
if (isBytesLikeSafe(padded)) {
validationErrorMessage.value = 'Value too short';
return false;
}
}

if (data.length > 64) {
validationErrorMessage.value = 'Value too long';
throw new Error('More than 32 bytes');
return false;
}

if (!isBytesLike(value)) {
throw new Error('Invalid bytes32');
if (!isBytesLikeSafe(value)) {
validationErrorMessage.value = undefined;
return false;
}
return true;
} catch {
validationErrorMessage.value = undefined;
return false;
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/plugins/oSnap/utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import memoize from 'lodash/memoize';
import { Contract } from '@ethersproject/contracts';
import { isBigNumberish } from '@ethersproject/bignumber/lib/bignumber';
import { isHexString } from '@ethersproject/bytes';
import { isBytesLike, isHexString } from '@ethersproject/bytes';
import getProvider from '@snapshot-labs/snapshot.js/src/utils/provider';
import { OPTIMISTIC_GOVERNOR_ABI } from '../constants';
import { BaseTransaction, NFT, Token, Transaction, GnosisSafe } from '../types';
Expand Down Expand Up @@ -119,6 +119,14 @@ export function amountPositive(amount: string, decimals = 18) {
}
}

export function isBytesLikeSafe(value: string): boolean {
try {
return isBytesLike(value);
} catch {
return false;
}
}

export function allTransactionsValid(transactions: Transaction[]): boolean {
return transactions.every(tx => tx.isValid === true);
}
Expand Down

0 comments on commit 4954ca9

Please sign in to comment.