diff --git a/src/chat/client.nim b/src/chat/client.nim index 02e2c73..06f2c3f 100644 --- a/src/chat/client.nim +++ b/src/chat/client.nim @@ -50,7 +50,7 @@ type KeyEntry* = object type Client* = ref object ident: Identity ds*: WakuClient - keyStore: Table[string, KeyEntry] # Keyed by HexEncoded Public Key + keyStore: Table[RemoteKeyIdentifier, seq[KeyEntry]] conversations: Table[string, Conversation] # Keyed by conversation ID inboundQueue: QueueRef isRunning: bool @@ -77,7 +77,7 @@ proc newClient*(cfg: WakuConfig, ident: Identity): Client {.raises: [IOError, var q = QueueRef(queue: newAsyncQueue[ChatPayload](10)) var c = Client(ident: ident, ds: waku, - keyStore: initTable[string, KeyEntry](), + keyStore: initTable[RemoteKeyIdentifier, seq[KeyEntry]](), conversations: initTable[string, Conversation](), inboundQueue: q, isRunning: false, @@ -150,18 +150,23 @@ proc notifyDeliveryAck(client: Client, convo: Conversation, # Functional ################################################# +proc cacheInviteKey(self: var Client, key: PrivateKey) = + + let rki_ephemeral = generateRemoteKeyIdentifier(key.getPublicKey()) + + self.keyStore[rki_ephemeral] = @[KeyEntry( + keyType: "ephemeral", + privateKey: key, + timestamp: getCurrentTimestamp() + )] + proc createIntroBundle*(self: var Client): IntroBundle = ## Generates an IntroBundle for the client, which includes ## the required information to send a message. # Create Ephemeral keypair, save it in the key store let ephemeralKey = generateKey() - - self.keyStore[ephemeralKey.getPublicKey().bytes().bytesToHex()] = KeyEntry( - keyType: "ephemeral", - privateKey: ephemeralKey, - timestamp: getCurrentTimestamp() - ) + self.cacheInviteKey(ephemeralKey) result = IntroBundle( ident: @(self.ident.getPubkey().bytes()), diff --git a/src/chat/crypto.nim b/src/chat/crypto.nim index 1aa4cc5..2e819d7 100644 --- a/src/chat/crypto.nim +++ b/src/chat/crypto.nim @@ -1,11 +1,13 @@ -import proto_types - -import strformat import crypto/ecdh -import std/[sysrand] import results +import sequtils +import std/[endians,sysrand] +import strformat +import types import utils +import proto_types + export PublicKey, PrivateKey, bytes, createRandomKey, loadPrivateKeyFromBytes, loadPublicKeyFromBytes, getPublicKey, Dh, Result, get_addr, `$` @@ -33,3 +35,10 @@ proc toHex*(key: PublicKey): string = proc `$`*(key: PublicKey): string = let byteStr = toHex(key) fmt"{byteStr[0..3]}..{byteStr[^4 .. ^1]}" + +proc generateRemoteKeyIdentifier*(publicKey: PublicKey): RemoteKeyIdentifier = + let bytes = cast[seq[byte]]("WAP") & publicKey.bytes().toSeq() + let hash = utils.hash_func_bytes(4,bytes) + + var result: uint32 + littleEndian32(addr result, unsafeAddr hash[0]) \ No newline at end of file diff --git a/src/chat/inbox.nim b/src/chat/inbox.nim index 7af319a..3f4e8d1 100644 --- a/src/chat/inbox.nim +++ b/src/chat/inbox.nim @@ -86,14 +86,14 @@ proc sendFrame(ds: WakuClient, remote: PublicKey, frame: InboxV1Frame ): Future[ proc newPrivateInvite(initator_static: PublicKey, initator_ephemeral: PublicKey, recipient_static: PublicKey, - recipient_ephemeral: uint32, + recipient_ephemeral: PublicKey, payload: EncryptedPayload) : InboxV1Frame = let invite = InvitePrivateV1( initiator: @(initator_static.bytes()), initiatorEphemeral: @(initator_ephemeral.bytes()), participant: @(recipient_static.bytes()), - participantEphemeralId: 0, + participantEphemeralId: cast[int32](generateRemoteKeyIdentifier(recipient_ephemeral)), discriminator: "", initial_message: payload ) @@ -115,7 +115,7 @@ proc inviteToPrivateConversation*(self: Inbox, ds: Wakuclient, remote_static: Pu result = convo # # Build Invite - let frame = newPrivateInvite(self.identity.getPubkey(), local_ephemeral.getPublicKey(), remote_static, 0, encPayload) + let frame = newPrivateInvite(self.identity.getPubkey(), local_ephemeral.getPublicKey(), remote_static, remote_ephemeral, encPayload) # Send await sendFrame(ds, remote_static, frame) diff --git a/src/chat/types.nim b/src/chat/types.nim index d5989fd..493a36d 100644 --- a/src/chat/types.nim +++ b/src/chat/types.nim @@ -1,2 +1,4 @@ type MessageId* = string type Content* = seq[byte] + +type RemoteKeyIdentifier* = uint32