Skip to content

Commit a18c083

Browse files
update overconstrained_l1_l2_interaction
1 parent 03d3129 commit a18c083

File tree

1 file changed

+31
-23
lines changed
  • not-so-smart-contracts/cairo/overconstrained_l1_l2_interaction

1 file changed

+31
-23
lines changed

not-so-smart-contracts/cairo/overconstrained_l1_l2_interaction/README.md

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,51 @@ When interacting with contracts that are designed to interact with both L1 and L
44

55
## Example
66

7-
The following Starknet bridge contract allows for permissionless deposit to any address from L1 via the `deposit_to_L2`. In particular, someone can deposit tokens to the `bad_address`.However the tokens will be trapped on L2 because the L2 contract's `deposit_from_L1` function is not permissionless and prevents `bad_address` from being the recipient.
7+
The following Starknet bridge contract allows for permissionless deposit to any address on L1 via the `deposit_to_L1` function. In particular, someone can deposit tokens to the `BAD_ADDRESS`.However the tokens will be trapped on L1 because the L1 contract's `depositFromL2` function is not permissionless and prevents `BAD_ADDRESS` from being the recipient.
88

9-
```solidity
10-
uint256 public immutable DEPOSIT_SELECTOR;
11-
address public immutable MESSENGER_CONTRACT;
12-
address public immutable L2_BRIDGE_ADDRESS;
13-
14-
constructor(uint256 _selector, address _messenger, address _bridge) {
15-
DEPOSIT_SELECTOR = _selector;
16-
MESSENGER_CONTRACT = _messenger;
17-
L2_BRIDGE_ADDRESS = _bridge;
9+
```Cairo
10+
#[storage]
11+
struct Storage {
12+
l1_bridge: EthAddress,
1813
1914
}
15+
#[derive(Serde)]
16+
struct Deposit {
17+
recipient: EthAddress,
18+
token: EthAddress,
19+
amount: u256
20+
}
2021
21-
function depositToL2(uint256[] calldata payload) external {
22-
require(owner == msg.sender, "not owner");
23-
IStarknetMessaging(MESSENGER_CONTRACT).sendMessageToL2(L2_BRIDGE_ADDRESS, DEPOSIT_SELECTOR, payload);
22+
#[l1_handler]
23+
fn deposit_to_l1(ref self:ContractState, deposit: Deposit) {
24+
let payload = ArrayTrait::new();
25+
starknet::send_message_to_l1_syscall(self.l1_bridge.read(),deposit.serialize(ref payload)).unwrap();
2426
}
2527
```
2628

27-
```Cairo
28-
#[storage]
29-
struct Storage {
30-
owner: ContractAddress,
31-
l1_bridge: EthAddress,
32-
bad_address: ContractAddress
29+
```solidity
30+
31+
address public immutable MESSENGER_CONTRACT;
32+
address public immutable L2_TOKEN_BRIDGE;
33+
address public constant BAD_ADDRESS = address(0xdead);
3334
35+
constructor(address _messenger, address _bridge) {
36+
MESSENGER_CONTRACT = _messenger;
37+
L2_TOKEN_BRIDGE = _bridge;
3438
}
3539
36-
#[l1_handler]
37-
fn deposit_from_l1(ref self:ContractState, from_address: felt252, recipient: ContractAddress, amount) {
38-
assert(from_address == l1_bridge, "not bridge");
39-
assert(recipient != bad_address, "not allowed to deposit");
40+
function depositFromL2(address recipient, address token, uint256 amount) external {
41+
require(recipient != BAD_ADDRESS, "blacklisted");
42+
uint256[] memory payload = _buildPayload(recipient,token,amount);
43+
MESSENGER_CONTRACT.consumeMessageFromL2(L2_TOKEN_BRIDGE,payload);
4044
//deposit logic
4145
[...]
4246
}
4347
48+
function _buildPayload(address recipient, address token, uint256 amount) internal returns (uint256[] memory) {
49+
//payload building logic for Starknet message
50+
[...]
51+
}
4452
```
4553
## Mitigations
4654

0 commit comments

Comments
 (0)