From ecac1d79f24c3db3a3822c80ae98d6d71455e63a Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 16 Nov 2016 23:08:41 +0100 Subject: [PATCH] apps.wallet: refactor message digest method into shared module naming would need some further thoughts, though --- src/apps/common/signverify.py | 13 +++++++++++++ src/apps/wallet/layout_sign_message.py | 12 +++--------- src/apps/wallet/layout_verify_message.py | 10 ++-------- 3 files changed, 18 insertions(+), 17 deletions(-) create mode 100644 src/apps/common/signverify.py diff --git a/src/apps/common/signverify.py b/src/apps/common/signverify.py new file mode 100644 index 0000000000..33ff3ef5d8 --- /dev/null +++ b/src/apps/common/signverify.py @@ -0,0 +1,13 @@ +from trezor.crypto.hashlib import sha256 + +from .signtx import HashWriter, write_varint + +def message_digest(coin, message): + + h = HashWriter(sha256) + write_varint(h, len(coin.signed_message_header)) + h.extend(coin.signed_message_header) + write_varint(h, len(message)) + h.extend(message) + + return sha256(h.getvalue()).digest() diff --git a/src/apps/wallet/layout_sign_message.py b/src/apps/wallet/layout_sign_message.py index b641cc5258..bcd6ff7a5b 100644 --- a/src/apps/wallet/layout_sign_message.py +++ b/src/apps/wallet/layout_sign_message.py @@ -5,11 +5,11 @@ from trezor.utils import unimport @unimport async def layout_sign_message(msg, session_id): from trezor.messages.MessageSignature import MessageSignature - from trezor.crypto.hashlib import sha256 from trezor.crypto.curve import secp256k1 - from ..common.signtx import node_derive, HashWriter, write_varint + from ..common.signtx import node_derive from ..common.seed import get_root_node from ..common import coins + from ..common.signverify import message_digest ui.display.clear() ui.display.text(10, 30, 'Signing message', @@ -26,13 +26,7 @@ async def layout_sign_message(msg, session_id): seckey = node.private_key() address = node.address(coin.address_type) - h = HashWriter(sha256) - write_varint(h, len(coin.signed_message_header)) - h.extend(coin.signed_message_header) - write_varint(h, len(message)) - h.extend(message) - - digest = sha256(h.getvalue()).digest() + digest = message_digest(coin, message) signature = secp256k1.sign(seckey, digest) diff --git a/src/apps/wallet/layout_verify_message.py b/src/apps/wallet/layout_verify_message.py index 2e2c14f5db..bfbbe2c325 100644 --- a/src/apps/wallet/layout_verify_message.py +++ b/src/apps/wallet/layout_verify_message.py @@ -9,7 +9,7 @@ async def layout_verify_message(msg, session_id): from trezor.crypto import base58 from ..common import address_type from ..common import coins - from ..common.signtx import node_derive, HashWriter, write_varint + from ..common.signverify import message_digest address = msg.address message = msg.message @@ -23,13 +23,7 @@ async def layout_verify_message(msg, session_id): ui.display.text(10, 60, message, ui.MONO, ui.WHITE, ui.BLACK) ui.display.text(10, 80, address, ui.MONO, ui.WHITE, ui.BLACK) - h = HashWriter(sha256) - write_varint(h, len(coin.signed_message_header)) - h.extend(coin.signed_message_header) - write_varint(h, len(message)) - h.extend(message) - - digest = sha256(h.getvalue()).digest() + digest = message_digest(coin, message) pubkey = secp256k1.verify_recover(signature, digest) if not pubkey: