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

fix: validate fee recipient #87

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 23 additions & 9 deletions near/nep141-locker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl Contract {
&mut self,
#[serializer(borsh)] storage_deposit_args: StorageDepositArgs,
#[serializer(borsh)] predecessor_account_id: AccountId,
#[serializer(borsh)] native_fee_recipient: OmniAddress,
#[serializer(borsh)] native_fee_recipient: Option<OmniAddress>,
) -> PromiseOrValue<U128> {
let Ok(ProverResult::InitTransfer(init_transfer)) = Self::decode_prover_result(0) else {
env::panic_str("Invalid proof message")
Expand All @@ -485,13 +485,22 @@ impl Contract {
let mut required_balance;

if let OmniAddress::Near(recipient) = &transfer_message.recipient {
required_balance = self.add_fin_transfer(
&transfer_message.get_transfer_id(),
&Some(NativeFee {
let native_fee = if transfer_message.fee.native_fee.0 != 0 {
let recipient = native_fee_recipient.sdk_expect("ERR_FEE_RECIPIENT_NOT_SET");
require!(
transfer_message.get_origin_chain() == recipient.get_chain(),
"ERR_WRONG_FEE_RECIPIENT_CHAIN"
);
Some(NativeFee {
amount: transfer_message.fee.native_fee,
recipient: native_fee_recipient,
}),
);
recipient,
})
} else {
None
};

required_balance =
self.add_fin_transfer(&transfer_message.get_transfer_id(), &native_fee);

let recipient: NearRecipient =
recipient.parse().sdk_expect("Failed to parse recipient");
Expand Down Expand Up @@ -616,7 +625,7 @@ impl Contract {
#[payable]
pub fn claim_fee_callback(
&mut self,
#[serializer(borsh)] native_fee_recipient: OmniAddress,
#[serializer(borsh)] native_fee_recipient: Option<OmniAddress>,
#[serializer(borsh)] predecessor_account_id: AccountId,
#[callback_result]
#[serializer(borsh)]
Expand All @@ -640,6 +649,12 @@ impl Contract {
let fee = message.amount.0 - fin_transfer.amount.0;

if message.fee.native_fee.0 != 0 {
let native_fee_recipient = native_fee_recipient.sdk_expect("ERR_FEE_RECIPIENT_NOT_SET");
require!(
message.get_origin_chain() == native_fee_recipient.get_chain(),
"ERR_WRONG_FEE_RECIPIENT_CHAIN"
);

if message.get_origin_chain() == ChainKind::Near {
let OmniAddress::Near(recipient) = &native_fee_recipient else {
env::panic_str("ERR_WRONG_CHAIN_KIND")
Expand Down Expand Up @@ -667,7 +682,6 @@ impl Contract {
env::log_str(
&Nep141LockerEvent::ClaimFeeEvent {
transfer_message: message,
native_fee_recipient,
}
.to_log_string(),
);
Expand Down
4 changes: 2 additions & 2 deletions near/omni-types/src/locker_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct StorageDepositArgs {
#[derive(BorshDeserialize, BorshSerialize, Clone)]
pub struct FinTransferArgs {
pub chain_kind: ChainKind,
pub native_fee_recipient: OmniAddress,
pub native_fee_recipient: Option<OmniAddress>,
pub storage_deposit_args: StorageDepositArgs,
pub prover_args: Vec<u8>,
}
Expand All @@ -22,7 +22,7 @@ pub struct FinTransferArgs {
pub struct ClaimFeeArgs {
pub chain_kind: ChainKind,
pub prover_args: Vec<u8>,
pub native_fee_recipient: OmniAddress,
pub native_fee_recipient: Option<OmniAddress>,
}

#[derive(BorshDeserialize, BorshSerialize, Clone)]
Expand Down
1 change: 0 additions & 1 deletion near/omni-types/src/near_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub enum Nep141LockerEvent {
},
ClaimFeeEvent {
transfer_message: TransferMessage,
native_fee_recipient: OmniAddress,
},
}

Expand Down