From 1e6c8b2171646b733ba8338a6820517e5fddf24c Mon Sep 17 00:00:00 2001 From: cheatfate Date: Mon, 4 Mar 2019 20:22:38 +0200 Subject: [PATCH 01/10] Fix examples. Add peer.nim. Switch daemonapi to use PeerID from peer.nim. --- examples/bootstrap.nim | 2 +- examples/node.nim | 2 +- libp2p/daemon/daemonapi.nim | 66 +++++++-------- libp2p/peer.nim | 164 ++++++++++++++++++++++++++++++++++++ 4 files changed, 198 insertions(+), 36 deletions(-) create mode 100644 libp2p/peer.nim diff --git a/examples/bootstrap.nim b/examples/bootstrap.nim index 9cd92e224..fc4c2ec97 100644 --- a/examples/bootstrap.nim +++ b/examples/bootstrap.nim @@ -1,4 +1,4 @@ -import asyncdispatch2, nimcrypto, strutils +import chronos, nimcrypto, strutils import ../libp2p/daemon/daemonapi, ../libp2p/[base58, multicodec, multiaddress] import hexdump diff --git a/examples/node.nim b/examples/node.nim index 9f6d2e756..8fae43d37 100644 --- a/examples/node.nim +++ b/examples/node.nim @@ -1,4 +1,4 @@ -import asyncdispatch2, nimcrypto, strutils, os +import chronos, nimcrypto, strutils, os import ../libp2p/daemon/daemonapi, ../libp2p/[base58, multiaddress] proc main(bn: string) {.async.} = diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index 7da60e761..a70da119f 100644 --- a/libp2p/daemon/daemonapi.nim +++ b/libp2p/daemon/daemonapi.nim @@ -10,7 +10,7 @@ ## This module implementes API for `go-libp2p-daemon`. import os, osproc, strutils, tables, streams, strtabs import chronos -import ../varint, ../multiaddress, ../multicodec, ../base58, ../cid +import ../varint, ../multiaddress, ../multicodec, ../base58, ../cid, ../peer import ../wire, ../protobuf/minprotobuf when not defined(windows): @@ -76,7 +76,7 @@ type VALUE = 1, END = 2 - PeerID* = seq[byte] + # PeerID* = seq[byte] MultiProtocol* = string LibP2PPublicKey* = seq[byte] DHTValue* = seq[byte] @@ -745,8 +745,7 @@ proc transactMessage(transp: StreamTransport, proc getPeerInfo(pb: var ProtoBuffer): PeerInfo = ## Get PeerInfo object from ``pb``. result.addresses = newSeq[MultiAddress]() - result.peer = newSeq[byte]() - if pb.getBytes(1, result.peer) == -1: + if pb.getValue(1, result.peer) == -1: raise newException(DaemonLocalError, "Missing required field `peer`!") var address = newSeq[byte]() while pb.getBytes(2, address) != -1: @@ -803,10 +802,10 @@ proc openStream*(api: DaemonAPI, peer: PeerID, pb.withMessage() do: var res = pb.enterSubmessage() if res == cast[int](ResponseType.STREAMINFO): - stream.peer = newSeq[byte]() + # stream.peer = newSeq[byte]() var raddress = newSeq[byte]() stream.protocol = "" - if pb.getLengthValue(1, stream.peer) == -1: + if pb.getValue(1, stream.peer) == -1: raise newException(DaemonLocalError, "Missing `peer` field!") if pb.getLengthValue(2, raddress) == -1: raise newException(DaemonLocalError, "Missing `address` field!") @@ -825,10 +824,9 @@ proc streamHandler(server: StreamServer, transp: StreamTransport) {.async.} = var message = await transp.recvMessage() var pb = initProtoBuffer(message) var stream = new P2PStream - stream.peer = newSeq[byte]() var raddress = newSeq[byte]() stream.protocol = "" - if pb.getLengthValue(1, stream.peer) == -1: + if pb.getValue(1, stream.peer) == -1: raise newException(DaemonLocalError, "Missing `peer` field!") if pb.getLengthValue(2, raddress) == -1: raise newException(DaemonLocalError, "Missing `address` field!") @@ -929,6 +927,10 @@ proc dhtGetSingleValue(pb: var ProtoBuffer): seq[byte] = if pb.getLengthValue(3, result) == -1: raise newException(DaemonLocalError, "Missing field `value`!") +proc dhtGetSinglePeerID(pb: var ProtoBuffer): PeerID = + if pb.getValue(3, result) == -1: + raise newException(DaemonLocalError, "Missing field `value`!") + proc enterDhtMessage(pb: var ProtoBuffer, rt: DHTResponseType) {.inline.} = var dtype: uint var res = pb.enterSubmessage() @@ -1074,7 +1076,7 @@ proc dhtGetClosestPeers*(api: DaemonAPI, key: string, var cpb = initProtoBuffer(message) if cpb.getDhtMessageType() == DHTResponseType.END: break - list.add(cpb.dhtGetSingleValue()) + list.add(cpb.dhtGetSinglePeerID()) result = list finally: await api.closeConnection(transp) @@ -1152,12 +1154,11 @@ proc pubsubListPeers*(api: DaemonAPI, try: var pb = await transp.transactMessage(requestPSListPeers(topic)) withMessage(pb) do: + var peer: PeerID pb.enterPsMessage() var peers = newSeq[PeerID]() - var peer = newSeq[byte]() - while pb.getBytes(2, peer) != -1: + while pb.getValue(2, peer) != -1: peers.add(peer) - peer.setLen(0) result = peers finally: await api.closeConnection(transp) @@ -1174,28 +1175,25 @@ proc pubsubPublish*(api: DaemonAPI, topic: string, await api.closeConnection(transp) proc getPubsubMessage*(pb: var ProtoBuffer): PubSubMessage = + result.data = newSeq[byte]() + result.seqno = newSeq[byte]() + result.signature = newSeq[byte]() + result.key = newSeq[byte]() + discard pb.getValue(1, result.peer) + discard pb.getBytes(2, result.data) + discard pb.getBytes(3, result.seqno) var item = newSeq[byte]() - for field in 1..6: - while true: - if pb.getBytes(field, item) == -1: - break - if field == 1: - result.peer = item - elif field == 2: - result.data = item - elif field == 3: - result.seqno = item - elif field == 4: - var copyitem = item - var stritem = cast[string](copyitem) - if len(result.topics) == 0: - result.topics = newSeq[string]() - result.topics.add(stritem) - elif field == 5: - result.signature = item - elif field == 6: - result.key = item - item.setLen(0) + while true: + if pb.getBytes(4, item) == -1: + break + var copyitem = item + var stritem = cast[string](copyitem) + if len(result.topics) == 0: + result.topics = newSeq[string]() + result.topics.add(stritem) + item.setLen(0) + discard pb.getBytes(5, result.signature) + discard pb.getBytes(6, result.key) proc pubsubLoop(api: DaemonAPI, ticket: PubsubTicket) {.async.} = while true: @@ -1232,7 +1230,7 @@ proc `$`*(pinfo: PeerInfo): string = ## Get string representation of ``PeerInfo`` object. result = newStringOfCap(128) result.add("{PeerID: '") - result.add(Base58.encode(pinfo.peer)) + result.add($pinfo.peer.pretty()) result.add("' Addresses: [") let length = len(pinfo.addresses) for i in 0..= result and result > 0: + copyMem(addr data[0], addr p[0], result) + +proc getBytes*(peerid: PeerID): seq[byte] {.inline.} = + ## Return PeerID as array of bytes. + var p = cast[seq[byte]](peerid) + result = @p + +proc fromKey*(pubkey: PublicKey): PeerID = + ## Returns the PeerID corresponding to public key ``pubkey``. + var pubraw = pubkey.getBytes() + var mh: MultiHash + var codec: MultiCodec + if len(pubraw) <= maxInlineKeyLength: + mh = MultiHash.digest("identity", pubraw) + else: + mh = MultiHash.digest("sha2-256", pubraw) + result = cast[PeerID](mh.data.buffer) + +proc fromKey*(seckey: PrivateKey): PeerID {.inline.} = + ## Returns the PeerID corresponding to private key ``seckey``. + result = fromKey(seckey.getKey()) + +proc hex*(peerid: PeerID): string = + ## Returns hexadecimal string representation of ``peerid``. + var p = cast[seq[byte]](peerid) + if len(p) > 0: + result = toHex(p) + +proc len*(a: PeerID): int {.borrow.} + +proc cmp*(a, b: PeerID): int = + ## Compares two peer ids ``a`` and ``b``. + ## Returns: + ## + ## | 0 iff a == b + ## | < 0 iff a < b + ## | > 0 iff a > b + var ab = cast[seq[byte]](a) + var bb = cast[seq[byte]](b) + var i = 0 + var m = min(len(ab), len(bb)) + while i < m: + result = ord(ab[i]) - ord(bb[i]) + if result != 0: return + inc(i) + result = len(ab) - len(bb) + +proc `<=`*(a, b: PeerID): bool {.inline.} = + (cmp(a, b) <= 0) + +proc `<`*(a, b: PeerID): bool {.inline.} = + (cmp(a, b) < 0) + +proc `>=`*(a, b: PeerID): bool {.inline.} = + (cmp(a, b) >= 0) + +proc `>`*(a, b: PeerID): bool {.inline.} = + (cmp(a, b) > 0) + +proc `==`*(a, b: PeerID): bool {.inline.} = + (cmp(a, b) == 0) + +proc hash*(peerid: PeerID): Hash {.inline.} = + var p = cast[seq[byte]](peerid) + result = hash(p) + +proc validate*(peerid: PeerID): bool = + ## Validate check if ``peerid`` is empty or not. + var p = cast[seq[byte]](peerid) + if len(p) > 0: + result = MultiHash.validate(p) + +proc hasPublicKey*(peerid: PeerID): bool = + ## Returns ``true`` if ``peerid`` is small enough to hold public key inside. + var mh: MultiHash + var p = cast[seq[byte]](peerid) + if len(p) > 0: + if MultiHash.decode(p, mh) > 0: + if mh.mcodec == multiCodec("identity"): + result = true + +proc extractPublicKey*(peerid: PeerID, pubkey: var PublicKey): bool = + ## Returns ``true`` if public key was successfully decoded and stored + ## in ``pubkey``. + ## + ## Returns ``false`` otherwise + var mh: MultiHash + var p = cast[seq[byte]](peerid) + if len(p) > 0: + if MultiHash.decode(p, mh) > 0: + if mh.mcodec == multiCodec("identity"): + let length = len(mh.data.buffer) + result = pubkey.init(mh.data.buffer.toOpenArray(mh.dpos, length - 1)) + +proc match*(peerid: PeerID, pubkey: PublicKey): bool {.inline.} = + ## Returns ``true`` if ``peerid`` matches public key ``pubkey``. + result = (peerid == pubkey.fromKey()) + +proc match*(peerid: PeerID, seckey: PrivateKey): bool {.inline.} = + ## Returns ``true`` if ``peerid`` matches private key ``seckey``. + result = (peerid == seckey.fromKey()) + +proc `$`*(peerid: PeerID): string = + ## Returns compact string representation of ``peerid``. + var pid = peerid.pretty() + if len(pid) <= 10: + result = pid + else: + for i in 0..<2: + result.add(pid[i]) + result.add("*") + for i in (len(pid) - 6)..(len(pid) - 1): + result.add(pid[i]) + +proc write*(vb: var VBuffer, peerid: PeerID) {.inline.} = + ## Write PeerID value ``peerid`` to buffer ``vb``. + var p = cast[seq[byte]](peerid) + vb.writeSeq(p) + +proc initProtoField*(index: int, peerid: PeerID): ProtoField = + ## Initialize ProtoField with PeerID ``value``. + var p = cast[seq[byte]](peerid) + result = initProtoField(index, p) + +proc getValue*(data: var ProtoBuffer, field: int, value: var PeerID): int = + ## Read ``PeerID`` from ProtoBuf's message and validate it. + var buffer: seq[byte] + result = getLengthValue(data, field, buffer) + if result > 0: + value = cast[PeerID](buffer) + if not value.validate(): + result = -1 From 53467d028fece0a67a4e567d878c00b50f347786 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Mon, 4 Mar 2019 20:28:59 +0200 Subject: [PATCH 02/10] Fix examples for new PeerID. --- examples/bootstrap.nim | 13 ++++++------- examples/node.nim | 7 +++---- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/examples/bootstrap.nim b/examples/bootstrap.nim index fc4c2ec97..94c8338e6 100644 --- a/examples/bootstrap.nim +++ b/examples/bootstrap.nim @@ -1,5 +1,6 @@ import chronos, nimcrypto, strutils -import ../libp2p/daemon/daemonapi, ../libp2p/[base58, multicodec, multiaddress] +import ../libp2p/daemon/daemonapi +import ../libp2p/[base58, multicodec, multiaddress, peer] import hexdump const @@ -9,28 +10,26 @@ proc dumpSubscribedPeers(api: DaemonAPI) {.async.} = var peers = await api.pubsubListPeers(PubSubTopic) echo "= List of connected and subscribed peers:" for item in peers: - echo Base58.encode(item) + echo item.pretty() proc main() {.async.} = echo "= Starting P2P bootnode" var api = await newDaemonApi({DHTFull, PSGossipSub}) var id = await api.identity() - let tpeerid = Base58.encode(id.peer) - echo "= P2P bootnode ", tpeerid, " started." + echo "= P2P bootnode ", id.peer.pretty(), " started." let mcip4 = multiCodec("ip4") let mcip6 = multiCodec("ip6") echo "= You can use one of this addresses to bootstrap your nodes:" for item in id.addresses: if item.protoCode() == mcip4 or item.protoCode() == mcip6: - echo $item & "/ipfs/" & tpeerid + echo $item & "/ipfs/" & id.peer.pretty() proc pubsubLogger(api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage): Future[bool] {.async.} = - let bpeer = Base58.encode(message.peer) let msglen = len(message.data) echo "= Recieved pubsub message wit length ", msglen, - " bytes from peer ", bpeer + " bytes from peer ", message.peer.pretty() echo dumpHex(message.data) await api.dumpSubscribedPeers() result = true diff --git a/examples/node.nim b/examples/node.nim index 8fae43d37..1bd5ca601 100644 --- a/examples/node.nim +++ b/examples/node.nim @@ -1,5 +1,5 @@ import chronos, nimcrypto, strutils, os -import ../libp2p/daemon/daemonapi, ../libp2p/[base58, multiaddress] +import ../libp2p/daemon/daemonapi, ../libp2p/[base58, multiaddress, peer] proc main(bn: string) {.async.} = echo "= Starting P2P node" @@ -8,17 +8,16 @@ proc main(bn: string) {.async.} = bootstrapNodes = bootnodes, peersRequired = 1) var id = await api.identity() - echo "= P2P node ", Base58.encode(id.peer), " started:" + echo "= P2P node ", id.peer.pretty(), " started:" for item in id.addresses: echo item proc pubsubLogger(api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage): Future[bool] {.async.} = - let bpeer = Base58.encode(message.peer) let msglen = len(message.data) echo "= Recieved pubsub message wit length ", msglen, - " bytes from peer ", bpeer + " bytes from peer ", message.peer.pretty() result = true var ticket = await api.pubsubSubscribe("test-net", pubsubLogger) From 77f34af73793cd50c774c718c9a051af1c47aa0f Mon Sep 17 00:00:00 2001 From: cheatfate Date: Mon, 4 Mar 2019 20:51:10 +0200 Subject: [PATCH 03/10] Fix testdaemon.nim. --- examples/bootstrap.nim | 2 +- examples/node.nim | 6 ++++-- tests/testdaemon.nim | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/bootstrap.nim b/examples/bootstrap.nim index 94c8338e6..0a4014c3d 100644 --- a/examples/bootstrap.nim +++ b/examples/bootstrap.nim @@ -28,7 +28,7 @@ proc main() {.async.} = ticket: PubsubTicket, message: PubSubMessage): Future[bool] {.async.} = let msglen = len(message.data) - echo "= Recieved pubsub message wit length ", msglen, + echo "= Recieved pubsub message with length ", msglen, " bytes from peer ", message.peer.pretty() echo dumpHex(message.data) await api.dumpSubscribedPeers() diff --git a/examples/node.nim b/examples/node.nim index 1bd5ca601..8bab23edd 100644 --- a/examples/node.nim +++ b/examples/node.nim @@ -16,8 +16,10 @@ proc main(bn: string) {.async.} = ticket: PubsubTicket, message: PubSubMessage): Future[bool] {.async.} = let msglen = len(message.data) - echo "= Recieved pubsub message wit length ", msglen, - " bytes from peer ", message.peer.pretty() + echo "= Recieved pubsub message with length ", msglen, + " bytes from peer ", message.peer.pretty(), ": " + var strdata = cast[string](message.data) + echo strdata result = true var ticket = await api.pubsubSubscribe("test-net", pubsubLogger) diff --git a/tests/testdaemon.nim b/tests/testdaemon.nim index 756542631..10ecaad20 100644 --- a/tests/testdaemon.nim +++ b/tests/testdaemon.nim @@ -1,7 +1,7 @@ import unittest import chronos import ../libp2p/daemon/daemonapi, ../libp2p/multiaddress, ../libp2p/multicodec, - ../libp2p/cid, ../libp2p/multihash + ../libp2p/cid, ../libp2p/multihash, ../libp2p/peer proc identitySpawnTest(): Future[bool] {.async.} = var api = await newDaemonApi() From d7d9d7bd409c923e07bc99cab5551ec35af5f8b2 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Tue, 5 Mar 2019 01:57:18 +0200 Subject: [PATCH 04/10] Fix daemonapi and examples. --- examples/bootstrap.nim | 15 ++++++++++++++- examples/node.nim | 16 +++++++++++++--- libp2p/daemon/daemonapi.nim | 5 +++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/examples/bootstrap.nim b/examples/bootstrap.nim index 0a4014c3d..1d2ffff15 100644 --- a/examples/bootstrap.nim +++ b/examples/bootstrap.nim @@ -1,6 +1,5 @@ import chronos, nimcrypto, strutils import ../libp2p/daemon/daemonapi -import ../libp2p/[base58, multicodec, multiaddress, peer] import hexdump const @@ -12,6 +11,18 @@ proc dumpSubscribedPeers(api: DaemonAPI) {.async.} = for item in peers: echo item.pretty() +proc dumpAllPeers(api: DaemonAPI) {.async.} = + var peers = await api.listPeers() + echo "Current connected peers count = ", len(peers) + for item in peers: + echo item.peer.pretty() + +proc monitor(api: DaemonAPI) {.async.} = + while true: + echo "Dumping all peers" + await dumpAllPeers(api) + await sleepAsync(5000) + proc main() {.async.} = echo "= Starting P2P bootnode" var api = await newDaemonApi({DHTFull, PSGossipSub}) @@ -24,6 +35,8 @@ proc main() {.async.} = if item.protoCode() == mcip4 or item.protoCode() == mcip6: echo $item & "/ipfs/" & id.peer.pretty() + asyncCheck monitor(api) + proc pubsubLogger(api: DaemonAPI, ticket: PubsubTicket, message: PubSubMessage): Future[bool] {.async.} = diff --git a/examples/node.nim b/examples/node.nim index 8bab23edd..0a77a3828 100644 --- a/examples/node.nim +++ b/examples/node.nim @@ -1,5 +1,8 @@ import chronos, nimcrypto, strutils, os -import ../libp2p/daemon/daemonapi, ../libp2p/[base58, multiaddress, peer] +import ../libp2p/daemon/daemonapi + +const + PubSubTopic = "test-net" proc main(bn: string) {.async.} = echo "= Starting P2P node" @@ -22,11 +25,18 @@ proc main(bn: string) {.async.} = echo strdata result = true - var ticket = await api.pubsubSubscribe("test-net", pubsubLogger) + var ticket = await api.pubsubSubscribe(PubSubTopic, pubsubLogger) + + # Waiting for gossipsub interval + while true: + var peers = await api.pubsubListPeers(PubSubTopic) + if len(peers) > 0: + break + await sleepAsync(1000) var data = "HELLO\r\n" var msgData = cast[seq[byte]](data) - await api.pubsubPublish("test-net", msgData) + await api.pubsubPublish(PubSubTopic, msgData) when isMainModule: if paramCount() != 1: diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index a70da119f..fce3dd8f8 100644 --- a/libp2p/daemon/daemonapi.nim +++ b/libp2p/daemon/daemonapi.nim @@ -13,6 +13,8 @@ import chronos import ../varint, ../multiaddress, ../multicodec, ../base58, ../cid, ../peer import ../wire, ../protobuf/minprotobuf +export peer, multiaddress, multicodec, multihash, cid + when not defined(windows): import posix @@ -147,7 +149,6 @@ type DaemonRemoteError* = object of Exception DaemonLocalError* = object of Exception - var daemonsCount {.threadvar.}: int proc requestIdentity(): ProtoBuffer = @@ -661,7 +662,7 @@ proc newDaemonApi*(flags: set[P2PDaemonFlags] = {}, raise newException(DaemonLocalError, "Could not find daemon executable!") # Starting daemon process - # echo "Starting ", cmd, " ", args.join(" ") + echo "Starting ", cmd, " ", args.join(" ") api.process = startProcess(cmd, "", args, env, {poStdErrToStdOut}) # Waiting until daemon will not be bound to control socket. while true: From 68f3f506f3e6d1746c95e2c1a8c924eafcf145bf Mon Sep 17 00:00:00 2001 From: cheatfate Date: Tue, 5 Mar 2019 02:04:45 +0200 Subject: [PATCH 05/10] Fix multihash dependency. --- libp2p/daemon/daemonapi.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index fce3dd8f8..9b97e109b 100644 --- a/libp2p/daemon/daemonapi.nim +++ b/libp2p/daemon/daemonapi.nim @@ -11,7 +11,7 @@ import os, osproc, strutils, tables, streams, strtabs import chronos import ../varint, ../multiaddress, ../multicodec, ../base58, ../cid, ../peer -import ../wire, ../protobuf/minprotobuf +import ../wire, ../multihash, ../protobuf/minprotobuf export peer, multiaddress, multicodec, multihash, cid From 215c6c4257e0818394149adfc4d93fa50bd8e0b2 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Tue, 5 Mar 2019 11:49:59 +0200 Subject: [PATCH 06/10] Add SHA-1 support to multihash.nim, enabled tests for it. --- libp2p/multihash.nim | 17 +++++++++++++---- tests/testmultihash.nim | 34 +++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/libp2p/multihash.nim b/libp2p/multihash.nim index 349b2e2d6..0b6d7e08e 100644 --- a/libp2p/multihash.nim +++ b/libp2p/multihash.nim @@ -15,13 +15,13 @@ ## 4. SHA3/KECCAK ## 5. SHAKE-128/SHAKE-256 ## 6. BLAKE2s/BLAKE2s +## 7. SHA1 ## ## Hashes which are not yet supported -## 1. SHA1 -## 2. SKEIN -## 3. MURMUR +## 1. SKEIN +## 2. MURMUR import tables -import nimcrypto/[sha2, keccak, blake2, hash, utils] +import nimcrypto/[sha, sha2, keccak, blake2, hash, utils] import varint, vbuffer, base58, multicodec, multibase const @@ -49,6 +49,13 @@ proc identhash(data: openarray[byte], output: var openarray[byte]) = else: len(data) copyMem(addr output[0], unsafeAddr data[0], length) +proc sha1hash(data: openarray[byte], output: var openarray[byte]) = + if len(output) > 0: + var digest = sha1.digest(data) + var length = if sha1.sizeDigest > len(output): len(output) + else: sha1.sizeDigest + copyMem(addr output[0], addr digest.data[0], length) + proc dblsha2_256hash(data: openarray[byte], output: var openarray[byte]) = if len(output) > 0: var digest1 = sha256.digest(data) @@ -163,6 +170,8 @@ const HashesList = [ MHash(mcodec: multiCodec("identity"), size: 0, coder: identhash), + MHash(mcodec: multiCodec("sha1"), size: sha1.sizeDigest, + coder: sha1hash), MHash(mcodec: multiCodec("dbl-sha2-256"), size: sha256.sizeDigest, coder: dblsha2_256hash ), diff --git a/tests/testmultihash.nim b/tests/testmultihash.nim index 7745175d5..26522308b 100644 --- a/tests/testmultihash.nim +++ b/tests/testmultihash.nim @@ -1,14 +1,14 @@ import unittest +import nimcrypto/utils import ../libp2p/multihash const RustTestVectors = [ - # TODO: SHA1 - # [ - # "sha1", - # "beep boop", - # "11147c8357577f51d4f0a8d393aa1aaafb28863d9421" - # ], + [ + "sha1", + "beep boop", + "11147C8357577F51D4F0A8D393AA1AAAFB28863D9421" + ], [ "sha2-256", "helloworld", @@ -22,7 +22,9 @@ const [ "sha2-512", "hello world", - "1340309ECC489C12D6EB4CC40F50C902F2B4D0ED77EE511A7C7A9BCD3CA86D4CD86F989DD35BC5FF499670DA34255B45B0CFD830E81F605DCF7DC5542E93AE9CD76F" + """1340309ECC489C12D6EB4CC40F50C902F2B4D0ED77EE511A7C7A9BCD3CA86D4C + D86F989DD35BC5FF499670DA34255B45B0CFD830E81F605DCF7DC5542E93AE9C + D76F""" ], [ "sha3-224", @@ -37,12 +39,15 @@ const [ "sha3-384", "hello world", - "153083BFF28DDE1B1BF5810071C6643C08E5B05BDB836EFFD70B403EA8EA0A634DC4997EB1053AA3593F590F9C63630DD90B" + """153083BFF28DDE1B1BF5810071C6643C08E5B05BDB836EFFD70B403EA8EA0A63 + 4DC4997EB1053AA3593F590F9C63630DD90B""" ], [ "sha3-512", "hello world", - "1440840006653E9AC9E95117A15C915CAAB81662918E925DE9E004F774FF82D7079A40D4D27B1B372657C61D46D470304C88C788B3A4527AD074D1DCCBEE5DBAA99A" + """1440840006653E9AC9E95117A15C915CAAB81662918E925DE9E004F774FF82D7 + 079A40D4D27B1B372657C61D46D470304C88C788B3A4527AD074D1DCCBEE5DBA + A99A""" ], [ "keccak-224", @@ -57,12 +62,15 @@ const [ "keccak-384", "hello world", - "1C3065FC99339A2A40E99D3C40D695B22F278853CA0F925CDE4254BCAE5E22ECE47E6441F91B6568425ADC9D95B0072EB49F" + """1C3065FC99339A2A40E99D3C40D695B22F278853CA0F925CDE4254BCAE5E22ECE4 + 7E6441F91B6568425ADC9D95B0072EB49F""" ], [ "keccak-512", "hello world", - "1D403EE2B40047B8060F68C67242175660F4174D0AF5C01D47168EC20ED619B0B7C42181F40AA1046F39E2EF9EFC6910782A998E0013D172458957957FAC9405B67D" + """1D403EE2B40047B8060F68C67242175660F4174D0AF5C01D47168EC20ED619B0 + B7C42181F40AA1046F39E2EF9EFC6910782A998E0013D172458957957FAC9405 + B67D""" ] ] @@ -73,8 +81,8 @@ suite "MultiHash test suite": var msg = item[1] var bmsg = cast[seq[byte]](msg) var mh1 = MultiHash.digest(item[0], bmsg) - var mh2 = MultiHash.init(item[2]) + var mh2 = MultiHash.init(stripSpaces(item[2])) check: - hex(mh1) == item[2] + hex(mh1) == stripSpaces(item[2]) hex(mh1) == hex(mh2) mh1 == mh2 From a96718cf9e8969e924ff40eb035502aeecc9f7d3 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Tue, 5 Mar 2019 14:09:26 +0200 Subject: [PATCH 07/10] Add PeerID support to multiaddress. Change some PeerID api. Some fixes to chat. --- examples/chat.nim | 30 +++++++-------- libp2p/multiaddress.nim | 13 +++++-- libp2p/peer.nim | 82 +++++++++++++++++++++++++++++------------ 3 files changed, 83 insertions(+), 42 deletions(-) diff --git a/examples/chat.nim b/examples/chat.nim index 4eaba4d64..045b6ef53 100644 --- a/examples/chat.nim +++ b/examples/chat.nim @@ -1,5 +1,5 @@ -import asyncdispatch2, nimcrypto, strutils -import ../libp2p/daemon/daemonapi, ../libp2p/[base58, multiaddress] +import chronos, nimcrypto, strutils +import ../libp2p/daemon/daemonapi const ConsoleAddress = "/tmp/console-chat.sock" @@ -39,10 +39,10 @@ proc serveThread(server: StreamServer, if line.startsWith("/connect"): var parts = line.split(" ") if len(parts) == 2: - var peerId = Base58.decode(parts[1]) - var address = MultiAddress.init(P_P2PCIRCUIT) - address &= MultiAddress.init(P_P2P, peerId) - echo "= Searching for peer ", parts[1] + var peerId = PeerID.init(parts[1]) + var address = MultiAddress.init(multiCodec("p2p-circuit")) + address &= MultiAddress.init(multiCodec("p2p"), peerId) + echo "= Searching for peer ", peerId.pretty() var id = await udata.api.dhtFindPeer(peerId) echo "= Peer " & parts[1] & " found at addresses:" for item in id.addresses: @@ -57,8 +57,8 @@ proc serveThread(server: StreamServer, elif line.startsWith("/search"): var parts = line.split(" ") if len(parts) == 2: - var peerId = Base58.decode(parts[1]) - echo "= Searching for peer ", parts[1] + var peerId = PeerID.init(parts[1]) + echo "= Searching for peer ", peerId.pretty() var id = await udata.api.dhtFindPeer(peerId) echo "= Peer " & parts[1] & " found at addresses:" for item in id.addresses: @@ -66,12 +66,12 @@ proc serveThread(server: StreamServer, elif line.startsWith("/consearch"): var parts = line.split(" ") if len(parts) == 2: - var peerId = Base58.decode(parts[1]) + var peerId = PeerID.init(parts[1]) echo "= Searching for peers connected to peer ", parts[1] var peers = await udata.api.dhtFindPeersConnectedToPeer(peerId) echo "= Found ", len(peers), " connected to peer ", parts[1] for item in peers: - var peer = Base58.encode(item.peer) + var peer = item.peer var addresses = newSeq[string]() var relay = false for a in item.addresses: @@ -80,9 +80,9 @@ proc serveThread(server: StreamServer, relay = true break if relay: - echo peer, " * ", " [", addresses.join(", "), "]" + echo peer.pretty(), " * ", " [", addresses.join(", "), "]" else: - echo peer, " [", addresses.join(", "), "]" + echo peer.pretty(), " [", addresses.join(", "), "]" elif line.startsWith("/exit"): quit(0) else: @@ -112,7 +112,7 @@ proc main() {.async.} = var id = await data.api.identity() proc streamHandler(api: DaemonAPI, stream: P2PStream) {.async.} = - echo "= Peer ", Base58.encode(stream.peer), " joined chat" + echo "= Peer ", stream.peer.pretty(), " joined chat" data.remotes.add(stream.transp) while true: var line = await stream.transp.readLine() @@ -121,8 +121,8 @@ proc main() {.async.} = echo ">> ", line await data.api.addHandler(ServerProtocols, streamHandler) - echo "= Your PeerID is ", Base58.encode(id.peer) - + echo "= Your PeerID is ", id.peer.pretty() + when isMainModule: waitFor(main()) while true: diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index 5e952ebdd..ad2d66e8d 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -10,6 +10,7 @@ ## This module implements MultiAddress. import tables, strutils, net import multicodec, multihash, multibase, transcoder, base58, base32, vbuffer +import peer {.deadCodeElim:on.} @@ -562,7 +563,7 @@ proc validate*(ma: MultiAddress): bool = discard result = true -proc init*(mtype: typedesc[MultiAddress], protocol: int, +proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, value: openarray[byte]): MultiAddress = ## Initialize MultiAddress object from protocol id ``protocol`` and array ## of bytes ``value``. @@ -582,7 +583,13 @@ proc init*(mtype: typedesc[MultiAddress], protocol: int, result.data.writeSeq(data) result.data.finish() -proc init*(mtype: typedesc[MultiAddress], protocol: int): MultiAddress = +proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, + value: PeerID): MultiAddress {.inline.} = + ## Initialize MultiAddress object from protocol id ``protocol`` and peer id + ## ``value``. + init(mtype, protocol, cast[seq[byte]](value)) + +proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec): MultiAddress = ## Initialize MultiAddress object from protocol id ``protocol``. let proto = CodeAddresses.getOrDefault(protocol) if proto.kind == None: @@ -590,7 +597,7 @@ proc init*(mtype: typedesc[MultiAddress], protocol: int): MultiAddress = result.data = initVBuffer() if proto.kind != Marker: raise newException(MultiAddressError, "Protocol missing value") - result.data.writeVarint(cast[uint64](proto.code)) + result.data.writeVarint(cast[uint64](proto.mcodec)) result.data.finish() proc getProtocol(name: string): MAProtocol {.inline.} = diff --git a/libp2p/peer.nim b/libp2p/peer.nim index 4edace19c..b8097af7d 100644 --- a/libp2p/peer.nim +++ b/libp2p/peer.nim @@ -19,6 +19,8 @@ const type PeerID* = distinct seq[byte] + PeerIDError* = object of Exception + proc pretty*(peerid: PeerID): string {.inline.} = ## Return base58 encoded ``peerid`` representation. Base58.encode(cast[seq[byte]](peerid)) @@ -37,22 +39,7 @@ proc getBytes*(peerid: PeerID): seq[byte] {.inline.} = var p = cast[seq[byte]](peerid) result = @p -proc fromKey*(pubkey: PublicKey): PeerID = - ## Returns the PeerID corresponding to public key ``pubkey``. - var pubraw = pubkey.getBytes() - var mh: MultiHash - var codec: MultiCodec - if len(pubraw) <= maxInlineKeyLength: - mh = MultiHash.digest("identity", pubraw) - else: - mh = MultiHash.digest("sha2-256", pubraw) - result = cast[PeerID](mh.data.buffer) - -proc fromKey*(seckey: PrivateKey): PeerID {.inline.} = - ## Returns the PeerID corresponding to private key ``seckey``. - result = fromKey(seckey.getKey()) - -proc hex*(peerid: PeerID): string = +proc hex*(peerid: PeerID): string {.inline.} = ## Returns hexadecimal string representation of ``peerid``. var p = cast[seq[byte]](peerid) if len(p) > 0: @@ -124,14 +111,6 @@ proc extractPublicKey*(peerid: PeerID, pubkey: var PublicKey): bool = let length = len(mh.data.buffer) result = pubkey.init(mh.data.buffer.toOpenArray(mh.dpos, length - 1)) -proc match*(peerid: PeerID, pubkey: PublicKey): bool {.inline.} = - ## Returns ``true`` if ``peerid`` matches public key ``pubkey``. - result = (peerid == pubkey.fromKey()) - -proc match*(peerid: PeerID, seckey: PrivateKey): bool {.inline.} = - ## Returns ``true`` if ``peerid`` matches private key ``seckey``. - result = (peerid == seckey.fromKey()) - proc `$`*(peerid: PeerID): string = ## Returns compact string representation of ``peerid``. var pid = peerid.pretty() @@ -144,6 +123,61 @@ proc `$`*(peerid: PeerID): string = for i in (len(pid) - 6)..(len(pid) - 1): result.add(pid[i]) +proc init*(pid: var PeerID, data: openarray[byte]): bool = + ## Initialize peer id from raw binary representation ``data``. + ## + ## Returns ``true`` if peer was successfully initialiazed. + var p = cast[PeerID](@data) + if p.validate(): + pid = p + +proc init*(pid: var PeerID, data: string): bool = + ## 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) + var opid = cast[PeerID](p) + if opid.validate(): + pid = opid + +proc init*(t: typedesc[PeerID], data: openarray[byte]): PeerID {.inline.} = + ## Create new peer id from raw binary representation ``data``. + if not init(result, data): + raise newException(PeerIDError, "Incorrect PeerID binary form") + +proc init*(t: typedesc[PeerID], data: string): PeerID {.inline.} = + ## Create new peer id from base58 encoded string representation ``data``. + if not init(result, data): + raise newException(PeerIDError, "Incorrect PeerID string") + +proc init*(t: typedesc[PeerID], pubkey: PublicKey): PeerID = + ## Create new peer id from public key ``pubkey``. + var pubraw = pubkey.getBytes() + var mh: MultiHash + var codec: MultiCodec + if len(pubraw) <= maxInlineKeyLength: + mh = MultiHash.digest("identity", pubraw) + else: + mh = MultiHash.digest("sha2-256", pubraw) + result = cast[PeerID](mh.data.buffer) + +proc init*(t: typedesc[PeerID], seckey: PrivateKey): PeerID {.inline.} = + ## Create new peer id from private key ``seckey``. + result = PeerID.init(seckey.getKey()) + +proc match*(peerid: PeerID, pubkey: PublicKey): bool {.inline.} = + ## Returns ``true`` if ``peerid`` matches public key ``pubkey``. + result = (peerid == PeerID.init(pubkey)) + +proc match*(peerid: PeerID, seckey: PrivateKey): bool {.inline.} = + ## Returns ``true`` if ``peerid`` matches private key ``seckey``. + result = (peerid == PeerID.init(seckey)) + +## Serialization/Deserialization helpers + proc write*(vb: var VBuffer, peerid: PeerID) {.inline.} = ## Write PeerID value ``peerid`` to buffer ``vb``. var p = cast[seq[byte]](peerid) From d7a7f8102d528f67df9f4ffcea33cc76879dce13 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Tue, 5 Mar 2019 22:28:46 +0200 Subject: [PATCH 08/10] Change PeerID type from distinct type. Fix MultiAddress compilation error. Fix chat example. --- examples/chat.nim | 5 +- libp2p/multiaddress.nim | 2 +- libp2p/peer.nim | 143 ++++++++++++++++++++-------------------- 3 files changed, 75 insertions(+), 75 deletions(-) diff --git a/examples/chat.nim b/examples/chat.nim index 045b6ef53..df296bea0 100644 --- a/examples/chat.nim +++ b/examples/chat.nim @@ -1,6 +1,9 @@ import chronos, nimcrypto, strutils import ../libp2p/daemon/daemonapi +when not(compileOption("threads")): + {.fatal: "Please, compile this program with the --threads:on option!".} + const ConsoleAddress = "/tmp/console-chat.sock" ServerAddress = "/tmp/remote-chat.sock" @@ -94,7 +97,7 @@ proc serveThread(server: StreamServer, if len(pending) > 0: var results = await all(pending) except: - break + echo getCurrentException().msg proc main() {.async.} = var data = new CustomData diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index ad2d66e8d..3132b74b7 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -571,7 +571,7 @@ proc init*(mtype: typedesc[MultiAddress], protocol: MultiCodec, if proto.kind == None: raise newException(MultiAddressError, "Protocol not found") result.data = initVBuffer() - result.data.writeVarint(cast[uint64](proto.code)) + result.data.writeVarint(cast[uint64](proto.mcodec)) if proto.kind in {Fixed, Length, Path}: if len(value) == 0: raise newException(MultiAddressError, "Value must not be empty array") diff --git a/libp2p/peer.nim b/libp2p/peer.nim index b8097af7d..dc9d1be48 100644 --- a/libp2p/peer.nim +++ b/libp2p/peer.nim @@ -17,35 +17,35 @@ const maxInlineKeyLength* = 42 type - PeerID* = distinct seq[byte] + PeerID* = object + data*: seq[byte] PeerIDError* = object of Exception -proc pretty*(peerid: PeerID): string {.inline.} = - ## Return base58 encoded ``peerid`` representation. - Base58.encode(cast[seq[byte]](peerid)) +proc pretty*(pid: PeerID): string {.inline.} = + ## Return base58 encoded ``pid`` representation. + result = Base58.encode(pid.data) -proc toBytes*(peerid: PeerID, data: var openarray[byte]): int = - ## Store PeerID ``peerid`` to array of bytes ``data``. +proc toBytes*(pid: PeerID, data: var openarray[byte]): int = + ## Store PeerID ``pid`` to array of bytes ``data``. ## - ## Returns number of bytes needed to store ``peerid``. - var p = cast[seq[byte]](peerid) - result = len(p) + ## Returns number of bytes needed to store ``pid``. + result = len(pid.data) if len(data) >= result and result > 0: - copyMem(addr data[0], addr p[0], result) + copyMem(addr data[0], unsafeAddr pid.data[0], result) -proc getBytes*(peerid: PeerID): seq[byte] {.inline.} = - ## Return PeerID as array of bytes. - var p = cast[seq[byte]](peerid) - result = @p +proc getBytes*(pid: PeerID): seq[byte] {.inline.} = + ## Return PeerID ``pid`` as array of bytes. + result = pid.data -proc hex*(peerid: PeerID): string {.inline.} = - ## Returns hexadecimal string representation of ``peerid``. - var p = cast[seq[byte]](peerid) - if len(p) > 0: - result = toHex(p) +proc hex*(pid: PeerID): string {.inline.} = + ## Returns hexadecimal string representation of ``pid``. + if len(pid.data) > 0: + result = toHex(pid.data) -proc len*(a: PeerID): int {.borrow.} +proc len*(pid: PeerID): int {.inline.} = + ## Returns length of ``pid`` binary representation. + result = len(pid.data) proc cmp*(a, b: PeerID): int = ## Compares two peer ids ``a`` and ``b``. @@ -54,15 +54,13 @@ proc cmp*(a, b: PeerID): int = ## | 0 iff a == b ## | < 0 iff a < b ## | > 0 iff a > b - var ab = cast[seq[byte]](a) - var bb = cast[seq[byte]](b) var i = 0 - var m = min(len(ab), len(bb)) + var m = min(len(a.data), len(b.data)) while i < m: - result = ord(ab[i]) - ord(bb[i]) + result = ord(a.data[i]) - ord(b.data[i]) if result != 0: return inc(i) - result = len(ab) - len(bb) + result = len(a.data) - len(b.data) proc `<=`*(a, b: PeerID): bool {.inline.} = (cmp(a, b) <= 0) @@ -79,57 +77,55 @@ proc `>`*(a, b: PeerID): bool {.inline.} = proc `==`*(a, b: PeerID): bool {.inline.} = (cmp(a, b) == 0) -proc hash*(peerid: PeerID): Hash {.inline.} = - var p = cast[seq[byte]](peerid) - result = hash(p) +proc hash*(pid: PeerID): Hash {.inline.} = + result = hash(pid.data) -proc validate*(peerid: PeerID): bool = - ## Validate check if ``peerid`` is empty or not. - var p = cast[seq[byte]](peerid) - if len(p) > 0: - result = MultiHash.validate(p) +proc validate*(pid: PeerID): bool = + ## Validate check if ``pid`` is empty or not. + if len(pid.data) > 0: + result = MultiHash.validate(pid.data) -proc hasPublicKey*(peerid: PeerID): bool = - ## Returns ``true`` if ``peerid`` is small enough to hold public key inside. - var mh: MultiHash - var p = cast[seq[byte]](peerid) - if len(p) > 0: - if MultiHash.decode(p, mh) > 0: +proc hasPublicKey*(pid: PeerID): bool = + ## Returns ``true`` if ``pid`` is small enough to hold public key inside. + if len(pid.data) > 0: + var mh: MultiHash + if MultiHash.decode(pid.data, mh) > 0: if mh.mcodec == multiCodec("identity"): result = true -proc extractPublicKey*(peerid: PeerID, pubkey: var PublicKey): bool = - ## Returns ``true`` if public key was successfully decoded and stored - ## in ``pubkey``. +proc extractPublicKey*(pid: PeerID, pubkey: var PublicKey): bool = + ## Returns ``true`` if public key was successfully decoded from PeerID + ## ``pid``and stored to ``pubkey``. ## - ## Returns ``false`` otherwise + ## Returns ``false`` otherwise. var mh: MultiHash - var p = cast[seq[byte]](peerid) - if len(p) > 0: - if MultiHash.decode(p, mh) > 0: + if len(pid.data) > 0: + if MultiHash.decode(pid.data, mh) > 0: if mh.mcodec == multiCodec("identity"): let length = len(mh.data.buffer) result = pubkey.init(mh.data.buffer.toOpenArray(mh.dpos, length - 1)) -proc `$`*(peerid: PeerID): string = - ## Returns compact string representation of ``peerid``. - var pid = peerid.pretty() - if len(pid) <= 10: - result = pid +proc `$`*(pid: PeerID): string = + ## Returns compact string representation of ``pid``. + var spid = pid.pretty() + if len(spid) <= 10: + result = spid else: + result = newStringOfCap(10) for i in 0..<2: - result.add(pid[i]) + result.add(spid[i]) result.add("*") - for i in (len(pid) - 6)..(len(pid) - 1): - result.add(pid[i]) + for i in (len(spid) - 6)..(len(spid) - 1): + result.add(spid[i]) proc init*(pid: var PeerID, data: openarray[byte]): bool = ## Initialize peer id from raw binary representation ``data``. ## ## Returns ``true`` if peer was successfully initialiazed. - var p = cast[PeerID](@data) + var p = PeerID(data: @data) if p.validate(): pid = p + result = true proc init*(pid: var PeerID, data: string): bool = ## Initialize peer id from base58 encoded string representation. @@ -139,9 +135,11 @@ proc init*(pid: var PeerID, data: string): bool = var length = 0 if Base58.decode(data, p, length) == Base58Status.Success: p.setLen(length) - var opid = cast[PeerID](p) + var opid: PeerID + shallowCopy(opid.data, p) if opid.validate(): pid = opid + result = true proc init*(t: typedesc[PeerID], data: openarray[byte]): PeerID {.inline.} = ## Create new peer id from raw binary representation ``data``. @@ -162,37 +160,36 @@ proc init*(t: typedesc[PeerID], pubkey: PublicKey): PeerID = mh = MultiHash.digest("identity", pubraw) else: mh = MultiHash.digest("sha2-256", pubraw) - result = cast[PeerID](mh.data.buffer) + result.data = mh.data.buffer proc init*(t: typedesc[PeerID], seckey: PrivateKey): PeerID {.inline.} = ## Create new peer id from private key ``seckey``. result = PeerID.init(seckey.getKey()) -proc match*(peerid: PeerID, pubkey: PublicKey): bool {.inline.} = - ## Returns ``true`` if ``peerid`` matches public key ``pubkey``. - result = (peerid == PeerID.init(pubkey)) +proc match*(pid: PeerID, pubkey: PublicKey): bool {.inline.} = + ## Returns ``true`` if ``pid`` matches public key ``pubkey``. + result = (pid == PeerID.init(pubkey)) -proc match*(peerid: PeerID, seckey: PrivateKey): bool {.inline.} = - ## Returns ``true`` if ``peerid`` matches private key ``seckey``. - result = (peerid == PeerID.init(seckey)) +proc match*(pid: PeerID, seckey: PrivateKey): bool {.inline.} = + ## Returns ``true`` if ``pid`` matches private key ``seckey``. + result = (pid == PeerID.init(seckey)) ## Serialization/Deserialization helpers -proc write*(vb: var VBuffer, peerid: PeerID) {.inline.} = +proc write*(vb: var VBuffer, pid: PeerID) {.inline.} = ## Write PeerID value ``peerid`` to buffer ``vb``. - var p = cast[seq[byte]](peerid) - vb.writeSeq(p) + vb.writeSeq(pid.data) -proc initProtoField*(index: int, peerid: PeerID): ProtoField = +proc initProtoField*(index: int, pid: PeerID): ProtoField = ## Initialize ProtoField with PeerID ``value``. - var p = cast[seq[byte]](peerid) - result = initProtoField(index, p) + result = initProtoField(index, pid.data) proc getValue*(data: var ProtoBuffer, field: int, value: var PeerID): int = ## Read ``PeerID`` from ProtoBuf's message and validate it. - var buffer: seq[byte] - result = getLengthValue(data, field, buffer) + var pid: PeerID + result = getLengthValue(data, field, pid.data) if result > 0: - value = cast[PeerID](buffer) - if not value.validate(): + if not pid.validate(): result = -1 + else: + value = pid From 39129d0ec18aed689b015933e5026b215b7a5873 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Wed, 6 Mar 2019 02:36:09 +0200 Subject: [PATCH 09/10] Add base64 and tests for it. Adopt multibase to be able to use base64. Fix base32 typos. --- libp2p.nimble | 1 + libp2p/base32.nim | 29 ++--- libp2p/base64.nim | 238 ++++++++++++++++++++++++++++++++++++++++ libp2p/multibase.nim | 77 +++++++++++-- tests/testbase64.nim | 162 +++++++++++++++++++++++++++ tests/testmultibase.nim | 72 ++++++------ 6 files changed, 518 insertions(+), 61 deletions(-) create mode 100644 libp2p/base64.nim create mode 100644 tests/testbase64.nim diff --git a/libp2p.nimble b/libp2p.nimble index 257df01c1..f084e0da2 100644 --- a/libp2p.nimble +++ b/libp2p.nimble @@ -15,6 +15,7 @@ task test, "Runs the test suite": exec "nim c -r tests/testvarint" exec "nim c -r tests/testbase58" exec "nim c -r tests/testbase32" + exec "nim c -r tests/testbase64" exec "nim c -r tests/testmultiaddress" exec "nim c -r tests/testmultihash" exec "nim c -r tests/testmultibase" diff --git a/libp2p/base32.nim b/libp2p/base32.nim index 0a3fa7c33..e48f12dbe 100644 --- a/libp2p/base32.nim +++ b/libp2p/base32.nim @@ -22,23 +22,23 @@ type encode*: array[32, uint8] Base32Upper* = object - ## Type to use RFC4868 alphabet in uppercase without padding + ## Type to use RFC4648 alphabet in uppercase without padding Base32Lower* = object - ## Type to use RFC4868 alphabet in lowercase without padding + ## Type to use RFC4648 alphabet in lowercase without padding Base32UpperPad* = object - ## Type to use RFC4868 alphabet in uppercase with padding + ## Type to use RFC4648 alphabet in uppercase with padding Base32LowerPad* = object - ## Type to use RFC4868 alphabet in lowercase with padding + ## Type to use RFC4648 alphabet in lowercase with padding HexBase32Upper* = object - ## Type to use RFC4868-HEX alphabet in uppercase without padding + ## Type to use RFC4648-HEX alphabet in uppercase without padding HexBase32Lower* = object - ## Type to use RFC4868-HEX alphabet in lowercase without padding + ## Type to use RFC4648-HEX alphabet in lowercase without padding HexBase32UpperPad* = object - ## Type to use RFC4868-HEX alphabet in uppercase with padding + ## Type to use RFC4648-HEX alphabet in uppercase with padding HexBase32LowerPad* = object - ## Type to use RFC4868-HEX alphabet in lowercase with padding + ## Type to use RFC4648-HEX alphabet in lowercase with padding Base32* = Base32Upper - ## By default we are using RFC4868 alphabet in uppercase without padding + ## By default we are using RFC4648 alphabet in uppercase without padding Base32PadTypes* = Base32UpperPad | Base32LowerPad | HexBase32UpperPad | HexBase32LowerPad ## All types with padding support @@ -240,12 +240,10 @@ proc decode*[T: byte|char](btype: typedesc[Base32Types], instr: openarray[T], for j in 0..<8: if (cast[byte](instr[i + j]) and 0x80'u8) != 0: outlen = 0 - zeroMem(addr outbytes[0], i + 8) return Base32Status.Incorrect - let ch = alphabet.decode[int8(instr[i + j])] + let ch = alphabet.decode[cast[int8](instr[i + j])] if ch == -1: outlen = 0 - zeroMem(addr outbytes[0], i + 8) return Base32Status.Incorrect buffer[j] = cast[byte](ch) discard convert8to5(buffer, outbytes.toOpenArray(k, k + 4), 8) @@ -256,18 +254,15 @@ proc decode*[T: byte|char](btype: typedesc[Base32Types], instr: openarray[T], if reminder != 0: if reminder == 1 or reminder == 3 or reminder == 6: outlen = 0 - zeroMem(addr outbytes[0], i + 8) return Base32Status.Incorrect for j in 0.. len(outbytes): + outlen = length + return Base64Status.Overrun + + var inlen = len(instr) + when (btype is Base64PadTypes): + for i in countdown(inlen - 1, 0): + if instr[i] != '=': + break + dec(inlen) + + let reminder = inlen mod 4 + let limit = inlen - reminder + var buffer: array[4, byte] + var i, k: int + while i < limit: + for j in 0..<4: + if (cast[byte](instr[i + j]) and 0x80'u8) != 0: + outlen = 0 + zeroMem(addr outbytes[0], i + 3) + return Base64Status.Incorrect + let ch = alphabet.decode[cast[int8](instr[i + j])] + if ch == -1: + outlen = 0 + zeroMem(addr outbytes[0], i + 3) + return Base64Status.Incorrect + buffer[j] = cast[byte](ch) + outbytes[k] = cast[byte]((buffer[0] shl 2) or (buffer[1] shr 4)) + inc(k) + outbytes[k] = cast[byte]((buffer[1] shl 4) or (buffer[2] shr 2)) + inc(k) + outbytes[k] = cast[byte]((buffer[2] shl 6) or buffer[3]) + inc(k) + i += 4 + + if reminder > 0: + if reminder == 1: + outlen = 0 + return Base64Status.Incorrect + + for j in 0.. 1: + outbytes[k] = cast[byte]((buffer[0] shl 2) or (buffer[1] shr 4)) + inc(k) + if reminder > 2: + outbytes[k] = cast[byte]((buffer[1] shl 4) or (buffer[2] shr 2)) + inc(k) + + outlen = k + result = Base64Status.Success + +proc decode*[T: byte|char](btype: typedesc[Base64Types], + instr: openarray[T]): seq[byte] = + ## Decode BASE64 string ``instr`` and return sequence of bytes as result. + if len(instr) == 0: + result = newSeq[byte]() + else: + var length = 0 + result = newSeq[byte](btype.decodedLength(len(instr))) + if btype.decode(instr, result, length) == Base64Status.Success: + result.setLen(length) + else: + raise newException(Base64Error, "Incorrect base64 string") diff --git a/libp2p/multibase.nim b/libp2p/multibase.nim index fec965a51..954228e9c 100644 --- a/libp2p/multibase.nim +++ b/libp2p/multibase.nim @@ -10,10 +10,9 @@ ## This module implements MultiBase. ## ## TODO: -## 1. base64 -## 2. base32z +## 1. base32z import tables, strutils -import base32, base58 +import base32, base58, base64 type MultibaseStatus* {.pure.} = enum @@ -28,7 +27,7 @@ type outbytes: var openarray[char], outlen: var int): MultibaseStatus {.nimcall.} MBCodeSize = proc(length: int): int {.nimcall.} - + MBCodec = object code: char name: string @@ -108,6 +107,15 @@ proc b58ce(r: Base58Status): MultibaseStatus {.inline.} = elif r == Base58Status.Success: result = MultibaseStatus.Success +proc b64ce(r: Base64Status): MultibaseStatus {.inline.} = + result = MultiBaseStatus.Error + if r == Base64Status.Incorrect: + result = MultibaseStatus.Incorrect + elif r == Base64Status.Overrun: + result = MultiBaseStatus.Overrun + elif r == Base64Status.Success: + result = MultibaseStatus.Success + proc b32hd(inbytes: openarray[char], outbytes: var openarray[byte], outlen: var int): MultibaseStatus = @@ -216,6 +224,51 @@ proc b58be(inbytes: openarray[byte], proc b58el(length: int): int = Base58.encodedLength(length) proc b58dl(length: int): int = Base58.decodedLength(length) +proc b64el(length: int): int = Base64.encodedLength(length) +proc b64dl(length: int): int = Base64.decodedLength(length) +proc b64pel(length: int): int = Base64Pad.encodedLength(length) +proc b64pdl(length: int): int = Base64Pad.decodedLength(length) + +proc b64e(inbytes: openarray[byte], + outbytes: var openarray[char], + outlen: var int): MultibaseStatus = + result = b64ce(Base64.encode(inbytes, outbytes, outlen)) + +proc b64d(inbytes: openarray[char], + outbytes: var openarray[byte], + outlen: var int): MultibaseStatus = + result = b64ce(Base64.decode(inbytes, outbytes, outlen)) + +proc b64pe(inbytes: openarray[byte], + outbytes: var openarray[char], + outlen: var int): MultibaseStatus = + result = b64ce(Base64Pad.encode(inbytes, outbytes, outlen)) + +proc b64pd(inbytes: openarray[char], + outbytes: var openarray[byte], + outlen: var int): MultibaseStatus = + result = b64ce(Base64Pad.decode(inbytes, outbytes, outlen)) + +proc b64ue(inbytes: openarray[byte], + outbytes: var openarray[char], + outlen: var int): MultibaseStatus = + result = b64ce(Base64Url.encode(inbytes, outbytes, outlen)) + +proc b64ud(inbytes: openarray[char], + outbytes: var openarray[byte], + outlen: var int): MultibaseStatus = + result = b64ce(Base64Url.decode(inbytes, outbytes, outlen)) + +proc b64upe(inbytes: openarray[byte], + outbytes: var openarray[char], + outlen: var int): MultibaseStatus = + result = b64ce(Base64UrlPad.encode(inbytes, outbytes, outlen)) + +proc b64upd(inbytes: openarray[char], + outbytes: var openarray[byte], + outlen: var int): MultibaseStatus = + result = b64ce(Base64UrlPad.decode(inbytes, outbytes, outlen)) + const MultibaseCodecs = [ MBCodec(name: "identity", code: chr(0x00), @@ -262,10 +315,18 @@ const MBCodec(name: "base58btc", code: 'z', decr: b58bd, encr: b58be, decl: b58dl, encl: b58el ), - MBCodec(name: "base64", code: 'm'), - MBCodec(name: "base64pad", code: 'M'), - MBCodec(name: "base64url", code: 'u'), - MBCodec(name: "base64urlpad", code: 'U') + MBCodec(name: "base64", code: 'm', + decr: b64d, encr: b64e, decl: b64dl, encl: b64el + ), + MBCodec(name: "base64pad", code: 'M', + decr: b64pd, encr: b64pe, decl: b64pdl, encl: b64pel + ), + MBCodec(name: "base64url", code: 'u', + decr: b64ud, encr: b64ue, decl: b64dl, encl: b64el + ), + MBCodec(name: "base64urlpad", code: 'U', + decr: b64upd, encr: b64upe, decl: b64pdl, encl: b64pel + ) ] proc initMultiBaseCodeTable(): Table[char, MBCodec] {.compileTime.} = diff --git a/tests/testbase64.nim b/tests/testbase64.nim new file mode 100644 index 000000000..61d5b5d0b --- /dev/null +++ b/tests/testbase64.nim @@ -0,0 +1,162 @@ +import unittest +import ../libp2p/base64 + +const TVBasePadding = [ + ["f", "Zg=="], + ["fo", "Zm8="], + ["foo", "Zm9v"], + ["foob", "Zm9vYg=="], + ["fooba", "Zm9vYmE="], + ["foobar", "Zm9vYmFy"] +] + +const TVBaseNoPadding = [ + ["f", "Zg"], + ["fo", "Zm8"], + ["foo", "Zm9v"], + ["foob", "Zm9vYg"], + ["fooba", "Zm9vYmE"], + ["foobar", "Zm9vYmFy"] +] + +suite "BASE64 encoding test suite": + test "Empty seq/string test": + var empty1 = newSeq[byte]() + var empty2 = "" + var encoded = newString(16) + var decoded = newSeq[byte](16) + + var o1, o2, o3, o4: int + var e1 = Base64.encode(empty1) + var e2 = Base64Url.encode(empty1) + var e3 = Base64Pad.encode(empty1) + var e4 = Base64UrlPad.encode(empty1) + check: + Base64.encode(empty1, encoded, o1) == Base64Status.Success + Base64Url.encode(empty1, encoded, o2) == Base64Status.Success + Base64Pad.encode(empty1, encoded, o3) == Base64Status.Success + Base64UrlPad.encode(empty1, encoded, o4) == Base64Status.Success + len(e1) == 0 + len(e2) == 0 + len(e3) == 0 + len(e4) == 0 + o1 == 0 + o2 == 0 + o3 == 0 + o4 == 0 + var d1 = Base64.decode("") + var d2 = Base64Url.decode("") + var d3 = Base64Pad.decode("") + var d4 = Base64UrlPad.decode("") + check: + Base64.decode(empty2, decoded, o1) == Base64Status.Success + Base64Url.decode(empty2, decoded, o2) == Base64Status.Success + Base64Pad.decode(empty2, decoded, o3) == Base64Status.Success + Base64UrlPad.decode(empty2, decoded, o4) == Base64Status.Success + len(d1) == 0 + len(d2) == 0 + len(d3) == 0 + len(d4) == 0 + o1 == 0 + o2 == 0 + o3 == 0 + o4 == 0 + + test "Zero test": + var s = newString(256) + for i in 0..255: + s[i] = 'A' + var buffer: array[256, byte] + for i in 0..255: + var a = Base64.encode(buffer.toOpenArray(0, i)) + var b = Base64.decode(a) + check b == buffer[0..i] + + test "Leading zero test": + var buffer: array[256, byte] + for i in 0..255: + buffer[255] = byte(i) + var a = Base64.encode(buffer) + var b = Base64.decode(a) + check: + equalMem(addr buffer[0], addr b[0], 256) == true + + test "BASE64 padding test vectors": + for item in TVBasePadding: + let plain = cast[seq[byte]](item[0]) + let expect = item[1] + var elen = 0 + var dlen = 0 + + var e1 = Base64Pad.encode(plain) + var e2 = newString(Base64Pad.encodedLength(len(plain))) + check: + Base64Pad.encode(plain, e2, elen) == Base64Status.Success + e2.setLen(elen) + check: + e1 == expect + e2 == expect + + var d1 = Base64Pad.decode(expect) + var d2 = newSeq[byte](Base64Pad.decodedLength(len(expect))) + check: + Base64Pad.decode(expect, d2, dlen) == Base64Status.Success + d2.setLen(dlen) + check: + d1 == plain + d2 == plain + + test "BASE64 no padding test vectors": + for item in TVBaseNoPadding: + let plain = cast[seq[byte]](item[0]) + let expect = item[1] + var elen = 0 + var dlen = 0 + + var e1 = Base64.encode(plain) + var e2 = newString(Base64.encodedLength(len(plain))) + check: + Base64.encode(plain, e2, elen) == Base64Status.Success + e2.setLen(elen) + check: + e1 == expect + e2 == expect + + var d1 = Base64.decode(expect) + var d2 = newSeq[byte](Base64.decodedLength(len(expect))) + check: + Base64.decode(expect, d2, dlen) == Base64Status.Success + d2.setLen(dlen) + check: + d1 == plain + d2 == plain + + test "Buffer Overrun test": + var encres = "" + var encsize = 0 + var decres: seq[byte] = @[] + var decsize = 0 + check: + Base64.encode([0'u8], encres, encsize) == Base64Status.Overrun + encsize == Base64.encodedLength(1) + Base64.decode("AA", decres, decsize) == Base64Status.Overrun + decsize == Base64.decodedLength(2) + + test "Incorrect test": + var decres = newSeq[byte](10) + var decsize = 0 + check: + Base64.decode("A", decres, decsize) == Base64Status.Incorrect + decsize == 0 + Base64.decode("AAAAA", decres, decsize) == Base64Status.Incorrect + decsize == 0 + Base64.decode("!", decres, decsize) == Base64Status.Incorrect + decsize == 0 + Base64.decode("!!", decres, decsize) == Base64Status.Incorrect + decsize == 0 + Base64.decode("AA==", decres, decsize) == Base64Status.Incorrect + decsize == 0 + Base64.decode("_-", decres, decsize) == Base64Status.Incorrect + decsize == 0 + Base64Url.decode("/+", decres, decsize) == Base64Status.Incorrect + decsize == 0 diff --git a/tests/testmultibase.nim b/tests/testmultibase.nim index 8a67ed86d..1679e8ccd 100644 --- a/tests/testmultibase.nim +++ b/tests/testmultibase.nim @@ -62,26 +62,26 @@ const GoTestVectors = [ "z36UQrhJq9fNDS7DiAHM9YXqDHMPfr4EMArvt", "Decentralize everything!!!" ], - # [ - # "base64", - # "mRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE", - # "Decentralize everything!!!" - # ], - # [ - # "base64url", - # "uRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE", - # "Decentralize everything!!!" - # ], - # [ - # "base64pad", - # "MRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=", - # "Decentralize everything!!!" - # ], - # [ - # "base64urlpad", - # "URGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=", - # "Decentralize everything!!!" - # ], + [ + "base64", + "mRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE", + "Decentralize everything!!!" + ], + [ + "base64url", + "uRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE", + "Decentralize everything!!!" + ], + [ + "base64pad", + "MRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=", + "Decentralize everything!!!" + ], + [ + "base64urlpad", + "URGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=", + "Decentralize everything!!!" + ], ] suite "MultiBase test suite": @@ -112,10 +112,10 @@ suite "MultiBase test suite": MultiBase.encode("base32padupper", plain) == "C" MultiBase.encode("base58btc", plain) == "z" MultiBase.encode("base58flickr", plain) == "Z" - # MultiBase.encode("base64", plain) == "m" - # MultiBase.encode("base64pad", plain) == "M" - # MultiBase.encode("base64url", plain) == "u" - # MultiBase.encode("base64urlpad", plain) == "U" + MultiBase.encode("base64", plain) == "m" + MultiBase.encode("base64pad", plain) == "M" + MultiBase.encode("base64url", plain) == "u" + MultiBase.encode("base64urlpad", plain) == "U" check: len(MultiBase.decode("\x00")) == 0 # len(MultiBase.decode("1")) == 0 @@ -134,10 +134,10 @@ suite "MultiBase test suite": len(MultiBase.decode("C")) == 0 len(MultiBase.decode("z")) == 0 len(MultiBase.decode("Z")) == 0 - # len(MultiBase.decode("m")) == 0 - # len(MultiBase.decode("M")) == 0 - # len(MultiBase.decode("u")) == 0 - # len(MultiBase.decode("U")) == 0 + len(MultiBase.decode("m")) == 0 + len(MultiBase.decode("M")) == 0 + len(MultiBase.decode("u")) == 0 + len(MultiBase.decode("U")) == 0 check: MultiBase.encode("identity", plain, enc, olens[0]) == MultiBaseStatus.Success @@ -243,14 +243,14 @@ suite "MultiBase test suite": olens[15] == 0 MultiBase.decode("Z", dec, olens[16]) == MultiBaseStatus.Success olens[16] == 0 - # MultiBase.decode("m", dec, olens[16]) == MultiBaseStatus.Success - # olens[16] == 0 - # MultiBase.decode("M", dec, olens[16]) == MultiBaseStatus.Success - # olens[16] == 0 - # MultiBase.decode("u", dec, olens[16]) == MultiBaseStatus.Success - # olens[16] == 0 - # MultiBase.decode("U", dec, olens[16]) == MultiBaseStatus.Success - # olens[16] == 0 + MultiBase.decode("m", dec, olens[16]) == MultiBaseStatus.Success + olens[16] == 0 + MultiBase.decode("M", dec, olens[16]) == MultiBaseStatus.Success + olens[16] == 0 + MultiBase.decode("u", dec, olens[16]) == MultiBaseStatus.Success + olens[16] == 0 + MultiBase.decode("U", dec, olens[16]) == MultiBaseStatus.Success + olens[16] == 0 test "go-multibase test vectors": for item in GoTestVectors: let encoding = item[0] From b185ad79f8912f13f622908f816b218ad74284bd Mon Sep 17 00:00:00 2001 From: cheatfate Date: Wed, 6 Mar 2019 03:48:22 +0200 Subject: [PATCH 10/10] Add PeerID test vectors. --- libp2p.nimble | 1 + tests/testpeer.nim | 155 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 tests/testpeer.nim diff --git a/libp2p.nimble b/libp2p.nimble index f084e0da2..5fa9bbc90 100644 --- a/libp2p.nimble +++ b/libp2p.nimble @@ -24,4 +24,5 @@ task test, "Runs the test suite": exec "nim c -r tests/testrsa" exec "nim c -r tests/tested25519" exec "nim c -r tests/testcrypto" + exec "nim c -r tests/testpeer" exec "nim c -r tests/testdaemon" diff --git a/tests/testpeer.nim b/tests/testpeer.nim new file mode 100644 index 000000000..98f5ef80e --- /dev/null +++ b/tests/testpeer.nim @@ -0,0 +1,155 @@ +## 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. + +## Test vectors was made using Go implementation +## https://github.com/libp2p/go-libp2p-peer +import unittest +import nimcrypto/utils +import ../libp2p/crypto/crypto, ../libp2p/peer, ../libp2p/base58 + +const + # Test vectors are generated using + # https://github.com/libp2p/go-libp2p-crypto/blob/master/key.go + # https://github.com/libp2p/go-libp2p-peer/blob/master/peer.go + PrivateKeys = [ + # RSA512 keys + """080012BE023082013A020100024100CCE59D6260E7D511DB3DD2C8A3CD5B0F1 + D16C8A6FD6A331A211305B677EADF48A7B06F0A74088E96458F23B453EC03A6 + 63985D32105017A26F7EEE8CF43FAB110203010001024056D5609396B9E9447 + E037F56E4845CF8D4BD19201BF96BDAC0F58CD03D95DE35EBB015A88FD8B8CF + 731F5604C3895E05A1268D218E64E0C077422AAB3762E801022100F5C1FAAC2 + 59F7A765C2F7B5639EF537CFAB102989924552C0EB3608936623DC1022100D5 + 6FAFBD6ED32E7C02D310CC5355EA4B85704D1F73AECCCAB28BDD3BBEC461510 + 220056286B796EB73A77C459E12399AC0DD9030A8B9E741208CA295D39BAE11 + 6101022016D390070170427B6ECDE5F104449EFF1EB148FE412003B4F5ED716 + 6AF5B4F51022100B9F2F54717615B6B34C08571098BAE081B5DDA58A35440BC + BD0222EB250682BB""", + """080012BD0230820139020100024100A0C957A3EC20A90D511F732ECC650E4EA + EC501EC8349E8891BAF786E6AB424B5665AC229BCB241217FFC51FB7A443C85 + 688CE0D12A4C9C267B511488B537D2BB0203010001024068AD8CA448F79E77F + 007A4AAA4216AA6293C539B52824CA4EEE45768B3896297BABA5CFDAE4D1345 + 3BF3B7CEC8DF4623693B7CA7F046982A6B0CFEC722338D19022100CEC09F191 + 08A63E059DE14B057D08D81D6F8583D9CDCB89300A8D11DED084C4D022100C7 + 15CDBEDAF98D9F0A73DF4880A511B70E17835DA94C32D765B57E88AC827F270 + 220040684127CDBF4EDDD52119D0D58A628AD02D0D50244D9E322D4269E2A8B + 4AD102201316D81F90C794F8C6FBCD511A64B98A1FE3C948D5CEE4A11AB3886 + A7A8018C9022075677C2486F633F74E5718681D0C75477B62F0CD14A5C2226A + 11DEC5CFA96299""", + """080012C0023082013C020100024100D6E4788FBF4FCD607D6F61E01E910CE58 + 14B8BBA5A0F878EDF1C5ACC46A7C3FDECDEEE990D67C3DE5AE4A0301A1621BA + 1903938D08451F475393E2C863A8FE230203010001024100980168DE95BF475 + 197A8F01CEEC23FD3205065F85DB85A754F90E91152715D8AB693A37504D700 + 703C09F3B171BB5441CD6B5BF462FADDAAB896F2E4047D8681022100DFEE0F7 + 3DC6C9DC269E44A722007D937238299EEB7D07CE7B527AD9EE40A30E1022100 + F5AB120334B11F27C475385B2769448A53D40B47AA48D40AF623C7E4CB525B8 + 3022100B340507451754CCFAC8E471D9610524BB60C3C867FF3CF987745DCD0 + 7D00BF61022100DBCAC400CE41B18818C26F3B4E9565D7048CEFD29C4D3038E + 662B8D2BFFDDD1502206BC3E38AA82EBA2B99D2FF56AB19B2CE118B9A8A2AD1 + AEAF45F3ED4FE957619F""", + # Ed25519 keys + """08011240B6F99B4E4422C516F1BD135B4D2B02AE62C48388CE31AFBA16496D2 + 42FABE09BF3848ADABAA9F1E1230A3B94EDD3247C2395397EAFB59790B86595 + F94F1CD6B9""", + """08011240C1F64B208C6D3F52DDDC2CFDB41D5555956C6D6AC6A006C0547C94D + 8AD00A639AF87C6EA4451B2C7ACF7E24AB3B8FDE206A984BB0F1C1338CB17AA + F65E944007""", + """080112401C2228F2880999FEC64401DD33A48C9C56FAF47EEB715CEA57F9F3B + FEAA6E9E132EFF1CABC2A629690CCE7978241315A965F3A1702AC63860BE42D + 72265EF250""", + # ECDSA keys + """08031279307702010104203E5B1FE9712E6C314942A750BD67485DE3C1EFE85 + B1BFB520AE8F9AE3DFA4A4CA00A06082A8648CE3D030107A14403420004DE3D + 300FA36AE0E8F5D530899D83ABAB44ABF3161F162A4BC901D8E6ECDA020E8B6 + D5F8DA30525E71D6851510C098E5C47C646A597FB4DCEC034E9F77C409E62""", + """080312793077020101042027DD515F39628504923D5A8935A95DE4AECE05AF2 + 451067A8B584887D67B6799A00A06082A8648CE3D030107A14403420004B544 + 92DA731A868A0F249288F43BBAEEC5FB166BB430F4AD256399FBD67FD6255BD + 5ADE57BA29BC6EF680D66A574788A03EC30B9D2F1C27A483E59FA62F6B03C""", + """0803127930770201010420746C012FB8E6882BC696AFAAFBCC4B16F8674C1B0 + 07A7F949EF0D6D485171ACEA00A06082A8648CE3D030107A144034200043E41 + 50BEB59FEAAC43389ABC490E11172750A94A01D155FE553DA9F559CE6687CDF + 6160B6C11BDD02F58D5E28A2BB1C59F991CE52A49618185C82E750A044979""", + # Secp256k1 keys + # "0802122053DADF1D5A164D6B4ACDB15E24AA4C5B1D3461BDBD42ABEDB0A4404D56CED8FB", + # "08021220FD659951E2ED440CC7ECE436357D123D4C8B3CF1056E3F1607FF3641FB578A1B", + # "08021220B333BE3E843339E0E2CE9E083ABC119BE05C7B65B8665ADE19E172D47BF91305" + ] + + PeerIDs = [ + "Qmdxy8GAu1pvi35xBAie9sMpMN4G9p6GK6WCNbSCDCDgyp", + "QmczLMwRH4cNaLZx1t7PTe5b7k6xKkd3RCJZSxmG3azXJK", + "QmS9Sg4ZA5Fd1hHjNNkqfuMUXX9QnEXxgvT5hfSRuKHp8p", + "12D3KooWSCxTfVvMBJbpF75PQmnFdxdBfC1ZxAGYbFc3U9MjALXz", + "12D3KooWMdZbdEudjgnCvQLoSoiqhQ4ET2gaA1d4JpC1CBkUnfzn", + "12D3KooWDFCm93uCnm8tVdk3DYxNeMFxMGBaywVSt8a8ULtdLoeX", + "QmVMT29id3TUASyfZZ6k9hmNyc2nYabCo4uMSpDw4zrgDk", + "QmXz4wPSQqYF33qB7JRdSExETu56HgWRpE9bsf75HgeXL5", + "Qmcfz2MaPjw44RfVpHKFgXwhW3uFBRBxByVEkgPhefKCJW", + # Secp256k1 peer ids + # "16Uiu2HAmLhLvBoYaoZfaMUKuibM6ac163GwKY74c5kiSLg5KvLpY", + # "16Uiu2HAmRRrT319h5upVoC3E8vs1Qej4UF3vPPnLgrhbpHhUb2Av", + # "16Uiu2HAmDrDaty3uYPgqSr1h5Cup32S2UdYo46rhqZfXPjJMABZL" + ] + +suite "Peer testing suite": + test "Go PeerID test vectors": + for i in 0..