def send_response(self, *, id: Optional[JsonRpcId] = None, result: Optional[JsonValue] = None, error: Optional[JsonRpcError] = None) -> None: response: Dict[str, JsonValue] = { 'id': id, 'error': None, 'result': None, } if error is not None: assert result is None, 'Can\'t have both an error and a result' response['error'] = error.to_dict() if error is not None else None else: assert id is not None, '`id` is required for a success response' response['result'] = result self.sendMessage(json_dumpb(response)) if error is not None and error.fatal: self.sendClose()
def render_POST(self, request): """ POST request for /submit_block/ """ request.setHeader(b'content-type', b'application/json; charset=utf-8') set_cors(request, 'GET') data = json_loadb(request.content.read()) tx = tx_or_block_from_bytes(bytes.fromhex(data['hexdata']), storage=self.manager.tx_storage) if not tx.is_block: self.log.debug('expected Block, received Transaction', data=data) raise APIError('Not a block') if not self.manager.can_start_mining(): self.log.debug('cannot propagate Block, node syncing', data=data) raise APIError('Node syncing') res = self.manager.submit_block(tx) return json_dumpb({'result': res})
def render_GET(self, request): """ Get request /validate_address/<address> that returns a script if address is valid. """ request.setHeader(b'content-type', b'application/json; charset=utf-8') set_cors(request, 'GET') try: base_script = create_base_script(self.address) except Exception as e: ret = { 'valid': False, 'error': type(e).__name__, 'msg': str(e), } else: ret = { 'valid': True, 'script': base64.b64encode(base_script.get_script()).decode('ascii'), 'address': base_script.get_address(), 'type': base_script.get_type().lower(), } return json_dumpb(ret)
def render_GET(self, request: Request) -> bytes: """ GET request for /push_tx/ Expects 'hex_tx' as args parameter that is the hex representation of the whole tx :rtype: string (json) This resource will be deprecated soon """ request.setHeader(b'content-type', b'application/json; charset=utf-8') set_cors(request, 'GET') parsed = parse_get_arguments(request.args, ARGS) if not parsed['success']: data = { 'success': False, 'message': 'Missing hexadecimal data', 'can_force': False } return json_dumpb(data) data = parsed['args'] data['force'] = b'force' in request.args and request.args[b'force'][ 0].decode('utf-8') == 'true' return self.handle_push_tx(data)
def render_POST(self, request: Request) -> bytes: """ POST request for /push_tx/ """ request.setHeader(b'content-type', b'application/json; charset=utf-8') set_cors(request, 'POST') error_ret = json_dumpb({ 'success': False, 'message': 'Missing hexadecimal data', 'can_force': False }) body_content = request.content.read() if not body_content: return error_ret data = json_loadb(body_content) # Need to do that because json_loadb returns an object, which is not compatible with Dict[str, Any] data = cast(Dict[str, Any], data) if 'hex_tx' not in data: return error_ret return self.handle_push_tx(data)
def _to_value_info(self, info: _InfoDict) -> bytes: return json_dumpb(info)
def _meta_to_bytes(self, meta: 'TransactionMetadata') -> bytes: return json_dumpb(meta.to_json())