|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import {StdConstants} from "forge-std/StdConstants.sol"; |
| 5 | +import {IAllowanceTransfer} from "permit2/interfaces/IAllowanceTransfer.sol"; |
| 6 | +import {ISignatureTransfer} from "permit2/interfaces/ISignatureTransfer.sol"; |
| 7 | +import {EIP2612Permit} from "./EIP2612Permit.sol"; |
| 8 | +import {TypeHashes} from "./TypeHashes.sol"; |
| 9 | + |
| 10 | +/// @title PermitSignatures |
| 11 | +library PermitSignatures { |
| 12 | + using TypeHashes for *; |
| 13 | + |
| 14 | + bytes32 internal constant DAI_DOMAIN_SEPARATOR = 0xdbb8cf42e1ecb028be3f3dbc922e1d878b963f411dc388ced501601c60f7c6f7; |
| 15 | + |
| 16 | + function sign(uint256 privateKey, bytes32 domainSeparator, EIP2612Permit memory permit) |
| 17 | + internal |
| 18 | + pure |
| 19 | + returns (bytes memory signature) |
| 20 | + { |
| 21 | + bytes32 structHash = permit.hash(domainSeparator != DAI_DOMAIN_SEPARATOR); |
| 22 | + return _sign(privateKey, domainSeparator.hashTypedData(structHash)); |
| 23 | + } |
| 24 | + |
| 25 | + function sign(uint256 privateKey, bytes32 domainSeparator, IAllowanceTransfer.PermitSingle memory permit) |
| 26 | + internal |
| 27 | + pure |
| 28 | + returns (bytes memory signature) |
| 29 | + { |
| 30 | + bytes32 structHash = permit.hash(); |
| 31 | + return _sign(privateKey, domainSeparator.hashTypedData(structHash)); |
| 32 | + } |
| 33 | + |
| 34 | + function signCompact(uint256 privateKey, bytes32 domainSeparator, IAllowanceTransfer.PermitSingle memory permit) |
| 35 | + internal |
| 36 | + pure |
| 37 | + returns (bytes memory signature) |
| 38 | + { |
| 39 | + bytes32 structHash = permit.hash(); |
| 40 | + return _signCompact(privateKey, domainSeparator.hashTypedData(structHash)); |
| 41 | + } |
| 42 | + |
| 43 | + function sign(uint256 privateKey, bytes32 domainSeparator, IAllowanceTransfer.PermitBatch memory permit) |
| 44 | + internal |
| 45 | + pure |
| 46 | + returns (bytes memory signature) |
| 47 | + { |
| 48 | + bytes32 structHash = permit.hash(); |
| 49 | + return _sign(privateKey, domainSeparator.hashTypedData(structHash)); |
| 50 | + } |
| 51 | + |
| 52 | + function signCompact(uint256 privateKey, bytes32 domainSeparator, IAllowanceTransfer.PermitBatch memory permit) |
| 53 | + internal |
| 54 | + pure |
| 55 | + returns (bytes memory signature) |
| 56 | + { |
| 57 | + bytes32 structHash = permit.hash(); |
| 58 | + return _signCompact(privateKey, domainSeparator.hashTypedData(structHash)); |
| 59 | + } |
| 60 | + |
| 61 | + function sign( |
| 62 | + uint256 privateKey, |
| 63 | + bytes32 domainSeparator, |
| 64 | + ISignatureTransfer.PermitTransferFrom memory permit, |
| 65 | + address spender |
| 66 | + ) internal pure returns (bytes memory signature) { |
| 67 | + bytes32 structHash = permit.hash(spender); |
| 68 | + return _sign(privateKey, domainSeparator.hashTypedData(structHash)); |
| 69 | + } |
| 70 | + |
| 71 | + function signCompact( |
| 72 | + uint256 privateKey, |
| 73 | + bytes32 domainSeparator, |
| 74 | + ISignatureTransfer.PermitTransferFrom memory permit, |
| 75 | + address spender |
| 76 | + ) internal pure returns (bytes memory signature) { |
| 77 | + bytes32 structHash = permit.hash(spender); |
| 78 | + return _signCompact(privateKey, domainSeparator.hashTypedData(structHash)); |
| 79 | + } |
| 80 | + |
| 81 | + function sign( |
| 82 | + uint256 privateKey, |
| 83 | + bytes32 domainSeparator, |
| 84 | + ISignatureTransfer.PermitBatchTransferFrom memory permit, |
| 85 | + address spender |
| 86 | + ) internal pure returns (bytes memory signature) { |
| 87 | + bytes32 structHash = permit.hash(spender); |
| 88 | + return _sign(privateKey, domainSeparator.hashTypedData(structHash)); |
| 89 | + } |
| 90 | + |
| 91 | + function signCompact( |
| 92 | + uint256 privateKey, |
| 93 | + bytes32 domainSeparator, |
| 94 | + ISignatureTransfer.PermitBatchTransferFrom memory permit, |
| 95 | + address spender |
| 96 | + ) internal pure returns (bytes memory signature) { |
| 97 | + bytes32 structHash = permit.hash(spender); |
| 98 | + return _signCompact(privateKey, domainSeparator.hashTypedData(structHash)); |
| 99 | + } |
| 100 | + |
| 101 | + function sign( |
| 102 | + uint256 privateKey, |
| 103 | + bytes32 domainSeparator, |
| 104 | + ISignatureTransfer.PermitTransferFrom memory permit, |
| 105 | + address spender, |
| 106 | + string memory witnessTypeString, |
| 107 | + bytes32 witness |
| 108 | + ) internal pure returns (bytes memory signature) { |
| 109 | + bytes32 structHash = permit.hash(spender, witnessTypeString, witness); |
| 110 | + return _sign(privateKey, domainSeparator.hashTypedData(structHash)); |
| 111 | + } |
| 112 | + |
| 113 | + function signCompact( |
| 114 | + uint256 privateKey, |
| 115 | + bytes32 domainSeparator, |
| 116 | + ISignatureTransfer.PermitTransferFrom memory permit, |
| 117 | + address spender, |
| 118 | + string memory witnessTypeString, |
| 119 | + bytes32 witness |
| 120 | + ) internal pure returns (bytes memory signature) { |
| 121 | + bytes32 structHash = permit.hash(spender, witnessTypeString, witness); |
| 122 | + return _signCompact(privateKey, domainSeparator.hashTypedData(structHash)); |
| 123 | + } |
| 124 | + |
| 125 | + function sign( |
| 126 | + uint256 privateKey, |
| 127 | + bytes32 domainSeparator, |
| 128 | + ISignatureTransfer.PermitBatchTransferFrom memory permit, |
| 129 | + address spender, |
| 130 | + string memory witnessTypeString, |
| 131 | + bytes32 witness |
| 132 | + ) internal pure returns (bytes memory signature) { |
| 133 | + bytes32 structHash = permit.hash(spender, witnessTypeString, witness); |
| 134 | + return _sign(privateKey, domainSeparator.hashTypedData(structHash)); |
| 135 | + } |
| 136 | + |
| 137 | + function signCompact( |
| 138 | + uint256 privateKey, |
| 139 | + bytes32 domainSeparator, |
| 140 | + ISignatureTransfer.PermitBatchTransferFrom memory permit, |
| 141 | + address spender, |
| 142 | + string memory witnessTypeString, |
| 143 | + bytes32 witness |
| 144 | + ) internal pure returns (bytes memory signature) { |
| 145 | + bytes32 structHash = permit.hash(spender, witnessTypeString, witness); |
| 146 | + return _signCompact(privateKey, domainSeparator.hashTypedData(structHash)); |
| 147 | + } |
| 148 | + |
| 149 | + function _sign(uint256 privateKey, bytes32 digest) private pure returns (bytes memory signature) { |
| 150 | + (uint8 v, bytes32 r, bytes32 s) = StdConstants.VM.sign(privateKey, digest); |
| 151 | + return bytes.concat(r, s, bytes1(v)); |
| 152 | + } |
| 153 | + |
| 154 | + function _signCompact(uint256 privateKey, bytes32 digest) private pure returns (bytes memory signature) { |
| 155 | + (bytes32 r, bytes32 vs) = StdConstants.VM.signCompact(privateKey, digest); |
| 156 | + return bytes.concat(r, vs); |
| 157 | + } |
| 158 | +} |
0 commit comments