def run(): logger.initialize_default(force_console_output=True).setLevel(logging.INFO) file_handler = logger.log_to_file() file_handler.setLevel(logging.INFO) LOG_FORMAT_CUSTOM = '%(asctime)s| %(levelname)s : %(message)s' # noqa logger.set_colors(False, LOG_FORMAT_CUSTOM) logger.set_unhandled_exception_handler() walletd = WalletD() # noqa wallet_server = grpc.server( ThreadPoolExecutor(max_workers=config.user.wallet_api_threads), maximum_concurrent_rpcs=config.user.wallet_api_max_concurrent_rpc) add_WalletAPIServicer_to_server(WalletAPIService(walletd), wallet_server) wallet_server.add_insecure_port("{0}:{1}".format( config.user.wallet_api_host, config.user.wallet_api_port)) wallet_server.start() logger.info("WalletAPIService Started") try: while True: sleep(60) except Exception: # noqa wallet_server.stop(0)
def sign_and_push_transaction(self, tx, xmss, index, group_index=None, slave_index=None, enable_save=True): logger.info("Signing %s transaction by %s | OTS index %s", tx.type, xmss.qaddress, xmss.ots_index) tx.sign(xmss) if not tx.validate(True): raise Exception("Invalid Transaction") if enable_save: if slave_index is None: # noqa self._wallet.set_ots_index( index, xmss.ots_index ) # Move to next OTS index before broadcasting txn else: self._wallet.set_slave_ots_index(index, group_index, slave_index, xmss.ots_index) push_transaction_req = xrd_pb2.PushTransactionReq( transaction_signed=tx.pbdata) push_transaction_resp = self._public_stub.PushTransaction( push_transaction_req, timeout=CONNECTION_TIMEOUT) if push_transaction_resp.error_code != xrd_pb2.PushTransactionResp.SUBMITTED: raise Exception(push_transaction_resp.error_description)
def remove_address(self, qaddress: str) -> bool: self.authenticate() if self._wallet.remove(qaddress): logger.info("Removed Address %s", qaddress) return True return False
def get_recovery_seeds(self, qaddress: str): self.authenticate() xmss = self._wallet.get_xmss_by_qaddress(qaddress, self._passphrase) if xmss: logger.info("Recovery seeds requested for %s", qaddress) return xmss.hexseed, xmss.mnemonic raise ValueError("No such address found in wallet")
def unlock_wallet(self, passphrase: str): if not self._wallet.is_encrypted(): raise Exception('You cannot unlock an unencrypted Wallet') self._passphrase = passphrase self._wallet.decrypt( passphrase, first_address_only=True) # Check if Password Correct self._wallet.encrypt_item(0, passphrase) # Re-Encrypt first address item logger.info("Wallet Unlocked")
def encrypt_wallet(self, passphrase: str): if self._wallet.is_encrypted(): raise Exception('Wallet Already Encrypted') if not passphrase: raise Exception("Missing Passphrase") if len(self._wallet.address_items) == 0: raise ValueError( 'Cannot be encrypted as wallet does not have any address.') self._wallet.encrypt(passphrase) self._wallet.save() logger.info("Wallet Encrypted")
def add_new_address(self, height=10, hash_function='shake128') -> str: self.authenticate() if not hash_function: hash_function = 'shake128' if not height: height = 10 self._wallet.add_new_address(height, hash_function, True) self._encrypt_last_item() self._wallet.save() logger.info("Added New Address") return self._wallet.address_items[-1].qaddress
def add_new_address_with_slaves( self, height=10, number_of_slaves=config.user.number_of_slaves, hash_function='shake128') -> str: self.authenticate() if not hash_function: hash_function = 'shake128' if not height: height = 10 if height < 6: raise Exception("Height cannot be less than 6") if not number_of_slaves: number_of_slaves = config.user.number_of_slaves if number_of_slaves > 100: raise Exception("Number of slaves cannot be more than 100") xmss = self._wallet.add_new_address(height, hash_function, True) slave_xmss_list = self._wallet.add_slave( index=-1, height=height, number_of_slaves=number_of_slaves, hash_function=hash_function, force=True) self._encrypt_last_item() slave_pk_list = self.get_pk_list_from_xmss_list(slave_xmss_list) slave_tx = self.generate_slave_tx(xmss.pk, slave_pk_list) self.sign_and_push_transaction(slave_tx, xmss, -1) self._wallet.save() logger.info("Added New Address With Slaves") return self._wallet.address_items[-1].qaddress
def change_passphrase(self, old_passphrase: str, new_passphrase: str): if len(old_passphrase) == 0: raise Exception('Missing Old Passphrase') if len(new_passphrase) == 0: raise Exception('Missing New Passphrase') if old_passphrase == new_passphrase: raise Exception('Old Passphrase and New Passphrase cannot be same') self._passphrase = old_passphrase if not self._wallet: self.unlock_wallet(old_passphrase) try: self._wallet.decrypt(old_passphrase) except WalletDecryptionError: raise ValueError('Invalid Old Passphrase') self._wallet.encrypt(new_passphrase) self._wallet.save() self.lock_wallet() logger.info("Passphrase Changed")
def lock_wallet(self): if not self._wallet.is_encrypted(): raise Exception('You cannot lock an unencrypted Wallet') self._passphrase = None logger.info("Wallet Locked")