Skip to content

Commit

Permalink
add inbox id to the create method
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed May 29, 2024
1 parent f59e581 commit cb316e4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -86,7 +86,7 @@ export async function createRandom(
hasCreateIdentityCallback?: boolean | undefined,
hasEnableIdentityCallback?: boolean | undefined,
enableAlphaMls?: boolean | undefined,
dbEncryptionKey?: Uint8Array | undefined,
dbEncryptionKey?: Uint8Array | undefined
): Promise<string> {
return await XMTPModule.createRandom(
environment,
Expand All @@ -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<string> {
return await XMTPModule.createFromKeyBundle(
keyBundle,
Expand Down
124 changes: 69 additions & 55 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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 || []
)
}

/**
Expand All @@ -173,62 +179,20 @@ export class Client<
opts?: Partial<ClientOptions> & { codecs?: ContentCodecs }
): Promise<Client<ContentCodecs>> {
const options = defaultOptions(opts)
const address = await XMTPModule.createFromKeyBundle(
const json = await XMTPModule.createFromKeyBundle(
keyBundle,
options.env,
options.appVersion,
Boolean(options.enableAlphaMls),
options.dbEncryptionKey,

Check warning on line 187 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `,`
)
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<boolean>} A Promise resolving to true if messaging is allowed, and false otherwise.
*/
async canMessage(peerAddress: string): Promise<boolean> {
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 || []
)
}

/**
Expand Down Expand Up @@ -306,9 +270,11 @@ export class Client<

constructor(
address: string,
inboxId: string,
codecs: XMTPModule.ContentCodec<ContentTypes>[] = []
) {
this.address = address
this.inboxId = inboxId
this.conversations = new Conversations(this)
this.contacts = new Contacts(this)
this.codecRegistry = {}
Expand Down Expand Up @@ -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<boolean>} A Promise resolving to true if messaging is allowed, and false otherwise.
*/
async canMessage(peerAddress: string): Promise<boolean> {
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<Conversation> { ... }
// async exportConversation(topic: string): Promise<string> { ... }
Expand Down

0 comments on commit cb316e4

Please sign in to comment.