Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Commit

Permalink
xmr: extra serialization refactored, manual serialization
Browse files Browse the repository at this point in the history
- extra is serialized manually to reduce serialization overhead
- extra contains simple structures now:
  - payment ID = already serialized manually
  - tx pub key = easy to serialize manually
  - tx additional pub keys = serialized manually with little effort, more efficient memory usage
  • Loading branch information
ph4r05 committed Oct 2, 2018
1 parent 8ce28a5 commit b98c2f8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 60 deletions.
4 changes: 1 addition & 3 deletions src/apps/monero/protocol/signing/step_07_all_out1_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,9 @@ def _set_tx_extra(state: State):

state.tx.extra = tsx_helper.add_tx_pub_key_to_extra(state.tx.extra, state.tx_pub)

# Not needed to remove - extra is clean
# state.tx.extra = await monero.remove_field_from_tx_extra(state.tx.extra, xmrtypes.TxExtraAdditionalPubKeys)
if state.need_additional_txkeys:
state.tx.extra = tsx_helper.add_additional_tx_pub_keys_to_extra(
state.tx.extra, pub_enc=state.additional_tx_public_keys
state.tx.extra, state.additional_tx_public_keys
)


Expand Down
35 changes: 0 additions & 35 deletions src/apps/monero/xmr/serialize_messages/tx_extra.py

This file was deleted.

39 changes: 17 additions & 22 deletions src/apps/monero/xmr/sub/tsx_helper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
from apps.monero.xmr import crypto
from apps.monero.xmr.serialize import xmrserialize
from apps.monero.xmr.serialize.readwriter import MemoryReaderWriter
from apps.monero.xmr.serialize_messages.tx_extra import (
TxExtraAdditionalPubKeys,
TxExtraField,
)
from apps.monero.xmr.serialize import int_serialize


def absolute_output_offsets_to_relative(off):
Expand All @@ -25,27 +20,27 @@ def add_tx_pub_key_to_extra(tx_extra, pub_key):
Adds public key to the extra
"""
to_add = bytearray(33)
to_add[0] = 1
crypto.encodepoint_into(memoryview(to_add)[1:], pub_key) # TX_EXTRA_TAG_PUBKEY
to_add[0] = 1 # TX_EXTRA_TAG_PUBKEY
crypto.encodepoint_into(memoryview(to_add)[1:], pub_key)
return tx_extra + to_add


def add_additional_tx_pub_keys_to_extra(
tx_extra, additional_pub_keys=None, pub_enc=None
):
def add_additional_tx_pub_keys_to_extra(tx_extra, pub_enc):
"""
Adds all pubkeys to the extra
Adds all additional tx public keys to the extra buffer
"""
pubs_msg = TxExtraAdditionalPubKeys(
data=pub_enc
if pub_enc
else [crypto.encodepoint(x) for x in additional_pub_keys]
)
# format: variant_tag (0x4) | array len varint | 32B | 32B | ...
num_keys = len(pub_enc)
len_size = int_serialize.uvarint_size(num_keys)
buffer = bytearray(1 + len_size + 32 * num_keys)

rw = MemoryReaderWriter()
ar = xmrserialize.Archive(rw, True)
buffer[0] = 0x4 # TX_EXTRA_TAG_ADDITIONAL_PUBKEYS
int_serialize.dump_uvarint_b_into(num_keys, buffer, 1) # uvarint(num_keys)
offset = 1 + len_size

# format: variant_tag (0x4) | array len varint | 32B | 32B | ...
ar.variant(pubs_msg, TxExtraField)
tx_extra += rw.get_buffer()
for idx in range(num_keys):
buffer[offset : offset + 32] = pub_enc[idx]
offset += 32

tx_extra += buffer
return tx_extra

0 comments on commit b98c2f8

Please sign in to comment.