More checks on amount and fees

Change-Id: If5a7f43faa0ea39e99138c0856756bad19a4b410
This commit is contained in:
Hendrik Hofstadt 2021-09-07 12:18:06 +02:00
parent 0702ee300f
commit 94695ee125
4 changed files with 24 additions and 6 deletions

View File

@ -36,7 +36,8 @@ pub struct VerifySignatures<'b> {
pub instruction_acc: Info<'b>, pub instruction_acc: Info<'b>,
} }
impl<'b> InstructionContext<'b> for VerifySignatures<'b> {} impl<'b> InstructionContext<'b> for VerifySignatures<'b> {
}
impl From<&VerifySignatures<'_>> for GuardianSetDerivationData { impl From<&VerifySignatures<'_>> for GuardianSetDerivationData {
fn from(data: &VerifySignatures<'_>) -> Self { fn from(data: &VerifySignatures<'_>) -> Self {
@ -66,7 +67,6 @@ struct SecpInstructionPart<'a> {
msg_size: u16, msg_size: u16,
} }
pub fn verify_signatures( pub fn verify_signatures(
ctx: &ExecutionContext, ctx: &ExecutionContext,
accs: &mut VerifySignatures, accs: &mut VerifySignatures,

View File

@ -142,7 +142,7 @@ pub fn complete_native(
accs.to.info().key, accs.to.info().key,
accs.custody_signer.key, accs.custody_signer.key,
&[], &[],
amount - fee, amount.checked_sub(fee).unwrap(),
)?; )?;
invoke_seeded(&transfer_ix, ctx, &accs.custody_signer, None)?; invoke_seeded(&transfer_ix, ctx, &accs.custody_signer, None)?;
@ -248,7 +248,11 @@ pub fn complete_wrapped(
accs.to.info().key, accs.to.info().key,
accs.mint_authority.key, accs.mint_authority.key,
&[], &[],
accs.vaa.amount.as_u64() - accs.vaa.fee.as_u64(), accs.vaa
.amount
.as_u64()
.checked_sub(accs.vaa.fee.as_u64())
.unwrap(),
)?; )?;
invoke_seeded(&mint_ix, ctx, &accs.mint_authority, None)?; invoke_seeded(&mint_ix, ctx, &accs.mint_authority, None)?;

View File

@ -16,7 +16,10 @@ use crate::{
messages::PayloadTransfer, messages::PayloadTransfer,
types::*, types::*,
TokenBridgeError, TokenBridgeError,
TokenBridgeError::WrongAccountOwner, TokenBridgeError::{
InvalidFee,
WrongAccountOwner,
},
}; };
use bridge::{ use bridge::{
accounts::Bridge, accounts::Bridge,
@ -134,6 +137,11 @@ pub fn transfer_native(
return Err(TokenBridgeError::InvalidMint.into()); return Err(TokenBridgeError::InvalidMint.into());
} }
// Fee must be less than amount
if data.fee > data.amount {
return Err(InvalidFee.into());
}
// Verify that the token is not a wrapped token // Verify that the token is not a wrapped token
if let COption::Some(mint_authority) = accs.mint.mint_authority { if let COption::Some(mint_authority) = accs.mint.mint_authority {
if mint_authority == MintSigner::key(None, ctx.program_id) { if mint_authority == MintSigner::key(None, ctx.program_id) {
@ -292,6 +300,11 @@ pub fn transfer_wrapped(
return Err(TokenBridgeError::InvalidMint.into()); return Err(TokenBridgeError::InvalidMint.into());
} }
// Fee must be less than amount
if data.fee > data.amount {
return Err(InvalidFee.into());
}
// Verify that meta is correct // Verify that meta is correct
let derivation_data: WrappedMetaDerivationData = (&*accs).into(); let derivation_data: WrappedMetaDerivationData = (&*accs).into();
accs.wrapped_meta accs.wrapped_meta

View File

@ -63,6 +63,7 @@ pub enum TokenBridgeError {
TokenNotNative, TokenNotNative,
UninitializedMint, UninitializedMint,
WrongAccountOwner, WrongAccountOwner,
InvalidFee,
} }
impl From<TokenBridgeError> for SolitaireError { impl From<TokenBridgeError> for SolitaireError {