Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
DuniterPy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
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
DuniterPy
Commits
90b96755
Commit
90b96755
authored
6 years ago
by
Vincent Texier
Browse files
Options
Downloads
Patches
Plain Diff
Revert "issue
#50
Add export/import PubSec V1 file and its example"
This reverts commit
466d248a
parent
466d248a
No related branches found
No related tags found
No related merge requests found
Pipeline
#4368
passed
6 years ago
Stage: github-sync
Stage: build
Stage: test
Stage: release
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
duniterpy/key/signing_key.py
+4
-67
4 additions, 67 deletions
duniterpy/key/signing_key.py
examples/save_and_load_private_key_file_pubsec.py
+0
-58
0 additions, 58 deletions
examples/save_and_load_private_key_file_pubsec.py
with
4 additions
and
125 deletions
duniterpy/key/signing_key.py
+
4
−
67
View file @
90b96755
...
...
@@ -79,60 +79,6 @@ class SigningKey(libnacl.sign.Signer):
curve25519_secret_key
=
libnacl
.
crypto_sign_ed25519_sk_to_curve25519
(
self
.
sk
)
return
libnacl
.
crypto_box_seal_open
(
message
,
curve25519_public_key
,
curve25519_secret_key
).
decode
(
'
utf-8
'
)
@classmethod
def
from_pubsec_file
(
cls
:
Type
[
SigningKeyType
],
path
:
str
)
->
SigningKeyType
:
"""
Return SigningKey instance from Duniter WIF file
:param path: Path to WIF file
"""
with
open
(
path
,
'
r
'
)
as
fh
:
pubsec_content
=
fh
.
read
()
# line patterns
regex_pubkey
=
compile
(
"
pub: ([1-9A-HJ-NP-Za-km-z]+)
"
,
MULTILINE
)
regex_signkey
=
compile
(
"
sec: ([1-9A-HJ-NP-Za-km-z]+)
"
,
MULTILINE
)
# check public key field
match
=
search
(
regex_pubkey
,
pubsec_content
)
if
not
match
:
raise
Exception
(
'
Error: Bad format PubSec v1 file, missing public key
'
)
# check signkey field
match
=
search
(
regex_signkey
,
pubsec_content
)
if
not
match
:
raise
Exception
(
'
Error: Bad format PubSec v1 file, missing sec key
'
)
# capture signkey
signkey_hex
=
match
.
groups
()[
0
]
# extract seed from signkey
seed
=
bytes
(
Base58Encoder
.
decode
(
signkey_hex
)[
0
:
32
])
return
cls
(
seed
)
def
save_pubsec_file
(
self
,
path
:
str
)
->
None
:
"""
Save a Duniter PubSec file (PubSec) v1
:param path: Path to file
"""
# version
version
=
1
# base58 encode keys
base58_signing_key
=
Base58Encoder
.
encode
(
self
.
sk
)
base58_public_key
=
self
.
pubkey
# save file
with
open
(
path
,
'
w
'
)
as
fh
:
fh
.
write
(
"""
Type: PubSec
Version: {version}
pub: {pubkey}
sec: {signkey}
"""
.
format
(
version
=
version
,
pubkey
=
base58_public_key
,
signkey
=
base58_signing_key
)
)
@classmethod
def
from_wif_file
(
cls
:
Type
[
SigningKeyType
],
path
:
str
)
->
SigningKeyType
:
"""
...
...
@@ -143,27 +89,23 @@ sec: {signkey}""".format(version=version, pubkey=base58_public_key, signkey=base
with
open
(
path
,
'
r
'
)
as
fh
:
wif_content
=
fh
.
read
()
# check data field
regex
=
compile
(
'
Data: ([1-9A-HJ-NP-Za-km-z]+)
'
,
MULTILINE
)
match
=
search
(
regex
,
wif_content
)
if
not
match
:
raise
Exception
(
'
Error: Bad format WIF v1 file
'
)
# capture hexa wif key
wif_hex
=
match
.
groups
()[
0
]
wif_bytes
=
Base58Encoder
.
decode
(
wif_hex
)
if
len
(
wif_bytes
)
!=
35
:
raise
Exception
(
"
Error: the size of WIF is invalid
"
)
# extract data
checksum_from_wif
=
wif_bytes
[
-
2
:]
fi
=
wif_bytes
[
0
:
1
]
seed
=
wif_bytes
[
1
:
-
2
]
seed_fi
=
wif_bytes
[
0
:
-
2
]
# check WIF format flag
if
fi
!=
b
"
\x01
"
:
raise
Exception
(
"
Error: bad
format
version
, not WIF
"
)
raise
Exception
(
"
Error: bad
WIF
version
"
)
# checksum control
checksum
=
libnacl
.
crypto_hash_sha256
(
libnacl
.
crypto_hash_sha256
(
seed_fi
))[
0
:
2
]
...
...
@@ -178,7 +120,7 @@ sec: {signkey}""".format(version=version, pubkey=base58_public_key, signkey=base
:param path: Path to file
"""
#
version
#
Cesium v1
version
=
1
# add format to seed (1=WIF,2=EWIF)
...
...
@@ -189,7 +131,6 @@ sec: {signkey}""".format(version=version, pubkey=base58_public_key, signkey=base
sha256_v2
=
libnacl
.
crypto_hash_sha256
(
sha256_v1
)
checksum
=
sha256_v2
[
0
:
2
]
# base58 encode key and checksum
wif_key
=
Base58Encoder
.
encode
(
seed_fi
+
checksum
)
with
open
(
path
,
'
w
'
)
as
fh
:
...
...
@@ -210,19 +151,16 @@ Data: {data}""".format(version=version, data=wif_key)
with
open
(
path
,
'
r
'
)
as
fh
:
wif_content
=
fh
.
read
()
# check data field
regex
=
compile
(
'
Data: ([1-9A-HJ-NP-Za-km-z]+)
'
,
MULTILINE
)
match
=
search
(
regex
,
wif_content
)
if
not
match
:
raise
Exception
(
'
Error: Bad format EWIF v1 file
'
)
# capture ewif key
ewif_hex
=
match
.
groups
()[
0
]
ewif_bytes
=
Base58Encoder
.
decode
(
ewif_hex
)
if
len
(
ewif_bytes
)
!=
39
:
raise
Exception
(
"
Error: the size of EWIF is invalid
"
)
# extract data
fi
=
ewif_bytes
[
0
:
1
]
checksum_from_ewif
=
ewif_bytes
[
-
2
:]
ewif_no_checksum
=
ewif_bytes
[
0
:
-
2
]
...
...
@@ -230,9 +168,8 @@ Data: {data}""".format(version=version, data=wif_key)
encryptedhalf1
=
ewif_bytes
[
5
:
21
]
encryptedhalf2
=
ewif_bytes
[
21
:
37
]
# check format flag
if
fi
!=
b
"
\x02
"
:
raise
Exception
(
"
Error: bad
format
version
, not EWIF
"
)
raise
Exception
(
"
Error: bad
EWIF
version
"
)
# checksum control
checksum
=
libnacl
.
crypto_hash_sha256
(
libnacl
.
crypto_hash_sha256
(
ewif_no_checksum
))[
0
:
2
]
...
...
@@ -272,7 +209,7 @@ Data: {data}""".format(version=version, data=wif_key)
:param path: Path to file
:param password:
"""
# version
#
WIF Format
version
version
=
1
# add version to seed
...
...
This diff is collapsed.
Click to expand it.
examples/save_and_load_private_key_file_pubsec.py
deleted
100644 → 0
+
0
−
58
View file @
466d248a
from
duniterpy.key
import
SigningKey
import
getpass
import
os
if
"
XDG_CONFIG_HOME
"
in
os
.
environ
:
home_path
=
os
.
environ
[
"
XDG_CONFIG_HOME
"
]
elif
"
HOME
"
in
os
.
environ
:
home_path
=
os
.
environ
[
"
HOME
"
]
elif
"
APPDATA
"
in
os
.
environ
:
home_path
=
os
.
environ
[
"
APPDATA
"
]
else
:
home_path
=
os
.
path
.
dirname
(
__file__
)
# CONFIG #######################################
# WARNING : Hide this file in a safe and secure place
# If one day you forget your credentials,
# you'll have to use one of your private keys instead
PRIVATE_KEY_FILE_PATH
=
os
.
path
.
join
(
home_path
,
"
.duniter_account_pubsec_v1.duniterkey
"
)
################################################
# prompt hidden user entry
# salt = getpass.getpass("Enter your passphrase (salt): ")
salt
=
"
vit@free.fr
"
# prompt hidden user entry
# password = getpass.getpass("Enter your password: ")
password
=
"
BxD765007
"
# prompt public key
# pubkey = input("Enter your public key: ")
pubkey
=
"
DpJse2t7fyH9LC9FTMQHsMGZToXLmVQ8EV2eP47ipHDC
"
# init signer instance
signer
=
SigningKey
.
from_credentials
(
salt
,
password
)
# check public key
if
signer
.
pubkey
!=
pubkey
:
print
(
"
Bad credentials!
"
)
exit
(
1
)
# save private key in a file (PubSec v1 format)
signer
.
save_pubsec_file
(
PRIVATE_KEY_FILE_PATH
)
# document saved
print
(
"
Private key for public key %s saved in %s
"
%
(
signer
.
pubkey
,
PRIVATE_KEY_FILE_PATH
))
try
:
# load private keys from file
loaded_signer
=
SigningKey
.
from_pubsec_file
(
PRIVATE_KEY_FILE_PATH
)
# check public key from file
print
(
"
Public key %s loaded from file %s
"
%
(
loaded_signer
.
pubkey
,
PRIVATE_KEY_FILE_PATH
))
except
Exception
as
e
:
print
(
e
)
exit
(
1
)
exit
(
0
)
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