@@ -22,14 +22,8 @@ export class RuneUtils extends CosmosUtils {
2222 const value = this . registry . decode ( message ) ;
2323 return {
2424 value : {
25- fromAddress :
26- this . networkType === NetworkType . TESTNET
27- ? bech32 . encode ( TESTNET_ADDRESS_PREFIX , value . fromAddress )
28- : bech32 . encode ( MAINNET_ADDRESS_PREFIX , value . fromAddress ) ,
29- toAddress :
30- this . networkType === NetworkType . TESTNET
31- ? bech32 . encode ( TESTNET_ADDRESS_PREFIX , value . toAddress )
32- : bech32 . encode ( MAINNET_ADDRESS_PREFIX , value . toAddress ) ,
25+ fromAddress : this . getEncodedAddress ( value . fromAddress ) ,
26+ toAddress : this . getEncodedAddress ( value . toAddress ) ,
3327 amount : value . amount ,
3428 } ,
3529 typeUrl : message . typeUrl ,
@@ -39,23 +33,74 @@ export class RuneUtils extends CosmosUtils {
3933
4034 /** @inheritdoc */
4135 isValidAddress ( address : string | Buffer ) : boolean {
42- if ( address === undefined ) {
36+ if ( address === undefined || address === null ) {
4337 return false ;
4438 }
45- if ( typeof address !== 'string' ) {
46- const encodedAddress =
47- this . networkType === NetworkType . TESTNET
48- ? bech32 . encode ( TESTNET_ADDRESS_PREFIX , address )
49- : bech32 . encode ( MAINNET_ADDRESS_PREFIX , address ) ;
50- if ( this . networkType === NetworkType . TESTNET ) {
51- return this . isValidCosmosLikeAddressWithMemoId ( encodedAddress , constants . testnetAccountAddressRegex ) ;
52- }
53- return this . isValidCosmosLikeAddressWithMemoId ( encodedAddress , constants . mainnetAccountAddressRegex ) ;
54- } else {
55- if ( this . networkType === NetworkType . TESTNET ) {
56- return this . isValidCosmosLikeAddressWithMemoId ( address , constants . testnetAccountAddressRegex ) ;
57- }
58- return this . isValidCosmosLikeAddressWithMemoId ( address , constants . mainnetAccountAddressRegex ) ;
39+ if ( address instanceof Uint8Array ) {
40+ return this . isValidDecodedAddress ( address ) ;
41+ }
42+ if ( typeof address === 'string' ) {
43+ return this . isValidEncodedAddress ( address ) ;
44+ }
45+ return false ;
46+ }
47+
48+ /**
49+ * Validates a decoded address in `Buffer` form by encoding it and
50+ * checking if the encoded version is valid
51+ *
52+ * @param address - The decoded address as a `Buffer`.
53+ * @returns `true` if the encoded address is valid, `false` otherwise.
54+ */
55+ private isValidDecodedAddress ( address : Buffer ) : boolean {
56+ const encodedAddress = this . getEncodedAddress ( address ) ;
57+ return this . isValidEncodedAddress ( encodedAddress ) ;
58+ }
59+
60+ /**
61+ * Validates an encoded address string against network-specific criteria.
62+ *
63+ * @param address - The encoded address as a `string`.
64+ * @returns `true` if the address meets network-specific validation criteria, `false` otherwise.
65+ */
66+ private isValidEncodedAddress ( address : string ) : boolean {
67+ if ( this . networkType === NetworkType . TESTNET ) {
68+ return this . isValidCosmosLikeAddressWithMemoId ( address , constants . testnetAccountAddressRegex ) ;
69+ }
70+ return this . isValidCosmosLikeAddressWithMemoId ( address , constants . mainnetAccountAddressRegex ) ;
71+ }
72+
73+ /**
74+ * Encodes a given address `Buffer` into a bech32 string format, based on the current network type.
75+ * Primarily serves as a utility to convert a `Buffer`-type address to a bech32 encoded string
76+ *
77+ * @param address - The address to be encoded, provided as a `Buffer`.
78+ * @returns A bech32-encoded string representing the address.
79+ * @throws Error - Throws an error if encoding fails
80+ */
81+ getEncodedAddress ( address : Buffer ) : string {
82+ try {
83+ return this . networkType === NetworkType . TESTNET
84+ ? bech32 . encode ( TESTNET_ADDRESS_PREFIX , address )
85+ : bech32 . encode ( MAINNET_ADDRESS_PREFIX , address ) ;
86+ } catch ( error ) {
87+ throw new Error ( `Failed to encode address: ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
88+ }
89+ }
90+
91+ /**
92+ * Decodes a bech32-encoded address string back into a `Buffer`.
93+ * Primarily serves as a utility to convert a string-type address into its binary representation,
94+ *
95+ * @param address - The bech32-encoded address as a `string`.
96+ * @returns The decoded address as a `Buffer`.
97+ * @throws Error - Throws an error if decoding fails
98+ */
99+ getDecodedAddress ( address : string ) : Buffer {
100+ try {
101+ return bech32 . decode ( address ) . data ;
102+ } catch ( error ) {
103+ throw new Error ( `Failed to decode address: ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
59104 }
60105 }
61106
0 commit comments