API reference: libp2p

base32

Types

Base32Status

undefined

Base32Status {.pure.} = enum
  Error, Success, Incorrect, Overrun
1
2

Base32Alphabet

undefined

Base32Alphabet = object
  decode*: array[128, int8]
  encode*: array[32, uint8]
1
2
3

Base32Upper

Type to use RFC4648 alphabet in uppercase without padding

Base32Upper = object
1

Base32Lower

Type to use RFC4648 alphabet in lowercase without padding

Base32Lower = object
1

Base32UpperPad

Type to use RFC4648 alphabet in uppercase with padding

Base32UpperPad = object
1

Base32LowerPad

Type to use RFC4648 alphabet in lowercase with padding

Base32LowerPad = object
1

HexBase32Upper

Type to use RFC4648-HEX alphabet in uppercase without padding

HexBase32Upper = object
1

HexBase32Lower

Type to use RFC4648-HEX alphabet in lowercase without padding

HexBase32Lower = object
1

HexBase32UpperPad

Type to use RFC4648-HEX alphabet in uppercase with padding

HexBase32UpperPad = object
1

HexBase32LowerPad

Type to use RFC4648-HEX alphabet in lowercase with padding

HexBase32LowerPad = object
1

Base32

By default we are using RFC4648 alphabet in uppercase without padding

Base32 = Base32Upper
1

Base32PadTypes

All types with padding support

Base32PadTypes = Base32UpperPad | Base32LowerPad | HexBase32UpperPad |
    HexBase32LowerPad
1
2

Base32NoPadTypes

All types without padding

Base32NoPadTypes = Base32Upper | Base32Lower | HexBase32Upper | HexBase32Lower
1

Base32Types

Supported types

Base32Types = Base32NoPadTypes | Base32PadTypes
1

Base32Error

Base32 specific exception type

Base32Error = object of CatchableError
1

Procs


newAlphabet32

undefined

proc newAlphabet32(s: string): Base32Alphabet {.raises: [], tags: [].}
1

encodedLength

Return estimated length of BASE32 encoded value for plain length length.

proc encodedLength(btype: typedesc[Base32Types]; length: int): int
1

decodedLength

Return estimated length of decoded value of BASE32 encoded value of length length.

proc decodedLength(btype: typedesc[Base32Types]; length: int): int
1

encode

Encode array of bytes inbytes using BASE32 encoding and store result to outstr. On success Base32Status.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 Base32Status.Overrun will be returned and outlen will be set to number of characters required.

proc encode(btype: typedesc[Base32Types]; inbytes: openArray[byte];
           outstr: var openArray[char]; outlen: var int): Base32Status
1
2

encode

Encode array of bytes inbytes using BASE32 encoding and return encoded string.

proc encode(btype: typedesc[Base32Types]; inbytes: openArray[byte]): string {.inline.}
1

decode

Decode BASE32 string and store array of bytes to outbytes. On success Base32Status.Success will be returned and outlen will be set to number of bytes stored.

## If length of outbytes is not enough to store decoded bytes, then Base32Status.Overrun will be returned and outlen will be set to number of bytes required.

proc decode[T: byte | char](btype: typedesc[Base32Types]; instr: openArray[T];
                        outbytes: var openArray[byte]; outlen: var int): Base32Status
1
2

decode

Decode BASE32 string instr and return sequence of bytes as result.

proc decode[T: byte | char](btype: typedesc[Base32Types]; instr: openArray[T]): seq[byte]
1

Templates


base58

Types

Base58Status

undefined

Base58Status {.pure.} = enum
  Error, Success, Incorrect, Overrun
1
2

Base58Alphabet

undefined

Base58Alphabet = object
  decode*: array[128, int8]
  encode*: array[58, uint8]
1
2
3

BTCBase58

Type to use Bitcoin alphabet

BTCBase58 = object
1

FLCBase58

Type to use Flickr alphabet

FLCBase58 = object
1

Base58

By default we are using Bitcoin alphabet

Base58 = BTCBase58
1

Base58C

Supported types

Base58C = BTCBase58 | FLCBase58
1

Base58Error

Base58 specific exception type

Base58Error = object of CatchableError
1

Procs


newAlphabet58

undefined

proc newAlphabet58(s: string): Base58Alphabet {.raises: [], tags: [].}
1

encodedLength

Return estimated length of BASE58 encoded value for plain length length.

proc encodedLength(btype: typedesc[Base58C]; length: int): int
1

decodedLength

Return estimated length of decoded value of BASE58 encoded value of length length.

proc decodedLength(btype: typedesc[Base58C]; length: int): int
1

encode

Encode array of bytes inbytes using BASE58 encoding and store result to outstr. On success Base58Status.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 Base58Status.Overrun will be returned and outlen will be set to number of characters required.

proc encode(btype: typedesc[Base58C]; inbytes: openArray[byte];
           outstr: var openArray[char]; outlen: var int): Base58Status
1
2

encode

Encode array of bytes inbytes using BASE58 encoding and return encoded string.

proc encode(btype: typedesc[Base58C]; inbytes: openArray[byte]): string {.inline.}
1

decode

