优化验证签名的代码
This commit is contained in:
parent
239bcf1591
commit
f548ea0581
@ -30119,12 +30119,12 @@
|
||||
}
|
||||
},
|
||||
"links": {},
|
||||
"address": "0xBCF4C381a0053236ea3DA79BeCe00F2dF3cB404D",
|
||||
"transactionHash": "0x768704273f471789083d4b7816d67d6e3d22b3cc6371c9a07c4f26c5eb5893e8"
|
||||
"address": "0x85A780d8A7AB5fcc4412f053c887B4a6A41ce885",
|
||||
"transactionHash": "0x9b70700a5a89ba90cf8e30afa08a5d98cd866d706bdd9f2e0e11d266dd734ba4"
|
||||
}
|
||||
},
|
||||
"schemaVersion": "3.4.4",
|
||||
"updatedAt": "2022-01-12T03:12:23.405Z",
|
||||
"updatedAt": "2022-01-12T06:14:06.672Z",
|
||||
"networkType": "ethereum",
|
||||
"devdoc": {
|
||||
"kind": "dev",
|
||||
|
@ -18080,12 +18080,12 @@
|
||||
}
|
||||
},
|
||||
"links": {},
|
||||
"address": "0xD733FB8412c09aab9a4b24d06B607bEEA79c5810",
|
||||
"transactionHash": "0xc2e3a40e56b93c879395a606f0cdf4926e0e210624199433f16011624c5cc095"
|
||||
"address": "0x23ea63535771da07F0dbc5169c9515ffCe8C908a",
|
||||
"transactionHash": "0xba84707e1943a789a05b3b27febfef0cefa3d2a2b6637da34910489571e9d8e9"
|
||||
}
|
||||
},
|
||||
"schemaVersion": "3.4.4",
|
||||
"updatedAt": "2022-01-12T03:12:23.441Z",
|
||||
"updatedAt": "2022-01-12T06:14:06.725Z",
|
||||
"networkType": "ethereum",
|
||||
"devdoc": {
|
||||
"kind": "dev",
|
||||
|
@ -30119,12 +30119,12 @@
|
||||
}
|
||||
},
|
||||
"links": {},
|
||||
"address": "0xC7c9a1b210F2048a3e2c016811BC401F625E4287",
|
||||
"transactionHash": "0xec634e5ea358b540ec7189b39d5ca09e2263778dbaad86d126f2d56edb3f32cc"
|
||||
"address": "0x0e436299A0275De90c63684514621E5288c4E583",
|
||||
"transactionHash": "0x13ef22d65ab890aa84adef31cdafb3bff18f32e9b230da361470b7df5785c78f"
|
||||
}
|
||||
},
|
||||
"schemaVersion": "3.4.4",
|
||||
"updatedAt": "2022-01-12T03:12:23.391Z",
|
||||
"updatedAt": "2022-01-12T06:14:06.646Z",
|
||||
"networkType": "ethereum",
|
||||
"devdoc": {
|
||||
"kind": "dev",
|
||||
|
@ -30125,12 +30125,12 @@
|
||||
}
|
||||
},
|
||||
"links": {},
|
||||
"address": "0xe4282f12C6Da2d2A27bBb192C1AA58e3Ad589EFB",
|
||||
"transactionHash": "0x717703990a50a36cfce6ef00a4a997728fde7040ae00b8ff378c71d6f9cbcfe1"
|
||||
"address": "0x51fE79AA2DDB1C5e6D5D00Bd442B032605771F15",
|
||||
"transactionHash": "0x86fd04bb5575e1be623bdf74c0b4af4cb044ae12f87898f57562148ef0a79680"
|
||||
}
|
||||
},
|
||||
"schemaVersion": "3.4.4",
|
||||
"updatedAt": "2022-01-12T03:12:23.375Z",
|
||||
"updatedAt": "2022-01-12T06:14:06.626Z",
|
||||
"networkType": "ethereum",
|
||||
"devdoc": {
|
||||
"kind": "dev",
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -2323,12 +2323,12 @@
|
||||
"1338": {
|
||||
"events": {},
|
||||
"links": {},
|
||||
"address": "0x5A9a2Adb814a7aE8a9d8F5ff0dc9bA9857C3AD62",
|
||||
"transactionHash": "0xcfe20aeec14f2a2d7f3607e3b0a9ce1c6f2a7fe6aa1488ef068fe1f9cd86bf26"
|
||||
"address": "0x3Fb7a6D924AcF05039F13D590d6298CFb52c1092",
|
||||
"transactionHash": "0x070fcbe6e808f2f3b9d746393b71ae4e98175f579711bd956edc77e74250dc05"
|
||||
}
|
||||
},
|
||||
"schemaVersion": "3.4.4",
|
||||
"updatedAt": "2022-01-12T03:12:23.445Z",
|
||||
"updatedAt": "2022-01-12T06:14:06.732Z",
|
||||
"networkType": "ethereum",
|
||||
"devdoc": {
|
||||
"kind": "dev",
|
||||
|
@ -9256,12 +9256,12 @@
|
||||
}
|
||||
},
|
||||
"links": {},
|
||||
"address": "0x701C957291F6771799f852882834bcf8EA400fEe",
|
||||
"transactionHash": "0xab7ddc18b4d9acd77d596ff5b6e928743052d07c29e5bd50df285a06c5199b10"
|
||||
"address": "0x06e2c4d37AC8BBc7a78DC987dD93F980959810fB",
|
||||
"transactionHash": "0x518014fad0c6be58cff21149df0c32e8821ae2b9ad40d8d892a50b3ce76c6ed2"
|
||||
}
|
||||
},
|
||||
"schemaVersion": "3.4.4",
|
||||
"updatedAt": "2022-01-12T03:12:23.416Z",
|
||||
"updatedAt": "2022-01-12T06:14:06.685Z",
|
||||
"networkType": "ethereum",
|
||||
"devdoc": {
|
||||
"kind": "dev",
|
||||
|
@ -1,63 +1,20 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.0;
|
||||
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
|
||||
|
||||
contract HasSignature {
|
||||
function getEthSignedMessageHash(bytes32 _messageHash)
|
||||
public
|
||||
pure
|
||||
returns (bytes32)
|
||||
{
|
||||
/*
|
||||
Signature is produced by signing a keccak256 hash with the following format:
|
||||
"\x19Ethereum Signed Message\n" + len(msg) + msg
|
||||
*/
|
||||
return
|
||||
keccak256(
|
||||
abi.encodePacked(
|
||||
"\x19Ethereum Signed Message:\n32",
|
||||
_messageHash
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function recoverSigner(
|
||||
bytes32 _ethSignedMessageHash,
|
||||
bytes memory _signature
|
||||
) public pure returns (address) {
|
||||
(bytes32 r, bytes32 s, uint8 v) = splitSignature(_signature);
|
||||
|
||||
return ecrecover(_ethSignedMessageHash, v, r, s);
|
||||
}
|
||||
|
||||
function splitSignature(bytes memory sig)
|
||||
public
|
||||
pure
|
||||
returns (
|
||||
bytes32 r,
|
||||
bytes32 s,
|
||||
uint8 v
|
||||
)
|
||||
{
|
||||
require(sig.length == 65, "invalid signature length");
|
||||
|
||||
assembly {
|
||||
/*
|
||||
First 32 bytes stores the length of the signature
|
||||
|
||||
add(sig, 32) = pointer of sig + 32
|
||||
effectively, skips first 32 bytes of signature
|
||||
|
||||
mload(p) loads next 32 bytes starting at the memory address p into memory
|
||||
*/
|
||||
|
||||
// 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)))
|
||||
}
|
||||
|
||||
// implicitly return (r, s, v)
|
||||
function checkSigner(
|
||||
address signer,
|
||||
bytes32 hash,
|
||||
bytes memory signature
|
||||
) public pure {
|
||||
require(signature.length == 65, "[BE] invalid signature length");
|
||||
bytes32 ethSignedMessageHash = ECDSA.toEthSignedMessageHash(
|
||||
hash
|
||||
);
|
||||
|
||||
address recovered = ECDSA.recover(ethSignedMessageHash, signature);
|
||||
require(recovered == signer, "[BE] invalid seller signature");
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ import "@openzeppelin/contracts/utils/math/SafeMath.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import "@openzeppelin/contracts/access/Ownable.sol";
|
||||
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
|
||||
import "./HasSignature.sol";
|
||||
|
||||
contract Marketplace is Ownable, HasSignature {
|
||||
@ -79,14 +80,7 @@ contract Marketplace is Ownable, HasSignature {
|
||||
values[2]
|
||||
);
|
||||
|
||||
bytes32 ethSignedMessageHash = getEthSignedMessageHash(
|
||||
criteriaMessageHash
|
||||
);
|
||||
|
||||
require(
|
||||
recoverSigner(ethSignedMessageHash, signature) == _msgSender(),
|
||||
"Marketplace: invalid signature"
|
||||
);
|
||||
checkSigner(_msgSender(), criteriaMessageHash, signature);
|
||||
|
||||
usedSignatures[signature] = true;
|
||||
}
|
||||
@ -120,14 +114,7 @@ contract Marketplace is Ownable, HasSignature {
|
||||
values[2]
|
||||
);
|
||||
|
||||
bytes32 ethSignedMessageHash = getEthSignedMessageHash(
|
||||
criteriaMessageHash
|
||||
);
|
||||
|
||||
require(
|
||||
recoverSigner(ethSignedMessageHash, signature) == addresses[0],
|
||||
"Marketplace: invalid seller signature"
|
||||
);
|
||||
checkSigner(addresses[0], criteriaMessageHash, signature);
|
||||
|
||||
// check current ownership
|
||||
IERC721 nft = IERC721(addresses[1]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user