Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6542dbc
add_output returns TxOutContext
Apr 19, 2022
96bc944
Overloading modified tx builder functions for compatibility
dolanbernard May 11, 2022
41c1c41
Add TxOutContext to transaction_std lib.rs so its scope is automatica…
the-real-adammork May 11, 2022
0bf1d79
add documentation to TxOutContext variables. Change copy out semantic…
the-real-adammork May 11, 2022
45bca78
Android Bindings TxOutContext
dolanbernard May 11, 2022
b2c01bd
Removing comments
dolanbernard May 12, 2022
e0acae0
Lint
dolanbernard May 12, 2022
8112d3f
Lint
dolanbernard May 12, 2022
7080744
Fixing test
dolanbernard May 12, 2022
7ae9bc6
Fixing test
dolanbernard May 12, 2022
d867ca6
Remove duplicated add_output and add_change_output
dolanbernard May 13, 2022
7b83218
Update libmobilecoin/include/transaction.h
dolanbernard May 13, 2022
de2c2b9
Update libmobilecoin/include/transaction.h
dolanbernard May 13, 2022
b4f37de
Struct deconstruction to fix build error
dolanbernard May 13, 2022
162d4fb
Merge branch 'tx-builder-shared-secret' of https://github.com/mobilec…
dolanbernard May 13, 2022
0b12de0
Fixing imports
dolanbernard May 13, 2022
8946ff2
Deconstructing more structs
dolanbernard May 13, 2022
8ac262f
Fixing syntax
dolanbernard May 13, 2022
d88c109
Fixing tests
dolanbernard May 13, 2022
9559d1b
Remove references to old new functions
dolanbernard May 13, 2022
21a3564
Fixing struct deconstruction
dolanbernard May 13, 2022
9e655f9
Test fix
dolanbernard May 14, 2022
e9f5d62
Test fixes
dolanbernard May 14, 2022
f204c71
Imports
dolanbernard May 14, 2022
652652c
Fixing test imports
dolanbernard May 14, 2022
6f12035
Lint
dolanbernard May 16, 2022
a76725e
Merge branch 'candidate-1.2' into tx-builder-shared-secret
dolanbernard May 16, 2022
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
76 changes: 68 additions & 8 deletions android-bindings/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use mc_transaction_core::{
use mc_transaction_std::{
AuthenticatedSenderMemo, AuthenticatedSenderWithPaymentRequestIdMemo, ChangeDestination,
DestinationMemo, InputCredentials, MemoBuilder, MemoPayload, RTHMemoBuilder,
SenderMemoCredential, TransactionBuilder,
SenderMemoCredential, TransactionBuilder, TxOutContext,
};

use mc_util_from_random::FromRandom;
Expand Down Expand Up @@ -1478,6 +1478,65 @@ pub unsafe extern "C" fn Java_com_mobilecoin_lib_TxOut_decrypt_1memo_1payload(
)
}

/********************************************************************
* TxOutContext
*/
#[no_mangle]
pub unsafe extern "C" fn Java_com_mobilecoin_lib_TxOutContext_get_1tx_1out(
env: JNIEnv,
obj: JObject,
) -> jlong {
jni_ffi_call_or(
|| Ok(0),
&env,
|env| {
let tx_out_context: MutexGuard<TxOutContext> =
env.get_rust_field(obj, RUST_OBJ_FIELD)?;
let tx_out = tx_out_context.tx_out.to_owned();
let mbox = Box::new(Mutex::new(tx_out));
let ptr: *mut Mutex<TxOut> = Box::into_raw(mbox);
Ok(ptr as jlong)
},
)
}

#[no_mangle]
pub unsafe extern "C" fn Java_com_mobilecoin_lib_TxOutContext_get_1confirmation_1number(
env: JNIEnv,
obj: JObject,
) -> jbyteArray {
jni_ffi_call_or(
|| Ok(JObject::null().into_inner()),
&env,
|env| {
let tx_out_context: MutexGuard<TxOutContext> =
env.get_rust_field(obj, RUST_OBJ_FIELD)?;
let confirmation_number = &tx_out_context.confirmation;
let bytes = mc_util_serial::encode(confirmation_number);
Ok(env.byte_array_from_slice(&bytes)?)
},
)
}