Decode BASE58 string and store array of bytes to outbytes. On success Base58Status.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 BASE58 alphabet, then Base58Status.Incorrect will be returned and outlen will be set to 0.

If length of outbytes is not enough to store decoded bytes, then Base58Status.Overrun will be returned and outlen will be set to number of bytes required.

proc decode[T: byte | char](btype: typedesc[Base58C]; instr: openArray[T];
                        outbytes: var openArray[byte]; outlen: var int): Base58Status
1
2

decode

Decode BASE58 string instr and return sequence of bytes as result.

proc decode(btype: typedesc[Base58C]; instr: string): seq[byte]
1

Templates


base64

Types

Base64Status

undefined

Base64Status {.pure.} = enum
  Error, Success, Incorrect, Overrun
1
2

Base64Alphabet

undefined

Base64Alphabet = object
  decode*: array[128, int8]
  encode*: array[64, uint8]
1
2
3

Base64

Type to use RFC4648 alphabet without padding

Base64 = object
1

Base64Pad

Type to use RFC4648 alphabet with padding

Base64Pad = object
1

Base64Url

Type to use RFC4648 URL alphabet without padding

Base64Url = object
1

Base64UrlPad

Type to use RFC4648 URL alphabet with padding

Base64UrlPad = object
1

Base64PadTypes

All types with padding support

Base64PadTypes = Base64Pad | Base64UrlPad
1

Base64NoPadTypes

All types without padding support

Base64NoPadTypes = Base64 | Base64Url
1

Base64Types

All types

Base64Types = Base64 | Base64Pad | Base64Url | Base64UrlPad
1

Base64Error

Base64 specific exception type

Base64Error = object of CatchableError
1

Procs


newAlphabet64

undefined

proc newAlphabet64(s: string): Base64Alphabet {.raises: [], tags: [].}
1

encodedLength

Return estimated length of BASE64 encoded value for plain length length.

proc encodedLength(btype: typedesc[Base64Types]; length: int): int {.inline.}
1

decodedLength

Return estimated length of decoded value of BASE64 encoded value of length length.

proc decodedLength(btype: typedesc[Base64Types]; length: int): int {.inline.}
1

encode

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.

proc encode(btype: typedesc[Base64Types]; inbytes: openArray[byte];
           outstr: var openArray[char]; outlen: var int): Base64Status
1
2

encode

Encode array of bytes inbytes using BASE64 encoding and return encoded string.

proc encode(btype: typedesc[Base64Types]; inbytes: openArray[byte]): string {.inline.}
1

decode

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.

proc decode[T: byte | char](btype: typedesc[Base64Types]; instr: openArray[T];
                        outbytes: var openArray[byte]; outlen: var int): Base64Status
1
2

decode

Decode BASE64 string instr and return sequence of bytes as result.

proc decode[T: byte | char](btype: typedesc[Base64Types]; instr: openArray[T]): seq[byte]
1

Templates


cid

Types

CidStatus

undefined

CidStatus {.pure.} = enum
  Error, Success, Incorrect, Overrun
1
2

CidVersion

undefined

CidVersion = enum
  CIDvIncorrect, CIDv0, CIDv1, CIDvReserved
1
2

Cid

undefined

Cid = object
  cidver*: CidVersion
  mcodec*: MultiCodec
  hpos*: int
  data*: VBuffer
1
2
3
4
5

CidError

undefined

CidError = object of CatchableError
1

Procs


validate

Returns true is data has valid binary CID representation.

proc validate(ctype: typedesc[Cid]; data: openArray[byte]): bool
1

mhash

Returns MultiHash part of CID.

proc mhash(cid: Cid): MultiHash {.raises: [CidError, Exception, MultiHashError],
                              tags: [RootEffect].}
1
2

contentType

Returns content type part of CID

proc contentType(cid: Cid): MultiCodec {.raises: [CidError], tags: [].}
1

version

Returns CID version

proc version(cid: Cid): CidVersion {.raises: [], tags: [].}
1

init

Create new content identifier using array of bytes or string data.

proc init[T: char | byte](ctype: typedesc[Cid]; data: openArray[T]): Cid
1

init

Create new content identifier using content type content and MultiHash hash using version version.

To create CIDv0 you need to use: Cid.init(CIDv0, multiCodec("dag-pb"), MultiHash.digest("sha2-256", data))

All other encodings and hashes are not supported by CIDv0.

proc init(ctype: typedesc[Cid]; version: CidVersion; content: MultiCodec;
         hash: MultiHash): Cid
1
2

==

Compares content identifiers a and b, returns true if hashes are equal, false otherwise.

proc `==`(a: Cid; b: Cid): bool {.raises: [Exception, MultiHashError], tags: [RootEffect].}
1

base58

Get BASE58 encoded string representation of content identifier cid.

proc base58(cid: Cid): string {.raises: [], tags: [].}
1

hex

Get hexadecimal string representation of content identifier cid.

proc hex(cid: Cid): string {.raises: [], tags: [].}
1

repr

Get string representation of content identifier cid.

proc repr(cid: Cid): string {.raises: [MultiCodecError, CidError, Exception,
                                  MultiHashError], tags: [RootEffect].}
1
2

write

Write CID value cid to buffer vb.

proc write(vb: var VBuffer; cid: Cid) {.inline, raises: [], tags: [].}
1

encode

Get MultiBase encoded representation of cid using encoding encoding.

proc encode(mbtype: typedesc[MultiBase]; encoding: string; cid: Cid): string {.inline.}
1

`####

Return official string representation of content identifier cid.

proc `#### `####  

 Return official string representation of content identifier <tt class="docutils literal"><span class="pre">cid</span></tt>. 

```nim(cid: Cid): string {.raises: [MultiBaseError, Exception], tags: [RootEffect].}
1
2
3
4
5

Templates


multiaddress

Types

MAKind

undefined

MAKind = enum
  None, Fixed, Length, Path, Marker
1
2

MAProtocol

undefined

MAProtocol = object
  mcodec*: MultiCodec
  size*: int
  kind: MAKind
  coder*: Transcoder
1
2
3
4
5

MultiAddress

undefined

MultiAddress = object
  data*: VBuffer
1
2

MaPatternOp

undefined

MaPatternOp = enum
  Eq, Or, And
1
2

MaPattern

undefined

MaPattern = object
  operator*: MaPatternOp
  args*: seq[MaPattern]
  value*: MultiCodec
1
2
3
4

MaPatResult

undefined

MaPatResult = object
  flag*: bool
  rem*: seq[MultiCodec]
1
2
3

MultiAddressError

undefined

MultiAddressError = object of CatchableError
1

Procs


shcopy

undefined

proc shcopy(m1: var MultiAddress; m2: MultiAddress) {.raises: [], tags: [].}
1

protoCode

Returns MultiAddress ma protocol code.

proc protoCode(ma: MultiAddress): MultiCodec {.raises: [MultiAddressError], tags: [].}
1

protoName

Returns MultiAddress ma protocol name.

proc protoName(ma: MultiAddress): string {.raises: [MultiAddressError, MultiCodecError],
                                       tags: [].}
1
2

protoArgument

Returns MultiAddress ma protocol argument value.

If current MultiAddress do not have argument value, then result will be 0.

proc protoArgument(ma: MultiAddress; value: var openArray[byte]): int {.
    raises: [MultiAddressError], tags: [].}
1
2

[]

Returns part with index i of MultiAddress ma.

proc `[]`(ma: MultiAddress; i: int): MultiAddress {.inline,
    raises: [MultiAddressError], tags: [].}
1
2

`####

Return string representation of MultiAddress value.

proc `#### `####  

 Return string representation of MultiAddress <tt class="docutils literal"><span class="pre">value</span></tt>. 

```nim(value: MultiAddress): string {.raises: [MultiAddressError, Exception,
    MultiCodecError], tags: [RootEffect].}
1
2
3
4
5
6

protocols

Returns list of protocol codecs inside of MultiAddress value.

proc protocols(value: MultiAddress): seq[MultiCodec] {.raises: [MultiAddressError],
    tags: [].}
1
2

hex

Return hexadecimal string representation of MultiAddress value.

proc hex(value: MultiAddress): string {.raises: [], tags: [].}
1

write

Write MultiAddress value ma to buffer vb.

proc write(vb: var VBuffer; ma: MultiAddress) {.inline, raises: [], tags: [].}
1

encode

Get MultiBase encoded representation of ma using encoding encoding.

proc encode(mbtype: typedesc[MultiBase]; encoding: string; ma: MultiAddress): string {.
    inline.}
1
2

validate

Returns true if MultiAddress ma is valid.

proc validate(ma: MultiAddress): bool {.raises: [Exception], tags: [RootEffect].}
1

init

Initialize MultiAddress object from protocol id protocol and array of bytes value.

proc init(mtype: typedesc[MultiAddress]; protocol: MultiCodec; value: openArray[byte]): MultiAddress
1

init

Initialize MultiAddress object from protocol id protocol and peer id value.

proc init(mtype: typedesc[MultiAddress]; protocol: MultiCodec; value: PeerID): MultiAddress {.
    inline.}
1
2

init

Initialize MultiAddress object from protocol id protocol.

proc init(mtype: typedesc[MultiAddress]; protocol: MultiCodec): MultiAddress
1

init

Initialize MultiAddress object from string representation value.

proc init(mtype: typedesc[MultiAddress]; value: string): MultiAddress
1

init

Initialize MultiAddress with array of bytes data.

proc init(mtype: typedesc[MultiAddress]; data: openArray[byte]): MultiAddress
1

init

Initialize empty MultiAddress.

proc init(mtype: typedesc[MultiAddress]): MultiAddress
1

init

Initialize MultiAddress using stdlib's net.IpAddress (IPv4/IPv6) and net.Protocol (UDP/TCP) information.

proc init(mtype: typedesc[MultiAddress]; address: IpAddress; protocol: Protocol;
         port: Port): MultiAddress
1
2

isEmpty

Returns true, if MultiAddress ma is empty or non initialized.

proc isEmpty(ma: MultiAddress): bool {.raises: [], tags: [].}
1

&amp;

Concatenates two addresses m1 and m2, and returns result.

This procedure performs validation of concatenated result and can raise exception on error.

proc `&`(m1, m2: MultiAddress): MultiAddress {.
    raises: [Exception, MultiAddressError], tags: [RootEffect].}
1
2

&amp;=

Concatenates two addresses m1 and m2.

This procedure performs validation of concatenated result and can raise exception on error.

proc `&=`(m1: var MultiAddress; m2: MultiAddress) {.
    raises: [Exception, MultiAddressError], tags: [RootEffect].}
1
2

isWire

Returns true if MultiAddress ma is one of:

  • {IP4}/{TCP, UDP}
  • {IP6}/{TCP, UDP}
  • {UNIX}/{PATH}

proc isWire(ma: MultiAddress): bool {.raises: [], tags: [].}
1

match

Match full address using pattern pat and return true if address satisfies pattern.

proc match(pat: MaPattern; address: MultiAddress): bool {.raises: [MultiAddressError],
    tags: [].}
1
2

matchPartial

Match prefix part of address using pattern pat and return true if address starts with pattern.

proc matchPartial(pat: MaPattern; address: MultiAddress): bool {.
    raises: [MultiAddressError], tags: [].}
1
2

`####

Return pattern pat as string.

proc `#### `####  

 Return pattern <tt class="docutils literal"><span class="pre">pat</span></tt> as string. 

```nim(pat: MaPattern): string {.raises: [MultiCodecError], tags: [].}
1
2
3
4
5

Templates


multibase

Types

MultibaseStatus

undefined

MultibaseStatus {.pure.} = enum
  Error, Success, Overrun, Incorrect, BadCodec, NotSupported
1
2

MultiBase

undefined

MultiBase = object
1

MultiBaseError

undefined

MultiBaseError = object of CatchableError
1

Procs


encodedLength

Return estimated size of buffer to store MultiBase encoded value with encoding encoding of length length.

Procedure returns -1 if encoding scheme is not supported or not present.

proc encodedLength(mbtype: typedesc[MultiBase]; encoding: string; length: int): int
1

decodedLength

Return estimated size of buffer to store MultiBase decoded value with encoding character encoding of length length.

proc decodedLength(mbtype: typedesc[MultiBase]; encoding: char; length: int): int
1

encode

Encode array inbytes using MultiBase encoding scheme encoding and store encoded value to outbytes.

If encoding is not supported MultiBaseStatus.NotSupported will be returned.

If encoding is not correct string, then MultBaseStatus.BadCodec will be returned.

If length of outbytes is not enough to store encoded result, then MultiBaseStatus.Overrun error will be returned and outlen will be set to length required.

On successfull encoding MultiBaseStatus.Success will be returned and outlen will be set to number of encoded octets (bytes).

proc encode(mbtype: typedesc[MultiBase]; encoding: string; inbytes: openArray[byte];
           outbytes: var openArray[char]; outlen: var int): MultibaseStatus
1
2

decode

Decode array inbytes using MultiBase encoding and store decoded value to outbytes.

If inbytes is not correct MultiBase string, then MultiBaseStatus.BadCodec if first character is wrong, or MultiBaseStatus.Incorrect if string has incorrect characters for such encoding.

If length of outbytes is not enough to store decoded result, then MultiBaseStatus.Overrun error will be returned and outlen will be set to length required.

On successfull decoding MultiBaseStatus.Success will be returned and outlen will be set to number of encoded octets (bytes).

proc decode(mbtype: typedesc[MultiBase]; inbytes: openArray[char];
           outbytes: var openArray[byte]; outlen: var int): MultibaseStatus
1
2

encode

Encode array inbytes using MultiBase encoding scheme encoding and return encoded string.

proc encode(mbtype: typedesc[MultiBase]; encoding: string; inbytes: openArray[byte]): string
1

decode

Decode MultiBase encoded array inbytes and return decoded sequence of bytes.

proc decode(mbtype: typedesc[MultiBase]; inbytes: openArray[char]): seq[byte]
1

Templates


multicodec

Types

MultiCodec

undefined

MultiCodec = distinct int
1

MultiCodecError

undefined

MultiCodecError = object of CatchableError
1

Procs


multiCodec

Generate MultiCodec from string name at compile time.

proc multiCodec(name: string): MultiCodec {.compileTime, raises: [MultiCodecError],
                                        tags: [].}
1
2

multiCodec

Generate MultiCodec from integer code at compile time.

proc multiCodec(code: int): MultiCodec {.compileTime, raises: [MultiCodecError],
                                     tags: [].}
1
2

`####

Returns string representation of MultiCodec mc.

proc `#### `####  

 Returns string representation of MultiCodec <tt class="docutils literal"><span class="pre">mc</span></tt>. 

```nim(mc: MultiCodec): string {.raises: [MultiCodecError], tags: [].}
1
2
3
4
5

==

Compares MultiCodec mc with string name.

proc `==`(mc: MultiCodec; name: string): bool {.inline, raises: [], tags: [].}
1

==

Compares MultiCodec mc with integer code.

proc `==`(mc: MultiCodec; code: int): bool {.inline, raises: [], tags: [].}
1

==

Returns true if MultiCodecs a and b are equal.

proc `==`(a, b: MultiCodec): bool {.raises: [], tags: [].}
1

!=

Returns true if MultiCodecs a and b are not equal.

proc `!=`(a, b: MultiCodec): bool {.raises: [], tags: [].}
1

hash

Hash procedure for tables.

proc hash(m: MultiCodec): Hash {.inline, raises: [], tags: [].}
1

codec

Return MultiCodec from string representation name. If name is not valid multicodec name, then InvalidMultiCodec will be returned.

proc codec(mt: typedesc[MultiCodec]; name: string): MultiCodec {.inline.}
1

codec

Return MultiCodec from integer representation code. If code is not valid multicodec code, then InvalidMultiCodec will be returned.

proc codec(mt: typedesc[MultiCodec]; code: int): MultiCodec {.inline.}
1

write

Write MultiCodec to buffer vb.

proc write(vb: var VBuffer; mc: MultiCodec) {.inline, raises: [], tags: [].}
1

Templates


multihash

Types

MHashCoderProc

undefined

MHashCoderProc = proc (data: openArray[byte]; output: var openArray[byte]) {.nimcall,
    gcsafe.}
1
2

MHash

undefined

MHash = object
  mcodec*: MultiCodec
  size*: int
  coder*: MHashCoderProc
1
2
3
4

MultiHash

undefined

MultiHash = object
  data*: VBuffer
  mcodec*: MultiCodec
  size*: int
  dpos*: int
1
2
3
4
5

MultiHashError

undefined

MultiHashError = object of CatchableError
1

Procs


digest

Perform digest calculation using hash algorithm with name hashname on data array data.

proc digest(mhtype: typedesc[MultiHash]; hashname: string; data: openArray[byte]): MultiHash {.
    inline.}
1
2

digest

Perform digest calculation using hash algorithm with code hashcode on data array data.

proc digest(mhtype: typedesc[MultiHash]; hashcode: int; data: openArray[byte]): MultiHash {.
    inline.}
1
2

init

Create MultiHash from nimcrypto's MDigest object and hash algorithm name hashname.

proc init[T](mhtype: typedesc[MultiHash]; hashname: string; mdigest: MDigest[T]): MultiHash {.
    inline.}
1
2

init

Create MultiHash from nimcrypto's MDigest and hash algorithm code hashcode.

proc init[T](mhtype: typedesc[MultiHash]; hashcode: MultiCodec; mdigest: MDigest[T]): MultiHash {.
    inline.}
1
2

init

Create MultiHash from array of bytes bdigest and hash algorithm code hashcode.

proc init(mhtype: typedesc[MultiHash]; hashname: string; bdigest: openArray[byte]): MultiHash {.
    inline.}
1
2

init

Create MultiHash from array of bytes bdigest and hash algorithm code hashcode.

proc init(mhtype: typedesc[MultiHash]; hashcode: MultiCodec; bdigest: openArray[byte]): MultiHash {.
    inline.}
1
2

decode

Decode MultiHash value from array of bytes data.

On success decoded MultiHash will be stored into mhash and number of bytes consumed will be returned.

On error -1 will be returned.

proc decode(mhtype: typedesc[MultiHash]; data: openArray[byte]; mhash: var MultiHash): int
1

validate

Returns true if array of bytes data has correct MultiHash inside.

proc validate(mhtype: typedesc[MultiHash]; data: openArray[byte]): bool
1

init

Create MultiHash from bytes array data.

proc init(mhtype: typedesc[MultiHash]; data: openArray[byte]): MultiHash {.inline.}
1

init

Create MultiHash from hexadecimal string representation data.

proc init(mhtype: typedesc[MultiHash]; data: string): MultiHash {.inline.}
1

init58

Create MultiHash from BASE58 encoded string representation data.

proc init58(mhtype: typedesc[MultiHash]; data: string): MultiHash {.inline.}
1

==

Compares MultiHash with nimcrypto's MDigest[T], returns true if hashes are equal, false otherwise.

proc `==`[T](mh: MultiHash; mdigest: MDigest[T]): bool
1

==

Compares MultiHash with nimcrypto's MDigest[T], returns true if hashes are equal, false otherwise.

proc `==`[T](mdigest: MDigest[T]; mh: MultiHash): bool {.inline.}
1

==

Compares MultiHashes a and b, returns true if hashes are equal, false otherwise.

proc `==`(a: MultiHash; b: MultiHash): bool {.raises: [], tags: [].}
1

hex

Return hexadecimal string representation of MultiHash value.

proc hex(value: MultiHash): string {.raises: [], tags: [].}
1

base58

Return Base58 encoded string representation of MultiHash value.

proc base58(value: MultiHash): string {.raises: [], tags: [].}
1

`####

Return string representation of MultiHash value.

proc `#### `####  

 Return string representation of MultiHash <tt class="docutils literal"><span class="pre">value</span></tt>. 

