Skip to content

Commit

Permalink
feat: purge non native token + reorder params in token portal (#2723)
Browse files Browse the repository at this point in the history
* Fix #2291
* Create `token_portal_content_hash_lib` folder in noir-contracts that
gets used by both token-bridge and test-contract.
* reorder params in token portal and token bridge
  • Loading branch information
rahul-kothari authored Oct 12, 2023
1 parent a3649df commit 447dade
Show file tree
Hide file tree
Showing 33 changed files with 252 additions and 1,060 deletions.
2 changes: 1 addition & 1 deletion docs/docs/dev_docs/contracts/portals/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Computing the `content` must be done manually in its current form, as we are sti
:::info
The `content_hash` is a sha256 truncated to a field element (~ 254 bits). In Aztec-nr, you can use our `sha256_to_field()` to do a sha256 hash which fits in one field element:

#include_code mint_public_content_hash_nr /yarn-project/noir-contracts/src/contracts/token_bridge_contract/src/util.nr rust
#include_code mint_public_content_hash_nr /yarn-project/noir-contracts/src/contracts/token_portal_content_hash_lib/src/lib.nr rust

In solidity, you can use our `Hash.sha256ToField()` method:

Expand Down
36 changes: 18 additions & 18 deletions l1-contracts/test/portals/TokenPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ contract TokenPortal {
// docs:start:deposit_public
/**
* @notice Deposit funds into the portal and adds an L2 message which can only be consumed publicly on Aztec
* @param _amount - The amount to deposit
* @param _to - The aztec address of the recipient
* @param _amount - The amount to deposit
* @param _canceller - The address that can cancel the L1 to L2 message
* @param _deadline - The timestamp after which the entry can be cancelled
* @param _secretHash - The hash of the secret consumable message. The hash should be 254 bits (so it can fit in a Field element)
* @return The key of the entry in the Inbox
*/
function depositToAztecPublic(
uint256 _amount,
bytes32 _to,
uint256 _amount,
address _canceller,
uint32 _deadline,
bytes32 _secretHash
Expand All @@ -47,7 +47,7 @@ contract TokenPortal {

// Hash the message content to be reconstructed in the receiving contract
bytes32 contentHash = Hash.sha256ToField(
abi.encodeWithSignature("mint_public(uint256,bytes32,address)", _amount, _to, _canceller)
abi.encodeWithSignature("mint_public(bytes32,uint256,address)", _to, _amount, _canceller)
);

// Hold the tokens in the portal
Expand All @@ -60,16 +60,16 @@ contract TokenPortal {

/**
* @notice Deposit funds into the portal and adds an L2 message which can only be consumed privately on Aztec
* @param _amount - The amount to deposit
* @param _secretHashForRedeemingMintedNotes - The hash of the secret to redeem minted notes privately on Aztec. The hash should be 254 bits (so it can fit in a Field element)
* @param _amount - The amount to deposit
* @param _canceller - The address that can cancel the L1 to L2 message
* @param _deadline - The timestamp after which the entry can be cancelled
* @param _secretHashForL2MessageConsumption - The hash of the secret consumable L1 to L2 message. The hash should be 254 bits (so it can fit in a Field element)
* @return The key of the entry in the Inbox
*/
function depositToAztecPrivate(
uint256 _amount,
bytes32 _secretHashForRedeemingMintedNotes,
uint256 _amount,
address _canceller,
uint32 _deadline,
bytes32 _secretHashForL2MessageConsumption
Expand All @@ -81,9 +81,9 @@ contract TokenPortal {
// Hash the message content to be reconstructed in the receiving contract
bytes32 contentHash = Hash.sha256ToField(
abi.encodeWithSignature(
"mint_private(uint256,bytes32,address)",
_amount,
"mint_private(bytes32,uint256,address)",
_secretHashForRedeemingMintedNotes,
_amount,
_canceller
)
);
Expand All @@ -101,16 +101,16 @@ contract TokenPortal {
/**
* @notice Cancel a public depositToAztec L1 to L2 message
* @dev only callable by the `canceller` of the message
* @param _amount - The amount to deposit per the original message
* @param _to - The aztec address of the recipient in the original message
* @param _amount - The amount to deposit per the original message
* @param _deadline - The timestamp after which the entry can be cancelled
* @param _secretHash - The hash of the secret consumable message in the original message
* @param _fee - The fee paid to the sequencer
* @return The key of the entry in the Inbox
*/
function cancelL1ToAztecMessagePublic(
uint256 _amount,
bytes32 _to,
uint256 _amount,
uint32 _deadline,
bytes32 _secretHash,
uint64 _fee
Expand All @@ -122,7 +122,7 @@ contract TokenPortal {
sender: l1Actor,
recipient: l2Actor,
content: Hash.sha256ToField(
abi.encodeWithSignature("mint_public(uint256,bytes32,address)", _amount, _to, msg.sender)
abi.encodeWithSignature("mint_public(bytes32,uint256,address)", _to, _amount, msg.sender)
),
secretHash: _secretHash,
deadline: _deadline,
Expand All @@ -138,16 +138,16 @@ contract TokenPortal {
/**
* @notice Cancel a private depositToAztec L1 to L2 message
* @dev only callable by the `canceller` of the message
* @param _amount - The amount to deposit per the original message
* @param _secretHashForRedeemingMintedNotes - The hash of the secret to redeem minted notes privately on Aztec
* @param _amount - The amount to deposit per the original message
* @param _deadline - The timestamp after which the entry can be cancelled
* @param _secretHashForL2MessageConsumption - The hash of the secret consumable L1 to L2 message
* @param _fee - The fee paid to the sequencer
* @return The key of the entry in the Inbox
*/
function cancelL1ToAztecMessagePrivate(
uint256 _amount,
bytes32 _secretHashForRedeemingMintedNotes,
uint256 _amount,
uint32 _deadline,
bytes32 _secretHashForL2MessageConsumption,
uint64 _fee
Expand All @@ -160,9 +160,9 @@ contract TokenPortal {
recipient: l2Actor,
content: Hash.sha256ToField(
abi.encodeWithSignature(
"mint_private(uint256,bytes32,address)",
_amount,
"mint_private(bytes32,uint256,address)",
_secretHashForRedeemingMintedNotes,
_amount,
msg.sender
)
),
Expand All @@ -183,13 +183,13 @@ contract TokenPortal {
/**
* @notice Withdraw funds from the portal
* @dev Second part of withdraw, must be initiated from L2 first as it will consume a message from outbox
* @param _amount - The amount to withdraw
* @param _recipient - The address to send the funds to
* @param _amount - The amount to withdraw
* @param _withCaller - Flag to use `msg.sender` as caller, otherwise address(0)
* Must match the caller of the message (specified from L2) to consume it.
* @return The key of the entry in the Outbox
*/
function withdraw(uint256 _amount, address _recipient, bool _withCaller)
function withdraw(address _recipient, uint256 _amount, bool _withCaller)
external
returns (bytes32)
{
Expand All @@ -198,9 +198,9 @@ contract TokenPortal {
recipient: DataStructures.L1Actor(address(this), block.chainid),
content: Hash.sha256ToField(
abi.encodeWithSignature(
"withdraw(uint256,address,address)",
_amount,
"withdraw(address,uint256,address)",
_recipient,
_amount,
_withCaller ? msg.sender : address(0)
)
)
Expand Down
34 changes: 17 additions & 17 deletions l1-contracts/test/portals/TokenPortal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ contract TokenPortalTest is Test {
recipient: DataStructures.L2Actor(l2TokenAddress, 1),
content: Hash.sha256ToField(
abi.encodeWithSignature(
"mint_private(uint256,bytes32,address)",
amount,
"mint_private(bytes32,uint256,address)",
secretHashForRedeemingMintedNotes,
amount,
_canceller
)
),
Expand All @@ -107,7 +107,7 @@ contract TokenPortalTest is Test {
sender: DataStructures.L1Actor(address(tokenPortal), block.chainid),
recipient: DataStructures.L2Actor(l2TokenAddress, 1),
content: Hash.sha256ToField(
abi.encodeWithSignature("mint_public(uint256,bytes32,address)", amount, to, _canceller)
abi.encodeWithSignature("mint_public(bytes32,uint256,address)", to, amount, _canceller)
),
secretHash: secretHashForL2MessageConsumption,
deadline: deadline,
Expand Down Expand Up @@ -142,8 +142,8 @@ contract TokenPortalTest is Test {

// Perform op
bytes32 entryKey = tokenPortal.depositToAztecPrivate{value: bid}(
amount,
secretHashForRedeemingMintedNotes,
amount,
address(this),
deadline,
secretHashForL2MessageConsumption
Expand Down Expand Up @@ -185,7 +185,7 @@ contract TokenPortalTest is Test {

// Perform op
bytes32 entryKey = tokenPortal.depositToAztecPublic{value: bid}(
amount, to, address(this), deadline, secretHashForL2MessageConsumption
to, amount, address(this), deadline, secretHashForL2MessageConsumption
);

assertEq(entryKey, expectedEntryKey, "returned entry key and calculated entryKey should match");
Expand All @@ -210,7 +210,7 @@ contract TokenPortalTest is Test {
abi.encodeWithSelector(Errors.Inbox__NothingToConsume.selector, expectedWrongEntryKey)
);
tokenPortal.cancelL1ToAztecMessagePublic(
amount, to, deadline, secretHashForL2MessageConsumption, bid
to, amount, deadline, secretHashForL2MessageConsumption, bid
);
vm.stopPrank();

Expand All @@ -221,7 +221,7 @@ contract TokenPortalTest is Test {
abi.encodeWithSelector(Errors.Inbox__NothingToConsume.selector, expectedWrongEntryKey)
);
tokenPortal.cancelL1ToAztecMessagePrivate(
amount, secretHashForRedeemingMintedNotes, deadline, secretHashForL2MessageConsumption, bid
secretHashForRedeemingMintedNotes, amount, deadline, secretHashForL2MessageConsumption, bid
);

// actually cancel the message
Expand All @@ -231,7 +231,7 @@ contract TokenPortalTest is Test {
emit L1ToL2MessageCancelled(expectedEntryKey);
// perform op
bytes32 entryKey = tokenPortal.cancelL1ToAztecMessagePublic(
amount, to, deadline, secretHashForL2MessageConsumption, bid
to, amount, deadline, secretHashForL2MessageConsumption, bid
);

assertEq(entryKey, expectedEntryKey, "returned entry key and calculated entryKey should match");
Expand All @@ -257,7 +257,7 @@ contract TokenPortalTest is Test {
abi.encodeWithSelector(Errors.Inbox__NothingToConsume.selector, expectedWrongEntryKey)
);
tokenPortal.cancelL1ToAztecMessagePrivate(
amount, secretHashForRedeemingMintedNotes, deadline, secretHashForL2MessageConsumption, bid
secretHashForRedeemingMintedNotes, amount, deadline, secretHashForL2MessageConsumption, bid
);
vm.stopPrank();

Expand All @@ -268,7 +268,7 @@ contract TokenPortalTest is Test {
abi.encodeWithSelector(Errors.Inbox__NothingToConsume.selector, expectedWrongEntryKey)
);
tokenPortal.cancelL1ToAztecMessagePublic(
amount, to, deadline, secretHashForL2MessageConsumption, bid
to, amount, deadline, secretHashForL2MessageConsumption, bid
);

// actually cancel the message
Expand All @@ -278,7 +278,7 @@ contract TokenPortalTest is Test {
emit L1ToL2MessageCancelled(expectedEntryKey);
// perform op
bytes32 entryKey = tokenPortal.cancelL1ToAztecMessagePrivate(
amount, secretHashForRedeemingMintedNotes, deadline, secretHashForL2MessageConsumption, bid
secretHashForRedeemingMintedNotes, amount, deadline, secretHashForL2MessageConsumption, bid
);

assertEq(entryKey, expectedEntryKey, "returned entry key and calculated entryKey should match");
Expand All @@ -302,7 +302,7 @@ contract TokenPortalTest is Test {
recipient: DataStructures.L1Actor({actor: address(tokenPortal), chainId: block.chainid}),
content: Hash.sha256ToField(
abi.encodeWithSignature(
"withdraw(uint256,address,address)", withdrawAmount, recipient, _designatedCaller
"withdraw(address,uint256,address)", recipient, withdrawAmount, _designatedCaller
)
)
})
Expand Down Expand Up @@ -331,7 +331,7 @@ contract TokenPortalTest is Test {
vm.startPrank(_caller);
vm.expectEmit(true, true, true, true);
emit MessageConsumed(expectedEntryKey, address(tokenPortal));
bytes32 actualEntryKey = tokenPortal.withdraw(withdrawAmount, recipient, false);
bytes32 actualEntryKey = tokenPortal.withdraw(recipient, withdrawAmount, false);
assertEq(expectedEntryKey, actualEntryKey);
// Should have received 654 RNA tokens
assertEq(portalERC20.balanceOf(recipient), withdrawAmount);
Expand All @@ -340,7 +340,7 @@ contract TokenPortalTest is Test {
vm.expectRevert(
abi.encodeWithSelector(Errors.Outbox__NothingToConsume.selector, actualEntryKey)
);
tokenPortal.withdraw(withdrawAmount, recipient, false);
tokenPortal.withdraw(recipient, withdrawAmount, false);
vm.stopPrank();
}

Expand All @@ -354,13 +354,13 @@ contract TokenPortalTest is Test {
vm.expectRevert(
abi.encodeWithSelector(Errors.Outbox__NothingToConsume.selector, entryKeyPortalChecksAgainst)
);
tokenPortal.withdraw(withdrawAmount, recipient, true);
tokenPortal.withdraw(recipient, withdrawAmount, true);

entryKeyPortalChecksAgainst = _createWithdrawMessageForOutbox(address(0));
vm.expectRevert(
abi.encodeWithSelector(Errors.Outbox__NothingToConsume.selector, entryKeyPortalChecksAgainst)
);
tokenPortal.withdraw(withdrawAmount, recipient, false);
tokenPortal.withdraw(recipient, withdrawAmount, false);
vm.stopPrank();
}

Expand All @@ -370,7 +370,7 @@ contract TokenPortalTest is Test {

vm.expectEmit(true, true, true, true);
emit MessageConsumed(expectedEntryKey, address(tokenPortal));
bytes32 actualEntryKey = tokenPortal.withdraw(withdrawAmount, recipient, true);
bytes32 actualEntryKey = tokenPortal.withdraw(recipient, withdrawAmount, true);
assertEq(expectedEntryKey, actualEntryKey);
// Should have received 654 RNA tokens
assertEq(portalERC20.balanceOf(recipient), withdrawAmount);
Expand Down
8 changes: 4 additions & 4 deletions l1-contracts/test/portals/UniswapPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ contract UniswapPortal {
vars.outputAsset = TokenPortal(_outputTokenPortal).underlying();

// Withdraw the input asset from the portal
TokenPortal(_inputTokenPortal).withdraw(_inAmount, address(this), true);
TokenPortal(_inputTokenPortal).withdraw(address(this), _inAmount, true);
{
// prevent stack too deep errors
vars.contentHash = Hash.sha256ToField(
Expand Down Expand Up @@ -123,7 +123,7 @@ contract UniswapPortal {

// Deposit the output asset to the L2 via its portal
return TokenPortal(_outputTokenPortal).depositToAztecPublic{value: msg.value}(
amountOut, _aztecRecipient, _canceller, _deadlineForL1ToL2Message, _secretHashForL1ToL2Message
_aztecRecipient, amountOut, _canceller, _deadlineForL1ToL2Message, _secretHashForL1ToL2Message
);
}
// docs:end:solidity_uniswap_swap
Expand Down Expand Up @@ -163,7 +163,7 @@ contract UniswapPortal {
vars.outputAsset = TokenPortal(_outputTokenPortal).underlying();

// Withdraw the input asset from the portal
TokenPortal(_inputTokenPortal).withdraw(_inAmount, address(this), true);
TokenPortal(_inputTokenPortal).withdraw(address(this), _inAmount, true);
{
// prevent stack too deep errors
vars.contentHash = Hash.sha256ToField(
Expand Down Expand Up @@ -216,8 +216,8 @@ contract UniswapPortal {

// Deposit the output asset to the L2 via its portal
return TokenPortal(_outputTokenPortal).depositToAztecPrivate{value: msg.value}(
amountOut,
_secretHashForRedeemingMintedNotes,
amountOut,
_canceller,
_deadlineForL1ToL2Message,
_secretHashForL1ToL2Message
Expand Down
4 changes: 2 additions & 2 deletions l1-contracts/test/portals/UniswapPortal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ contract UniswapPortalTest is Test {
sender: DataStructures.L2Actor(l2TokenAddress, 1),
recipient: DataStructures.L1Actor(address(daiTokenPortal), block.chainid),
content: Hash.sha256ToField(
abi.encodeWithSignature("withdraw(uint256,address,address)", amount, _recipient, _caller)
abi.encodeWithSignature("withdraw(address,uint256,address)", _recipient, amount, _caller)
)
});
entryKey = outbox.computeEntryKey(message);
Expand Down Expand Up @@ -379,7 +379,7 @@ contract UniswapPortalTest is Test {
// perform op
// TODO(2167) - Update UniswapPortal properly with new portal standard.
bytes32 entryKey = wethTokenPortal.cancelL1ToAztecMessagePublic(
wethAmountOut, aztecRecipient, deadlineForL1ToL2Message, secretHash, 1 ether
aztecRecipient, wethAmountOut, deadlineForL1ToL2Message, secretHash, 1 ether
);
assertEq(entryKey, l1ToL2MessageKey, "returned entry key and calculated entryKey should match");
assertFalse(inbox.contains(entryKey), "entry still in inbox");
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/acir-simulator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"toml": "^3.0.0",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
"typescript": "^5.0.4",
"viem": "^1.2.5"
},
"files": [
"dest",
Expand Down
Loading

0 comments on commit 447dade

Please sign in to comment.