Skip to content

Commit

Permalink
feat: add empty asset check for p2id notes
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyrd committed Feb 3, 2025
1 parent 1dd7908 commit 35a4d6c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 40 deletions.
75 changes: 37 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 37 additions & 2 deletions crates/rust-client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use miden_objects::{
Account, AccountBuilder, AccountCode, AccountHeader, AccountId, AccountStorageMode,
AccountType, AuthSecretKey,
},
asset::{FungibleAsset, TokenSymbol},
asset::{Asset, FungibleAsset, TokenSymbol},
crypto::{dsa::rpo_falcon512::SecretKey, rand::FeltRng},
note::{
Note, NoteAssets, NoteExecutionHint, NoteExecutionMode, NoteFile, NoteMetadata, NoteTag,
Expand All @@ -34,7 +34,8 @@ use crate::{
rpc::NodeRpcClient,
store::{InputNoteRecord, NoteFilter, Store, StoreError},
transaction::{
TransactionRequestBuilder, TransactionRequestError, TransactionScriptBuilderError,
PaymentTransactionData, TransactionRequestBuilder, TransactionRequestError,
TransactionScriptBuilderError,
},
Client, ClientError,
};
Expand Down Expand Up @@ -692,4 +693,38 @@ async fn test_note_without_asset() {
)
)
));

let error = TransactionRequestBuilder::pay_to_id(
PaymentTransactionData::new(vec![], faucet.id(), wallet.id()),
None,
NoteType::Public,
client.rng(),
)
.unwrap_err();

assert!(matches!(
error,
TransactionRequestError::TransactionScriptBuilderError(
TransactionScriptBuilderError::P2IDNoteWithoutAsset
)
));

let error = TransactionRequestBuilder::pay_to_id(
PaymentTransactionData::new(
vec![Asset::Fungible(FungibleAsset::new(faucet.id(), 0).unwrap())],
faucet.id(),
wallet.id(),
),
None,
NoteType::Public,
client.rng(),
)
.unwrap_err();

assert!(matches!(
error,
TransactionRequestError::TransactionScriptBuilderError(
TransactionScriptBuilderError::P2IDNoteWithoutAsset
)
));
}
14 changes: 14 additions & 0 deletions crates/rust-client/src/transaction/request/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use super::{
ForeignAccount, NoteArgs, TransactionRequest, TransactionRequestError,
TransactionScriptTemplate,
};
use crate::transaction::TransactionScriptBuilderError;

// TRANSACTION REQUEST BUILDER
// ================================================================================================
Expand Down Expand Up @@ -290,6 +291,19 @@ impl TransactionRequestBuilder {
sender_account_id,
target_account_id,
} = payment_data;

if assets.iter().all(|asset| {
if let Asset::Fungible(asset) = asset {
asset.amount() == 0
} else {
false
}
}) {
return Err(TransactionRequestError::TransactionScriptBuilderError(
TransactionScriptBuilderError::P2IDNoteWithoutAsset,
));
}

let created_note = if let Some(recall_height) = recall_height {
create_p2idr_note(
sender_account_id,
Expand Down
2 changes: 2 additions & 0 deletions crates/rust-client/src/transaction/script_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ impl TransactionScriptBuilder {
pub enum TransactionScriptBuilderError {
#[error("invalid asset: {0}")]
InvalidAsset(AccountIdPrefix),
#[error("pay to id note doesn't contain at least one asset")]
P2IDNoteWithoutAsset,
#[error("note created by the faucet doesn't contain exactly one asset")]
FaucetNoteWithoutAsset,
#[error("invalid transaction script")]
Expand Down

0 comments on commit 35a4d6c

Please sign in to comment.