Exemple #1
0
def parse_URI(uri):
    import bitcoin
    from bitcoin import COIN

    if ':' not in uri:
        assert bitcoin.is_address(uri)
        return {'address': uri}

    u = urlparse.urlparse(uri)
    assert u.scheme == 'verge'

    address = u.path

    # python for android fails to parse query
    if address.find('?') > 0:
        address, query = u.path.split('?')
        pq = urlparse.parse_qs(query)
    else:
        pq = urlparse.parse_qs(u.query)

    for k, v in pq.items():
        if len(v) != 1:
            raise Exception('Duplicate Key', k)

    out = {k: v[0] for k, v in pq.items()}
    if address:
        assert bitcoin.is_address(address)
        out['address'] = address
    if 'amount' in out:
        am = out['amount']
        m = re.match('([0-9\.]+)X([0-9])', am)
        if m:
            k = int(m.group(2)) - 8
            amount = Decimal(m.group(1)) * pow(Decimal(10), k)
        else:
            amount = Decimal(am) * COIN
        out['amount'] = int(amount)
    if 'message' in out:
        out['message'] = out['message'].decode('utf8')
        out['memo'] = out['message']
    if 'time' in out:
        out['time'] = int(out['time'])
    if 'exp' in out:
        out['exp'] = int(out['exp'])
    if 'sig' in out:
        out['sig'] = bitcoin.base_decode(out['sig'], None,
                                         base=58).encode('hex')

    return out
Exemple #2
0
def parse_URI(uri):
    import bitcoin
    from bitcoin import COIN

    if ':' not in uri:
        assert bitcoin.is_address(uri)
        return {'address': uri}

    u = urlparse.urlparse(uri)
    assert u.scheme == 'bitcoin'

    address = u.path

    # python for android fails to parse query
    if address.find('?') > 0:
        address, query = u.path.split('?')
        pq = urlparse.parse_qs(query)
    else:
        pq = urlparse.parse_qs(u.query)

    for k, v in pq.items():
        if len(v)!=1:
            raise Exception('Duplicate Key', k)

    out = {k: v[0] for k, v in pq.items()}
    if address:
        assert bitcoin.is_address(address)
        out['address'] = address
    if 'amount' in out:
        am = out['amount']
        m = re.match('([0-9\.]+)X([0-9])', am)
        if m:
            k = int(m.group(2)) - 8
            amount = Decimal(m.group(1)) * pow(  Decimal(10) , k)
        else:
            amount = Decimal(am) * COIN
        out['amount'] = int(amount)
    if 'message' in out:
        out['message'] = out['message'].decode('utf8')
        out['memo'] = out['message']
    if 'time' in out:
        out['time'] = int(out['time'])
    if 'exp' in out:
        out['exp'] = int(out['exp'])
    if 'sig' in out:
        out['sig'] = bitcoin.base_decode(out['sig'], None, base=58).encode('hex')

    return out
Exemple #3
0
def parse_URI(uri, on_pr=None):
    import bitcoin
    from bitcoin import COIN

    if ':' not in uri:
        if not bitcoin.is_address(uri):
            raise BaseException("Not a bitcoin address")
        return {'address': uri}

    u = urlparse.urlparse(uri)
    if u.scheme != 'bitcoin':
        raise BaseException("Not a bitcoin URI")
    address = u.path

    # python for android fails to parse query
    if address.find('?') > 0:
        address, query = u.path.split('?')
        pq = urlparse.parse_qs(query)
    else:
        pq = urlparse.parse_qs(u.query)

    for k, v in pq.items():
        if len(v)!=1:
            raise Exception('Duplicate Key', k)

    out = {k: v[0] for k, v in pq.items()}
    if address:
        if not bitcoin.is_address(address):
            raise BaseException("Invalid bitcoin address:" + address)
        out['address'] = address
    if 'amount' in out:
        am = out['amount']
        m = re.match('([0-9\.]+)X([0-9])', am)
        if m:
            k = int(m.group(2)) - 8
            amount = Decimal(m.group(1)) * pow(  Decimal(10) , k)
        else:
            amount = Decimal(am) * COIN
        out['amount'] = int(amount)
    if 'message' in out:
        out['message'] = out['message'].decode('utf8')
        out['memo'] = out['message']
    if 'time' in out:
        out['time'] = int(out['time'])
    if 'exp' in out:
        out['exp'] = int(out['exp'])
    if 'sig' in out:
        out['sig'] = bitcoin.base_decode(out['sig'], None, base=58).encode('hex')

    r = out.get('r')
    sig = out.get('sig')
    name = out.get('name')
    if on_pr and (r or (name and sig)):
        def get_payment_request_thread():
            import paymentrequest as pr
            if name and sig:
                s = pr.serialize_request(out).SerializeToString()
                request = pr.PaymentRequest(s)
            else:
                request = pr.get_payment_request(r)
            on_pr(request)
        t = threading.Thread(target=get_payment_request_thread)
        t.setDaemon(True)
        t.start()

    return out
