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-06-09 18:42:52 +00:00
|
|
|
import options, sequtils
|
2020-05-11 19:05:24 +00:00
|
|
|
import chronos, chronicles
|
2020-07-01 06:25:09 +00:00
|
|
|
import peerid, multiaddress, crypto/crypto
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2020-07-01 07:22:29 +00:00
|
|
|
export peerid, multiaddress, crypto
|
|
|
|
|
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
|
|
|
|
|
|
|
|
PeerInfo* = ref object of RootObj
|
|
|
|
peerId*: PeerID
|
2019-12-04 04:44:54 +00:00
|
|
|
addrs*: seq[MultiAddress]
|
|
|
|
protocols*: seq[string]
|
2020-02-11 17:53:39 +00:00
|
|
|
lifefut: Future[void]
|
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-05-11 19:05:24 +00:00
|
|
|
proc id*(p: PeerInfo): string =
|
2020-06-29 15:15:31 +00:00
|
|
|
if not(isNil(p)):
|
|
|
|
return p.peerId.pretty()
|
2020-05-11 19:05:24 +00:00
|
|
|
|
2020-06-11 14:45:59 +00:00
|
|
|
proc `$`*(p: PeerInfo): string = p.id
|
|
|
|
|
2020-06-09 18:42:52 +00:00
|
|
|
proc shortLog*(p: PeerInfo): auto =
|
|
|
|
(
|
|
|
|
id: p.id(),
|
|
|
|
addrs: mapIt(p.addrs, $it),
|
|
|
|
protocols: mapIt(p.protocols, $it),
|
|
|
|
protoVersion: p.protoVersion,
|
|
|
|
agentVersion: p.agentVersion,
|
|
|
|
)
|
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
|
|
|
|
peerinfo.lifefut = newFuture[void]("libp2p.peerinfo.lifetime")
|
|
|
|
|
2020-06-11 14:45:59 +00:00
|
|
|
proc init*(p: typedesc[PeerInfo],
|
|
|
|
key: PrivateKey,
|
2020-02-11 17:53:39 +00:00
|
|
|
addrs: openarray[MultiAddress] = [],
|
|
|
|
protocols: openarray[string] = []): PeerInfo {.inline.} =
|
2020-07-01 06:25:09 +00:00
|
|
|
result = PeerInfo(keyType: HasPrivate, peerId: PeerID.init(key).tryGet(),
|
2020-02-11 17:53:39 +00:00
|
|
|
privateKey: key)
|
|
|
|
result.postInit(addrs, protocols)
|
|
|
|
|
2020-06-11 14:45:59 +00:00
|
|
|
proc init*(p: typedesc[PeerInfo],
|
|
|
|
peerId: PeerID,
|
2020-02-11 17:53:39 +00:00
|
|
|
addrs: openarray[MultiAddress] = [],
|
|
|
|
protocols: openarray[string] = []): PeerInfo {.inline.} =
|
|
|
|
result = PeerInfo(keyType: HasPublic, peerId: peerId)
|
|
|
|
result.postInit(addrs, protocols)
|
|
|
|
|
2020-06-11 14:45:59 +00:00
|
|
|
proc init*(p: typedesc[PeerInfo],
|
|
|
|
peerId: string,
|
2020-02-11 17:53:39 +00:00
|
|
|
addrs: openarray[MultiAddress] = [],
|
|
|
|
protocols: openarray[string] = []): PeerInfo {.inline.} =
|
2020-07-01 06:25:09 +00:00
|
|
|
result = PeerInfo(keyType: HasPublic, peerId: PeerID.init(peerId).tryGet())
|
2020-02-11 17:53:39 +00:00
|
|
|
result.postInit(addrs, protocols)
|
|
|
|
|
2020-06-11 14:45:59 +00:00
|
|
|
proc init*(p: typedesc[PeerInfo],
|
|
|
|
key: PublicKey,
|
2020-02-11 17:53:39 +00:00
|
|
|
addrs: openarray[MultiAddress] = [],
|
|
|
|
protocols: openarray[string] = []): PeerInfo {.inline.} =
|
2020-06-11 14:45:59 +00:00
|
|
|
result = PeerInfo(keyType: HasPublic,
|
2020-07-01 06:25:09 +00:00
|
|
|
peerId: PeerID.init(key).tryGet(),
|
2020-02-11 17:53:39 +00:00
|
|
|
key: some(key))
|
2020-06-11 14:45:59 +00:00
|
|
|
|
2020-02-11 17:53:39 +00:00
|
|
|
result.postInit(addrs, protocols)
|
|
|
|
|
|
|
|
proc close*(p: PeerInfo) {.inline.} =
|
2020-05-11 19:05:24 +00:00
|
|
|
if not p.lifefut.finished:
|
|
|
|
p.lifefut.complete()
|
|
|
|
else:
|
|
|
|
# TODO this should ideally not happen
|
|
|
|
notice "Closing closed peer", peer = p.id
|
2020-02-11 17:53:39 +00:00
|
|
|
|
|
|
|
proc join*(p: PeerInfo): Future[void] {.inline.} =
|
|
|
|
var retFuture = newFuture[void]()
|
|
|
|
proc continuation(udata: pointer) {.gcsafe.} =
|
|
|
|
if not(retFuture.finished()):
|
|
|
|
retFuture.complete()
|
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
|
|
|
p.lifefut.removeCallback(continuation)
|
|
|
|
if p.lifefut.finished:
|
|
|
|
retFuture.complete()
|
|
|
|
else:
|
|
|
|
p.lifefut.addCallback(continuation)
|
|
|
|
retFuture.cancelCallback = cancellation
|
|
|
|
return retFuture
|
|
|
|
|
|
|
|
proc isClosed*(p: PeerInfo): bool {.inline.} =
|
|
|
|
result = p.lifefut.finished()
|
|
|
|
|
|
|
|
proc lifeFuture*(p: PeerInfo): Future[void] {.inline.} =
|
|
|
|
result = p.lifefut
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
proc publicKey*(p: PeerInfo): Option[PublicKey] {.inline.} =
|
|
|
|
if p.keyType == HasPublic:
|
|
|
|
if p.peerId.hasPublicKey():
|
|
|
|
var pubKey: PublicKey
|
|
|
|
if p.peerId.extractPublicKey(pubKey):
|
|
|
|
result = some(pubKey)
|
|
|
|
elif p.key.isSome:
|
|
|
|
result = p.key
|
|
|
|
else:
|
2020-05-18 05:25:55 +00:00
|
|
|
result = some(p.privateKey.getKey().tryGet())
|