1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 09:28:13 +00:00

src/apps/ethereum: implement RSKIP-60 checksum in get_address

This commit is contained in:
Ilan 2018-06-13 13:37:51 -03:00 committed by Pavol Rusnak
parent 0a6d890335
commit 0c13a43518
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 39 additions and 3 deletions

View File

@ -1,4 +1,5 @@
from apps.wallet.get_address import _show_address, _show_qr
from apps.ethereum import networks
async def ethereum_get_address(ctx, msg):
@ -16,7 +17,9 @@ async def ethereum_get_address(ctx, msg):
address = sha3_256(public_key[1:]).digest(True)[12:] # Keccak
if msg.show_display:
hex_addr = _ethereum_address_hex(address)
network = networks.by_slip44(address_n[1] & 0x7fffffff)
hex_addr = _ethereum_address_hex(address, network)
while True:
if await _show_address(ctx, hex_addr):
break
@ -26,12 +29,16 @@ async def ethereum_get_address(ctx, msg):
return EthereumAddress(address=address)
def _ethereum_address_hex(address):
def _ethereum_address_hex(address, network=None):
from ubinascii import hexlify
from trezor.crypto.hashlib import sha3_256
rskip60 = network is not None and network.rskip60
hx = hexlify(address).decode()
hs = sha3_256(hx).digest(True)
prefix = str(network.chain_id) + '|' if rskip60 else ''
hs = sha3_256(prefix + hx).digest(True)
h = ''
for i in range(20):

View File

@ -1,5 +1,6 @@
from common import *
from apps.ethereum.get_address import _ethereum_address_hex
from apps.ethereum.networks import NetworkInfo
class TestEthereumGetAddress(unittest.TestCase):
@ -23,5 +24,33 @@ class TestEthereumGetAddress(unittest.TestCase):
self.assertEqual(h, '0x' + s)
def test_ethereum_address_hex_rskip60(self):
# https://github.com/rsksmart/RSKIPs/blob/master/IPs/RSKIP60.md
rskip60_chain_30 = [
'0x5AaEb6053f3e94C9B9A09f33669435e7Ef1BeaeD',
'0xFb6916095ca1dF60BB79Ce92cE3EA74C37C5D359',
'0xdbf03b407C01e7cd3CbEA99509D93F8Dddc8c6fB',
'0xD1220A0cF47C7B9bE7a2E6ba89F429762e7b9aDB'
]
rskip60_chain_31 = [
'0x5AAEb6053f3E94c9B9A09f33669435e7EF1BeaeD',
'0xfB6916095CA1Df60bb79ce92CE3Ea74c37C5D359',
'0xDBF03B407C01E7Cd3cBEa99509d93f8DddC8C6Fb',
'0xd1220a0cf47C7b9be7A2e6BA89f429762e7b9AdB'
]
n = NetworkInfo(chain_id=30, slip44=1, shortcut='T', name='T', rskip60=True)
for s in rskip60_chain_30:
s = s[2:]
b = bytes([int(s[i:i + 2], 16) for i in range(0, len(s), 2)])
h = _ethereum_address_hex(b, n)
self.assertEqual(h, '0x' + s)
n.chain_id = 31
for s in rskip60_chain_31:
s = s[2:]
b = bytes([int(s[i:i + 2], 16) for i in range(0, len(s), 2)])
h = _ethereum_address_hex(b, n)
self.assertEqual(h, '0x' + s)
if __name__ == '__main__':
unittest.main()