```nim(mh: MultiHash): string {.raises: [MultiCodecError], tags: [].}
1
2
3
4
5

write

Write MultiHash value mh to buffer vb.

proc write(vb: var VBuffer; mh: MultiHash) {.inline, raises: [], tags: [].}
1

encode

Get MultiBase encoded representation of mh using encoding encoding.

proc encode(mbtype: typedesc[MultiBase]; encoding: string; mh: MultiHash): string {.
    inline.}
1
2

Templates


peer

Types

PeerID

undefined

PeerID = object
  data*: seq[byte]
1
2

PeerIDError

undefined

PeerIDError = object of CatchableError
1

Procs


pretty

Return base58 encoded pid representation.

proc pretty(pid: PeerID): string {.inline, raises: [], tags: [].}
1

toBytes

Store PeerID pid to array of bytes data.

Returns number of bytes needed to store pid.

proc toBytes(pid: PeerID; data: var openArray[byte]): int {.raises: [], tags: [].}
1

getBytes

Return PeerID pid as array of bytes.

proc getBytes(pid: PeerID): seq[byte] {.inline, raises: [], tags: [].}
1

hex

Returns hexadecimal string representation of pid.

proc hex(pid: PeerID): string {.inline, raises: [], tags: [].}
1

len

Returns length of pid binary representation.

proc len(pid: PeerID): int {.inline, raises: [], tags: [].}
1

cmp

Compares two peer ids a and b. Returns:

0 iff a == b
< 0 iff a < b
> 0 iff a > b

proc cmp(a, b: PeerID): int {.raises: [], tags: [].}
1

&lt;=

undefined

proc `<=`(a, b: PeerID): bool {.inline, raises: [], tags: [].}
1

&lt;

undefined

proc `<`(a, b: PeerID): bool {.inline, raises: [], tags: [].}
1

&gt;=

undefined

proc `>=`(a, b: PeerID): bool {.inline, raises: [], tags: [].}
1

&gt;

undefined

proc `>`(a, b: PeerID): bool {.inline, raises: [], tags: [].}
1

==

undefined

proc `==`(a, b: PeerID): bool {.inline, raises: [], tags: [].}
1

hash

undefined

proc hash(pid: PeerID): Hash {.inline, raises: [], tags: [].}
1

validate

Validate check if pid is empty or not.

proc validate(pid: PeerID): bool {.raises: [Exception], tags: [RootEffect].}
1

hasPublicKey

Returns true if pid is small enough to hold public key inside.

proc hasPublicKey(pid: PeerID): bool {.raises: [Exception, MultiHashError],
                                   tags: [RootEffect].}
1
2

extractPublicKey

Returns true if public key was successfully decoded from PeerID pid``and stored to ``pubkey.

Returns false otherwise.

proc extractPublicKey(pid: PeerID; pubkey: var PublicKey): bool {.
    raises: [Exception, MultiHashError], tags: [RootEffect].}
1
2

`####

Returns compact string representation of pid.

proc `#### `####  

 Returns compact string representation of <tt class="docutils literal"><span class="pre">pid</span></tt>. 