Exemple #4
0
def parse_URI(uri, on_pr=None):
    import bitcoin
    from bitcoin import COIN

    if ":" not in uri:
        assert bitcoin.is_address(uri)
        return {"address": uri}

    u = urlparse.urlparse(uri)
    assert u.scheme == "bitcoin"

    address = u.path

    # python for android fails to parse query
    if address.find("?") > 0:
        address, query = u.path.split("?")
        pq = urlparse.parse_qs(query)
    else:
        pq = urlparse.parse_qs(u.query)

    for k, v in pq.items():
        if len(v) != 1:
            raise Exception("Duplicate Key", k)

    out = {k: v[0] for k, v in pq.items()}
    if address:
        assert bitcoin.is_address(address)
        out["address"] = address
    if "amount" in out:
        am = out["amount"]
        m = re.match("([0-9\.]+)X([0-9])", am)
        if m:
            k = int(m.group(2)) - 8
            amount = Decimal(m.group(1)) * pow(Decimal(10), k)
        else:
            amount = Decimal(am) * COIN
        out["amount"] = int(amount)
    if "message" in out:
        out["message"] = out["message"].decode("utf8")
        out["memo"] = out["message"]
    if "time" in out:
        out["time"] = int(out["time"])
    if "exp" in out:
        out["exp"] = int(out["exp"])
    if "sig" in out:
        out["sig"] = bitcoin.base_decode(out["sig"], None, base=58).encode("hex")

    r = out.get("r")
    sig = out.get("sig")
    name = out.get("name")
    if r or (name and sig):

        def get_payment_request_thread():
            import paymentrequest as pr

            if name and sig:
                s = pr.serialize_request(out).SerializeToString()
                request = pr.PaymentRequest(s)
            else:
                request = pr.get_payment_request(r)
            on_pr(request)

        t = threading.Thread(target=get_payment_request_thread)
        t.setDaemon(True)
        t.start()

    return out
Exemple #5
0
def parse_URI(uri, on_pr=None):
    import bitcoin
    from bitcoin import COIN

    if ':' not in uri:
        if not bitcoin.is_address(uri):
            raise BaseException("Not a bitcoin address")
        return {'address': uri}

    u = urlparse.urlparse(uri)
    if u.scheme != 'bitcoin':
        raise BaseException("Not a bitcoin URI")
    address = u.path

    # python for android fails to parse query
    if address.find('?') > 0:
        address, query = u.path.split('?')
        pq = urlparse.parse_qs(query)
    else:
        pq = urlparse.parse_qs(u.query)

    for k, v in pq.items():
        if len(v)!=1:
            raise Exception('Duplicate Key', k)

    out = {k: v[0] for k, v in pq.items()}
    if address:
        if not bitcoin.is_address(address):
            raise BaseException("Invalid bitcoin address:" + address)
        out['address'] = address
    if 'amount' in out:
        am = out['amount']
        m = re.match('([0-9\.]+)X([0-9])', am)
        if m:
            k = int(m.group(2)) - 8
            amount = Decimal(m.group(1)) * pow(  Decimal(10) , k)
        else:
            amount = Decimal(am) * COIN
        out['amount'] = int(amount)
    if 'message' in out:
        out['message'] = out['message'].decode('utf8')
        out['memo'] = out['message']
    if 'time' in out:
        out['time'] = int(out['time'])
    if 'exp' in out:
        out['exp'] = int(out['exp'])
    if 'sig' in out:
        out['sig'] = bitcoin.base_decode(out['sig'], None, base=58).encode('hex')

    r = out.get('r')
    sig = out.get('sig')
    name = out.get('name')
    if on_pr and (r or (name and sig)):
        def get_payment_request_thread():
            import paymentrequest as pr
            if name and sig:
                s = pr.serialize_request(out).SerializeToString()
                request = pr.PaymentRequest(s)
            else:
                request = pr.get_payment_request(r)
            on_pr(request)
        t = threading.Thread(target=get_payment_request_thread)
        t.setDaemon(True)
        t.start()

    return out