Skip to content

Commit

Permalink
disallow ISCC-ID creation from ISCC-IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
titusz committed Apr 21, 2022
1 parent 7ef3a32 commit 2bcd4ce
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion iscc_core/iscc_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
24 changes: 24 additions & 0 deletions tests/test_iscc_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 2bcd4ce

Please sign in to comment.