Skip to content

Commit

Permalink
Return the ZIP 32 account index from RustBackend.getAccounts
Browse files Browse the repository at this point in the history
Previously we were returning the internal account private key, which
is incorrect for how `Account` is currently used on the Kotlin side.

Closes #1638.
  • Loading branch information
str4d committed Nov 22, 2024
1 parent 6798669 commit 15f46f8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import androidx.annotation.Keep
/**
* Serves as cross layer (Kotlin, Rust) communication class.
*
* @param account the account ID
* @param accountIndex the ZIP 32 account index.
* @param ufvk The account's Unified Full Viewing Key, if any.
* @throws IllegalArgumentException if the values are inconsistent.
*/
@Keep
@Suppress("LongParameterList")
class JniAccount(
val accountId: Long,
val accountIndex: Int,
val ufvk: String?,
) {
init {
require(accountId >= 0) {
"Account ID must be non-negative"
require(accountIndex >= 0) {
"Account index must be non-negative"
}
}
}
16 changes: 14 additions & 2 deletions backend-lib/src/main/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,22 @@ fn encode_account<'a, P: Parameters>(
network: &P,
account: zcash_client_sqlite::wallet::Account,
) -> jni::errors::Result<JObject<'a>> {
let account_index = match account.source() {
AccountSource::Derived { account_index, .. } => account_index,
AccountSource::Imported { .. } => panic!("Should have been filtered out"),
};

let ufvk = match account.ufvk() {
Some(ufvk) => env.new_string(ufvk.encode(network))?.into(),
None => JObject::null(),
};

env.new_object(
JNI_ACCOUNT,
"(JLjava/lang/String;)V",
"(ILjava/lang/String;)V",
&[
// TODO: This will be replaced by the multi-seed-compatible account ID.
JValue::Long(i64::from(account.id().as_u32())),
JValue::Int(u32::from(account_index) as i32),
(&ufvk).into(),
],
)
Expand Down Expand Up @@ -336,6 +341,13 @@ pub extern "C" fn Java_cash_z_ecc_android_sdk_internal_jni_RustBackend_getAccoun
})
.collect::<Result<Vec<_>, _>>()?;

// Filter out imported accounts (for which we don't know a ZIP 32 account index).
// TODO: Remove this when we switch to account identifiers.
let accounts = accounts
.into_iter()
.filter(|account| matches!(account.source(), AccountSource::Derived { .. }))
.collect::<Vec<_>>();

let first_account = accounts.first().cloned();

Ok(utils::rust_vec_to_java(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class TypesafeBackendImpl(private val backend: Backend) : TypesafeBacke
override val network: ZcashNetwork
get() = ZcashNetwork.from(backend.networkId)

override suspend fun getAccounts(): List<Account> = backend.getAccounts().map { Account(it.accountId.toInt()) }
override suspend fun getAccounts(): List<Account> = backend.getAccounts().map { Account(it.accountIndex.toInt()) }

override suspend fun createAccountAndGetSpendingKey(
seed: ByteArray,
Expand Down

0 comments on commit 15f46f8

Please sign in to comment.