diff --git a/integration_tests/tests/wallet_ffi.rs b/integration_tests/tests/wallet_ffi.rs index 5fd40817..ec0329a4 100644 --- a/integration_tests/tests/wallet_ffi.rs +++ b/integration_tests/tests/wallet_ffi.rs @@ -31,7 +31,7 @@ use tempfile::tempdir; use wallet::{account::HumanReadableAccount, program_facades::vault::Vault}; use wallet_ffi::{ FfiAccount, FfiAccountIdentity, FfiAccountList, FfiBytes32, FfiPrivateAccountKeys, - FfiPublicAccountKey, FfiTransferResult, FfiU128, WalletHandle, error, + FfiProgramId, FfiPublicAccountKey, FfiTransferResult, FfiU128, WalletHandle, error, generic_transaction::{FfiProgramWithDependencies, FfiTransactionResult}, wallet::FfiCreateWalletOutput, }; @@ -234,7 +234,7 @@ unsafe extern "C" { account_identities_size: usize, instruction_words: *const u32, instruction_words_size: usize, - program_with_dependencies: *const FfiProgramWithDependencies, + program_id: FfiProgramId, out_result: *mut FfiTransactionResult, ) -> error::WalletFfiError; @@ -1595,7 +1595,7 @@ fn test_wallet_ffi_transfer_generic_public() -> Result<()> { account_identities_size, instruction_words, instruction_words_size, - &raw const program_with_dependencies, + program_id.into(), &raw mut transaction_result, ) .unwrap(); diff --git a/lez/wallet-ffi/src/generic_transaction.rs b/lez/wallet-ffi/src/generic_transaction.rs index 7a7f86ca..7be6ddaf 100644 --- a/lez/wallet-ffi/src/generic_transaction.rs +++ b/lez/wallet-ffi/src/generic_transaction.rs @@ -10,7 +10,7 @@ use crate::{ error::{print_error, WalletFfiError}, map_execution_error, wallet::get_wallet, - FfiAccountIdentity, FfiBytes32, WalletHandle, + FfiAccountIdentity, FfiBytes32, FfiProgramId, WalletHandle, }; #[repr(C)] @@ -214,7 +214,7 @@ pub unsafe extern "C" fn wallet_ffi_send_generic_public_transaction( account_identities_size: usize, instruction_words: *const u32, instruction_words_size: usize, - program_with_dependencies: *const FfiProgramWithDependencies, + program_id: FfiProgramId, out_result: *mut FfiTransactionResult, ) -> WalletFfiError { let wrapper = match get_wallet(handle) { @@ -260,12 +260,7 @@ pub unsafe extern "C" fn wallet_ffi_send_generic_public_transaction( } } - let program = match unsafe { &*program_with_dependencies }.try_into() { - Ok(v) => v, - Err(err) => return err, - }; - - match block_on(wallet.send_pub_tx(accounts, instruction_data.to_vec(), &program)) { + match block_on(wallet.send_pub_tx(accounts, instruction_data.to_vec(), program_id.into())) { Ok(tx_hash) => { let tx_hash = CString::new(tx_hash.to_string()) .map_or(std::ptr::null_mut(), std::ffi::CString::into_raw); diff --git a/lez/wallet-ffi/src/types.rs b/lez/wallet-ffi/src/types.rs index ad366b91..cbe9fab7 100644 --- a/lez/wallet-ffi/src/types.rs +++ b/lez/wallet-ffi/src/types.rs @@ -7,7 +7,7 @@ use std::{ str::FromStr as _, }; -use lee::{Data, SharedSecretKey}; +use lee::{Data, ProgramId, SharedSecretKey}; use lee_core::{encryption::MlKem768EncapsulationKey, NullifierPublicKey}; use wallet::AccountIdentity; @@ -581,6 +581,18 @@ impl TryFrom<&FfiAccountIdentity> for AccountIdentity { } } +impl From for FfiProgramId { + fn from(value: ProgramId) -> Self { + Self { data: value } + } +} + +impl From for ProgramId { + fn from(value: FfiProgramId) -> Self { + value.data + } +} + #[cfg(test)] mod tests { use lee::{AccountId, PrivateKey, PublicKey}; diff --git a/lez/wallet-ffi/wallet_ffi.h b/lez/wallet-ffi/wallet_ffi.h index a6ce6b72..d83c520e 100644 --- a/lez/wallet-ffi/wallet_ffi.h +++ b/lez/wallet-ffi/wallet_ffi.h @@ -256,23 +256,6 @@ typedef struct FfiAccountIdentity { struct FfiU128 identifier; } FfiAccountIdentity; -/** - * Intended to be created manually. - */ -typedef struct FfiProgram { - const uint8_t *elf_data; - uintptr_t elf_size; -} FfiProgram; - -/** - * Intended to be created manually. - */ -typedef struct FfiProgramWithDependencies { - struct FfiProgram program; - const struct FfiProgram *deps; - uintptr_t deps_size; -} FfiProgramWithDependencies; - /** * Result of a generic transaction operation. */ @@ -292,6 +275,23 @@ typedef struct FfiTransactionResult { uintptr_t secrets_size; } FfiTransactionResult; +/** + * Intended to be created manually. + */ +typedef struct FfiProgram { + const uint8_t *elf_data; + uintptr_t elf_size; +} FfiProgram; + +/** + * Intended to be created manually. + */ +typedef struct FfiProgramWithDependencies { + struct FfiProgram program; + const struct FfiProgram *deps; + uintptr_t deps_size; +} FfiProgramWithDependencies; + /** * Public key info for a public account. */ @@ -613,7 +613,7 @@ enum WalletFfiError wallet_ffi_send_generic_public_transaction(struct WalletHand uintptr_t account_identities_size, const uint32_t *instruction_words, uintptr_t instruction_words_size, - const struct FfiProgramWithDependencies *program_with_dependencies, + struct FfiProgramId program_id, struct FfiTransactionResult *out_result); /** diff --git a/lez/wallet/src/lib.rs b/lez/wallet/src/lib.rs index 6c285a26..80b42e17 100644 --- a/lez/wallet/src/lib.rs +++ b/lez/wallet/src/lib.rs @@ -16,7 +16,7 @@ use common::{HashType, transaction::LeeTransaction}; use config::WalletConfig; use key_protocol::key_management::key_tree::chain_index::ChainIndex; use lee::{ - Account, AccountId, PrivacyPreservingTransaction, + Account, AccountId, PrivacyPreservingTransaction, ProgramId, privacy_preserving_transaction::{ circuit::ProgramWithDependencies, message::EncryptedAccountData, }, @@ -619,9 +619,9 @@ impl WalletCore { &self, accounts: Vec, instruction_data: InstructionData, - program: &ProgramWithDependencies, + program_id: ProgramId, ) -> Result { - self.send_pub_tx_with_pre_check(accounts, instruction_data, program, |_| Ok(())) + self.send_pub_tx_with_pre_check(accounts, instruction_data, program_id, |_| Ok(())) .await } @@ -629,7 +629,7 @@ impl WalletCore { &self, accounts: Vec, instruction_data: InstructionData, - program: &ProgramWithDependencies, + program_id: ProgramId, tx_pre_check: impl FnOnce(&[&Account]) -> Result<(), ExecutionFailureKind>, ) -> Result { // Public transaction, all accounts must be public @@ -652,7 +652,6 @@ impl WalletCore { )?; let account_ids = acc_manager.public_account_ids(); - let program_id = program.program.id(); let nonces = acc_manager.public_account_nonces(); let message = lee::public_transaction::Message::new_preserialized( diff --git a/lez/wallet/src/program_facades/amm.rs b/lez/wallet/src/program_facades/amm.rs index 0349e57c..bc5c5d62 100644 --- a/lez/wallet/src/program_facades/amm.rs +++ b/lez/wallet/src/program_facades/amm.rs @@ -22,7 +22,6 @@ impl Amm<'_> { .public_account_id() .ok_or(ExecutionFailureKind::KeyNotFoundError)?; - let program = programs::amm(); let amm_program_id = programs::amm().id(); let user_a_acc = self .0 @@ -67,7 +66,7 @@ impl Amm<'_> { user_holding_lp, ], instruction_data, - &program.into(), + amm_program_id, ) .await } @@ -87,7 +86,6 @@ impl Amm<'_> { .public_account_id() .ok_or(ExecutionFailureKind::KeyNotFoundError)?; - let program = programs::amm(); let amm_program_id = programs::amm().id(); let user_a_acc = self .0 @@ -149,7 +147,7 @@ impl Amm<'_> { user_b_signing_identity, ], instruction_data, - &program.into(), + amm_program_id, ) .await } @@ -169,7 +167,6 @@ impl Amm<'_> { .public_account_id() .ok_or(ExecutionFailureKind::KeyNotFoundError)?; - let program = programs::amm(); let amm_program_id = programs::amm().id(); let user_a_acc = self .0 @@ -231,7 +228,7 @@ impl Amm<'_> { user_b_signing_identity, ], instruction_data, - &program.into(), + amm_program_id, ) .await } @@ -252,7 +249,6 @@ impl Amm<'_> { .public_account_id() .ok_or(ExecutionFailureKind::KeyNotFoundError)?; - let program = programs::amm(); let amm_program_id = programs::amm().id(); let user_a_acc = self .0 @@ -297,7 +293,7 @@ impl Amm<'_> { user_holding_lp, ], instruction_data, - &program.into(), + amm_program_id, ) .await } @@ -311,7 +307,6 @@ impl Amm<'_> { min_amount_to_remove_token_a: u128, min_amount_to_remove_token_b: u128, ) -> Result { - let program = programs::amm(); let amm_program_id = programs::amm().id(); let user_a_acc = self .0 @@ -356,7 +351,7 @@ impl Amm<'_> { user_holding_lp, ], instruction_data, - &program.into(), + amm_program_id, ) .await } diff --git a/lez/wallet/src/program_facades/ata.rs b/lez/wallet/src/program_facades/ata.rs index 842ba529..6c9ec568 100644 --- a/lez/wallet/src/program_facades/ata.rs +++ b/lez/wallet/src/program_facades/ata.rs @@ -21,8 +21,7 @@ impl Ata<'_> { .public_account_id() .ok_or(ExecutionFailureKind::KeyNotFoundError)?; - let program = programs::ata(); - let ata_program_id = program.id(); + let ata_program_id = programs::ata().id(); let ata_id = get_associated_token_account_id( &ata_program_id, &compute_ata_seed(owner_id, definition_id), @@ -39,7 +38,7 @@ impl Ata<'_> { AccountIdentity::PublicNoSign(ata_id), ], instruction_data, - &program.into(), + ata_program_id, ) .await } @@ -55,8 +54,7 @@ impl Ata<'_> { .public_account_id() .ok_or(ExecutionFailureKind::KeyNotFoundError)?; - let program = programs::ata(); - let ata_program_id = program.id(); + let ata_program_id = programs::ata().id(); let sender_ata_id = get_associated_token_account_id( &ata_program_id, &compute_ata_seed(owner_id, definition_id), @@ -76,7 +74,7 @@ impl Ata<'_> { AccountIdentity::PublicNoSign(recipient_id), ], instruction_data, - &program.into(), + ata_program_id, ) .await } @@ -91,8 +89,7 @@ impl Ata<'_> { .public_account_id() .ok_or(ExecutionFailureKind::KeyNotFoundError)?; - let program = programs::ata(); - let ata_program_id = program.id(); + let ata_program_id = programs::ata().id(); let holder_ata_id = get_associated_token_account_id( &ata_program_id, &compute_ata_seed(owner_id, definition_id), @@ -112,7 +109,7 @@ impl Ata<'_> { AccountIdentity::PublicNoSign(definition_id), ], instruction_data, - &program.into(), + ata_program_id, ) .await } diff --git a/lez/wallet/src/program_facades/bridge.rs b/lez/wallet/src/program_facades/bridge.rs index 728868ed..c9aa585e 100644 --- a/lez/wallet/src/program_facades/bridge.rs +++ b/lez/wallet/src/program_facades/bridge.rs @@ -12,7 +12,6 @@ impl Bridge<'_> { amount: u64, bedrock_account_pk: [u8; 32], ) -> Result { - let program = programs::bridge(); let bridge_account_id = system_accounts::bridge_account_id(); let instruction = bridge_core::Instruction::Withdraw { amount, @@ -28,7 +27,7 @@ impl Bridge<'_> { AccountIdentity::PublicNoSign(bridge_account_id), ], instruction_data, - &program.into(), + programs::bridge().id(), ) .await } diff --git a/lez/wallet/src/program_facades/native_token_transfer/mod.rs b/lez/wallet/src/program_facades/native_token_transfer/mod.rs index 6c8e9715..c4e673fa 100644 --- a/lez/wallet/src/program_facades/native_token_transfer/mod.rs +++ b/lez/wallet/src/program_facades/native_token_transfer/mod.rs @@ -26,7 +26,6 @@ fn auth_transfer_preparation( amount: balance_to_move, }) .unwrap(); - let program = programs::authenticated_transfer(); // TODO: handle large Err-variant properly let tx_pre_check = move |accounts: &[&Account]| { @@ -38,5 +37,9 @@ fn auth_transfer_preparation( } }; - (instruction_data, program, tx_pre_check) + ( + instruction_data, + programs::authenticated_transfer(), + tx_pre_check, + ) } diff --git a/lez/wallet/src/program_facades/native_token_transfer/public.rs b/lez/wallet/src/program_facades/native_token_transfer/public.rs index 2ea70502..ee01d9c8 100644 --- a/lez/wallet/src/program_facades/native_token_transfer/public.rs +++ b/lez/wallet/src/program_facades/native_token_transfer/public.rs @@ -21,7 +21,7 @@ impl NativeTokenTransfer<'_> { .send_pub_tx_with_pre_check( vec![from, to], instruction_data, - &program.into(), + program.id(), tx_pre_check, ) .await @@ -31,11 +31,14 @@ impl NativeTokenTransfer<'_> { &self, account: AccountIdentity, ) -> Result { - let program = programs::authenticated_transfer(); let instruction_data = Program::serialize_instruction(AuthTransferInstruction::Initialize)?; self.0 - .send_pub_tx(vec![account], instruction_data, &program.into()) + .send_pub_tx( + vec![account], + instruction_data, + programs::authenticated_transfer().id(), + ) .await } } diff --git a/lez/wallet/src/program_facades/pinata.rs b/lez/wallet/src/program_facades/pinata.rs index eec9ec01..3a1ff898 100644 --- a/lez/wallet/src/program_facades/pinata.rs +++ b/lez/wallet/src/program_facades/pinata.rs @@ -13,7 +13,6 @@ impl Pinata<'_> { winner_account_id: AccountId, solution: u128, ) -> Result { - let program = programs::pinata(); let instruction = solution; let instruction_data = Program::serialize_instruction(instruction).expect("Instruction should serialize"); @@ -25,7 +24,7 @@ impl Pinata<'_> { AccountIdentity::PublicNoSign(winner_account_id), ], instruction_data, - &program.into(), + programs::pinata().id(), ) .await } diff --git a/lez/wallet/src/program_facades/token.rs b/lez/wallet/src/program_facades/token.rs index ca34b1e6..d634976f 100644 --- a/lez/wallet/src/program_facades/token.rs +++ b/lez/wallet/src/program_facades/token.rs @@ -15,13 +15,16 @@ impl Token<'_> { name: String, total_supply: u128, ) -> Result { - let program = programs::token(); let instruction = Instruction::NewFungibleDefinition { name, total_supply }; let instruction_data = Program::serialize_instruction(instruction).expect("Instruction should serialize"); self.0 - .send_pub_tx(vec![definition, supply], instruction_data, &program.into()) + .send_pub_tx( + vec![definition, supply], + instruction_data, + programs::token().id(), + ) .await } @@ -128,7 +131,6 @@ impl Token<'_> { recipient: AccountIdentity, amount: u128, ) -> Result { - let program = programs::token(); let instruction = Instruction::Transfer { amount_to_transfer: amount, }; @@ -136,7 +138,11 @@ impl Token<'_> { Program::serialize_instruction(instruction).expect("Instruction should serialize"); self.0 - .send_pub_tx(vec![sender, recipient], instruction_data, &program.into()) + .send_pub_tx( + vec![sender, recipient], + instruction_data, + programs::token().id(), + ) .await } @@ -319,7 +325,6 @@ impl Token<'_> { holder: AccountIdentity, amount: u128, ) -> Result { - let program = programs::token(); let instruction = Instruction::Burn { amount_to_burn: amount, }; @@ -330,7 +335,7 @@ impl Token<'_> { .send_pub_tx( vec![AccountIdentity::PublicNoSign(definition_account_id), holder], instruction_data, - &program.into(), + programs::token().id(), ) .await } @@ -441,7 +446,6 @@ impl Token<'_> { holder: AccountIdentity, amount: u128, ) -> Result { - let program = programs::token(); let instruction = Instruction::Mint { amount_to_mint: amount, }; @@ -449,7 +453,11 @@ impl Token<'_> { Program::serialize_instruction(instruction).expect("Instruction should serialize"); self.0 - .send_pub_tx(vec![definition, holder], instruction_data, &program.into()) + .send_pub_tx( + vec![definition, holder], + instruction_data, + programs::token().id(), + ) .await } diff --git a/lez/wallet/src/program_facades/vault.rs b/lez/wallet/src/program_facades/vault.rs index ad25f5e0..dcb5c5e0 100644 --- a/lez/wallet/src/program_facades/vault.rs +++ b/lez/wallet/src/program_facades/vault.rs @@ -17,8 +17,7 @@ impl Vault<'_> { recipient_id: AccountId, amount: u128, ) -> Result { - let program = programs::vault(); - let vault_program_id = program.id(); + let vault_program_id = programs::vault().id(); let recipient_vault_id = vault_core::compute_vault_account_id(vault_program_id, recipient_id); @@ -36,7 +35,7 @@ impl Vault<'_> { AccountIdentity::PublicNoSign(recipient_vault_id), ], instruction_data, - &program.into(), + vault_program_id, ) .await } @@ -80,8 +79,7 @@ impl Vault<'_> { owner_id: AccountId, amount: u128, ) -> Result { - let program = programs::vault(); - let vault_program_id = program.id(); + let vault_program_id = programs::vault().id(); let owner_vault_id = vault_core::compute_vault_account_id(vault_program_id, owner_id); let instruction = vault_core::Instruction::Claim { amount }; @@ -95,7 +93,7 @@ impl Vault<'_> { AccountIdentity::PublicNoSign(owner_vault_id), ], instruction_data, - &program.into(), + vault_program_id, ) .await }