Skip to content

Commit

Permalink
xmr: wrapper protocol messages removed
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Sep 18, 2018
1 parent 6f40ce1 commit 57a1f25
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/apps/monero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def __init__(self):
def boot():
wire.add(MessageType.MoneroGetAddress, __name__, "get_address")
wire.add(MessageType.MoneroGetWatchKey, __name__, "get_watch_only")
wire.add(MessageType.MoneroTransactionSignRequest, __name__, "sign_tx")
wire.add(MessageType.MoneroKeyImageSyncRequest, __name__, "key_image_sync")
wire.add(MessageType.MoneroTransactionInitRequest, __name__, "sign_tx")
wire.add(MessageType.MoneroKeyImageExportInitRequest, __name__, "key_image_sync")

if hasattr(MessageType, "MoneroLiteInitRequest"):
wire.add(MessageType.MoneroLiteInitRequest, "lite_protocol", STATE, 1)
Expand Down
35 changes: 22 additions & 13 deletions src/apps/monero/key_image_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ async def key_image_sync(ctx, msg):
state = None

while True:
res, state = await key_image_sync_step(ctx, msg, state)
if msg.final_msg:
res, state, accept_msgs = await key_image_sync_step(ctx, msg, state)
if accept_msgs is None:
break
msg = await ctx.call(res, MessageType.MoneroKeyImageSyncRequest)
msg = await ctx.call(res, *accept_msgs)

return res

Expand All @@ -23,21 +23,30 @@ async def key_image_sync_step(ctx, msg, state):

from apps.monero.protocol import key_image_sync

if __debug__:
log.debug(__name__, "f: %s a: %s", gc.mem_free(), gc.mem_alloc())
gc.collect()

if msg.init:
if msg.MESSAGE_WIRE_TYPE == MessageType.MoneroKeyImageExportInitRequest:
from apps.monero.controller import iface

state = key_image_sync.KeyImageSync(ctx=ctx, iface=iface.get_iface(ctx))
return await state.init(ctx, msg.init), state

elif msg.step:
return await state.sync(ctx, msg.step), state

elif msg.final_msg:
return await state.final(ctx, msg.final_msg), None
return (
await state.init(ctx, msg),
state,
(MessageType.MoneroKeyImageSyncStepRequest,),
)

elif msg.MESSAGE_WIRE_TYPE == MessageType.MoneroKeyImageSyncStepRequest:
return (
await state.sync(ctx, msg),
state,
(
MessageType.MoneroKeyImageSyncStepRequest,
MessageType.MoneroKeyImageSyncFinalRequest,
),
)

elif msg.MESSAGE_WIRE_TYPE == MessageType.MoneroKeyImageSyncFinalRequest:
return await state.final(ctx, msg), None, None

else:
raise ValueError("Unknown error")
104 changes: 76 additions & 28 deletions src/apps/monero/sign_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ async def sign_tx(ctx, msg):
while True:
if __debug__:
log.debug(__name__, "#### F: %s, A: %s", gc.mem_free(), gc.mem_alloc())
res, state = await sign_tx_step(ctx, msg, state)
if msg.final_msg:
res, state, accept_msgs = await sign_tx_step(ctx, msg, state)
if accept_msgs is None:
break

await ctx.write(res)
del (res, msg)
utils.unimport_end(mods)

msg = await ctx.read((MessageType.MoneroTransactionSignRequest,))
msg = await ctx.read(accept_msgs)
gc.collect()

utils.unimport_end(mods)
Expand All @@ -34,42 +34,90 @@ async def sign_tx_step(ctx, msg, state):
from apps.monero.controller import iface, wrapper
from apps.monero.protocol.tsx_sign_builder import TTransactionBuilder

if msg.init:
init = msg.init
creds = await wrapper.monero_get_creds(ctx, init.address_n, init.network_type)
if msg.MESSAGE_WIRE_TYPE == MessageType.MoneroTransactionInitRequest:
creds = await wrapper.monero_get_creds(ctx, msg.address_n, msg.network_type)
state = TTransactionBuilder(iface.get_iface(ctx), creds)
del creds

