コード例 #1
0
    def do_test_payment(self, wc1, wc2, amt=1.1):
        wallet_structures = [self.wallet_structure] * 2
        wallet_cls = (wc1, wc2)
        self.wallet_services = []
        self.wallet_services.append(make_wallets_to_list(make_wallets(
            1, wallet_structures=[wallet_structures[0]],
            mean_amt=self.mean_amt, wallet_cls=wallet_cls[0]))[0])
        self.wallet_services.append(make_wallets_to_list(make_wallets(
                1, wallet_structures=[wallet_structures[1]],
                mean_amt=self.mean_amt, wallet_cls=wallet_cls[1]))[0])
        jm_single().bc_interface.tickchain()
        sync_wallets(self.wallet_services)

        # For accounting purposes, record the balances
        # at the start.
        self.rsb = getbals(self.wallet_services[0], 0)
        self.ssb = getbals(self.wallet_services[1], 0)

        self.cj_amount = int(amt * 10**8)
        def cbStopListening():
            return self.port.stopListening()
        b78rm = JMBIP78ReceiverManager(self.wallet_services[0], 0,
                                       self.cj_amount, 47083)
        resource = DummyBIP78ReceiverResource(jmprint, cbStopListening, b78rm)
        self.site = Site(resource)
        self.site.displayTracebacks = False
        # NB The connectivity aspects of the onion-based BIP78 setup
        # are time heavy. This server is TCP only.
        self.port = reactor.listenTCP(47083, self.site)
        self.addCleanup(cbStopListening)

        # setup of spender
        bip78_btc_amount = amount_to_btc(amount_to_sat(self.cj_amount))
        bip78_uri = encode_bip21_uri(str(b78rm.receiving_address),
                                {"amount": bip78_btc_amount,
                                 "pj": b"http://127.0.0.1:47083"},
                                safe=":/")
        self.manager = parse_payjoin_setup(bip78_uri, self.wallet_services[1], 0)
        self.manager.mode = "testing"
        success, msg = make_payment_psbt(self.manager)
        assert success, msg
        params = make_payjoin_request_params(self.manager)
        # avoiding backend daemon (testing only jmclient code here),
        # we send the http request manually:
        serv = b"http://127.0.0.1:47083"
        agent = get_nontor_agent()
        body = BytesProducer(self.manager.initial_psbt.to_base64().encode("utf-8"))
        url_parts = list(wrapped_urlparse(serv))
        url_parts[4] = urlencode(params).encode("utf-8")
        destination_url = urlparse.urlunparse(url_parts)
        d = agent.request(b"POST", destination_url,
                          Headers({"Content-Type": ["text/plain"]}),
                          bodyProducer=body)
        d.addCallback(bip78_receiver_response, self.manager)
        return d
コード例 #2
0
 def bip21_uri_from_onion_hostname(self, host):
     """ Encoding the BIP21 URI according to BIP78 specifications,
     and specifically only supporting a hidden service endpoint.
     Note: we hardcode http; no support for TLS over HS.
     Second, note we convert the amount-in-sats self.amount
     to BTC denomination as expected by BIP21.
     """
     port_str = ":" + str(self.port) if self.port != 80 else ""
     full_pj_string = "http://" + host + port_str
     bip78_btc_amount = btc.amount_to_btc(btc.amount_to_sat(self.amount))
     # "safe" option is required to encode url in url unmolested:
     bip21_uri = btc.encode_bip21_uri(str(self.receiving_address), {
         "amount": bip78_btc_amount,
         "pj": full_pj_string.encode("utf-8")
     },
                                      safe=":/")
     self.info_callback("Your hidden service is available. Please\n"
                        "now pass this URI string to the sender to\n"
                        "effect the payjoin payment:")
     self.uri_created_callback(bip21_uri)
     if self.mode == "command-line":
         self.info_callback("Keep this process running until the payment "
                            "is received.")
