mirror of
https://github.com/logos-messaging/nim-chat-poc.git
synced 2026-01-04 15:13:08 +00:00
Populate ephemeral key id
This commit is contained in:
parent
db5e57a48b
commit
917c97e46c
@ -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()),
|
||||
|
||||
@ -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])
|
||||
@ -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)
|
||||
|
||||
@ -1,2 +1,4 @@
|
||||
type MessageId* = string
|
||||
type Content* = seq[byte]
|
||||
|
||||
type RemoteKeyIdentifier* = uint32
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user