Skip to content
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

NEAR: verify token and recipient addresses #70

Merged
merged 4 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 42 additions & 31 deletions near/contracts/bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ impl FastBridge {
"The fee token does not match the transfer token"
);

require!(
transfer_message.transfer.token_eth != transfer_message.recipient,
"The eth token address and recipient address can't be the same"
);

sept-en marked this conversation as resolved.
Show resolved Hide resolved
if let Some(update_balance) = update_balance.as_ref() {
self.increase_balance(
&update_balance.sender_id,
Expand Down Expand Up @@ -872,7 +877,15 @@ mod unit_tests {
}

fn eth_bridge_address() -> String {
"6b175474e89094c44da98b954eedeac495271d0f".to_string()
"6b175474e89094c44da98b954eedeac495271d0f".to_owned()
}

fn eth_token_address() -> String {
"71c7656ec7ab88b098defb751b7401b5f6d8976f".to_owned()
}

fn eth_recipient_address() -> String {
"8ba1f109551bd432803012645ac136ddd64dba72".to_owned()
}

fn prover() -> AccountId {
Expand Down Expand Up @@ -961,14 +974,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": "token_near",
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "75"
},
"fee": {
"token": "token_near",
"amount": "75"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});

contract.ft_on_transfer(
Expand Down Expand Up @@ -1051,14 +1064,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": token,
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "100"
},
"fee": {
"token": token,
"amount": "100"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});

let transfer_message = serde_json::from_value(msg).unwrap();
Expand All @@ -1068,16 +1081,14 @@ mod unit_tests {
valid_till: current_timestamp,
transfer: TransferDataEthereum {
token_near: AccountId::try_from("alice_near".to_string()).unwrap(),
token_eth: get_eth_address("71C7656EC7ab88b098defB751B7401B5f6d8976F".to_string()),
token_eth: get_eth_address(eth_token_address()),
amount: U128(100),
},
fee: TransferDataNear {
token: AccountId::try_from("alice_near".to_string()).unwrap(),
amount: U128(100),
},
recipient: fast_bridge_common::get_eth_address(
"71C7656EC7ab88b098defB751B7401B5f6d8976F".to_string(),
),
recipient: fast_bridge_common::get_eth_address(eth_recipient_address()),
valid_till_block_height: None,
};
assert_eq!(
Expand All @@ -1098,14 +1109,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": token,
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "100"
},
"fee": {
"token": token,
"amount": "100"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});

contract.validate_transfer_message(
Expand All @@ -1126,14 +1137,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": token,
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "100"
},
"fee": {
"token": token,
"amount": "100"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});

contract.validate_transfer_message(
Expand Down Expand Up @@ -1199,14 +1210,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": "token_near",
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "100"
},
"fee": {
"token": "token_near",
"amount": "100"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});

contract.init_transfer_callback(
Expand Down Expand Up @@ -1246,14 +1257,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": "token_near",
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "100"
},
"fee": {
"token": "token_near",
"amount": "100"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});

contract.init_transfer_callback(
Expand Down Expand Up @@ -1285,14 +1296,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": "token_near",
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "75"
},
"fee": {
"token": "token_near",
"amount": "75"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});
contract.init_transfer_callback(
10,
Expand Down Expand Up @@ -1335,14 +1346,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": "token_near",
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "200"
},
"fee": {
"token": "token_near",
"amount": "200"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});
contract.init_transfer_callback(
10,
Expand Down Expand Up @@ -1378,14 +1389,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": "token_near",
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "75"
},
"fee": {
"token": "token_near",
"amount": "75"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});
contract.init_transfer_callback(
10,
Expand All @@ -1411,14 +1422,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": "token_near299",
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "75"
},
"fee": {
"token": "token_near",
"amount": "75"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});
contract.init_transfer_callback(
10,
Expand Down Expand Up @@ -1449,14 +1460,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": "token_near",
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "75"
},
"fee": {
"token": "token_near299",
"amount": "75"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});
contract.init_transfer_callback(
10,
Expand Down Expand Up @@ -1489,14 +1500,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": "token_near",
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "75"
},
"fee": {
"token": "token_near",
"amount": "75"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});
contract.init_transfer_callback(
10,
Expand Down Expand Up @@ -1568,14 +1579,14 @@ mod unit_tests {
"valid_till": current_timestamp,
"transfer": {
"token_near": "token_near",
"token_eth": "71c7656ec7ab88b098defb751b7401b5f6d8976f",
"token_eth": eth_token_address(),
"amount": "75"
},
"fee": {
"token": "token_near",
"amount": "75"
},
"recipient": "71c7656ec7ab88b098defb751b7401b5f6d8976f"
"recipient": eth_recipient_address()
});
contract.init_transfer_callback(
10,
Expand Down
2 changes: 1 addition & 1 deletion near/contracts/bridge/src/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ mod integration_tests {
token: token.id().to_string().parse().unwrap(),
amount: fee_amount.into(),
},
recipient: [0; 20],
recipient: [1; 20],
valid_till_block_height: None,
};
let msg = near_sdk::base64::encode(msg.try_to_vec().unwrap());
Expand Down
Binary file modified near/res/fastbridge.wasm
Binary file not shown.
10 changes: 10 additions & 0 deletions near/res/fastbridge_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14013,6 +14013,16 @@ impl FastBridge {
&"The fee token does not match the transfer token",
)
}
if true {
let msg: &str = &"The eth token address and recipient address can't be the same";
if !(transfer_message.transfer.token_eth != transfer_message.recipient) {
::core::panicking::panic_display(&msg)
}
} else if !(transfer_message.transfer.token_eth != transfer_message.recipient) {
::near_sdk::env::panic_str(
&"The eth token address and recipient address can't be the same",
)
}
if let Some(update_balance) = update_balance.as_ref() {
self.increase_balance(
&update_balance.sender_id,
Expand Down