Skip to content
Merged
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
5 changes: 3 additions & 2 deletions dash-sdk-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ android {
}
}

// 64bit only due to some things in the rust crate `platform-mobile`
// 64bit only due to some things in the rust crate `platform-mobile`;
// the 32-bit ABIs (armeabi-v7a, x86) crash at runtime and are not shipped
defaultConfig {
ndk {
abiFilters 'arm64-v8a', 'x86_64', 'x86', 'armeabi-v7a'
abiFilters 'arm64-v8a', 'x86_64'
}
}
publishing {
Expand Down
18,424 changes: 10,341 additions & 8,083 deletions dash-sdk-android/src/main/rust/src/fermented.rs

Large diffs are not rendered by default.

18,424 changes: 10,341 additions & 8,083 deletions dash-sdk-bindings/src/fermented.rs

Large diffs are not rendered by default.

177 changes: 90 additions & 87 deletions dash-sdk-java/src/main/cpp/clone.h

Large diffs are not rendered by default.

423 changes: 218 additions & 205 deletions dash-sdk-java/src/main/swig/ignore.i

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dash-sdk-java/src/main/swig/root.i
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ MemoryFactory & memoryFactory = *MemoryFactory::getInstance();
%include "platform_value.i"
%include "platform_value_array.i"
%include "vote.i"
%include "state_transition.i"

// modules and crates
%include "dpp.i"
Expand Down
19 changes: 19 additions & 0 deletions dash-sdk-java/src/main/swig/state_transition.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Typemaps for the native state-transition deserialization API (platform-mobile/src/state_transition.rs).
// identity_update_public_keys_to_add returns Result<Vec<IdentityPublicKey>>; map it to
// Result<List<IdentityPublicKey>> (the Vec<IdentityPublicKey> item typemap and clone already exist
// via identity_public_key.i / the generated clone.h). The _ctor/_destroy %ignore directives for
// these types are generated into ignore.i by ignore.py.

DEFINE_RESULT(
platform_mobile_state_transition_StateTransitionInfo,
String,
Result_ok_platform_mobile_state_transition_StateTransitionInfo_err_String,
clone
);

DEFINE_LIST_RESULT(
IdentityPublicKey,
String,
Result_ok_Vec_dpp_identity_identity_public_key_IdentityPublicKey_err_String,
dpp_identity_identity_public_key_IdentityPublicKey
);
5 changes: 3 additions & 2 deletions dpp/src/main/java/org/dashj/platform/dapiclient/DapiClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ class DapiClient(
)
try {
return result.unwrap().map {
Document(it, contractIdentifier)
Document(it, contractIdentifier, type)
}
} catch (e: Exception) {
if (e.message?.contains("context provider error: invalid quorum: quorum not found") == true ||
Expand Down Expand Up @@ -1206,7 +1206,8 @@ class DapiClient(
dataContractId.toNative(),
documentType
).unwrap(),
dataContractId
dataContractId,
documentType
)
}

Expand Down
18 changes: 15 additions & 3 deletions dpp/src/main/java/org/dashj/platform/dashpay/ContactRequests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.util.Date
import java.util.Timer
import kotlin.concurrent.timerTask
import org.bouncycastle.crypto.params.KeyParameter
import org.dashj.platform.dapiclient.SystemIds
import org.dashj.platform.dapiclient.model.DocumentQuery
import org.dashj.platform.dashpay.BlockchainIdentity.KeyIndexPurpose
import org.dashj.platform.dashpay.callback.SendContactRequestCallback
Expand Down Expand Up @@ -37,11 +38,20 @@ class ContactRequests(val platform: Platform) {
val contactKey = contactKeyChain.watchingKey
val contactPub = contactKey.serializeContactPub()

// The key must be ESCDA_SECP256K1 for ECDH
// The key must be ESCDA_SECP256K1 for ECDH.
// Prefer an encryption key whose contract bounds are scoped to the DashPay contract,
// then fall back to any encryption key, then to a high-security authentication key.
val dashpayContractId = platform.apps["dashpay"]?.contractId ?: SystemIds.dashpayDataContractId
val toUserPublicKey = toUser.getFirstPublicKey(
Purpose.ENCRYPTION,
SecurityLevel.MEDIUM,
KeyType.ECDSA_SECP256K1
KeyType.ECDSA_SECP256K1,
dashpayContractId
) ?: toUser.getFirstPublicKey(
Comment thread
HashEngineering marked this conversation as resolved.
Resolved
Purpose.ENCRYPTION,
SecurityLevel.MEDIUM,
KeyType.ECDSA_SECP256K1,
null
) ?: toUser.getFirstPublicKey(
Purpose.AUTHENTICATION,
SecurityLevel.HIGH,
Expand Down Expand Up @@ -71,7 +81,9 @@ class ContactRequests(val platform: Platform) {
)
val publishedContactRequest = documentResult.unwrap()

return ContactRequest(Document(publishedContactRequest, contactRequestDocument.dataContractId!!))
return ContactRequest(
Document(publishedContactRequest, contactRequestDocument.dataContractId!!, contactRequestDocument.type!!)
)
}
throw IllegalArgumentException("No valid keys to use in toUser's identity")
}
Expand Down
4 changes: 2 additions & 2 deletions dpp/src/main/java/org/dashj/platform/dashpay/Profiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Profiles(
signer.nativeContext,
BigInteger.valueOf(signer.signerCallback)
)
return Document(profileResult.unwrap(), profileDocument.dataContractId!!)
return Document(profileResult.unwrap(), profileDocument.dataContractId!!, profileDocument.type!!)
}

