Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check validator address #89

Merged
merged 5 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions components/forms/DelegationForm.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import axios from "axios";
import { Account, calculateFee } from "@cosmjs/stargate";
import { Decimal } from "@cosmjs/math";
import { Account, calculateFee } from "@cosmjs/stargate";
import { assert } from "@cosmjs/utils";
import axios from "axios";
import { NextRouter, withRouter } from "next/router";
import React, { useState } from "react";
import { withRouter, NextRouter } from "next/router";
import { useAppContext } from "../../context/AppContext";
import { checkAddress, exampleValidatorAddress } from "../../lib/displayHelpers";
import Button from "../inputs/Button";
import Input from "../inputs/Input";
import StackableContainer from "../layout/StackableContainer";
import { checkValidatorAddress } from "../../lib/displayHelpers";

interface Props {
address: string | null;
Expand Down Expand Up @@ -60,7 +60,7 @@ const DelegationForm = (props: Props) => {

const handleCreate = async () => {
assert(state.chain.addressPrefix, "addressPrefix missing");
const validatorAddressError = checkValidatorAddress(validatorAddress, "cosmosvaloper");
const validatorAddressError = checkAddress(validatorAddress, state.chain.addressPrefix);
if (validatorAddressError) {
setAddressError(
`Invalid address for network ${state.chain.chainId}: ${validatorAddressError}`,
Expand Down Expand Up @@ -93,7 +93,7 @@ const DelegationForm = (props: Props) => {
value={validatorAddress}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setValidatorAddress(e.target.value)}
error={addressError}
placeholder={`E.g. cosmosvaloper1sjllsnramtg3ewxqwwrwjxfgc4n4ef9u2lcnj0`}
placeholder={`E.g. ${exampleValidatorAddress(0, state.chain.addressPrefix)}`}
/>
</div>
<div className="form-item">
Expand Down
49 changes: 19 additions & 30 deletions lib/displayHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fromBase64, fromBech32, toBase64, toBech32 } from "@cosmjs/encoding";
import { Coin } from "@cosmjs/amino";
import { sha512 } from "@cosmjs/crypto";
import { fromBase64, fromBech32, toBase64, toBech32 } from "@cosmjs/encoding";
import { Decimal } from "@cosmjs/math";
import { Coin } from "@cosmjs/amino";
import { ChainInfo } from "../types";

/**
Expand Down Expand Up @@ -80,6 +80,21 @@ const exampleAddress = (index: number, chainAddressPrefix: string) => {
return toBech32(chainAddressPrefix, data);
};

/**
* Generates an example address for the configured blockchain.
*
* `index` can be set to a small integer in order to get different addresses. Defaults to 0.
*/
const exampleValidatorAddress = (index: number, chainAddressPrefix: string) => {
const usedIndex = index || 0;
let data = fromBech32("cosmosvaloper10v6wvdenee8r9l6wlsphcgur2ltl8ztkfrvj9a").data;
for (let i = 0; i < usedIndex; ++i) {
data = sha512(data).slice(0, data.length); // hash one time and trim to original length
}
const validatorPrefix = chainAddressPrefix + "valoper";
return toBech32(validatorPrefix, data);
};

/**
* Generates an example pubkey (secp256k1, compressed).
*
Expand Down Expand Up @@ -115,7 +130,7 @@ const checkAddress = (input: string, chainAddressPrefix: string) => {
return error.toString();
}

if (prefix !== chainAddressPrefix) {
if (!prefix.startsWith(chainAddressPrefix)) {
return `Expected address prefix '${chainAddressPrefix}' but got '${prefix}'`;
}

Expand All @@ -126,32 +141,6 @@ const checkAddress = (input: string, chainAddressPrefix: string) => {
return null;
};

/**
* Returns an error message for invalid addresses.
*
* Returns null of there is no error.
*/
const checkValidatorAddress = (input: string, chainAddressPrefix: string): string | null => {
if (!input) return "Empty";
let prefix;
try {
({ prefix } = fromBech32(input));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
return error.toString();
}

if (prefix !== chainAddressPrefix) {
return `Expected address prefix '${chainAddressPrefix}' but got '${prefix}'`;
}

if (input.length !== 52) {
return "Invalid address length in validator address. Must be 52 bytes.";
}

return null;
};

/**
* Returns a link to a transaction in an explorer if an explorer is configured
* for transactions. Returns null otherwise.
Expand All @@ -168,8 +157,8 @@ export {
printableCoin,
printableCoins,
exampleAddress,
exampleValidatorAddress,
examplePubkey,
checkAddress,
explorerLinkTx,
checkValidatorAddress,
};