コード例 #3
0
 def inform_user_details(self):
     self.user_info("Your receiving address is: " + self.destination_addr)
     self.user_info("You will receive amount: " +
                    str(self.receiving_amount) + " satoshis.")
     self.user_info("The sender also needs to know your ephemeral "
                    "nickname: " + jm_single().nickname)
     receive_uri = btc.encode_bip21_uri(
         self.destination_addr, {
             'amount': btc.sat_to_btc(self.receiving_amount),
             'jmnick': jm_single().nickname
         })
     self.user_info("Receive URI: " + receive_uri)
     self.user_info(
         "This information has also been stored in a file payjoin.txt;"
         " send it to your counterparty when you are ready.")
     with open("payjoin.txt", "w") as f:
         f.write("Payjoin transfer details:\n\n")
         f.write("Receive URI: " + receive_uri + "\n")
         f.write("Address: " + self.destination_addr + "\n")
         f.write("Amount (in sats): " + str(self.receiving_amount) + "\n")
         f.write("Receiver nick: " + jm_single().nickname + "\n")
     if not self.user_check("Enter 'y' to wait for the payment:"):
         sys.exit(EXIT_SUCCESS)
コード例 #4
0
def test_bip21_encode():
    assert(
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W', {}) ==
        'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W'
    )
    assert(
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W', {
            'label': 'Luke-Jr'
        }) ==
        'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?label=Luke-Jr'
    )
    assert(
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W', {
            'amount': 20.3,
            'label': 'Luke-Jr'
        }) ==
        'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?amount=20.3&label=Luke-Jr'
    )
    assert(
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W', {
            'amount': 50,
            'label': 'Luke-Jr',
            'message': 'Donation for project xyz'
        }) ==
        'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?amount=50&label=Luke-Jr&message=Donation%20for%20project%20xyz'
    )
    assert(
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W', {
            'req-somethingyoudontunderstand': 50,
            'req-somethingelseyoudontget': 999
        }) ==
        'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?req-somethingyoudontunderstand=50&req-somethingelseyoudontget=999'
    )
    assert(
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W', {
            'somethingyoudontunderstand': 50,
            'somethingelseyoudontget': 999
        }) ==
        'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?somethingyoudontunderstand=50&somethingelseyoudontget=999'
    )
    # Invalid amounts must raise ValueError
    with pytest.raises(ValueError):
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W', {
            'amount': ''
        })
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W', {
            'amount': 'XYZ'
        })
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W', {
            'amount': '100\'000'
        })
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W', {
            'amount': '100,000'
        })
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W', {
            'amount': '100000000'
        })
コード例 #5
0
def test_bip21_encode():
    assert (btc.encode_bip21_uri(
        '175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
        {}) == 'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W')
    assert (btc.encode_bip21_uri(
        '175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
        {'label': 'Luke-Jr'
         }) == 'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?label=Luke-Jr')
    # Both dictionary and list of tuples should work
    assert (btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W', [
        ('label', 'Luke-Jr')
    ]) == 'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?label=Luke-Jr')
    # Use list of tuples version for multiple parameter tests, as dicts don't
    # have guaranteed ordering.
    assert (
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             [('amount', 20.3), ('label', 'Luke-Jr')]) ==
        'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?amount=20.3&label=Luke-Jr')
    assert (
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             [('amount', 50), ('label', 'Luke-Jr'),
                              ('message', 'Donation for project xyz')]) ==
        'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?amount=50&label=Luke-Jr&message=Donation%20for%20project%20xyz'
    )
    assert (
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             [('req-somethingyoudontunderstand', 50),
                              ('req-somethingelseyoudontget', 999)]) ==
        'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?req-somethingyoudontunderstand=50&req-somethingelseyoudontget=999'
    )
    assert (
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             [('somethingyoudontunderstand', 50),
                              ('somethingelseyoudontget', 999)]) ==
        'bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?somethingyoudontunderstand=50&somethingelseyoudontget=999'
    )
    # Invalid amounts must raise ValueError
    with pytest.raises(ValueError):
        # test dicts
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             {'amount': ''})
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             {'amount': 'XYZ'})
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             {'amount': '100\'000'})
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             {'amount': '100,000'})
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             {'amount': '100000000'})
        # test list of tuples
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             [('amount', '')])
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             [('amount', 'XYZ')])
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             [('amount', '100\'000')])
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             [('amount', '100,000')])
        btc.encode_bip21_uri('175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W',
                             [('amount', '100000000')])