diff --git a/monero/backends/jsonrpc.py b/monero/backends/jsonrpc.py index 7203ae4..4247a4d 100644 --- a/monero/backends/jsonrpc.py +++ b/monero/backends/jsonrpc.py @@ -5,6 +5,7 @@ import operator import json import logging import requests +import six from .. import exceptions from ..account import Account @@ -67,7 +68,7 @@ class JSONRPCDaemon(object): def send_transaction(self, blob, relay=True): res = self.raw_request('/sendrawtransaction', { - 'tx_as_hex': binascii.hexlify(blob), + 'tx_as_hex': six.ensure_text(binascii.hexlify(blob)), 'do_not_relay': not relay}) if res['status'] == 'OK': return res diff --git a/tests/data/test_jsonrpcdaemon/0e8fa9202e0773333360e5b9e8fb8e94272c16a8a58b6fe7cf3b4327158e3a44.tx b/tests/data/test_jsonrpcdaemon/0e8fa9202e0773333360e5b9e8fb8e94272c16a8a58b6fe7cf3b4327158e3a44.tx new file mode 100644 index 0000000..6e4ad39 Binary files /dev/null and b/tests/data/test_jsonrpcdaemon/0e8fa9202e0773333360e5b9e8fb8e94272c16a8a58b6fe7cf3b4327158e3a44.tx differ diff --git a/tests/data/test_jsonrpcdaemon/test_send_transaction.json b/tests/data/test_jsonrpcdaemon/test_send_transaction.json new file mode 100644 index 0000000..5388e75 --- /dev/null +++ b/tests/data/test_jsonrpcdaemon/test_send_transaction.json @@ -0,0 +1,18 @@ +{ + "credits": 0, + "double_spend": false, + "fee_too_low": false, + "invalid_input": false, + "invalid_output": false, + "low_mixin": false, + "not_rct": false, + "not_relayed": false, + "overspend": false, + "reason": "", + "sanity_check_failed": false, + "status": "OK", + "too_big": false, + "too_few_outputs": false, + "top_hash": "", + "untrusted": false +} diff --git a/tests/test_jsonrpcdaemon.py b/tests/test_jsonrpcdaemon.py index 4e0e065..f8dd6dd 100644 --- a/tests/test_jsonrpcdaemon.py +++ b/tests/test_jsonrpcdaemon.py @@ -1,9 +1,11 @@ import decimal +import os import responses from monero.const import NET_STAGE from monero.daemon import Daemon from monero.backends.jsonrpc import JSONRPCDaemon +from monero.transaction import Transaction from .base import JSONTestCase @@ -11,6 +13,7 @@ class JSONRPCDaemonTestCase(JSONTestCase): jsonrpc_url = 'http://127.0.0.1:18081/json_rpc' mempool_url = 'http://127.0.0.1:18081/get_transaction_pool' transactions_url = 'http://127.0.0.1:18081/get_transactions' + sendrawtransaction_url = 'http://127.0.0.1:18081/sendrawtransaction' data_subdir = 'test_jsonrpcdaemon' def setUp(self): @@ -110,3 +113,18 @@ class JSONRPCDaemonTestCase(JSONTestCase): "035a1cfadd2f80124998f5af8c7bb6703743a4f322d0a20b7f7b502956ada59d") self.assertIsNone(txs[3].height) self.assertEqual(txs[3].size, 2724) + + @responses.activate + def test_send_transaction(self): + path = os.path.join( + os.path.dirname(__file__), + "data", + self.data_subdir, + "0e8fa9202e0773333360e5b9e8fb8e94272c16a8a58b6fe7cf3b4327158e3a44.tx") + responses.add(responses.POST, self.sendrawtransaction_url, + json=self._read('test_send_transaction.json'), + status=200) + tx = Transaction( + blob=open(path, "rb").read()) + rsp = self.daemon.send_transaction(tx) + self.assertEqual(rsp["status"], "OK") diff --git a/utils/pushtx.py b/utils/pushtx.py index 0a1419a..5374d50 100755 --- a/utils/pushtx.py +++ b/utils/pushtx.py @@ -4,6 +4,7 @@ import logging import operator import re import sys +import six from monero.backends.jsonrpc import JSONRPCDaemon from monero.daemon import Daemon @@ -36,9 +37,9 @@ elif args.verbosity > 1: level = logging.DEBUG logging.basicConfig(level=level, format="%(asctime)-15s %(message)s") if args.tx_filenames: - blobs = [(f, open(f, 'r').read()) for f in args.tx_filenames] + blobs = [(f, open(f, 'rb').read()) for f in args.tx_filenames] else: - blobs = [('transaction', sys.stdin.read())] + blobs = [('transaction', sys.stdin.buffer.read() if six.PY3 else sys.stdin.read())] d = Daemon(JSONRPCDaemon(timeout=args.timeout, proxy_url=args.proxy_url, **args.daemon_rpc_url)) for name, blob in blobs: logging.debug("Sending {}".format(name)) diff --git a/utils/transfer.py b/utils/transfer.py index 9847e2c..6487711 100755 --- a/utils/transfer.py +++ b/utils/transfer.py @@ -63,6 +63,6 @@ for tx in txns: if args.outdir: outname = os.path.join(args.outdir, tx.hash + '.tx') outfile = open(outname, 'wb') - outfile.write(tx.blob.encode()) + outfile.write(tx.blob) outfile.close() print(u"Transaction saved to {}".format(outname))