1
- import { fromBase64 , fromBech32 , toBase64 , toBech32 } from "@cosmjs/encoding " ;
1
+ import { Coin } from "@cosmjs/amino " ;
2
2
import { sha512 } from "@cosmjs/crypto" ;
3
+ import { fromBase64 , fromBech32 , toBase64 , toBech32 } from "@cosmjs/encoding" ;
3
4
import { Decimal } from "@cosmjs/math" ;
4
- import { Coin } from "@cosmjs/amino" ;
5
5
import { ChainInfo } from "../types" ;
6
6
7
7
/**
@@ -80,6 +80,21 @@ const exampleAddress = (index: number, chainAddressPrefix: string) => {
80
80
return toBech32 ( chainAddressPrefix , data ) ;
81
81
} ;
82
82
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
+
83
98
/**
84
99
* Generates an example pubkey (secp256k1, compressed).
85
100
*
@@ -115,7 +130,7 @@ const checkAddress = (input: string, chainAddressPrefix: string) => {
115
130
return error . toString ( ) ;
116
131
}
117
132
118
- if ( prefix !== chainAddressPrefix ) {
133
+ if ( ! prefix . startsWith ( chainAddressPrefix ) ) {
119
134
return `Expected address prefix '${ chainAddressPrefix } ' but got '${ prefix } '` ;
120
135
}
121
136
@@ -126,32 +141,6 @@ const checkAddress = (input: string, chainAddressPrefix: string) => {
126
141
return null ;
127
142
} ;
128
143
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
-
155
144
/**
156
145
* Returns a link to a transaction in an explorer if an explorer is configured
157
146
* for transactions. Returns null otherwise.
@@ -168,8 +157,8 @@ export {
168
157
printableCoin ,
169
158
printableCoins ,
170
159
exampleAddress ,
160
+ exampleValidatorAddress ,
171
161
examplePubkey ,
172
162
checkAddress ,
173
163
explorerLinkTx ,
174
- checkValidatorAddress ,
175
164
} ;
0 commit comments