argentlabs / argent-x

ArgentX browser extension for StarkNet - made with ❤️ by Argent
https://github.com/argentlabs/argent-x
Other
625 stars 273 forks source link

Verify signed EIP-712 message off-chain using starkCurve #2099

Open haardikk21 opened 1 year ago

haardikk21 commented 1 year ago

Hey,

I'm trying to sign a message on the frontend - I'm using StarknetKit and Argent X as the wallet. Later, I'm trying to verify that message on the backend using the starknet library and ec.starkCurve.verify

After a lot of digging, it seems like Argent X returns an ArraySignatureType through signMessage, and that message can be verified on the frontend if I use connection.account.verifyMessage

But, I tried to convert the ArraySignatureType to a WeierstraussSignatureType by treating the item at index 0 as r and index 1 as s but it fails to verify when I do that

Docs are basically non-existent on this topic, so can anyone here help point me in the right direction on what I'm doing wrong?

    const typedDataMsg = {...} // Redacted for brevity

    const pubKey = await connection.account.signer.getPubKey();
    const sig = (await connection.account.signMessage(
      typedDataMsg
    )) as ArraySignatureType;

    const ecSig = new ec.starkCurve.Signature(BigInt(sig[0]), BigInt(sig[1]));

    const msgHash = typedData.getMessageHash(
      typedDataMsg,
      connection.selectedAddress
    );

    const verifyResDirect = ec.starkCurve.verify(ecSig, msgHash, pubKey);

    console.log({ verifyResDirect }); // False

    const verifyResult = await connection.account.verifyMessage(
      typedDataMsg,
      sig
    );

    console.log({ verifyResult }); // True