Merge pull request #136 from nuttycom/fix_orchard_memo_retrieval

Fix orchard memo retrieval
This commit is contained in:
Kris Nuttycombe 2024-04-17 12:08:35 -06:00 committed by GitHub
commit 0a6fa33117
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 18 additions and 4 deletions

View File

@ -1024,19 +1024,19 @@ pub unsafe extern "C" fn zcashlc_get_sapling_receiver_for_unified_address(
/// - `address` must be non-null and must point to a null-terminated UTF-8 string.
/// - The memory referenced by `address` must not be mutated for the duration of the function call.
#[no_mangle]
pub unsafe extern "C" fn zcashlc_is_valid_shielded_address(
pub unsafe extern "C" fn zcashlc_is_valid_sapling_address(
address: *const c_char,
network_id: u32,
) -> bool {
let res = catch_panic(|| {
let network = parse_network(network_id)?;
let addr = unsafe { CStr::from_ptr(address).to_str()? };
Ok(is_valid_shielded_address(addr, &network))
Ok(is_valid_sapling_address(addr, &network))
});
unwrap_exc_or(res, false)
}
fn is_valid_shielded_address(address: &str, network: &Network) -> bool {
fn is_valid_sapling_address(address: &str, network: &Network) -> bool {
match Address::decode(network, address) {
Some(addr) => match addr {
Address::Sapling(_) => true,
@ -1523,6 +1523,14 @@ pub unsafe extern "C" fn zcashlc_get_total_transparent_balance_for_account(
unwrap_exc_or(res, -1)
}
fn parse_protocol(code: u32) -> Option<ShieldedProtocol> {
match code {
2 => Some(ShieldedProtocol::Sapling),
3 => Some(ShieldedProtocol::Orchard),
_ => None,
}
}
/// Returns the memo for a note by copying the corresponding bytes to the received
/// pointer in `memo_bytes_ret`.
///
@ -1542,6 +1550,7 @@ pub unsafe extern "C" fn zcashlc_get_memo(
db_data: *const u8,
db_data_len: usize,
txid_bytes: *const u8,
output_pool: u32,
output_index: u16,
memo_bytes_ret: *mut u8,
network_id: u32,
@ -1553,8 +1562,13 @@ pub unsafe extern "C" fn zcashlc_get_memo(
let txid_bytes = unsafe { slice::from_raw_parts(txid_bytes, 32) };
let txid = TxId::read(&txid_bytes[..])?;
let protocol = parse_protocol(output_pool).ok_or(anyhow!(
"Shielded protocol not recognized for code: {}",
output_pool
))?;
let memo_bytes = db_data
.get_memo(NoteId::new(txid, ShieldedProtocol::Sapling, output_index))
.get_memo(NoteId::new(txid, protocol, output_index))
.map_err(|e| anyhow!("An error occurred retrieving the memo: {}", e))
.and_then(|memo| memo.ok_or(anyhow!("Memo not available")))
.map(|memo| memo.encode())?;