2

Are there any examples available? I would like to verify the signature on a contract. I hope to achieve something similar to the following:

fn verify(message: &[u8; 32], sig_bytes: &[u8; 64], signer: &Pubkey) -> bool {
    let signature = Signature::new(sig_bytes);
    let keypair = Signer::new(signer.to_bytes());
    let result = keypair.verify(message, &signature);
    result.is_ok()
}

1 Answer 1

1

While you can't perform all cryptographic operations on-chain, the runtime provides the Ed25519 program to verify signatures. Here's an example of how to use it, copied from the tests at https://github.com/solana-labs/solana/blob/166be2995ee76e51a343f04e41d348cfeb1cc8d9/sdk/src/ed25519_instruction.rs#L347:

        let privkey = ed25519_dalek::Keypair::generate(&mut thread_rng());
        let message_arr = b"hello";
        let mut instruction = new_ed25519_instruction(&privkey, message_arr);
        let mint_keypair = Keypair::new();
        let feature_set = FeatureSet::all_enabled();

        let tx = Transaction::new_signed_with_payer(
            &[instruction.clone()],
            Some(&mint_keypair.pubkey()),
            &[&mint_keypair],
            Hash::default(),
        );

        assert!(tx.verify_precompiles(&feature_set).is_ok());

You can read more at https://docs.solana.com/developing/runtime-facilities/programs#ed25519-program

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.