def test_verify_prefix_complete(multicodec, prefix): data = b'testbytesbuffer' prefix_int = prefix['prefix'] prefixed_data = add_prefix(multicodec, data) assert is_codec(multicodec) assert get_codec(prefixed_data) == multicodec assert remove_prefix(prefixed_data) == data assert extract_prefix(prefixed_data) == prefix_int
def buffer(self): """ The raw representation of the CID :return: raw representation of the CID :rtype: bytes """ return b''.join([ bytes([self.version]), multicodec.add_prefix(self.codec, self.multihash) ])
def encode(codec, value): """ Encode a content hash. :param str codec: a codec of a content hash :param str value: a value of a content hash :return: the resulting content hash :rtype: str """ profile = get_profile(codec) value = profile.encode(value) value = multicodec.add_prefix(codec, value) return multihash.to_hex_string(value)
def generate_digest(url): if url is None or url == "": return "" with urllib.request.urlopen(url) as response: with tempfile.NamedTemporaryFile(delete=False) as tmp_file: shutil.copyfileobj(response, tmp_file) sha256_hash = hashlib.sha256() # Read and update hash string value in blocks of 4K for byte_block in iter(lambda: tmp_file.file.read(4096), b""): sha256_hash.update(byte_block) hex_digest = sha256_hash.hexdigest() multi_codec = add_prefix('sha2-256', str.encode(hex_digest)) base58btc = encode('base58btc', multi_codec) digest = base58btc.decode() return digest
def naked_to_did_key(key: str) -> str: """Convert a naked ed25519 verkey to W3C did:key format.""" key_bytes = b58_to_bytes(key) prefixed_key_bytes = add_prefix("ed25519-pub", key_bytes) did_key = f"did:key:z{bytes_to_b58(prefixed_key_bytes)}" return did_key
def test_buffer(self, cid): """ .buffer: buffer is computed properly """ buffer = cid.buffer assert buffer[0] == 1 assert buffer[1:] == multicodec.add_prefix(self.TEST_CODEC, cid.multihash)