1

I want to know if the address of Solana either is valid or invalid by using Python. How can I do that? Like in web3.py we validate an Ethereum account/address and it returns True ofFalse.

2 Answers 2

1

You can use web3.js new PublicKey('your_address') to verify if it is a valid public key.

web3.js also provides PublicKey.isOnCurve(...) to validate if a public key is on ed25519 curve. Given there are PDAs in Solana, and that their addresses are valid public key, but are NOT on the curve.

To summarize: If you are able to instantiate, it means it is a valid public key, if you want to know if the address is on the curve, you need to further validate by calling isOnCurve(...)

Just noticed that you wanted an answer specific to python, depending on the client that you are using, there should be similar methods to use. One that I found is in solana-py, use the PublicKey object to validate.

3
-1

The examples in solders don't always work as intended. This is the current working example:

address = 'solana address'

sol_addr = Pubkey.from_string(address)

if sol_addr.is_on_curve():

print('passed test1')

return

if sol_addr.LENGTH == 32:

print ('passed test2')

These are the two major ways of validating a Sol address. Another way is to use the pair, but there are many dexes too on Sol so that won't be too solid.

5
  • 0x0euuejei isn't a valid base58 pubkey, probably better to use a valid one in the example
    – KevinH
    Mar 11, 2023 at 18:07
  • Yeah edited to : solana address , for a better understanding
    – thewise
    Mar 12, 2023 at 8:01
  • Also what doesn't work in Solders?
    – KevinH
    Mar 13, 2023 at 18:10
  • The example given on solders webpage doesn't include converting the address from a string(Pubkey.from_string('address'), though the API , in venv file houses the example, you need to first convert from a valid string else you won't be able to run the rest of the code on the address. Same with converting to string , Pubkey.string() doesn't work, but Str('address') works perf
    – thewise
    Mar 14, 2023 at 20:25
  • can you share a link to the part of the docs that is incorrect please?
    – KevinH
    Mar 14, 2023 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.