Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion packages/kotlin-sdk/PARITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ rebuilt `libdash_sdk_jni.so` and a network at protocol version 15.
| QRScannerView.swift | ui/scanner/QrScannerScreen.kt · `QrScanner` | ported |
| ReceiveAddressView.swift | ui/wallet/ReceiveAddressSheet.kt | ported |
| SeedBackupView.swift | ui/wallet/SeedBackupScreen.kt · `SeedBackup` | ported |
| SendTransactionView.swift | ui/wallet/SendTransactionScreen.kt · `SendTransaction` | ported — `ManagedPlatformWallet.sendToAddresses` drives the atomic finalized-transaction path (`CoreTransactionBuilder.finalizeAtomic` → `core_wallet_tx_builder_finalize` → `core_wallet_broadcast_signed_transaction`); shared Rust owns selection, reservation, signing, and broadcast classification. The SDK also bridges Swift's `shieldedShieldToRecipient` (Platform credits → a third party's Orchard address) as `PlatformWalletManager.shieldedShieldToRecipient` (JNI `FundingNative.shieldedShieldToRecipient` → `platform_wallet_manager_shielded_shield_to_recipient`); SDK only, no UI: like SwiftExampleApp, the screen does not call it, and its caller is the Android wallet |
| SendTransactionView.swift | ui/wallet/SendTransactionScreen.kt · `SendTransaction` | ported — `ManagedPlatformWallet.sendToAddresses` drives the atomic finalized-transaction path (`CoreTransactionBuilder.finalizeAtomic` → `core_wallet_tx_builder_finalize` → `core_wallet_broadcast_signed_transaction`); shared Rust owns selection, reservation, signing, and broadcast classification. The SDK also bridges Swift's `shieldedShieldToRecipient` (Platform credits → a third party's Orchard address) as `PlatformWalletManager.shieldedShieldToRecipient` (JNI `FundingNative.shieldedShieldToRecipient` → `platform_wallet_manager_shielded_shield_to_recipient`); SDK only, no UI: like SwiftExampleApp, the screen does not call it, and its caller is the Android wallet. Its identity-funded sibling `PlatformWalletManager.shieldedShieldFromIdentityToRecipient` (identity balance → a third party's Orchard address; JNI `FundingNative.shieldedShieldFromIdentityToRecipient` → `platform_wallet_manager_shielded_shield_from_identity_to_recipient`) is also SDK only, no UI, and has no Swift counterpart yet; its caller is the Android wallet |
| ShieldedActivityView.swift | ui/shielded/ShieldedActivityScreen.kt · `ShieldedActivity` | ported |
| TransactionDetailView.swift | ui/wallet/TransactionDetailScreen.kt · `WalletTransactionDetail` | ported |
| TransactionListView.swift | ui/wallet/TransactionListScreen.kt · `WalletTransactions` | ported |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package org.dashfoundation.dashsdk.wallet

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.dashfoundation.dashsdk.errors.DashSdkError
import org.dashfoundation.dashsdk.ffi.DashSDKException
import org.dashfoundation.dashsdk.ffi.FundingNative
import org.dashfoundation.dashsdk.ffi.NativeLoader
import org.junit.Assert.assertEquals
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith

/**
* Binding-level coverage for the `shieldedShieldFromIdentityToRecipient` JNI
* export, with the same no-wallet discipline as [ShieldToRecipientBindingTest].
* It proves the Kotlin `external` declaration, the generated JNI symbol and
* the parameter descriptor stay in lockstep (a mismatch surfaces here as
* `UnsatisfiedLinkError`), and pins each rejection to its layer. No network,
* no wallet, no funds.
*
* Three layers are asserted apart:
* - the JNI trampoline's marshalling checks (sign checks before the unsigned
* casts, fixed-size arrays, non-null signer) throw [DashSDKException] with
* RAW code 1 and a message naming the argument, BEFORE any FFI call. The
* two 32-byte ids share a descriptor slot type, so the message checks
* double as parameter-order pins;
* - a zero amount and an over-limit memo are not checked there. Zero passes
* the JNI layer and fails at the dead handle; the memo is rejected inside
* the FFI by Rust's `encode_memo_text` (the single owner of the 32-byte
* rule) as `ErrorInvalidParameter`, before the handle is resolved;
* - a well-formed call with dead manager handle 0 reaches the FFI's handle
* lookup and comes back as `ErrorInvalidHandle`.
*
* The FFI encodes the memo and resolves the manager handle before it ever
* dereferences the signer pointer, so [DUMMY_SIGNER] is never read; it only
* has to be non-zero to pass the JNI null check.
*/
@RunWith(AndroidJUnit4::class)
class ShieldFromIdentityToRecipientBindingTest {

private companion object {
const val DEAD_HANDLE = 0L
const val DUMMY_SIGNER = 1L

/** `PlatformWalletFFIResultCode` values, shifted into the Kotlin range. */
const val INVALID_HANDLE = DashSdkError.PLATFORM_WALLET_CODE_OFFSET + 1
const val INVALID_PARAMETER = DashSdkError.PLATFORM_WALLET_CODE_OFFSET + 2
}

private fun callExpectingThrow(
walletId: ByteArray = ByteArray(32) { 1 },
shieldedAccount: Int = 0,
identityId: ByteArray = ByteArray(32) { 3 },
recipient: ByteArray = ByteArray(43) { 2 },
amount: Long = 1_000L,
memo: String? = null,
signer: Long = DUMMY_SIGNER,
): DashSDKException {
NativeLoader.ensureLoaded()
return assertThrows(DashSDKException::class.java) {
FundingNative.shieldedShieldFromIdentityToRecipient(
DEAD_HANDLE,
walletId,
shieldedAccount,
identityId,
recipient,
amount,
memo,
signer,
)
}
}

private fun assertJniRejection(e: DashSDKException, argument: String) {
assertEquals("JNI-side rejection carries raw code 1: ${e.message}", 1, e.code)
assertTrue(
"the rejection must name $argument, got: ${e.message}",
e.message.orEmpty().contains(argument),
)
}

@Test
fun shouldRejectNegativeAmountAndAccountBeforeTheFfi() {
assertJniRejection(callExpectingThrow(amount = -1L), "amount")
assertJniRejection(callExpectingThrow(shieldedAccount = -1), "shieldedAccount")
}

@Test
fun shouldRejectANullSignerBeforeTheFfi() {
assertJniRejection(callExpectingThrow(signer = 0L), "signerIdentityHandle")
}

@Test
fun shouldRejectMisSizedIdsBeforeTheFfi() {
assertJniRejection(callExpectingThrow(walletId = ByteArray(31)), "walletId")
assertJniRejection(callExpectingThrow(identityId = ByteArray(33)), "identityId")
assertJniRejection(callExpectingThrow(recipient = ByteArray(42)), "recipientRaw43")
}

@Test
fun shouldLeaveAZeroAmountToRust() {
// The JNI layer only blocks negatives; zero crosses into the FFI and
// here fails at the dead handle, before the wallet's own > 0 check.
val e = callExpectingThrow(amount = 0L)
assertEquals("zero amount: ${e.message}", INVALID_HANDLE, e.code)
}

@Test
fun shouldLeaveTheMemoLimitToRust() {
// 20 × "€" = 60 UTF-8 bytes. Neither Kotlin nor the JNI layer checks
// the length; Rust's encode_memo_text rejects it inside the FFI.
val e = callExpectingThrow(memo = "€".repeat(20))
assertEquals("over-limit memo: ${e.message}", INVALID_PARAMETER, e.code)
}

@Test
fun shouldReachTheFfiInvalidHandlePathForWellFormedCalls() {
// null, empty and a multibyte memo within 32 UTF-8 bytes (10 × "€" =
// 30 bytes) all pass every check and fail only at the handle lookup.
for (memo in listOf(null, "", "€".repeat(10))) {
val e = callExpectingThrow(memo = memo)
assertEquals("memo=${memo?.length}: ${e.message}", INVALID_HANDLE, e.code)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,26 @@ internal object FundingNative {
signerIdentityHandle: Long,
): Long

/**
* Shield from a Platform IDENTITY's balance to a third-party Orchard
* address, Type 21 (bridges
* `platform_wallet_manager_shielded_shield_from_identity_to_recipient`).
* See
* [org.dashfoundation.dashsdk.wallet.PlatformWalletManager.shieldedShieldFromIdentityToRecipient].
* [memoText] null or empty means no memo. Blocks for the ~30s Halo 2
* proof; returns the identity's proven post-debit credit balance.
*/
external fun shieldedShieldFromIdentityToRecipient(
managerHandle: Long,
walletId: ByteArray,
shieldedAccount: Int,
identityId: ByteArray,
recipientRaw43: ByteArray,
amount: Long,
memoText: String?,
signerIdentityHandle: Long,
): Long

/**
* Create an identity funded from the shielded pool, Type 20 (bridges
* `platform_wallet_manager_shielded_identity_create_from_pool`). Spends a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,86 @@ class PlatformWalletManager(
}
}

/**
* Identity → EXTERNAL shielded (Type 21). The same `ShieldFromIdentity`
* transition as [shieldedShieldFromIdentity], with the same identity
* debit ([amount] plus the metered fee plus the shielded compute fee),
* the same Keystore identity signer ([signerHandle], TRANSFER key) and
* the same error shapes. The only difference is that the note is
* assigned to [recipientRaw43], a third party's raw 43-byte Orchard
* payment address, instead of this wallet's own default address.
*
* The recipient must actually be a third party: Rust rejects an address
* the account's own IVK recognizes (default or any diversified index).
* Self-shields must use [shieldedShieldFromIdentity]. A bound shielded
* account ([shieldedAccount]) is still required: the send is
* OVK-encrypted to that account and recorded there, which is how the
* wallet's own scan later shows it as sent history.
*
* [memo] is optional: null or empty means no memo. When set, its UTF-8
* byte length must be at most 32. Rust owns these rules, as it owns the
* zero-amount rejection, so a violation surfaces as a native error, not a
* Kotlin precondition.
*
* Do NOT retry on either of these; a retry could debit the identity
* twice, and the next sync reconciles the outcome:
* - [org.dashfoundation.dashsdk.errors.DashSdkError.PlatformWallet.ShieldedSpendUnconfirmed]:
* the transition may already have executed.
* - [org.dashfoundation.dashsdk.errors.DashSdkError.PlatformWallet.ShieldedIdentityDebitPending]:
* an earlier identity debit is still unresolved, so this one was
* never built or broadcast.
*
* Cancellation: if the caller is cancelled after the native call has
* completed, the post-debit balance is still returned (see
* [opReturningCompletedResult]), so a completed identity debit is never
* hidden. Handle the return before the next suspension point.
*
* Blocks for the ~30s Halo 2 proof; the note arrives on the recipient's
* next shielded sync pass.
*
* @param walletId the 32-byte wallet id.
* @param identityId the 32-byte funding identity id; must be managed by
* this wallet.
* @param recipientRaw43 the recipient's raw 43-byte Orchard payment
* address (11-byte diversifier + 32-byte pk_d), the same shape
* [shieldedTransfer] takes.
* @param amount credits to shield (1 DASH = 1e11).
* @param memo optional UTF-8 memo for the recipient's note.
* @return the identity's proven post-debit credit balance.
*/
suspend fun shieldedShieldFromIdentityToRecipient(
walletId: ByteArray,
identityId: ByteArray,
recipientRaw43: ByteArray,
amount: Long,
shieldedAccount: Int = 0,
memo: String? = null,
): Long = teardownGate.opReturningCompletedResult {
require(identityId.size == 32) {
"identityId must be exactly 32 bytes, got ${identityId.size}"
}
require(recipientRaw43.size == 43) {
"recipientRaw43 must be exactly 43 bytes, got ${recipientRaw43.size}"
}
// Sign checks only, before the native unsigned casts; zero is Rust's call.
require(amount >= 0) { "amount must be non-negative, got $amount" }
require(shieldedAccount >= 0) {
"shieldedAccount must be non-negative, got $shieldedAccount"
}
mapNativeErrors {
FundingNative.shieldedShieldFromIdentityToRecipient(
managerHandle,
walletId,
shieldedAccount,
identityId,
recipientRaw43,
amount,
memo,
signerHandle,
)
}
}

/** List active and archived durable identity-funded shields for this manager's wallet. */
suspend fun shieldedIdentityDebitRecoveryRecords(
walletId: ByteArray,
Expand Down
Loading
Loading