Created
March 7, 2022 16:01
-
-
Save DhairyaSethi/5ab1c9232a3d0cac593037716c5642e8 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8.0; | |
contract VerifySign { | |
struct EIP712Domain { | |
string name; | |
string version; | |
uint256 chainId; | |
address verifyingContract; | |
} | |
struct BorrowerSignData { | |
uint256 nftID; | |
uint256 borrowerNonce; | |
address borrowerAddress; | |
address nftContractAddress; | |
} | |
bytes32 constant EIP712DOMAIN_TYPEHASH = keccak256( | |
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" | |
); | |
bytes32 constant DATA_TYPEHASH = keccak256( | |
"BorrowerSignData(uint256 nftID,uint256 borrowerNonce,address borrowerAddress,address nftContractAddress)" | |
); | |
function verify(uint256 nftID, uint256 borrowerNonce, address borrowerAddress, address nftContractAddress, bytes32 signedMessage) internal view returns (bool) { | |
uint8 v; | |
bytes32 r; | |
bytes32 s; | |
(v, r, s) = splitSignature(signedMessage); | |
bytes32 DOMAIN_SEPARATOR = | |
keccak256(abi.encode( | |
EIP712DOMAIN_TYPEHASH, | |
"NordLoan Promissory Note" | |
'1', // version | |
'4', // chain id | |
address(this) | |
)); | |
bytes32 dataHash = keccak256(abi.encode( | |
DATA_TYPEHASH, | |
nftID, | |
borrowerNonce, | |
borrowerAddress, | |
nftContractAddress | |
)); | |
// Note: we need to use `encodePacked` here instead of `encode`. | |
bytes32 digest = keccak256(abi.encodePacked( | |
"\x19\x01", | |
DOMAIN_SEPARATOR, | |
dataHash | |
)); | |
return ecrecover(digest, v, r, s) == borrowerAddress; | |
} | |
function splitSignature(bytes32 sig) | |
internal | |
pure | |
returns (uint8, bytes32, bytes32) | |
{ | |
require(sig.length == 65); | |
bytes32 r; | |
bytes32 s; | |
uint8 v; | |
assembly { | |
// first 32 bytes, after the length prefix | |
r := mload(add(sig, 32)) | |
// second 32 bytes | |
s := mload(add(sig, 64)) | |
// final byte (first byte of the next 32 bytes) | |
v := byte(0, mload(add(sig, 96))) | |
} | |
return (v, r, s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment