def _load_or_create_node_key(self) -> KeyPair: if self.boot_info.args.beacon_nodekey: privkey = Secp256k1PrivateKey.new( decode_hex(self.boot_info.args.beacon_nodekey) ) key_pair = KeyPair(private_key=privkey, public_key=privkey.get_public_key()) return key_pair else: config = self.boot_info.trinity_config beacon_nodekey_path = f"{config.nodekey_path}-beacon" if os.path.isfile(beacon_nodekey_path): with open(beacon_nodekey_path, "rb") as f: key_data = f.read() private_key = Secp256k1PrivateKey.new(key_data) key_pair = KeyPair( private_key=private_key, public_key=private_key.get_public_key() ) return key_pair else: key_pair = create_new_key_pair() private_key_bytes = key_pair.private_key.to_bytes() with open(beacon_nodekey_path, "wb") as f: f.write(private_key_bytes) return key_pair
def create_new_key_pair(curve: str) -> KeyPair: """ Return a new ECC keypair with the requested ``curve`` type, e.g. "P-256". """ private_key = ECCPrivateKey.new(curve) public_key = private_key.get_public_key() return KeyPair(private_key, public_key)
def create_new_key_pair(bits: int = 2048, e: int = 65537) -> KeyPair: """ Returns a new RSA keypair with the requested key size (``bits``) and the given public exponent ``e``. Sane defaults are provided for both values. """ private_key = RSAPrivateKey.new(bits, e) public_key = private_key.get_public_key() return KeyPair(private_key, public_key)
def create_new_key_pair(secret: bytes = None) -> KeyPair: """ Returns a new Secp256k1 keypair derived from the provided ``secret``, a sequence of bytes corresponding to some integer between 0 and the group order. A valid secret is created if ``None`` is passed. """ private_key = Secp256k1PrivateKey.new(secret) public_key = private_key.get_public_key() return KeyPair(private_key, public_key)
def do_start(self) -> None: trinity_config = self.boot_info.trinity_config beacon_app_config = trinity_config.get_app_config(BeaconAppConfig) base_db = DBClient.connect(trinity_config.database_ipc_path) chain_config = beacon_app_config.get_chain_config() attestation_pool = AttestationPool() chain = chain_config.beacon_chain_class(base_db, attestation_pool, chain_config.genesis_config) key_pair: KeyPair if self.boot_info.args.beacon_nodekey: privkey = Secp256k1PrivateKey( bytes.fromhex(self.boot_info.args.beacon_nodekey)) key_pair = KeyPair(private_key=privkey, public_key=privkey.get_public_key()) else: key_pair = create_new_key_pair() # TODO: Handle `bootstrap_nodes`. libp2p_node = Node( key_pair=key_pair, listen_ip="127.0.0.1", # FIXME: Should be configurable listen_port=self.boot_info.args.port, preferred_nodes=trinity_config.preferred_nodes, chain=chain, ) state = chain.get_state_by_slot( chain_config.genesis_config.GENESIS_SLOT) registry_pubkeys = [v_record.pubkey for v_record in state.validators] validator_privkeys = {} validator_keymap = chain_config.genesis_data.validator_keymap for pubkey in validator_keymap: validator_index = cast(ValidatorIndex, registry_pubkeys.index(pubkey)) validator_privkeys[validator_index] = validator_keymap[pubkey] def fake_get_ready_attestations_fn() -> Tuple[Attestation, ...]: return tuple() validator = Validator( chain=chain, p2p_node=libp2p_node, validator_privkeys=validator_privkeys, event_bus=self.event_bus, token=libp2p_node.cancel_token, get_ready_attestations_fn= fake_get_ready_attestations_fn, # FIXME: BCCReceiveServer.get_ready_attestations # noqa: E501 ) slot_ticker = SlotTicker( genesis_slot=chain_config.genesis_config.GENESIS_SLOT, genesis_time=chain_config.genesis_data.genesis_time, seconds_per_slot=chain_config.genesis_config.SECONDS_PER_SLOT, event_bus=self.event_bus, token=libp2p_node.cancel_token, ) asyncio.ensure_future( exit_with_services( self._event_bus_service, libp2p_node, slot_ticker, validator, )) asyncio.ensure_future(libp2p_node.run()) asyncio.ensure_future(slot_ticker.run()) asyncio.ensure_future(validator.run())
def create_new_key_pair(seed: bytes = None) -> KeyPair: private_key = Ed25519PrivateKey.new(seed) public_key = private_key.get_public_key() return KeyPair(private_key, public_key)