2020-07-07 15:19:15 +00:00
|
|
|
# ENR implementation according to specification in EIP-778:
|
2019-12-18 11:06:45 +00:00
|
|
|
# https://github.com/ethereum/EIPs/blob/master/EIPS/eip-778.md
|
|
|
|
|
2020-02-11 16:25:31 +00:00
|
|
|
import
|
2020-06-05 14:10:15 +00:00
|
|
|
strutils, macros, algorithm, options,
|
2020-06-09 09:09:35 +00:00
|
|
|
stew/shims/net, nimcrypto, stew/base64,
|
2020-05-28 08:19:36 +00:00
|
|
|
eth/[rlp, keys]
|
2019-12-10 18:34:57 +00:00
|
|
|
|
2020-04-06 16:24:15 +00:00
|
|
|
export options
|
|
|
|
|
2020-04-29 22:11:03 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2019-12-18 11:06:45 +00:00
|
|
|
const
|
2020-07-07 15:19:15 +00:00
|
|
|
maxEnrSize = 300 ## Maximum size of an encoded node record, in bytes.
|
|
|
|
minRlpListLen = 4 ## Minimum node record RLP list has: signature, seqId,
|
|
|
|
## "id" key and value.
|
2019-12-18 11:06:45 +00:00
|
|
|
|
2019-12-10 18:34:57 +00:00
|
|
|
type
|
2020-04-15 02:32:52 +00:00
|
|
|
FieldPair* = (string, Field)
|
|
|
|
|
2019-12-10 18:34:57 +00:00
|
|
|
Record* = object
|
2020-02-11 16:25:31 +00:00
|
|
|
seqNum*: uint64
|
2019-12-10 18:34:57 +00:00
|
|
|
# signature: seq[byte]
|
|
|
|
raw*: seq[byte] # RLP encoded record
|
2020-04-15 02:32:52 +00:00
|
|
|
pairs: seq[FieldPair] # sorted list of all key/value pairs
|
2019-12-10 18:34:57 +00:00
|
|
|
|
2019-12-17 21:20:26 +00:00
|
|
|
EnrUri* = distinct string
|
|
|
|
|
2019-12-18 01:10:09 +00:00
|
|
|
TypedRecord* = object
|
|
|
|
id*: string
|
|
|
|
secp256k1*: Option[array[33, byte]]
|
|
|
|
ip*: Option[array[4, byte]]
|
|
|
|
ip6*: Option[array[16, byte]]
|
|
|
|
tcp*: Option[int]
|
|
|
|
udp*: Option[int]
|
|
|
|
tcp6*: Option[int]
|
|
|
|
udp6*: Option[int]
|
|
|
|
|
2019-12-10 18:34:57 +00:00
|
|
|
FieldKind = enum
|
|
|
|
kString,
|
|
|
|
kNum,
|
|
|
|
kBytes
|
|
|
|
|
|
|
|
Field = object
|
|
|
|
case kind: FieldKind
|
|
|
|
of kString:
|
|
|
|
str: string
|
|
|
|
of kNum:
|
2020-02-11 16:25:31 +00:00
|
|
|
num: BiggestUInt
|
2019-12-10 18:34:57 +00:00
|
|
|
of kBytes:
|
|
|
|
bytes: seq[byte]
|
|
|
|
|
2020-04-29 22:11:03 +00:00
|
|
|
EnrResult*[T] = Result[T, cstring]
|
|
|
|
|
2019-12-10 18:34:57 +00:00
|
|
|
template toField[T](v: T): Field =
|
|
|
|
when T is string:
|
|
|
|
Field(kind: kString, str: v)
|
2020-02-11 16:25:31 +00:00
|
|
|
elif T is array:
|
|
|
|
Field(kind: kBytes, bytes: @v)
|
2019-12-10 18:34:57 +00:00
|
|
|
elif T is seq[byte]:
|
|
|
|
Field(kind: kBytes, bytes: v)
|
2020-02-11 16:25:31 +00:00
|
|
|
elif T is SomeUnsignedInt:
|
|
|
|
Field(kind: kNum, num: BiggestUInt(v))
|
2019-12-10 18:34:57 +00:00
|
|
|
else:
|
|
|
|
{.error: "Unsupported field type".}
|
|
|
|
|
2020-07-07 15:19:15 +00:00
|
|
|
proc `==`(a, b: Field): bool =
|
|
|
|
if a.kind == b.kind:
|
|
|
|
case a.kind
|
|
|
|
of kString:
|
|
|
|
return a.str == b.str
|
|
|
|
of kNum:
|
|
|
|
return a.num == b.num
|
|
|
|
of kBytes:
|
|
|
|
return a.bytes == b.bytes
|
|
|
|
else:
|
|
|
|
return false
|
2019-12-10 18:34:57 +00:00
|
|
|
|
2020-07-08 10:14:00 +00:00
|
|
|
proc cmp(a, b: FieldPair): int = cmp(a[0], b[0])
|
|
|
|
|
2020-07-07 15:19:15 +00:00
|
|
|
proc makeEnrRaw(seqNum: uint64, pk: PrivateKey,
|
|
|
|
pairs: openarray[FieldPair]): EnrResult[seq[byte]] =
|
2020-04-29 22:11:03 +00:00
|
|
|
proc append(w: var RlpWriter, seqNum: uint64,
|
2020-07-07 15:19:15 +00:00
|
|
|
pairs: openarray[FieldPair]): seq[byte] =
|
2020-02-11 16:25:31 +00:00
|
|
|
w.append(seqNum)
|
2019-12-10 18:34:57 +00:00
|
|
|
for (k, v) in pairs:
|
|
|
|
w.append(k)
|
|
|
|
case v.kind
|
|
|
|
of kString: w.append(v.str)
|
|
|
|
of kNum: w.append(v.num)
|
|
|
|
of kBytes: w.append(v.bytes)
|
|
|
|
w.finish()
|
|
|
|
|
|
|
|
let toSign = block:
|
2020-07-07 15:19:15 +00:00
|
|
|
var w = initRlpList(pairs.len * 2 + 1)
|
|
|
|
w.append(seqNum, pairs)
|
2020-04-29 22:11:03 +00:00
|
|
|
|
2020-06-22 16:07:48 +00:00
|
|
|
let sig = signNR(pk, toSign)
|
2019-12-10 18:34:57 +00:00
|
|
|
|
2020-07-07 15:19:15 +00:00
|
|
|
var raw = block:
|
|
|
|
var w = initRlpList(pairs.len * 2 + 2)
|
2020-04-29 22:11:03 +00:00
|
|
|
w.append(sig.toRaw())
|
2020-07-07 15:19:15 +00:00
|
|
|
w.append(seqNum, pairs)
|
|
|
|
|
|
|
|
if raw.len > maxEnrSize:
|
|
|
|
err("Record exceeds maximum size")
|
|
|
|
else:
|
|
|
|
ok(raw)
|
2019-12-10 18:34:57 +00:00
|
|
|
|
2020-07-07 15:19:15 +00:00
|
|
|
proc makeEnrAux(seqNum: uint64, pk: PrivateKey,
|
|
|
|
pairs: openarray[FieldPair]): EnrResult[Record] =
|
|
|
|
var record: Record
|
|
|
|
record.pairs = @pairs
|
|
|
|
record.seqNum = seqNum
|
|
|
|
|
|
|
|
let pubkey = pk.toPublicKey()
|
|
|
|
|
|
|
|
record.pairs.add(("id", Field(kind: kString, str: "v4")))
|
|
|
|
record.pairs.add(("secp256k1",
|
|
|
|
Field(kind: kBytes, bytes: @(pubkey.toRawCompressed()))))
|
|
|
|
|
|
|
|
# Sort by key
|
2020-07-08 10:14:00 +00:00
|
|
|
record.pairs.sort(cmp)
|
|
|
|
# TODO: Should deduplicate on keys here also. Should we error on that or just
|
|
|
|
# deal with it?
|
2020-07-07 15:19:15 +00:00
|
|
|
|
|
|
|
record.raw = ? makeEnrRaw(seqNum, pk, record.pairs)
|
2020-04-29 22:11:03 +00:00
|
|
|
ok(record)
|
2019-12-10 18:34:57 +00:00
|
|
|
|
2020-04-29 22:11:03 +00:00
|
|
|
macro initRecord*(seqNum: uint64, pk: PrivateKey,
|
|
|
|
pairs: untyped{nkTableConstr}): untyped =
|
2020-07-07 15:19:15 +00:00
|
|
|
## Initialize a `Record` with given sequence number, private key and k:v
|
|
|
|
## pairs.
|
|
|
|
##
|
|
|
|
## Can fail in case the record exceeds the `maxEnrSize`.
|
2019-12-10 18:34:57 +00:00
|
|
|
for c in pairs:
|
|
|
|
c.expectKind(nnkExprColonExpr)
|
|
|
|
c[1] = newCall(bindSym"toField", c[1])
|
|
|
|
|
|
|
|
result = quote do:
|
2020-02-11 16:25:31 +00:00
|
|
|
makeEnrAux(`seqNum`, `pk`, `pairs`)
|
|
|
|
|
2020-04-15 02:32:52 +00:00
|
|
|
template toFieldPair*(key: string, value: auto): FieldPair =
|
|
|
|
(key, toField(value))
|
|
|
|
|
2020-07-08 10:14:00 +00:00
|
|
|
proc addAddress(fields: var seq[FieldPair], ip: Option[ValidIpAddress],
|
|
|
|
tcpPort, udpPort: Port) =
|
2020-03-30 11:21:32 +00:00
|
|
|
if ip.isSome():
|
2020-03-20 15:38:46 +00:00
|
|
|
let
|
2020-03-30 11:21:32 +00:00
|
|
|
ipExt = ip.get()
|
|
|
|
isV6 = ipExt.family == IPv6
|
2020-03-20 15:38:46 +00:00
|
|
|
|
2020-04-15 02:32:52 +00:00
|
|
|
fields.add(if isV6: ("ip6", ipExt.address_v6.toField)
|
|
|
|
else: ("ip", ipExt.address_v4.toField))
|
|
|
|
fields.add(((if isV6: "tcp6" else: "tcp"), tcpPort.uint16.toField))
|
|
|
|
fields.add(((if isV6: "udp6" else: "udp"), udpPort.uint16.toField))
|
2020-03-20 15:38:46 +00:00
|
|
|
else:
|
2020-04-15 02:32:52 +00:00
|
|
|
fields.add(("tcp", tcpPort.uint16.toField))
|
|
|
|
fields.add(("udp", udpPort.uint16.toField))
|
|
|
|
|
2020-07-08 10:14:00 +00:00
|
|
|
proc init*(T: type Record, seqNum: uint64,
|
|
|
|
pk: PrivateKey,
|
|
|
|
ip: Option[ValidIpAddress],
|
|
|
|
tcpPort, udpPort: Port,
|
|
|
|
extraFields: openarray[FieldPair] = []):
|
|
|
|
EnrResult[T] =
|
|
|
|
## Initialize a `Record` with given sequence number, private key, optional
|
2020-07-08 12:56:56 +00:00
|
|
|
## ip address, tcp port, udp port, and optional custom k:v pairs.
|
2020-07-08 10:14:00 +00:00
|
|
|
##
|
|
|
|
## Can fail in case the record exceeds the `maxEnrSize`.
|
|
|
|
var fields = newSeq[FieldPair]()
|
|
|
|
|
|
|
|
fields.addAddress(ip, tcpPort, udpPort)
|
2020-04-15 02:32:52 +00:00
|
|
|
fields.add extraFields
|
|
|
|
makeEnrAux(seqNum, pk, fields)
|
2019-12-10 18:34:57 +00:00
|
|
|
|
2020-05-01 20:34:26 +00:00
|
|
|
proc getField(r: Record, name: string, field: var Field): bool =
|
2019-12-10 18:34:57 +00:00
|
|
|
# It might be more correct to do binary search,
|
|
|
|
# as the fields are sorted, but it's unlikely to
|
|
|
|
# make any difference in reality.
|
|
|
|
for (k, v) in r.pairs:
|
|
|
|
if k == name:
|
|
|
|
field = v
|
|
|
|
return true
|
|
|
|
|
2020-04-29 22:11:03 +00:00
|
|
|
proc requireKind(f: Field, kind: FieldKind) {.raises: [ValueError].} =
|
2019-12-10 18:34:57 +00:00
|
|
|
if f.kind != kind:
|
|
|
|
raise newException(ValueError, "Wrong field kind")
|
|
|
|
|
2020-05-01 20:34:26 +00:00
|
|
|
proc get*(r: Record, key: string, T: type): T {.raises: [ValueError, Defect].} =
|
2019-12-10 18:34:57 +00:00
|
|
|
var f: Field
|
|
|
|
if r.getField(key, f):
|
2019-12-18 01:10:09 +00:00
|
|
|
when T is SomeInteger:
|
2019-12-10 18:34:57 +00:00
|
|
|
requireKind(f, kNum)
|
2019-12-18 01:10:09 +00:00
|
|
|
return T(f.num)
|
|
|
|
elif T is seq[byte]:
|
2019-12-10 18:34:57 +00:00
|
|
|
requireKind(f, kBytes)
|
|
|
|
return f.bytes
|
2019-12-18 01:10:09 +00:00
|
|
|
elif T is string:
|
2019-12-10 18:34:57 +00:00
|
|
|
requireKind(f, kString)
|
|
|
|
return f.str
|
2020-02-17 22:47:13 +00:00
|
|
|
elif T is PublicKey:
|
|
|
|
requireKind(f, kBytes)
|
2020-04-04 16:44:01 +00:00
|
|
|
let pk = PublicKey.fromRaw(f.bytes)
|
|
|
|
if pk.isErr:
|
2020-02-17 22:47:13 +00:00
|
|
|
raise newException(ValueError, "Invalid public key")
|
2020-04-04 16:44:01 +00:00
|
|
|
return pk[]
|
2019-12-18 01:10:09 +00:00
|
|
|
elif T is array:
|
|
|
|
when type(result[0]) is byte:
|
|
|
|
requireKind(f, kBytes)
|
|
|
|
if f.bytes.len != result.len:
|
|
|
|
raise newException(ValueError, "Invalid byte blob length")
|
|
|
|
copyMem(addr result[0], addr f.bytes[0], result.len)
|
|
|
|
else:
|
|
|
|
{.fatal: "Unsupported output type in enr.get".}
|
|
|
|
else:
|
|
|
|
{.fatal: "Unsupported output type in enr.get".}
|
2019-12-13 17:29:42 +00:00
|
|
|
else:
|
|
|
|
raise newException(KeyError, "Key not found in ENR: " & key)
|
2019-12-10 18:34:57 +00:00
|
|
|
|
2020-05-01 20:34:26 +00:00
|
|
|
proc get*(r: Record, T: type PublicKey): Option[T] =
|
2019-12-10 18:34:57 +00:00
|
|
|
var pubkeyField: Field
|
|
|
|
if r.getField("secp256k1", pubkeyField) and pubkeyField.kind == kBytes:
|
2020-04-04 16:44:01 +00:00
|
|
|
let pk = PublicKey.fromRaw(pubkeyField.bytes)
|
|
|
|
if pk.isOk:
|
2020-04-06 16:24:15 +00:00
|
|
|
return some pk[]
|
2019-12-10 18:34:57 +00:00
|
|
|
|
2020-07-07 15:19:15 +00:00
|
|
|
proc find(r: Record, key: string): Option[int] =
|
|
|
|
## Search for key in record key:value pairs.
|
|
|
|
##
|
|
|
|
## Returns some(index of key) if key is found in record. Else return none.
|
|
|
|
for i, (k, v) in r.pairs:
|
|
|
|
if k == key:
|
|
|
|
return some(i)
|
|
|
|
|
2020-07-08 12:23:43 +00:00
|
|
|
proc update*(record: var Record, pk: PrivateKey,
|
|
|
|
fieldPairs: openarray[FieldPair]): EnrResult[void] =
|
2020-07-08 12:56:56 +00:00
|
|
|
## Update a `Record` k:v pairs.
|
|
|
|
##
|
|
|
|
## In case any of the k:v pairs is updated or added (new), the sequence number
|
|
|
|
## of the `Record` will be incremented and a new signature will be applied.
|
|
|
|
##
|
|
|
|
## Can fail in case of wrong `PrivateKey`, if the size of the resulting record
|
|
|
|
## exceeds `maxEnrSize` or if maximum sequence number is reached. The `Record`
|
|
|
|
## will not be altered in these cases.
|
2020-07-07 15:19:15 +00:00
|
|
|
var r = record
|
|
|
|
|
|
|
|
let pubkey = r.get(PublicKey)
|
|
|
|
if pubkey.isNone() or pubkey.get() != pk.toPublicKey():
|
|
|
|
return err("Public key does not correspond with given private key")
|
|
|
|
|
2020-07-07 20:48:26 +00:00
|
|
|
var updated = false
|
|
|
|
for fieldPair in fieldPairs:
|
|
|
|
let index = r.find(fieldPair[0])
|
|
|
|
if(index.isSome()):
|
|
|
|
if r.pairs[index.get()][1] == fieldPair[1]:
|
|
|
|
# Exact k:v pair is already in record, nothing to do here.
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# Need to update the value.
|
|
|
|
r.pairs[index.get()] = fieldPair
|
|
|
|
updated = true
|
2020-07-07 15:19:15 +00:00
|
|
|
else:
|
2020-07-07 20:48:26 +00:00
|
|
|
# Add new k:v pair.
|
2020-07-08 10:14:00 +00:00
|
|
|
r.pairs.insert(fieldPair, lowerBound(r.pairs, fieldPair, cmp))
|
2020-07-07 20:48:26 +00:00
|
|
|
updated = true
|
|
|
|
|
|
|
|
if updated:
|
2020-07-08 11:13:29 +00:00
|
|
|
if r.seqNum == high(r.seqNum): # highly unlikely
|
|
|
|
return err("Maximum sequence number reached")
|
2020-07-07 20:48:26 +00:00
|
|
|
r.seqNum.inc()
|
|
|
|
r.raw = ? makeEnrRaw(r.seqNum, pk, r.pairs)
|
2020-07-08 12:23:43 +00:00
|
|
|
record = r
|
2020-07-07 20:48:26 +00:00
|
|
|
|
2020-07-08 12:23:43 +00:00
|
|
|
ok()
|
2020-07-07 21:39:32 +00:00
|
|
|
|
2020-07-08 12:23:43 +00:00
|
|
|
proc update*(r: var Record, pk: PrivateKey,
|
|
|
|
ip: Option[ValidIpAddress],
|
|
|
|
tcpPort, udpPort: Port,
|
|
|
|
extraFields: openarray[FieldPair] = []):
|
|
|
|
EnrResult[void] =
|
2020-07-08 12:56:56 +00:00
|
|
|
## Update a `Record` with given ip address, tcp port, udp port and optional
|
|
|
|
## custom k:v pairs.
|
|
|
|
##
|
|
|
|
## In case any of the k:v pairs is updated or added (new), the sequence number
|
|
|
|
## of the `Record` will be incremented and a new signature will be applied.
|
|
|
|
##
|
|
|
|
## Can fail in case of wrong `PrivateKey`, if the size of the resulting record
|
|
|
|
## exceeds `maxEnrSize` or if maximum sequence number is reached. The `Record`
|
|
|
|
## will not be altered in these cases.
|
2020-07-07 20:48:26 +00:00
|
|
|
var fields = newSeq[FieldPair]()
|
|
|
|
|
2020-07-08 10:14:00 +00:00
|
|
|
fields.addAddress(ip, tcpPort, udpPort)
|
2020-07-07 20:48:26 +00:00
|
|
|
fields.add extraFields
|
2020-07-07 21:39:32 +00:00
|
|
|
r.update(pk, fields)
|
2020-07-07 15:19:15 +00:00
|
|
|
|
2020-05-01 20:34:26 +00:00
|
|
|
proc tryGet*(r: Record, key: string, T: type): Option[T] =
|
2019-12-18 01:10:09 +00:00
|
|
|
try:
|
2020-02-17 22:47:13 +00:00
|
|
|
return some get(r, key, T)
|
2020-05-28 08:19:36 +00:00
|
|
|
except ValueError:
|
2019-12-18 01:10:09 +00:00
|
|
|
discard
|
|
|
|
|
2020-05-28 08:19:36 +00:00
|
|
|
proc toTypedRecord*(r: Record): EnrResult[TypedRecord] =
|
2019-12-18 01:10:09 +00:00
|
|
|
let id = r.tryGet("id", string)
|
|
|
|
if id.isSome:
|
|
|
|
var tr: TypedRecord
|
|
|
|
tr.id = id.get
|
|
|
|
|
|
|
|
template readField(fieldName: untyped) {.dirty.} =
|
|
|
|
tr.fieldName = tryGet(r, astToStr(fieldName), type(tr.fieldName.get))
|
|
|
|
|
|
|
|
readField secp256k1
|
|
|
|
readField ip
|
|
|
|
readField ip6
|
|
|
|
readField tcp
|
|
|
|
readField tcp6
|
|
|
|
readField udp
|
|
|
|
readField udp6
|
|
|
|
|
2020-05-28 08:19:36 +00:00
|
|
|
ok(tr)
|
|
|
|
else:
|
|
|
|
err("Record without id field")
|
2019-12-18 01:10:09 +00:00
|
|
|
|
2020-06-11 19:24:52 +00:00
|
|
|
proc contains*(r: Record, fp: (string, seq[byte])): bool =
|
|
|
|
# TODO: use FieldPair for this, but that is a bit cumbersome. Perhaps the
|
|
|
|
# `get` call can be improved to make this easier.
|
|
|
|
let field = r.tryGet(fp[0], seq[byte])
|
|
|
|
if field.isSome():
|
|
|
|
if field.get() == fp[1]:
|
|
|
|
return true
|
|
|
|
|
2020-04-29 22:11:03 +00:00
|
|
|
proc verifySignatureV4(r: Record, sigData: openarray[byte], content: seq[byte]):
|
|
|
|
bool =
|
2020-04-06 16:24:15 +00:00
|
|
|
let publicKey = r.get(PublicKey)
|
|
|
|
if publicKey.isSome:
|
2020-04-04 16:44:01 +00:00
|
|
|
let sig = SignatureNR.fromRaw(sigData)
|
|
|
|
if sig.isOk:
|
2019-12-10 18:34:57 +00:00
|
|
|
var h = keccak256.digest(content)
|
2020-07-07 08:56:26 +00:00
|
|
|
return verify(sig[], SkMessage(h.data), publicKey.get)
|
2019-12-10 18:34:57 +00:00
|
|
|
|
2020-04-29 22:11:03 +00:00
|
|
|
proc verifySignature(r: Record): bool {.raises: [RlpError, Defect].} =
|
2020-04-20 18:14:39 +00:00
|
|
|
var rlp = rlpFromBytes(r.raw)
|
2019-12-10 18:34:57 +00:00
|
|
|
let sz = rlp.listLen
|
2020-02-27 18:09:05 +00:00
|
|
|
if not rlp.enterList:
|
|
|
|
return false
|
2020-04-20 18:14:39 +00:00
|
|
|
let sigData = rlp.read(seq[byte])
|
2019-12-10 18:34:57 +00:00
|
|
|
let content = block:
|
|
|
|
var writer = initRlpList(sz - 1)
|
|
|
|
var reader = rlp
|
|
|
|
for i in 1 ..< sz:
|
|
|
|
writer.appendRawBytes(reader.rawData)
|
|
|
|
reader.skipElem
|
|
|
|
writer.finish()
|
|
|
|
|
|
|
|
var id: Field
|
|
|
|
if r.getField("id", id) and id.kind == kString:
|
|
|
|
case id.str
|
|
|
|
of "v4":
|
|
|
|
result = verifySignatureV4(r, sigData, content)
|
|
|
|
else:
|
|
|
|
# Unknown Identity Scheme
|
|
|
|
discard
|
|
|
|
|
2020-04-29 22:11:03 +00:00
|
|
|
proc fromBytesAux(r: var Record): bool {.raises: [RlpError, Defect].} =
|
2020-02-11 16:25:31 +00:00
|
|
|
if r.raw.len > maxEnrSize:
|
|
|
|
return false
|
2019-12-18 11:06:45 +00:00
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
var rlp = rlpFromBytes(r.raw)
|
2020-02-27 18:09:05 +00:00
|
|
|
if not rlp.isList:
|
|
|
|
return false
|
|
|
|
|
2019-12-10 18:34:57 +00:00
|
|
|
let sz = rlp.listLen
|
2019-12-18 11:06:45 +00:00
|
|
|
if sz < minRlpListLen or sz mod 2 != 0:
|
2019-12-10 18:34:57 +00:00
|
|
|
# Wrong rlp object
|
|
|
|
return false
|
|
|
|
|
2020-02-27 18:09:05 +00:00
|
|
|
# We already know we are working with a list
|
2020-02-29 16:35:08 +00:00
|
|
|
doAssert rlp.enterList()
|
2019-12-10 18:34:57 +00:00
|
|
|
rlp.skipElem() # Skip signature
|
|
|
|
|
2020-02-11 16:25:31 +00:00
|
|
|
r.seqNum = rlp.read(uint64)
|
2019-12-10 18:34:57 +00:00
|
|
|
|
|
|
|
let numPairs = (sz - 2) div 2
|
|
|
|
|
|
|
|
for i in 0 ..< numPairs:
|
|
|
|
let k = rlp.read(string)
|
|
|
|
case k
|
|
|
|
of "id":
|
2019-12-18 11:06:45 +00:00
|
|
|
let id = rlp.read(string)
|
2019-12-10 18:34:57 +00:00
|
|
|
r.pairs.add((k, Field(kind: kString, str: id)))
|
|
|
|
of "secp256k1":
|
2019-12-18 11:06:45 +00:00
|
|
|
let pubkeyData = rlp.read(seq[byte])
|
2019-12-10 18:34:57 +00:00
|
|
|
r.pairs.add((k, Field(kind: kBytes, bytes: pubkeyData)))
|
2020-02-11 16:25:31 +00:00
|
|
|
of "tcp", "udp", "tcp6", "udp6":
|
|
|
|
let v = rlp.read(uint16)
|
2019-12-10 18:34:57 +00:00
|
|
|
r.pairs.add((k, Field(kind: kNum, num: v)))
|
|
|
|
else:
|
2020-04-20 18:14:39 +00:00
|
|
|
r.pairs.add((k, Field(kind: kBytes, bytes: rlp.read(seq[byte]))))
|
2019-12-10 18:34:57 +00:00
|
|
|
|
|
|
|
verifySignature(r)
|
|
|
|
|
|
|
|
proc fromBytes*(r: var Record, s: openarray[byte]): bool =
|
2020-04-29 22:11:03 +00:00
|
|
|
## Loads ENR from rlp-encoded bytes, and validated the signature.
|
2019-12-10 18:34:57 +00:00
|
|
|
r.raw = @s
|
|
|
|
try:
|
|
|
|
result = fromBytesAux(r)
|
2020-05-28 08:19:36 +00:00
|
|
|
except RlpError:
|
2019-12-10 18:34:57 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
proc fromBase64*(r: var Record, s: string): bool =
|
2020-04-29 22:11:03 +00:00
|
|
|
## Loads ENR from base64-encoded rlp-encoded bytes, and validated the
|
|
|
|
## signature.
|
2019-12-10 18:34:57 +00:00
|
|
|
try:
|
|
|
|
r.raw = Base64Url.decode(s)
|
|
|
|
result = fromBytesAux(r)
|
2020-05-28 08:19:36 +00:00
|
|
|
except RlpError, Base64Error:
|
2019-12-10 18:34:57 +00:00
|
|
|
discard
|
|
|
|
|
|
|
|
proc fromURI*(r: var Record, s: string): bool =
|
2020-04-29 22:11:03 +00:00
|
|
|
## Loads ENR from its text encoding: base64-encoded rlp-encoded bytes,
|
|
|
|
## prefixed with "enr:".
|
2019-12-10 18:34:57 +00:00
|
|
|
const prefix = "enr:"
|
|
|
|
if s.startsWith(prefix):
|
|
|
|
result = r.fromBase64(s[prefix.len .. ^1])
|
|
|
|
|
2019-12-17 21:20:26 +00:00
|
|
|
template fromURI*(r: var Record, url: EnrUri): bool =
|
|
|
|
fromURI(r, string(url))
|
|
|
|
|
2020-05-01 20:34:26 +00:00
|
|
|
proc toBase64*(r: Record): string =
|
2019-12-10 18:34:57 +00:00
|
|
|
result = Base64Url.encode(r.raw)
|
|
|
|
|
2020-05-01 20:34:26 +00:00
|
|
|
proc toURI*(r: Record): string = "enr:" & r.toBase64
|
2019-12-10 18:34:57 +00:00
|
|
|
|
2020-05-01 20:34:26 +00:00
|
|
|
proc `$`(f: Field): string =
|
2019-12-10 18:34:57 +00:00
|
|
|
case f.kind
|
|
|
|
of kNum:
|
|
|
|
$f.num
|
|
|
|
of kBytes:
|
|
|
|
"0x" & f.bytes.toHex
|
|
|
|
of kString:
|
|
|
|
"\"" & f.str & "\""
|
|
|
|
|
2020-05-01 20:34:26 +00:00
|
|
|
proc `$`*(r: Record): string =
|
2019-12-10 18:34:57 +00:00
|
|
|
result = "("
|
|
|
|
var first = true
|
|
|
|
for (k, v) in r.pairs:
|
|
|
|
if first:
|
|
|
|
first = false
|
|
|
|
else:
|
|
|
|
result &= ", "
|
|
|
|
result &= k
|
|
|
|
result &= ": "
|
|
|
|
result &= $v
|
|
|
|
result &= ')'
|
2019-12-16 19:38:45 +00:00
|
|
|
|
2020-03-24 09:51:34 +00:00
|
|
|
proc `==`*(a, b: Record): bool = a.raw == b.raw
|
|
|
|
|
2020-04-29 22:11:03 +00:00
|
|
|
proc read*(rlp: var Rlp, T: typedesc[Record]):
|
|
|
|
T {.inline, raises:[RlpError, ValueError, Defect].} =
|
2020-04-20 18:14:39 +00:00
|
|
|
if not result.fromBytes(rlp.rawData):
|
2020-05-28 08:19:36 +00:00
|
|
|
# TODO: This could also just be an invalid signature, would be cleaner to
|
|
|
|
# split of RLP deserialisation errors from this.
|
2019-12-16 19:38:45 +00:00
|
|
|
raise newException(ValueError, "Could not deserialize")
|
|
|
|
rlp.skipElem()
|
|
|
|
|
2020-05-01 20:34:26 +00:00
|
|
|
proc append*(rlpWriter: var RlpWriter, value: Record) =
|
2020-04-20 18:14:39 +00:00
|
|
|
rlpWriter.appendRawBytes(value.raw)
|