-
Notifications
You must be signed in to change notification settings - Fork 3
Improve batch wire format #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,42 @@ | ||
| # Constants for Facet Batch V2 protocol | ||
| module FacetBatchConstants | ||
| # Magic prefix to identify batch payloads | ||
| MAGIC_PREFIX = ByteString.from_hex("0x0000000000012345") | ||
| # Magic prefix ("unstoppable sequencing" ASCII -> hex) | ||
| MAGIC_PREFIX = ByteString.from_hex("0x756e73746f707061626c652073657175656e63696e67") | ||
|
|
||
| # Protocol version | ||
| VERSION = 1 | ||
|
|
||
|
|
||
| # Wire format header sizes (in bytes) | ||
| MAGIC_SIZE = MAGIC_PREFIX.to_bin.bytesize | ||
| CHAIN_ID_SIZE = 8 # uint64 | ||
| VERSION_SIZE = 1 # uint8 | ||
| ROLE_SIZE = 1 # uint8 | ||
| LENGTH_SIZE = 4 # uint32 | ||
| HEADER_SIZE = MAGIC_SIZE + CHAIN_ID_SIZE + VERSION_SIZE + ROLE_SIZE + LENGTH_SIZE # 36 bytes | ||
| SIGNATURE_SIZE = 65 # secp256k1: r(32) + s(32) + v(1) | ||
|
|
||
| # Wire format offsets | ||
| MAGIC_OFFSET = 0 | ||
| CHAIN_ID_OFFSET = MAGIC_SIZE | ||
| VERSION_OFFSET = CHAIN_ID_OFFSET + CHAIN_ID_SIZE | ||
| ROLE_OFFSET = VERSION_OFFSET + VERSION_SIZE | ||
| LENGTH_OFFSET = ROLE_OFFSET + ROLE_SIZE | ||
| RLP_OFFSET = HEADER_SIZE | ||
|
|
||
| # Size limits | ||
| MAX_BATCH_BYTES = Integer(ENV.fetch('MAX_BATCH_BYTES', 131_072)) # 128KB default | ||
| MAX_TXS_PER_BATCH = Integer(ENV.fetch('MAX_TXS_PER_BATCH', 1000)) | ||
| MAX_BATCHES_PER_PAYLOAD = Integer(ENV.fetch('MAX_BATCHES_PER_PAYLOAD', 10)) | ||
|
|
||
| # Batch roles | ||
| module Role | ||
| FORCED = 0x00 # Anyone can post, no signature required | ||
| PRIORITY = 0x01 # Requires authorized signature | ||
| PERMISSIONLESS = 0x00 # Anyone can post, no signature required (formerly FORCED) | ||
| PRIORITY = 0x01 # Requires authorized signature | ||
| end | ||
|
|
||
| # Source types for tracking where batch came from | ||
| module Source | ||
| CALLDATA = 'calldata' | ||
| BLOB = 'blob' | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,99 +1,54 @@ | ||
| # EIP-712 signature verification for Facet batches | ||
| # Signature verification for Facet batches | ||
| class BatchSignatureVerifier | ||
| include SysConfig | ||
|
|
||
| # EIP-712 domain | ||
| DOMAIN_NAME = "FacetBatch" | ||
| DOMAIN_VERSION = "1" | ||
|
|
||
| # Type hash for FacetBatchData | ||
| # struct FacetBatchData { | ||
| # uint8 version; | ||
| # uint256 chainId; | ||
| # uint8 role; | ||
| # uint64 targetL1Block; | ||
| # bytes[] transactions; | ||
| # bytes extraData; | ||
| # } | ||
| BATCH_DATA_TYPE_HASH = Eth::Util.keccak256( | ||
| "FacetBatchData(uint8 version,uint256 chainId,uint8 role,uint64 targetL1Block,bytes[] transactions,bytes extraData)" | ||
| ) | ||
|
|
||
|
|
||
| attr_reader :chain_id | ||
|
|
||
| def initialize(chain_id: ChainIdManager.current_l2_chain_id) | ||
| @chain_id = chain_id | ||
| end | ||
| # Verify a batch signature and return the signer address | ||
| # Returns nil if signature is invalid or missing | ||
| # batch_data_rlp: The RLP array [version, chainId, role, targetL1Block, transactions[], extraData] | ||
| def verify(batch_data_rlp, signature) | ||
|
|
||
| # Verify signature for new wire format | ||
| # signed_data: [CHAIN_ID:8][VERSION:1][ROLE:1][RLP_TX_LIST] | ||
| # signature: 65-byte secp256k1 signature | ||
| def verify_wire_format(signed_data, signature) | ||
| return nil unless signature | ||
|
|
||
| sig_bytes = signature.is_a?(ByteString) ? signature.to_bin : signature | ||
| return nil unless sig_bytes.length == 65 | ||
| # Calculate EIP-712 hash of the RLP-encoded batch data | ||
| message_hash = eip712_hash_rlp(batch_data_rlp) | ||
|
|
||
| # Hash the signed data | ||
| message_hash = Eth::Util.keccak256(signed_data) | ||
|
|
||
| # Recover signer from signature | ||
| recover_signer(message_hash, sig_bytes) | ||
| rescue => e | ||
| Rails.logger.debug "Signature verification failed: #{e.message}" | ||
| nil | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def domain_separator | ||
| # EIP-712 domain separator | ||
| @domain_separator ||= begin | ||
| domain_type_hash = Eth::Util.keccak256( | ||
| "EIP712Domain(string name,string version,uint256 chainId)" | ||
| ) | ||
|
|
||
| encoded = [ | ||
| domain_type_hash, | ||
| Eth::Util.keccak256(DOMAIN_NAME), | ||
| Eth::Util.keccak256(DOMAIN_VERSION), | ||
| Eth::Util.zpad_int(chain_id, 32) | ||
| ].join | ||
|
|
||
| Eth::Util.keccak256(encoded) | ||
| rescue StandardError => e | ||
| if signature_error?(e) | ||
| Rails.logger.debug "Signature verification failed: #{e.message}" | ||
| nil | ||
| else | ||
| raise | ||
| end | ||
| end | ||
|
|
||
| def eip712_hash_rlp(batch_data_rlp) | ||
| # For RLP batches, we sign the keccak256 of the RLP-encoded FacetBatchData | ||
| # This is simpler and more standard than EIP-712 structured data | ||
| batch_data_encoded = Eth::Rlp.encode(batch_data_rlp) | ||
|
|
||
| # Create the message to sign: Ethereum signed message prefix + hash | ||
| message_hash = Eth::Util.keccak256(batch_data_encoded) | ||
|
|
||
| # Apply EIP-191 personal message signing format | ||
| # "\x19Ethereum Signed Message:\n32" + message_hash | ||
| prefix = "\x19Ethereum Signed Message:\n32" | ||
| Eth::Util.keccak256(prefix + message_hash) | ||
| end | ||
|
|
||
| def hash_transactions_array(transactions) | ||
| # Hash array of transactions according to EIP-712 | ||
| # Each transaction is hashed, then the array of hashes is hashed | ||
| tx_hashes = transactions.map { |tx| Eth::Util.keccak256(tx.to_bin) } | ||
| encoded = tx_hashes.join | ||
| Eth::Util.keccak256(encoded) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def recover_signer(message_hash, sig_bytes) | ||
| # Extract r, s, v from signature | ||
| r = sig_bytes[0, 32] | ||
| s = sig_bytes[32, 32] | ||
| v = sig_bytes[64].ord | ||
|
|
||
| # Adjust v for EIP-155 | ||
| v = v < 27 ? v + 27 : v | ||
| raw_v = sig_bytes[64].ord | ||
|
|
||
| # Normalise recovery id so both {0,1} and {27,28} inputs are accepted | ||
| v_normalised = raw_v | ||
| v_normalised -= 27 if v_normalised >= 27 | ||
|
|
||
| unless [0, 1].include?(v_normalised) | ||
| error_class = defined?(Eth::Signature::SignatureError) ? Eth::Signature::SignatureError : StandardError | ||
| raise error_class, "Invalid recovery id #{raw_v}" | ||
| end | ||
|
|
||
| v = v_normalised + 27 | ||
|
|
||
| # Create signature for recovery | ||
| # The eth.rb gem expects r (32 bytes) + s (32 bytes) + v (variable length hex) | ||
|
|
@@ -110,4 +65,14 @@ def recover_signer(message_hash, sig_bytes) | |
|
|
||
| Address20.from_hex(address) | ||
| end | ||
| end | ||
|
|
||
| def signature_error?(error) | ||
| return true if defined?(Eth::Signature::SignatureError) && error.is_a?(Eth::Signature::SignatureError) | ||
|
|
||
| if defined?(Secp256k1) && Secp256k1.const_defined?(:Error) | ||
| return true if error.is_a?(Secp256k1.const_get(:Error)) | ||
| end | ||
|
|
||
| false | ||
| end | ||
|
Comment on lines
+69
to
+77
|
||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error class selection logic is repeated in the
signature_error?method. Consider extracting this into a helper method or using a consistent approach for signature error handling.