gc.collect()
res = await sign_tx_dispatch(state, msg)
res, accept_msgs = await sign_tx_dispatch(state, msg)
gc.collect()

if state.is_terminal():
state = None
return res, state
return res, state, accept_msgs


async def sign_tx_dispatch(tsx, msg):
if msg.init:
return await tsx_init(tsx, msg.init.tsx_data)
elif msg.set_input:
return await tsx_set_input(tsx, msg.set_input)
elif msg.input_permutation:
return await tsx_inputs_permutation(tsx, msg.input_permutation)
elif msg.input_vini:
return await tsx_input_vini(tsx, msg.input_vini)
elif msg.all_in_set:
return await tsx_all_in_set(tsx, msg.all_in_set)
elif msg.set_output:
return await tsx_set_output1(tsx, msg.set_output)
elif msg.all_out_set:
return await tsx_all_out1_set(tsx, msg.all_out_set)
elif msg.mlsag_done:
return await tsx_mlsag_done(tsx)
elif msg.sign_input:
return await tsx_sign_input(tsx, msg.sign_input)
elif msg.final_msg:
return await tsx_sign_final(tsx)
if msg.MESSAGE_WIRE_TYPE == MessageType.MoneroTransactionInitRequest:
return (
await tsx_init(tsx, msg.tsx_data),
(MessageType.MoneroTransactionSetInputRequest,),
)

elif msg.MESSAGE_WIRE_TYPE == MessageType.MoneroTransactionSetInputRequest:
return (
await tsx_set_input(tsx, msg),
(
MessageType.MoneroTransactionSetInputRequest,
MessageType.MoneroTransactionInputsPermutationRequest,
),
)

elif msg.MESSAGE_WIRE_TYPE == MessageType.MoneroTransactionInputsPermutationRequest:
return (
await tsx_inputs_permutation(tsx, msg),
(MessageType.MoneroTransactionInputViniRequest,),
)

elif msg.MESSAGE_WIRE_TYPE == MessageType.MoneroTransactionInputViniRequest:
return (
await tsx_input_vini(tsx, msg),
(
MessageType.MoneroTransactionInputViniRequest,
MessageType.MoneroTransactionAllInputsSetRequest,
),
)

elif msg.MESSAGE_WIRE_TYPE == MessageType.MoneroTransactionAllInputsSetRequest:
return (
await tsx_all_in_set(tsx, msg),
(MessageType.MoneroTransactionSetOutputRequest,),
)

elif msg.MESSAGE_WIRE_TYPE == MessageType.MoneroTransactionSetOutputRequest:
return (
await tsx_set_output1(tsx, msg),
(
MessageType.MoneroTransactionSetOutputRequest,
MessageType.MoneroTransactionAllOutSetRequest,
),
)

elif msg.MESSAGE_WIRE_TYPE == MessageType.MoneroTransactionAllOutSetRequest:
return (
await tsx_all_out1_set(tsx, msg),
(MessageType.MoneroTransactionMlsagDoneRequest,),
)

elif msg.MESSAGE_WIRE_TYPE == MessageType.MoneroTransactionMlsagDoneRequest:
return (
await tsx_mlsag_done(tsx),
(MessageType.MoneroTransactionSignInputRequest,),
)

elif msg.MESSAGE_WIRE_TYPE == MessageType.MoneroTransactionSignInputRequest:
return (
await tsx_sign_input(tsx, msg),
(
MessageType.MoneroTransactionSignInputRequest,
MessageType.MoneroTransactionFinalRequest,
),
)

elif msg.MESSAGE_WIRE_TYPE == MessageType.MoneroTransactionFinalRequest:
return await tsx_sign_final(tsx), None

else:
from trezor import wire

Expand Down

0 comments on commit 57a1f25

Please sign in to comment.