2019-03-04 18:22:38 +00:00
|
|
|
## Nim-LibP2P
|
|
|
|
## Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
## 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.
|
|
|
|
|
|
|
|
## This module implementes API for libp2p peer.
|
2020-07-01 06:25:09 +00:00
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
import
|
|
|
|
std/[hashes, strutils],
|
|
|
|
stew/[base58, results],
|
|
|
|
chronicles,
|
|
|
|
nimcrypto/utils,
|
|
|
|
./crypto/crypto, ./multicodec, ./multihash, ./vbuffer,
|
|
|
|
./protobuf/minprotobuf
|
2020-09-06 08:31:47 +00:00
|
|
|
|
2020-07-01 06:25:09 +00:00
|
|
|
export results
|
2019-03-04 18:22:38 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
maxInlineKeyLength* = 42
|
|
|
|
|
|
|
|
type
|
2019-03-05 20:28:46 +00:00
|
|
|
PeerID* = object
|
|
|
|
data*: seq[byte]
|
2019-03-04 18:22:38 +00:00
|
|
|
|
2020-09-06 08:31:47 +00:00
|
|
|
func `$`*(pid: PeerID): string =
|
2019-03-05 20:28:46 +00:00
|
|
|
## Return base58 encoded ``pid`` representation.
|
2020-09-06 08:31:47 +00:00
|
|
|
Base58.encode(pid.data)
|
|
|
|
|
|
|
|
func shortLog*(pid: PeerId): string =
|
|
|
|
## Returns compact string representation of ``pid``.
|
|
|
|
var spid = $pid
|
2020-12-03 19:53:16 +00:00
|
|
|
if len(spid) > 10:
|
|
|
|
spid[3] = '*'
|
|
|
|
spid.delete(4, spid.high - 6)
|
|
|
|
|
|
|
|
spid
|
2020-09-06 08:31:47 +00:00
|
|
|
|
|
|
|
chronicles.formatIt(PeerID): shortLog(it)
|
2019-03-04 18:22:38 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func toBytes*(pid: PeerID, data: var openarray[byte]): int =
|
2019-03-05 20:28:46 +00:00
|
|
|
## Store PeerID ``pid`` to array of bytes ``data``.
|
2019-03-04 18:22:38 +00:00
|
|
|
##
|
2019-03-05 20:28:46 +00:00
|
|
|
## Returns number of bytes needed to store ``pid``.
|
|
|
|
result = len(pid.data)
|
2019-03-04 18:22:38 +00:00
|
|
|
if len(data) >= result and result > 0:
|
2019-03-05 20:28:46 +00:00
|
|
|
copyMem(addr data[0], unsafeAddr pid.data[0], result)
|
2019-03-04 18:22:38 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
template getBytes*(pid: PeerID): seq[byte] =
|
2019-03-05 20:28:46 +00:00
|
|
|
## Return PeerID ``pid`` as array of bytes.
|
2020-12-03 19:53:16 +00:00
|
|
|
pid.data
|
2019-03-04 18:22:38 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func hex*(pid: PeerID): string =
|
2019-03-05 20:28:46 +00:00
|
|
|
## Returns hexadecimal string representation of ``pid``.
|
2020-12-03 19:53:16 +00:00
|
|
|
toHex(pid.data)
|
2019-03-04 18:22:38 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
template len*(pid: PeerID): int =
|
2019-03-05 20:28:46 +00:00
|
|
|
## Returns length of ``pid`` binary representation.
|
2020-12-03 19:53:16 +00:00
|
|
|
len(pid.data)
|
2019-03-04 18:22:38 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func cmp*(a, b: PeerID): int =
|
2019-03-04 18:22:38 +00:00
|
|
|
## Compares two peer ids ``a`` and ``b``.
|
|
|
|
## Returns:
|
|
|
|
##
|
|
|
|
## | 0 iff a == b
|
|
|
|
## | < 0 iff a < b
|
|
|
|
## | > 0 iff a > b
|
|
|
|
var i = 0
|
2019-03-05 20:28:46 +00:00
|
|
|
var m = min(len(a.data), len(b.data))
|
2019-03-04 18:22:38 +00:00
|
|
|
while i < m:
|
2019-03-05 20:28:46 +00:00
|
|
|
result = ord(a.data[i]) - ord(b.data[i])
|
2019-03-04 18:22:38 +00:00
|
|
|
if result != 0: return
|
|
|
|
inc(i)
|
2019-03-05 20:28:46 +00:00
|
|
|
result = len(a.data) - len(b.data)
|
2019-03-04 18:22:38 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
template `<=`*(a, b: PeerID): bool =
|
2019-03-04 18:22:38 +00:00
|
|
|
(cmp(a, b) <= 0)
|
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
template `<`*(a, b: PeerID): bool =
|
2019-03-04 18:22:38 +00:00
|
|
|
(cmp(a, b) < 0)
|
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
template `>=`*(a, b: PeerID): bool =
|
2019-03-04 18:22:38 +00:00
|
|
|
(cmp(a, b) >= 0)
|
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
template `>`*(a, b: PeerID): bool =
|
2019-03-04 18:22:38 +00:00
|
|
|
(cmp(a, b) > 0)
|
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
template `==`*(a, b: PeerID): bool =
|
2019-03-04 18:22:38 +00:00
|
|
|
(cmp(a, b) == 0)
|
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
template hash*(pid: PeerID): Hash =
|
|
|
|
hash(pid.data)
|
2019-03-04 18:22:38 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func validate*(pid: PeerID): bool =
|
2019-03-05 20:28:46 +00:00
|
|
|
## Validate check if ``pid`` is empty or not.
|
2020-12-03 19:53:16 +00:00
|
|
|
len(pid.data) > 0 and MultiHash.validate(pid.data)
|
2019-03-04 18:22:38 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func hasPublicKey*(pid: PeerID): bool =
|
2019-03-05 20:28:46 +00:00
|
|
|
## Returns ``true`` if ``pid`` is small enough to hold public key inside.
|
|
|
|
if len(pid.data) > 0:
|
|
|
|
var mh: MultiHash
|
2020-05-18 05:25:55 +00:00
|
|
|
if MultiHash.decode(pid.data, mh).isOk:
|
2019-03-04 18:22:38 +00:00
|
|
|
if mh.mcodec == multiCodec("identity"):
|
|
|
|
result = true
|
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func extractPublicKey*(pid: PeerID, pubkey: var PublicKey): bool =
|
2019-03-05 20:28:46 +00:00
|
|
|
## Returns ``true`` if public key was successfully decoded from PeerID
|
|
|
|
## ``pid``and stored to ``pubkey``.
|
2019-03-04 18:22:38 +00:00
|
|
|
##
|
2019-03-05 20:28:46 +00:00
|
|
|
## Returns ``false`` otherwise.
|
|
|
|
if len(pid.data) > 0:
|
2020-12-03 19:53:16 +00:00
|
|
|
var mh: MultiHash
|
2020-05-18 05:25:55 +00:00
|
|
|
if MultiHash.decode(pid.data, mh).isOk:
|
2019-03-04 18:22:38 +00:00
|
|
|
if mh.mcodec == multiCodec("identity"):
|
|
|
|
let length = len(mh.data.buffer)
|
|
|
|
result = pubkey.init(mh.data.buffer.toOpenArray(mh.dpos, length - 1))
|
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func init*(pid: var PeerID, data: openarray[byte]): bool =
|
2019-03-05 12:09:26 +00:00
|
|
|
## Initialize peer id from raw binary representation ``data``.
|
|
|
|
##
|
|
|
|
## Returns ``true`` if peer was successfully initialiazed.
|
2019-03-05 20:28:46 +00:00
|
|
|
var p = PeerID(data: @data)
|
2019-03-05 12:09:26 +00:00
|
|
|
if p.validate():
|
|
|
|
pid = p
|
2019-03-05 20:28:46 +00:00
|
|
|
result = true
|
2019-03-05 12:09:26 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func init*(pid: var PeerID, data: string): bool =
|
2019-03-05 12:09:26 +00:00
|
|
|
## Initialize peer id from base58 encoded string representation.
|
|
|
|
##
|
|
|
|
## Returns ``true`` if peer was successfully initialiazed.
|
|
|
|
var p = newSeq[byte](len(data) + 4)
|
|
|
|
var length = 0
|
|
|
|
if Base58.decode(data, p, length) == Base58Status.Success:
|
|
|
|
p.setLen(length)
|
2019-03-05 20:28:46 +00:00
|
|
|
var opid: PeerID
|
|
|
|
shallowCopy(opid.data, p)
|
2019-03-05 12:09:26 +00:00
|
|
|
if opid.validate():
|
|
|
|
pid = opid
|
2019-03-05 20:28:46 +00:00
|
|
|
result = true
|
2019-03-05 12:09:26 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func init*(t: typedesc[PeerID], data: openarray[byte]): Result[PeerID, cstring] =
|
2019-03-05 12:09:26 +00:00
|
|
|
## Create new peer id from raw binary representation ``data``.
|
2020-07-01 06:25:09 +00:00
|
|
|
var res: PeerID
|
|
|
|
if not init(res, data):
|
|
|
|
err("peerid: incorrect PeerID binary form")
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-03-05 12:09:26 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func init*(t: typedesc[PeerID], data: string): Result[PeerID, cstring] =
|
2019-03-05 12:09:26 +00:00
|
|
|
## Create new peer id from base58 encoded string representation ``data``.
|
2020-07-01 06:25:09 +00:00
|
|
|
var res: PeerID
|
|
|
|
if not init(res, data):
|
|
|
|
err("peerid: incorrect PeerID string")
|
|
|
|
else:
|
|
|
|
ok(res)
|
2019-03-05 12:09:26 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func init*(t: typedesc[PeerID], pubkey: PublicKey): Result[PeerID, cstring] =
|
2019-03-05 12:09:26 +00:00
|
|
|
## Create new peer id from public key ``pubkey``.
|
2020-12-03 19:53:16 +00:00
|
|
|
var pubraw = ? pubkey.getBytes().orError(
|
|
|
|
cstring("peerid: failed to get bytes from given key"))
|
2019-03-05 12:09:26 +00:00
|
|
|
var mh: MultiHash
|
|
|
|
if len(pubraw) <= maxInlineKeyLength:
|
2020-07-01 06:25:09 +00:00
|
|
|
mh = ? MultiHash.digest("identity", pubraw)
|
2019-03-05 12:09:26 +00:00
|
|
|
else:
|
2020-07-01 06:25:09 +00:00
|
|
|
mh = ? MultiHash.digest("sha2-256", pubraw)
|
|
|
|
ok(PeerID(data: mh.data.buffer))
|
2019-03-05 12:09:26 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func init*(t: typedesc[PeerID], seckey: PrivateKey): Result[PeerID, cstring] =
|
2019-03-05 12:09:26 +00:00
|
|
|
## Create new peer id from private key ``seckey``.
|
2021-09-02 10:03:40 +00:00
|
|
|
PeerID.init(? seckey.getPublicKey().orError(cstring("invalid private key")))
|
2019-03-05 12:09:26 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func match*(pid: PeerID, pubkey: PublicKey): bool =
|
2019-03-05 20:28:46 +00:00
|
|
|
## Returns ``true`` if ``pid`` matches public key ``pubkey``.
|
2020-07-01 06:25:09 +00:00
|
|
|
let p = PeerID.init(pubkey)
|
|
|
|
if p.isErr:
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
pid == p.get()
|
2019-03-05 12:09:26 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func match*(pid: PeerID, seckey: PrivateKey): bool =
|
2019-03-05 20:28:46 +00:00
|
|
|
## Returns ``true`` if ``pid`` matches private key ``seckey``.
|
2020-07-01 06:25:09 +00:00
|
|
|
let p = PeerID.init(seckey)
|
|
|
|
if p.isErr:
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
pid == p.get()
|
2019-03-05 12:09:26 +00:00
|
|
|
|
|
|
|
## Serialization/Deserialization helpers
|
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func write*(vb: var VBuffer, pid: PeerID) =
|
2019-03-04 18:22:38 +00:00
|
|
|
## Write PeerID value ``peerid`` to buffer ``vb``.
|
2019-03-05 20:28:46 +00:00
|
|
|
vb.writeSeq(pid.data)
|
2019-03-04 18:22:38 +00:00
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func write*(pb: var ProtoBuffer, field: int, pid: PeerID) =
|
2020-07-13 12:43:07 +00:00
|
|
|
## Write PeerID value ``peerid`` to object ``pb`` using ProtoBuf's encoding.
|
|
|
|
write(pb, field, pid.data)
|
|
|
|
|
2020-12-03 19:53:16 +00:00
|
|
|
func getField*(pb: ProtoBuffer, field: int,
|
2020-07-15 08:25:39 +00:00
|
|
|
pid: var PeerID): ProtoResult[bool] {.inline.} =
|
2020-07-13 12:43:07 +00:00
|
|
|
## Read ``PeerID`` from ProtoBuf's message and validate it
|
|
|
|
var buffer: seq[byte]
|
2020-07-15 08:25:39 +00:00
|
|
|
let res = ? pb.getField(field, buffer)
|
|
|
|
if not(res):
|
|
|
|
ok(false)
|
2020-07-13 12:43:07 +00:00
|
|
|
else:
|
2020-07-15 08:25:39 +00:00
|
|
|
var peerId: PeerID
|
|
|
|
if peerId.init(buffer):
|
|
|
|
pid = peerId
|
|
|
|
ok(true)
|
|
|
|
else:
|
|
|
|
err(ProtoError.IncorrectBlob)
|