1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 14:28:07 +00:00

client: get rid of TrezorClient._convert_prime

This commit is contained in:
matejcik 2018-06-14 14:57:20 +02:00
parent 8f03bd0165
commit 7e90e89e69
12 changed files with 23 additions and 42 deletions

View File

@ -6,14 +6,12 @@ from .tools import expect, field, CallException, normalize_nfc, session
@expect(proto.PublicKey)
def get_public_node(client, n, ecdsa_curve_name=None, show_display=False, coin_name=None):
n = client._convert_prime(n)
return client.call(proto.GetPublicKey(address_n=n, ecdsa_curve_name=ecdsa_curve_name, show_display=show_display, coin_name=coin_name))
@field('address')
@expect(proto.Address)
def get_address(client, coin_name, n, show_display=False, multisig=None, script_type=proto.InputScriptType.SPENDADDRESS):
n = client._convert_prime(n)
if multisig:
return client.call(proto.GetAddress(address_n=n, coin_name=coin_name, show_display=show_display, multisig=multisig, script_type=script_type))
else:
@ -22,7 +20,6 @@ def get_address(client, coin_name, n, show_display=False, multisig=None, script_
@expect(proto.MessageSignature)
def sign_message(client, coin_name, n, message, script_type=proto.InputScriptType.SPENDADDRESS):
n = client._convert_prime(n)
message = normalize_nfc(message)
return client.call(proto.SignMessage(coin_name=coin_name, address_n=n, message=message, script_type=script_type))
@ -39,7 +36,6 @@ def verify_message(client, coin_name, address, signature, message):
@expect(proto.EncryptedMessage)
def encrypt_message(client, pubkey, message, display_only, coin_name, n):
if coin_name and n:
n = client._convert_prime(n)
return client.call(proto.EncryptMessage(pubkey=pubkey, message=message, display_only=display_only, coin_name=coin_name, address_n=n))
else:
return client.call(proto.EncryptMessage(pubkey=pubkey, message=message, display_only=display_only))
@ -47,7 +43,6 @@ def encrypt_message(client, pubkey, message, display_only, coin_name, n):
@expect(proto.DecryptedMessage)
def decrypt_message(client, n, nonce, message, msg_hmac):
n = client._convert_prime(n)
return client.call(proto.DecryptMessage(address_n=n, nonce=nonce, message=message, hmac=msg_hmac))

View File

@ -410,11 +410,6 @@ class ProtocolMixin(object):
if str(self.features.vendor) not in self.VENDORS:
raise RuntimeError("Unsupported device")
@staticmethod
def _convert_prime(n: tools.Address) -> tools.Address:
# Convert minus signs to uint32 with flag
return [tools.H_(int(abs(x))) if x < 0 else x for x in n]
@staticmethod
def expand_path(n):
warnings.warn('expand_path is deprecated, use tools.parse_path', DeprecationWarning, stacklevel=2)

View File

@ -96,11 +96,9 @@ def sign_with_privkey(digest: bytes, privkey: Ed25519PrivateKey,
@expect(messages.CosiCommitment)
def commit(client, n, data):
n = client._convert_prime(n)
return client.call(messages.CosiCommit(address_n=n, data=data))
@expect(messages.CosiSignature)
def sign(client, n, data, global_commitment, global_pubkey):
n = client._convert_prime(n)
return client.call(messages.CosiSign(address_n=n, data=data, global_commitment=global_commitment, global_pubkey=global_pubkey))

View File

@ -12,14 +12,11 @@ def int_to_big_endian(value):
@field('address')
@expect(proto.EthereumAddress)
def get_address(client, n, show_display=False, multisig=None):
n = client._convert_prime(n)
return client.call(proto.EthereumGetAddress(address_n=n, show_display=show_display))
@session
def sign_tx(client, n, nonce, gas_price, gas_limit, to, value, data=None, chain_id=None, tx_type=None):
n = client._convert_prime(n)
msg = proto.EthereumSignTx(
address_n=n,
nonce=int_to_big_endian(nonce),
@ -53,7 +50,6 @@ def sign_tx(client, n, nonce, gas_price, gas_limit, to, value, data=None, chain_
@expect(proto.EthereumMessageSignature)
def sign_message(client, n, message):
n = client._convert_prime(n)
message = normalize_nfc(message)
return client.call(proto.EthereumSignMessage(address_n=n, message=message))

View File

@ -7,19 +7,16 @@ from .tools import field, expect, CallException, normalize_nfc
@field('address')
@expect(proto.LiskAddress)
def get_address(client, n, show_display=False):
n = client._convert_prime(n)
return client.call(proto.LiskGetAddress(address_n=n, show_display=show_display))
@expect(proto.LiskPublicKey)
def get_public_key(client, n, show_display=False):
n = client._convert_prime(n)
return client.call(proto.LiskGetPublicKey(address_n=n, show_display=show_display))
@expect(proto.LiskMessageSignature)
def sign_message(client, n, message):
n = client._convert_prime(n)
message = normalize_nfc(message)
return client.call(proto.LiskSignMessage(address_n=n, message=message))
@ -56,8 +53,6 @@ def _asset_to_proto(asset):
@expect(proto.LiskSignedTx)
def sign_tx(client, n, transaction):
n = client._convert_prime(n)
msg = proto.LiskTransactionCommon()
msg.type = transaction["type"]

View File

@ -21,7 +21,6 @@ def get_ecdh_session_key(client, identity, peer_public_key, ecdsa_curve_name=Non
@field('value')
@expect(proto.CipheredKeyValue)
def encrypt_keyvalue(client, n, key, value, ask_on_encrypt=True, ask_on_decrypt=True, iv=b''):
n = client._convert_prime(n)
return client.call(proto.CipherKeyValue(address_n=n,
key=key,
value=value,
@ -34,7 +33,6 @@ def encrypt_keyvalue(client, n, key, value, ask_on_encrypt=True, ask_on_decrypt=
@field('value')
@expect(proto.CipheredKeyValue)
def decrypt_keyvalue(client, n, key, value, ask_on_encrypt=True, ask_on_decrypt=True, iv=b''):
n = client._convert_prime(n)
return client.call(proto.CipherKeyValue(address_n=n,
key=key,
value=value,

View File

@ -173,13 +173,11 @@ def create_sign_tx(transaction):
@field("address")
@expect(proto.NEMAddress)
def get_address(client, n, network, show_display=False):
n = client._convert_prime(n)
return client.call(proto.NEMGetAddress(address_n=n, network=network, show_display=show_display))
@expect(proto.NEMSignedTx)
def sign_tx(client, n, transaction):
n = client._convert_prime(n)
try:
msg = create_sign_tx(transaction)
except ValueError as e:

View File

@ -19,6 +19,8 @@ import pytest
from .common import TrezorTest
from trezorlib.tools import H_
class TestBip32Speed(TrezorTest):
@ -42,7 +44,8 @@ class TestBip32Speed(TrezorTest):
for depth in range(8):
start = time.time()
self.client.get_address('Bitcoin', range(-depth, 0))
address_n = [H_(-i) for i in range(-depth, 0)]
self.client.get_address('Bitcoin', address_n)
delay = time.time() - start
expected = (depth + 1) * 0.26
print("DEPTH", depth, "EXPECTED DELAY", expected, "REAL DELAY", delay)

View File

@ -19,6 +19,8 @@ import pytest
from .common import TrezorTest
from trezorlib.tools import H_
@pytest.mark.ethereum
class TestMsgEthereumGetaddress(TrezorTest):
@ -27,6 +29,6 @@ class TestMsgEthereumGetaddress(TrezorTest):
self.setup_mnemonic_nopin_nopassphrase()
assert hexlify(self.client.ethereum_get_address([])) == b'1d1c328764a41bda0492b66baa30c4a339ff85ef'
assert hexlify(self.client.ethereum_get_address([1])) == b'437207ca3cf43bf2e47dea0756d736c5df4f597a'
assert hexlify(self.client.ethereum_get_address([0, -1])) == b'e5d96dfa07bcf1a3ae43677840c31394258861bf'
assert hexlify(self.client.ethereum_get_address([-9, 0])) == b'f68804ac9eca9483ab4241d3e4751590d2c05102'
assert hexlify(self.client.ethereum_get_address([0, H_(1)])) == b'e5d96dfa07bcf1a3ae43677840c31394258861bf'
assert hexlify(self.client.ethereum_get_address([H_(9), 0])) == b'f68804ac9eca9483ab4241d3e4751590d2c05102'
assert hexlify(self.client.ethereum_get_address([0, 9999999])) == b'7a6366ecfcaf0d5dcc1539c171696c6cdd1eb8ed'

View File

@ -20,7 +20,7 @@ from .common import TrezorTest
from ..support import ckd_public as bip32
from trezorlib import messages as proto
from trezorlib.tools import parse_path
from trezorlib.tools import parse_path, H_
class TestMsgGetaddress(TrezorTest):
@ -29,16 +29,16 @@ class TestMsgGetaddress(TrezorTest):
self.setup_mnemonic_nopin_nopassphrase()
assert self.client.get_address('Bitcoin', []) == '1EfKbQupktEMXf4gujJ9kCFo83k1iMqwqK'
assert self.client.get_address('Bitcoin', [1]) == '1CK7SJdcb8z9HuvVft3D91HLpLC6KSsGb'
assert self.client.get_address('Bitcoin', [0, -1]) == '1JVq66pzRBvqaBRFeU9SPVvg3er4ZDgoMs'
assert self.client.get_address('Bitcoin', [-9, 0]) == '1F4YdQdL9ZQwvcNTuy5mjyQxXkyCfMcP2P'
assert self.client.get_address('Bitcoin', [0, H_(1)]) == '1JVq66pzRBvqaBRFeU9SPVvg3er4ZDgoMs'
assert self.client.get_address('Bitcoin', [H_(9), 0]) == '1F4YdQdL9ZQwvcNTuy5mjyQxXkyCfMcP2P'
assert self.client.get_address('Bitcoin', [0, 9999999]) == '1GS8X3yc7ntzwGw9vXwj9wqmBWZkTFewBV'
def test_ltc(self):
self.setup_mnemonic_nopin_nopassphrase()
assert self.client.get_address('Litecoin', []) == 'LYtGrdDeqYUQnTkr5sHT2DKZLG7Hqg7HTK'
assert self.client.get_address('Litecoin', [1]) == 'LKRGNecThFP3Q6c5fosLVA53Z2hUDb1qnE'
assert self.client.get_address('Litecoin', [0, -1]) == 'LcinMK8pVrAtpz7Qpc8jfWzSFsDLgLYfG6'
assert self.client.get_address('Litecoin', [-9, 0]) == 'LZHVtcwAEDf1BR4d67551zUijyLUpDF9EX'
assert self.client.get_address('Litecoin', [0, H_(1)]) == 'LcinMK8pVrAtpz7Qpc8jfWzSFsDLgLYfG6'
assert self.client.get_address('Litecoin', [H_(9), 0]) == 'LZHVtcwAEDf1BR4d67551zUijyLUpDF9EX'
assert self.client.get_address('Litecoin', [0, 9999999]) == 'Laf5nGHSCT94C5dK6fw2RxuXPiw2ZuRR9S'
def test_tbtc(self):

View File

@ -17,6 +17,8 @@
from .common import TrezorTest
from ..support import ckd_public as bip32
from trezorlib.tools import H_
class TestMsgGetpublickey(TrezorTest):
@ -26,10 +28,10 @@ class TestMsgGetpublickey(TrezorTest):
assert self.client.get_public_node([], coin_name='Bitcoin').xpub == 'xpub661MyMwAqRbcF1zGijBb2K6x9YiJPh58xpcCeLvTxMX6spkY3PcpJ4ABcCyWfskq5DDxM3e6Ez5ePCqG5bnPUXR4wL8TZWyoDaUdiWW7bKy'
assert bip32.serialize(self.client.get_public_node([1]).node, 0x0488B21E) == 'xpub68zNxjsTrV8y9AadThLW7dTAqEpZ7xBLFSyJ3X9pjTv6Njg6kxgjXJkzxq8u3ttnjBw1jupQHMP3gpGZzZqd1eh5S4GjkaMhPR18vMyUi8N'
assert self.client.get_public_node([1], coin_name='Bitcoin').xpub == 'xpub68zNxjsTrV8y9AadThLW7dTAqEpZ7xBLFSyJ3X9pjTv6Njg6kxgjXJkzxq8u3ttnjBw1jupQHMP3gpGZzZqd1eh5S4GjkaMhPR18vMyUi8N'
assert bip32.serialize(self.client.get_public_node([0, -1]).node, 0x0488B21E) == 'xpub6A3FoZqYXj1AbW4thRwBh26YwZWbmoyjTaZwwxJjY1oKUpefLepL3RFS9DHKQrjAfxDrzDepYMDZPqXN6upQm3bHQ9xaXD5a3mqni3goF4v'
assert self.client.get_public_node([0, -1], coin_name='Bitcoin').xpub == 'xpub6A3FoZqYXj1AbW4thRwBh26YwZWbmoyjTaZwwxJjY1oKUpefLepL3RFS9DHKQrjAfxDrzDepYMDZPqXN6upQm3bHQ9xaXD5a3mqni3goF4v'
assert bip32.serialize(self.client.get_public_node([-9, 0]).node, 0x0488B21E) == 'xpub6A2h5mzLDfYginoD7q7wCWbq18wTbN9gducRr2w5NRTwdLeoT3cJSwefFqW7uXTpVFGtpUyDMBNYs3DNvvXx6NPjF9YEbUQrtxFSWnPtVrv'
assert self.client.get_public_node([-9, 0], coin_name='Bitcoin').xpub == 'xpub6A2h5mzLDfYginoD7q7wCWbq18wTbN9gducRr2w5NRTwdLeoT3cJSwefFqW7uXTpVFGtpUyDMBNYs3DNvvXx6NPjF9YEbUQrtxFSWnPtVrv'
assert bip32.serialize(self.client.get_public_node([0, H_(1)]).node, 0x0488B21E) == 'xpub6A3FoZqYXj1AbW4thRwBh26YwZWbmoyjTaZwwxJjY1oKUpefLepL3RFS9DHKQrjAfxDrzDepYMDZPqXN6upQm3bHQ9xaXD5a3mqni3goF4v'
assert self.client.get_public_node([0, H_(1)], coin_name='Bitcoin').xpub == 'xpub6A3FoZqYXj1AbW4thRwBh26YwZWbmoyjTaZwwxJjY1oKUpefLepL3RFS9DHKQrjAfxDrzDepYMDZPqXN6upQm3bHQ9xaXD5a3mqni3goF4v'
assert bip32.serialize(self.client.get_public_node([H_(9), 0]).node, 0x0488B21E) == 'xpub6A2h5mzLDfYginoD7q7wCWbq18wTbN9gducRr2w5NRTwdLeoT3cJSwefFqW7uXTpVFGtpUyDMBNYs3DNvvXx6NPjF9YEbUQrtxFSWnPtVrv'
assert self.client.get_public_node([H_(9), 0], coin_name='Bitcoin').xpub == 'xpub6A2h5mzLDfYginoD7q7wCWbq18wTbN9gducRr2w5NRTwdLeoT3cJSwefFqW7uXTpVFGtpUyDMBNYs3DNvvXx6NPjF9YEbUQrtxFSWnPtVrv'
assert bip32.serialize(self.client.get_public_node([0, 9999999]).node, 0x0488B21E) == 'xpub6A3FoZqQEK6iwLZ4HFkqSo5fb35BH4bpjC4SPZ63prfLdGYPwYxEuC6o91bUvFFdMzKWe5rs3axHRUjxJaSvBnKKFtnfLwDACRxPxabsv2r'
assert self.client.get_public_node([0, 9999999], coin_name='Bitcoin').xpub == 'xpub6A3FoZqQEK6iwLZ4HFkqSo5fb35BH4bpjC4SPZ63prfLdGYPwYxEuC6o91bUvFFdMzKWe5rs3axHRUjxJaSvBnKKFtnfLwDACRxPxabsv2r'
@ -39,10 +41,10 @@ class TestMsgGetpublickey(TrezorTest):
assert self.client.get_public_node([], coin_name='Litecoin').xpub == 'Ltub2SSUS19CirucVPGDKDBatBDBEM2s9UbH66pBURfaKrMocCPLhQ7Z7hecy5VYLHA5fRdXwB2e61j2VJCNzVsqKTCVEU1vECjqi5EyczFX9xp'
assert bip32.serialize(self.client.get_public_node([1]).node, 0x019DA462) == 'Ltub2VRVRP5VjvSyPXra4BLVyVZPv397sjhUNjBGsbtw6xko77JuQyBULxFSKheviJJ3KQLbL3Cx8P2RnudguTw4raUVjCACRG7jsumUptYx55C'
assert self.client.get_public_node([1], coin_name='Litecoin').xpub == 'Ltub2VRVRP5VjvSyPXra4BLVyVZPv397sjhUNjBGsbtw6xko77JuQyBULxFSKheviJJ3KQLbL3Cx8P2RnudguTw4raUVjCACRG7jsumUptYx55C'
assert bip32.serialize(self.client.get_public_node([0, -1]).node, 0x019DA462) == 'Ltub2WUNGD3aRAKAqsLqHuwBYtCn2MqAXbVsarmvn33quWe2DCHTzfK4s4jsW5oM5G8RGAdSaM3NPNrwVvtV1ourbyNhhHr3BtqcYGc8caf5GoT'
assert self.client.get_public_node([0, -1], coin_name='Litecoin').xpub == 'Ltub2WUNGD3aRAKAqsLqHuwBYtCn2MqAXbVsarmvn33quWe2DCHTzfK4s4jsW5oM5G8RGAdSaM3NPNrwVvtV1ourbyNhhHr3BtqcYGc8caf5GoT'
assert bip32.serialize(self.client.get_public_node([-9, 0]).node, 0x019DA462) == 'Ltub2WToYRCN76rgyA59iK7w4Ni45wG2M9fpmBpQg7gBjvJeMiHc7473Gb96ci29Zvs55TgUQcMmCD1vy8aVqpdPwJB9YHRhGAAuPT1nRLLXmFu'
assert self.client.get_public_node([-9, 0], coin_name='Litecoin').xpub == 'Ltub2WToYRCN76rgyA59iK7w4Ni45wG2M9fpmBpQg7gBjvJeMiHc7473Gb96ci29Zvs55TgUQcMmCD1vy8aVqpdPwJB9YHRhGAAuPT1nRLLXmFu'
assert bip32.serialize(self.client.get_public_node([0, H_(1)]).node, 0x019DA462) == 'Ltub2WUNGD3aRAKAqsLqHuwBYtCn2MqAXbVsarmvn33quWe2DCHTzfK4s4jsW5oM5G8RGAdSaM3NPNrwVvtV1ourbyNhhHr3BtqcYGc8caf5GoT'
assert self.client.get_public_node([0, H_(1)], coin_name='Litecoin').xpub == 'Ltub2WUNGD3aRAKAqsLqHuwBYtCn2MqAXbVsarmvn33quWe2DCHTzfK4s4jsW5oM5G8RGAdSaM3NPNrwVvtV1ourbyNhhHr3BtqcYGc8caf5GoT'
assert bip32.serialize(self.client.get_public_node([H_(9), 0]).node, 0x019DA462) == 'Ltub2WToYRCN76rgyA59iK7w4Ni45wG2M9fpmBpQg7gBjvJeMiHc7473Gb96ci29Zvs55TgUQcMmCD1vy8aVqpdPwJB9YHRhGAAuPT1nRLLXmFu'
assert self.client.get_public_node([H_(9), 0], coin_name='Litecoin').xpub == 'Ltub2WToYRCN76rgyA59iK7w4Ni45wG2M9fpmBpQg7gBjvJeMiHc7473Gb96ci29Zvs55TgUQcMmCD1vy8aVqpdPwJB9YHRhGAAuPT1nRLLXmFu'
assert bip32.serialize(self.client.get_public_node([0, 9999999]).node, 0x019DA462) == 'Ltub2WUNGD3S7kQjBhpzsjkqJfBtfqPk2r7xrUGRDdqACMW3MeBCbZSyiqbEVt7WaeesxCj6EDFQtcbfXa75DUYN2i6jZ2g81cyCgvijs9J2u2n'
assert self.client.get_public_node([0, 9999999], coin_name='Litecoin').xpub == 'Ltub2WUNGD3S7kQjBhpzsjkqJfBtfqPk2r7xrUGRDdqACMW3MeBCbZSyiqbEVt7WaeesxCj6EDFQtcbfXa75DUYN2i6jZ2g81cyCgvijs9J2u2n'

View File

@ -184,7 +184,6 @@ class TestMsgNEMSignTxMosaics(TrezorTest):
def _nem_sign(self, num_of_swipes, test_suite):
n = parse_path("m/44'/1'/0'/0'/0'")
n = self.client._convert_prime(n)
msg = nem.create_sign_tx(test_suite)
assert msg.transaction is not None
msg.transaction.address_n = n