@@ -20,10 +20,12 @@ import org.xmtp.android.library.messages.generate
20
20
import org.xmtp.android.library.messages.secp256K1Uncompressed
21
21
import org.xmtp.android.library.messages.toPublicKeyBundle
22
22
import org.xmtp.android.library.messages.walletAddress
23
+ import org.xmtp.proto.keystore.api.v1.Keystore
23
24
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.QueryRequest
24
25
import org.xmtp.proto.message.contents.Contact
25
26
import org.xmtp.proto.message.contents.InvitationV1Kt.context
26
27
import org.xmtp.proto.message.contents.PrivateKeyOuterClass
28
+ import org.xmtp.proto.message.contents.PrivateKeyOuterClass.PrivateKeyBundle
27
29
import java.util.Date
28
30
29
31
@RunWith(AndroidJUnit4 ::class )
@@ -186,6 +188,50 @@ class LocalInstrumentedTest {
186
188
assertEquals(" example.com/alice-bob-1" , aliceConvoList[1 ].conversationId)
187
189
}
188
190
191
+ @Test
192
+ fun testUsingSavedCredentialsAndKeyMaterial () {
193
+ val options = ClientOptions (ClientOptions .Api (XMTPEnvironment .LOCAL , isSecure = false ))
194
+ val alice = Client ().create(PrivateKeyBuilder (), options)
195
+ val bob = Client ().create(PrivateKeyBuilder (), options)
196
+
197
+ // Alice starts a conversation with Bob
198
+ val aliceConvo = alice.conversations.newConversation(
199
+ bob.address,
200
+ context = context {
201
+ conversationId = " example.com/alice-bob-1"
202
+ metadata[" title" ] = " Chatting Using Saved Credentials"
203
+ }
204
+ )
205
+ aliceConvo.send(" Hello Bob" )
206
+ delayToPropagate()
207
+
208
+ // Alice stores her credentials and conversations to her device
209
+ val keyBundle = alice.privateKeyBundle.toByteArray()
210
+ val topicData = aliceConvo.toTopicData().toByteArray()
211
+
212
+ // Meanwhile, Bob sends a reply.
213
+ val bobConvos = bob.conversations.list()
214
+ val bobConvo = bobConvos[0 ]
215
+ bobConvo.send(" Oh, hello Alice" )
216
+ delayToPropagate()
217
+
218
+ // When Alice's device wakes up, it uses her saved credentials
219
+ val alice2 = Client ().buildFromBundle(
220
+ PrivateKeyBundle .parseFrom(keyBundle),
221
+ options
222
+ )
223
+ // And it uses the saved topic data for the conversation
224
+ val aliceConvo2 = alice2.conversations.importTopicData(
225
+ Keystore .TopicMap .TopicData .parseFrom(topicData)
226
+ )
227
+ assertEquals(" example.com/alice-bob-1" , aliceConvo2.conversationId)
228
+
229
+ // Now Alice should be able to load message using her saved key material.
230
+ val messages = aliceConvo2.messages()
231
+ assertEquals(" Hello Bob" , messages[1 ].body)
232
+ assertEquals(" Oh, hello Alice" , messages[0 ].body)
233
+ }
234
+
189
235
@Test
190
236
fun testCanPaginateV1Messages () {
191
237
val bob = PrivateKeyBuilder ()
0 commit comments