Skip to content

Commit

Permalink
add specifying the db directory to android
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jun 4, 2024
1 parent cdaddc0 commit 07547af
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class XMTPModule : Module() {
//
// Auth functions
//
AsyncFunction("auth") { address: String, environment: String, appVersion: String?, hasCreateIdentityCallback: Boolean?, hasEnableIdentityCallback: Boolean?, enableAlphaMls: Boolean?, dbEncryptionKey: List<Int>? ->
AsyncFunction("auth") { address: String, environment: String, appVersion: String?, hasCreateIdentityCallback: Boolean?, hasEnableIdentityCallback: Boolean?, enableAlphaMls: Boolean?, dbEncryptionKey: List<Int>?, dbDirectory: String? ->
logV("auth")
requireNotProductionEnvForAlphaMLS(enableAlphaMls, environment)
val reactSigner = ReactNativeSigner(module = this@XMTPModule, address = address)
Expand All @@ -236,6 +236,7 @@ class XMTPModule : Module() {
enableAlphaMls = enableAlphaMls == true,
appContext = context,
dbEncryptionKey = encryptionKeyBytes,
dbDirectory = dbDirectory
)
val client = Client().create(account = reactSigner, options = options)
clients[address] = client
Expand All @@ -250,7 +251,7 @@ class XMTPModule : Module() {
}

// Generate a random wallet and set the client to that
AsyncFunction("createRandom") { environment: String, appVersion: String?, hasCreateIdentityCallback: Boolean?, hasEnableIdentityCallback: Boolean?, enableAlphaMls: Boolean?, dbEncryptionKey: List<Int>? ->
AsyncFunction("createRandom") { environment: String, appVersion: String?, hasCreateIdentityCallback: Boolean?, hasEnableIdentityCallback: Boolean?, enableAlphaMls: Boolean?, dbEncryptionKey: List<Int>?, dbDirectory: String? ->
logV("createRandom")
requireNotProductionEnvForAlphaMLS(enableAlphaMls, environment)
val privateKey = PrivateKeyBuilder()
Expand All @@ -276,6 +277,7 @@ class XMTPModule : Module() {
enableAlphaMls = enableAlphaMls == true,
appContext = context,
dbEncryptionKey = encryptionKeyBytes,
dbDirectory = dbDirectory
)
val randomClient = Client().create(account = privateKey, options = options)
ContentJson.Companion
Expand All @@ -286,7 +288,7 @@ class XMTPModule : Module() {
)
}

AsyncFunction("createFromKeyBundle") { keyBundle: String, environment: String, appVersion: String?, enableAlphaMls: Boolean?, dbEncryptionKey: List<Int>? ->
AsyncFunction("createFromKeyBundle") { keyBundle: String, environment: String, appVersion: String?, enableAlphaMls: Boolean?, dbEncryptionKey: List<Int>?, dbDirectory: String? ->
logV("createFromKeyBundle")
requireNotProductionEnvForAlphaMLS(enableAlphaMls, environment)

Expand All @@ -301,6 +303,7 @@ class XMTPModule : Module() {
enableAlphaMls = enableAlphaMls == true,
appContext = context,
dbEncryptionKey = encryptionKeyBytes,
dbDirectory = dbDirectory
)
val bundle =
PrivateKeyOuterClass.PrivateKeyBundle.parseFrom(
Expand Down
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export async function auth(
hasCreateIdentityCallback?: boolean | undefined,
hasEnableIdentityCallback?: boolean | undefined,
enableAlphaMls?: boolean | undefined,
dbEncryptionKey?: Uint8Array | undefined
dbEncryptionKey?: Uint8Array | undefined,
dbDirectory?: string | undefined
) {
return await XMTPModule.auth(
address,
Expand All @@ -73,7 +74,8 @@ export async function auth(
hasCreateIdentityCallback,
hasEnableIdentityCallback,
enableAlphaMls,
dbEncryptionKey ? Array.from(dbEncryptionKey) : undefined
dbEncryptionKey ? Array.from(dbEncryptionKey) : undefined,
dbDirectory
)
}

Expand All @@ -87,15 +89,17 @@ export async function createRandom(
hasCreateIdentityCallback?: boolean | undefined,
hasEnableIdentityCallback?: boolean | undefined,
enableAlphaMls?: boolean | undefined,
dbEncryptionKey?: Uint8Array | undefined
dbEncryptionKey?: Uint8Array | undefined,
dbDirectory?: string | undefined
): Promise<string> {
return await XMTPModule.createRandom(
environment,
appVersion,
hasCreateIdentityCallback,
hasEnableIdentityCallback,
enableAlphaMls,
dbEncryptionKey ? Array.from(dbEncryptionKey) : undefined
dbEncryptionKey ? Array.from(dbEncryptionKey) : undefined,
dbDirectory
)
}

Expand All @@ -104,14 +108,16 @@ export async function createFromKeyBundle(
environment: 'local' | 'dev' | 'production',
appVersion?: string | undefined,
enableAlphaMls?: boolean | undefined,
dbEncryptionKey?: Uint8Array | undefined
dbEncryptionKey?: Uint8Array | undefined,
dbDirectory?: string | undefined
): Promise<string> {
return await XMTPModule.createFromKeyBundle(
keyBundle,
environment,
appVersion,
enableAlphaMls,
dbEncryptionKey ? Array.from(dbEncryptionKey) : undefined
dbEncryptionKey ? Array.from(dbEncryptionKey) : undefined,
dbDirectory
)
}

Expand Down
12 changes: 10 additions & 2 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export class Client<
Boolean(createSubscription),
Boolean(enableSubscription),
Boolean(options.enableAlphaMls),
options.dbEncryptionKey
options.dbEncryptionKey,
options.dbDirectory
)
})().catch((error) => {
console.error('ERROR in create: ', error)
Expand Down Expand Up @@ -148,7 +149,8 @@ export class Client<
Boolean(createSubscription),
Boolean(enableSubscription),
Boolean(options.enableAlphaMls),
options.dbEncryptionKey
options.dbEncryptionKey,
options.dbDirectory
)
this.removeSubscription(enableSubscription)
this.removeSubscription(createSubscription)
Expand Down Expand Up @@ -183,6 +185,7 @@ export class Client<
options.appVersion,
Boolean(options.enableAlphaMls),
options.dbEncryptionKey
options.dbDirectory

Check failure on line 188 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / test

',' expected.

Check failure on line 188 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / lint

',' expected.

Check failure on line 188 in src/lib/Client.ts

View workflow job for this annotation

GitHub Actions / lint

',' expected.
)

return new Client(
Expand Down Expand Up @@ -468,6 +471,10 @@ export type ClientOptions = {
* OPTIONAL specify the encryption key for the database. The encryption key must be exactly 32 bytes.
*/
dbEncryptionKey?: Uint8Array
/**
* OPTIONAL specify the XMTP managed database directory
*/
dbDirectory?: string
}

export type KeyType = {
Expand All @@ -485,6 +492,7 @@ export function defaultOptions(opts?: Partial<ClientOptions>): ClientOptions {
env: 'dev',
enableAlphaMls: false,
dbEncryptionKey: undefined,
dbDirectory: undefined,
}

return { ..._defaultOptions, ...opts } as ClientOptions
Expand Down

0 comments on commit 07547af

Please sign in to comment.