In ink! 4.0 ink::env crate, it provides ecdsa_recover function. It returns compressed public key &[u8, 33] as is explained here.

Assumed that blake2 is used for hashing.

pub fn verify(&self, req: ForwardRequest, signature: [u8; 65]) -> bool {
    let mut message_hash = <ink::env::hash::Blake2x256 as ink::env::hash::HashOutput>::Type::default();
    ink::env::hash_encoded::<ink::env::hash::Blake2x256, _>(&req, &mut message_hash);

    let output = self.env().ecdsa_recover(&signature, &message_hash).expect("Failed to recover");

    let signature_account_id = // How to get AccountId from compressed public key?


(Above code before todo!() is not confirmed to work)

How can I recover AccountId from compressed public key?

1 Answer 1


To get the AccountId, you need to hash the compressed public key with Blake2x256

I will just update your question - the function used in the question (where the output public key is the output of ecdsa_recover) is for ink! version < 4.0.0, ink! v4's ecdsa_recover function stores the output public key in the third parameter output, see the function here.

pub fn ecdsa_recover(
    signature: &[u8; 65],
    message_hash: &[u8; 32],
    output: &mut [u8; 33]
) -> Result<()>

Then you can hash the output value with Blake and get the AccountId.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.