2024-01-13 01:41:57 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-01-31 17:40:00 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-01-31 17:40:00 +00:00
|
|
|
|
|
|
|
|
2022-12-13 18:22:36 +00:00
|
|
|
import
|
2024-01-13 01:41:57 +00:00
|
|
|
std/[hashes, json],
|
2022-01-31 17:40:00 +00:00
|
|
|
json_rpc/jsonmarshal,
|
2023-09-13 02:32:38 +00:00
|
|
|
stew/[byteutils, endians2],
|
2022-01-31 17:40:00 +00:00
|
|
|
eth/p2p/discoveryv5/node,
|
|
|
|
eth/utp/[utp_discv5_protocol, utp_router]
|
|
|
|
|
2024-01-13 01:41:57 +00:00
|
|
|
export jsonmarshal, json
|
2022-01-31 17:40:00 +00:00
|
|
|
|
|
|
|
type SKey* = object
|
|
|
|
id*: uint16
|
|
|
|
nodeId*: NodeId
|
|
|
|
|
2024-01-13 01:41:57 +00:00
|
|
|
proc writeValue*(w: var JsonWriter[JrpcConv], v: SKey)
|
|
|
|
{.gcsafe, raises: [IOError].} =
|
|
|
|
let hex = v.nodeId.toBytesBE().toHex()
|
|
|
|
let numId = v.id.toBytesBE().toHex()
|
2022-01-31 17:40:00 +00:00
|
|
|
let finalStr = hex & numId
|
2024-01-13 01:41:57 +00:00
|
|
|
w.writeValue(finalStr)
|
|
|
|
|
|
|
|
proc readValue*(r: var JsonReader[JrpcConv], val: var SKey)
|
|
|
|
{.gcsafe, raises: [IOError, JsonReaderError].} =
|
|
|
|
let str = r.parseString()
|
|
|
|
if str.len < 64:
|
|
|
|
r.raiseUnexpectedValue("SKey: too short string")
|
|
|
|
|
|
|
|
try:
|
2022-01-31 17:40:00 +00:00
|
|
|
let nodeIdStr = str.substr(0, 63)
|
|
|
|
let connIdStr = str.substr(64)
|
|
|
|
let nodeId = NodeId.fromHex(nodeIdStr)
|
|
|
|
let connId = uint16.fromBytesBE(connIdStr.hexToSeqByte())
|
2024-01-13 01:41:57 +00:00
|
|
|
val = SKey(nodeId: nodeId, id: connId)
|
|
|
|
except ValueError as exc:
|
|
|
|
r.raiseUnexpectedValue("Skey parser error: " & exc.msg)
|
2022-01-31 17:40:00 +00:00
|
|
|
|
|
|
|
proc hash*(x: SKey): Hash =
|
|
|
|
var h = 0
|
|
|
|
h = h !& x.id.hash
|
|
|
|
h = h !& x.nodeId.hash
|
|
|
|
!$h
|
|
|
|
|
|
|
|
func toSKey*(k: UtpSocketKey[NodeAddress]): SKey =
|
|
|
|
SKey(id: k.rcvId, nodeId: k.remoteAddress.nodeId)
|