fun replace(
Expand Down Expand Up @@ -120,7 +120,7 @@ class Profiles(
signer.nativeContext,
BigInteger.valueOf(signer.signerCallback)
)
return Document(profileResult.unwrap(), profileDocument.dataContractId!!)
return Document(profileResult.unwrap(), profileDocument.dataContractId!!, profileDocument.type!!)
}

fun createProfileDocument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ class Document : BaseObject {
this.data = data
}

constructor(document: RustDocument, dataContractId: Identifier) {
constructor(document: RustDocument, dataContractId: Identifier, type: String) {
this.dataContractId = dataContractId
// The native document does not carry its document-type name, so the caller (which
// queried by type) supplies it. Leaving it null breaks any later replace/put, since
// the native put/replace calls require a non-null type string.
this.type = type
when (document.tag) {
V0 -> {
val doc = document.v0._0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ abstract class ContractBounds(val type: String, val identifier: Identifier)
from(rustContractBounds)
}
}

fun from(rawContractBounds: Map<String, Any?>): ContractBounds {
val identifier = Identifier.from(rawContractBounds["identifier"])
return when (val type = rawContractBounds["type"]) {
"singleContract" -> SingleContractBounds(identifier)
"documentType" -> SingleContractDocumentType(
identifier,
rawContractBounds["documentType"] as String
)
else -> error("Unknown ContractBounds type: $type")
}
}
}

open fun toJSON(): Map<String, Any> {
Expand All @@ -40,12 +52,23 @@ abstract class ContractBounds(val type: String, val identifier: Identifier)
"identifier" to identifier
)
}

/** Convert to the native (Rust SDK) representation. */
abstract fun toNative(): RustContractBounds
}

class SingleContractBounds(identifier: Identifier) : ContractBounds("singleContract", identifier)
class SingleContractBounds(identifier: Identifier) : ContractBounds("singleContract", identifier) {
override fun toNative(): RustContractBounds {
return RustContractBounds(identifier.toNative())
}
}


