-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathConversation.ts
372 lines (340 loc) · 9.73 KB
/
Conversation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import {
buildUserIntroTopic,
buildDirectMessageTopic,
dateToNs,
nsToDate,
} from '../utils'
import { utils } from 'ethers'
import { DecodedMessage } from './../Message'
import Stream from '../Stream'
import Client, {
ListMessagesOptions,
ListMessagesPaginatedOptions,
SendOptions,
} from '../Client'
import {
InvitationContext,
InvitationV1,
SealedInvitationHeaderV1,
} from '../Invitation'
import { MessageV1, MessageV2, decodeContent } from '../Message'
import { messageApi, message, content as proto, fetcher } from '@xmtp/proto'
import {
encrypt,
decrypt,
SignedPublicKey,
Signature,
PublicKeyBundle,
} from '../crypto'
import Ciphertext from '../crypto/Ciphertext'
import { sha256 } from '../crypto/encryption'
import { ContentTypeText } from '../codecs/Text'
const { b64Decode } = fetcher
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/**
* Conversation class allows you to view, stream, and send messages to/from a peer address
*/
export class ConversationV1 {
peerAddress: string
createdAt: Date
context = null
private client: Client
constructor(client: Client, address: string, createdAt: Date) {
this.peerAddress = utils.getAddress(address)
this.client = client
this.createdAt = createdAt
}
/**
* Returns a list of all messages to/from the peerAddress
*/
async messages(opts?: ListMessagesOptions): Promise<DecodedMessage[]> {
const topics = [
buildDirectMessageTopic(this.peerAddress, this.client.address),
]
return this.client.listEnvelopes(
topics,
this.decodeMessage.bind(this),
opts
)
}
get topic(): string {
return buildDirectMessageTopic(this.peerAddress, this.client.address)
}
messagesPaginated(
opts?: ListMessagesPaginatedOptions
): AsyncGenerator<DecodedMessage[]> {
return this.client.listEnvelopesPaginated(
[this.topic],
this.decodeMessage.bind(this),
opts
)
}
/**
* Returns a Stream of any new messages to/from the peerAddress
*/
streamMessages(): Promise<Stream<DecodedMessage>> {
return Stream.create<DecodedMessage>(
this.client,
[this.topic],
this.decodeMessage.bind(this)
)
}
async decodeMessage({
message,
contentTopic,
}: messageApi.Envelope): Promise<DecodedMessage> {
const messageBytes = fetcher.b64Decode(message as unknown as string)
const decoded = await MessageV1.fromBytes(messageBytes)
const { senderAddress, recipientAddress } = decoded
// Filter for topics
if (
!senderAddress ||
!recipientAddress ||
!contentTopic ||
buildDirectMessageTopic(senderAddress, recipientAddress) !== this.topic
) {
throw new Error('Headers do not match intended recipient')
}
const decrypted = await decoded.decrypt(this.client.legacyKeys)
const { content, contentType, error } = decodeContent(
decrypted,
this.client
)
return DecodedMessage.fromV1Message(
decoded,
content,
contentType,
contentTopic,
this,
error
)
}
/**
* Send a message into the conversation
*/
async send(
content: any, // eslint-disable-line @typescript-eslint/no-explicit-any
options?: SendOptions
): Promise<DecodedMessage> {
let topics: string[]
let recipient = await this.client.getUserContact(this.peerAddress)
if (!recipient) {
throw new Error(`recipient ${this.peerAddress} is not registered`)
}
if (!(recipient instanceof PublicKeyBundle)) {
recipient = recipient.toLegacyBundle()
}
if (!this.client.contacts.has(this.peerAddress)) {
topics = [
buildUserIntroTopic(this.peerAddress),
buildUserIntroTopic(this.client.address),
this.topic,
]
this.client.contacts.add(this.peerAddress)
} else {
topics = [this.topic]
}
const contentType = options?.contentType || ContentTypeText
const timestamp = options?.timestamp || new Date()
const payload = await this.client.encodeContent(content, options)
const msg = await MessageV1.encode(
this.client.legacyKeys,
recipient,
payload,
timestamp
)
await this.client.publishEnvelopes(
topics.map((topic) => ({
contentTopic: topic,
message: msg.toBytes(),
timestamp: msg.sent,
}))
)
return DecodedMessage.fromV1Message(
msg,
content,
contentType,
topics[0], // Just use the first topic for the returned value
this
)
}
get clientAddress() {
return this.client.address
}
}
export class ConversationV2 {
topic: string
keyMaterial: Uint8Array // MUST be kept secret
context?: InvitationContext
private header: SealedInvitationHeaderV1
private client: Client
peerAddress: string
constructor(
client: Client,
invitation: InvitationV1,
header: SealedInvitationHeaderV1,
peerAddress: string
) {
this.topic = invitation.topic
this.keyMaterial = invitation.aes256GcmHkdfSha256.keyMaterial
this.context = invitation.context
this.client = client
this.header = header
this.peerAddress = utils.getAddress(peerAddress)
}
static async create(
client: Client,
invitation: InvitationV1,
header: SealedInvitationHeaderV1
): Promise<ConversationV2> {
const myKeys = client.keys.getPublicKeyBundle()
const peer = myKeys.equals(header.sender) ? header.recipient : header.sender
const peerAddress = await peer.walletSignatureAddress()
return new ConversationV2(client, invitation, header, peerAddress)
}
get createdAt(): Date {
return nsToDate(this.header.createdNs)
}
/**
* Returns a list of all messages to/from the peerAddress
*/
async messages(opts?: ListMessagesOptions): Promise<DecodedMessage[]> {
return this.client.listEnvelopes(
[this.topic],
this.decodeMessage.bind(this),
opts
)
}
messagesPaginated(
opts?: ListMessagesPaginatedOptions
): AsyncGenerator<DecodedMessage[]> {
return this.client.listEnvelopesPaginated(
[this.topic],
this.decodeMessage.bind(this),
opts
)
}
/**
* Returns a Stream of any new messages to/from the peerAddress
*/
streamMessages(): Promise<Stream<DecodedMessage>> {
return Stream.create<DecodedMessage>(
this.client,
[this.topic],
this.decodeMessage.bind(this)
)
}
/**
* Send a message into the conversation
*/
async send(
content: any, // eslint-disable-line @typescript-eslint/no-explicit-any
options?: SendOptions
): Promise<DecodedMessage> {
const msg = await this.encodeMessage(content, options)
await this.client.publishEnvelopes([
{
contentTopic: this.topic,
message: msg.toBytes(),
timestamp: msg.sent,
},
])
const contentType = options?.contentType || ContentTypeText
return DecodedMessage.fromV2Message(
msg,
content,
contentType,
this.topic,
this
)
}
get clientAddress() {
return this.client.address
}
private async encodeMessage(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
content: any,
options?: SendOptions
): Promise<MessageV2> {
const payload = await this.client.encodeContent(content, options)
const header: message.MessageHeaderV2 = {
topic: this.topic,
createdNs: dateToNs(options?.timestamp || new Date()),
}
const headerBytes = message.MessageHeaderV2.encode(header).finish()
const digest = await sha256(concat(headerBytes, payload))
const signed = {
payload,
sender: this.client.keys.getPublicKeyBundle(),
signature: await this.client.keys.getCurrentPreKey().sign(digest),
}
const signedBytes = proto.SignedContent.encode(signed).finish()
const ciphertext = await encrypt(signedBytes, this.keyMaterial, headerBytes)
const protoMsg = {
v1: undefined,
v2: { headerBytes, ciphertext },
}
const bytes = message.Message.encode(protoMsg).finish()
return MessageV2.create(protoMsg, header, signed, bytes)
}
async decodeMessage(env: messageApi.Envelope): Promise<DecodedMessage> {
if (!env.message || !env.contentTopic) {
throw new Error('empty envelope')
}
const messageBytes = b64Decode(env.message.toString())
const msg = message.Message.decode(messageBytes)
if (!msg.v2) {
throw new Error('unknown message version')
}
const msgv2 = msg.v2
const header = message.MessageHeaderV2.decode(msgv2.headerBytes)
if (header.topic !== this.topic) {
throw new Error('topic mismatch')
}
if (!msgv2.ciphertext) {
throw new Error('missing ciphertext')
}
const decrypted = await decrypt(
new Ciphertext(msgv2.ciphertext),
this.keyMaterial,
msgv2.headerBytes
)
const signed = proto.SignedContent.decode(decrypted)
if (
!signed.sender?.identityKey ||
!signed.sender?.preKey ||
!signed.signature
) {
throw new Error('incomplete signed content')
}
const digest = await sha256(concat(msgv2.headerBytes, signed.payload))
if (
!new SignedPublicKey(signed.sender?.preKey).verify(
new Signature(signed.signature),
digest
)
) {
throw new Error('invalid signature')
}
const messageV2 = await MessageV2.create(msg, header, signed, messageBytes)
const { content, contentType, error } = decodeContent(
signed.payload,
this.client
)
return DecodedMessage.fromV2Message(
messageV2,
content,
contentType,
env.contentTopic,
this,
error
)
}
}
export type Conversation = ConversationV1 | ConversationV2
function concat(a: Uint8Array, b: Uint8Array): Uint8Array {
const ab = new Uint8Array(a.length + b.length)
ab.set(a)
ab.set(b, a.length)
return ab
}