mirror of
https://github.com/logos-messaging/nim-chat-poc.git
synced 2026-01-02 14:13:10 +00:00
fix: invitee as the first message sender.
This commit is contained in:
parent
6dff127cab
commit
dd86f2b976
@ -145,7 +145,7 @@ proc wireCallbacks(convo: PrivateV1, deliveryAckCb: proc(
|
||||
|
||||
|
||||
proc initPrivateV1*(owner: Identity, participant: PublicKey, seedKey: array[32, byte],
|
||||
discriminator: string = "default", isSender: bool, deliveryAckCb: proc(
|
||||
discriminator: string = "default", inviter: bool, deliveryAckCb: proc(
|
||||
conversation: Conversation,
|
||||
msgId: string): Future[void] {.async.} = nil):
|
||||
PrivateV1 =
|
||||
@ -161,7 +161,7 @@ proc initPrivateV1*(owner: Identity, participant: PublicKey, seedKey: array[32,
|
||||
topic: derive_topic(participants, discriminator),
|
||||
participant: participant,
|
||||
discriminator: discriminator,
|
||||
doubleratchet: initDoubleratchet(seedKey, owner.privateKey.bytes, participant.bytes, isSender)
|
||||
doubleratchet: initDoubleratchet(seedKey, owner.privateKey.bytes, participant.bytes, inviter)
|
||||
)
|
||||
|
||||
result.wireCallbacks(deliveryAckCb)
|
||||
|
||||
@ -12,13 +12,13 @@ export PublicKey, PrivateKey, bytes, createRandomKey, loadPrivateKeyFromBytes, l
|
||||
|
||||
proc encrypt_plain*[T: EncryptableTypes](frame: T): EncryptedPayload =
|
||||
return EncryptedPayload(
|
||||
plaintext: Plaintext(payload: encode(frame)),
|
||||
plaintext: Plaintext(payload: proto_types.encode(frame)),
|
||||
)
|
||||
|
||||
proc decrypt_plain*[T: EncryptableTypes](ciphertext: Plaintext, t: typedesc[
|
||||
T]): Result[T, string] =
|
||||
|
||||
let obj = decode(ciphertext.payload, T)
|
||||
let obj = proto_types.decode(ciphertext.payload, T)
|
||||
if obj.isErr:
|
||||
return err("Protobuf decode failed: " & obj.error)
|
||||
result = ok(obj.get())
|
||||
|
||||
@ -14,6 +14,8 @@ import
|
||||
types,
|
||||
utils
|
||||
|
||||
import ../content_types
|
||||
|
||||
logScope:
|
||||
topics = "chat inbox"
|
||||
|
||||
@ -42,7 +44,7 @@ proc decrypt*(inbox: Inbox, encbytes: EncryptedPayload): Result[InboxV1Frame, st
|
||||
result = res_frame
|
||||
|
||||
proc wrap_env*(payload: EncryptedPayload, convo_id: string): WapEnvelopeV1 =
|
||||
let bytes = encode(payload)
|
||||
let bytes = proto_types.encode(payload)
|
||||
let salt = generateSalt()
|
||||
|
||||
return WapEnvelopeV1(
|
||||
@ -87,12 +89,16 @@ proc createPrivateV1FromInvite*[T: ConversationStore](client: T,
|
||||
topic = convo.getConvoId()
|
||||
client.addConversation(convo)
|
||||
|
||||
# TODO send a control frame instead
|
||||
discard convo.sendMessage(client.ds, initTextFrame("Hello").toContentFrame())
|
||||
|
||||
|
||||
proc handleFrame*[T: ConversationStore](convo: Inbox, client: T, bytes: seq[
|
||||
byte]) =
|
||||
## Dispatcher for Incoming `InboxV1Frames`.
|
||||
## Calls further processing depending on the kind of frame.
|
||||
|
||||
let enc = decode(bytes, EncryptedPayload).valueOr:
|
||||
let enc = proto_types.decode(bytes, EncryptedPayload).valueOr:
|
||||
raise newException(ValueError, "Failed to decode payload")
|
||||
|
||||
let frame = convo.decrypt(enc).valueOr:
|
||||
|
||||
@ -56,6 +56,8 @@ proc initTextFrame*(text: string): TextFrame =
|
||||
result = TextFrame(encoding: ord(Utf8), text: text)
|
||||
|
||||
|
||||
|
||||
|
||||
proc `$`*(frame: TextFrame): string =
|
||||
|
||||
result = fmt"TextFrame(encoding:{TextEncoding(frame.encoding)} text:{frame.text})"
|
||||
|
||||
@ -76,6 +76,8 @@ func kdfChain(self: Doubleratchet, chainKey: ChainKey): (MessageKey, ChainKey) =
|
||||
proc dhRatchetSend(self: var Doubleratchet) =
|
||||
# Perform DH Ratchet step when receiving a new peer key.
|
||||
info "dhRatchetSend DH Self: ", dhSelf = self.dhSelf
|
||||
self.dhSelf = generateKeypair().get()[0]
|
||||
info "dhRatchetSend new DH Self: ", dhSelf = self.dhSelf
|
||||
let dhOutput : DhDerivedKey = dhExchange(self.dhSelf, self.dhRemote).get()
|
||||
let (newRootKey, newChainKeySend) = kdfRoot(self, self.rootKey, dhOutput)
|
||||
self.rootKey = newRootKey
|
||||
@ -185,7 +187,7 @@ proc encrypt*(self: var Doubleratchet, plaintext: var seq[byte]) : (DrHeader, Ci
|
||||
encrypt(self, plaintext,@[])
|
||||
|
||||
|
||||
proc initDoubleratchet*(sharedSecret: array[32, byte], dhSelf: PrivateKey, dhRemote: PublicKey, isSending: bool = true): Doubleratchet =
|
||||
proc initDoubleratchet*(sharedSecret: array[32, byte], dhSelf: PrivateKey, dhRemote: PublicKey, inviter: bool = true): Doubleratchet =
|
||||
|
||||
info "Initializing Double Ratchet"
|
||||
info "DH Self: ", dhSelf = dhSelf
|
||||
@ -201,5 +203,5 @@ proc initDoubleratchet*(sharedSecret: array[32, byte], dhSelf: PrivateKey, dhRem
|
||||
skippedMessageKeys: initTable[(PublicKey, MsgCount), MessageKey]()
|
||||
)
|
||||
|
||||
if isSending:
|
||||
if not inviter:
|
||||
result.dhRatchetSend()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user