Skip to content

Commit

Permalink
Update User Preferences (#145)
Browse files Browse the repository at this point in the history
* dump in the new rust stuff

* rename pppp and transition to new rust
  • Loading branch information
nplasterer authored Dec 6, 2023
1 parent 09a4da1 commit 14d9c60
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions library/src/main/java/org/xmtp/android/library/Contacts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ConsentList(val client: Client) {
val preferences: MutableList<PrivatePreferencesAction> = mutableListOf()

for (envelope in envelopes.envelopesList) {
val payload = uniffi.xmtp_dh.eciesDecryptK256Sha3256(
val payload = uniffi.xmtp_dh.userPreferencesDecrypt(
publicKey.toByteArray().toUByteArray().toList(),
privateKey.toByteArray().toUByteArray().toList(),
envelope.message.toByteArray().toUByteArray().toList()
Expand Down Expand Up @@ -101,7 +101,7 @@ class ConsentList(val client: Client) {
}
}.build()

val message = uniffi.xmtp_dh.eciesEncryptK256Sha3256(
val message = uniffi.xmtp_dh.userPreferencesEncrypt(
publicKey.toByteArray().toUByteArray().toList(),
privateKey.toByteArray().toUByteArray().toList(),
payload.toByteArray().toUByteArray().toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sealed class Topic {
}

is directMessageV2 -> wrap("m-$addresses")
is preferenceList -> wrap("pppp-$identifier")
is preferenceList -> wrap("userpreferences-$identifier")
}
}

Expand Down
60 changes: 30 additions & 30 deletions library/src/main/java/xmtp_dh.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.sun.jna.IntegerType
import com.sun.jna.Native
import com.sun.jna.Pointer
import com.sun.jna.Structure
import com.sun.jna.Callback
import com.sun.jna.ptr.*
import java.nio.ByteBuffer
import java.nio.ByteOrder
Expand Down Expand Up @@ -106,7 +107,6 @@ class RustBufferByReference : ByReference(16) {
open class ForeignBytes : Structure() {
@JvmField
var len: Int = 0

@JvmField
var data: Pointer? = null

Expand Down Expand Up @@ -192,7 +192,6 @@ public interface FfiConverterRustBuffer<KotlinType> : FfiConverter<KotlinType, R
internal open class RustCallStatus : Structure() {
@JvmField
var code: Byte = 0

@JvmField
var error_buf: RustBuffer.ByValue = RustBuffer.ByValue()

Expand Down Expand Up @@ -387,14 +386,14 @@ internal interface _UniFFILib : Library {
_uniffi_out_err: RustCallStatus,
): RustBuffer.ByValue

fun uniffi_xmtp_dh_fn_func_ecies_encrypt_k256_sha3_256(
fun uniffi_xmtp_dh_fn_func_user_preferences_encrypt(
`publicKeyBytes`: RustBuffer.ByValue,
`privateKeyBytes`: RustBuffer.ByValue,
`messageBytes`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus,
): RustBuffer.ByValue

fun uniffi_xmtp_dh_fn_func_ecies_decrypt_k256_sha3_256(
fun uniffi_xmtp_dh_fn_func_user_preferences_decrypt(
`publicKeyBytes`: RustBuffer.ByValue,
`privateKeyBytes`: RustBuffer.ByValue,
`messageBytes`: RustBuffer.ByValue,
Expand Down Expand Up @@ -432,10 +431,10 @@ internal interface _UniFFILib : Library {
fun uniffi_xmtp_dh_checksum_func_diffie_hellman_k256(
): Short

fun uniffi_xmtp_dh_checksum_func_ecies_encrypt_k256_sha3_256(
fun uniffi_xmtp_dh_checksum_func_user_preferences_encrypt(
): Short

fun uniffi_xmtp_dh_checksum_func_ecies_decrypt_k256_sha3_256(
fun uniffi_xmtp_dh_checksum_func_user_preferences_decrypt(
): Short

fun uniffi_xmtp_dh_checksum_func_generate_private_preferences_topic_identifier(
Expand Down Expand Up @@ -464,13 +463,13 @@ private fun uniffiCheckApiChecksums(lib: _UniFFILib) {
if (lib.uniffi_xmtp_dh_checksum_func_diffie_hellman_k256() != 64890.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_xmtp_dh_checksum_func_ecies_encrypt_k256_sha3_256() != 28010.toShort()) {
if (lib.uniffi_xmtp_dh_checksum_func_user_preferences_encrypt() != 59502.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_xmtp_dh_checksum_func_ecies_decrypt_k256_sha3_256() != 45037.toShort()) {
if (lib.uniffi_xmtp_dh_checksum_func_user_preferences_decrypt() != 60388.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_xmtp_dh_checksum_func_generate_private_preferences_topic_identifier() != 65141.toShort()) {
if (lib.uniffi_xmtp_dh_checksum_func_generate_private_preferences_topic_identifier() != 48427.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_xmtp_dh_checksum_func_verify_k256_sha256() != 45969.toShort()) {
Expand Down Expand Up @@ -606,35 +605,36 @@ public object FfiConverterTypeDiffieHellmanError : FfiConverterRustBuffer<Diffie
}


sealed class EciesException(message: String) : Exception(message) {
sealed class UserPreferencesException(message: String) : Exception(message) {
// Each variant is a nested class
// Flat enums carries a string error message, so no special implementation is necessary.
class GenericException(message: String) : EciesException(message)
class GenericException(message: String) : UserPreferencesException(message)


companion object ErrorHandler : CallStatusErrorHandler<EciesException> {
override fun lift(error_buf: RustBuffer.ByValue): EciesException =
FfiConverterTypeEciesError.lift(error_buf)
companion object ErrorHandler : CallStatusErrorHandler<UserPreferencesException> {
override fun lift(error_buf: RustBuffer.ByValue): UserPreferencesException =
FfiConverterTypeUserPreferencesError.lift(error_buf)
}
}

public object FfiConverterTypeEciesError : FfiConverterRustBuffer<EciesException> {
override fun read(buf: ByteBuffer): EciesException {
public object FfiConverterTypeUserPreferencesError :
FfiConverterRustBuffer<UserPreferencesException> {
override fun read(buf: ByteBuffer): UserPreferencesException {

return when (buf.getInt()) {
1 -> EciesException.GenericException(FfiConverterString.read(buf))
1 -> UserPreferencesException.GenericException(FfiConverterString.read(buf))
else -> throw RuntimeException("invalid error enum value, something is very wrong!!")
}

}

override fun allocationSize(value: EciesException): Int {
override fun allocationSize(value: UserPreferencesException): Int {
return 4
}

override fun write(value: EciesException, buf: ByteBuffer) {
override fun write(value: UserPreferencesException, buf: ByteBuffer) {
when (value) {
is EciesException.GenericException -> {
is UserPreferencesException.GenericException -> {
buf.putInt(1)
Unit
}
Expand Down Expand Up @@ -720,16 +720,16 @@ fun `diffieHellmanK256`(
})
}

@Throws(EciesException::class)
@Throws(UserPreferencesException::class)

fun `eciesEncryptK256Sha3256`(
fun `userPreferencesEncrypt`(
`publicKeyBytes`: List<UByte>,
`privateKeyBytes`: List<UByte>,
`messageBytes`: List<UByte>,
): List<UByte> {
return FfiConverterSequenceUByte.lift(
rustCallWithError(EciesException) { _status ->
_UniFFILib.INSTANCE.uniffi_xmtp_dh_fn_func_ecies_encrypt_k256_sha3_256(
rustCallWithError(UserPreferencesException) { _status ->
_UniFFILib.INSTANCE.uniffi_xmtp_dh_fn_func_user_preferences_encrypt(
FfiConverterSequenceUByte.lower(`publicKeyBytes`),
FfiConverterSequenceUByte.lower(`privateKeyBytes`),
FfiConverterSequenceUByte.lower(`messageBytes`),
Expand All @@ -738,16 +738,16 @@ fun `eciesEncryptK256Sha3256`(
})
}

@Throws(EciesException::class)
@Throws(UserPreferencesException::class)

fun `eciesDecryptK256Sha3256`(
fun `userPreferencesDecrypt`(
`publicKeyBytes`: List<UByte>,
`privateKeyBytes`: List<UByte>,
`messageBytes`: List<UByte>,
): List<UByte> {
return FfiConverterSequenceUByte.lift(
rustCallWithError(EciesException) { _status ->
_UniFFILib.INSTANCE.uniffi_xmtp_dh_fn_func_ecies_decrypt_k256_sha3_256(
rustCallWithError(UserPreferencesException) { _status ->
_UniFFILib.INSTANCE.uniffi_xmtp_dh_fn_func_user_preferences_decrypt(
FfiConverterSequenceUByte.lower(`publicKeyBytes`),
FfiConverterSequenceUByte.lower(`privateKeyBytes`),
FfiConverterSequenceUByte.lower(`messageBytes`),
Expand All @@ -756,11 +756,11 @@ fun `eciesDecryptK256Sha3256`(
})
}

@Throws(EciesException::class)
@Throws(UserPreferencesException::class)

fun `generatePrivatePreferencesTopicIdentifier`(`privateKeyBytes`: List<UByte>): String {
return FfiConverterString.lift(
rustCallWithError(EciesException) { _status ->
rustCallWithError(UserPreferencesException) { _status ->
_UniFFILib.INSTANCE.uniffi_xmtp_dh_fn_func_generate_private_preferences_topic_identifier(
FfiConverterSequenceUByte.lower(`privateKeyBytes`),
_status
Expand Down
Binary file modified library/src/main/jniLibs/arm64-v8a/libuniffi_xmtp_dh.so
Binary file not shown.
Binary file modified library/src/main/jniLibs/armeabi-v7a/libuniffi_xmtp_dh.so
Binary file not shown.
Binary file modified library/src/main/jniLibs/x86/libuniffi_xmtp_dh.so
Binary file not shown.
Binary file modified library/src/main/jniLibs/x86_64/libuniffi_xmtp_dh.so
Binary file not shown.

0 comments on commit 14d9c60

Please sign in to comment.