def _init_node_key(self): prikey_file = conf.PRIVATE_PATH if conf.PRIVATE_PASSWORD: password = conf.PRIVATE_PASSWORD else: password = getpass.getpass(f"Input your keystore password: ") signer = Signer.from_prikey_file(prikey_file, password) self._make_peer_id(signer.address) self._node_key = signer.get_private_secret()
def key_convert(): import os if os.path.exists(conf.PRIVATE_PATH) and conf.PRIVATE_PATH.endswith( (".pem", ".der")): import getpass import re from loopchain.crypto.signature import Signer from eth_keyfile import create_keyfile_json private_path = conf.PRIVATE_PATH password = conf.PRIVATE_PASSWORD if not password: password = getpass.getpass(f"Input your keystore password: "******"Something wrong. {e!r}") return private_key = signer.private_key.secret address = signer.address key_store_contents = create_keyfile_json(private_key, bytes(password, "utf-8"), iterations=16384, kdf="scrypt") key_store_contents["address"] = address key_store_contents["coinType"] = "icx" new_keypath = re.sub("(pem|der)$", "json", private_path) with open(new_keypath, 'w') as f: f.write(json.dumps(key_store_contents)) print( f"{new_keypath} created. update your PRIVATE_PATH in config file") else: print(f"Nothing to do. you don't have .pem or .der key file!")
def setUpClass(cls) -> None: cls.temp_dir = tempfile.TemporaryDirectory() # Private Key cls.private_key = ec.generate_private_key(ec.SECP256K1, default_backend()) cls.private_der_path = os.path.join(cls.temp_dir.name, "private.der") with open(cls.private_der_path, "wb") as private_der_file: private_der_file.write( cls.private_key.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.BestAvailableEncryption(b"TEST") ) ) cls.private_pem_path = os.path.join(cls.temp_dir.name, "private.pem") with open(cls.private_pem_path, "wb") as private_pem_file: private_pem_file.write( cls.private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.BestAvailableEncryption(b"TEST") ) ) key_info = keys.PrivateKeyInfo.load(cls.private_key.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() )) cls.private_key_bytes = long_to_bytes(key_info['private_key'].native['private_key']) # Public Key cls.public_key = cls.private_key.public_key() cls.public_der_path = os.path.join(cls.temp_dir.name, "public.der") with open(cls.public_der_path, "wb") as public_der_file: public_der_file.write( cls.public_key.public_bytes( encoding=serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo ) ) cls.public_pem_path = os.path.join(cls.temp_dir.name, "public.pem") with open(cls.public_pem_path, "wb") as public_pem_file: public_pem_file.write( cls.public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) ) key_info = keys.PublicKeyInfo.load( cls.public_key.public_bytes( encoding=serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo ) ) cls.public_key_bytes = key_info['public_key'].native cls.signer_private_key_bytes = Signer.from_prikey(cls.private_key_bytes) cls.signer_private_key_der = Signer.from_prikey_file(cls.private_der_path, b"TEST") cls.signer_private_key_pem = Signer.from_prikey_file(cls.private_pem_path, b"TEST") cls.sign_verifier_private_key_bytes = SignVerifier.from_prikey(cls.private_key_bytes) cls.sign_verifier_private_key_der = SignVerifier.from_prikey_file(cls.private_der_path, b"TEST") cls.sign_verifier_private_key_pem = SignVerifier.from_prikey_file(cls.private_pem_path, b"TEST") cls.sign_verifier_public_key_bytes = SignVerifier.from_pubkey(cls.public_key_bytes) cls.sign_verifier_public_key_der = SignVerifier.from_pubkey_file(cls.public_der_path) cls.sign_verifier_public_key_pem = SignVerifier.from_pubkey_file(cls.public_pem_path)