diff --git a/libp2p/peer.nim b/libp2p/peer.nim index a9b144d70..db6d9c3b5 100644 --- a/libp2p/peer.nim +++ b/libp2p/peer.nim @@ -16,12 +16,22 @@ import protobuf/minprotobuf const maxInlineKeyLength* = 42 +# TODO: add proper on disc serialization +# using peer-id protobuf format type PeerID* = object data*: seq[byte] + privateKey: PrivateKey + publicKey: PublicKey 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.} = ## Return base58 encoded ``pid`` representation. result = Base58.encode(pid.data) @@ -161,10 +171,12 @@ proc init*(t: typedesc[PeerID], pubkey: PublicKey): PeerID = else: mh = MultiHash.digest("sha2-256", pubraw) result.data = mh.data.buffer + result.publicKey = pubkey proc init*(t: typedesc[PeerID], seckey: PrivateKey): PeerID {.inline.} = ## Create new peer id from private key ``seckey``. result = PeerID.init(seckey.getKey()) + result.privateKey = seckey proc match*(pid: PeerID, pubkey: PublicKey): bool {.inline.} = ## Returns ``true`` if ``pid`` matches public key ``pubkey``.