0

I made an app that returns user assets from the Aptos blockchain. In the app, I have a form for the user's address. My goal is to validate that address using RegExp or something like that. I have this code:

const address = '0xcd30fbbda98b2aed026772c13e5ed90a7f056b589ef9e78cd96415e1af12451c';
function isValidAddress(address) { // code to check address }
console.log(isValidAddress(address));

I will appreciate any help.

Daniel Porteous
  • 5,536
  • 3
  • 25
  • 44
Cosmin Ciolacu
  • 406
  • 9
  • 29

1 Answers1

0

It depends on your use case.

If you want to validate client side, you can try something like this:

import { TxnBuilderTypes } from "aptos";
import const { AccountAddress } = TxnBuilderTypes;

// Validate.
let accountAddress = AccountAddress.fromHex("0xcd30fbbda98b2aed026772c13e5ed90a7f056b589ef9e78cd96415e1af12451c");

// You can get it back as a string like this.
let accountAddressString = accountAddress.toHexString();

You can also try to validate using an Aptos node running the API (i.e. a fullnode). For example:

let client = AptosClient("https://fullnode.mainnet.aptoslabs.com");
client.getAccount("0xcd30fbbda98b2aed026772c13e5ed90a7f056b589ef9e78cd96415e1af12451c");

If the account exists, you'll get a 200 or if it doesn't, you'll get a 404. But if the account address was invalid, you'll get a 400 (bad request).

Daniel Porteous
  • 5,536
  • 3
  • 25
  • 44