#[no_mangle]
pub unsafe extern "C" fn Java_com_mobilecoin_lib_TxOutContext_get_1shared_1secret(
env: JNIEnv,
obj: JObject,
) -> jlong {
jni_ffi_call_or(
|| Ok(0),
&env,
|env| {
let tx_out_context: MutexGuard<TxOutContext> =
env.get_rust_field(obj, RUST_OBJ_FIELD)?;
let shared_secret = tx_out_context.shared_secret.to_owned();
let mbox = Box::new(Mutex::new(shared_secret));
let ptr: *mut Mutex<RistrettoPublic> = Box::into_raw(mbox);
Ok(ptr as jlong)
},
)
}

/********************************************************************
* TxOutMembershipProof
*/
Expand Down Expand Up @@ -1713,8 +1772,8 @@ pub unsafe extern "C" fn Java_com_mobilecoin_lib_TransactionBuilder_add_1output(
env.get_rust_field(recipient, RUST_OBJ_FIELD)?;

let mut rng = McRng::default();
let (tx_out, confirmation_number) =
tx_builder.add_output(value as u64, &recipient, &mut rng)?;
let tx_out_context = tx_builder.add_output(value as u64, &recipient, &mut rng)?;
let confirmation_number = &tx_out_context.confirmation;
if !confirmation_number_out.is_null() {
let len = env.get_array_length(confirmation_number_out)?;
if len as usize >= confirmation_number.to_vec().len() {
Expand All @@ -1731,8 +1790,8 @@ pub unsafe extern "C" fn Java_com_mobilecoin_lib_TransactionBuilder_add_1output(
}
}

let mbox = Box::new(Mutex::new(tx_out));
let ptr: *mut Mutex<TxOut> = Box::into_raw(mbox);
let mbox = Box::new(Mutex::new(tx_out_context));
let ptr: *mut Mutex<TxOutContext> = Box::into_raw(mbox);
Ok(ptr as jlong)
},
)
Expand Down Expand Up @@ -1760,8 +1819,9 @@ pub unsafe extern "C" fn Java_com_mobilecoin_lib_TransactionBuilder_add_1change_
ChangeDestination::from(&*source_account_key);
let mut rng = McRng::default();

let (tx_out, confirmation_number) =
let tx_out_context =
tx_builder.add_change_output(value, &change_destination, &mut rng)?;
let confirmation_number = &tx_out_context.confirmation;
if !confirmation_number_out.is_null() {
let len = env.get_array_length(confirmation_number_out)?;
if len as usize >= confirmation_number.to_vec().len() {
Expand All @@ -1778,8 +1838,8 @@ pub unsafe extern "C" fn Java_com_mobilecoin_lib_TransactionBuilder_add_1change_
}
}

let mbox = Box::new(Mutex::new(tx_out));
let ptr: *mut Mutex<TxOut> = Box::into_raw(mbox);
let mbox = Box::new(Mutex::new(tx_out_context));
let ptr: *mut Mutex<TxOutContext> = Box::into_raw(mbox);
Ok(ptr as jlong)
},
)
Expand Down
6 changes: 4 additions & 2 deletions libmobilecoin/include/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,10 @@ McData* MC_NULLABLE mc_transaction_builder_add_output(
const McPublicAddress* MC_NONNULL recipient_address,
McRngCallback* MC_NULLABLE rng_callback,
McMutableBuffer* MC_NONNULL out_tx_out_confirmation_number,
McMutableBuffer* MC_NONNULL out_tx_out_shared_secret,
McError* MC_NULLABLE * MC_NULLABLE out_error
)
MC_ATTRIBUTE_NONNULL(1, 3, 6);
MC_ATTRIBUTE_NONNULL(1, 3, 5, 6);

/// # Preconditions
///
Expand All @@ -237,9 +238,10 @@ McData* MC_NULLABLE mc_transaction_builder_add_change_output(
uint64_t amount,
McRngCallback* MC_NULLABLE rng_callback,
McMutableBuffer* MC_NONNULL out_tx_out_confirmation_number,
McMutableBuffer* MC_NONNULL out_tx_out_shared_secret,
McError* MC_NULLABLE * MC_NULLABLE out_error
)
MC_ATTRIBUTE_NONNULL(1, 2, 4, 6);
MC_ATTRIBUTE_NONNULL(1, 2, 4, 5, 6);

