Skip to content

Commit 85a0d2d

Browse files
authored
Merge pull request #89 from cosmos/fix/check-validator-address
Fix check validator address
2 parents 9408e1a + edbd1d2 commit 85a0d2d

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed

components/forms/DelegationForm.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import axios from "axios";
2-
import { Account, calculateFee } from "@cosmjs/stargate";
31
import { Decimal } from "@cosmjs/math";
2+
import { Account, calculateFee } from "@cosmjs/stargate";
43
import { assert } from "@cosmjs/utils";
4+
import axios from "axios";
5+
import { NextRouter, withRouter } from "next/router";
56
import React, { useState } from "react";
6-
import { withRouter, NextRouter } from "next/router";
77
import { useAppContext } from "../../context/AppContext";
8+
import { checkAddress, exampleValidatorAddress } from "../../lib/displayHelpers";
89
import Button from "../inputs/Button";
910
import Input from "../inputs/Input";
1011
import StackableContainer from "../layout/StackableContainer";
11-
import { checkValidatorAddress } from "../../lib/displayHelpers";
1212

1313
interface Props {
1414
address: string | null;
@@ -60,7 +60,7 @@ const DelegationForm = (props: Props) => {
6060

6161
const handleCreate = async () => {
6262
assert(state.chain.addressPrefix, "addressPrefix missing");
63-
const validatorAddressError = checkValidatorAddress(validatorAddress, "cosmosvaloper");
63+
const validatorAddressError = checkAddress(validatorAddress, state.chain.addressPrefix);
6464
if (validatorAddressError) {
6565
setAddressError(
6666
`Invalid address for network ${state.chain.chainId}: ${validatorAddressError}`,
@@ -93,7 +93,7 @@ const DelegationForm = (props: Props) => {
9393
value={validatorAddress}
9494
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setValidatorAddress(e.target.value)}
9595
error={addressError}
96-
placeholder={`E.g. cosmosvaloper1sjllsnramtg3ewxqwwrwjxfgc4n4ef9u2lcnj0`}
96+
placeholder={`E.g. ${exampleValidatorAddress(0, state.chain.addressPrefix)}`}
9797
/>
9898
</div>
9999
<div className="form-item">

lib/displayHelpers.ts

+19-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { fromBase64, fromBech32, toBase64, toBech32 } from "@cosmjs/encoding";
1+
import { Coin } from "@cosmjs/amino";
22
import { sha512 } from "@cosmjs/crypto";
3+
import { fromBase64, fromBech32, toBase64, toBech32 } from "@cosmjs/encoding";
34
import { Decimal } from "@cosmjs/math";
4-
import { Coin } from "@cosmjs/amino";
55
import { ChainInfo } from "../types";
66

77
/**
@@ -80,6 +80,21 @@ const exampleAddress = (index: number, chainAddressPrefix: string) => {
8080
return toBech32(chainAddressPrefix, data);
8181
};
8282

83+
/**
84+
* Generates an example address for the configured blockchain.
85+
*
86+
* `index` can be set to a small integer in order to get different addresses. Defaults to 0.
87+
*/
88+
const exampleValidatorAddress = (index: number, chainAddressPrefix: string) => {
89+
const usedIndex = index || 0;
90+
let data = fromBech32("cosmosvaloper10v6wvdenee8r9l6wlsphcgur2ltl8ztkfrvj9a").data;
91+
for (let i = 0; i < usedIndex; ++i) {
92+
data = sha512(data).slice(0, data.length); // hash one time and trim to original length
93+
}
94+
const validatorPrefix = chainAddressPrefix + "valoper";
95+
return toBech32(validatorPrefix, data);
96+
};
97+
8398
/**
8499
* Generates an example pubkey (secp256k1, compressed).
85100
*
@@ -115,7 +130,7 @@ const checkAddress = (input: string, chainAddressPrefix: string) => {
115130
return error.toString();
116131
}
117132

118-
if (prefix !== chainAddressPrefix) {
133+
if (!prefix.startsWith(chainAddressPrefix)) {
119134
return `Expected address prefix '${chainAddressPrefix}' but got '${prefix}'`;
120135
}
121136

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

129-
/**
130-
* Returns an error message for invalid addresses.
131-
*
132-
* Returns null of there is no error.
133-
*/
134-
const checkValidatorAddress = (input: string, chainAddressPrefix: string): string | null => {
135-
if (!input) return "Empty";
136-
let prefix;
137-
try {
138-
({ prefix } = fromBech32(input));
139-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
140-
} catch (error: any) {
141-
return error.toString();
142-
}
143-
144-
if (prefix !== chainAddressPrefix) {
145-
return `Expected address prefix '${chainAddressPrefix}' but got '${prefix}'`;
146-
}
147-
148-
if (input.length !== 52) {
149-
return "Invalid address length in validator address. Must be 52 bytes.";
150-
}
151-
152-
return null;
153-
};
154-
155144
/**
156145
* Returns a link to a transaction in an explorer if an explorer is configured
157146
* for transactions. Returns null otherwise.
@@ -168,8 +157,8 @@ export {
168157
printableCoin,
169158
printableCoins,
170159
exampleAddress,
160+
exampleValidatorAddress,
171161
examplePubkey,
172162
checkAddress,
173163
explorerLinkTx,
174-
checkValidatorAddress,
175164
};

0 commit comments

Comments
 (0)