signature = await signer._signTypedData(domain, types, value);
// '0x463b9c9971d1a144507d2e905f4e98becd159139421a4bb8d3c9c2ed04eb401057dd0698d504fd6ca48829a3c8a7a98c1c961eae617096cb54264bbdd082e13d1c'
const domain = {
name: "og-nft",
version: "1",
};
const types = {
NFTVoucher: [
{ name: "URI", type: "string" },
{ name: "price", type: "uint256" },
],
};
i am using above function in etherjs library to get the signature, and when I use this signature in smart contract to recover the address of signer, the address comes out to be completely random.
in the contract, I am using
function _hash(NFTVoucher calldata voucher)
internal
view
returns (bytes32)
{
console.log("voucher.price",voucher.price);
return
_hashTypedDataV4(
keccak256(
abi.encode(
keccak256(
"NFTVoucher(string URI,uint256 price)"
),
keccak256(bytes(voucher.URI)),
voucher.price
)
)
);
}
function verifyVoucher(NFTVoucher calldata voucher, bytes memory signature)
public
view
returns (address)
{
require(voucher.price > 0, "Price must be greater than 0");
bytes32 hash = _hash(voucher);
return ECDSA.recover(hash, signature);
}
// below is the constructor for domain separator,
constructor() ERC721("nftStoreBuilder", "Hash") EIP712("og-nft", "1") {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
}
now when I pass down the signature from the frontend
address _signer = verifyVoucher(voucher, signature);
console.log(_signer,"signer");
console.log(owner,"owner");
0xe8c795f9168269940b31a470ad82e89a453e88b9 signer
0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 owner
BOTH ARE MISMATCHING