2024-06-28 10:34:57 +00:00
|
|
|
{.push raises: [].}
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
import
|
2024-05-16 20:29:11 +00:00
|
|
|
std/[options, sequtils, net],
|
2024-07-09 11:14:28 +00:00
|
|
|
stew/endians2,
|
|
|
|
results,
|
2023-04-17 13:21:20 +00:00
|
|
|
eth/keys,
|
|
|
|
libp2p/[multiaddress, multicodec],
|
|
|
|
libp2p/crypto/crypto
|
2024-03-15 23:08:47 +00:00
|
|
|
import ../common/enr
|
2023-04-17 13:21:20 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
const MultiaddrEnrField* = "multiaddrs"
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
func encodeMultiaddrs*(multiaddrs: seq[MultiAddress]): seq[byte] =
|
|
|
|
var buffer = newSeq[byte]()
|
|
|
|
for multiaddr in multiaddrs:
|
|
|
|
let
|
|
|
|
raw = multiaddr.data.buffer # binary encoded multiaddr
|
2024-03-15 23:08:47 +00:00
|
|
|
size = raw.len.uint16.toBytes(Endianness.bigEndian)
|
|
|
|
# size as Big Endian unsigned 16-bit integer
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
buffer.add(concat(@size, raw))
|
|
|
|
|
|
|
|
buffer
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
func readBytes(
|
|
|
|
rawBytes: seq[byte], numBytes: int, pos: var int = 0
|
|
|
|
): Result[seq[byte], cstring] =
|
2023-04-17 13:21:20 +00:00
|
|
|
## Attempts to read `numBytes` from a sequence, from
|
|
|
|
## position `pos`. Returns the requested slice or
|
|
|
|
## an error if `rawBytes` boundary is exceeded.
|
|
|
|
##
|
|
|
|
## If successful, `pos` is advanced by `numBytes`
|
2024-03-15 23:08:47 +00:00
|
|
|
if rawBytes[pos ..^ 1].len() < numBytes:
|
2023-04-17 13:21:20 +00:00
|
|
|
return err("insufficient bytes")
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let slicedSeq = rawBytes[pos ..< pos + numBytes]
|
2023-04-17 13:21:20 +00:00
|
|
|
pos += numBytes
|
|
|
|
|
|
|
|
return ok(slicedSeq)
|
|
|
|
|
|
|
|
func decodeMultiaddrs(buffer: seq[byte]): EnrResult[seq[MultiAddress]] =
|
|
|
|
## Parses a `multiaddrs` ENR field according to
|
|
|
|
## https://rfc.vac.dev/spec/31/
|
|
|
|
var multiaddrs: seq[MultiAddress]
|
|
|
|
|
|
|
|
var pos = 0
|
|
|
|
while pos < buffer.len():
|
2024-03-15 23:08:47 +00:00
|
|
|
let addrLenRaw = ?readBytes(buffer, 2, pos)
|
2023-04-17 13:21:20 +00:00
|
|
|
let addrLen = uint16.fromBytesBE(addrLenRaw)
|
|
|
|
if addrLen == 0:
|
|
|
|
# Ensure pos always advances and we don't get stuck in infinite loop
|
|
|
|
return err("malformed multiaddr field: invalid length")
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let addrRaw = ?readBytes(buffer, addrLen.int, pos)
|
2024-06-27 14:01:47 +00:00
|
|
|
let address = MultiAddress.init(addrRaw).valueOr:
|
|
|
|
continue # Not a valid multiaddress
|
2023-04-17 13:21:20 +00:00
|
|
|
|
|
|
|
multiaddrs.add(address)
|
|
|
|
|
|
|
|
return ok(multiaddrs)
|
|
|
|
|
|
|
|
# ENR builder extension
|
|
|
|
func stripPeerId(multiaddr: MultiAddress): MultiAddress =
|
|
|
|
if not multiaddr.contains(multiCodec("p2p")).get():
|
|
|
|
return multiaddr
|
|
|
|
|
|
|
|
var cleanAddr = MultiAddress.init()
|
|
|
|
for item in multiaddr.items:
|
|
|
|
if item.value.protoName().get() != "p2p":
|
|
|
|
# Add all parts except p2p peerId
|
|
|
|
discard cleanAddr.append(item.value)
|
|
|
|
|
|
|
|
return cleanAddr
|
|
|
|
|
|
|
|
func withMultiaddrs*(builder: var EnrBuilder, multiaddrs: seq[MultiAddress]) =
|
|
|
|
let multiaddrs = multiaddrs.map(stripPeerId)
|
|
|
|
let value = encodeMultiaddrs(multiaddrs)
|
|
|
|
builder.addFieldPair(MultiaddrEnrField, value)
|
|
|
|
|
|
|
|
func withMultiaddrs*(builder: var EnrBuilder, multiaddrs: varargs[MultiAddress]) =
|
|
|
|
withMultiaddrs(builder, @multiaddrs)
|
|
|
|
|
|
|
|
# ENR record accessors (e.g., Record, TypedRecord, etc.)
|
|
|
|
|
|
|
|
func multiaddrs*(record: TypedRecord): Option[seq[MultiAddress]] =
|
|
|
|
let field = record.tryGet(MultiaddrEnrField, seq[byte])
|
|
|
|
if field.isNone():
|
|
|
|
return none(seq[MultiAddress])
|
|
|
|
|
|
|
|
let decodeRes = decodeMultiaddrs(field.get())
|
|
|
|
if decodeRes.isErr():
|
|
|
|
return none(seq[MultiAddress])
|
|
|
|
|
|
|
|
some(decodeRes.value)
|