From cb316e407710846f92ec750a434646d2cbedb4cf Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Wed, 29 May 2024 13:22:49 -0700 Subject: [PATCH] add inbox id to the create method --- .../modules/xmtpreactnativesdk/XMTPModule.kt | 5 +- src/index.ts | 6 +- src/lib/Client.ts | 124 ++++++++++-------- 3 files changed, 75 insertions(+), 60 deletions(-) diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt index d1134dc52..9a4ce2095 100644 --- a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt @@ -236,10 +236,11 @@ class XMTPModule : Module() { appContext = context, dbEncryptionKey = encryptionKeyBytes, ) - clients[address] = Client().create(account = reactSigner, options = options) + val client = Client().create(account = reactSigner, options = options) + clients[address] = client ContentJson.Companion signer = null - sendEvent("authed") + sendEvent("authed", mapOf("inboxId" to client.inboxId)) } Function("receiveSignature") { requestID: String, signature: String -> diff --git a/src/index.ts b/src/index.ts index 07c2ea48f..c1a8cab07 100644 --- a/src/index.ts +++ b/src/index.ts @@ -63,7 +63,7 @@ export async function auth( hasCreateIdentityCallback?: boolean | undefined, hasEnableIdentityCallback?: boolean | undefined, enableAlphaMls?: boolean | undefined, - dbEncryptionKey?: Uint8Array | undefined, + dbEncryptionKey?: Uint8Array | undefined ) { return await XMTPModule.auth( address, @@ -86,7 +86,7 @@ export async function createRandom( hasCreateIdentityCallback?: boolean | undefined, hasEnableIdentityCallback?: boolean | undefined, enableAlphaMls?: boolean | undefined, - dbEncryptionKey?: Uint8Array | undefined, + dbEncryptionKey?: Uint8Array | undefined ): Promise { return await XMTPModule.createRandom( environment, @@ -103,7 +103,7 @@ export async function createFromKeyBundle( environment: 'local' | 'dev' | 'production', appVersion?: string | undefined, enableAlphaMls?: boolean | undefined, - dbEncryptionKey?: Uint8Array | undefined, + dbEncryptionKey?: Uint8Array | undefined ): Promise { return await XMTPModule.createFromKeyBundle( keyBundle, diff --git a/src/lib/Client.ts b/src/lib/Client.ts index 751881cef..c9da806bc 100644 --- a/src/lib/Client.ts +++ b/src/lib/Client.ts @@ -92,13 +92,13 @@ export class Client< this.authSubscription = XMTPModule.emitter.addListener( 'authed', - async () => { + async (message: { inboxId: string }) => { this.removeSubscription(enableSubscription) this.removeSubscription(createSubscription) this.removeSignSubscription() this.removeAuthSubscription() const address = await signer.getAddress() - resolve(new Client(address, opts?.codecs || [])) + resolve(new Client(address, message.inboxId, opts?.codecs || [])) } ) await XMTPModule.auth( @@ -108,7 +108,7 @@ export class Client< Boolean(createSubscription), Boolean(enableSubscription), Boolean(options.enableAlphaMls), - options.dbEncryptionKey, + options.dbEncryptionKey ) })().catch((error) => { console.error('ERROR in create: ', error) @@ -142,18 +142,24 @@ export class Client< const options = defaultOptions(opts) const { enableSubscription, createSubscription } = this.setupSubscriptions(options) - const address = await XMTPModule.createRandom( + const json = await XMTPModule.createRandom( options.env, options.appVersion, Boolean(createSubscription), Boolean(enableSubscription), Boolean(options.enableAlphaMls), - options.dbEncryptionKey, + options.dbEncryptionKey ) + + const addressInboxId = JSON.parse(json) this.removeSubscription(enableSubscription) this.removeSubscription(createSubscription) - return new Client(address, opts?.codecs || []) + return new Client( + addressInboxId.address, + addressInboxId.inboxId, + opts?.codecs || [] + ) } /** @@ -173,62 +179,20 @@ export class Client< opts?: Partial & { codecs?: ContentCodecs } ): Promise> { const options = defaultOptions(opts) - const address = await XMTPModule.createFromKeyBundle( + const json = await XMTPModule.createFromKeyBundle( keyBundle, options.env, options.appVersion, Boolean(options.enableAlphaMls), options.dbEncryptionKey, ) - return new Client(address, opts?.codecs || []) - } - - /** - * Determines whether the current user can send messages to a specified peer over 1:1 conversations. - * - * This method checks if the specified peer has signed up for XMTP - * and ensures that the message is not addressed to the sender (no self-messaging). - * - * @param {string} peerAddress - The address of the peer to check for messaging eligibility. - * @returns {Promise} A Promise resolving to true if messaging is allowed, and false otherwise. - */ - async canMessage(peerAddress: string): Promise { - return await XMTPModule.canMessage(this.address, peerAddress) - } - - /** - * Deletes the local database. This cannot be undone and these stored messages will not be refetched from the network. - */ - async deleteLocalDatabase() { - return await XMTPModule.deleteLocalDatabase(this.address) - } + const addressInboxId = JSON.parse(json) - /** - * Drop the local database connection. This function is delicate and should be used with caution. App will error if database not properly reconnected. See: reconnectLocalDatabase() - */ - dropLocalDatabaseConnection() { - return XMTPModule.dropLocalDatabaseConnection() - } - - /** - * Reconnects the local database after being dropped. - */ - async reconnectLocalDatabase() { - return await XMTPModule.reconnectLocalDatabase() - } - - /** - * Determines whether the current user can send messages to the specified peers over groups. - * - * This method checks if the specified peers are using clients that support group messaging. - * - * @param {string[]} addresses - The addresses of the peers to check for messaging eligibility. - * @returns {Promise<{ [key: string]: boolean }>} A Promise resolving to a hash of addresses and booleans if they can message on the V3 network. - */ - async canGroupMessage( - addresses: string[] - ): Promise<{ [key: string]: boolean }> { - return await XMTPModule.canGroupMessage(this.address, addresses) + return new Client( + addressInboxId.address, + addressInboxId.inboxId, + opts?.codecs || [] + ) } /** @@ -306,9 +270,11 @@ export class Client< constructor( address: string, + inboxId: string, codecs: XMTPModule.ContentCodec[] = [] ) { this.address = address + this.inboxId = inboxId this.conversations = new Conversations(this) this.contacts = new Contacts(this) this.codecRegistry = {} @@ -350,6 +316,54 @@ export class Client< return XMTPModule.exportKeyBundle(this.address) } + /** + * Determines whether the current user can send messages to a specified peer over 1:1 conversations. + * + * This method checks if the specified peer has signed up for XMTP + * and ensures that the message is not addressed to the sender (no self-messaging). + * + * @param {string} peerAddress - The address of the peer to check for messaging eligibility. + * @returns {Promise} A Promise resolving to true if messaging is allowed, and false otherwise. + */ + async canMessage(peerAddress: string): Promise { + return await XMTPModule.canMessage(this.address, peerAddress) + } + + /** + * Deletes the local database. This cannot be undone and these stored messages will not be refetched from the network. + */ + async deleteLocalDatabase() { + return await XMTPModule.deleteLocalDatabase(this.address) + } + + /** + * Drop the local database connection. This function is delicate and should be used with caution. App will error if database not properly reconnected. See: reconnectLocalDatabase() + */ + dropLocalDatabaseConnection() { + return XMTPModule.dropLocalDatabaseConnection(this.address) + } + + /** + * Reconnects the local database after being dropped. + */ + async reconnectLocalDatabase() { + return await XMTPModule.reconnectLocalDatabase(this.address) + } + + /** + * Determines whether the current user can send messages to the specified peers over groups. + * + * This method checks if the specified peers are using clients that support group messaging. + * + * @param {string[]} addresses - The addresses of the peers to check for messaging eligibility. + * @returns {Promise<{ [key: string]: boolean }>} A Promise resolving to a hash of addresses and booleans if they can message on the V3 network. + */ + async canGroupMessage( + addresses: string[] + ): Promise<{ [key: string]: boolean }> { + return await XMTPModule.canGroupMessage(this.address, addresses) + } + // TODO: support persisting conversations for quick lookup // async importConversation(exported: string): Promise { ... } // async exportConversation(topic: string): Promise { ... }