Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
silkaj
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
clients
python
silkaj
Commits
e2e26a38
Commit
e2e26a38
authored
2 years ago
by
Moul
Browse files
Options
Downloads
Patches
Plain Diff
[mypy]
#163
: Add type annotation on crypto_tools
parent
f6fac1dc
No related branches found
No related tags found
1 merge request
!214
#163: Introduce mypy
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
silkaj/crypto_tools.py
+8
-6
8 additions, 6 deletions
silkaj/crypto_tools.py
with
8 additions
and
6 deletions
silkaj/crypto_tools.py
+
8
−
6
View file @
e2e26a38
...
@@ -15,6 +15,7 @@
...
@@ -15,6 +15,7 @@
import
hashlib
import
hashlib
import
re
import
re
from
typing
import
Any
,
Optional
,
Union
import
base58
import
base58
...
@@ -27,7 +28,7 @@ CHECKSUM_PATTERN = f"[1-9A-HJ-NP-Za-km-z]{{{CHECKSUM_SIZE}}}"
...
@@ -27,7 +28,7 @@ CHECKSUM_PATTERN = f"[1-9A-HJ-NP-Za-km-z]{{{CHECKSUM_SIZE}}}"
PUBKEY_CHECKSUM_PATTERN
=
f
"
^
{
PUBKEY_PATTERN
}
:
{
CHECKSUM_PATTERN
}
$
"
PUBKEY_CHECKSUM_PATTERN
=
f
"
^
{
PUBKEY_PATTERN
}
:
{
CHECKSUM_PATTERN
}
$
"
def
is_pubkey_and_check
(
pubkey
)
:
def
is_pubkey_and_check
(
pubkey
:
str
)
->
Union
[
str
,
bool
]
:
"""
"""
Checks if the given argument contains a pubkey.
Checks if the given argument contains a pubkey.
If so, verifies the checksum if needed and returns the pubkey.
If so, verifies the checksum if needed and returns the pubkey.
...
@@ -41,7 +42,7 @@ def is_pubkey_and_check(pubkey):
...
@@ -41,7 +42,7 @@ def is_pubkey_and_check(pubkey):
return
False
return
False
def
check_pubkey_format
(
pubkey
,
display_error
=
True
)
:
def
check_pubkey_format
(
pubkey
:
str
,
display_error
:
bool
=
True
)
->
Optional
[
bool
]
:
"""
"""
Checks if a pubkey has a checksum.
Checks if a pubkey has a checksum.
Exits if the pubkey is invalid.
Exits if the pubkey is invalid.
...
@@ -52,10 +53,10 @@ def check_pubkey_format(pubkey, display_error=True):
...
@@ -52,10 +53,10 @@ def check_pubkey_format(pubkey, display_error=True):
return
True
return
True
elif
display_error
:
elif
display_error
:
message_exit
(
f
"
Error: bad format for following public key:
{
pubkey
}
"
)
message_exit
(
f
"
Error: bad format for following public key:
{
pubkey
}
"
)
return
return
None
def
validate_checksum
(
pubkey_checksum
)
:
def
validate_checksum
(
pubkey_checksum
:
str
)
->
Any
:
"""
"""
Check pubkey checksum after the pubkey, delimited by
"
:
"
.
Check pubkey checksum after the pubkey, delimited by
"
:
"
.
If check pass: return pubkey
If check pass: return pubkey
...
@@ -68,12 +69,13 @@ def validate_checksum(pubkey_checksum):
...
@@ -68,12 +69,13 @@ def validate_checksum(pubkey_checksum):
f
"
Error: public key
'
{
pubkey
}
'
does not match checksum
'
{
checksum
}
'
.
\n\
f
"
Error: public key
'
{
pubkey
}
'
does not match checksum
'
{
checksum
}
'
.
\n\
Please verify the public key.
"
Please verify the public key.
"
)
)
return
None
def
gen_checksum
(
pubkey
)
:
def
gen_checksum
(
pubkey
:
str
)
->
str
:
"""
"""
Returns the checksum of the input pubkey (encoded in b58)
Returns the checksum of the input pubkey (encoded in b58)
"""
"""
pubkey_byte
=
base58
.
b58decode
(
pubkey
)
pubkey_byte
=
base58
.
b58decode
(
pubkey
)
hash
=
hashlib
.
sha256
(
hashlib
.
sha256
(
pubkey_byte
).
digest
()).
digest
()
hash
=
hashlib
.
sha256
(
hashlib
.
sha256
(
pubkey_byte
).
digest
()).
digest
()
return
base58
.
b58encode
(
hash
)[:
3
].
decode
(
"
utf-8
"
)
return
str
(
base58
.
b58encode
(
hash
)[:
3
].
decode
(
"
utf-8
"
)
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment