-
Notifications
You must be signed in to change notification settings - Fork 54
fix(swap): decode non-EVM receiver addresses from Bitcoin #256
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -34,6 +34,7 @@ contract Swap is | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error ApprovalFailed(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error TransferFailed(string); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error InsufficientAmount(string); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
error InvalidMessageLength(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
event TokenSwap( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bytes sender, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -91,13 +92,16 @@ contract Swap is | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (context.chainID == BITCOIN_TESTNET || context.chainID == BITCOIN) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (message.length < 41) revert InvalidMessageLength(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
params.target = BytesHelperLib.bytesToAddress(message, 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
params.to = abi.encodePacked( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
BytesHelperLib.bytesToAddress(message, 20) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (message.length >= 41) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
params.withdraw = BytesHelperLib.bytesToBool(message, 40); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
params.to = new bytes(message.length - 21); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think some comments might help here, and some constants, but up to you btw do we need to open issue to not forget about solana related discussion regarding this topic? or is already there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion! I'm going to refactor this pretty soon to make sure it supports ABI encoding through inscriptions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the Solana issue is just that it doesn't fit in OP_RETURN, nothing has changed on that side. What we should do is switch to inscriptions by default to make Bitcoin naturally compatible with other chains, like swapping BTC into SOL. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (uint256 i = 0; i < message.length - 21; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
params.to[i] = message[20 + i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
params.withdraw = BytesHelperLib.bytesToBool( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
message.length - 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
94
to
105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Validate target address to prevent security issues. There is no validation that the target address (extracted at byte 0) is not the zero address, which could lead to security vulnerabilities. if (message.length < 41) revert InvalidMessageLength();
params.target = BytesHelperLib.bytesToAddress(message, 0);
+ if (params.target == address(0)) revert InvalidAddress();
params.to = new bytes(message.length - 21); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
address targetToken, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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.
💡 Verification agent
🧩 Analysis chain
Consider adding maximum length validation.
While you've added a minimum length validation which is good, consider also adding a maximum length check to prevent excessively large messages that could lead to out-of-gas errors when processing.
Let's check if the codebase handles any potential upper bounds for messages elsewhere:
🏁 Script executed:
Length of output: 1025
Enforce an upper bound on
message.length
To prevent out-of-gas attacks from excessively large payloads, add a maximum length check alongside the existing minimum check.
• File: examples/swap/contracts/Swap.sol
Line: 95
Suggested diff:
Consider defining a named constant for clarity: