Merge pull request #12 from status-im/peer

Peer
This commit is contained in:
Eugene Kabanov 2019-03-06 04:20:07 +02:00 committed by GitHub
commit 6321689c3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 1010 additions and 153 deletions

View File

@ -1,5 +1,5 @@
import asyncdispatch2, nimcrypto, strutils
import ../libp2p/daemon/daemonapi, ../libp2p/[base58, multicodec, multiaddress]
import chronos, nimcrypto, strutils
import ../libp2p/daemon/daemonapi
import hexdump
const
@ -9,28 +9,40 @@ 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 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})
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()
asyncCheck monitor(api)
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
echo "= Recieved pubsub message with length ", msglen,
" bytes from peer ", message.peer.pretty()
echo dumpHex(message.data)
await api.dumpSubscribedPeers()
result = true

View File

@ -1,5 +1,8 @@
import asyncdispatch2, nimcrypto, strutils
import ../libp2p/daemon/daemonapi, ../libp2p/[base58, multiaddress]
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"
@ -39,10 +42,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 +60,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 +69,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 +83,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:
@ -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
@ -112,7 +115,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 +124,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:

View File

@ -1,5 +1,8 @@
import asyncdispatch2, nimcrypto, strutils, os
import ../libp2p/daemon/daemonapi, ../libp2p/[base58, multiaddress]
import chronos, nimcrypto, strutils, os
import ../libp2p/daemon/daemonapi
const
PubSubTopic = "test-net"
proc main(bn: string) {.async.} =
echo "= Starting P2P node"
@ -8,24 +11,32 @@ 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
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)
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:

View File

@ -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"
@ -23,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"

View File

@ -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..<reminder:
if (cast[byte](instr[i + j]) and 0x80'u8) != 0:
outlen = 0
zeroMem(addr outbytes[0], i + 8)
result = Base32Status.Incorrect
return
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)
result = Base32Status.Incorrect
return
buffer[j] = cast[byte](ch)
@ -287,4 +282,4 @@ proc decode*[T: byte|char](btype: typedesc[Base32Types],
if btype.decode(instr, result, length) == Base32Status.Success:
result.setLen(length)
else:
raise newException(Base32Error, "Incorrect base58 string")
raise newException(Base32Error, "Incorrect base32 string")

238
libp2p/base64.nim Normal file
View File

@ -0,0 +1,238 @@
## 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 implements BASE64 encoding and decoding procedures.
type
Base64Status* {.pure.} = enum
Error,
Success,
Incorrect,
Overrun
Base64Alphabet* = object
decode*: array[128, int8]
encode*: array[64, uint8]
Base64* = object
## Type to use RFC4648 alphabet without padding
Base64Pad* = object
## Type to use RFC4648 alphabet with padding
Base64Url* = object
## Type to use RFC4648 URL alphabet without padding
Base64UrlPad* = object
## Type to use RFC4648 URL alphabet with padding
Base64PadTypes* = Base64Pad | Base64UrlPad
## All types with padding support
Base64NoPadTypes* = Base64 | Base64Url
## All types without padding support
Base64Types* = Base64 | Base64Pad | Base64Url | Base64UrlPad
## All types
Base64Error* = object of Exception
## Base64 specific exception type
proc newAlphabet64*(s: string): Base64Alphabet =
doAssert(len(s) == 64)
for i in 0..<len(s):
result.encode[i] = cast[uint8](s[i])
for i in 0..<len(result.decode):
result.decode[i] = -1
for i in 0..<len(result.encode):
result.decode[int(result.encode[i])] = int8(i)
const
B64Alphabet* = newAlphabet64("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef" &
"ghijklmnopqrstuvwxyz0123456789+/")
B64UrlAlphabet* = newAlphabet64("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef" &
"ghijklmnopqrstuvwxyz0123456789-_")
proc encodedLength*(btype: typedesc[Base64Types],
length: int): int {.inline.} =
## Return estimated length of BASE64 encoded value for plain length
## ``length``.
result = (((length + 2) div 3) * 4) + 1
proc decodedLength*(btype: typedesc[Base64Types],
length: int): int {.inline.} =
## Return estimated length of decoded value of BASE64 encoded value of length
## ``length``.
when (btype is Base64Pad) or (btype is Base64UrlPad):
result = ((length + 3 - 1) div 3) * 4
elif (btype is Base64) or (btype is Base64Url):
result = (length * 4 + 3 - 1) div 3
proc encode*(btype: typedesc[Base64Types], inbytes: openarray[byte],
outstr: var openarray[char], outlen: var int): Base64Status =
## Encode array of bytes ``inbytes`` using BASE64 encoding and store
## result to ``outstr``.
##
## On success ``Base64Status.Success`` will be returned and ``outlen`` will
## be set to number of characters stored inside of ``outstr``.
##
## If length of ``outstr`` is not enough then ``Base64Status.Overrun`` will
## be returned and ``outlen`` will be set to number of characters required.
when (btype is Base64) or (btype is Base64Pad):
const alphabet = B64Alphabet
elif (btype is Base64Url) or (btype is Base64UrlPad):
const alphabet = B64UrlAlphabet
let length = len(inbytes)
if len(outstr) < btype.encodedLength(length):
outlen = btype.encodedLength(length)
result = Base64Status.Overrun
else:
var offset = 0
var i = 0
while i < (length - 2):
outstr[offset] = chr(alphabet.encode[(inbytes[i] shr 2) and 0x3F'u8])
inc(offset)
outstr[offset] = chr(alphabet.encode[((inbytes[i] and 0x03) shl 4) or
((inbytes[i + 1] and 0xF0) shr 4)])
inc(offset)
outstr[offset] = chr(alphabet.encode[((inbytes[i + 1] and 0x0F) shl 2) or
((inbytes[i + 2] and 0xC0) shr 6)])
inc(offset)
outstr[offset] = chr(alphabet.encode[inbytes[i + 2] and 0x3F])
inc(offset)
i += 3
if i < length:
outstr[offset] = chr(alphabet.encode[(inbytes[i] shr 2) and 0x3F])
inc(offset)
if i == length - 1:
outstr[offset] = chr(alphabet.encode[(inbytes[i] and 0x03) shl 4])
inc(offset)
when (btype is Base64Pad) or (btype is Base64UrlPad):
outstr[offset] = '='
inc(offset)
else:
outstr[offset] = chr(alphabet.encode[((inbytes[i] and 0x03) shl 4) or
((inbytes[i + 1] and 0xF0) shr 4)])
inc(offset)
outstr[offset] = chr(alphabet.encode[(inbytes[i + 1] and 0x0F) shl 2])
inc(offset)
when (btype is Base64Pad) or (btype is Base64UrlPad):
outstr[offset] = '='
inc(offset)
outlen = offset
result = Base64Status.Success
proc encode*(btype: typedesc[Base64Types],
inbytes: openarray[byte]): string {.inline.} =
## Encode array of bytes ``inbytes`` using BASE64 encoding and return
## encoded string.
var size = btype.encodedLength(len(inbytes))
result = newString(size)
if btype.encode(inbytes, result.toOpenArray(0, size - 1),
size) == Base64Status.Success:
result.setLen(size)
else:
result = ""
proc decode*[T: byte|char](btype: typedesc[Base64Types], instr: openarray[T],
outbytes: var openarray[byte], outlen: var int): Base64Status =
## Decode BASE64 string and store array of bytes to ``outbytes``. On success
## ``Base64Status.Success`` will be returned and ``outlen`` will be set
## to number of bytes stored.
##
## Length of ``outbytes`` must be equal or more then ``len(instr) + 4``.
##
## If ``instr`` has characters which are not part of BASE64 alphabet, then
## ``Base64Status.Incorrect`` will be returned and ``outlen`` will be set to
## ``0``.
##
## If length of ``outbytes`` is not enough to store decoded bytes, then
## ``Base64Status.Overrun`` will be returned and ``outlen`` will be set to
## number of bytes required.
when (btype is Base64) or (btype is Base64Pad):
const alphabet = B64Alphabet
elif (btype is Base64Url) or (btype is Base64UrlPad):
const alphabet = B64UrlAlphabet
if len(instr) == 0:
outlen = 0
return Base64Status.Success
let length = btype.decodedLength(len(instr))
if length > 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..<reminder:
if (cast[byte](instr[i + j]) and 0x80'u8) != 0:
outlen = 0
return Base64Status.Incorrect
let ch = alphabet.decode[cast[int8](instr[i + j])]
if ch == -1:
outlen = 0
return Base64Status.Incorrect
buffer[j] = cast[byte](ch)
if reminder > 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")

View File

@ -10,8 +10,10 @@
## This module implementes API for `go-libp2p-daemon`.
import os, osproc, strutils, tables, streams, strtabs
import chronos
import ../varint, ../multiaddress, ../multicodec, ../base58, ../cid
import ../wire, ../protobuf/minprotobuf
import ../varint, ../multiaddress, ../multicodec, ../base58, ../cid, ../peer
import ../wire, ../multihash, ../protobuf/minprotobuf
export peer, multiaddress, multicodec, multihash, cid
when not defined(windows):
import posix
@ -76,7 +78,7 @@ type
VALUE = 1,
END = 2
PeerID* = seq[byte]
# PeerID* = seq[byte]
MultiProtocol* = string
LibP2PPublicKey* = seq[byte]
DHTValue* = seq[byte]
@ -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:
@ -745,8 +746,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 +803,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 +825,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 +928,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 +1077,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 +1155,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 +1176,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 +1231,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..<length:

View File

@ -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``.
@ -570,7 +571,7 @@ proc init*(mtype: typedesc[MultiAddress], protocol: int,
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")
@ -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.} =

View File

@ -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.} =

View File

@ -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
),

195
libp2p/peer.nim Normal file
View File

@ -0,0 +1,195 @@
## 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.
import hashes
import nimcrypto/utils
import crypto/crypto, multicodec, multihash, base58, vbuffer
import protobuf/minprotobuf
const
maxInlineKeyLength* = 42
type
PeerID* = object
data*: seq[byte]
PeerIDError* = object of Exception
proc pretty*(pid: PeerID): string {.inline.} =
## Return base58 encoded ``pid`` representation.
result = Base58.encode(pid.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 ``pid``.
result = len(pid.data)
if len(data) >= result and result > 0:
copyMem(addr data[0], unsafeAddr pid.data[0], result)
proc getBytes*(pid: PeerID): seq[byte] {.inline.} =
## Return PeerID ``pid`` as array of bytes.
result = pid.data
proc hex*(pid: PeerID): string {.inline.} =
## Returns hexadecimal string representation of ``pid``.
if len(pid.data) > 0:
result = toHex(pid.data)
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``.
## Returns:
##
## | 0 iff a == b
## | < 0 iff a < b
## | > 0 iff a > b
var i = 0
var m = min(len(a.data), len(b.data))
while i < m:
result = ord(a.data[i]) - ord(b.data[i])
if result != 0: return
inc(i)
result = len(a.data) - len(b.data)
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*(pid: PeerID): Hash {.inline.} =
result = hash(pid.data)
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*(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*(pid: PeerID, pubkey: var PublicKey): bool =
## Returns ``true`` if public key was successfully decoded from PeerID
## ``pid``and stored to ``pubkey``.
##
## Returns ``false`` otherwise.
var mh: MultiHash
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 `$`*(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(spid[i])
result.add("*")
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 = 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.
##
## 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: 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``.
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.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*(pid: PeerID, pubkey: PublicKey): bool {.inline.} =
## Returns ``true`` if ``pid`` matches public key ``pubkey``.
result = (pid == PeerID.init(pubkey))
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, pid: PeerID) {.inline.} =
## Write PeerID value ``peerid`` to buffer ``vb``.
vb.writeSeq(pid.data)
proc initProtoField*(index: int, pid: PeerID): ProtoField =
## Initialize ProtoField with PeerID ``value``.
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 pid: PeerID
result = getLengthValue(data, field, pid.data)
if result > 0:
if not pid.validate():
result = -1
else:
value = pid

162
tests/testbase64.nim Normal file
View File

@ -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

View File

@ -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()

View File

@ -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]

View File

@ -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

155
tests/testpeer.nim Normal file
View File

@ -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..<len(PrivateKeys):
var seckey = PrivateKey.init(stripSpaces(PrivateKeys[i]))
var pubkey = seckey.getKey()
var p1 = PeerID.init(seckey)
var p2 = PeerID.init(pubkey)
var p3 = PeerID.init(PeerIDs[i])
var b1 = Base58.decode(PeerIDs[i])
var p4 = PeerID.init(b1)
var buf1 = newSeq[byte](len(p1))
var buf2 = newSeq[byte](len(p2))
var buf3 = newSeq[byte](len(p3))
var buf4 = newSeq[byte](len(p4))
check:
p1 == p3
p2 == p3
p4 == p3
p1 == p2
p1 == p4
p2 == p4
p1.pretty() == PeerIDs[i]
p2.pretty() == PeerIDs[i]
p3.pretty() == PeerIDs[i]
p4.pretty() == PeerIDs[i]
p1.match(seckey) == true
p1.match(pubkey) == true
p1.getBytes() == p2.getBytes()
p1.getBytes() == p3.getBytes()
p1.getBytes() == p4.getBytes()
p1.toBytes(buf1) == len(p1)
p1.toBytes(buf2) == len(p2)
p1.toBytes(buf3) == len(p3)
p1.toBytes(buf4) == len(p4)
p1.validate() == true
p2.validate() == true
p3.validate() == true
p4.validate() == true
$p1 == $p2
$p1 == $p3
$p1 == $p4
if i in {3, 4, 5}:
var ekey1, ekey2, ekey3, ekey4: PublicKey
check:
p1.hasPublicKey() == true
p2.hasPublicKey() == true
p3.hasPublicKey() == true
p4.hasPublicKey() == true
p1.extractPublicKey(ekey1) == true
p2.extractPublicKey(ekey2) == true
p3.extractPublicKey(ekey3) == true
p4.extractPublicKey(ekey4) == true
ekey1 == pubkey
ekey2 == pubkey
ekey3 == pubkey
ekey4 == pubkey