```nim(pid: PeerID): string {.raises: [], tags: [].}
1
2
3
4
5

init

Initialize peer id from raw binary representation data.

Returns true if peer was successfully initialiazed.

proc init(pid: var PeerID; data: openArray[byte]): bool {.raises: [Exception],
    tags: [RootEffect].}
1
2

init

Initialize peer id from base58 encoded string representation.

Returns true if peer was successfully initialiazed.

proc init(pid: var PeerID; data: string): bool {.raises: [Exception], tags: [RootEffect].}
1

init

Create new peer id from raw binary representation data.

proc init(t: typedesc[PeerID]; data: openArray[byte]): PeerID {.inline.}
1

init

Create new peer id from base58 encoded string representation data.

proc init(t: typedesc[PeerID]; data: string): PeerID {.inline.}
1

init

Create new peer id from public key pubkey.

proc init(t: typedesc[PeerID]; pubkey: PublicKey): PeerID
1

init

Create new peer id from private key seckey.

proc init(t: typedesc[PeerID]; seckey: PrivateKey): PeerID {.inline.}
1

match

Returns true if pid matches public key pubkey.

proc match(pid: PeerID; pubkey: PublicKey): bool {.inline, raises: [
    RsaKeyIncorrectError, EcKeyIncorrectError, MultiHashError, Exception],
    tags: [RootEffect].}
1
2
3

match

Returns true if pid matches private key seckey.

proc match(pid: PeerID; seckey: PrivateKey): bool {.inline, raises: [
    RsaKeyIncorrectError, EcKeyIncorrectError, MultiHashError, Exception],
    tags: [RootEffect].}
1
2
3

write

Write PeerID value peerid to buffer vb.

proc write(vb: var VBuffer; pid: PeerID) {.inline, raises: [], tags: [].}
1

initProtoField

Initialize ProtoField with PeerID value.

proc initProtoField(index: int; pid: PeerID): ProtoField {.raises: [], tags: [].}
1

getValue

Read PeerID from ProtoBuf's message and validate it.

proc getValue(data: var ProtoBuffer; field: int; value: var PeerID): int {.
    raises: [Exception], tags: [RootEffect].}
1
2

Templates


transcoder

Types

Transcoder

undefined

Transcoder = object
  stringToBuffer*: proc (s: string; vb: var VBuffer): bool {.nimcall, gcsafe.}
  bufferToString*: proc (vb: var VBuffer; s: var string): bool {.nimcall, gcsafe.}
  validateBuffer*: proc (vb: var VBuffer): bool {.nimcall, gcsafe.}
1
2
3
4

Procs



Templates


varint

Types

VarintStatus

undefined

VarintStatus {.pure.} = enum
  Error, Success, Overflow, Incomplete, Overrun
1
2

PB

Use this type to specify Google ProtoBuf's varint encoding

PB = object
1

LP

Use this type to specify LibP2P varint encoding

LP = object
1

PBSomeUVarint

undefined

PBSomeUVarint = uint | uint64 | uint32
1

PBSomeSVarint

undefined

PBSomeSVarint = int | int64 | int32
1

PBSomeVarint

undefined

PBSomeVarint = PBSomeUVarint | PBSomeSVarint
1

LPSomeUVarint

undefined

LPSomeUVarint = uint | uint64 | uint32 | uint16 | uint8
1

LPSomeVarint

undefined

LPSomeVarint = LPSomeUVarint
1

SomeVarint

undefined

SomeVarint = PBSomeVarint | LPSomeVarint
1

SomeUVarint

undefined

SomeUVarint = PBSomeUVarint | LPSomeUVarint
1

VarintError

undefined

VarintError = object of CatchableError
1

Procs


vsizeof

Returns number of bytes required to encode integer x as varint.

proc vsizeof(x: SomeVarint): int {.inline.}
1

getUVarint

Decode unsigned varint from buffer pbytes and store it to outval. On success outlen will be set to number of bytes processed while decoding unsigned varint.

If array pbytes is empty, Incomplete error will be returned.

If there not enough bytes available in array pbytes to decode unsigned varint, Incomplete error will be returned.

If encoded value can produce integer overflow, Overflow error will be returned.

Google ProtoBuf When decoding 10th byte of Google Protobuf's 64bit integer only 1 bit from byte will be decoded, all other bits will be ignored. When decoding 5th byte of 32bit integer only 4 bits from byte will be decoded, all other bits will be ignored.

LibP2P When decoding 5th byte of 32bit integer only 4 bits from byte will be decoded, all other bits will be ignored.

proc getUVarint[T: PB | LP](vtype: typedesc[T]; pbytes: openArray[byte];
                        outlen: var int; outval: var SomeUVarint): VarintStatus
1
2

putUVarint

Encode unsigned varint outval and store it to array pbytes.

On success outlen will hold number of bytes (octets) used to encode unsigned integer v.

If there not enough bytes available in buffer pbytes, Incomplete error will be returned and outlen will be set to number of bytes required.

Google ProtoBuf Maximum encoded length of 64bit integer is 10 octets. Maximum encoded length of 32bit integer is 5 octets.

LibP2P Maximum encoded length of 63bit integer is 9 octets. Maximum encoded length of 32bit integer is 5 octets.

proc putUVarint[T: PB | LP](vtype: typedesc[T]; pbytes: var openArray[byte];
                        outlen: var int; outval: SomeUVarint): VarintStatus
1
2

getSVarint

Decode Google ProtoBuf's signed varint from buffer pbytes and store it to outval. On success outlen will be set to number of bytes processed while decoding signed varint.

If array pbytes is empty, Incomplete error will be returned.

If there not enough bytes available in array pbytes to decode signed varint, Incomplete error will be returned.

If encoded value can produce integer overflow, Overflow error will be returned.

Note, when decoding 10th byte of 64bit integer only 1 bit from byte will be decoded, all other bits will be ignored. When decoding 5th byte of 32bit integer only 4 bits from byte will be decoded, all other bits will be ignored.

proc getSVarint(pbytes: openArray[byte]; outsize: var int; outval: var PBSomeSVarint): VarintStatus {.
    inline.}
1
2

putSVarint

Encode Google ProtoBuf's signed varint outval and store it to array pbytes.

On success outlen will hold number of bytes (octets) used to encode unsigned integer v.

If there not enough bytes available in buffer pbytes, Incomplete error will be returned and outlen will be set to number of bytes required.

Maximum encoded length of 64bit integer is 10 octets. Maximum encoded length of 32bit integer is 5 octets.

proc putSVarint(pbytes: var openArray[byte]; outsize: var int; outval: PBSomeSVarint): VarintStatus {.
    inline.}
1
2

encodeVarint

Encode integer to Google ProtoBuf's signed/unsigned varint and returns sequence of bytes as result.

proc encodeVarint(vtype: typedesc[PB]; value: PBSomeVarint): seq[byte] {.inline.}
1

encodeVarint

Encode integer to LibP2P unsigned varint and returns sequence of bytes as result.

proc encodeVarint(vtype: typedesc[LP]; value: LPSomeVarint): seq[byte] {.inline.}
1

decodeSVarint

Decode signed integer from array data and return it as result.

proc decodeSVarint(data: openArray[byte]): int {.inline, raises: [VarintError],
    tags: [].}
1
2

decodeUVarint

Decode unsigned integer from array data and return it as result.

proc decodeUVarint[T: PB | LP](vtype: typedesc[T]; data: openArray[byte]): uint {.inline.}
1

Templates


vbuffer

Types

VBuffer

undefined

VBuffer = object
  buffer*: seq[byte]
  offset*: int
  length*: int
1
2
3
4

Procs


len

Returns number of bytes left in buffer vb.

proc len(vb: VBuffer): int {.raises: [], tags: [].}
1

initVBuffer

Initialize VBuffer with shallow copy of data.

proc initVBuffer(data: seq[byte]; offset = 0): VBuffer {.raises: [], tags: [].}
1

initVBuffer

Initialize VBuffer with copy of data.

proc initVBuffer(data: openArray[byte]; offset = 0): VBuffer {.raises: [], tags: [].}
1

initVBuffer

Initialize empty VBuffer.

proc initVBuffer(): VBuffer {.raises: [], tags: [].}
1

writeVarint

Write value as variable unsigned integer.

proc writeVarint(vb: var VBuffer; value: LPSomeUVarint)
1

writeSeq

Write array value to buffer vb, value will be prefixed with varint length of the array.

proc writeSeq[T: byte | char](vb: var VBuffer; value: openArray[T])
1

writeArray

Write array value to buffer vb, value will NOT be prefixed with varint length of the array.

proc writeArray[T: byte | char](vb: var VBuffer; value: openArray[T])
1

finish

Finishes vb.

proc finish(vb: var VBuffer) {.raises: [], tags: [].}
1

peekVarint

Peek unsigned integer from buffer vb and store result to value.

This procedure will not adjust internal offset.

Returns number of bytes peeked from vb or -1 on error.

proc peekVarint(vb: var VBuffer; value: var LPSomeUVarint): int
1

peekSeq

Peek length prefixed array from buffer vb and store result to value.

This procedure will not adjust internal offset.

Returns number of bytes peeked from vb or -1 on error.

proc peekSeq[T: string | seq[byte]](vb: var VBuffer; value: var T): int
1

peekArray

Peek array from buffer vb and store result to value.

This procedure will not adjust internal offset.

Returns number of bytes peeked from vb or -1 on error.

proc peekArray[T: char | byte](vb: var VBuffer; value: var openArray[T]): int
1

readVarint

Read unsigned integer from buffer vb and store result to value.

Returns number of bytes consumed from vb or -1 on error.

proc readVarint(vb: var VBuffer; value: var LPSomeUVarint): int {.inline.}
1

readSeq

Read length prefixed array from buffer vb and store result to value.

Returns number of bytes consumed from vb or -1 on error.

proc readSeq[T: string | seq[byte]](vb: var VBuffer; value: var T): int {.inline.}
1

readArray

Read array from buffer vb and store result to value.

Returns number of bytes consumed from vb or -1 on error.

proc readArray[T: char | byte](vb: var VBuffer; value: var openArray[T]): int {.inline.}
1

`####

