Skip to content
Snippets Groups Projects
Commit 6e016734 authored by Vincent Texier's avatar Vincent Texier
Browse files
parent d0d74e92
No related branches found
No related tags found
No related merge requests found
Pipeline #12185 passed
...@@ -22,7 +22,7 @@ from typing import Optional, Union, TypeVar, Type ...@@ -22,7 +22,7 @@ from typing import Optional, Union, TypeVar, Type
import libnacl.sign import libnacl.sign
import pyaes import pyaes
from libnacl.utils import load_key from libnacl.utils import load_key
from hashlib import scrypt from hashlib import scrypt, sha256
from .scrypt_params import ScryptParams from .scrypt_params import ScryptParams
from .base58 import Base58Encoder from .base58 import Base58Encoder
...@@ -509,3 +509,33 @@ Data: {data}""".format( ...@@ -509,3 +509,33 @@ Data: {data}""".format(
seed = bytes(base64.b64decode(secret)[0:32]) seed = bytes(base64.b64decode(secret)[0:32])
return cls(seed) return cls(seed)
@classmethod
def from_dubp_mnemonic(
cls, mnemonic: str, scrypt_params: ScryptParams = None
) -> SigningKeyType:
"""
Generate key pair instance from a DUBP mnemonic passphrase (128 bits, twelve words)
See https://git.duniter.org/documents/rfcs/blob/dubp-mnemonic/rfc/0014_Dubp_Mnemonic.md
:param mnemonic: Passphrase generated from a mnemonic algorithm
:param scrypt_params: ScryptParams instance (default=None)
:return:
"""
assert len(mnemonic.split(" ")) == 12
if scrypt_params is None:
scrypt_params = ScryptParams()
_password = mnemonic.encode("utf-8") # type: bytes
_salt = sha256(b"dubp" + _password).digest() # type: bytes
_seed = scrypt(
password=_password,
salt=_salt,
n=scrypt_params.N, # 4096
r=scrypt_params.r, # 16
p=scrypt_params.p, # 1
dklen=scrypt_params.seed_length, # 32
) # type: bytes
return cls(_seed)
...@@ -14,7 +14,7 @@ GNU General Public License for more details. ...@@ -14,7 +14,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
import base64
import os import os
from duniterpy.key import VerifyingKey, SigningKey, PublicKey from duniterpy.key import VerifyingKey, SigningKey, PublicKey
...@@ -144,3 +144,16 @@ class TestSigningKey(unittest.TestCase): ...@@ -144,3 +144,16 @@ class TestSigningKey(unittest.TestCase):
sign_key_load.vk.hex(), sign_key_load.vk.hex(),
"d27f4cb2bfadbaf45b61714b896d4639ab90db035aee746611cdd342bdaa8996", "d27f4cb2bfadbaf45b61714b896d4639ab90db035aee746611cdd342bdaa8996",
) )
def test_dubp_mnemonic(self):
mnemonic = (
"tongue cute mail fossil great frozen same social weasel impact brush kind"
)
keypair = SigningKey.from_dubp_mnemonic(mnemonic)
self.assertEqual(
base64.b64encode(keypair.seed).decode("utf-8"),
"qGdvpbP9lJe7ZG4ZUSyu33KFeAEs/KkshAp9gEI4ReY=",
)
self.assertEqual(keypair.pubkey, "732SSfuwjB7jkt9th1zerGhphs6nknaCBCTozxUcPWPU")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment