Merge pull request #18 from webmaster128/advanced-address-checks
Implement more advanced address checks
This commit is contained in:
@@ -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) {
|
||||
@@ -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() {
|
||||
|
||||
+28
-1
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user