Factor out the conversion of the `diversifier_index_be` field in the

`addresses` table to a `NonHardenedChildIndex`.

(This moves where a `diversifier_index_be` field of the wrong length would
be detected and so is not quite a no-op, but that shouldn't matter.)

Signed-off-by: Daira-Emma Hopwood <daira@jacaranda.org>
This commit is contained in:
Daira-Emma Hopwood 2024-06-25 16:59:34 +01:00
parent 4f43a01f83
commit 5a90fffed4
1 changed files with 25 additions and 24 deletions

View File

@ -54,6 +54,28 @@ pub(crate) fn detect_spending_accounts<'a>(
Ok(acc)
}
/// Returns the `NonHardenedChildIndex` corresponding to a diversifier index
/// given as bytes in big-endian order (the reverse of the usual order).
fn address_index_from_diversifier_index_be(
diversifier_index_be: &[u8],
) -> Result<NonHardenedChildIndex, SqliteClientError> {
let mut di: [u8; 11] = diversifier_index_be.try_into().map_err(|_| {
SqliteClientError::CorruptedData("Diversifier index is not an 11-byte value".to_owned())
})?;
di.reverse(); // BE -> LE conversion
NonHardenedChildIndex::from_index(DiversifierIndex::from(di).try_into().map_err(|_| {
SqliteClientError::CorruptedData(
"Unable to get diversifier for transparent address.".to_string(),
)
})?)
.ok_or_else(|| {
SqliteClientError::CorruptedData(
"Unexpected hardened index for transparent address.".to_string(),
)
})
}
pub(crate) fn get_transparent_receivers<P: consensus::Parameters>(
conn: &rusqlite::Connection,
params: &P,
@ -70,10 +92,6 @@ pub(crate) fn get_transparent_receivers<P: consensus::Parameters>(
while let Some(row) = rows.next()? {
let ua_str: String = row.get(0)?;
let di_vec: Vec<u8> = row.get(1)?;
let mut di: [u8; 11] = di_vec.try_into().map_err(|_| {
SqliteClientError::CorruptedData("Diversifier index is not an 11-byte value".to_owned())
})?;
di.reverse(); // BE -> LE conversion
let ua = Address::decode(params, &ua_str)
.ok_or_else(|| {
@ -88,26 +106,9 @@ pub(crate) fn get_transparent_receivers<P: consensus::Parameters>(
})?;
if let Some(taddr) = ua.transparent() {
let index = NonHardenedChildIndex::from_index(
DiversifierIndex::from(di).try_into().map_err(|_| {
SqliteClientError::CorruptedData(
"Unable to get diversifier for transparent address.".to_string(),
)
})?,
)
.ok_or_else(|| {
SqliteClientError::CorruptedData(
"Unexpected hardened index for transparent address.".to_string(),
)
})?;
ret.insert(
*taddr,
Some(TransparentAddressMetadata::new(
Scope::External.into(),
index,
)),
);
let address_index = address_index_from_diversifier_index_be(&di_vec)?;
let metadata = TransparentAddressMetadata::new(Scope::External.into(), address_index);
ret.insert(*taddr, Some(metadata));
}
}