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
.
3 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. Commented Sep 28, 2022 at 6:08
-
2solders has
Pubkey.is_on_curve
: kevinheavey.github.io/solders/api_reference/…– KevinHCommented Oct 2, 2022 at 1:22
import base58
def is_valid_solana_address(address):
if len(address) != 44:
return False
decoded_address = base58.b58decode(address)
return len(decoded_address) == 32
-
Hey Max! Someone flagged this answer, and while I don't think there's anything worth flagging in it, I'm pretty sure it's incorrect, in that valid addresses may actually vary in length.– mikemaccana ♦Commented Aug 20 at 18:44
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– KevinHCommented Mar 11, 2023 at 18:07
-
Yeah edited to : solana address , for a better understanding– thewiseCommented Mar 12, 2023 at 8:01
-
-
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– thewiseCommented Mar 14, 2023 at 20:25
-
can you share a link to the part of the docs that is incorrect please?– KevinHCommented Mar 14, 2023 at 21:25