beepboopbopp
beepboopbopp

Reputation: 21

How to verify public key is on Ed25519 curve in PHP? (verify Solana address)

Building a server-side implementation to do Solana verification for a contract, when we receive a Solana address (Ed25519 public key) from client. They only want me to use native PHP methods, no imports, idk why. Open source libraries are still helpful as I can try my best to pull bits and pieces from it. I do have access to all of the libsodium PHP\Sodium library methods here: https://www.php.net/manual/en/book.sodium.php (which I believe allows us to do Ed25519)

This is the implementation in JS: https://solana-labs.github.io/solana-web3.js/classes/PublicKey.html#isOnCurve

I need this in PHP. In other words:

How can I verify Solana addresses (such as AJXw4EJtRBGswEestiD3gztdbsAh8fe3VSJXs6U33UBv) in PHP? As in, how do I verify a public key is on the Ed25519 curve?

Thanks in advance! I don't usually post on StackOverflow but I'm hoping this answer will be useful as Web3 continues to evolve.

Upvotes: 2

Views: 1427

Answers (2)

Russo
Russo

Reputation: 3062

The verze-app repo has been archived, and has been forked to https://github.com/Attestto-com/solana-php-sdk

Inside that forked repo, take a look at the "isOnCurve" function in "src/PublicKey.php"

Upvotes: 0

Jon C
Jon C

Reputation: 8462

It looks like your best bet is to try sodium_crypto_sign_ed25519_pk_to_curve25519 and catch the exception if it fails, ie:

        try {
            $_ = sodium_crypto_sign_ed25519_pk_to_curve25519($pubkeyBinaryString);
            return true;
        } catch (SodiumException $exception) {
            return false;
        }

Code lifted from https://github.com/verze-app/solana-php-sdk/blob/ab97975d4588fd759c63f7967caee1a5401cb2fe/src/PublicKey.php#L187

Upvotes: 1

Related Questions