Return hexadecimal string representation of buffer vb.

proc `#### `####  

 Return hexadecimal string representation of buffer <tt class="docutils literal"><span class="pre">vb</span></tt>. 

```nim(vb: VBuffer): string {.raises: [], tags: [].}
1
2
3
4
5

Templates


isEmpty

Returns true if buffer vb is empty.

template isEmpty(vb: VBuffer): bool
1

isEnough

Returns true if buffer vb holds at least length bytes.

template isEnough(vb: VBuffer; length: int): bool
1

wire

Types


Procs


initTAddress

Initialize TransportAddress with MultiAddress ma.

MultiAddress must be wire address, e.g. {IP4, IP6, UNIX}/{TCP, UDP}.

proc initTAddress(ma: MultiAddress): TransportAddress {.
    raises: [MultiAddressError, TransportAddressError], tags: [].}
1
2

connect

Open new connection to remote peer with address ma and create new transport object StreamTransport for established connection. bufferSize is size of internal buffer for transport.

proc connect(ma: MultiAddress; bufferSize = DefaultStreamBufferSize;
            child: StreamTransport = nil): Future[StreamTransport] {.raises: [
    MultiAddressError, TransportAddressError, FutureError, IndexError, Exception,
    OSError], tags: [RootEffect].}
1
2
3
4

createStreamServer

Create new TCP stream server which bounds to ma address.

proc createStreamServer[T](ma: MultiAddress; cbproc: StreamCallback;
                          flags: set[ServerFlags] = {}; udata: ref T;
                          sock: AsyncFD = asyncInvalidSocket; backlog: int = 100;
                          bufferSize: int = DefaultStreamBufferSize;
                          child: StreamServer = nil;
                          init: TransportInitCallback = nil): StreamServer
1
2
3
4
5
6

createAsyncSocket

Create new asynchronous socket using MultiAddress' ma socket type and protocol information.

Returns asyncInvalidSocket on error.

proc createAsyncSocket(ma: MultiAddress): AsyncFD {.
    raises: [MultiAddressError, Exception, OSError], tags: [RootEffect].}
1
2

bindAsyncSocket

Bind socket sock to MultiAddress ma.

proc bindAsyncSocket(sock: AsyncFD; ma: MultiAddress): bool {.raises: [], tags: [].}
1

getLocalAddress

Retrieve local socket sock address.

proc getLocalAddress(sock: AsyncFD): TransportAddress {.raises: [], tags: [].}
1

Templates