Skip to content

Commit

Permalink
check address validity
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-1979 committed Dec 5, 2023
1 parent ef1de64 commit 6058d51
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/dapp/src/TransferFund.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TxResult, send } from './apis/send';
import { amountToMachine } from './util/utils';
import { buildPayload } from './util/buildPayload';
import subscan from './assets/subscan.svg';
import isValidAddress from './util/isValidAddress';

interface Props {
api: ApiPromise | undefined;
Expand All @@ -31,11 +32,14 @@ function TransferFund({ api, account, balances, currentChainName, formatted, isP

const handleToAddress = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
event?.target?.value && setToAddress(event.target.value);
const mayBeAddress = event?.target?.value
if (isValidAddress(mayBeAddress)) {
setToAddress(event.target.value);
}
}, [],);

const handleAmount = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
if (isNaN((Number(event?.target?.value)))){
if (isNaN((Number(event?.target?.value)))) {
return
}

Expand Down
16 changes: 16 additions & 0 deletions packages/dapp/src/util/isValidAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { decodeAddress, encodeAddress } from '@polkadot/keyring';
import { hexToU8a, isHex } from '@polkadot/util';

export default function isValidAddress(_address: string | undefined): boolean {
try {
encodeAddress(
isHex(_address)
? hexToU8a(_address)
: decodeAddress(_address)
);

return true;
} catch (error) {
return false;
}
}

0 comments on commit 6058d51

Please sign in to comment.