2016-11-23 13:51:05 +00:00
|
|
|
from trezor import ui
|
2018-02-27 01:35:20 +00:00
|
|
|
from trezor.wire import FailureError
|
2018-02-06 13:07:46 +00:00
|
|
|
from trezor.crypto.curve import secp256k1
|
2018-02-27 16:25:09 +00:00
|
|
|
from trezor.messages.InputScriptType import SPENDADDRESS, SPENDP2SHWITNESS, SPENDWITNESS
|
2018-02-27 01:35:20 +00:00
|
|
|
from trezor.messages.FailureType import ProcessError
|
2018-02-06 13:07:46 +00:00
|
|
|
from trezor.messages.MessageSignature import MessageSignature
|
|
|
|
from trezor.ui.text import Text
|
|
|
|
from apps.common import coins, seed
|
|
|
|
from apps.common.confirm import require_confirm
|
|
|
|
from apps.common.signverify import message_digest, split_message
|
2018-02-27 16:25:09 +00:00
|
|
|
from apps.wallet.sign_tx.addresses import get_address
|
2016-06-09 15:34:43 +00:00
|
|
|
|
2016-09-21 12:24:12 +00:00
|
|
|
|
2018-02-06 13:07:46 +00:00
|
|
|
async def sign_message(ctx, msg):
|
|
|
|
message = msg.message
|
|
|
|
address_n = msg.address_n
|
2016-11-23 13:51:05 +00:00
|
|
|
coin_name = msg.coin_name or 'Bitcoin'
|
2018-02-27 01:35:20 +00:00
|
|
|
script_type = msg.script_type or 0
|
2016-11-16 20:28:27 +00:00
|
|
|
coin = coins.by_name(coin_name)
|
2016-09-26 11:07:53 +00:00
|
|
|
|
2018-02-28 19:20:39 +00:00
|
|
|
await require_confirm_sign_message(ctx, message)
|
2016-12-12 14:19:51 +00:00
|
|
|
|
2018-02-06 13:28:22 +00:00
|
|
|
node = await seed.derive_node(ctx, address_n)
|
2016-11-16 20:28:27 +00:00
|
|
|
seckey = node.private_key()
|
|
|
|
|
2018-02-27 16:25:09 +00:00
|
|
|
address = get_address(script_type, coin, node)
|
2018-02-06 13:07:46 +00:00
|
|
|
digest = message_digest(coin, message)
|
2016-11-16 20:28:27 +00:00
|
|
|
signature = secp256k1.sign(seckey, digest)
|
|
|
|
|
2018-02-27 16:25:09 +00:00
|
|
|
if script_type == SPENDADDRESS:
|
|
|
|
pass
|
|
|
|
elif script_type == SPENDP2SHWITNESS:
|
|
|
|
signature = bytes([signature[0] + 4]) + signature[1:]
|
|
|
|
elif script_type == SPENDWITNESS:
|
|
|
|
signature = bytes([signature[0] + 8]) + signature[1:]
|
|
|
|
else:
|
|
|
|
raise FailureError(ProcessError, 'Unsupported script type')
|
|
|
|
|
2016-11-16 20:28:27 +00:00
|
|
|
return MessageSignature(address=address, signature=signature)
|
2018-02-06 13:07:46 +00:00
|
|
|
|
|
|
|
|
2018-02-28 19:20:39 +00:00
|
|
|
async def require_confirm_sign_message(ctx, message):
|
2018-02-06 13:07:46 +00:00
|
|
|
message = split_message(message)
|
2018-02-27 14:04:03 +00:00
|
|
|
content = Text('Sign message', ui.ICON_DEFAULT, max_lines=5, *message)
|
2018-02-06 13:07:46 +00:00
|
|
|
await require_confirm(ctx, content)
|