Exemplo n.º 1
0
async def test_send_payment(mock_mqtt):
    wallet = Wallet.factory("manta://localhost:8000/123")

    await wallet.send_payment(transaction_hash="myhash", crypto_currency="nano")

    expected = PaymentMessage(transaction_hash="myhash", crypto_currency="nano")

    mock_mqtt.subscribe.assert_called_with("acks/123")
    mock_mqtt.publish.assert_called_with("payments/123", JsonContains(expected), qos=1)
Exemplo n.º 2
0
async def test_on_ack(mock_mqtt):
    wallet = Wallet.factory("manta://localhost:8000/123")

    expected = AckMessage(txid="0", transaction_hash="myhash", status=Status.PENDING)

    mock_mqtt.push("acks/123", expected.to_json())

    ack_message = await wallet.acks.get()

    assert ack_message == expected
Exemplo n.º 3
0
async def test_get_payment_request(mock_mqtt, payment_request, caplog):
    caplog.set_level(logging.INFO)
    wallet = Wallet.factory("manta://localhost:8000/123")

    # noinspection PyUnusedLocal
    def se(topic, payload=None):
        nonlocal mock_mqtt, payment_request

        if topic == "payment_requests/123/btc":
            mock_mqtt.push("payment_requests/123", payment_request.to_json())
        else:
            assert True, "Unknown topic"

    mock_mqtt.publish.side_effect = se

    envelope = await wallet.get_payment_request("btc")
    assert envelope.unpack() == payment_request.unpack()
    assert envelope.verify(CERTIFICATE)
Exemplo n.º 4
0
async def test_get_certificate(mock_mqtt):
    with open(CERTIFICATE, "rb") as myfile:
        pem = myfile.read()

    def se(topic):
        nonlocal mock_mqtt, pem

        if topic == "certificate":
            mock_mqtt.push("certificate", pem)
        else:
            assert True, "Unknown Topic"

    wallet = Wallet.factory("manta://localhost:8000/123")

    mock_mqtt.subscribe.side_effect = se
    certificate = await wallet.get_certificate()

    mock_mqtt.subscribe.assert_called_with("certificate")
    assert (
        "test"
        == certificate.subject.get_attributes_for_oid(NameOID.COMMON_NAME)[0].value
    )
Exemplo n.º 5
0
    async def test_get_payment_request(self, dummy_payproc, dummy_store,
                                       web_post):
        if dummy_store.url:
            r = await web_post(dummy_store.url + "/merchant_order",
                               json={
                                   "amount": "10",
                                   "fiat": "EUR"
                               })
            logging.info(r)
            ack_message = r.json()
            url = ack_message['url']
        else:
            ack_message = await dummy_store.manta.merchant_order_request(
                amount=Decimal("10"), fiat='EUR')
            url = ack_message.url
        logging.info(url)
        wallet = Wallet.factory(url)

        envelope = await wallet.get_payment_request('NANO')
        self.pr = envelope.unpack()

        assert 10 == self.pr.amount
        assert "EUR" == self.pr.fiat_currency
        return wallet
Exemplo n.º 6
0
def test_factory(mock_mqtt):
    wallet = Wallet.factory("manta://127.0.0.1/123")
    assert wallet.host == "127.0.0.1"
    assert wallet.port == 1883
    assert wallet.session_id == "123"
Exemplo n.º 7
0
def test_parse_url_with_port():
    match = Wallet.parse_url("manta://127.0.0.1:8000/123")
    assert "127.0.0.1" == match[1]
    assert "8000" == match[2]
    assert "123" == match[3]
Exemplo n.º 8
0
def test_parse_url():
    match = Wallet.parse_url("manta://localhost/JqhCQ64gTYi02xu4GhBzZg==")
    assert "localhost" == match[1]
    assert "JqhCQ64gTYi02xu4GhBzZg==" == match[3]
Exemplo n.º 9
0
async def test_connect(broker):
    _, host, port, _ = broker
    wallet = Wallet.factory('manta://localhost:{}/123'.format(port))
    await wallet.connect()
    wallet.close()
Exemplo n.º 10
0
async def get_payment(url: str,
                      interactive: bool = False,
                      nano_wallet: str = None,
                      account: str = None,
                      ca_certificate: str = None):
    wallet = Wallet.factory(url)

    envelope = await get_payment_request(wallet)

    verified = False
    certificate = None

    if ca_certificate:
        certificate = await wallet.get_certificate()

        verified = verify_envelope(envelope, certificate, ca_certificate)

    pr = envelope.unpack()

    logger.info("Payment request: {}".format(pr))

    options = [x for x in pr.supported_cryptos]

    questions = [
        inquirer.List('crypto',
                      message=' What crypto you want to pay with?',
                      choices=options)
    ]

    if interactive:
        answers = inquirer.prompt(questions)

        chosen_crypto = answers['crypto']

        # Check if we have already the destination
        destination = pr.get_destination(chosen_crypto)

        # Otherwise ask payment provider
        if not destination:
            logger.info(
                'Requesting payment request for {}'.format(chosen_crypto))
            envelope = await get_payment_request(wallet, chosen_crypto)
            verified = False

            if ca_certificate:
                verified = verify_envelope(envelope, certificate,
                                           ca_certificate)

            pr = envelope.unpack()
            logger.info("Payment request: {}".format(pr))

            destination = pr.get_destination(chosen_crypto)

        if answers['crypto'] == 'NANO':
            rpc = nano.rpc.Client(host="http://localhost:7076")
            balance = rpc.account_balance(account=account)
            print()
            print("Actual balance: {}".format(
                str(
                    nano.convert(from_unit="raw",
                                 to_unit="XRB",
                                 value=balance['balance']))))

            if not verified:
                print("WARNING!!!! THIS IS NOT VERIFIED REQUEST")

            destination = pr.get_destination('NANO')

            if query_yes_no("Pay {} {} ({} {}) to {}".format(
                    destination.amount, destination.crypto_currency, pr.amount,
                    pr.fiat_currency, pr.merchant)):
                amount = int(
                    nano.convert(from_unit='XRB',
                                 to_unit="raw",
                                 value=destination.amount))

                print(amount)

                block = rpc.send(wallet=nano_wallet,
                                 source=account,
                                 destination=destination.destination_address,
                                 amount=amount)

                await wallet.send_payment(transaction_hash=block,
                                          crypto_currency='NANO')
        elif answers['crypto'] == 'TESTCOIN':
            destination = pr.get_destination('TESTCOIN')
            await wallet.send_payment(transaction_hash='test_hash',
                                      crypto_currency='TESTCOIN')
        else:
            print("Not supported!")
            sys.exit()

    else:
        await wallet.send_payment("myhash", pr.destinations[0].crypto_currency)

    ack = await wallet.acks.get()
    print(ack)