Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix create from key bundle #251

Merged
merged 8 commits into from
Jun 4, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ClientTest {

val bundle = client.privateKeyBundle
val clientFromV1Bundle =
Client().buildFromBundle(bundle, account = fakeWallet, options = options)
Client().buildFromBundle(bundle, options = options)
assertEquals(client.address, clientFromV1Bundle.address)
assertEquals(
client.privateKeyBundleV1.identityKey,
Expand Down
4 changes: 2 additions & 2 deletions library/src/main/java/libxmtp-version.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 2137e4f1
Version: 46fad302
Branch: main
Date: 2024-05-30 18:16:18 +0000
Date: 2024-06-04 02:30:29 +0000
20 changes: 10 additions & 10 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import org.xmtp.proto.message.api.v1.MessageApiOuterClass
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.BatchQueryResponse
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.QueryRequest
import uniffi.xmtpv3.FfiXmtpClient
import uniffi.xmtpv3.LegacyIdentitySource
import uniffi.xmtpv3.createClient
import uniffi.xmtpv3.generateInboxId
import uniffi.xmtpv3.getInboxIdForAddress
Expand Down Expand Up @@ -216,7 +215,7 @@ class Client() {
val clientOptions = options ?: ClientOptions()
return runBlocking {
try {
val (privateKeyBundleV1, legacyIdentityKey) = loadOrCreateKeys(
val privateKeyBundleV1 = loadOrCreateKeys(
account,
apiClient,
clientOptions
Expand All @@ -228,7 +227,6 @@ class Client() {
account,
clientOptions.appContext,
privateKeyBundleV1,
legacyIdentityKey,
account.address
)

Expand Down Expand Up @@ -276,7 +274,6 @@ class Client() {
account,
options?.appContext,
v1Bundle,
LegacyIdentitySource.STATIC,
address
)
}
Expand All @@ -302,10 +299,10 @@ class Client() {
account: SigningKey?,
appContext: Context?,
privateKeyBundleV1: PrivateKeyBundleV1,
legacyIdentitySource: LegacyIdentitySource,
accountAddress: String,
address: String,
): Pair<FfiXmtpClient?, String> {
var dbPath = ""
val accountAddress = address.lowercase()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neekolas I still think there is probably an improvement on the libxmtp side for normalizing addresses maybe in the getInboxIdForAddress

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure

val v3Client: FfiXmtpClient? =
if (isAlphaMlsEnabled(options)) {
var inboxId = getInboxIdForAddress(
Expand All @@ -314,9 +311,11 @@ class Client() {
isSecure = options.api.isSecure,
accountAddress = accountAddress
)

if (inboxId.isNullOrBlank()) {
inboxId = generateInboxId(accountAddress, 0.toULong())
}

val alias = "xmtp-${options.api.env}-$inboxId"

val dbDir = if (options.dbDirectory == null) {
Expand Down Expand Up @@ -367,7 +366,8 @@ class Client() {
db = dbPath,
encryptionKey = encryptionKey,
accountAddress = accountAddress,
legacyIdentitySource = legacyIdentitySource,
inboxId = inboxId,
nonce = 0.toULong(),
legacySignedPrivateKeyProto = privateKeyBundleV1.toV2().identityKey.toByteArray()
)
} else {
Expand Down Expand Up @@ -408,16 +408,16 @@ class Client() {
account: SigningKey,
apiClient: ApiClient,
options: ClientOptions? = null,
): Pair<PrivateKeyBundleV1, LegacyIdentitySource> {
): PrivateKeyBundleV1 {
val keys = loadPrivateKeys(account, apiClient, options)
return if (keys != null) {
Pair(keys, LegacyIdentitySource.NETWORK)
keys
} else {
val v1Keys = PrivateKeyBundleV1.newBuilder().build().generate(account, options)
val keyBundle = PrivateKeyBundleBuilder.buildFromV1Key(v1Keys)
val encryptedKeys = keyBundle.encrypted(account, options?.preEnableIdentityCallback)
authSave(apiClient, keyBundle.v1, encryptedKeys)
Pair(v1Keys, LegacyIdentitySource.KEY_GENERATOR)
v1Keys
}
}

Expand Down
109 changes: 71 additions & 38 deletions library/src/main/java/xmtpv3.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,10 @@ internal interface UniffiLib : Library {
`chainRpcUrl`: RustBuffer.ByValue,
): Long

fun uniffi_xmtpv3_fn_method_ffisignaturerequest_is_ready(
`ptr`: Pointer,
): Long

fun uniffi_xmtpv3_fn_method_ffisignaturerequest_missing_address_signatures(
`ptr`: Pointer,
): Long
Expand Down Expand Up @@ -1157,8 +1161,9 @@ internal interface UniffiLib : Library {
`isSecure`: Byte,
`db`: RustBuffer.ByValue,
`encryptionKey`: RustBuffer.ByValue,
`inboxId`: RustBuffer.ByValue,
`accountAddress`: RustBuffer.ByValue,
`legacyIdentitySource`: RustBuffer.ByValue,
`nonce`: Long,
`legacySignedPrivateKeyProto`: RustBuffer.ByValue,
): Long

Expand Down Expand Up @@ -1626,6 +1631,9 @@ internal interface UniffiLib : Library {
fun uniffi_xmtpv3_checksum_method_ffisignaturerequest_add_erc1271_signature(
): Short

fun uniffi_xmtpv3_checksum_method_ffisignaturerequest_is_ready(
): Short

fun uniffi_xmtpv3_checksum_method_ffisignaturerequest_missing_address_signatures(
): Short

Expand Down Expand Up @@ -1718,7 +1726,7 @@ private fun uniffiCheckContractApiVersion(lib: UniffiLib) {

@Suppress("UNUSED_PARAMETER")
private fun uniffiCheckApiChecksums(lib: UniffiLib) {
if (lib.uniffi_xmtpv3_checksum_func_create_client() != 30339.toShort()) {
if (lib.uniffi_xmtpv3_checksum_func_create_client() != 51078.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_xmtpv3_checksum_func_create_v2_client() != 48060.toShort()) {
Expand Down Expand Up @@ -1877,6 +1885,9 @@ private fun uniffiCheckApiChecksums(lib: UniffiLib) {
if (lib.uniffi_xmtpv3_checksum_method_ffisignaturerequest_add_erc1271_signature() != 27040.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_xmtpv3_checksum_method_ffisignaturerequest_is_ready() != 65051.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_xmtpv3_checksum_method_ffisignaturerequest_missing_address_signatures() != 34688.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
Expand Down Expand Up @@ -4149,6 +4160,8 @@ public interface FfiSignatureRequestInterface {
`chainRpcUrl`: kotlin.String,
)

suspend fun `isReady`(): kotlin.Boolean

/**
* missing signatures that are from [MemberKind::Address]
*/
Expand Down Expand Up @@ -4313,6 +4326,37 @@ open class FfiSignatureRequest : Disposable, AutoCloseable, FfiSignatureRequestI
}


@Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE")
override suspend fun `isReady`(): kotlin.Boolean {
return uniffiRustCallAsync(
callWithPointer { thisPtr ->
UniffiLib.INSTANCE.uniffi_xmtpv3_fn_method_ffisignaturerequest_is_ready(
thisPtr,

)
},
{ future, callback, continuation ->
UniffiLib.INSTANCE.ffi_xmtpv3_rust_future_poll_i8(
future,
callback,
continuation
)
},
{ future, continuation ->
UniffiLib.INSTANCE.ffi_xmtpv3_rust_future_complete_i8(
future,
continuation
)
},
{ future -> UniffiLib.INSTANCE.ffi_xmtpv3_rust_future_free_i8(future) },
// lift function
{ FfiConverterBoolean.lift(it) },
// Error FFI converter
UniffiNullRustCallStatusErrorHandler,
)
}


/**
* missing signatures that are from [MemberKind::Address]
*/
Expand Down Expand Up @@ -6372,40 +6416,6 @@ public object FfiConverterTypeGroupPermissions : FfiConverterRustBuffer<GroupPer
}


/**
* XMTP SDK's may embed libxmtp (v3) alongside existing v2 protocol logic
* for backwards-compatibility purposes. In this case, the client may already
* have a wallet-signed v2 key. Depending on the source of this key,
* libxmtp may choose to bootstrap v3 installation keys using the existing
* legacy key.
*/

enum class LegacyIdentitySource {

NONE,
STATIC,
NETWORK,
KEY_GENERATOR;

companion object
}


public object FfiConverterTypeLegacyIdentitySource : FfiConverterRustBuffer<LegacyIdentitySource> {
override fun read(buf: ByteBuffer) = try {
LegacyIdentitySource.values()[buf.getInt() - 1]
} catch (e: IndexOutOfBoundsException) {
throw RuntimeException("invalid enum value, something is very wrong!!", e)
}

override fun allocationSize(value: LegacyIdentitySource) = 4UL

override fun write(value: LegacyIdentitySource, buf: ByteBuffer) {
buf.putInt(value.ordinal + 1)
}
}


sealed class SigningException(message: String) : Exception(message) {

class Generic(message: String) : SigningException(message)
Expand Down Expand Up @@ -7149,6 +7159,27 @@ public object FfiConverterMapStringBoolean :
}


/**
* It returns a new client of the specified `inbox_id`.
* Note that the `inbox_id` must be either brand new or already associated with the `account_address`.
* i.e. `inbox_id` cannot be associated with another account address.
*
* Prior to calling this function, it's suggested to form `inbox_id`, `account_address`, and `nonce` like below.
*
* ```text
* inbox_id = get_inbox_id_for_address(account_address)
* nonce = 0
*
* // if inbox_id is not associated, we will create new one.
* if !inbox_id {
* if !legacy_key { nonce = random_u64() }
* inbox_id = generate_inbox_id(account_address, nonce)
* } // Otherwise, we will just use the inbox and ignore the nonce.
* db_path = $inbox_id-$env
*
* xmtp.create_client(account_address, nonce, inbox_id, Option<legacy_signed_private_key_proto>)
* ```
*/
@Throws(GenericException::class)
@Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE")
suspend fun `createClient`(
Expand All @@ -7157,8 +7188,9 @@ suspend fun `createClient`(
`isSecure`: kotlin.Boolean,
`db`: kotlin.String?,
`encryptionKey`: kotlin.ByteArray?,
`inboxId`: kotlin.String,
`accountAddress`: kotlin.String,
`legacyIdentitySource`: LegacyIdentitySource,
`nonce`: kotlin.ULong,
`legacySignedPrivateKeyProto`: kotlin.ByteArray?,
): FfiXmtpClient {
return uniffiRustCallAsync(
Expand All @@ -7170,8 +7202,9 @@ suspend fun `createClient`(
FfiConverterBoolean.lower(`isSecure`),
FfiConverterOptionalString.lower(`db`),
FfiConverterOptionalByteArray.lower(`encryptionKey`),
FfiConverterString.lower(`inboxId`),
FfiConverterString.lower(`accountAddress`),
FfiConverterTypeLegacyIdentitySource.lower(`legacyIdentitySource`),
FfiConverterULong.lower(`nonce`),
FfiConverterOptionalByteArray.lower(`legacySignedPrivateKeyProto`),
),
{ future, callback, continuation ->
Expand Down
Binary file modified library/src/main/jniLibs/arm64-v8a/libuniffi_xmtpv3.so
Binary file not shown.
Binary file modified library/src/main/jniLibs/armeabi-v7a/libuniffi_xmtpv3.so
Binary file not shown.
Binary file modified library/src/main/jniLibs/x86/libuniffi_xmtpv3.so
Binary file not shown.
Binary file modified library/src/main/jniLibs/x86_64/libuniffi_xmtpv3.so
Binary file not shown.
Loading