nim-libp2p/libp2p/cid.nim

285 lines
8.6 KiB
Nim
Raw Normal View History

2022-07-01 18:19:57 +00:00
# Nim-LibP2P
# Copyright (c) 2022 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.
2018-12-16 02:55:20 +00:00
## This module implementes CID (Content IDentifier).
{.push raises: [Defect].}
import tables, hashes
2019-12-13 09:42:47 +00:00
import multibase, multicodec, multihash, vbuffer, varint
import stew/[base58, results]
export results
2018-12-16 02:55:20 +00:00
type
CidError* {.pure.} = enum
Error, Incorrect, Unsupported, Overrun
2018-12-16 02:55:20 +00:00
CidVersion* = enum
CIDvIncorrect, CIDv0, CIDv1, CIDvReserved
Cid* = object
cidver*: CidVersion
mcodec*: MultiCodec
hpos*: int
data*: VBuffer
const
ContentIdsList = [
multiCodec("raw"),
multiCodec("dag-pb"),
multiCodec("dag-cbor"),
multiCodec("dag-json"),
multiCodec("git-raw"),
multiCodec("eth-block"),
multiCodec("eth-block-list"),
multiCodec("eth-tx-trie"),
multiCodec("eth-tx"),
multiCodec("eth-tx-receipt-trie"),
multiCodec("eth-tx-receipt"),
multiCodec("eth-state-trie"),
multiCodec("eth-account-snapshot"),
multiCodec("eth-storage-trie"),
multiCodec("bitcoin-block"),
multiCodec("bitcoin-tx"),
multiCodec("zcash-block"),
multiCodec("zcash-tx"),
multiCodec("stellar-block"),
multiCodec("stellar-tx"),
multiCodec("decred-block"),
multiCodec("decred-tx"),
multiCodec("dash-block"),
multiCodec("dash-tx"),
multiCodec("torrent-info"),
multiCodec("torrent-file"),
multiCodec("ed25519-pub")
]
proc initCidCodeTable(): Table[int, MultiCodec] {.compileTime.} =
for item in ContentIdsList:
result[int(item)] = item
const
CodeContentIds = initCidCodeTable()
template orError*(exp: untyped, err: untyped): untyped =
(exp.mapErr do (_: auto) -> auto: err)
2021-12-16 10:05:20 +00:00
proc decode(data: openArray[byte]): Result[Cid, CidError] =
if len(data) == 34 and data[0] == 0x12'u8 and data[1] == 0x20'u8:
ok(Cid(
cidver: CIDv0,
mcodec: multiCodec("dag-pb"),
hpos: 0,
data: initVBuffer(data)))
else:
2018-12-16 02:55:20 +00:00
var version, codec: uint64
var res, offset: int
var vb = initVBuffer(data)
if vb.isEmpty():
err(CidError.Incorrect)
else:
res = vb.readVarint(version)
if res == -1:
err(CidError.Incorrect)
else:
offset += res
if version != 1'u64:
err(CidError.Incorrect)
else:
res = vb.readVarint(codec)
if res == -1:
err(CidError.Incorrect)
else:
offset += res
var mcodec = CodeContentIds.getOrDefault(cast[int](codec),
InvalidMultiCodec)
if mcodec == InvalidMultiCodec:
err(CidError.Incorrect)
else:
if not MultiHash.validate(vb.buffer.toOpenArray(vb.offset,
vb.buffer.high)):
err(CidError.Incorrect)
else:
vb.finish()
ok(Cid(
cidver: CIDv1,
mcodec: mcodec,
hpos: offset,
data: vb))
2018-12-16 02:55:20 +00:00
2021-12-16 10:05:20 +00:00
proc decode(data: openArray[char]): Result[Cid, CidError] =
2018-12-16 02:55:20 +00:00
var buffer: seq[byte]
var plen = 0
if len(data) < 2:
return err(CidError.Incorrect)
2018-12-16 02:55:20 +00:00
if len(data) == 46:
if data[0] == 'Q' and data[1] == 'm':
buffer = newSeq[byte](BTCBase58.decodedLength(len(data)))
if BTCBase58.decode(data, buffer, plen) != Base58Status.Success:
return err(CidError.Incorrect)
2018-12-16 02:55:20 +00:00
buffer.setLen(plen)
if len(buffer) == 0:
let length = MultiBase.decodedLength(data[0], len(data))
if length == -1:
return err(CidError.Incorrect)
2018-12-16 02:55:20 +00:00
buffer = newSeq[byte](length)
if MultiBase.decode(data, buffer, plen) != MultiBaseStatus.Success:
return err(CidError.Incorrect)
2018-12-16 02:55:20 +00:00
buffer.setLen(plen)
if buffer[0] == 0x12'u8:
return err(CidError.Incorrect)
decode(buffer)
2018-12-16 02:55:20 +00:00
2021-12-16 10:05:20 +00:00
proc validate*(ctype: typedesc[Cid], data: openArray[byte]): bool =
2018-12-16 02:55:20 +00:00
## Returns ``true`` is data has valid binary CID representation.
var version, codec: uint64
var res: VarintResult[void]
2018-12-16 02:55:20 +00:00
if len(data) < 2:
return false
let last = data.high
2018-12-16 02:55:20 +00:00
if len(data) == 34:
if data[0] == 0x12'u8 and data[1] == 0x20'u8:
return true
var offset = 0
var length = 0
res = LP.getUVarint(data.toOpenArray(offset, last), length, version)
if res.isErr():
2018-12-16 02:55:20 +00:00
return false
if version != 1'u64:
return false
offset += length
if offset >= len(data):
return false
res = LP.getUVarint(data.toOpenArray(offset, last), length, codec)
if res.isErr():
2018-12-16 02:55:20 +00:00
return false
var mcodec = CodeContentIds.getOrDefault(cast[int](codec), InvalidMultiCodec)
if mcodec == InvalidMultiCodec:
return false
if not MultiHash.validate(data.toOpenArray(offset, last)):
return false
result = true
proc mhash*(cid: Cid): Result[MultiHash, CidError] =
2018-12-16 02:55:20 +00:00
## Returns MultiHash part of CID.
if cid.cidver notin {CIDv0, CIDv1}:
err(CidError.Incorrect)
else:
MultiHash.init(cid.data.buffer.toOpenArray(cid.hpos, cid.data.high)).orError(CidError.Incorrect)
2018-12-16 02:55:20 +00:00
proc contentType*(cid: Cid): Result[MultiCodec, CidError] =
2018-12-16 02:55:20 +00:00
## Returns content type part of CID
if cid.cidver notin {CIDv0, CIDv1}:
err(CidError.Incorrect)
else:
ok(cid.mcodec)
2018-12-16 02:55:20 +00:00
proc version*(cid: Cid): CidVersion =
## Returns CID version
result = cid.cidver
2021-12-16 10:05:20 +00:00
proc init*[T: char|byte](ctype: typedesc[Cid], data: openArray[T]): Result[Cid, CidError] =
2018-12-16 02:55:20 +00:00
## Create new content identifier using array of bytes or string ``data``.
decode(data)
2018-12-16 02:55:20 +00:00
proc init*(ctype: typedesc[Cid], version: CidVersion, content: MultiCodec,
hash: MultiHash): Result[Cid, CidError] =
2018-12-16 02:55:20 +00:00
## 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.
var res: Cid
res.cidver = version
2018-12-16 02:55:20 +00:00
if version == CIDv0:
if content != multiCodec("dag-pb"):
return err(CidError.Unsupported)
res.data = initVBuffer()
2018-12-16 02:55:20 +00:00
if hash.mcodec != multiCodec("sha2-256"):
return err(CidError.Unsupported)
res.mcodec = content
res.data.write(hash)
res.data.finish()
return ok(res)
2018-12-16 02:55:20 +00:00
elif version == CIDv1:
let mcodec = CodeContentIds.getOrDefault(cast[int](content),
InvalidMultiCodec)
if mcodec == InvalidMultiCodec:
return err(CidError.Incorrect)
res.mcodec = mcodec
res.data = initVBuffer()
res.data.writeVarint(cast[uint64](1))
res.data.write(mcodec)
res.hpos = len(res.data.buffer)
res.data.write(hash)
res.data.finish()
return ok(res)
2018-12-16 02:55:20 +00:00
else:
return err(CidError.Unsupported)
2018-12-16 02:55:20 +00:00
proc `==`*(a: Cid, b: Cid): bool =
## Compares content identifiers ``a`` and ``b``, returns ``true`` if hashes
## are equal, ``false`` otherwise.
if a.mcodec == b.mcodec:
var ah, bh: MultiHash
if MultiHash.decode(
a.data.buffer.toOpenArray(a.hpos, a.data.high), ah).isErr:
2018-12-16 02:55:20 +00:00
return false
if MultiHash.decode(
b.data.buffer.toOpenArray(b.hpos, b.data.high), bh).isErr:
2018-12-16 02:55:20 +00:00
return false
result = (ah == bh)
proc base58*(cid: Cid): string =
## Get BASE58 encoded string representation of content identifier ``cid``.
result = BTCBase58.encode(cid.data.buffer)
proc hex*(cid: Cid): string =
## Get hexadecimal string representation of content identifier ``cid``.
result = $(cid.data)
proc repr*(cid: Cid): string =
## Get string representation of content identifier ``cid``.
result = $(cid.cidver)
result.add("/")
result.add($(cid.mcodec))
result.add("/")
result.add($(cid.mhash()))
proc write*(vb: var VBuffer, cid: Cid) {.inline.} =
## Write CID value ``cid`` to buffer ``vb``.
vb.writeArray(cid.data.buffer)
proc encode*(mbtype: typedesc[MultiBase], encoding: string,
cid: Cid): string {.inline.} =
## Get MultiBase encoded representation of ``cid`` using encoding
## ``encoding``.
result = MultiBase.encode(encoding, cid.data.buffer).tryGet()
2018-12-16 02:55:20 +00:00
proc hash*(cid: Cid): Hash {.inline.} =
hash(cid.data.buffer)
2018-12-16 02:55:20 +00:00
proc `$`*(cid: Cid): string =
## Return official string representation of content identifier ``cid``.
if cid.cidver == CIDv0:
BTCBase58.encode(cid.data.buffer)
2018-12-16 02:55:20 +00:00
elif cid.cidver == CIDv1:
2021-12-16 10:05:20 +00:00
let res = MultiBase.encode("base58btc", cid.data.buffer)
if res.isOk():
res.get()
else:
""
else:
""