2019-08-20 16:18:15 +00:00
|
|
|
## Nim-LibP2P
|
2019-09-24 17:48:23 +00:00
|
|
|
## Copyright (c) 2019 Status Research & Development GmbH
|
2019-08-20 16:18:15 +00:00
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2020-08-08 06:52:20 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2021-05-22 18:27:30 +00:00
|
|
|
import std/[options, sequtils, hashes]
|
|
|
|
import pkg/[chronos, chronicles, stew/results]
|
|
|
|
import peerid, multiaddress, crypto/crypto, errors
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2021-05-22 18:27:30 +00:00
|
|
|
export peerid, multiaddress, crypto, errors, results
|
2020-07-01 07:22:29 +00:00
|
|
|
|
2019-12-07 16:36:39 +00:00
|
|
|
## A peer can be constructed in one of tree ways:
|
|
|
|
## 1) A local peer with a private key
|
|
|
|
## 2) A remote peer with a PeerID and it's public key stored
|
|
|
|
## in the ``id`` itself
|
|
|
|
## 3) A remote peer with a standalone public key, that isn't
|
|
|
|
## encoded in the ``id``
|
|
|
|
##
|
|
|
|
|
|
|
|
type
|
|
|
|
KeyType* = enum
|
|
|
|
HasPrivate,
|
|
|
|
HasPublic
|
|
|
|
|
2021-05-22 18:27:30 +00:00
|
|
|
PeerInfoError* = LPError
|
|
|
|
|
2019-12-07 16:36:39 +00:00
|
|
|
PeerInfo* = ref object of RootObj
|
|
|
|
peerId*: PeerID
|
2019-12-04 04:44:54 +00:00
|
|
|
addrs*: seq[MultiAddress]
|
|
|
|
protocols*: seq[string]
|
2020-06-09 18:42:52 +00:00
|
|
|
protoVersion*: string
|
|
|
|
agentVersion*: string
|
2020-07-07 11:14:11 +00:00
|
|
|
secure*: string
|
2019-12-07 16:36:39 +00:00
|
|
|
case keyType*: KeyType:
|
|
|
|
of HasPrivate:
|
|
|
|
privateKey*: PrivateKey
|
|
|
|
of HasPublic:
|
|
|
|
key: Option[PublicKey]
|
|
|
|
|
2020-09-06 08:31:47 +00:00
|
|
|
func shortLog*(p: PeerInfo): auto =
|
2020-06-09 18:42:52 +00:00
|
|
|
(
|
2020-09-06 08:31:47 +00:00
|
|
|
peerId: $p.peerId,
|
2020-06-09 18:42:52 +00:00
|
|
|
addrs: mapIt(p.addrs, $it),
|
|
|
|
protocols: mapIt(p.protocols, $it),
|
|
|
|
protoVersion: p.protoVersion,
|
|
|
|
agentVersion: p.agentVersion,
|
|
|
|
)
|
2020-09-06 08:31:47 +00:00
|
|
|
chronicles.formatIt(PeerInfo): shortLog(it)
|
2020-05-11 19:05:24 +00:00
|
|
|
|
2020-02-11 17:53:39 +00:00
|
|
|
template postInit(peerinfo: PeerInfo,
|
|
|
|
addrs: openarray[MultiAddress],
|
|
|
|
protocols: openarray[string]) =
|
|
|
|
if len(addrs) > 0:
|
|
|
|
peerinfo.addrs = @addrs
|
|
|
|
if len(protocols) > 0:
|
|
|
|
peerinfo.protocols = @protocols
|
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
proc init*(
|
|
|
|
p: typedesc[PeerInfo],
|
|
|
|
key: PrivateKey,
|
|
|
|
addrs: openarray[MultiAddress] = [],
|
|
|
|
protocols: openarray[string] = [],
|
|
|
|
protoVersion: string = "",
|
2021-05-22 18:27:30 +00:00
|
|
|
agentVersion: string = ""): PeerInfo
|
|
|
|
{.raises: [Defect, PeerInfoError].} =
|
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
let peerInfo = PeerInfo(
|
|
|
|
keyType: HasPrivate,
|
2021-05-22 18:27:30 +00:00
|
|
|
peerId: PeerID.init(key).tryGet(),
|
2021-05-21 16:27:01 +00:00
|
|
|
privateKey: key,
|
|
|
|
protoVersion: protoVersion,
|
|
|
|
agentVersion: agentVersion)
|
|
|
|
|
|
|
|
peerInfo.postInit(addrs, protocols)
|
|
|
|
return peerInfo
|
|
|
|
|
|
|
|
proc init*(
|
|
|
|
p: typedesc[PeerInfo],
|
|
|
|
peerId: PeerID,
|
|
|
|
addrs: openarray[MultiAddress] = [],
|
|
|
|
protocols: openarray[string] = [],
|
|
|
|
protoVersion: string = "",
|
|
|
|
agentVersion: string = ""): PeerInfo =
|
|
|
|
let peerInfo = PeerInfo(
|
|
|
|
keyType: HasPublic,
|
|
|
|
peerId: peerId,
|
|
|
|
protoVersion: protoVersion,
|
|
|
|
agentVersion: agentVersion)
|
|
|
|
|
|
|
|
peerInfo.postInit(addrs, protocols)
|
|
|
|
return peerInfo
|
|
|
|
|
|
|
|
proc init*(
|
|
|
|
p: typedesc[PeerInfo],
|
|
|
|
peerId: string,
|
|
|
|
addrs: openarray[MultiAddress] = [],
|
|
|
|
protocols: openarray[string] = [],
|
|
|
|
protoVersion: string = "",
|
2021-05-22 18:27:30 +00:00
|
|
|
agentVersion: string = ""): PeerInfo
|
|
|
|
{.raises: [Defect, PeerInfoError].} =
|
2021-05-21 16:27:01 +00:00
|
|
|
|
|
|
|
let peerInfo = PeerInfo(
|
|
|
|
keyType: HasPublic,
|
2021-05-22 18:27:30 +00:00
|
|
|
peerId: PeerID.init(peerId).tryGet(),
|
2021-05-21 16:27:01 +00:00
|
|
|
protoVersion: protoVersion,
|
|
|
|
agentVersion: agentVersion)
|
|
|
|
|
|
|
|
peerInfo.postInit(addrs, protocols)
|
|
|
|
return peerInfo
|
|
|
|
|
|
|
|
proc init*(
|
|
|
|
p: typedesc[PeerInfo],
|
|
|
|
key: PublicKey,
|
|
|
|
addrs: openarray[MultiAddress] = [],
|
|
|
|
protocols: openarray[string] = [],
|
|
|
|
protoVersion: string = "",
|
2021-05-22 18:27:30 +00:00
|
|
|
agentVersion: string = ""): PeerInfo
|
|
|
|
{.raises: [Defect, PeerInfoError].} =
|
2021-05-21 16:27:01 +00:00
|
|
|
|
|
|
|
let peerInfo = PeerInfo(
|
|
|
|
keyType: HasPublic,
|
2021-05-22 18:27:30 +00:00
|
|
|
peerId: PeerID.init(key).tryGet(),
|
2021-05-21 16:27:01 +00:00
|
|
|
key: some(key),
|
|
|
|
protoVersion: protoVersion,
|
|
|
|
agentVersion: agentVersion)
|
|
|
|
|
|
|
|
peerInfo.postInit(addrs, protocols)
|
|
|
|
return peerInfo
|
|
|
|
|
|
|
|
proc publicKey*(p: PeerInfo): Option[PublicKey] =
|
|
|
|
var res = none(PublicKey)
|
2019-12-07 16:36:39 +00:00
|
|
|
if p.keyType == HasPublic:
|
|
|
|
if p.peerId.hasPublicKey():
|
|
|
|
var pubKey: PublicKey
|
|
|
|
if p.peerId.extractPublicKey(pubKey):
|
2021-05-21 16:27:01 +00:00
|
|
|
res = some(pubKey)
|
2019-12-07 16:36:39 +00:00
|
|
|
elif p.key.isSome:
|
2021-05-21 16:27:01 +00:00
|
|
|
res = p.key
|
2019-12-07 16:36:39 +00:00
|
|
|
else:
|
2021-09-02 10:03:40 +00:00
|
|
|
let pkeyRes = p.privateKey.getPublicKey()
|
2021-05-21 16:27:01 +00:00
|
|
|
if pkeyRes.isOk:
|
|
|
|
res = some(pkeyRes.get())
|
|
|
|
|
|
|
|
return res
|
2020-07-15 19:18:55 +00:00
|
|
|
|
2020-07-17 15:36:48 +00:00
|
|
|
func hash*(p: PeerInfo): Hash =
|
|
|
|
cast[pointer](p).hash
|