From 5c234ddd37007bd677708ccde14cd0d014fb5c53 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Fri, 5 Feb 2021 10:12:44 -0600 Subject: [PATCH] add hash proc to support using with containers (#516) --- libp2p/cid.nim | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libp2p/cid.nim b/libp2p/cid.nim index c1590ffb2..400e285d7 100644 --- a/libp2p/cid.nim +++ b/libp2p/cid.nim @@ -11,7 +11,7 @@ {.push raises: [Defect].} -import tables +import tables, hashes import multibase, multicodec, multihash, vbuffer, varint import stew/[base58, results] @@ -199,10 +199,10 @@ proc init*(ctype: typedesc[Cid], version: CidVersion, content: MultiCodec, ## Cid.init(CIDv0, multiCodec("dag-pb"), MultiHash.digest("sha2-256", data)) ## ## All other encodings and hashes are not supported by CIDv0. - + var res: Cid res.cidver = version - + if version == CIDv0: if content != multiCodec("dag-pb"): return err(CidError.Unsupported) @@ -268,6 +268,9 @@ proc encode*(mbtype: typedesc[MultiBase], encoding: string, ## ``encoding``. result = MultiBase.encode(encoding, cid.data.buffer).tryGet() +proc hash*(cid: Cid): Hash {.inline.} = + hash(cid.data.buffer) + proc `$`*(cid: Cid): string = ## Return official string representation of content identifier ``cid``. if cid.cidver == CIDv0: