@@ -2,11 +2,11 @@ package com.getcode.model
22
33import com.codeinc.gen.chat.v1.ChatService
44import com.codeinc.gen.chat.v1.ChatService.Content
5+ import com.codeinc.gen.chat.v2.ChatService as ChatServiceV2
6+ import com.codeinc.gen.chat.v2.ChatService.Content as MessageContentV2
57import com.getcode.ed25519.Ed25519.KeyPair
68import com.getcode.network.repository.toPublicKey
7- import com.getcode.solana.keys.SodiumError
89
9- typealias ID = List <Byte >
1010typealias Cursor = List <Byte >
1111
1212/* *
@@ -136,6 +136,7 @@ sealed interface Verb {
136136 }
137137
138138 companion object {
139+ @Deprecated(" Replaced with v2" )
139140 operator fun invoke (proto : ChatService .ExchangeDataContent .Verb ): Verb {
140141 return when (proto) {
141142 ChatService .ExchangeDataContent .Verb .UNKNOWN -> Unknown
@@ -153,6 +154,24 @@ sealed interface Verb {
153154 ChatService .ExchangeDataContent .Verb .SENT_TIP -> SentTip
154155 }
155156 }
157+
158+ fun fromV2 (proto : ChatServiceV2 .ExchangeDataContent .Verb ): Verb {
159+ return when (proto) {
160+ ChatServiceV2 .ExchangeDataContent .Verb .UNKNOWN -> Unknown
161+ ChatServiceV2 .ExchangeDataContent .Verb .GAVE -> Gave
162+ ChatServiceV2 .ExchangeDataContent .Verb .RECEIVED -> Received
163+ ChatServiceV2 .ExchangeDataContent .Verb .WITHDREW -> Withdrew
164+ ChatServiceV2 .ExchangeDataContent .Verb .DEPOSITED -> Deposited
165+ ChatServiceV2 .ExchangeDataContent .Verb .SENT -> Sent
166+ ChatServiceV2 .ExchangeDataContent .Verb .RETURNED -> Returned
167+ ChatServiceV2 .ExchangeDataContent .Verb .SPENT -> Spent
168+ ChatServiceV2 .ExchangeDataContent .Verb .PAID -> Paid
169+ ChatServiceV2 .ExchangeDataContent .Verb .PURCHASED -> Purchased
170+ ChatServiceV2 .ExchangeDataContent .Verb .UNRECOGNIZED -> Unknown
171+ ChatServiceV2 .ExchangeDataContent .Verb .RECEIVED_TIP -> ReceivedTip
172+ ChatServiceV2 .ExchangeDataContent .Verb .SENT_TIP -> SentTip
173+ }
174+ }
156175 }
157176}
158177
@@ -222,9 +241,10 @@ sealed interface MessageContent {
222241 ) : MessageContent
223242
224243 companion object {
244+ @Deprecated(" Replaced with v2" )
225245 operator fun invoke (proto : Content ): MessageContent ? {
226246 return when (proto.typeCase) {
227- Content .TypeCase .LOCALIZED -> Localized (proto.localized .keyOrText)
247+ Content .TypeCase .SERVER_LOCALIZED -> Localized (proto.serverLocalized .keyOrText)
228248 Content .TypeCase .EXCHANGE_DATA -> {
229249 val verb = Verb (proto.exchangeData.verb)
230250 val messageStatus = if (verb.increasesBalance) MessageStatus .Incoming else MessageStatus .Delivered
@@ -277,6 +297,62 @@ sealed interface MessageContent {
277297 else -> return null
278298 }
279299 }
300+
301+ fun fromV2 (proto : MessageContentV2 ): MessageContent ? {
302+ return when (proto.typeCase) {
303+ MessageContentV2 .TypeCase .LOCALIZED -> Localized (proto.localized.keyOrText)
304+ MessageContentV2 .TypeCase .EXCHANGE_DATA -> {
305+ val verb = Verb .fromV2(proto.exchangeData.verb)
306+ val messageStatus = if (verb.increasesBalance) MessageStatus .Incoming else MessageStatus .Delivered
307+ when (proto.exchangeData.exchangeDataCase) {
308+ ChatServiceV2 .ExchangeDataContent .ExchangeDataCase .EXACT -> {
309+ val exact = proto.exchangeData.exact
310+ val currency = CurrencyCode .tryValueOf(exact.currency) ? : return null
311+ val kinAmount = KinAmount .newInstance(
312+ kin = Kin .fromQuarks(exact.quarks),
313+ rate = Rate (
314+ fx = exact.exchangeRate,
315+ currency = currency
316+ )
317+ )
318+
319+ Exchange (GenericAmount .Exact (kinAmount), verb, status = messageStatus)
320+ }
321+
322+ ChatServiceV2 .ExchangeDataContent .ExchangeDataCase .PARTIAL -> {
323+ val partial = proto.exchangeData.partial
324+ val currency = CurrencyCode .tryValueOf(partial.currency) ? : return null
325+
326+ val fiat = Fiat (
327+ currency = currency,
328+ amount = partial.nativeAmount
329+ )
330+
331+ Exchange (GenericAmount .Partial (fiat), verb, status = messageStatus)
332+ }
333+
334+ ChatServiceV2 .ExchangeDataContent .ExchangeDataCase .EXCHANGEDATA_NOT_SET -> return null
335+ else -> return null
336+ }
337+ }
338+
339+ MessageContentV2 .TypeCase .NACL_BOX -> {
340+ val encryptedContent = proto.naclBox
341+ val peerPublicKey =
342+ encryptedContent.peerPublicKey.value.toByteArray().toPublicKey()
343+
344+ val data = EncryptedData (
345+ peerPublicKey = peerPublicKey,
346+ nonce = encryptedContent.nonce.toByteArray().toList(),
347+ encryptedData = encryptedContent.encryptedPayload.toByteArray().toList(),
348+ )
349+ SodiumBox (data = data)
350+ }
351+
352+ MessageContentV2 .TypeCase .TYPE_NOT_SET -> return null
353+ else -> return null
354+ }
355+ }
280356 }
281357}
282358
0 commit comments