|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | +import {Sapphire} from "./Sapphire.sol"; |
| 4 | +import {EthereumUtils, SignatureRSV} from "./EthereumUtils.sol"; |
| 5 | +import {RLPWriter} from "./RLPWriter.sol"; |
| 6 | +import {EIPTypes} from "./EIPTypes.sol"; |
| 7 | + |
| 8 | +/** |
| 9 | + * @title Ethereum EIP-1559 transaction signer & encoder |
| 10 | + */ |
| 11 | +library EIP1559Signer { |
| 12 | + struct EIP1559Tx { |
| 13 | + uint64 nonce; |
| 14 | + uint256 maxPriorityFeePerGas; |
| 15 | + uint256 maxFeePerGas; |
| 16 | + uint64 gasLimit; |
| 17 | + address to; |
| 18 | + uint256 value; |
| 19 | + bytes data; |
| 20 | + EIPTypes.AccessList accessList; |
| 21 | + uint256 chainId; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @notice Encode an unsigned EIP-1559 transaction for signing |
| 26 | + * @param rawTx Transaction to encode |
| 27 | + */ |
| 28 | + function encodeUnsignedTx(EIP1559Tx memory rawTx) |
| 29 | + internal |
| 30 | + pure |
| 31 | + returns (bytes memory) |
| 32 | + { |
| 33 | + bytes[] memory b = new bytes[](9); |
| 34 | + b[0] = RLPWriter.writeUint(rawTx.chainId); |
| 35 | + b[1] = RLPWriter.writeUint(rawTx.nonce); |
| 36 | + b[2] = RLPWriter.writeUint(rawTx.maxPriorityFeePerGas); |
| 37 | + b[3] = RLPWriter.writeUint(rawTx.maxFeePerGas); |
| 38 | + b[4] = RLPWriter.writeUint(rawTx.gasLimit); |
| 39 | + b[5] = RLPWriter.writeAddress(rawTx.to); |
| 40 | + b[6] = RLPWriter.writeUint(rawTx.value); |
| 41 | + b[7] = RLPWriter.writeBytes(rawTx.data); |
| 42 | + b[8] = EIPTypes.encodeAccessList(rawTx.accessList); |
| 43 | + |
| 44 | + // RLP encode the transaction data |
| 45 | + bytes memory rlpEncodedTx = RLPWriter.writeList(b); |
| 46 | + |
| 47 | + // Return the unsigned transaction with EIP-1559 type prefix |
| 48 | + return abi.encodePacked(hex"02", rlpEncodedTx); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @notice Encode a signed EIP-1559 transaction |
| 53 | + * @param rawTx Transaction which was signed |
| 54 | + * @param rsv R, S & V parameters of signature |
| 55 | + */ |
| 56 | + function encodeSignedTx(EIP1559Tx memory rawTx, SignatureRSV memory rsv) |
| 57 | + internal |
| 58 | + pure |
| 59 | + returns (bytes memory) |
| 60 | + { |
| 61 | + bytes[] memory b = new bytes[](12); |
| 62 | + b[0] = RLPWriter.writeUint(rawTx.chainId); |
| 63 | + b[1] = RLPWriter.writeUint(rawTx.nonce); |
| 64 | + b[2] = RLPWriter.writeUint(rawTx.maxPriorityFeePerGas); |
| 65 | + b[3] = RLPWriter.writeUint(rawTx.maxFeePerGas); |
| 66 | + b[4] = RLPWriter.writeUint(rawTx.gasLimit); |
| 67 | + b[5] = RLPWriter.writeAddress(rawTx.to); |
| 68 | + b[6] = RLPWriter.writeUint(rawTx.value); |
| 69 | + b[7] = RLPWriter.writeBytes(rawTx.data); |
| 70 | + b[8] = EIPTypes.encodeAccessList(rawTx.accessList); |
| 71 | + b[9] = RLPWriter.writeUint(uint256(rsv.v)); |
| 72 | + b[10] = RLPWriter.writeUint(uint256(rsv.r)); |
| 73 | + b[11] = RLPWriter.writeUint(uint256(rsv.s)); |
| 74 | + |
| 75 | + // RLP encode the transaction data |
| 76 | + bytes memory rlpEncodedTx = RLPWriter.writeList(b); |
| 77 | + |
| 78 | + // Return the signed transaction with EIP-1559 type prefix |
| 79 | + return abi.encodePacked(hex"02", rlpEncodedTx); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @notice Sign a raw transaction |
| 84 | + * @param rawTx Transaction to sign |
| 85 | + * @param pubkeyAddr Ethereum address of secret key |
| 86 | + * @param secretKey Secret key used to sign |
| 87 | + */ |
| 88 | + function signRawTx( |
| 89 | + EIP1559Tx memory rawTx, |
| 90 | + address pubkeyAddr, |
| 91 | + bytes32 secretKey |
| 92 | + ) internal view returns (SignatureRSV memory ret) { |
| 93 | + // First encode the transaction without signature fields |
| 94 | + bytes memory encoded = encodeUnsignedTx(rawTx); |
| 95 | + |
| 96 | + // Hash the encoded unsigned transaction |
| 97 | + bytes32 digest = keccak256(encoded); |
| 98 | + |
| 99 | + // Sign the hash |
| 100 | + ret = EthereumUtils.sign(pubkeyAddr, secretKey, digest); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @notice Sign a transaction, returning it in EIP-1559 encoded form |
| 105 | + * @param publicAddress Ethereum address of secret key |
| 106 | + * @param secretKey Secret key used to sign |
| 107 | + * @param transaction Transaction to sign |
| 108 | + */ |
| 109 | + function sign( |
| 110 | + address publicAddress, |
| 111 | + bytes32 secretKey, |
| 112 | + EIP1559Tx memory transaction |
| 113 | + ) internal view returns (bytes memory) { |
| 114 | + SignatureRSV memory rsv = signRawTx( |
| 115 | + transaction, |
| 116 | + publicAddress, |
| 117 | + secretKey |
| 118 | + ); |
| 119 | + |
| 120 | + // For EIP-1559, we only need to normalize v to 0/1 |
| 121 | + rsv.v = rsv.v - 27; |
| 122 | + |
| 123 | + return encodeSignedTx(transaction, rsv); |
| 124 | + } |
| 125 | +} |
0 commit comments