|
8 | 8 | import os
|
9 | 9 | from py_ecc.bls import G2ProofOfPossession as bls
|
10 | 10 | from secrets import randbits
|
11 |
| -from typing import Any, Dict, Union |
| 11 | +from typing import Any, Dict, Optional, Union |
12 | 12 | from unicodedata import normalize
|
13 | 13 | from uuid import uuid4
|
14 | 14 |
|
@@ -127,18 +127,20 @@ def _process_password(password: str) -> bytes:
|
127 | 127 |
|
128 | 128 | @classmethod
|
129 | 129 | def encrypt(cls, *, secret: bytes, password: str, path: str='',
|
130 |
| - kdf_salt: bytes=randbits(256).to_bytes(32, 'big'), |
131 |
| - aes_iv: bytes=randbits(128).to_bytes(16, 'big')) -> 'Keystore': |
| 130 | + kdf_salt: Optional[bytes]=None, |
| 131 | + aes_iv: Optional[bytes]=None) -> 'Keystore': |
132 | 132 | """
|
133 | 133 | Encrypt a secret (BLS SK) as an EIP 2335 Keystore.
|
134 | 134 | """
|
135 | 135 | keystore = cls()
|
136 | 136 | keystore.uuid = str(uuid4())
|
| 137 | + kdf_salt = kdf_salt if kdf_salt is not None else randbits(256).to_bytes(32, 'big') |
137 | 138 | keystore.crypto.kdf.params['salt'] = kdf_salt
|
138 | 139 | decryption_key = keystore.kdf(
|
139 | 140 | password=cls._process_password(password),
|
140 | 141 | **keystore.crypto.kdf.params
|
141 | 142 | )
|
| 143 | + aes_iv = aes_iv if aes_iv is not None else randbits(128).to_bytes(16, 'big') |
142 | 144 | keystore.crypto.cipher.params['iv'] = aes_iv
|
143 | 145 | cipher = AES_128_CTR(key=decryption_key[:16], **keystore.crypto.cipher.params)
|
144 | 146 | keystore.crypto.cipher.message = cipher.encrypt(secret)
|
|
0 commit comments