def pl_bri(self, address, value): if int(value) not in range(0, 32): raise ValueError("Dim value must be in the range(0, 32)") validate_address(address) cmd = "pl %s bright %s" % (address, value) self.send_event('connex', "EV_SEND_DATA", data=cmd)
def pl_bri(address, value): if int(value) not in range(0, 32): raise ValueError("Dim value must be in the range(0, 32)") validate_address(address) cmd = "pl %s bright %s\n" % (address, value) netcat(settings.MOCHAD_HOST, settings.MOCHAD_PORT, cmd)
def pl_switch(self, address, value): if value not in ["on", "off"]: raise ValueError("Switch value must be 'on' or 'off'") validate_address(address) cmd = b"pl %s %s\n" % (address, value) self.send_event(self.connex, "EV_SEND_DATA", data=cmd)
def pl_switch(address, value): if value not in ["on", "off"]: raise ValueError("Switch value must be 'on' or 'off'") validate_address(address) cmd = b"pl %s %s\n" % (address, value) netcat(settings.MOCHAD_HOST, settings.MOCHAD_PORT, cmd)
def _do_sign_transaction(self, acct_addr, unsigned_tx_str): ''' Make sure account is in the keystore and unlocked, then sign the tx unsigned_tx_str is a hex-encoded string. probably starts with '0x' returns: (signed_tx, result_code, msg) ''' priv_key = None errmsg = None errcode = EthSigDelegate.SUCCESS v_addr = utils.validate_address(acct_addr) if not v_addr: errmsg = 'Invalid account address: {0}'.format(acct_addr) errcode = EthSigDelegate.INVALID_ADDR if not errmsg: acct_data = self._accounts.get(v_addr) if not acct_data: errmsg = 'Account: {0} not in keystore'.format(v_addr) errcode = EthSigDelegate.UNKNOWN_ADDR if not errmsg: priv_key = self._cached_pks.get(v_addr) if not priv_key: errmsg = 'Account locked: {0}'.format(v_addr) errcode = EthSigDelegate.ADDR_LOCKED signed_tx = None if priv_key: tx = TxData.createFromTxData(unsigned_tx_str) rawhash = sha3(rlp.encode(tx, UnsignedTxData)) v, r, s = ecdsa_sign_raw(rawhash, priv_key) signed_tx = tx.getSignedTxData(v, r, s) return (signed_tx, errcode, errmsg)
def _do_sign_data(self, acct_addr, hash_str): ''' Make sure account is in the keystore and unlocked, then sign the hash. Returns the sig as a hex string. ''' priv_key = None errmsg = None errcode = EthSigDelegate.SUCCESS v_addr = utils.validate_address(acct_addr) if not v_addr: errmsg = 'Invalid account address: {0}'.format(acct_addr) errcode = EthSigDelegate.INVALID_ADDR if not errmsg: acct_data = self._accounts.get(v_addr) if not acct_data: errmsg = 'Account: {0} not in keystore'.format(v_addr) errcode = EthSigDelegate.UNKNOWN_ADDR if not errmsg: priv_key = self._cached_pks.get(v_addr) if not priv_key: errmsg = 'Account locked: {0}'.format(v_addr) errcode = EthSigDelegate.ADDR_LOCKED sig_str = None if priv_key: # It's a hex-encoded string starting with '0x' data_hash = hash_str[2:].decode('hex') v, r, s = ecdsa_sign_raw(data_hash, priv_key) sig_str = utils.vrs_to_sig(v, r, s) return ( sig_str, errcode, errmsg)
def _do_sign_transaction(self, acct_addr, unsigned_tx_str): ''' Make sure account is in the keystore and unlocked, then sign the tx unsigned_tx_str is a hex-encoded string. probably starts with '0x' returns: (signed_tx, result_code, msg) ''' priv_key = None errmsg = None errcode = EthSigDelegate.SUCCESS v_addr = utils.validate_address(acct_addr) if not v_addr: errmsg = 'Invalid account address: {0}'.format(acct_addr) errcode = EthSigDelegate.INVALID_ADDR if not errmsg: acct_data = self._accounts.get(v_addr) if not acct_data: errmsg = 'Account: {0} not in keystore'.format(v_addr) errcode = EthSigDelegate.UNKNOWN_ADDR if not errmsg: priv_key = self._cached_pks.get(v_addr) if not priv_key: errmsg = 'Account locked: {0}'.format(v_addr) errcode = EthSigDelegate.ADDR_LOCKED signed_tx = None if priv_key: tx = TxData.createFromTxData(unsigned_tx_str) rawhash = sha3(rlp.encode(tx, UnsignedTxData)) v, r, s = ecdsa_sign_raw(rawhash, priv_key) signed_tx = tx.getSignedTxData(v,r,s) return (signed_tx, errcode, errmsg)
def unlock_account(self, acct_addr, password): ''' look for acct addr and recover the private key and cache it. returns rrMsg ''' priv_key = None errmsg = None v_addr = utils.validate_address(acct_addr) if not v_addr: errmsg = 'Invalid account address: {0}'.format(acct_addr) if not errmsg: acct_data = self._accounts.get(v_addr) if not acct_data: errmsg = 'Account: {0} not in keystore'.format(v_addr) if not errmsg: try: priv_key = keys.decode_keystore_json(acct_data, password) except keys.PasswordError as ex: errmsg = 'Password failed for account: {0}'.format(v_addr) except keys.HashNotSupportedError as ex: errmsg = 'Keystore: {0}'.format(str(ex)) except keys.EncryptionNotSupportedError as ex: errmsg = 'Keystore: {0}'.format(str(ex)) except Exception as ex: errmsg = 'Keystore Exception: {0}'.format(ex.text()) if priv_key: self._cached_pks[v_addr] = priv_key return errmsg
def _do_sign_data(self, acct_addr, hash_str): ''' Make sure account is in the keystore and unlocked, then sign the hash. Returns the sig as a hex string. ''' priv_key = None errmsg = None errcode = EthSigDelegate.SUCCESS v_addr = utils.validate_address(acct_addr) if not v_addr: errmsg = 'Invalid account address: {0}'.format(acct_addr) errcode = EthSigDelegate.INVALID_ADDR if not errmsg: acct_data = self._accounts.get(v_addr) if not acct_data: errmsg = 'Account: {0} not in keystore'.format(v_addr) errcode = EthSigDelegate.UNKNOWN_ADDR if not errmsg: priv_key = self._cached_pks.get(v_addr) if not priv_key: errmsg = 'Account locked: {0}'.format(v_addr) errcode = EthSigDelegate.ADDR_LOCKED sig_str = None if priv_key: # It's a hex-encoded string starting with '0x' data_hash = hash_str[2:].decode('hex') v, r, s = ecdsa_sign_raw(data_hash, priv_key) sig_str = utils.vrs_to_sig(v, r, s) return (sig_str, errcode, errmsg)
def _do_sign_transaction(self, acct_addr, unsigned_tx_str): ''' Queries a running node. ''' errmsg = None errcode = EthSigDelegate.SUCCESS all_accts = self.list_accounts() signed_tx = None v_addr = utils.validate_address(acct_addr) if not v_addr: errmsg = 'Invalid account address: {0}'.format(acct_addr) errcode = EthSigDelegate.INVALID_ADDR if not errmsg: if not v_addr in all_accts: errmsg = 'Account: {0} not in keystore'.format(v_addr) errcode = EthSigDelegate.UNKNOWN_ADDR if not errmsg: tx = TxData.createFromTxData(unsigned_tx_str) rawhash = sha3(rlp.encode(tx, UnsignedTxData)) rh_str = '0x{0}'.format(rawhash.encode('hex')) sig = self.eth.eth_sign(v_addr, rh_str) v, r, s = utils.sig_to_vrs(sig) signed_tx = tx.getSignedTxData(v, r, s) return (signed_tx, errcode, errmsg)
def _do_sign_transaction(self, acct_addr, unsigned_tx_str): ''' Queries a running node. ''' errmsg = None errcode = EthSigDelegate.SUCCESS all_accts = self.list_accounts() signed_tx = None v_addr = utils.validate_address(acct_addr) if not v_addr: errmsg = 'Invalid account address: {0}'.format(acct_addr) errcode = EthSigDelegate.INVALID_ADDR if not errmsg: if not v_addr in all_accts: errmsg = 'Account: {0} not in keystore'.format(v_addr) errcode = EthSigDelegate.UNKNOWN_ADDR if not errmsg: tx = TxData.createFromTxData(unsigned_tx_str) rawhash = sha3(rlp.encode(tx, UnsignedTxData)) rh_str = '0x{0}'.format(rawhash.encode('hex')) sig = self.eth.eth_sign(v_addr, rh_str) v,r,s = utils.sig_to_vrs(sig) signed_tx = tx.getSignedTxData(v,r,s) return (signed_tx, errcode, errmsg)
def __init__(self, server_address, version, socket_type, HandlerClass, active=False): """ :param server_address: tuple of host address and port :param version: IP version (4 or 6) :param protocol: Layer 4 protocol (TCP or UDP) :param HandlerClass: Class to handle, send and receive data :param active: Boolean to start server """ self.server_address = server_address self._version = version try: validate_address(self.server_address[0], self.server_address[1], version) except (ValueError, TypeError): raise self.__shutdown_flag = False self.__continue_flag = True self.HandlerClass = HandlerClass self.address_family = getattr(socket, Version(version).name) self.socket_type = socket_type self.socket = socket.socket(self.address_family, self.socket_type) if active: try: self.start() except: self.stop() raise
def new_account(self, password): ''' Create a new account. Returns address ''' priv_key = keys.new_priv_key() # try scrypt first try: acct_data = keys.make_keystore_json(priv_key, password, kdf='scrypt') except: acct_data = keys.make_keystore_json(priv_key, password, kdf='pbkdf2') addrStr = utils.validate_address(acct_data.get('address')) self._accounts[addrStr] = acct_data self.write_account(addrStr) return addrStr
def load_accounts(self): ''' Read in json data for all accounts in folder. ''' if not self._directory_path: raise( RuntimeError("No keystore path specified. Can only use unlocked accounts.")) # Ensure directory is there try: os.makedirs(self._directory_path) except OSError: if not os.path.isdir(self._directory_path): raise filePaths = os.listdir(self._directory_path) for fPath in filePaths: fullPath = os.path.join(self._directory_path, fPath) with open(fullPath) as f: acctData = json.load(f) if keys.check_keystore_json(acctData): addrStr = utils.validate_address(acctData.get('address')) self._accounts[addrStr] = acctData
def _do_sign_data(self, acct_addr, data): ''' ? ''' errmsg = None errcode = EthSigDelegate.SUCCESS all_accts = self.list_accounts() v_addr = utils.validate_address(acct_addr) if not v_addr: errmsg = 'Invalid account address: {0}'.format(acct_addr) errcode = EthSigDelegate.INVALID_ADDR if not errmsg: if not v_addr in all_accts: errmsg = 'Account: {0} not in keystore'.format(v_addr) errcode = EthSigDelegate.UNKNOWN_ADDR signature = None if not errmsg: signature = self.eth.eth_sign(v_addr, data) return (signature, errcode, errmsg)
def load_accounts(self): ''' Read in json data for all accounts in folder. ''' if not self._directory_path: raise (RuntimeError( "No keystore path specified. Can only use unlocked accounts.")) # Ensure directory is there try: os.makedirs(self._directory_path) except OSError: if not os.path.isdir(self._directory_path): raise filePaths = os.listdir(self._directory_path) for fPath in filePaths: fullPath = os.path.join(self._directory_path, fPath) with open(fullPath) as f: acctData = json.load(f) if keys.check_keystore_json(acctData): addrStr = utils.validate_address(acctData.get('address')) self._accounts[addrStr] = acctData
async def claim(request): # handle faucet claim data = await request.post() if config.debug: print(request.headers) request_ip = '' if config.x_real_ip: request_ip = request.headers.get('X-Real-IP', '') if request_ip == '': request_ip = request.remote # not secure! cookie = request.cookies.get(config.cookie_name, '') if config.debug: print(request_ip, data, cookie) claim_address = data['_address'] recaptcha = data.get('_recaptcha', '') # anti-DoS / sanity check if len(claim_address) > config.maxAddressLen: print( f"Invalid request from IP {request_ip}: Address field too long (DoS?)", flush=True) return web.json_response({ 'status': 'Error', 'msg': 'Address is invalid' }) if claim_address == config.faucet_address: print( f"Invalid request from IP {request_ip}: Address == faucet_address (DoS?)", flush=True) return web.json_response({ 'status': 'Error', 'msg': 'Address is invalid' }) if len(recaptcha) > 1024: # should be enough?! print( f"Invalid request from IP {request_ip}: Recaptcha field too long (DoS?)", flush=True) return web.json_response({'status': 'Error', 'msg': 'Recaptcha error'}) # check address is correct format if not validate_address(claim_address): print(f"Invalid request from IP {request_ip}: Address invalid", flush=True) return web.json_response({ 'status': 'Error', 'msg': f'Address {claim_address} is invalid' }) # limit claims by address if not check_claims("address", claim_address): print( f"Too many claims for address {claim_address} from IP {request_ip}", flush=True) return web.json_response({ 'status': 'Error', 'msg': 'Maximum claims exceeded. Please try again later' }) # validate cookie if not validate_cookie(cookie): print(f"Invalid cookie from IP {request_ip}", flush=True) return web.json_response({ 'status': 'Error', 'msg': 'Cookie validation error. Javascript and cookies must be enabled for this faucet to work' }) # limit claims by cookie if not check_claims("cookie", cookie): print(f"Too many claims for cookie from IP {request_ip}", flush=True) return web.json_response({ 'status': 'Error', 'msg': 'Maximum claims exceeded. Please try again later' }) # limit claims by ip if not check_ip(request_ip): print(f"Too many claims from IP {request_ip}", flush=True) return web.json_response({ 'status': 'Error', 'msg': 'Maximum claims exceeded. Please try again later' }) # validate reCAPTCHA if not validate_recaptcha(recaptcha): print(f"Recaptcha validation failed for IP {request_ip}", flush=True) return web.json_response({ 'status': 'Error', 'msg': 'reCAPTCHA validation failed' }) try: rvn = RavenProxy(service_url=config.coin_daemon_url, datadir=config.args.datadir) txid = b2lx(rvn.sendtoaddress(claim_address, config.claim_amount)) update_claimtime(request_ip, claim_address, cookie) print( f"Sent {config.claim_amount/COIN} {config.denom} to address {claim_address} for IP {request_ip}", flush=True) return web.json_response({ 'status': 'Success', 'msg': f'Sent {config.claim_amount/COIN} {config.denom} to {claim_address},<br> \ txid <a href="https://testnet.ravencoin.network/tx/{txid}">{txid}</s>' }) except Exception as e: if config.debug: print(e) print( f"Error sending {config.claim_amount/COIN} {config.denom} to address {claim_address} for IP {request_ip}", flush=True) return web.json_response({ 'status': 'Error', 'msg': 'Error sending coins. Please try again later' })