Skip to content

Commit

Permalink
Merge pull request #18 from webmaster128/advanced-address-checks
Browse files Browse the repository at this point in the history
Implement more advanced address checks
  • Loading branch information
samepant authored Jan 12, 2022
2 parents d24abd0 + d302119 commit c6552a0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
36 changes: 19 additions & 17 deletions components/forms/TransactionForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { withRouter } from "next/router";
import Button from "../../components/inputs/Button";
import Input from "../../components/inputs/Input";
import StackableContainer from "../layout/StackableContainer";
import { exampleAddress } from "../../lib/displayHelpers";
import { checkAddress, exampleAddress } from "../../lib/displayHelpers";

class TransactionForm extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -56,23 +56,25 @@ class TransactionForm extends React.Component {
};

handleCreate = async () => {
if (this.state.toAddress.length === 45) {
this.setState({ processing: true });
const tx = this.createTransaction(
this.state.toAddress,
this.state.amount,
this.state.gas
);
console.log(tx);
const dataJSON = JSON.stringify(tx);
const res = await axios.post("/api/transaction", { dataJSON });
const { transactionID } = res.data;
this.props.router.push(
`${this.props.address}/transaction/${transactionID}`
);
} else {
this.setState({ addressError: "Use a valid cosmos-hub address" });
const addressError = checkAddress(this.state.toAddress);
if (addressError) {
this.setState({ addressError: `Invalid address for network ${process.env.NEXT_PUBLIC_CHAIN_ID}: ${addressError}` });
return;
}

this.setState({ processing: true });
const tx = this.createTransaction(
this.state.toAddress,
this.state.amount,
this.state.gas
);
console.log(tx);
const dataJSON = JSON.stringify(tx);
const res = await axios.post("/api/transaction", { dataJSON });
const { transactionID } = res.data;
this.props.router.push(
`${this.props.address}/transaction/${transactionID}`
);
};

render() {
Expand Down
29 changes: 28 additions & 1 deletion lib/displayHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,31 @@ const examplePubkey = (index) => {
return toBase64(data);
}

export { abbreviateLongString, printableCoin, printableCoins, exampleAddress, examplePubkey };
/**
* Returns an error message for invalid addresses.
*
* Returns null of there is no error.
*/
const checkAddress = (input) => {
if (!input) return "Empty";

let data;
let prefix;
try {
({ data, prefix } = Bech32.decode(input));
} catch (error) {
return error.toString();
}

if (prefix !== process.env.NEXT_PUBLIC_ADDRESS_PREFIX) {
return `Expected address prefix '${process.env.NEXT_PUBLIC_ADDRESS_PREFIX}' but got '${prefix}'`;
}

if (data.length !== 20) {
return "Invalid address length in bech32 data. Must be 20 bytes.";
}

return null;
}

export { abbreviateLongString, printableCoin, printableCoins, exampleAddress, examplePubkey, checkAddress };

0 comments on commit c6552a0

Please sign in to comment.