class SingleContractDocumentType(identifier: Identifier, val documentType: String) : ContractBounds("documentType", identifier) {
override fun toNative(): RustContractBounds {
return RustContractBounds(identifier.toNative(), documentType)
}

override fun toJSON(): Map<String, Any> {
val json = super.toJSON().toMutableMap()
json["documentType"] = documentType
Expand Down
22 changes: 22 additions & 0 deletions dpp/src/main/java/org/dashj/platform/dpp/identity/Identity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ class Identity : BaseObject {
}
}

/**
* Finds the first public key that matches [purpose], [securityLevel] and [type] and whose
* [IdentityPublicKey.contractBounds] is scoped to [contractId] (contract bounds rules).
* Both [SingleContractBounds] and [SingleContractDocumentType] match on their identifier.
* Pass `null` for [contractId] to match only keys with no contract bounds at all.
*/
fun getFirstPublicKey(
purpose: Purpose,
securityLevel: SecurityLevel,
type: KeyType,
contractId: Identifier?
): IdentityPublicKey? {
return try {
publicKeys.first {
it.purpose == purpose && it.securityLevel == securityLevel && it.type == type &&
it.disabledAt == null && it.contractBounds?.identifier == contractId
}
} catch (_: NoSuchElementException) {
null
}
}

fun toNative(): RustIdentity {
val identityV0 = IdentityV0(
id.toNative(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ class IdentityPublicKey(
is Int -> SecurityLevel.swigToEnum(rawIdentityPublicKey["securityLevel"] as Int)
else -> SecurityLevel.CRITICAL
},
when (rawIdentityPublicKey["contractBounds"]) {
is ContractBounds -> rawIdentityPublicKey["contractBounds"] as ContractBounds
when (val contractBounds = rawIdentityPublicKey["contractBounds"]) {
is ContractBounds -> contractBounds
is Map<*, *> -> ContractBounds.from(contractBounds as Map<String, Any?>)
else -> null
},
when (rawIdentityPublicKey["data"]) {
Expand Down Expand Up @@ -191,7 +192,7 @@ class IdentityPublicKey(
objMap.put("disabledAt", this)
}
contractBounds?.run {
objMap.put("contractBounds", this)
objMap.put("contractBounds", this.toObject())
}
if (signature != null && !skipSignature) {
objMap["signature"] = signature!!
Expand All @@ -217,7 +218,7 @@ class IdentityPublicKey(
KeyID(id),
purpose,
securityLevel,
null,
contractBounds?.toNative(),
type,
readOnly,
BinaryData(data),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright (c) 2018-present, Dash Core Team
*
* This source code is licensed under the MIT license found in the
* COPYING file in the root directory of this source tree.
*/

package org.dashj.platform.dpp.statetransition

import org.dashj.platform.dpp.identifier.Identifier
import org.dashj.platform.dpp.identity.IdentityPublicKey
import org.dashj.platform.sdk.dashsdk

/**
* A summary of a state transition, produced by natively deserializing its bincode-serialized
* bytes. Unlike [StateTransitionFactory] (CBOR-only, and lacking IdentityUpdate support) this
* works for any modern transition. The identity-update fields ([revision], [identityNonce],
* [addPublicKeys]) are populated only when [type] is IdentityUpdate (5).
*/
data class StateTransitionInfo(
/** The transition type discriminant (e.g. 5 = IdentityUpdate). */
val type: Int,
/** Human-readable transition name. */
val name: String,
/** The owner/identity id, or null when the transition has no owner. */
val ownerId: Identifier?,
/** Identity-update: the new identity revision (0 otherwise). */
val revision: Long,
/** Identity-update: the identity nonce (0 otherwise). */
val identityNonce: Long,
/** Identity-update: the public keys being added (empty otherwise). */
val addPublicKeys: List<IdentityPublicKey>
)

/** Native (bincode) deserialization of platform state transitions. */
object NativeStateTransition {

/**
* Deserializes a bincode-serialized state transition and returns a [StateTransitionInfo],
* including the public keys added when the transition is an identity update.
*
* @throws Exception if the bytes are not a valid state transition (surfaced from the SDK).
*/
@JvmStatic
fun deserialize(bytes: ByteArray): StateTransitionInfo {
// base.Result.unwrap() returns a Java-owned StateTransitionInfo (or throws on error).
val info = dashsdk.platformMobileStateTransitionDeserializeStateTransition(bytes).unwrap()

val ownerBytes = dashsdk.platformMobileStateTransitionStateTransitionInfoGetOwnerId(info)
val ownerId = if (ownerBytes != null && ownerBytes.isNotEmpty()) Identifier.from(ownerBytes) else null

val addPublicKeys = dashsdk.platformMobileStateTransitionIdentityUpdatePublicKeysToAdd(bytes)
.unwrap()
.map { IdentityPublicKey.from(it) }

return StateTransitionInfo(
type = dashsdk.platformMobileStateTransitionStateTransitionInfoGetTransitionType(info).toInt(),
name = dashsdk.platformMobileStateTransitionStateTransitionInfoGetName(info),
ownerId = ownerId,
revision = dashsdk.platformMobileStateTransitionStateTransitionInfoGetRevision(info).toLong(),
identityNonce = dashsdk.platformMobileStateTransitionStateTransitionInfoGetIdentityNonce(info).toLong(),
addPublicKeys = addPublicKeys
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ open class PlatformStateRepository(val platform: Platform) : StateRepository {
}
}
)
log.info("store identity: {}", identity.toBuffer().toHex())
try {
log.info("store identity: {}", identity.toBuffer().toHex())
} catch (e: Exception) {
log.info("store identity: {} (cannot serialize: {})", identity.id, e.message)
}
}

override fun storeIdentityPublicKeyHashes(identity: Identifier, publicKeyHashes: List<ByteArray>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class IdentityVerify(
)
val domain = documentResult.unwrap()

return IdentityVerifyDocument(Document(domain, identityVerifyDocument.dataContractId!!))
return IdentityVerifyDocument(Document(domain, identityVerifyDocument.dataContractId!!, identityVerifyDocument.type!!))
}

fun createDocument(
Expand Down
4 changes: 2 additions & 2 deletions dpp/src/main/java/org/dashj/platform/wallet/TxMetadata.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TxMetadata(
)
val domain = documentResult.unwrap()

return Document(domain, profileDocument.dataContractId!!)
return Document(domain, profileDocument.dataContractId!!, profileDocument.type!!)
}

fun publish(
Expand All @@ -87,7 +87,7 @@ class TxMetadata(
)
val domain = documentResult.unwrap()

return Document(domain, txMetadataDocument.dataContractId!!)
return Document(domain, txMetadataDocument.dataContractId!!, txMetadataDocument.type!!)
}

fun createDocument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class TxMetaDataTests : PlatformNetwork() {
assertMapEquals(
mapOf(
"txId" to txId,
"timestamp" to txMetadataItem.timestamp,
"memo" to "Alice's Pizza Party",
"exchangeRate" to 51.00,
"currencyCode" to "USD",
Expand All @@ -115,9 +116,19 @@ class TxMetaDataTests : PlatformNetwork() {

@Test
fun toCborTestForOneItemTest() {
// use a fixed timestamp so the CBOR encoding is deterministic
val item = TxMetadataItem(
Converters.fromHex("c44d1077cd4628d0ac06e22032a4e8458f9d01be6342453de3eef88657b193ce"),
1738177719,
"Bob's Burger Joint",
52.23,
"USD",
"expense",
"DashDirect"
)
assertArrayEquals(
Converters.fromHex("A6646D656D6F72426F62277320427572676572204A6F696E7464747849645820C44D1077CD4628D0AC06E22032A4E8458F9D01BE6342453DE3EEF88657B193CE67736572766963656A446173684469726563746B74617843617465676F727967657870656E73656C63757272656E6379436F6465635553446C65786368616E676552617465FB404A1D70A3D70A3D".lowercase()),
Cbor.encode(txMetadataItemTwo.toObject())
Converters.fromHex("A7646D656D6F72426F62277320427572676572204A6F696E7464747849645820C44D1077CD4628D0AC06E22032A4E8458F9D01BE6342453DE3EEF88657B193CE67736572766963656A446173684469726563746974696D657374616D701A679A7CB76B74617843617465676F727967657870656E73656C63757272656E6379436F6465635553446C65786368616E676552617465FB404A1D70A3D70A3D".lowercase()),
Cbor.encode(item.toObject())
)
}

Expand Down
Loading
Loading