2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +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.
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2022-07-01 18:19:57 +00:00
|
|
|
{.push public.}
|
2020-08-08 06:52:20 +00:00
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
import std/sequtils
|
2021-05-22 18:27:30 +00:00
|
|
|
import pkg/[chronos, chronicles, stew/results]
|
2022-10-30 07:49:02 +00:00
|
|
|
import peerid, multiaddress, multicodec, crypto/crypto, routing_record, errors, utility
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2022-03-14 08:39:30 +00:00
|
|
|
export peerid, multiaddress, crypto, routing_record, errors, results
|
2020-07-01 07:22:29 +00:00
|
|
|
|
2021-09-08 09:07:46 +00:00
|
|
|
## Our local peer info
|
2019-12-07 16:36:39 +00:00
|
|
|
|
|
|
|
type
|
2022-10-20 10:22:28 +00:00
|
|
|
PeerInfoError* = object of LPError
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
AddressMapper* = proc(listenAddrs: seq[MultiAddress]): Future[seq[MultiAddress]] {.
|
|
|
|
gcsafe, raises: []
|
|
|
|
.} ## A proc that expected to resolve the listen addresses into dialable addresses
|
2021-05-22 18:27:30 +00:00
|
|
|
|
2022-07-01 18:19:57 +00:00
|
|
|
PeerInfo* {.public.} = ref object
|
2021-12-16 10:05:20 +00:00
|
|
|
peerId*: PeerId
|
2022-10-20 10:22:28 +00:00
|
|
|
listenAddrs*: seq[MultiAddress]
|
2024-06-06 11:05:45 +00:00
|
|
|
## contains addresses the node listens on, which may include wildcard and private addresses (not directly reachable).
|
|
|
|
addrs*: seq[MultiAddress]
|
|
|
|
## contains resolved addresses that other peers can use to connect, including public-facing NAT and port-forwarded addresses.
|
2022-10-20 10:22:28 +00:00
|
|
|
addressMappers*: seq[AddressMapper]
|
2024-06-06 11:05:45 +00:00
|
|
|
## contains a list of procs that can be used to resolve the listen addresses into dialable addresses.
|
2019-12-04 04:44:54 +00:00
|
|
|
protocols*: seq[string]
|
2020-06-09 18:42:52 +00:00
|
|
|
protoVersion*: string
|
|
|
|
agentVersion*: string
|
2021-09-08 09:07:46 +00:00
|
|
|
privateKey*: PrivateKey
|
|
|
|
publicKey*: PublicKey
|
2022-04-13 07:35:28 +00:00
|
|
|
signedPeerRecord*: SignedPeerRecord
|
2019-12-07 16:36:39 +00:00
|
|
|
|
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,
|
2022-10-20 10:22:28 +00:00
|
|
|
listenAddrs: mapIt(p.listenAddrs, $it),
|
2020-06-09 18:42:52 +00:00
|
|
|
addrs: mapIt(p.addrs, $it),
|
|
|
|
protocols: mapIt(p.protocols, $it),
|
|
|
|
protoVersion: p.protoVersion,
|
|
|
|
agentVersion: p.agentVersion,
|
|
|
|
)
|
2024-06-11 15:18:06 +00:00
|
|
|
chronicles.formatIt(PeerInfo):
|
|
|
|
shortLog(it)
|
2020-05-11 19:05:24 +00:00
|
|
|
|
2022-10-20 10:22:28 +00:00
|
|
|
proc update*(p: PeerInfo) {.async.} =
|
2024-06-06 11:05:45 +00:00
|
|
|
# p.addrs.len == 0 overrides addrs only if it is the first time update is being executed or if the field is empty.
|
|
|
|
# p.addressMappers.len == 0 is for when all addressMappers have been removed,
|
|
|
|
# and we wish to have addrs in its initial state, i.e., a copy of listenAddrs.
|
|
|
|
if p.addrs.len == 0 or p.addressMappers.len == 0:
|
|
|
|
p.addrs = p.listenAddrs
|
2022-10-20 10:22:28 +00:00
|
|
|
for mapper in p.addressMappers:
|
|
|
|
p.addrs = await mapper(p.addrs)
|
|
|
|
|
2023-06-28 14:44:58 +00:00
|
|
|
p.signedPeerRecord = SignedPeerRecord.init(
|
2024-06-11 15:18:06 +00:00
|
|
|
p.privateKey, PeerRecord.init(p.peerId, p.addrs)
|
|
|
|
).valueOr:
|
2023-06-28 14:44:58 +00:00
|
|
|
info "Can't update the signed peer record"
|
|
|
|
return
|
2022-04-13 07:35:28 +00:00
|
|
|
|
2022-10-20 10:22:28 +00:00
|
|
|
proc addrs*(p: PeerInfo): seq[MultiAddress] =
|
|
|
|
p.addrs
|
|
|
|
|
2022-10-30 07:49:02 +00:00
|
|
|
proc fullAddrs*(p: PeerInfo): MaResult[seq[MultiAddress]] =
|
2024-06-11 15:18:06 +00:00
|
|
|
let peerIdPart = ?MultiAddress.init(multiCodec("p2p"), p.peerId.data)
|
2022-10-30 07:49:02 +00:00
|
|
|
var res: seq[MultiAddress]
|
|
|
|
for address in p.addrs:
|
2024-06-11 15:18:06 +00:00
|
|
|
res.add(?concat(address, peerIdPart))
|
2022-10-30 07:49:02 +00:00
|
|
|
ok(res)
|
|
|
|
|
|
|
|
proc parseFullAddress*(ma: MultiAddress): MaResult[(PeerId, MultiAddress)] =
|
2024-06-11 15:18:06 +00:00
|
|
|
let p2pPart = ?ma[^1]
|
|
|
|
if ?p2pPart.protoCode != multiCodec("p2p"):
|
2022-10-30 07:49:02 +00:00
|
|
|
return err("Missing p2p part from multiaddress!")
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
let res =
|
|
|
|
(?PeerId.init(?p2pPart.protoArgument()).orErr("invalid peerid"), ?ma[0 .. ^2])
|
2022-10-30 07:49:02 +00:00
|
|
|
ok(res)
|
|
|
|
|
|
|
|
proc parseFullAddress*(ma: string | seq[byte]): MaResult[(PeerId, MultiAddress)] =
|
2024-06-11 15:18:06 +00:00
|
|
|
parseFullAddress(?MultiAddress.init(ma))
|
2022-10-30 07:49:02 +00:00
|
|
|
|
2021-10-25 08:26:32 +00:00
|
|
|
proc new*(
|
2024-06-11 15:18:06 +00:00
|
|
|
p: typedesc[PeerInfo],
|
|
|
|
key: PrivateKey,
|
|
|
|
listenAddrs: openArray[MultiAddress] = [],
|
|
|
|
protocols: openArray[string] = [],
|
|
|
|
protoVersion: string = "",
|
|
|
|
agentVersion: string = "",
|
|
|
|
addressMappers = newSeq[AddressMapper](),
|
|
|
|
): PeerInfo {.raises: [LPError].} =
|
|
|
|
let pubkey =
|
|
|
|
try:
|
2021-10-25 08:26:32 +00:00
|
|
|
key.getPublicKey().tryGet()
|
2021-09-08 09:07:46 +00:00
|
|
|
except CatchableError:
|
|
|
|
raise newException(PeerInfoError, "invalid private key")
|
2022-10-20 10:22:28 +00:00
|
|
|
|
2022-07-27 11:47:50 +00:00
|
|
|
let peerId = PeerId.init(key).tryGet()
|
2022-03-14 08:39:30 +00:00
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
let peerInfo = PeerInfo(
|
2022-03-14 08:39:30 +00:00
|
|
|
peerId: peerId,
|
2021-09-08 09:07:46 +00:00
|
|
|
publicKey: pubkey,
|
|
|
|
privateKey: key,
|
2021-05-21 16:27:01 +00:00
|
|
|
protoVersion: protoVersion,
|
2021-09-08 09:07:46 +00:00
|
|
|
agentVersion: agentVersion,
|
2022-10-20 10:22:28 +00:00
|
|
|
listenAddrs: @listenAddrs,
|
2022-03-14 08:39:30 +00:00
|
|
|
protocols: @protocols,
|
2024-06-11 15:18:06 +00:00
|
|
|
addressMappers: addressMappers,
|
2022-04-13 07:35:28 +00:00
|
|
|
)
|
|
|
|
|
2021-05-21 16:27:01 +00:00
|
|
|
return peerInfo
|