Add write() and encode() procedures.
This commit is contained in:
parent
8b00514dd7
commit
38f82df582
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
## This module implements MultiAddress.
|
## This module implements MultiAddress.
|
||||||
import tables, strutils, net
|
import tables, strutils, net
|
||||||
import multicodec, multihash, transcoder, base58, base32, vbuffer
|
import multicodec, multihash, multibase, transcoder, base58, base32, vbuffer
|
||||||
|
|
||||||
{.deadCodeElim:on.}
|
{.deadCodeElim:on.}
|
||||||
|
|
||||||
|
@ -460,6 +460,7 @@ proc getPart(ma: MultiAddress, index: int): MultiAddress =
|
||||||
inc(offset)
|
inc(offset)
|
||||||
|
|
||||||
proc `[]`*(ma: MultiAddress, i: int): MultiAddress {.inline.} =
|
proc `[]`*(ma: MultiAddress, i: int): MultiAddress {.inline.} =
|
||||||
|
## Returns part with index ``i`` of MultiAddress ``ma``.
|
||||||
result = ma.getPart(i)
|
result = ma.getPart(i)
|
||||||
|
|
||||||
iterator items*(ma: MultiAddress): MultiAddress =
|
iterator items*(ma: MultiAddress): MultiAddress =
|
||||||
|
@ -526,6 +527,16 @@ proc hex*(value: MultiAddress): string =
|
||||||
## Return hexadecimal string representation of MultiAddress ``value``.
|
## Return hexadecimal string representation of MultiAddress ``value``.
|
||||||
result = $(value.data)
|
result = $(value.data)
|
||||||
|
|
||||||
|
proc write*(vb: var VBuffer, ma: MultiAddress) {.inline.} =
|
||||||
|
## Write MultiAddress value ``ma`` to buffer ``vb``.
|
||||||
|
vb.writeArray(ma.data.buffer)
|
||||||
|
|
||||||
|
proc encode*(mbtype: typedesc[MultiBase], encoding: string,
|
||||||
|
ma: MultiAddress): string {.inline.} =
|
||||||
|
## Get MultiBase encoded representation of ``ma`` using encoding
|
||||||
|
## ``encoding``.
|
||||||
|
result = MultiBase.encode(encoding, ma.data.buffer)
|
||||||
|
|
||||||
proc validate*(ma: MultiAddress): bool =
|
proc validate*(ma: MultiAddress): bool =
|
||||||
## Returns ``true`` if MultiAddress ``ma`` is valid.
|
## Returns ``true`` if MultiAddress ``ma`` is valid.
|
||||||
var header: uint64
|
var header: uint64
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
## 3. MURMUR
|
## 3. MURMUR
|
||||||
import tables
|
import tables
|
||||||
import nimcrypto/[sha2, keccak, blake2, hash, utils]
|
import nimcrypto/[sha2, keccak, blake2, hash, utils]
|
||||||
import varint, vbuffer, base58, multicodec
|
import varint, vbuffer, base58, multicodec, multibase
|
||||||
|
|
||||||
const
|
const
|
||||||
MaxHashSize* = 128
|
MaxHashSize* = 128
|
||||||
|
@ -461,12 +461,12 @@ proc validate*(mhtype: typedesc[MultiHash], data: openarray[byte]): bool =
|
||||||
offset += length
|
offset += length
|
||||||
if size > 0x7FFF_FFFF'u64:
|
if size > 0x7FFF_FFFF'u64:
|
||||||
return false
|
return false
|
||||||
let hash = CodeHashes.getOrDefault(MultiCodec(code))
|
let hash = CodeHashes.getOrDefault(cast[MultiCodec](code))
|
||||||
if isNil(hash.coder):
|
if isNil(hash.coder):
|
||||||
return false
|
return false
|
||||||
if (hash.size != 0) and (hash.size != int(size)):
|
if (hash.size != 0) and (hash.size != int(size)):
|
||||||
return false
|
return false
|
||||||
if offset + length + int(size) > len(data):
|
if offset + int(size) > len(data):
|
||||||
return false
|
return false
|
||||||
result = true
|
result = true
|
||||||
|
|
||||||
|
@ -514,8 +514,8 @@ proc `==`*[T](mdigest: MDigest[T], mh: MultiHash): bool {.inline.} =
|
||||||
result = `==`(mh, mdigest)
|
result = `==`(mh, mdigest)
|
||||||
|
|
||||||
proc `==`*(a: MultiHash, b: MultiHash): bool =
|
proc `==`*(a: MultiHash, b: MultiHash): bool =
|
||||||
## Compares MultiHashes ``a`` and ``b``, returns ``true`` if
|
## Compares MultiHashes ``a`` and ``b``, returns ``true`` if hashes are equal,
|
||||||
## hashes are equal, ``false`` otherwise.
|
## ``false`` otherwise.
|
||||||
if a.dpos == 0 and b.dpos == 0:
|
if a.dpos == 0 and b.dpos == 0:
|
||||||
return true
|
return true
|
||||||
if a.mcodec != b.mcodec:
|
if a.mcodec != b.mcodec:
|
||||||
|
@ -542,3 +542,9 @@ proc `$`*(mh: MultiHash): string =
|
||||||
proc write*(vb: var VBuffer, mh: MultiHash) {.inline.} =
|
proc write*(vb: var VBuffer, mh: MultiHash) {.inline.} =
|
||||||
## Write MultiHash value ``mh`` to buffer ``vb``.
|
## Write MultiHash value ``mh`` to buffer ``vb``.
|
||||||
vb.writeArray(mh.data.buffer)
|
vb.writeArray(mh.data.buffer)
|
||||||
|
|
||||||
|
proc encode*(mbtype: typedesc[MultiBase], encoding: string,
|
||||||
|
mh: MultiHash): string {.inline.} =
|
||||||
|
## Get MultiBase encoded representation of ``mh`` using encoding
|
||||||
|
## ``encoding``.
|
||||||
|
result = MultiBase.encode(encoding, mh.data.buffer)
|
||||||
|
|
Loading…
Reference in New Issue