1

I am having an issue trying to verify ecdsa signature in ink! for my test.

I am generating the signature this way

const keyring = new Keyring({type: 'ecdsa'});
const pair =  keyring.addFromUri('//Alice');

Then i get the public key to be stored in the contract storage as the governor during instantiation of the contract

const publicKey = pair.publicKey;

This is how i am signing the message(data)

const sig = Array.from(pair.sign(hashDataArray));

Then i pass the signature into the contract.

But when i call ink_env::ecdsa_recover in the contract to recover the public key from the signature, i get a different public key entirely different from the one i stored(generated) in the contract during instantiation.

1
  • 2
    Does your signature verify on PolkadotJS? The signature creation in the Docs looks like its using the data and not the hash. Anyway, please consider sharing the code which makes it much easier to help. Aug 3, 2022 at 20:35

1 Answer 1

2

So I figured the signature creation in polkadotJS is to receive the raw data and not the hash.

More so, I had to generate the public key this way:

const mnemonic = mnemonicGenerate();
const keyring = new Keyring({type: 'ethereum'});
const pair = keyring.addFromUri(mnemonic, { name: 'first pair' }, 'ethereum');
      
const publicKey = pair.publicKey;

And then i created the signature this way

const sig = Array.from(pair.sign(rawData));

With this generation, I was able to recover the public key successfully with ink_env::ecdsa_recover

N.B: I am using keccak256 for hashing. The Keyring type ecdsa uses blake2 for hashing hence why I used the type ethereum which uses keccak for hashing. You can find the details here

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.