def test_hash_format(self, response): reader = csv.DictReader( (line.decode('utf8') for line in response.iter_lines())) for entry in reader: blob_hash = entry['blob-hash'] assert multihash.is_valid(bytes.fromhex(blob_hash))
def is_valid_multihash(self): """ Returns true if the URI is a valid multihash. Note: in some scenarios, this could be a very interesting URI type. If countries have a way to resolve document repositories, and if the resources are unchanging, then multihash identifiers have the interesting proof characteristics. Multihash URIs are complimentary of Name URIs in distributed asynchronous systems, due to the way they support "append only" data structures. """ if not isinstance(self.value, str): return False if self.value.startswith('/ipfs/'): normalized_value = self.value[len('/ipfs/'):] else: normalized_value = self.value try: return multihash.is_valid( multihash.from_b58_string(normalized_value)) except ValueError: return False
def test_hash_format(self, endpoint, blob_response): reader = csv.DictReader( (line.decode('utf8') for line in blob_response.iter_lines())) for blob in reader: blob_id = blob['_id'] assert multihash.is_valid(bytes.fromhex(blob_id))
def get_body(self, b58_multihash: str): # Note: not for a very big files assert multihash.is_valid( multihash.from_b58_string(b58_multihash) ), "A valid multihash must be provided" # 1. calculate slash_chunked path from multihash # 2. get multihash-named object from store (or return False) # 3. return retrieved object return self.client.get_object( Bucket=self.bucket_name, Key=miniorepo.slash_chunk(b58_multihash), )['Body'].read()
def test_check_sum_sha256(): """Test basic check_sum multihash generation.""" with TemporaryDirectory() as tmp: tmp_file = Path(tmp) / 'file.txt' message = 'Testing' with open(tmp_file, 'w') as f: f.write(message) _hash = multihash_checksum_sha256(tmp_file) expected_hash = '1220e806a291cfc3e61f83b98d344ee57e3e8933cccece4fb45e1481f1f560e70eb1' assert _hash == expected_hash # Try using stream instead file stream = BytesIO(message.encode()) stream_hash = multihash_checksum_sha256(stream) assert stream_hash == expected_hash digest = multihash.from_hex_string(_hash) # Ensure digest is a multihash assert multihash.is_valid(digest)
def make_or_get(): """ If the user doesn't have a .gnize dir in their home directory, initialize it Otherwise just provide a config summary and return the contents of config.yaml """ # ~/.gnize if not dir_path.exists(): print(f"creating {dir_path}", file=sys.stderr) dir_path.mkdir(exist_ok=True, parents=True) print(f"{dir_path} exists", file=sys.stderr) # ~/.gnize/config.yaml try: with open(config_path, "w") as f: config_str = dump(json.loads(default_config.to_json())) # the json conversion above can be dispensed with, like so: # config_str = dump(default_config) # but then you end up with yaml like this: # # !!python/object:gnize.dotdir.Config # canvasses: !!python/object:gnize.dotdir.CanvasSource # path: /home/matt/.gnize/canvasses # use: filesystem f.write(config_str) print(f"{config_path} already exists", file=sys.stderr) config = default_config except FileNotFoundError: with open(config_path, "r") as f: config_str = f.read() print("wrote {config_path}", file=sys.stderr) config = load(config_str, Loader=BaseLoader) # canvasses if config.canvasses.use == "filesystem": canvas_dir = Path(config.canvasses.path) canvas_dir.mkdir(parents=True, exist_ok=True) count = 0 for child in canvas_dir.glob("*"): if multihash.is_valid(str(child)): count += 1 print(f"{count} canvasses found in f{canvas_dir}", file=sys.stderr) # fingerprints database if config.fingerprints.use == "sqlite3": conn = sqlite3.connect(config.fingerprints.connect) cursor = conn.cursor() cursor.execute( dedent(""" CREATE TABLE IF NOT EXISTS prints ( channel INTEGER NOT NULL, fingerprint INTEGER NOT NULL, repeat_num INTEGER NOT NULL DEFAULT 0, canvas_hash TEXT NOT NULL, canvas sub INTEGER NOT NULL, sub_idx INTEGER NOT NULL, len INTEGER NOT NULL, PRIMARY KEY (channel, fingerprint, repeat_num) ); """)) cursor.execute("SELECT count(*) from prints;") count = cursor.fetchone()[0] cursor.execute("SELECT count(distinct canvas_hash) from prints;") count = cursor.fetchone()[0] print(f"{count} fingerprints cognized so far", file=sys.stderr) return config
def test_is_valid_invalid(self, value): """ is_valid: returns False for all invalid cases """ assert not is_valid( make_hash(value['code'], value['length'], value['hex']))
def test_is_valid_valid(self, value): """ is_valid: returns True for all valid cases """ assert is_valid( make_hash(value['encoding']['code'], value['length'], value['hex']))
def test_hash_format(self, response): reader = csv.DictReader((line.decode('utf8') for line in response.iter_lines())) for entry in reader: blob_hash = entry['blob-hash'] assert multihash.is_valid(bytes.fromhex(blob_hash))
def test_hash_format(self, response): entry = response.json() blob_hash = entry['blob-hash'] assert multihash.is_valid(bytes.fromhex(blob_hash))
def test_hash_format(self, blob_response, endpoint): blob = blob_response.json() blob_id = blob['_id'] assert multihash.is_valid(bytes.fromhex(blob_id))
def test_hash_format(self, endpoint, blob_response): reader = csv.DictReader((line.decode('utf8') for line in blob_response.iter_lines())) for blob in reader: blob_id = blob['_id'] assert multihash.is_valid(bytes.fromhex(blob_id))