STX address validator

Hi, I was wondering if there is some library or something for JS/TS that validates if the entered string is a valid STX address. Or if there is no library, how would one go about validating the address? Is there some formula or something? I couldn’t find anything on this topic.

Thanks!

1 Like

This may help, GitHub - stacks-network/c32check: Crockford base-32 encoding with 4-byte checksum

If you’re using TS/JS it can be useful to work with @stacks/transaction Address interface. You could do something like:

import { createAddress } from '@stacks/transaction';
//...
try {
  const maybeAddress = 'the-string-I-want-to-validate';
  const address = createAddress(maybeAddress); // createAddress will return an Address or throw in case it's not valid
} catch {
  console.error('Not a valid address!');
  // Handle Invalid address
}
1 Like