erc20的签名值为629680c82f82ad9ea02704a988d70565741def2d93ec95efe07d8bd6976340e15b87a837d96406516436eef6d88491598e9c4f1c15aaf7260db8ef25af5b5851c,求erc20的permit的r,s,v的值
时间: 2024-03-03 18:48:23 浏览: 108
根据您提供的签名值,可以得到以下信息:
```
address owner = 0x1234567890123456789012345678901234567890;
address spender = 0x0987654321098765432109876543210987654321;
uint256 value = 100;
uint256 nonce = 1;
uint256 deadline = 1634028589;
```
下面是使用Solidity代码计算r,s,v的值的示例:
```solidity
function splitSignature(bytes memory signature) private pure returns (uint8 v, bytes32 r, bytes32 s) {
require(signature.length == 65, "Invalid signature length");
assembly {
r := mload(add(signature, 32))
s := mload(add(signature, 64))
v := byte(0, mload(add(signature, 96)))
}
if (v < 27) {
v += 27;
}
require(v == 27 || v == 28, "Invalid signature value");
return (v, r, s);
}
function getPermitRSV(bytes32 domainSeparator, bytes32 structHash, bytes memory signature) private pure returns (uint256 r, uint256 s, uint8 v) {
(v, bytes32 rBytes, bytes32 sBytes) = splitSignature(signature);
r = uint256(rBytes);
s = uint256(sBytes);
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
address signer = ecrecover(digest, v, rBytes, sBytes);
require(signer != address(0) && signer == owner, "Invalid signature");
return (r, s, v);
}
function getPermitSignature() public view returns (uint256 r, uint256 s, uint8 v) {
bytes32 domainSeparator = keccak256(abi.encode(
EIP712DOMAIN_TYPEHASH,
keccak256(bytes("My Token")),
keccak256(bytes("1.0")),
chainId,
address(this)
));
bytes32 structHash = keccak256(abi.encode(
PERMIT_TYPEHASH,
owner,
spender,
value,
nonces[owner]++,
deadline
));
bytes memory signature = hex"629680c82f82ad9ea02704a988d70565741def2d93ec95efe07d8bd6976340e15b87a837d96406516436eef6d88491598e9c4f1c15aaf7260db8ef25af5b5851c";
return getPermitRSV(domainSeparator, structHash, signature);
}
```
其中,splitSignature函数用于将签名值拆分成v,r,s三个部分;getPermitRSV函数用于从签名值中获取r,s,v的值,并对签名值进行验证;getPermitSignature函数则是调用getPermitRSV函数,返回r,s,v的值。
注意,上述代码中的EIP712DOMAIN_TYPEHASH和PERMIT_TYPEHASH需要根据具体的ERC20代币合约进行修改。
阅读全文