-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check DID Identifier checksum in validation #8
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
# Copyright (c) IOTIC LABS LIMITED. All rights reserved. Licensed under the Apache License, Version 2.0. | ||
|
||
import re | ||
from hashlib import blake2b | ||
|
||
from iotics.lib.identity.const import IDENTIFIER_ID_PATTERN, IDENTIFIER_NAME_PATTERN, ISSUER_PATTERN | ||
import base58 | ||
|
||
from iotics.lib.identity.const import IDENTIFIER_ID_PATTERN, IDENTIFIER_NAME_PATTERN, ISSUER_PATTERN, IDENTIFIER_PREFIX | ||
from iotics.lib.identity.error import IdentityValidationError | ||
|
||
|
||
|
@@ -20,6 +23,17 @@ def validate_identifier(did: str): | |
if result is None: | ||
raise IdentityValidationError(f'Identifier does not match pattern {did} - {IDENTIFIER_ID_PATTERN}') | ||
|
||
did_bytes = base58.b58decode(did[len(IDENTIFIER_PREFIX):], alphabet=base58.BITCOIN_ALPHABET) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. following up the comment above, should this magic number be put in a constant (since it's repeated in many places or maybe have a |
||
did_digest = did_bytes[3:23] | ||
did_checksum = did_bytes[-4:].hex() | ||
|
||
cl2 = blake2b(digest_size=20) | ||
cl2.update(did_digest) | ||
checksum = cl2.hexdigest()[:8] | ||
|
||
if did_checksum != checksum: | ||
raise IdentityValidationError(f'Identifier checksum does not match {did_checksum} != {checksum}') | ||
|
||
@staticmethod | ||
def validate_issuer_string(issuer: str): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,9 +83,14 @@ def other_key_pair(other_key_pair_secrets): | |
|
||
@pytest.fixture | ||
def doc_did(): | ||
return 'did:iotics:iotHHHHKpPGWyEC4FFo4d6oyzVVk6MXLmEgY' | ||
return 'did:iotics:iotDadb3rSWedk8iqExSbwqLtijG5XQByHC7' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this is a magic string that's repeated in many tests, would it be better to have it in a constant with a meaningful name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it would be clearer. I did not want to refactor too much of the tests. I had to override this value because it does not pass the identifier checksum validation. |
||
|
||
|
||
@pytest.fixture | ||
def deleg_doc_did(): | ||
return 'did:iotics:iotHHHHKpPGWWWC4FFo4d6oyzVVk6MXLmEgY' | ||
return 'did:iotics:iotDX4pLcd4y9RA6EvUrj6QaLSjxsZT6EEPJ' | ||
|
||
|
||
@pytest.fixture | ||
def another_doc_did(): | ||
return 'did:iotics:iotCmGJdzEte5dtvjjUspcoAeiWk5D5NTTyE' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the use of this alphabet is fundamental - how do other impl of the spec would know about this?
would all of them need to use the same "bitcoin_alphabet" ??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
base58 was written for bitcoin so the default is "bitcoin alphabet". I only specify here in case the default in the upstream library changes and to make the code clearer to read. I'm happy to revert this override if you prefer.