Skip to content

Commit f6a9c30

Browse files
authored
Granular Control Over Creation Flow (#386)
* add granular functions * exposes methods to create client manually * fix up the lin t * make client creation and build static methods * bump lib
1 parent 995f6e5 commit f6a9c30

File tree

17 files changed

+286
-194
lines changed

17 files changed

+286
-194
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ val clientOptions = ClientOptions(
5454

5555
// Create the client with your wallet. This will connect to the XMTP `dev` network by default.
5656
// The account is anything that conforms to the `XMTP.SigningKey` protocol.
57-
val client = Client().create(account = account, options = clientOptions)
57+
val client = Client.create(account = account, options = clientOptions)
5858

5959
// Start a dm conversation
6060
val conversation = client.conversations.newConversation("0x3F11b27F323b62B159D2642964fa27C46C841897")
@@ -75,15 +75,15 @@ conversation.streamMessages().collect {
7575

7676
## Create a client
7777

78-
A client is created with `Client().create(account: SigningKey, options: ClientOptions): Client` that requires passing in an object capable of creating signatures on your behalf. The client will request a signature for any new installation.
78+
A client is created with `Client.create(account: SigningKey, options: ClientOptions): Client` that requires passing in an object capable of creating signatures on your behalf. The client will request a signature for any new installation.
7979

8080
> **Note**
8181
> The client connects to the XMTP `dev` environment by default. [Use `ClientOptions`](#configure-the-client) to change this and other parameters of the network connection.
8282
8383
```kotlin
8484
// Create the client with a `SigningKey` from your app
8585
val options = ClientOptions(api = ClientOptions.Api(env = XMTPEnvironment.PRODUCTION, isSecure = true), dbEncryptionKey = encryptionKey, appContext = context)
86-
val client = Client().create(account = account, options = options)
86+
val client = Client.create(account = account, options = options)
8787
```
8888

8989
### Create a client from saved encryptionKey
@@ -93,7 +93,7 @@ You can save your encryptionKey for the local database and build the client via
9393
```kotlin
9494
// Create the client with a `SigningKey` from your app
9595
val options = ClientOptions(api = ClientOptions.Api(env = XMTPEnvironment.PRODUCTION, isSecure = true), dbEncryptionKey = encryptionKey, appContext = context)
96-
val client = Client().build(address = account.address, options = options)
96+
val client = Client.build(address = account.address, options = options)
9797
```
9898

9999
### Configure the client

example/src/main/java/org/xmtp/android/example/ClientManager.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ object ClientManager {
5151
GlobalScope.launch(Dispatchers.IO) {
5252
try {
5353
_client =
54-
Client().build(address, clientOptions(appContext, address))
54+
Client.build(address, clientOptions(appContext, address))
5555
Client.register(codec = GroupUpdatedCodec())
5656
_clientState.value = ClientState.Ready
5757
} catch (e: Exception) {

example/src/main/java/org/xmtp/android/example/connect/ConnectWalletViewModel.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class ConnectWalletViewModel(application: Application) : AndroidViewModel(applic
8585
_uiState.value = ConnectUiState.Loading
8686
try {
8787
val wallet = PrivateKeyBuilder()
88-
val client = Client().create(wallet, ClientManager.clientOptions(getApplication(), wallet.address))
88+
val client = Client.create(wallet, ClientManager.clientOptions(getApplication(), wallet.address))
8989
Client.register(codec = GroupUpdatedCodec())
9090
_uiState.value = ConnectUiState.Success(
9191
wallet.address
@@ -109,7 +109,7 @@ class ConnectWalletViewModel(application: Application) : AndroidViewModel(applic
109109
it.copy(showWallet = true, uri = uri)
110110
}
111111
}
112-
val client = Client().create(wallet, ClientManager.clientOptions(getApplication(), wallet.address))
112+
val client = Client.create(wallet, ClientManager.clientOptions(getApplication(), wallet.address))
113113
Client.register(codec = GroupUpdatedCodec())
114114
_uiState.value = ConnectUiState.Success(
115115
wallet.address

library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt

+52-23
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.junit.Assert.fail
99
import org.junit.Test
1010
import org.junit.runner.RunWith
1111
import org.xmtp.android.library.messages.PrivateKeyBuilder
12+
import org.xmtp.android.library.messages.rawData
1213
import org.xmtp.android.library.messages.walletAddress
1314
import uniffi.xmtpv3.GenericException
1415
import java.io.File
@@ -29,15 +30,15 @@ class ClientTest {
2930
dbEncryptionKey = key
3031
)
3132
val client = runBlocking {
32-
Client().create(account = fakeWallet, options = options)
33+
Client.create(account = fakeWallet, options = options)
3334
}
3435

3536
runBlocking {
3637
client.canMessage(listOf(client.address))[client.address]?.let { assert(it) }
3738
}
3839

3940
val fromBundle = runBlocking {
40-
Client().build(fakeWallet.address, options = options)
41+
Client.build(fakeWallet.address, options = options)
4142
}
4243
assertEquals(client.address, fromBundle.address)
4344
assertEquals(client.inboxId, fromBundle.inboxId)
@@ -59,7 +60,7 @@ class ClientTest {
5960
)
6061
val inboxId = runBlocking { Client.getOrCreateInboxId(options.api, fakeWallet.address) }
6162
val client = runBlocking {
62-
Client().create(
63+
Client.create(
6364
account = fakeWallet,
6465
options = options
6566
)
@@ -126,7 +127,7 @@ class ClientTest {
126127
val fakeWallet = PrivateKeyBuilder()
127128
val fakeWallet2 = PrivateKeyBuilder()
128129
var client = runBlocking {
129-
Client().create(
130+
Client.create(
130131
account = fakeWallet,
131132
options = ClientOptions(
132133
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -136,7 +137,7 @@ class ClientTest {
136137
)
137138
}
138139
val client2 = runBlocking {
139-
Client().create(
140+
Client.create(
140141
account = fakeWallet2,
141142
options = ClientOptions(
142143
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -156,7 +157,7 @@ class ClientTest {
156157
client.deleteLocalDatabase()
157158

158159
client = runBlocking {
159-
Client().create(
160+
Client.create(
160161
account = fakeWallet,
161162
options = ClientOptions(
162163
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -177,7 +178,7 @@ class ClientTest {
177178
val context = InstrumentationRegistry.getInstrumentation().targetContext
178179
val fakeWallet = PrivateKeyBuilder()
179180
val client = runBlocking {
180-
Client().create(
181+
Client.create(
181182
account = fakeWallet,
182183
options = ClientOptions(
183184
ClientOptions.Api(XMTPEnvironment.DEV, true),
@@ -197,7 +198,7 @@ class ClientTest {
197198
val context = InstrumentationRegistry.getInstrumentation().targetContext
198199
val fakeWallet = PrivateKeyBuilder()
199200
val client = runBlocking {
200-
Client().create(
201+
Client.create(
201202
account = fakeWallet,
202203
options = ClientOptions(
203204
ClientOptions.Api(XMTPEnvironment.PRODUCTION, true),
@@ -230,7 +231,7 @@ class ClientTest {
230231
)
231232

232233
try {
233-
runBlocking { Client().create(account = fakeWallet, options = opts) }
234+
runBlocking { Client.create(account = fakeWallet, options = opts) }
234235
expectation.get(5, TimeUnit.SECONDS)
235236
} catch (e: Exception) {
236237
fail("Error: $e")
@@ -244,7 +245,7 @@ class ClientTest {
244245
val fakeWallet = PrivateKeyBuilder()
245246
val fakeWallet2 = PrivateKeyBuilder()
246247
val boClient = runBlocking {
247-
Client().create(
248+
Client.create(
248249
account = fakeWallet,
249250
options = ClientOptions(
250251
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -254,7 +255,7 @@ class ClientTest {
254255
)
255256
}
256257
val alixClient = runBlocking {
257-
Client().create(
258+
Client.create(
258259
account = fakeWallet2,
259260
options = ClientOptions(
260261
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -294,7 +295,7 @@ class ClientTest {
294295
val alixWallet = PrivateKeyBuilder()
295296
val boWallet = PrivateKeyBuilder()
296297
val alixClient = runBlocking {
297-
Client().create(
298+
Client.create(
298299
account = alixWallet,
299300
options = ClientOptions(
300301
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -304,7 +305,7 @@ class ClientTest {
304305
)
305306
}
306307
val boClient = runBlocking {
307-
Client().create(
308+
Client.create(
308309
account = boWallet,
309310
options = ClientOptions(
310311
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -326,7 +327,7 @@ class ClientTest {
326327
val alixWallet = PrivateKeyBuilder()
327328

328329
val alixClient = runBlocking {
329-
Client().create(
330+
Client.create(
330331
account = alixWallet,
331332
options = ClientOptions(
332333
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -337,7 +338,7 @@ class ClientTest {
337338
}
338339

339340
val alixClient2 = runBlocking {
340-
Client().create(
341+
Client.create(
341342
account = alixWallet,
342343
options = ClientOptions(
343344
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -349,7 +350,7 @@ class ClientTest {
349350
}
350351

351352
val alixClient3 = runBlocking {
352-
Client().create(
353+
Client.create(
353354
account = alixWallet,
354355
options = ClientOptions(
355356
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -378,7 +379,7 @@ class ClientTest {
378379
val context = InstrumentationRegistry.getInstrumentation().targetContext
379380
val alixWallet = PrivateKeyBuilder()
380381
runBlocking {
381-
val alixClient = Client().create(
382+
val alixClient = Client.create(
382383
account = alixWallet,
383384
options = ClientOptions(
384385
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -387,7 +388,7 @@ class ClientTest {
387388
)
388389
)
389390

390-
val alixClient2 = Client().create(
391+
val alixClient2 = Client.create(
391392
account = alixWallet,
392393
options = ClientOptions(
393394
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -399,7 +400,7 @@ class ClientTest {
399400
}
400401

401402
val alixClient3 = runBlocking {
402-
Client().create(
403+
Client.create(
403404
account = alixWallet,
404405
options = ClientOptions(
405406
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -487,7 +488,7 @@ class ClientTest {
487488
val key = SecureRandom().generateSeed(32)
488489
val context = InstrumentationRegistry.getInstrumentation().targetContext
489490
val alixClient2 = runBlocking {
490-
Client().create(
491+
Client.create(
491492
account = fixtures.alixAccount,
492493
options = ClientOptions(
493494
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -610,7 +611,7 @@ class ClientTest {
610611
val alixWallet = PrivateKeyBuilder()
611612

612613
val alixClient = runBlocking {
613-
Client().create(
614+
Client.create(
614615
account = alixWallet,
615616
options = ClientOptions(
616617
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -625,7 +626,7 @@ class ClientTest {
625626
XMTPException::class.java
626627
) {
627628
runBlocking {
628-
Client().build(
629+
Client.build(
629630
address = alixClient.address,
630631
options = ClientOptions(
631632
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -641,7 +642,7 @@ class ClientTest {
641642
XMTPException::class.java
642643
) {
643644
runBlocking {
644-
Client().create(
645+
Client.create(
645646
account = alixWallet,
646647
options = ClientOptions(
647648
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -652,4 +653,32 @@ class ClientTest {
652653
}
653654
}
654655
}
656+
657+
@Test
658+
fun testCreatesAClientManually() {
659+
val key = SecureRandom().generateSeed(32)
660+
val context = InstrumentationRegistry.getInstrumentation().targetContext
661+
val fakeWallet = PrivateKeyBuilder()
662+
val options = ClientOptions(
663+
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
664+
appContext = context,
665+
dbEncryptionKey = key
666+
)
667+
val inboxId = runBlocking { Client.getOrCreateInboxId(options.api, fakeWallet.address) }
668+
val client = runBlocking {
669+
Client.ffiCreateClient(fakeWallet.address, options)
670+
}
671+
runBlocking {
672+
val sigRequest = client.ffiSignatureRequest()
673+
sigRequest?.let { signatureRequest ->
674+
signatureRequest.addEcdsaSignature(fakeWallet.sign(signatureRequest.signatureText()).rawData)
675+
client.ffiRegisterIdentity(signatureRequest)
676+
}
677+
}
678+
runBlocking {
679+
client.canMessage(listOf(client.address))[client.address]?.let { assert(it) }
680+
}
681+
assert(client.installationId.isNotEmpty())
682+
assertEquals(inboxId, client.inboxId)
683+
}
655684
}

library/src/androidTest/java/org/xmtp/android/library/ConversationsTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class ConversationsTest {
252252
val conversations = mutableListOf<Conversation>()
253253
repeat(5) {
254254
val account = PrivateKeyBuilder()
255-
val client = runBlocking { Client().create(account, fixtures.clientOptions) }
255+
val client = runBlocking { Client.create(account, fixtures.clientOptions) }
256256
runBlocking {
257257
conversations.add(
258258
alixClient.conversations.newConversation(client.address)

library/src/androidTest/java/org/xmtp/android/library/HistorySyncTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class HistorySyncTest {
4747
alixGroup = runBlocking { alixClient.conversations.newGroup(listOf(bo.walletAddress)) }
4848

4949
alixClient2 = runBlocking {
50-
Client().create(
50+
Client.create(
5151
account = alixWallet,
5252
options = ClientOptions(
5353
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -109,7 +109,7 @@ class HistorySyncTest {
109109
assertEquals(state.installations.size, 2)
110110

111111
val alixClient3 = runBlocking {
112-
Client().create(
112+
Client.create(
113113
account = alixWallet,
114114
options = ClientOptions(
115115
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
@@ -193,7 +193,7 @@ class HistorySyncTest {
193193

194194
runBlocking {
195195
val alixClient3 = runBlocking {
196-
Client().create(
196+
Client.create(
197197
account = alixWallet,
198198
options = ClientOptions(
199199
ClientOptions.Api(XMTPEnvironment.LOCAL, false),

library/src/androidTest/java/org/xmtp/android/library/PerformanceTest.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class PerformanceTest {
112112
val fakeWallet = PrivateKeyBuilder()
113113
val start = Date()
114114
val client = runBlocking {
115-
Client().create(
115+
Client.create(
116116
account = fakeWallet,
117117
options = ClientOptions(
118118
ClientOptions.Api(XMTPEnvironment.DEV, true),
@@ -127,7 +127,7 @@ class PerformanceTest {
127127

128128
val start2 = Date()
129129
val buildClient1 = runBlocking {
130-
Client().build(
130+
Client.build(
131131
fakeWallet.address,
132132
options = ClientOptions(
133133
ClientOptions.Api(XMTPEnvironment.DEV, true),
@@ -142,7 +142,7 @@ class PerformanceTest {
142142

143143
val start3 = Date()
144144
val buildClient2 = runBlocking {
145-
Client().build(
145+
Client.build(
146146
fakeWallet.address,
147147
options = ClientOptions(
148148
ClientOptions.Api(XMTPEnvironment.DEV, true),
@@ -159,7 +159,7 @@ class PerformanceTest {
159159
runBlocking { Client.connectToApiBackend(ClientOptions.Api(XMTPEnvironment.DEV, true)) }
160160
val start4 = Date()
161161
runBlocking {
162-
Client().create(
162+
Client.create(
163163
PrivateKeyBuilder(),
164164
options = ClientOptions(
165165
ClientOptions.Api(XMTPEnvironment.DEV, true),

0 commit comments

Comments
 (0)