cosmos / cosmjs

The Swiss Army knife to power JavaScript based client solutions ranging from Web apps/explorers over browser extensions to server-side clients like faucets/scrapers.
https://cosmos.github.io/cosmjs/
Apache License 2.0
645 stars 330 forks source link

Is there way to verify signature without passing public key? #1487

Closed dovigod closed 10 months ago

dovigod commented 11 months ago

Hi devs, While verifing signature, is there way to verify by wallet address instead of using public key?

` const secp256K1PubKey = { type: "tendermint/PubKeySecp256k1", value: input.secp256k1PubKey } as Secp256k1Pubkey

const signer = pubkeyToAddress(secp256K1PubKey, 'sei');
const rawPubKey = fromBase64(input.secp256k1PubKey);
const signDoc = makeADR36AminoSignDoc(signer, input.plainMessage)

const isVerified = await Secp256k1.verifySignature(
  Secp256k1Signature.fromFixedLength(fromBase64(input.signature)),
  sha256(serializeSignDoc(signDoc)),
  rawPubKey
);

`

while current codes works well, but I just want to verify signature with wallet address.

is there any way to do this?

webmaster128 commented 11 months ago

I never tried this in Cosmos space, but it might be possible using Secp256k1.recoverPubkey. For this you need an ExtendedSecp256k1Signature, which is just the signature plus a recovery param. If you don't have the recovery param you can loop through 0, 1, 2, 3 and check each public key you get.

dovigod commented 11 months ago

@webmaster128 “Simon the Best” Thanks for answering, I’ll comment if this works :) thanks again

dovigod commented 10 months ago

@webmaster128

YYYYEEEAAAhhhh it works!!!! once again, Thanks for the help :)

since I'd need to change signature(A) which is result of signAmino to ExtendedSecp256k1Signature,

what I did is, use 'A' as plain message and instantiate it to ExtendedSecp256k1Signature below is my source code.,

` const sigFromSignAmino = signature.signature.signature; // result of signAmino const x = new TextEncoder().encode(sigFromSignAmino); const hashBuffer = await crypto.subtle.digest("SHA-256", x); const privateKey = await getPrivateKeyFromMnemonic( seiWalletSet2.mnemonic ); const hashMessage = new Uint8Array(hashBuffer); const sig = await Secp256k1.createSignature( hashMessage, privateKey as Uint8Array ); console.log(sig); //extendedSig

    const pubKey = await Secp256k1.recoverPubkey(sig, hashMessage);
    console.dir(pubKey);

`