s/OneTimeOutput/Output/

Store amount only if known, None otherwise
pull/93/head
Michał Sałaban 3 years ago
parent 3fd58b2ed0
commit 70be9453a1

@ -192,18 +192,17 @@ class Transaction(object):
self.json["rct_signatures"]["ecdhInfo"][idx]["amount"]
)
payment = None
amount = from_atomic(vout['amount'])
amount = from_atomic(vout['amount']) if self.version == 1 or self.is_coinbase else None
if wallet:
for addridx, addr in enumerate(addresses):
psk = binascii.unhexlify(addr.spend_key())
payment = _scan_pubkeys(svk, psk, stealth_address, amount, encamount)
if payment:
break
outs.append(OneTimeOutput(
outs.append(Output(
stealth_address=vout['target']['key'],
amount=payment.amount if payment else amount,
index=self.output_indices[idx] if self.output_indices else None,
height=self.height,
transaction=self,
payment=payment))
return outs
@ -212,7 +211,7 @@ class Transaction(object):
return self.hash
class OneTimeOutput(object):
class Output(object):
"""
A Monero one-time public output (A.K.A stealth address).
Identified by `stealth_address`, or `index` and `amount`
@ -224,29 +223,27 @@ class OneTimeOutput(object):
stealth_address = None
amount = None
index = None
height = None
mask = None
transaction = None
payment = None
unlocked = None
def __init__(self, **kwargs):
self.stealth_address = kwargs.get('stealth_address', self.stealth_address)
self.amount = kwargs.get('amount', self.amount)
self.index = kwargs.get('index', self.index)
self.height = kwargs.get('height', self.height)
self.mask = kwargs.get('mask', self.mask)
self.transaction = kwargs.get('transaction', self.transaction)
self.payment = kwargs.get('payment', self.payment)
self.unlocked = kwargs.get('unlocked', self.unlocked)
def __repr__(self):
# Try to represent output as (index, amount) pair if applicable because there is no RPC
# daemon command to lookup outputs by their stealth_address ;(
if self.stealth_address:
return self.stealth_address
res = self.stealth_address
else:
return '(index={},amount={})'.format(self.index, self.amount)
res = "(index={},amount={})".format(self.index, self.amount)
if self.payment:
return "{:s}, {:.12f} to [{:s}]".format(
res, self.payment.amount, str(self.payment.local_address)[:6])
return res
def __eq__(self, other):
# Try to compare stealth_addresses, then try to compare (index,amount) pairs, else raise error

@ -68,6 +68,7 @@ class OutputTestCase(JSONTestCase):
self.assertEqual(outs[3].amount, outs[3].payment.amount)
self.assertEqual(outs[4].amount, outs[4].payment.amount)
self.assertEqual(outs[0].amount, Decimal(4))
self.assertIsNone(outs[1].amount)
self.assertEqual(outs[2].amount, Decimal(1))
self.assertEqual(outs[3].amount, Decimal(2))
self.assertEqual(outs[4].amount, Decimal(8))
@ -83,6 +84,10 @@ class OutputTestCase(JSONTestCase):
self.assertEqual(
outs[4].payment.local_address,
"7BJxHKTa4p5USJ9Z5GY15ZARXL6Qe84qT3FnWkMbSJSoEj9ugGjnpQ1N9H1jqkjsTzLiN5VTbCP8f4MYYVPAcXhr36bHXzP")
self.assertEqual(
repr(outs[0]),
"d3eb42322566c1d48685ee0d1ad7aed2ba6210291a785ec051d8b13ae797d202, 4.000000000000 "
"to [76Qt2x]")
def test_coinbase_no_own_output(self):
txdata = self._read("test_coinbase_no_own_output-26dcb5.json")

@ -6,7 +6,7 @@ import unittest
from monero.address import address
from monero.numbers import PaymentID
from monero.transaction import IncomingPayment, Transaction, OneTimeOutput, _ByHeight
from monero.transaction import IncomingPayment, Transaction, Output, _ByHeight
from monero import exceptions
class FiltersTestCase(unittest.TestCase):
@ -36,8 +36,8 @@ class FiltersTestCase(unittest.TestCase):
'5997e64b90d59f7f810ddbc801f747c4fa43e2de593e4ea48531e16d776c00fd']}}
self.outind1 = [25884175, 25884176]
self.tx2 = Transaction(json=self.json1, output_indices=self.outind1)
self.oto1 = OneTimeOutput(index=25973289, amount=Decimal('0.000000000000'))
self.oto2 = OneTimeOutput(pubkey='0faff18f7149a0db5aa0dc3c9116887740ccbb5dc4d1eeff87895288e55e5052')
self.oto1 = Output(index=25973289, amount=Decimal('0.000000000000'))
self.oto2 = Output(pubkey='0faff18f7149a0db5aa0dc3c9116887740ccbb5dc4d1eeff87895288e55e5052')
def test_hash(self):
self.assertIn(
@ -52,9 +52,9 @@ class FiltersTestCase(unittest.TestCase):
self.assertEqual(out1.transaction, self.tx2)
self.assertEqual(out2.transaction, self.tx2)
self.assertIn(self.json1['vout'][0]['target']['key'], repr(out1))
self.assertFalse(out2 != OneTimeOutput(stealth_address=self.json1['vout'][1]['target']['key']))
self.assertFalse(out2 != Output(stealth_address=self.json1['vout'][1]['target']['key']))
self.assertIn('(index=25973289,amount=0E-12)', repr(self.oto1))
self.assertEqual(self.oto1, OneTimeOutput(index=25973289, amount=Decimal('0.000000000000')))
self.assertEqual(self.oto1, Output(index=25973289, amount=Decimal('0.000000000000')))
with self.assertRaises(exceptions.TransactionWithoutJSON):
self.tx1.outputs()
@ -62,6 +62,7 @@ class FiltersTestCase(unittest.TestCase):
with self.assertRaises(TypeError):
self.oto1 == self.oto2
class SortingTestCase(unittest.TestCase):
def test_sorting(self):
pmts = [

Loading…
Cancel
Save