def __init__(self, private_key=None, public_key=None): """ Constructor for RSA key pair. If only private key available then public key will be generate from private. Args: private_key (bytes): rsa private key public_key (bytes, optional): rsa public key """ super(RSA, self).__init__() if private_key and public_key: self._private_key = private_key self._public_key = public_key self._private_key_obj = private_key_der_to_object(private_key=self._private_key) self._public_key_obj = public_key_der_to_object(public_key=self._public_key) elif private_key: self._private_key = private_key self._private_key_obj = private_key_der_to_object(private_key=self._private_key) self._public_key_obj = self._private_key_obj.public_key() self._public_key = public_key_to_der(public_key=self._public_key_obj) elif public_key: self._public_key = public_key self._public_key_obj = public_key_der_to_object(public_key=self._public_key) if self._private_key: self._private_key_hex = self._private_key.hex() self._public_key_hex = self._public_key.hex() self._address = generate_address(RemmeFamilyName.PUBLIC_KEY.value, self._public_key) self._key_type = KeyType.RSA
def get_address_from_public_key(public_key): """ Get address from public key. Args: public_key (bytes): public key in bytes Returns: Address in blockchain generated from public key string. """ return generate_address(RemmeFamilyName.PUBLIC_KEY.value, public_key)
def __init__(self, private_key=None, public_key=None): """ Constructor for ``ECDSA`` key pair. If only private key available then public key will be generate from private. Args: private_key (bytes): secp256k1 private key public_key (bytes, optional): secp256k1 public key """ if private_key and public_key: self._private_key = private_key self._public_key = public_key self._private_key_obj = self._private_key_bytes_to_object( private_key=self._private_key) self._public_key_obj = self._public_key_bytes_to_object( public_key=self._public_key) elif private_key: self._private_key = private_key self._private_key_obj = self._private_key_bytes_to_object( private_key=self._private_key) self._public_key_obj = Secp256k1PublicKey( self._private_key_obj.secp256k1_private_key.pubkey) self._public_key = self._public_key_obj.as_bytes() elif public_key: self._public_key = public_key self._public_key_obj = self._public_key_bytes_to_object( public_key=self._public_key) if private_key: self._private_key_hex = self._private_key.hex() else: self._private_key_hex = None self._public_key_hex = self._public_key.hex() self._address = generate_address(RemmeFamilyName.PUBLIC_KEY.value, self._public_key) self._key_type = KeyType.ECDSA super(ECDSA, self).__init__( private_key=private_key if private_key else None, public_key=self._public_key, private_key_hex=self._private_key_hex, public_key_hex=self._public_key_hex, key_type=self._key_type, address=self._address, )
async def _get_info_by_public_key(self, address): validate_address(address=address) info = await self._remme_api.send_request( method=RemmeMethods.PUBLIC_KEY, params=public_key_address(address), ) if info.get('error') is None: info['address'] = generate_address(self._family_name, address) return PublicKeyInfo(data=info) raise Exception('This public key was not found.')
def __init__(self, private_key_hex='', account_type=AccountType.USER): """ Get private key, create signer by using private key, generate public key from private key and generate account address by using public key and family name. Args: private_key_hex (string): private key in hex format account_type (enum): account type (user, node) References:: - https://docs.remme.io/remme-core/docs/family-account.html#addressing To use: **Get private key:** .. code-block:: python private_key_hex = 'ac124700cc4325cc2a78b22b9acb039d9efe859ef673b871d55d1078391934f9' account = RemmeAccount() print(account.private_key_hex) # 'ac124700cc4325cc2a78b22b9acb039d9efe859ef673b871d55d1078391934f9' **Generate new private key:** .. sourcecode:: python account = RemmeAccount() print(account.private_key_hex) # 'b5167700cc4325cc2a78b22b9acb039d9efe859ef673b871d55d10783919129f' """ if private_key_hex: private_key = hex_to_bytes(private_key_hex) else: private_key, _ = ECDSA.generate_key_pair() super(RemmeAccount, self).__init__(private_key=private_key) is_user = account_type == AccountType.USER self._family_name = RemmeFamilyName.ACCOUNT.value if is_user else RemmeFamilyName.NODE_ACCOUNT.value self._address = generate_address(self._family_name, self._public_key_hex)
def __init__(self, private_key, public_key): """ Constructor for EdDSA key pair. If only private key available then public key will be generate from private. Args: private_key (bytes): ed25519 private key public_key (bytes, optional): ed25519 public key """ super(EdDSA, self).__init__() if private_key and public_key: self._private_key = private_key self._public_key = public_key self._private_key_obj = SigningKey(self._private_key) self._public_key_obj = VerifyingKey(self._public_key) elif private_key: self._private_key = private_key self._private_key_obj = SigningKey(self._private_key) self._public_key_obj = self._private_key_obj.get_verifying_key() self._public_key = self._public_key_obj.to_bytes() elif public_key: self._public_key = public_key self._public_key_obj = VerifyingKey(self._public_key) if self._private_key: self._private_key_hex = self._private_key.hex() self._public_key_hex = self._public_key.hex() self._address = generate_address( _family_name=RemmeFamilyName.PUBLIC_KEY.value, _public_key_to=self._public_key, ) self._key_type = KeyType.EdDSA
async def store(self, data): """ Store public key payload bytes with data into REMChain. Send transaction to chain. Args: data (object): payload bytes Returns: Information about storing public key to REMChain. To use: .. code-block:: python # payload_bytes is the transaction payload generated from method # remme.public_key_storage.create from remme import Remme as remme store_response = await remme.public_key_storage.store(payload_bytes) async for msg in store_response.connect_to_web_socket(): print(msg) store_response.close_web_socket() """ owner_address = '' owner_payload = NewPubKeyStoreAndPayPayload() owner_payload.ParseFromString(data) new_pub_key_payload = NewPubKeyPayload() new_pub_key_payload.ParseFromString(data) message = owner_payload if owner_payload.pub_key_payload.entity_hash else new_pub_key_payload if isinstance(message, NewPubKeyPayload): pub_key_address = self._construct_address_from_payload( payload=message) elif isinstance(message, NewPubKeyStoreAndPayPayload): owner_public_key = message.owner_public_key signature_by_owner = message.signature_by_owner pub_key_payload = message.pub_key_payload self._verify_payload_owner( owner_public_key=owner_public_key, signature_by_owner=signature_by_owner, pub_key_payload=pub_key_payload, ) pub_key_address = self._construct_address_from_payload( payload=pub_key_payload) owner_address = generate_address( _family_name=RemmeFamilyName.ACCOUNT.value, _public_key_to=owner_public_key, ) else: raise Exception('Invalid payload.') inputs_and_outputs = [ pub_key_address, CONSENSUS_ADDRESS, self._settings_address ] if owner_address: inputs_and_outputs.append(owner_address) payload_bytes = self._generate_transaction_payload( method=PubKeyMethod.STORE_AND_PAY if owner_address else PubKeyMethod.STORE, data=data, ) return await self._create_and_send_transaction( inputs=inputs_and_outputs, outputs=inputs_and_outputs, payload_bytes=payload_bytes, )
def _get_addresses(self, method, swap_id, receiver_address=None): """ Get addresses for inputs and outputs. Args: method (protobuf): AtomicSwapMethod swap_id (string): swap id optional receiver_address (string): receiver address Returns: Lists of addresses inputs and outputs. """ addresses = [ generate_address(_family_name=self._family_name, _public_key_to=swap_id) ] inputs, outputs = None, None if method not in ATOMIC_SWAP_METHODS: inputs = outputs = addresses return inputs, outputs if method == AtomicSwapMethod.INIT: inputs = [ self._settings_swap_comission, CONSENSUS_ADDRESS, ZERO_ADDRESS, BLOCK_INFO_CONFIG_ADDRESS, BLOCK_INFO_NAMESPACE_ADDRESS, ] outputs = [ self._settings_swap_comission, CONSENSUS_ADDRESS, ZERO_ADDRESS, ] elif method == AtomicSwapMethod.EXPIRE: inputs = [ CONSENSUS_ADDRESS, ZERO_ADDRESS, BLOCK_INFO_CONFIG_ADDRESS, BLOCK_INFO_NAMESPACE_ADDRESS, ] outputs = [ CONSENSUS_ADDRESS, ZERO_ADDRESS, ] elif method == AtomicSwapMethod.CLOSE: inputs = [ CONSENSUS_ADDRESS, ZERO_ADDRESS, receiver_address, ] outputs = [ CONSENSUS_ADDRESS, ZERO_ADDRESS, receiver_address, ] elif method == AtomicSwapMethod.SET_SECRET_LOCK: inputs = [ CONSENSUS_ADDRESS, ] outputs = [ CONSENSUS_ADDRESS, ] elif method == AtomicSwapMethod.APPROVE: inputs = [ CONSENSUS_ADDRESS, ] outputs = [ CONSENSUS_ADDRESS, ] inputs.extend(addresses) outputs.extend(addresses) return inputs, outputs