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

Commit

Permalink
ffix
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Aug 14, 2018
1 parent ae83d0e commit cfdf2b5
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 266 deletions.
4 changes: 2 additions & 2 deletions src/apps/monero/controller/iface.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ async def confirm_transaction(self, tsx_data, creds=None, ctx=None):
for idx, dst in enumerate(outs):
addr = encode_addr(
net_version(creds.network_type),
dst.addr.m_spend_public_key,
dst.addr.m_view_public_key,
dst.addr.spend_public_key,
dst.addr.view_public_key,
)
is_change = change_idx and idx == change_idx
await layout.require_confirm_tx(
Expand Down
46 changes: 2 additions & 44 deletions src/apps/monero/controller/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,6 @@ def compute_tx_key(spend_key_private, tx_prefix_hash, salt=None, rand_mult=None)
return tx_key, salt, rand_mult


def translate_monero_dest_entry(dst_entry):
from apps.monero.xmr.serialize_messages.tx_dest_entry import TxDestinationEntry
from apps.monero.xmr.serialize_messages.addr import AccountPublicAddress

d = TxDestinationEntry()
d.amount = dst_entry.amount
d.is_subaddress = dst_entry.is_subaddress
d.addr = AccountPublicAddress(
m_spend_public_key=dst_entry.addr.spend_public_key,
m_view_public_key=dst_entry.addr.view_public_key,
)
return d


async def translate_tsx_data(tsx_data):
from apps.monero.xmr.tsx_data import TsxData

tsxd = TsxData()
for fld in TsxData.f_specs():
fname = fld[0]
if hasattr(tsx_data, fname):
setattr(tsxd, fname, getattr(tsx_data, fname))

if tsx_data.change_dts:
tsxd.change_dts = translate_monero_dest_entry(tsx_data.change_dts)

tsxd.outputs = [translate_monero_dest_entry(x) for x in tsx_data.outputs]
return tsxd


async def parse_msg(bts, msg):
from apps.monero.xmr.serialize import xmrserialize
from apps.monero.xmr.serialize.readwriter import MemoryReaderWriter
Expand All @@ -91,18 +61,6 @@ async def parse_msg(bts, msg):
return await ar.message(msg)


async def parse_src_entry(bts):
from apps.monero.xmr.serialize_messages.tx_src_entry import TxSourceEntry

return await parse_msg(bts, TxSourceEntry())


async def parse_dst_entry(bts):
from apps.monero.xmr.serialize_messages.tx_dest_entry import TxDestinationEntry

return await parse_msg(bts, TxDestinationEntry())


async def parse_vini(bts):
from apps.monero.xmr.serialize_messages.tx_prefix import TxinToKey

Expand Down Expand Up @@ -135,7 +93,7 @@ def dst_entry_to_stdobj(dst):
return None

addr = StdObj(
m_spend_public_key=dst.addr.m_spend_public_key,
m_view_public_key=dst.addr.m_view_public_key,
spend_public_key=dst.addr.spend_public_key,
view_public_key=dst.addr.view_public_key,
)
return StdObj(amount=dst.amount, addr=addr, is_subaddress=dst.is_subaddress)
6 changes: 3 additions & 3 deletions src/apps/monero/key_image_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def layout_key_image_sync(state, ctx, msg):
state.ctx_ki = None

log.debug(__name__, "KI error, %s: %s", type(e), e)
raise
# from trezor.messages.Failure import Failure

from trezor.messages.Failure import Failure

return Failure()
# return Failure()
23 changes: 5 additions & 18 deletions src/apps/monero/protocol/tsx_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ async def tsx_init(self, tsx_data):
"""
self.tsx_ctr += 1
try:
tsxd = await misc.translate_tsx_data(tsx_data)
del tsx_data

return await self.tsx_obj.init_transaction(tsxd, self.tsx_ctr)
return await self.tsx_obj.init_transaction(tsx_data, self.tsx_ctr)
except Exception as e:
await self.tsx_exc_handler(e)
raise
Expand All @@ -153,10 +150,7 @@ async def tsx_set_input(self, msg):
:return:
"""
try:
src_entr = await misc.parse_src_entry(msg.src_entr)
del msg.src_entr

return await self.tsx_obj.set_input(src_entr)
return await self.tsx_obj.set_input(msg.src_entr)
except Exception as e:
await self.tsx_exc_handler(e)
raise
Expand All @@ -181,13 +175,11 @@ async def tsx_input_vini(self, msg):
:return:
"""
try:
src_entr = await misc.parse_src_entry(msg.src_entr)
vini = await misc.parse_vini(msg.vini)
del msg.src_entr
del msg.vini

return await self.tsx_obj.input_vini(
src_entr, vini, msg.vini_hmac, msg.pseudo_out, msg.pseudo_out_hmac
msg.src_entr, vini, msg.vini_hmac, msg.pseudo_out, msg.pseudo_out_hmac
)
except Exception as e:
await self.tsx_exc_handler(e)
Expand All @@ -202,10 +194,7 @@ async def tsx_set_output1(self, msg):
:return:
"""
try:
dst_entr = await misc.parse_dst_entry(msg.dst_entr)
del msg.dst_entr

return await self.tsx_obj.set_out1(dst_entr, msg.dst_entr_hmac)
return await self.tsx_obj.set_out1(msg.dst_entr, msg.dst_entr_hmac)
except Exception as e:
await self.tsx_exc_handler(e)
raise
Expand Down Expand Up @@ -252,13 +241,11 @@ async def tsx_sign_input(self, msg):
:return:
"""
try:
src_entr = await misc.parse_src_entry(msg.src_entr)
vini = await misc.parse_vini(msg.vini)
del msg.src_entr
del msg.vini

return await self.tsx_obj.sign_input(
src_entr,
msg.src_entr,
vini,
msg.vini_hmac,
msg.pseudo_out,
Expand Down
44 changes: 19 additions & 25 deletions src/apps/monero/protocol/tsx_sign_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,14 @@ async def gen_hmac_vini(self, src_entr, vini, idx):
:param idx:
:return:
"""
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_src_entry import TxSourceEntry
from apps.monero.xmr.serialize_messages.tx_prefix import TxinToKey

kwriter = get_keccak_writer()
ar = xmrserialize.Archive(kwriter, True)
await ar.message(src_entr, TxSourceEntry)
await protobuf.dump_message(kwriter, src_entr)
await ar.message(vini, TxinToKey)

hmac_key_vini = self.hmac_key_txin(idx)
Expand All @@ -380,14 +380,14 @@ async def gen_hmac_vouti(self, dst_entr, tx_out, idx):
:param idx:
:return:
"""
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_dest_entry import TxDestinationEntry
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)
await ar.message(dst_entr, TxDestinationEntry)
await ar.message(tx_out, TxOut)

hmac_key_vouti = self.hmac_key_txout(idx)
Expand All @@ -401,13 +401,11 @@ async def gen_hmac_tsxdest(self, dst_entr, idx):
:param idx:
:return:
"""
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_dest_entry import TxDestinationEntry

kwriter = get_keccak_writer()
ar = xmrserialize.Archive(kwriter, True)
await ar.message(dst_entr, TxDestinationEntry)
await protobuf.dump_message(kwriter, dst_entr)

hmac_key = self.hmac_key_txdst(idx)
hmac_tsxdest = crypto.compute_hmac(hmac_key, kwriter.get_digest())
Expand Down Expand Up @@ -479,7 +477,7 @@ async def init_transaction(self, tsx_data, tsx_ctr):
# if this is a single-destination transfer to a subaddress, we set the tx pubkey to R=s*D
if num_stdaddresses == 0 and num_subaddresses == 1:
self.r_pub = crypto.ge_scalarmult(
self.r, crypto.decodepoint(single_dest_subaddress.m_spend_public_key)
self.r, crypto.decodepoint(single_dest_subaddress.spend_public_key)
)

self.need_additional_txkeys = num_subaddresses > 0 and (
Expand Down Expand Up @@ -568,14 +566,15 @@ async def compute_sec_keys(self, tsx_data, tsx_ctr):
Generate master key H(TsxData || r || c_tsx)
:return:
"""
import protobuf
from apps.monero.xmr.sub.keccak_hasher import get_keccak_writer
from apps.monero.xmr.serialize import xmrserialize

writer = get_keccak_writer()
ar1 = xmrserialize.Archive(writer, True)
await ar1.message(tsx_data)
await protobuf.dump_message(writer, tsx_data)
await writer.awrite(crypto.encodeint(self.r))
await xmrserialize.dump_uvarint(writer, tsx_ctr)

self.key_master = crypto.keccak_2hash(
writer.get_digest() + crypto.encodeint(crypto.random_scalar())
)
Expand Down Expand Up @@ -603,7 +602,6 @@ async def set_input(self, src_entr):
with key derived for exactly this purpose.
:param src_entr:
:type src_entr: apps.monero.xmr.serialize_messages.tx_construct.TxSourceEntry
:return:
"""
from trezor.messages.MoneroTransactionSetInputAck import (
Expand All @@ -630,7 +628,7 @@ async def set_input(self, src_entr):
self.summary_inputs_money += src_entr.amount

# Secrets derivation
out_key = crypto.decodepoint(src_entr.outputs[src_entr.real_output][1].dest)
out_key = crypto.decodepoint(src_entr.outputs[src_entr.real_output].key.dest)
tx_key = crypto.decodepoint(src_entr.real_out_tx_key)
additional_keys = [
crypto.decodepoint(x) for x in src_entr.real_out_additional_tx_keys
Expand All @@ -649,7 +647,7 @@ async def set_input(self, src_entr):
# Construct tx.vin
ki_real = src_entr.multisig_kLRki.ki if self.multi_sig else ki
vini = TxinToKey(amount=src_entr.amount, k_image=crypto.encodepoint(ki_real))
vini.key_offsets = [x[0] for x in src_entr.outputs]
vini.key_offsets = [x.idx for x in src_entr.outputs]
vini.key_offsets = tsx_helper.absolute_output_offsets_to_relative(
vini.key_offsets
)
Expand Down Expand Up @@ -976,7 +974,7 @@ async def _set_out1_additional_keys(self, dst_entr):
if dst_entr.is_subaddress:
additional_txkey = crypto.ge_scalarmult(
additional_txkey_priv,
crypto.decodepoint(dst_entr.addr.m_spend_public_key),
crypto.decodepoint(dst_entr.addr.spend_public_key),
)
else:
additional_txkey = crypto.ge_scalarmult_base(additional_txkey_priv)
Expand Down Expand Up @@ -1004,7 +1002,7 @@ async def _set_out1_derivation(self, dst_entr, additional_txkey_priv):
else self.r
)
derivation = monero.generate_key_derivation(
crypto.decodepoint(dst_entr.addr.m_view_public_key), deriv_priv
crypto.decodepoint(dst_entr.addr.view_public_key), deriv_priv
)
return derivation

Expand All @@ -1014,7 +1012,6 @@ async def set_out1(self, dst_entr, dst_entr_hmac):
Computes destination stealth address, amount key, range proof + HMAC, out_pk, ecdh_info.
:param dst_entr
:type dst_entr: TxDestinationEntry
:param dst_entr_hmac
:return:
"""
Expand Down Expand Up @@ -1057,9 +1054,7 @@ async def set_out1(self, dst_entr, dst_entr_hmac):

amount_key = crypto.derivation_to_scalar(derivation, self.out_idx)
tx_out_key = crypto.derive_public_key(
derivation,
self.out_idx,
crypto.decodepoint(dst_entr.addr.m_spend_public_key),
derivation, self.out_idx, crypto.decodepoint(dst_entr.addr.spend_public_key)
)

from apps.monero.xmr.serialize_messages.tx_prefix import TxoutToKey
Expand Down Expand Up @@ -1271,7 +1266,6 @@ async def sign_input(
Generates a signature for one input.
:param src_entr: Source entry
:type src_entr: apps.monero.xmr.serialize_messages.tx_construct.TxSourceEntry
:param vini: tx.vin[i] for the transaction. Contains key image, offsets, amount (usually zero)
:param hmac_vini: HMAC for the tx.vin[i] as returned from Trezor
:param pseudo_out: pedersen commitment for the current input, uses alpha as the mask.
Expand Down Expand Up @@ -1355,14 +1349,14 @@ async def sign_input(
# Private key correctness test
self.assrt(
crypto.point_eq(
crypto.decodepoint(src_entr.outputs[src_entr.real_output][1].dest),
crypto.decodepoint(src_entr.outputs[src_entr.real_output].key.dest),
crypto.scalarmult_base(in_sk.dest),
),
"a1",
)
self.assrt(
crypto.point_eq(
crypto.decodepoint(src_entr.outputs[src_entr.real_output][1].mask),
crypto.decodepoint(src_entr.outputs[src_entr.real_output].key.mask),
crypto.gen_c(in_sk.mask, src_entr.amount),
),
"a2",
Expand All @@ -1378,7 +1372,7 @@ async def sign_input(
mg = None
if self.use_simple_rct:
# Simple RingCT
mix_ring = [x[1] for x in src_entr.outputs]
mix_ring = [x.key for x in src_entr.outputs]
mg, msc = mlsag2.prove_rct_mg_simple(
self.full_message,
mix_ring,
Expand All @@ -1393,7 +1387,7 @@ async def sign_input(
else:
# Full RingCt, only one input
txn_fee_key = crypto.scalarmult_h(self.get_fee())
mix_ring = [[x[1]] for x in src_entr.outputs]
mix_ring = [[x.key] for x in src_entr.outputs]

mg, msc = mlsag2.prove_rct_mg(
self.full_message,
Expand Down
Loading

0 comments on commit cfdf2b5

Please sign in to comment.