def start_miner(): from halocoin.model.wallet import Wallet wallet_name = request.values.get('wallet_name', None) password = request.values.get('password', None) if wallet_name is None: default_wallet = engine.instance.account.get_default_wallet() if default_wallet is not None: wallet_name = default_wallet['wallet_name'] password = default_wallet['password'] encrypted_wallet_content = engine.instance.account.get_wallet(wallet_name) if encrypted_wallet_content is not None: try: wallet = Wallet.from_string( tools.decrypt(password, encrypted_wallet_content)) except: return generate_json_response("Wallet password incorrect") else: return generate_json_response("Error occurred") if engine.instance.miner.get_state() == Service.RUNNING: return generate_json_response('Miner is already running.') elif wallet is None: return generate_json_response('Given wallet is not valid.') else: engine.instance.miner.set_wallet(wallet) engine.instance.miner.register() return generate_json_response('Running miner')
def new_wallet(): from halocoin.model.wallet import Wallet wallet_name = request.values.get('wallet_name', None) pw = request.values.get('password', None) wallet = Wallet(wallet_name) success = engine.instance.account.new_wallet(pw, wallet) return generate_json_response({"name": wallet_name, "success": success})
def info_wallet(): from halocoin.model.wallet import Wallet wallet_name = request.values.get('wallet_name', None) password = request.values.get('password', None) if wallet_name is None: default_wallet = engine.instance.account.get_default_wallet() if default_wallet is not None: wallet_name = default_wallet['wallet_name'] password = default_wallet['password'] encrypted_wallet_content = engine.instance.account.get_wallet(wallet_name) if encrypted_wallet_content is not None: try: wallet = Wallet.from_string( tools.decrypt(password, encrypted_wallet_content)) account = engine.instance.account.get_account(wallet.address, apply_tx_pool=True) return generate_json_response({ "name": wallet.name, "pubkey": wallet.get_pubkey_str(), "privkey": wallet.get_privkey_str(), "address": wallet.address, "balance": account['amount'] }) except: return generate_json_response("Password incorrect") else: return generate_json_response("Error occurred")
def history(): from halocoin.model.wallet import Wallet address = request.values.get('address', None) if address is None: default_wallet = engine.instance.clientdb.get_default_wallet() if default_wallet is not None: wallet_name = default_wallet['wallet_name'] password = default_wallet['password'] encrypted_wallet_content = engine.instance.clientdb.get_wallet( wallet_name) wallet = Wallet.from_string( tools.decrypt(password, encrypted_wallet_content)) address = wallet.address account = engine.instance.statedb.get_account(wallet.address) txs = {"send": [], "recv": []} for block_index in reversed(account['tx_blocks']): block = engine.instance.blockchain.get_block(block_index) for tx in block['txs']: if tx['type'] == 'mint': continue tx['block'] = block_index owner = tools.tx_owner_address(tx) if owner == address: txs['send'].append(tx) elif tx['type'] == 'spend' and tx['to'] == address: txs['recv'].append(tx) return generate_json_response(txs)
def new_wallet(): from halocoin.model.wallet import Wallet wallet_name = request.values.get('wallet_name', None) pw = request.values.get('password', None) set_default = request.values.get('set_default', None) wallet = Wallet(wallet_name) success = engine.instance.clientdb.new_wallet(pw, wallet) if set_default: engine.instance.clientdb.set_default_wallet(wallet_name, pw) return generate_json_response({"name": wallet_name, "success": success})
def remove_wallet(): from halocoin.model.wallet import Wallet wallet_name = request.values.get('wallet_name', None) password = request.values.get('password', None) default_wallet = engine.instance.account.get_default_wallet() if default_wallet is not None and default_wallet[ 'wallet_name'] == wallet_name: return generate_json_response({ 'success': False, 'error': 'Cannot remove default wallet. First remove its default state!' }) encrypted_wallet_content = engine.instance.account.get_wallet(wallet_name) if encrypted_wallet_content is not None: try: Wallet.from_string( tools.decrypt(password, encrypted_wallet_content)) engine.instance.account.remove_wallet(wallet_name) return generate_json_response({ "success": True, "message": "Successfully removed wallet" }) except: return generate_json_response({ "success": False, "error": "Password incorrect" }) else: return generate_json_response({ "success": False, "error": "Unidentified error occurred!" })
def balance(): from halocoin.model.wallet import Wallet address = request.values.get('address', None) if address is None: default_wallet = engine.instance.account.get_default_wallet() if default_wallet is not None: wallet_name = default_wallet['wallet_name'] password = default_wallet['password'] encrypted_wallet_content = engine.instance.account.get_wallet( wallet_name) wallet = Wallet.from_string( tools.decrypt(password, encrypted_wallet_content)) address = wallet.address account = engine.instance.account.get_account(address, apply_tx_pool=True) return generate_json_response({'balance': account['amount']})
def set_default_wallet(self, wallet_name, password): try: from halocoin.model.wallet import Wallet encrypted_wallet_content = self.get_wallet(wallet_name) wallet = Wallet.from_string( tools.decrypt(password, encrypted_wallet_content)) if wallet.name == wallet_name: self.db.put('default_wallet', { "wallet_name": wallet_name, "password": password }) return True else: return False except Exception as e: tools.log(e) return False
def send(): from halocoin.model.wallet import Wallet amount = int(request.values.get('amount', 0)) address = request.values.get('address', None) message = request.values.get('message', '') wallet_name = request.values.get('wallet_name', None) password = request.values.get('password', None) if wallet_name is None: default_wallet = engine.instance.account.get_default_wallet() if default_wallet is not None: wallet_name = default_wallet['wallet_name'] response = {"success": False} if amount <= 0: response['error'] = "Amount cannot be lower than or equalto 0" return generate_json_response(response) elif address is None: response[ 'error'] = "You need to specify a receiving address for transaction" return generate_json_response(response) elif wallet_name is None: response[ 'error'] = "Wallet name is not given and there is no default wallet" return generate_json_response(response) elif password is None: response['error'] = "Password missing!" return generate_json_response(response) tx = { 'type': 'spend', 'amount': int(amount), 'to': address, 'message': message } encrypted_wallet_content = engine.instance.account.get_wallet(wallet_name) if encrypted_wallet_content is not None: try: wallet = Wallet.from_string( tools.decrypt(password, encrypted_wallet_content)) except: response['error'] = "Wallet password incorrect" return generate_json_response(response) else: response['error'] = "Error occurred" return generate_json_response(response) if 'count' not in tx: try: tx['count'] = engine.instance.account.known_tx_count( wallet.address) except: tx['count'] = 0 if 'pubkeys' not in tx: tx['pubkeys'] = [wallet.get_pubkey_str()] # We use pubkey as string if 'signatures' not in tx: tx['signatures'] = [tools.sign(tools.det_hash(tx), wallet.privkey)] engine.instance.blockchain.tx_queue.put(tx) response["success"] = True response["message"] = 'Tx amount:{} to:{} added to the pool'.format( tx['amount'], tx['to']) return generate_json_response(response)