Skip to content

Commit

Permalink
fix verify signature
Browse files Browse the repository at this point in the history
  • Loading branch information
elshan-eth committed Feb 27, 2024
1 parent 82dcddd commit aab503a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 41 deletions.
96 changes: 61 additions & 35 deletions web/src/pages/proof-page/proof-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import { MerkleTree } from "merkletreejs";
import keccak256 from "keccak256";
import { hashedLeaf } from "../../utils/award";
import { Buffer } from "buffer";
import { validateSignature } from "../../utils/asn1";
import { ethers } from "ethers";
import { findEthereumSig } from "../../utils/asn1";
import { Contract, ethers } from "ethers";
import { useWeb3Connection } from "../../hooks/useWeb3Connection";
import { governanceContracts } from "../../constants";
import abis from "../../contracts";

const ProofPage = () => {
const { address, provider, network } = useWeb3Connection();
Expand Down Expand Up @@ -74,46 +76,70 @@ const ProofPage = () => {
let merkleProof = JSON.parse(
Buffer.from(merkleProofHex, "base64").toString(),
);
try {
console.log("address is", address);
let signedHash = ethers.utils.hashMessage(fromHex(address));
console.log("Signed Hash", signedHash);
let verified = false;
console.log("network", network);

let signature = validateSignature(
signedHash,
signatureHex,
tmpEthAddr,
isASN1,
);
console.log("Signature is correct.", signature);
let contract = new Contract(
governanceContracts[network.name].devRewardDistributor,
abis.DevRewardDistributor.abi,
provider,
);

let signer = provider.getSigner();
let signed = await contract.connect(signer);

let signature = signatureHex;
if (isASN1) {
let asn1Signature = Buffer.from(signatureHex, "hex");

const leaf = await hashedLeaf(userId, tmpEthAddr);
const verified = MerkleTree.verify(
merkleProof,
leaf,
merkleRoot,
keccak256,
{ hashLeaves: false, sortPairs: true },
);
let bufferSig = Buffer.from(asn1Signature);

console.log("MerkleTree verified", verified);
if (verified) {
setHaveProof(true);
dispatch(
storeProof({ userId, tmpEthAddr, signature, merkleProof }),
let { r, s } = findEthereumSig(bufferSig);
let v = 27;
let raw_signature = {
r: "0x" + r.toString(16, 32),
s: "0x" + s.toString(16, 32),
v,
};
signature = ethers.utils.splitSignature(raw_signature);

signature = ethers.utils.joinSignature(signature);
try {
await signed.estimateGas.claimTokens(
userId,
merkleProof,
tmpEthAddr,
signature,
);
} else {
toast("Invalid merkle proof. Please check the data.");
} catch (error) {
console.log("invalid v", error);
raw_signature.v = 28;
signature = ethers.utils.joinSignature(signature);
}
} catch (error) {
console.log(error);
toast("Invalid signature.");
}
} catch (error) {
console.log(error);
toast(
"Invalid proof format. Please check the data. It should be [userId,tmpEthAddr,signatureHex,merkleProofHex].",

console.log("claiming with", {
userId,
merkleProof,
tmpEthAddr,
signature,
});
await signed.estimateGas.claimTokens(
userId,
merkleProof,
tmpEthAddr,
signature,
);
verified = true;

if (verified) {
setHaveProof(true);
dispatch(storeProof({ userId, tmpEthAddr, signature, merkleProof }));
} else {
toast("Invalid merkle proof. Please check the data.");
}
} catch (error) {
toast(error.error.message);
}
} catch (error) {
console.log(error);
Expand Down
13 changes: 7 additions & 6 deletions web/src/utils/asn1.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function validateSignature(
hashHex,
signatureArg,
expectedEthAddr,
isASN1
isASN1,
) {
// let hash = "50b2c43fd39106bafbba0da34fc430e1f91e3c96ea2acee2bc34119f92b37750";
// let asn1Signature = "304402204c45f724b4bc4b7994f634f94807701e399731f422f2556d205ffa10df1ab1b302206685617710ad55a5ac4f9b605e5d21461feba47ddf76eea8c581657eebc20734";
Expand Down Expand Up @@ -57,7 +57,7 @@ export function validateSignature(

if (recoveredAddress.toLowerCase() !== expectedEthAddr.toLowerCase()) {
throw new Error(
`Expected ETH addr ${expectedEthAddr}, but recovered ${recoveredAddress}. Signature must be invalid.`
`Expected ETH addr ${expectedEthAddr}, but recovered ${recoveredAddress}. Signature must be invalid.`,
);
}

Expand All @@ -71,7 +71,7 @@ function recoverPubKeyFromSig(msg, r, s, v) {
" r: " +
r.toString(16) +
" s: " +
s.toString(16)
s.toString(16),
);
let rBuffer = r.toArrayLike(Buffer);
let sBuffer = s.toArrayLike(Buffer);
Expand Down Expand Up @@ -102,18 +102,19 @@ function findRightKey(msg, r, s, expectedEthAddr) {
return { pubKey, v };
}

function findEthereumSig(signatureHex) {
export function findEthereumSig(signatureHex) {
let decoded = EcdsaSigAsnParse.decode(signatureHex, "der");
let r = decoded.r;
let s = decoded.s;
console.log("decoded: " + JSON.stringify(decoded));
console.log("r: " + r.toString(10));
console.log("s: " + s.toString(10));

let tempsig = r.toString(16) + s.toString(16);

let secp256k1N = new BN(
"fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141",
16
16,
); // max value on the curve
let secp256k1halfN = secp256k1N.div(new BN(2)); // half of the curve
// Because of EIP-2 not all elliptic curve signatures are accepted
Expand All @@ -124,7 +125,7 @@ function findEthereumSig(signatureHex) {
"s is on the wrong side of the curve... flipping - tempsig: " +
tempsig +
" length: " +
tempsig.length
tempsig.length,
);
// According to EIP2 https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.md
// if s < half the curve we need to invert it
Expand Down

0 comments on commit aab503a

Please sign in to comment.