Skip to content

Commit

Permalink
xmr: TxOut manual serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Sep 13, 2018
1 parent 44e3834 commit ce0d9b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/apps/monero/protocol/tsx_sign_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ async def gen_hmac_vini(self, src_entr, vini, idx):
hmac_vini = crypto.compute_hmac(hmac_key_vini, kwriter.get_digest())
return hmac_vini

async def gen_hmac_vouti(self, dst_entr, tx_out, idx):
async def gen_hmac_vouti(self, dst_entr, tx_out_bin, idx):
"""
Generates HMAC for (TxDestinationEntry[i] || tx.vout[i])
:param dst_entr:
Expand All @@ -430,13 +430,10 @@ async def gen_hmac_vouti(self, dst_entr, tx_out, idx):
"""
import protobuf
from apps.monero.xmr.sub.keccak_hasher import get_keccak_writer
from apps.monero.xmr.serialize import xmrserialize
from apps.monero.xmr.serialize_messages.tx_prefix import TxOut

kwriter = get_keccak_writer()
await protobuf.dump_message(kwriter, dst_entr)
ar = xmrserialize.Archive(kwriter, True)
ar.message(tx_out, TxOut)
kwriter.write(tx_out_bin)

hmac_key_vouti = self.hmac_key_txout(idx)
hmac_vouti = crypto.compute_hmac(hmac_key_vouti, kwriter.get_digest())
Expand Down Expand Up @@ -1215,22 +1212,25 @@ def _set_out1_derivation(self, dst_entr, additional_txkey_priv):
return derivation

async def _set_out1_tx_out(self, dst_entr, tx_out_key):
from apps.monero.xmr.serialize_messages.tx_prefix import TxoutToKey
from apps.monero.xmr.serialize_messages.tx_prefix import TxOut

tk = TxoutToKey(key=crypto.encodepoint(tx_out_key))
tx_out = TxOut(amount=0, target=tk)
from apps.monero.xmr.serialize import xmrserialize
from apps.monero.xmr.serialize.readwriter import MemoryReaderWriter

# Manual serialization of TxOut(0, TxoutToKey(key))
writer = MemoryReaderWriter(preallocate=34)
xmrserialize.dump_uvarint(writer, 0) # amount
xmrserialize.dump_uint(writer, 2, 1) # variant code TxoutToKey
writer.write(crypto.encodepoint(tx_out_key))
tx_out_bin = writer.get_buffer()
del(writer, xmrserialize)
self._mem_trace(8)

# Tx header prefix hashing
self.tx_prefix_hasher.field(tx_out, TxOut)
self.tx_prefix_hasher.buffer(tx_out_bin)
self._mem_trace(9, True)

# Hmac dest_entr.
hmac_vouti = await self.gen_hmac_vouti(dst_entr, tx_out, self.out_idx)
hmac_vouti = await self.gen_hmac_vouti(dst_entr, tx_out_bin, self.out_idx)
self._mem_trace(10, True)

tx_out_bin = misc.dump_msg(tx_out, preallocate=34)
return tx_out_bin, hmac_vouti

async def set_out1(self, dst_entr, dst_entr_hmac, rsig_data=None):
Expand Down
3 changes: 3 additions & 0 deletions src/apps/monero/xmr/sub/keccak_hasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def keep(self, keep=True):
def release(self):
self.ar = None

def buffer(self, buf):
return self.kwriter.write(buf)

def field(self, elem=None, elem_type=None, params=None, xser=None):
ar = self._ar(xser)
return ar.field(elem, elem_type, params)
Expand Down

0 comments on commit ce0d9b0

Please sign in to comment.