I want to validate the address of solana either is valid or invalid by using python how could I do that?like in web3.py we validate an ethereum account/address and it returns false or true as output.
2 Answers
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.
-
-
you don't have isOnCurve(), but just try to instantiate by using new PublicKey(...) -> if this throws error, it is not valid. Sep 28, 2022 at 6:08
-
1solders has
Pubkey.is_on_curve
: kevinheavey.github.io/solders/api_reference/…– KevinHOct 2, 2022 at 1:22
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.
-
0x0euuejei isn't a valid base58 pubkey, probably better to use a valid one in the example– KevinHMar 11 at 18:07
-
-
-
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– thewiseMar 14 at 20:25
-