expose private and public keys

This commit is contained in:
Dmitriy Ryajov 2019-08-29 23:17:29 -06:00
parent e5be1fcaee
commit e7c3412d69
1 changed files with 12 additions and 0 deletions

View File

@ -16,12 +16,22 @@ import protobuf/minprotobuf
const const
maxInlineKeyLength* = 42 maxInlineKeyLength* = 42
# TODO: add proper on disc serialization
# using peer-id protobuf format
type type
PeerID* = object PeerID* = object
data*: seq[byte] data*: seq[byte]
privateKey: PrivateKey
publicKey: PublicKey
PeerIDError* = object of CatchableError PeerIDError* = object of CatchableError
proc publicKey*(pid: PeerID): PublicKey {.inline.} =
if len(pid.publicKey.getBytes()) > 0:
result = pid.publicKey
elif len(pid.privateKey.getBytes()) > 0:
result = pid.privateKey.getKey()
proc pretty*(pid: PeerID): string {.inline.} = proc pretty*(pid: PeerID): string {.inline.} =
## Return base58 encoded ``pid`` representation. ## Return base58 encoded ``pid`` representation.
result = Base58.encode(pid.data) result = Base58.encode(pid.data)
@ -161,10 +171,12 @@ proc init*(t: typedesc[PeerID], pubkey: PublicKey): PeerID =
else: else:
mh = MultiHash.digest("sha2-256", pubraw) mh = MultiHash.digest("sha2-256", pubraw)
result.data = mh.data.buffer result.data = mh.data.buffer
result.publicKey = pubkey
proc init*(t: typedesc[PeerID], seckey: PrivateKey): PeerID {.inline.} = proc init*(t: typedesc[PeerID], seckey: PrivateKey): PeerID {.inline.} =
## Create new peer id from private key ``seckey``. ## Create new peer id from private key ``seckey``.
result = PeerID.init(seckey.getKey()) result = PeerID.init(seckey.getKey())
result.privateKey = seckey
proc match*(pid: PeerID, pubkey: PublicKey): bool {.inline.} = proc match*(pid: PeerID, pubkey: PublicKey): bool {.inline.} =
## Returns ``true`` if ``pid`` matches public key ``pubkey``. ## Returns ``true`` if ``pid`` matches public key ``pubkey``.