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
2 changes: 1 addition & 1 deletion dash-sdk-android/src/main/rust/src/fermented.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dash-sdk-bindings/src/fermented.rs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dash-sdk-java/src/main/swig/key_wallet.i
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
%rename(applicationSessionAuthenticationPath) platform_mobile_key_wallet_application_session_authentication_path(bool is_testnet, Arr_u8_32 *identity_id, Arr_u8_32 *request_id);
%rename(applicationSessionAuthenticationPrivateKeyFromSeed) platform_mobile_key_wallet_application_session_authentication_private_key_from_seed(bool is_testnet, Vec_u8 *seed, Arr_u8_32 *identity_id, Arr_u8_32 *request_id);
%rename(applicationEncryptionPrivateKeyFromSeed) platform_mobile_key_wallet_application_encryption_private_key_from_seed;
%rename(applicationEncryptionLegacyPrivateKeyFromSeed) platform_mobile_key_wallet_application_encryption_legacy_private_key_from_seed;
%newobject platform_mobile_key_wallet_application_session_authentication_private_key_from_seed;
%newobject platform_mobile_key_wallet_application_encryption_private_key_from_seed;
%newobject platform_mobile_key_wallet_application_encryption_legacy_private_key_from_seed;

%extend Result_ok_Vec_u8_err_String {
~Result_ok_Vec_u8_err_String() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,45 @@ import org.dashj.platform.sdk.dashsdk

/** DIP-13 application keys. Testnet and all devnets use the non-mainnet coin type. */
object ApplicationKeyDerivation {
private const val ROTATION_LIMIT = 1L shl 31

/** The caller supplies the request leaf (v2: hash256(appEphPub || bindingIndex)). */
fun sessionAuthentication(
seed: ByteArray, identityId: ByteArray, requestLeaf: ByteArray, isMainnet: Boolean
): ByteArray = privateKey(dashsdk.applicationSessionAuthenticationPrivateKeyFromSeed(
!isMainnet, seed, identityId, requestLeaf
))

/** Rotation-zero key for ENCRYPTION or DECRYPTION, bound to the app contract. */
/**
* Key for ENCRYPTION or DECRYPTION, bound to the app contract, at
* `m/9'/coin'/5'/7'/0'/identityId'/contractId'/purpose'/rotation'` (DIP-13).
* New keys use `rotation = 0`; `rotation` is a hardened 31-bit index (0 until 2^31).
*/
fun encryption(
seed: ByteArray, identityId: ByteArray, contractId: ByteArray, purpose: Purpose, isMainnet: Boolean
seed: ByteArray,
identityId: ByteArray,
contractId: ByteArray,
purpose: Purpose,
rotation: Long,
isMainnet: Boolean
): ByteArray {
require(purpose == Purpose.ENCRYPTION || purpose == Purpose.DECRYPTION)
require(rotation in 0 until ROTATION_LIMIT) { "rotation must be below 2^31" }
return privateKey(dashsdk.applicationEncryptionPrivateKeyFromSeed(
!isMainnet, seed, identityId, contractId, purpose.swigValue().toLong(), rotation
))
}

/**
* Key at the pre-rotation path `m/9'/coin'/5'/7'/0'/identityId'/contractId'/purpose'`.
* Only for recognising keys that wallets registered on testnet/devnet before DIP-13 added
* the rotation level. Never use it to derive new keys: use [encryption] instead.
*/
fun legacyEncryptionWithoutRotation(
seed: ByteArray, identityId: ByteArray, contractId: ByteArray, purpose: Purpose, isMainnet: Boolean
): ByteArray {
require(purpose == Purpose.ENCRYPTION || purpose == Purpose.DECRYPTION)
return privateKey(dashsdk.applicationEncryptionLegacyPrivateKeyFromSeed(
!isMainnet, seed, identityId, contractId, purpose.swigValue().toLong()
))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,60 @@ class AppConnectKeyTest {
val originalSeed = seed.copyOf()
val identityId = ByteArray(32) { 1 }
val contractId = ByteArray(32) { 2 }
val encryption = ApplicationKeyDerivation.encryption(
seed, identityId, contractId, Purpose.ENCRYPTION, false
)
fun derive(purpose: Purpose = Purpose.ENCRYPTION, rotation: Long = 0, isMainnet: Boolean = false) =
ApplicationKeyDerivation.encryption(seed, identityId, contractId, purpose, rotation, isMainnet)
val encryption = derive()
assertEquals(32, encryption.size)
assertArrayEquals(encryption, ApplicationKeyDerivation.encryption(
seed, identityId, contractId, Purpose.ENCRYPTION, false
))
assertFalse(encryption.contentEquals(ApplicationKeyDerivation.encryption(
seed, identityId, contractId, Purpose.DECRYPTION, false
)))
assertFalse(encryption.contentEquals(ApplicationKeyDerivation.encryption(
seed, identityId, contractId, Purpose.ENCRYPTION, true
)))
assertArrayEquals(encryption, derive())
assertFalse(encryption.contentEquals(derive(Purpose.DECRYPTION)))
assertFalse(encryption.contentEquals(derive(isMainnet = true)))
assertFalse(encryption.contentEquals(derive(rotation = 1)))
assertFalse(encryption.contentEquals(ApplicationKeyDerivation.sessionAuthentication(
seed, identityId, contractId, false
)))
assertArrayEquals(originalSeed, seed)
}

@Test
fun legacyEncryptionKeyIsTheRotationlessPathAndNeverEqualsARotatedKey() {
val seed = ByteArray(32) { 42 }
val originalSeed = seed.copyOf()
val identityId = ByteArray(32) { 1 }
val contractId = ByteArray(32) { 2 }
val legacy = ApplicationKeyDerivation.legacyEncryptionWithoutRotation(
seed, identityId, contractId, Purpose.ENCRYPTION, false
)
assertEquals(32, legacy.size)
assertArrayEquals(legacy, ApplicationKeyDerivation.legacyEncryptionWithoutRotation(
seed, identityId, contractId, Purpose.ENCRYPTION, false
))
for (rotation in listOf(0L, 1L, (1L shl 31) - 1)) {
assertFalse(legacy.contentEquals(ApplicationKeyDerivation.encryption(
seed, identityId, contractId, Purpose.ENCRYPTION, rotation, false
)))
}
assertThrows(IllegalArgumentException::class.java) {
ApplicationKeyDerivation.legacyEncryptionWithoutRotation(
seed, identityId, contractId, Purpose.AUTHENTICATION, false
)
}
assertArrayEquals(originalSeed, seed)
}

@Test
fun encryptionRotationMustFitIn31Bits() {
val seed = ByteArray(32) { 42 }
val identityId = ByteArray(32) { 1 }
val contractId = ByteArray(32) { 2 }
for (rotation in listOf(-1L, 1L shl 31, 1L shl 32)) {
assertThrows(IllegalArgumentException::class.java) {
ApplicationKeyDerivation.encryption(
seed, identityId, contractId, Purpose.ENCRYPTION, rotation, false
)
}
}
assertEquals(32, ApplicationKeyDerivation.encryption(
seed, identityId, contractId, Purpose.ENCRYPTION, (1L shl 31) - 1, false
).size)
}
}
Loading