diff --git a/CHANGELOG.md b/CHANGELOG.md index caaa28d..754197e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [0.2.8] - Unreleased - Fixed bug with subtype for semantic code - Changed URI representation to upper case +- Changed to disallow ISCC-ID creation from ISCC-IDs - Added line conversion tool - Updated dependencies diff --git a/docs/changelog.md b/docs/changelog.md index caaa28d..754197e 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -3,6 +3,7 @@ ## [0.2.8] - Unreleased - Fixed bug with subtype for semantic code - Changed URI representation to upper case +- Changed to disallow ISCC-ID creation from ISCC-IDs - Added line conversion tool - Updated dependencies diff --git a/iscc_core/iscc_id.py b/iscc_core/iscc_id.py index 9d7f661..17199fe 100644 --- a/iscc_core/iscc_id.py +++ b/iscc_core/iscc_id.py @@ -92,8 +92,11 @@ def soft_hash_iscc_id_v0(iscc_code, wallet, uc=0): digests.append(decoded[0][:1] + unpacked[0][-1][:7]) else: for dec, unp in zip(decoded, unpacked): - if unp[0] == ic.MT.INSTANCE: + mt = unp[0] + if mt == ic.MT.INSTANCE: continue + if mt == ic.MT.ID: + raise ValueError("Cannot create ISCC-ID from ISCC-ID") # first byte of header + first 7 bytes of body digests.append(dec[:1] + unp[-1][:7]) diff --git a/tests/test_iscc_id.py b/tests/test_iscc_id.py index 68dc778..affd260 100644 --- a/tests/test_iscc_id.py +++ b/tests/test_iscc_id.py @@ -136,3 +136,27 @@ def test_iscc_id_incr_v0_raises_bad_chain_id(): assert iscc_id == "MQAAAAAAAAAAAAAA" with pytest.raises(AssertionError): ic.iscc_id_incr_v0(iscc_id) + + +def test_iscc_id_unit_types(): + # Meta-Code 256-bits only + mc = ic.Code.rnd(mt=ic.MT.META, bits=256) + assert ic.gen_iscc_id(mc.code, 0, "a") == {"iscc": "ISCC:MAAMUFVSHK6CYVQW"} + assert ic.gen_iscc_id(mc.uri, 0, "a") == {"iscc": "ISCC:MAAMUFVSHK6CYVQW"} + + # Meta-Code 64-bit prefix + mc2 = ic.encode_component(ic.MT.META, ic.ST.NONE, 0, bit_length=64, digest=mc.hash_bytes[:8]) + assert ic.gen_iscc_id(mc2, 0, "a") == {"iscc": "ISCC:MAAMUFVSHK6CYVQW"} + + cc = ic.Code.rnd(mt=ic.MT.CONTENT, bits=64) + assert ic.gen_iscc_id(cc.code, 0, "a") == {"iscc": "ISCC:MAAOXNCHOPSVHG2M"} + + fc = ic.Code.rnd(mt=ic.MT.FLAKE, bits=64) + assert ic.gen_iscc_id(fc.code, 0, "a") == {"iscc": "ISCC:MAALUWZEW5VYL4FE"} + + +def test_iscc_id_from_iscc_id_raises(): + iid = ic.Code.rnd(mt=ic.MT.ID, bits=64) + assert iid.code == "MIAORZJBNL6L2BGD" + with pytest.raises(ValueError): + ic.gen_iscc_id(iid.code, 0, "a")