Skip to content

Commit

Permalink
Merge pull request #2422 from dusk-network/wallet-core-ffi
Browse files Browse the repository at this point in the history
wallet-core: adjust lib for FFI usage
  • Loading branch information
ZER0 authored Sep 25, 2024
2 parents 4317213 + 5e868ef commit 22a2254
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
33 changes: 32 additions & 1 deletion wallet-core/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ use core::{ptr, slice};
use dusk_bytes::Serializable;
use execution_core::{
signatures::bls::PublicKey as BlsPublicKey,
transfer::phoenix::{NoteLeaf, PublicKey as PhoenixPublicKey},
transfer::phoenix::{
ArchivedNoteLeaf, NoteLeaf, PublicKey as PhoenixPublicKey,
},
BlsScalar,
};
use zeroize::Zeroize;

use rkyv::to_bytes;

#[no_mangle]
static KEY_SIZE: usize = BlsScalar::SIZE;
#[no_mangle]
static ITEM_SIZE: usize = core::mem::size_of::<ArchivedNoteLeaf>();

/// The size of the scratch buffer used for parsing the notes.
const NOTES_BUFFER_SIZE: usize = 96 * 1024;

Expand Down Expand Up @@ -89,10 +97,23 @@ pub unsafe fn map_owned(
indexes: *const u8,
notes_ptr: *const u8,
owned_ptr: *mut *mut u8,
last_info_ptr: *mut [u8; 16],
) -> ErrorCode {
use core::cmp::max;

let keys = indexes_into_keys(seed, indexes, derive_phoenix_sk);
let notes: Vec<NoteLeaf> = mem::from_buffer(notes_ptr)?;

let (block_height, pos) =
notes
.iter()
.fold((0u64, 0u64), |(block_height, pos), leaf| {
(
max(block_height, leaf.block_height),
max(pos, *leaf.note.pos()),
)
});

let owned = notes::owned::map(&keys, notes);

keys.into_iter().for_each(|mut sk| sk.zeroize());
Expand All @@ -110,6 +131,16 @@ pub unsafe fn map_owned(

ptr::copy_nonoverlapping(len.as_ptr(), ptr, 4);
ptr::copy_nonoverlapping(bytes.as_ptr(), ptr.add(4), bytes.len());
ptr::copy_nonoverlapping(
block_height.to_le_bytes().as_ptr(),
&mut (*last_info_ptr)[0],
8,
);
ptr::copy_nonoverlapping(
pos.to_le_bytes().as_ptr(),
&mut (*last_info_ptr)[8],
8,
);

ErrorCode::Ok
}
Expand Down
27 changes: 27 additions & 0 deletions wallet-core/src/notes/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ pub fn calculate<T>(
vk: &PhoenixViewKey,
notes: impl Iterator<Item = T>,
) -> TotalAmount
where
T: AsRef<Note>,
{
let mut values: Vec<u64> = notes
.filter_map(|note| {
vk.owns(note.as_ref().stealth_address())
.then_some(true)
.and(note.as_ref().value(Some(vk)).ok())
})
.collect();

values.sort_by(|a, b| b.cmp(a));

let spendable = values.iter().take(MAX_INPUT_NOTES).sum();
let value = spendable + values.iter().skip(MAX_INPUT_NOTES).sum::<u64>();

TotalAmount { value, spendable }
}

/// Calculate the sum for all the given [`Note`]s without
/// performing any ownership checks. The [`PhoenixViewKey`]
/// is used solely for decrypting the values of obfuscated
/// notes.
pub fn calculate_unchecked<T>(
vk: &PhoenixViewKey,
notes: impl Iterator<Item = T>,
) -> TotalAmount
where
T: AsRef<Note>,
{
Expand Down

0 comments on commit 22a2254

Please sign in to comment.