mirror of
https://github.com/codex-storage/nim-codex-dht.git
synced 2025-02-06 00:53:38 +00:00
4c65f4bd94
Closes: #2. Libp2p supports multiple cryptographic curves, however we have currently only implented support for secp256k1. This needs to be run with the compiler flag `libp2p_pki_schemes` set to `secp256k1`. If running the tests, this can be run like so: `nimble test —libp2p_pki_schemes=secp256k1` to put secp as the first supported crypto scheme.
31 lines
1.0 KiB
Nim
31 lines
1.0 KiB
Nim
import
|
|
std/sugar,
|
|
libp2p/crypto/[crypto, secp]
|
|
|
|
from secp256k1 import ecdhRaw, SkEcdhRawSecret, toRaw
|
|
|
|
proc fromHex*(T: type PrivateKey, data: string): Result[PrivateKey, cstring] =
|
|
let skKey = ? SkPrivateKey.init(data).mapErr(e =>
|
|
("Failed to init private key from hex string: " & $e).cstring)
|
|
ok PrivateKey.init(skKey)
|
|
|
|
proc fromHex*(T: type PublicKey, data: string): Result[PublicKey, cstring] =
|
|
let skKey = ? SkPublicKey.init(data).mapErr(e =>
|
|
("Failed to init public key from hex string: " & $e).cstring)
|
|
ok PublicKey.init(skKey)
|
|
|
|
func ecdhRaw*(seckey: SkPrivateKey, pubkey: SkPublicKey): SkEcdhRawSecret {.borrow.}
|
|
|
|
proc ecdhRaw*(
|
|
priv: PrivateKey,
|
|
pub: PublicKey): Result[SkEcdhRawSecret, cstring] =
|
|
|
|
# TODO: Do we need to support non-secp256k1 schemes?
|
|
if priv.scheme != Secp256k1 or pub.scheme != Secp256k1:
|
|
return err "Must use secp256k1 scheme".cstring
|
|
|
|
ok ecdhRaw(priv.skkey, pub.skkey)
|
|
|
|
proc toRaw*(pubkey: PublicKey): seq[byte] =
|
|
secp256k1.SkPublicKey(pubkey.skkey).toRaw()[1..^1]
|