1

I'm trying to write a conditional to see if a string is valid according to Solana standards. In documentation it says:

Each digital signature is in the ed25519 binary format and consumes 64 bytes.

Based on this info, how can I make sure if a signature string is valid? I want to avoid checking invalid signatures that would give an error.

I assume Solana RPC functions does this check automatically and it rejects invalid signatures but I need this for another purpose so I should do it before feeding it to functions.

Valid signature example:

"4gVuB1KsM58fb3vRpnDucwW4Vi6fVGA51QDQd9ARvx4GH5yYVDPzDnvzUbSJf3YLWWdsX7zCMSN9N1GMnTYwWiJf"

Invalid Signature example:

"xeneansitametliberoleoNuncornarelaoreetsapieneubibendumturpislaciniaut"

2 Answers 2

2

It seems you are asking about how you can verify that a string can be a syntactically valid signature.

I think you want to that test on the client side, i.e. JS/TS.

Correct me if I misunderstood your question.

All signatures, pubkeys etc. will be notated in base58 format on Solana.

So the first thing you want to do with the string is to check if it's in valid Base58 format, and has the correct length, i.e. it resolves to a 64 byte buffer.

The best way to do that is use the bs58 package, that you can find here or here. In your package.json, just add "bs58": "^5.0.0" to your "dependencies" section, or just run npm i bs58.

Now you can simply run a bs58.decode(yourString) on your String. If that throws an error, the string is invalid, i.e. not a Base58 string.

If it is valid, however, you can check the length of the resulting Buffer or Uint8Array and see if it is 64, as valid signatures are 64 bytes long.

If the length is not equal to 64, your string is not a valid signature string.

If your string passes both tests, i.e. bs58.decode didn't throw any error and and the length of the resulting buffer/array is 64, then the string is syntactically a valid signature string.

Then, of course, you haven't tested if it is indeed a valid signature for whatever needs to be signed, i.e. if the signature is semantically correct. But - as you mentioned in your question - Solana will do that anyway. If you do want to check it yourself, @sohrab's answer sounds like a good bet.

0

To verify the transaction signature, you also need the data being signed, i.e. transaction, and also the public key(s) that have signed the transaction.

When you have the transaction loaded, there is a Transaction.verifySignatures() method that does this validation for you.

If that doesn't work for your use case, you may have to use a lower-level library like tweetnacl-js, specifically nacl.sign.detached.verify:

// all args are Buffer
nacl.sign.detached.verify(transaction, txSignature, signerPubkey)
2
  • hey, this works nice for public keys but question is about signatures. PublicKey.isOnCurve() doesn't work on signatures. It says "Error: Invalid public key input"
    – curiosity
    Jul 27, 2022 at 0:01
  • My bad. Updated the answer.
    – sohrab
    Jul 27, 2022 at 0:59

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.