mirror of
https://github.com/logos-messaging/nim-chat-poc.git
synced 2026-01-03 14:43:08 +00:00
flatten publickey
This commit is contained in:
parent
a6d54ab180
commit
627e6c8dd9
@ -1,11 +1,13 @@
|
||||
import proto_types
|
||||
|
||||
import strformat
|
||||
import crypto/ecdh
|
||||
import std/[sysrand]
|
||||
import results
|
||||
import utils
|
||||
|
||||
export PublicKey, PrivateKey, bytes, createRandomKey, loadPrivateKeyFromBytes, loadPublicKeyFromBytes,
|
||||
getPublicKey, Dh, Result
|
||||
getPublicKey, Dh, Result, get_addr
|
||||
|
||||
|
||||
proc encrypt_plain*[T: EncryptableTypes](frame: T): EncryptedPayload =
|
||||
@ -24,3 +26,6 @@ proc decrypt_plain*[T: EncryptableTypes](ciphertext: Plaintext, t: typedesc[
|
||||
proc generate_key*(): PrivateKey =
|
||||
createRandomKey().get()
|
||||
|
||||
proc `$`*(key: PublicKey): string =
|
||||
let byteStr = bytesToHex(key.bytes())
|
||||
fmt"{byteStr[0..3]}..{byteStr[^4 .. ^1]}"
|
||||
@ -2,18 +2,24 @@ import results
|
||||
import libp2p/crypto/curve25519
|
||||
import bearssl/rand
|
||||
|
||||
import ../utils
|
||||
|
||||
type PrivateKey* = object
|
||||
bytes: Curve25519Key
|
||||
|
||||
type PublicKey* = object
|
||||
bytes: Curve25519Key
|
||||
# type PublicKey* = object
|
||||
# bytes: Curve25519Key
|
||||
|
||||
type PublicKey* = distinct Curve25519Key # TODO: define outside of ECDH
|
||||
|
||||
|
||||
|
||||
|
||||
proc bytes*(key: PrivateKey): Curve25519Key =
|
||||
return key.bytes
|
||||
|
||||
proc bytes*(key: PublicKey): Curve25519Key =
|
||||
return key.bytes
|
||||
proc bytes*(key: PublicKey): array[Curve25519KeySize, byte] =
|
||||
cast[array[Curve25519KeySize, byte]](key)
|
||||
|
||||
proc createRandomKey*(): Result[PrivateKey, string] =
|
||||
let rng = HmacDrbgContext.new()
|
||||
@ -29,11 +35,11 @@ proc loadPrivateKeyFromBytes*(bytes: openArray[byte]): Result[PrivateKey, string
|
||||
proc loadPublicKeyFromBytes*(bytes: openArray[byte]): Result[PublicKey, string] =
|
||||
if bytes.len != Curve25519KeySize:
|
||||
return err("Public key size must be 32 bytes")
|
||||
ok(PublicKey(bytes: intoCurve25519Key(bytes)))
|
||||
ok(PublicKey(intoCurve25519Key(bytes)))
|
||||
|
||||
|
||||
proc getPublicKey*(privateKey: PrivateKey): PublicKey =
|
||||
PublicKey(bytes: public(privateKey.bytes))
|
||||
PublicKey( public(privateKey.bytes))
|
||||
|
||||
|
||||
proc Dh*(privateKey: PrivateKey, publicKey: PublicKey): Result[seq[
|
||||
@ -46,3 +52,10 @@ proc Dh*(privateKey: PrivateKey, publicKey: PublicKey): Result[seq[
|
||||
return err("Failed to compute shared secret: " & e.msg)
|
||||
|
||||
return ok(outputKey.getBytes())
|
||||
|
||||
|
||||
proc get_addr*(pubkey: PublicKey): string =
|
||||
# TODO: Needs Spec
|
||||
result = hash_func(pubkey.bytes().bytesToHex())
|
||||
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
import crypto
|
||||
import utils
|
||||
import results
|
||||
import crypto
|
||||
|
||||
|
||||
type
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import waku/waku_core
|
||||
import std/[macros, random, times]
|
||||
import crypto
|
||||
import blake2
|
||||
import strutils
|
||||
|
||||
@ -19,10 +18,6 @@ proc bytesToHex*[T](bytes: openarray[T], lowercase: bool = false): string =
|
||||
let hex = b.toHex(2)
|
||||
result.add(if lowercase: hex.toLower() else: hex)
|
||||
|
||||
proc get_addr*(pubkey: PublicKey): string =
|
||||
# TODO: Needs Spec
|
||||
result = hash_func(pubkey.bytes().bytesToHex())
|
||||
|
||||
proc toBytes*(s: string): seq[byte] =
|
||||
result = cast[seq[byte]](s)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user