Herman

Herman

"Getting Started with Solana": Official Tutorial Lesson 2

"Solana Introduction": Official Tutorial Lesson 2#

Course Link

  • Complete the code with the lab.
  • Complete all challenges.
    • Add instructions to handle invalid wallet addresses.
    • Modify the script to connect to mainNet and look up some famous Solana wallets. Try toly.sol, shaq.sol, or mccann.sol.

The second challenge is definitely not a difficult problem. It seems like something that can be found by changing a few parameters.

But I am not familiar with it. I have spent a lot of time reading the documentation, and GPT's answers are not helpful. I don't know what I missed that prevented me from completing it.

This will be updated in the future, let's continue.


It is easy to complete the code with the lab, and there are no difficult parts. I won't provide detailed records.

Regarding Add instructions to handle invalid wallet addresses:

  • The most direct solution according to ChatGPT, internet searches, etc. is:
try {
  const public_key = new PublicKey(address);
} catch (error) {
  console.log('address is not valid.');
}

Of course, there are doubts. As a beginner, I don't know what needs to be checked for an address to be considered valid.

  • The second step is to ask ChatGPT how to determine if an address is valid.

I was given two options:

  • Use a library directly. The library will handle this. (web3.js, Solana CLI)

  • Since the address is derived from base58, the algorithm itself has some characteristics. For example, the length and the absence of certain characters.

But the basic conclusion is that the address can be validated based on the characteristics of base58.

  • The third step is simple verification. I read the code of web3.js and Solana CLI to see how they validate addresses.
  1. The PublicKey constructor in web3.js has a validation based on length. Then it decodes using the bn library to check for any issues.

  2. The constructor in Solana CLI also only checks the length. It simply decodes using the base58 library and if there are no errors, it is considered valid. They are directly sent.

The nesting is too deep (just a little criticism, it feels like Java).

So the conclusion here is:

  1. If you want a simple and quick way to validate if an address is valid, just call the constructor of web3.js, it will handle the validation for you.

  2. If you want your code to clearly indicate the intention, rather than implementing it in a roundabout way, it is recommended to follow the algorithm rules, check the length, and try decoding once. If it passes, the address is correct. If it fails, the address is invalid and an error should be returned.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.