Skip to content

Commit

Permalink
Format signer contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and CedarMist committed Nov 23, 2024
1 parent afe5b66 commit b971cb2
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 155 deletions.
18 changes: 9 additions & 9 deletions contracts/contracts/EIP1559Signer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ library EIP1559Signer {
b[6] = RLPWriter.writeUint(rawTx.value);
b[7] = RLPWriter.writeBytes(rawTx.data);
b[8] = EIPTypes.encodeAccessList(rawTx.accessList);

// RLP encode the transaction data
bytes memory rlpEncodedTx = RLPWriter.writeList(b);

// Return the unsigned transaction with EIP-1559 type prefix
return abi.encodePacked(hex"02", rlpEncodedTx);
}
Expand Down Expand Up @@ -71,10 +71,10 @@ library EIP1559Signer {
b[9] = RLPWriter.writeUint(uint256(rsv.v));
b[10] = RLPWriter.writeUint(uint256(rsv.r));
b[11] = RLPWriter.writeUint(uint256(rsv.s));

// RLP encode the transaction data
bytes memory rlpEncodedTx = RLPWriter.writeList(b);

// Return the signed transaction with EIP-1559 type prefix
return abi.encodePacked(hex"02", rlpEncodedTx);
}
Expand All @@ -92,10 +92,10 @@ library EIP1559Signer {
) internal view returns (SignatureRSV memory ret) {
// First encode the transaction without signature fields
bytes memory encoded = encodeUnsignedTx(rawTx);

// Hash the encoded unsigned transaction
bytes32 digest = keccak256(encoded);

// Sign the hash
ret = EthereumUtils.sign(pubkeyAddr, secretKey, digest);
}
Expand All @@ -116,10 +116,10 @@ library EIP1559Signer {
publicAddress,
secretKey
);

// For EIP-1559, we only need to normalize v to 0/1
rsv.v = rsv.v - 27;

return encodeSignedTx(transaction, rsv);
}
}
}
18 changes: 9 additions & 9 deletions contracts/contracts/EIP2930Signer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ library EIP2930Signer {
b[5] = RLPWriter.writeUint(rawTx.value);
b[6] = RLPWriter.writeBytes(rawTx.data);
b[7] = EIPTypes.encodeAccessList(rawTx.accessList);

// RLP encode the transaction data
bytes memory rlpEncodedTx = RLPWriter.writeList(b);

// Return the unsigned transaction with EIP-2930 type prefix
return abi.encodePacked(hex"01", rlpEncodedTx);
}
Expand All @@ -68,10 +68,10 @@ library EIP2930Signer {
b[8] = RLPWriter.writeUint(uint256(rsv.v));
b[9] = RLPWriter.writeUint(uint256(rsv.r));
b[10] = RLPWriter.writeUint(uint256(rsv.s));

// RLP encode the transaction data
bytes memory rlpEncodedTx = RLPWriter.writeList(b);

// Return the signed transaction with EIP-2930 type prefix
return abi.encodePacked(hex"01", rlpEncodedTx);
}
Expand All @@ -89,10 +89,10 @@ library EIP2930Signer {
) internal view returns (SignatureRSV memory ret) {
// First encode the transaction without signature fields
bytes memory encoded = encodeUnsignedTx(rawTx);

// Hash the encoded unsigned transaction
bytes32 digest = keccak256(encoded);

// Sign the hash
ret = EthereumUtils.sign(pubkeyAddr, secretKey, digest);
}
Expand All @@ -113,10 +113,10 @@ library EIP2930Signer {
publicAddress,
secretKey
);

// For EIP-2930, we only need to normalize v to 0/1
rsv.v = rsv.v - 27;

return encodeSignedTx(transaction, rsv);
}
}
}
26 changes: 15 additions & 11 deletions contracts/contracts/EIPTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ library EIPTypes {
struct AccessList {
AccessListItem[] items;
}

struct AccessListItem {
address addr;
bytes32[] storageKeys;
}

/**
* @notice Encode an access list for EIP-1559 and EIP-2930 transactions
*/
Expand All @@ -21,23 +21,27 @@ library EIPTypes {
returns (bytes memory)
{
bytes[] memory items = new bytes[](list.items.length);
for (uint i = 0; i < list.items.length; i++) {

for (uint256 i = 0; i < list.items.length; i++) {
bytes[] memory item = new bytes[](2);
// Encode the address
item[0] = RLPWriter.writeAddress(list.items[i].addr);

// Encode storage keys
bytes[] memory storageKeys = new bytes[](list.items[i].storageKeys.length);
for (uint j = 0; j < list.items[i].storageKeys.length; j++) {
bytes[] memory storageKeys = new bytes[](
list.items[i].storageKeys.length
);
for (uint256 j = 0; j < list.items[i].storageKeys.length; j++) {
// Use writeBytes for the full storage key
storageKeys[j] = RLPWriter.writeBytes(abi.encodePacked(list.items[i].storageKeys[j]));
storageKeys[j] = RLPWriter.writeBytes(
abi.encodePacked(list.items[i].storageKeys[j])
);
}
item[1] = RLPWriter.writeList(storageKeys);

items[i] = RLPWriter.writeList(item);
}

return RLPWriter.writeList(items);
}
}
}
Loading

0 comments on commit b971cb2

Please sign in to comment.