2024-06-28 16:04:57 +05:30
|
|
|
{.push raises: [].}
|
2023-03-21 17:27:51 +01:00
|
|
|
|
|
|
|
|
import
|
2024-05-16 22:29:11 +02:00
|
|
|
std/[options, net],
|
2024-07-09 13:14:28 +02:00
|
|
|
results,
|
2023-03-21 17:27:51 +01:00
|
|
|
eth/keys as eth_keys,
|
|
|
|
|
eth/p2p/discoveryv5/enr,
|
|
|
|
|
libp2p/crypto/crypto as libp2p_crypto
|
|
|
|
|
|
2025-01-28 10:04:34 +01:00
|
|
|
import ./typed_record
|
|
|
|
|
|
2023-03-21 17:27:51 +01:00
|
|
|
## Builder
|
|
|
|
|
|
|
|
|
|
type EnrBuilder* = object
|
2024-03-16 00:08:47 +01:00
|
|
|
seqNumber: uint64
|
|
|
|
|
privateKey: eth_keys.PrivateKey
|
2025-01-28 10:04:34 +01:00
|
|
|
ipAddress: Opt[IpAddress]
|
|
|
|
|
tcpPort: Opt[Port]
|
|
|
|
|
udpPort: Opt[Port]
|
2024-03-16 00:08:47 +01:00
|
|
|
fields: seq[FieldPair]
|
2023-03-21 17:27:51 +01:00
|
|
|
|
|
|
|
|
proc init*(T: type EnrBuilder, key: eth_keys.PrivateKey, seqNum: uint64 = 1): T =
|
2025-01-28 10:04:34 +01:00
|
|
|
EnrBuilder(
|
|
|
|
|
seqNumber: seqNum,
|
|
|
|
|
privateKey: key,
|
|
|
|
|
ipAddress: Opt.none(IpAddress),
|
|
|
|
|
tcpPort: Opt.none(Port),
|
|
|
|
|
udpPort: Opt.none(Port),
|
|
|
|
|
fields: newSeq[FieldPair](),
|
|
|
|
|
)
|
2023-03-21 17:27:51 +01:00
|
|
|
|
|
|
|
|
proc init*(T: type EnrBuilder, key: libp2p_crypto.PrivateKey, seqNum: uint64 = 1): T =
|
|
|
|
|
# TODO: Inconvenient runtime assertion. Move this assertion to compile time
|
|
|
|
|
if key.scheme != PKScheme.Secp256k1:
|
|
|
|
|
raise newException(Defect, "invalid private key scheme")
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
bytes = key.getRawBytes().expect("Private key is valid")
|
2024-03-16 00:08:47 +01:00
|
|
|
privateKey =
|
|
|
|
|
eth_keys.PrivateKey.fromRaw(bytes).expect("Raw private key is of valid length")
|
2023-03-21 17:27:51 +01:00
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
EnrBuilder.init(key = privateKey, seqNum = seqNum)
|
2023-03-21 17:27:51 +01:00
|
|
|
|
|
|
|
|
proc addFieldPair*(builder: var EnrBuilder, pair: FieldPair) =
|
|
|
|
|
builder.fields.add(pair)
|
|
|
|
|
|
|
|
|
|
proc addFieldPair*[V](builder: var EnrBuilder, key: string, value: V) =
|
|
|
|
|
builder.addFieldPair(toFieldPair(key, value))
|
|
|
|
|
|
|
|
|
|
proc build*(builder: EnrBuilder): EnrResult[enr.Record] =
|
|
|
|
|
# Note that nim-eth's `Record.init` does not deduplicate the field pairs.
|
|
|
|
|
# See: https://github.com/status-im/nim-eth/blob/4b22fcd/eth/p2p/discoveryv5/enr.nim#L143-L144
|
|
|
|
|
enr.Record.init(
|
|
|
|
|
seqNum = builder.seqNumber,
|
|
|
|
|
pk = builder.privateKey,
|
2025-01-28 10:04:34 +01:00
|
|
|
ip = builder.ipAddress,
|
|
|
|
|
tcpPort = builder.tcpPort,
|
|
|
|
|
udpPort = builder.udpPort,
|
2024-03-16 00:08:47 +01:00
|
|
|
extraFields = builder.fields,
|
2023-03-21 17:27:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
## Builder extension: IP address and TCP/UDP ports
|
|
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
proc addAddressAndPorts(
|
|
|
|
|
builder: var EnrBuilder, ip: IpAddress, tcpPort, udpPort: Option[Port]
|
|
|
|
|
) =
|
2025-01-28 10:04:34 +01:00
|
|
|
builder.ipAddress = Opt.some(ip)
|
|
|
|
|
builder.tcpPort = tcpPort.toOpt()
|
|
|
|
|
builder.udpPort = udpPort.toOpt()
|
2023-03-21 17:27:51 +01:00
|
|
|
|
|
|
|
|
proc addPorts(builder: var EnrBuilder, tcp, udp: Option[Port]) =
|
|
|
|
|
# Based on: https://github.com/status-im/nim-eth/blob/4b22fcd/eth/p2p/discoveryv5/enr.nim#L166
|
2025-01-28 10:04:34 +01:00
|
|
|
builder.tcpPort = tcp.toOpt()
|
|
|
|
|
builder.udpPort = udp.toOpt()
|
2023-03-21 17:27:51 +01:00
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
proc withIpAddressAndPorts*(
|
|
|
|
|
builder: var EnrBuilder,
|
|
|
|
|
ipAddr = none(IpAddress),
|
|
|
|
|
tcpPort = none(Port),
|
|
|
|
|
udpPort = none(Port),
|
|
|
|
|
) =
|
2023-03-21 17:27:51 +01:00
|
|
|
if ipAddr.isSome():
|
|
|
|
|
addAddressAndPorts(builder, ipAddr.get(), tcpPort, udpPort)
|
|
|
|
|
else:
|
|
|
|
|
addPorts(builder, tcpPort, udpPort)
|