/// # Preconditions
///
Expand Down
2 changes: 2 additions & 0 deletions libmobilecoin/libmobilecoin_cbindgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ FfiOptOwnedPtr<McData> mc_transaction_builder_add_output(FfiMutPtr<McTransaction
FfiRefPtr<McPublicAddress> recipient_address,
FfiOptMutPtr<McRngCallback> rng_callback,
FfiMutPtr<McMutableBuffer> out_tx_out_confirmation_number,
FfiMutPtr<McMutableBuffer> out_tx_out_shared_secret,
FfiOptMutPtr<FfiOptOwnedPtr<McError>> out_error);

/**
Expand All @@ -806,6 +807,7 @@ FfiOptOwnedPtr<McData> mc_transaction_builder_add_change_output(FfiRefPtr<McAcco
uint64_t amount,
FfiOptMutPtr<McRngCallback> rng_callback,
FfiMutPtr<McMutableBuffer> out_tx_out_confirmation_number,
FfiMutPtr<McMutableBuffer> out_tx_out_shared_secret,
FfiOptMutPtr<FfiOptOwnedPtr<McError>> out_error);

/**
Expand Down
28 changes: 22 additions & 6 deletions libmobilecoin/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ pub extern "C" fn mc_transaction_builder_add_output(
recipient_address: FfiRefPtr<McPublicAddress>,
rng_callback: FfiOptMutPtr<McRngCallback>,
out_tx_out_confirmation_number: FfiMutPtr<McMutableBuffer>,
out_tx_out_shared_secret: FfiMutPtr<McMutableBuffer>,
Comment on lines 483 to +484
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're switching TransactionBuilder::add_*output to return TxOutContext, would it make sense to similarly make these FFI add_output methods return the whole TxOutContext, replacing these 2 out pointers?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, using the second out pointer was my idea, but I think returning a whole TxOutContext would be better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ill work on a commit to make this change

out_error: FfiOptMutPtr<FfiOptOwnedPtr<McError>>,
) -> FfiOptOwnedPtr<McData> {
ffi_boundary_with_error(out_error, || {
Expand All @@ -496,11 +497,18 @@ pub extern "C" fn mc_transaction_builder_add_output(
.as_slice_mut_of_len(TxOutConfirmationNumber::size())
.expect("out_tx_out_confirmation_number length is insufficient");

let (tx_out, confirmation) =
let tx_out_context =
transaction_builder.add_output(amount, &recipient_address, &mut rng)?;

out_tx_out_confirmation_number.copy_from_slice(confirmation.as_ref());
Ok(mc_util_serial::encode(&tx_out))
out_tx_out_confirmation_number.copy_from_slice(tx_out_context.confirmation.as_ref());

let out_tx_out_shared_secret = out_tx_out_shared_secret
.into_mut()
.as_slice_mut_of_len(RistrettoPublic::size())
.expect("out_tx_out_shared_secret length is insufficient");

out_tx_out_shared_secret.copy_from_slice(&tx_out_context.shared_secret.to_bytes());
Ok(mc_util_serial::encode(&tx_out_context.tx_out))
})
}

Expand All @@ -523,6 +531,7 @@ pub extern "C" fn mc_transaction_builder_add_change_output(
amount: u64,
rng_callback: FfiOptMutPtr<McRngCallback>,
out_tx_out_confirmation_number: FfiMutPtr<McMutableBuffer>,
out_tx_out_shared_secret: FfiMutPtr<McMutableBuffer>,
out_error: FfiOptMutPtr<FfiOptOwnedPtr<McError>>,
) -> FfiOptOwnedPtr<McData> {
ffi_boundary_with_error(out_error, || {
Expand All @@ -539,11 +548,18 @@ pub extern "C" fn mc_transaction_builder_add_change_output(
.as_slice_mut_of_len(TxOutConfirmationNumber::size())
.expect("out_tx_out_confirmation_number length is insufficient");

let (tx_out, confirmation) =
let tx_out_context =
transaction_builder.add_change_output(amount, &change_destination, &mut rng)?;

out_tx_out_confirmation_number.copy_from_slice(confirmation.as_ref());
Ok(mc_util_serial::encode(&tx_out))
out_tx_out_confirmation_number.copy_from_slice(tx_out_context.confirmation.as_ref());

let out_tx_out_shared_secret = out_tx_out_shared_secret
.into_mut()
.as_slice_mut_of_len(RistrettoPublic::size())
.expect("out_tx_out_shared_secret length is insufficient");

out_tx_out_shared_secret.copy_from_slice(&tx_out_context.shared_secret.to_bytes());
Ok(mc_util_serial::encode(&tx_out_context.tx_out))
})
}

Expand Down
9 changes: 7 additions & 2 deletions mobilecoind/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use mc_transaction_core::{
};
use mc_transaction_std::{
ChangeDestination, EmptyMemoBuilder, InputCredentials, MemoBuilder, TransactionBuilder,
TxOutContext,
};
use mc_util_uri::FogUri;
use rand::Rng;
Expand Down Expand Up @@ -970,12 +971,16 @@ impl<T: BlockchainConnection + UserTxConnection + 'static, FPR: FogPubkeyResolve
let mut tx_out_to_outlay_index = HashMap::default();
let mut outlay_confirmation_numbers = Vec::default();
for (i, outlay) in destinations.iter().enumerate() {
let (tx_out, confirmation_number) = tx_builder
let TxOutContext {
tx_out,
confirmation,
..
} = tx_builder
.add_output(outlay.value, &outlay.receiver, rng)
.map_err(|err| Error::TxBuild(format!("failed adding output: {}", err)))?;

tx_out_to_outlay_index.insert(tx_out, i);
outlay_confirmation_numbers.push(confirmation_number);
outlay_confirmation_numbers.push(confirmation);

total_value += outlay.value;
}
Expand Down
14 changes: 9 additions & 5 deletions mobilecoind/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ mod test {
tx::{Tx, TxOut},
Amount, Block, BlockContents, BlockVersion, Token,
};
use mc_transaction_std::{EmptyMemoBuilder, MemoType, TransactionBuilder};
use mc_transaction_std::{EmptyMemoBuilder, MemoType, TransactionBuilder, TxOutContext};
use mc_util_repr_bytes::{typenum::U32, GenericArray, ReprBytes};
use mc_util_uri::FogUri;
use rand::{rngs::StdRng, SeedableRng};
Expand Down Expand Up @@ -3047,7 +3047,11 @@ mod test {
EmptyMemoBuilder::default(),
)
.unwrap();
let (tx_out, tx_confirmation) = transaction_builder
let TxOutContext {
tx_out,
confirmation,
..
} = transaction_builder
.add_output(10, &receiver.subaddress(0), &mut rng)
.unwrap();

Expand All @@ -3064,7 +3068,7 @@ mod test {
));
receipt.set_tx_out_hash(hash.to_vec());
receipt.set_tombstone(10);
receipt.set_confirmation_number(tx_confirmation.to_vec());
receipt.set_confirmation_number(confirmation.to_vec());

let mut request = mc_mobilecoind_api::GetTxStatusAsReceiverRequest::new();
request.set_receipt(receipt);
Expand Down Expand Up @@ -5522,7 +5526,7 @@ mod test {
EmptyMemoBuilder::default(),
)
.unwrap();
let (tx_out, _tx_confirmation) = transaction_builder
let TxOutContext { tx_out, .. } = transaction_builder
.add_output(
10,
&account_key.subaddress(DEFAULT_SUBADDRESS_INDEX),
Expand Down Expand Up @@ -5636,7 +5640,7 @@ mod test {
EmptyMemoBuilder::default(),
)
.unwrap();
let (tx_out, _tx_confirmation) = transaction_builder
let TxOutContext { tx_out, .. } = transaction_builder
.add_output(
10,
&account_key.subaddress(DEFAULT_SUBADDRESS_INDEX),
Expand Down
4 changes: 3 additions & 1 deletion transaction/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ pub use memo::{
SenderMemoCredential, UnusedMemo,
};
pub use memo_builder::{BurnRedemptionMemoBuilder, EmptyMemoBuilder, MemoBuilder, RTHMemoBuilder};
pub use transaction_builder::{DefaultTxOutputsOrdering, TransactionBuilder, TxOutputsOrdering};
pub use transaction_builder::{
DefaultTxOutputsOrdering, TransactionBuilder, TxOutContext, TxOutputsOrdering,
};

// Re-export this to help the exported macros work
pub use mc_transaction_core::MemoPayload;
Loading