Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
DuniterPy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
31
Issues
31
List
Boards
Labels
Service Desk
Milestones
Merge Requests
4
Merge Requests
4
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
clients
python
DuniterPy
Commits
de855d0a
Commit
de855d0a
authored
Jan 20, 2019
by
Vincent Texier
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '71_handle_seedhex' into 'dev'
#71
: handle seedhex See merge request
!54
parents
8bcc1373
36572ae8
Pipeline
#4416
passed with stages
in 3 minutes and 43 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
3 deletions
+60
-3
duniterpy/helpers.py
duniterpy/helpers.py
+21
-0
duniterpy/key/signing_key.py
duniterpy/key/signing_key.py
+39
-3
No files found.
duniterpy/helpers.py
View file @
de855d0a
from
typing
import
Union
from
typing
import
Union
from
libnacl.encode
import
hex_decode
,
hex_encode
def
ensure_bytes
(
data
:
Union
[
str
,
bytes
])
->
bytes
:
def
ensure_bytes
(
data
:
Union
[
str
,
bytes
])
->
bytes
:
...
@@ -39,3 +40,23 @@ def xor_bytes(b1: bytes, b2: bytes) -> bytearray:
...
@@ -39,3 +40,23 @@ def xor_bytes(b1: bytes, b2: bytes) -> bytearray:
for
i1
,
i2
in
zip
(
b1
,
b2
):
for
i1
,
i2
in
zip
(
b1
,
b2
):
result
.
append
(
i1
^
i2
)
result
.
append
(
i1
^
i2
)
return
result
return
result
def
convert_seedhex_to_seed
(
seedhex
:
str
)
->
bytes
:
"""
Convert seedhex to seed
:param seedhex: seed coded in hexadecimal base
:rtype bytes:
"""
return
bytes
(
hex_decode
(
seedhex
.
encode
(
"utf-8"
)))
def
convert_seed_to_seedhex
(
seed
:
bytes
)
->
str
:
"""
Convert seed to seedhex
:param seed: seed
:rtype str:
"""
return
hex_encode
(
seed
).
decode
(
"utf-8"
)
duniterpy/key/signing_key.py
View file @
de855d0a
...
@@ -12,7 +12,7 @@ from libnacl.utils import load_key
...
@@ -12,7 +12,7 @@ from libnacl.utils import load_key
from
pylibscrypt
import
scrypt
from
pylibscrypt
import
scrypt
from
.base58
import
Base58Encoder
from
.base58
import
Base58Encoder
from
..helpers
import
ensure_bytes
,
xor_bytes
from
..helpers
import
ensure_bytes
,
xor_bytes
,
convert_seedhex_to_seed
,
convert_seed_to_seedhex
SEED_LENGTH
=
32
# Length of the key
SEED_LENGTH
=
32
# Length of the key
crypto_sign_BYTES
=
64
crypto_sign_BYTES
=
64
...
@@ -68,6 +68,42 @@ class SigningKey(libnacl.sign.Signer):
...
@@ -68,6 +68,42 @@ class SigningKey(libnacl.sign.Signer):
return
cls
(
seed
)
return
cls
(
seed
)
def
save_seedhex_file
(
self
,
path
:
str
)
->
None
:
"""
Save hexadecimal seed file from seed
:param path: Authentication file path
"""
seedhex
=
convert_seed_to_seedhex
(
self
.
seed
)
with
open
(
path
,
'w'
)
as
fh
:
fh
.
write
(
seedhex
)
@
staticmethod
def
from_seedhex_file
(
path
:
str
)
->
SigningKeyType
:
"""
Return SigningKey instance from Seedhex file
:param str path: Hexadecimal seed file path
"""
with
open
(
path
,
'r'
)
as
fh
:
seedhex
=
fh
.
read
()
return
SigningKey
.
from_seedhex
(
seedhex
)
@
classmethod
def
from_seedhex
(
cls
:
Type
[
SigningKeyType
],
seedhex
:
str
)
->
SigningKeyType
:
"""
Return SigningKey instance from Seedhex
:param str seedhex: Hexadecimal seed string
"""
regex_seedhex
=
compile
(
"([0-9a-fA-F]{64})"
)
match
=
search
(
regex_seedhex
,
seedhex
)
if
not
match
:
raise
Exception
(
'Error: Bad seed hexadecimal format'
)
seedhex
=
match
.
groups
()[
0
]
seed
=
convert_seedhex_to_seed
(
seedhex
)
return
cls
(
seed
)
def
save_private_key
(
self
,
path
:
str
)
->
None
:
def
save_private_key
(
self
,
path
:
str
)
->
None
:
"""
"""
Save authentication file
Save authentication file
...
@@ -111,8 +147,8 @@ class SigningKey(libnacl.sign.Signer):
...
@@ -111,8 +147,8 @@ class SigningKey(libnacl.sign.Signer):
pubsec_content
=
fh
.
read
()
pubsec_content
=
fh
.
read
()
# line patterns
# line patterns
regex_pubkey
=
compile
(
"pub: ([1-9A-HJ-NP-Za-km-z]
+
)"
,
MULTILINE
)
regex_pubkey
=
compile
(
"pub: ([1-9A-HJ-NP-Za-km-z]
{43,44}
)"
,
MULTILINE
)
regex_signkey
=
compile
(
"sec: ([1-9A-HJ-NP-Za-km-z]
+
)"
,
MULTILINE
)
regex_signkey
=
compile
(
"sec: ([1-9A-HJ-NP-Za-km-z]
{88,90}
)"
,
MULTILINE
)
# check public key field
# check public key field
match
=
search
(
regex_pubkey
,
pubsec_content
)
match
=
search
(
regex_pubkey
,
pubsec_content
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment