Example #1
0
 def verify_token(self, token: str) -> bool:
     raw_token = b58.a2b_base58(token)
     hashed_token = sha256d(raw_token)
     if self.get(hashed_token):
         return True
     else:
         return False
Example #2
0
async def register_token(req: Request, resp: Response) -> None:
    request: Dict = await req.media()
    token: str = request.get("token")
    try:
        exist = token_db.verify_token(token)
    except Exception:
        exist = False

    raw_token = b58.a2b_base58(token)
    hashed_token = sha256d(raw_token)
    try:
        used = bool(tx_db.get(hashed_token))
    except Exception:
        used = False

    if not exist:
        result = {
            "code": "Failed",
            "error": "Token is not registered or is invalid."
        }
    elif used:
        result = {"code": "Failed", "error": "Token is already used."}
    else:
        want_currency: str = request.get("wantCurrency")
        want_amount: int = request.get("wantAmount")
        send_currency: str = request.get("sendCurrency")
        send_amount: int = request.get("sendAmount")
        receive_address: str = request.get("receiveAddress")
        try:
            if want_amount.count(".") or send_amount.count("."):
                raise  # amount type isn't int...
            want_amount = int(want_amount)
            send_amount = int(send_amount)
        except Exception:
            pass

        if not (isinstance(want_currency, str) and isinstance(
                want_amount, int) and isinstance(send_currency, str)
                and isinstance(send_amount, int)
                and isinstance(receive_address, str)):
            result = {"code": "Failed", "error": "Request data is invalid."}
        else:
            data = TxDBData(i_currency=want_currency,
                            i_receive_amount=send_amount,
                            p_currency=send_currency,
                            p_receive_amount=want_amount,
                            p_addr=receive_address)
            try:
                tx_db.put(hashed_token, data)
                result = {"code": "Success"}
            except Exception as e:
                result = {"code": "Failed", "error": str(e)}

    resp.media = result
Example #3
0
def parse_arg(arg, force_b58):
    is_hex_input = False
    blob = None
    if not force_b58:
        try:
            blob = h2b(arg)
            is_hex_input = True
        except Exception:
            pass
    if blob is None:
        try:
            blob = a2b_base58(arg)
        except KeyError:
            pass
    if blob is None:
        raise argparse.ArgumentTypeError("can't parse %s" % arg)
    return blob, is_hex_input
Example #4
0
def parse_arg(arg, force_b58):
    is_hex_input = False
    blob = None
    if not force_b58:
        try:
            blob = h2b(arg)
            is_hex_input = True
        except Exception:
            pass
    if blob is None:
        try:
            blob = a2b_base58(arg)
        except KeyError:
            pass
    if blob is None:
        raise argparse.ArgumentTypeError("can't parse %s" % arg)
    return blob, is_hex_input
Example #5
0
 def do_test(as_text, as_bin):
     self.assertEqual(as_bin, a2b_base58(as_text))
     self.assertEqual(as_text, b2a_base58(as_bin))
Example #6
0
 def do_test(as_text, as_bin):
     self.assertEqual(as_bin, a2b_base58(as_text))
     self.assertEqual(as_text, b2a_base58(as_bin))