From 32e699efdb3dba696cb935607b4439eba4e62691 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Tue, 8 Nov 2016 20:25:55 +0100 Subject: [PATCH] trezor.crypto.der: convert -> encode small cleanup to new unit test --- src/apps/common/signtx.py | 3 +- src/trezor/crypto/der.py | 12 +++--- tests/test_apps_common.signtx.py | 26 ++++++------- tests/test_trezor.crypto.bip32.py | 64 +++++++++++++++--------------- tests/test_trezor.crypto.der.py | 65 ++++++++++++++++--------------- 5 files changed, 85 insertions(+), 85 deletions(-) diff --git a/src/apps/common/signtx.py b/src/apps/common/signtx.py index c5f76b4c99..6dad239291 100644 --- a/src/apps/common/signtx.py +++ b/src/apps/common/signtx.py @@ -320,8 +320,7 @@ def ecdsa_hash_pubkey(pubkey: bytes) -> bytes: def ecdsa_sign(node, digest: bytes) -> bytes: sig = secp256k1.sign(node.private_key(), digest) - print(len(sig)) - sigder = der.convert_seq((sig[:32], sig[32:])) + sigder = der.encode_seq((sig[:32], sig[32:])) return sigder diff --git a/src/trezor/crypto/der.py b/src/trezor/crypto/der.py index b5eef5246d..8ad0564d63 100644 --- a/src/trezor/crypto/der.py +++ b/src/trezor/crypto/der.py @@ -1,4 +1,4 @@ -def convert_length(l: int): +def encode_length(l: int): if l < 0x80: return bytes([l]) elif l <= 0xFF: @@ -8,14 +8,14 @@ def convert_length(l: int): else: raise ValueError -def convert_int(i: bytes): +def encode_int(i: bytes): i = i.lstrip(b'\x00') if i[0] >= 0x80: i = b'\x00' + i - return b'\x02' + convert_length(len(i)) + i + return b'\x02' + encode_length(len(i)) + i -def convert_seq(seq: tuple): +def encode_seq(seq: tuple): res = b'' for i in seq: - res += convert_int(i) - return b'\x30' + convert_length(len(res)) + res + res += encode_int(i) + return b'\x30' + encode_length(len(res)) + res diff --git a/tests/test_apps_common.signtx.py b/tests/test_apps_common.signtx.py index 102e8f1624..a8b2577a9a 100644 --- a/tests/test_apps_common.signtx.py +++ b/tests/test_apps_common.signtx.py @@ -24,18 +24,18 @@ class TestSignTx(unittest.TestCase): # input 0: 0.0039 BTC ptx1 = TransactionType(version=1, lock_time=0, inputs_cnt=2, outputs_cnt=1) - pinp1 = TxInputType(script_sig=unhexlify(b'483045022072ba61305fe7cb542d142b8f3299a7b10f9ea61f6ffaab5dca8142601869d53c0221009a8027ed79eb3b9bc13577ac2853269323434558528c6b6a7e542be46e7e9a820141047a2d177c0f3626fc68c53610b0270fa6156181f46586c679ba6a88b34c6f4874686390b4d92e5769fbb89c8050b984f4ec0b257a0e5c4ff8bd3b035a51709503'), - prev_hash=unhexlify(b'c16a03f1cf8f99f6b5297ab614586cacec784c2d259af245909dedb0e39eddcf'), + pinp1 = TxInputType(script_sig=unhexlify('483045022072ba61305fe7cb542d142b8f3299a7b10f9ea61f6ffaab5dca8142601869d53c0221009a8027ed79eb3b9bc13577ac2853269323434558528c6b6a7e542be46e7e9a820141047a2d177c0f3626fc68c53610b0270fa6156181f46586c679ba6a88b34c6f4874686390b4d92e5769fbb89c8050b984f4ec0b257a0e5c4ff8bd3b035a51709503'), + prev_hash=unhexlify('c16a03f1cf8f99f6b5297ab614586cacec784c2d259af245909dedb0e39eddcf'), prev_index=1) - pinp2 = TxInputType(script_sig=unhexlify(b'48304502200fd63adc8f6cb34359dc6cca9e5458d7ea50376cbd0a74514880735e6d1b8a4c0221008b6ead7fe5fbdab7319d6dfede3a0bc8e2a7c5b5a9301636d1de4aa31a3ee9b101410486ad608470d796236b003635718dfc07c0cac0cfc3bfc3079e4f491b0426f0676e6643a39198e8e7bdaffb94f4b49ea21baa107ec2e237368872836073668214'), - prev_hash=unhexlify(b'1ae39a2f8d59670c8fc61179148a8e61e039d0d9e8ab08610cb69b4a19453eaf'), + pinp2 = TxInputType(script_sig=unhexlify('48304502200fd63adc8f6cb34359dc6cca9e5458d7ea50376cbd0a74514880735e6d1b8a4c0221008b6ead7fe5fbdab7319d6dfede3a0bc8e2a7c5b5a9301636d1de4aa31a3ee9b101410486ad608470d796236b003635718dfc07c0cac0cfc3bfc3079e4f491b0426f0676e6643a39198e8e7bdaffb94f4b49ea21baa107ec2e237368872836073668214'), + prev_hash=unhexlify('1ae39a2f8d59670c8fc61179148a8e61e039d0d9e8ab08610cb69b4a19453eaf'), prev_index=1) - pout1 = TxOutputBinType(script_pubkey=unhexlify(b'76a91424a56db43cf6f2b02e838ea493f95d8d6047423188ac'), + pout1 = TxOutputBinType(script_pubkey=unhexlify('76a91424a56db43cf6f2b02e838ea493f95d8d6047423188ac'), amount=390000) inp1 = TxInputType(address_n=[0], # 14LmW5k4ssUrtbAB4255zdqv3b4w1TuX9e # amount=390000, - prev_hash=unhexlify(b'd5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882'), + prev_hash=unhexlify('d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882'), prev_index=0) out1 = TxOutputType(address='1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1', amount=390000 - 10000, @@ -46,13 +46,13 @@ class TestSignTx(unittest.TestCase): None, TxRequest(request_type=TXINPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None)), TxAck(tx=TransactionType(inputs=[inp1])), - TxRequest(request_type=TXMETA, details=TxRequestDetailsType(request_index=None, tx_hash=unhexlify(b"d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882")), serialized=None), + TxRequest(request_type=TXMETA, details=TxRequestDetailsType(request_index=None, tx_hash=unhexlify('d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882')), serialized=None), TxAck(tx=ptx1), - TxRequest(request_type=TXINPUT, details=TxRequestDetailsType(request_index=0, tx_hash=unhexlify(b"d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882")), serialized=None), + TxRequest(request_type=TXINPUT, details=TxRequestDetailsType(request_index=0, tx_hash=unhexlify('d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882')), serialized=None), TxAck(tx=TransactionType(inputs=[pinp1])), - TxRequest(request_type=TXINPUT, details=TxRequestDetailsType(request_index=1, tx_hash=unhexlify(b"d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882")), serialized=None), + TxRequest(request_type=TXINPUT, details=TxRequestDetailsType(request_index=1, tx_hash=unhexlify('d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882')), serialized=None), TxAck(tx=TransactionType(inputs=[pinp2])), - TxRequest(request_type=TXOUTPUT, details=TxRequestDetailsType(request_index=0, tx_hash=unhexlify(b"d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882")), serialized=None), + TxRequest(request_type=TXOUTPUT, details=TxRequestDetailsType(request_index=0, tx_hash=unhexlify('d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882')), serialized=None), TxAck(tx=TransactionType(bin_outputs=[pout1])), TxRequest(request_type=TXOUTPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None), serialized=None), TxAck(tx=TransactionType(outputs=[out1])), @@ -64,13 +64,13 @@ class TestSignTx(unittest.TestCase): TxAck(tx=TransactionType(outputs=[out1])), TxRequest(request_type=TXOUTPUT, details=TxRequestDetailsType(request_index=0, tx_hash=None), serialized=TxRequestSerializedType( signature_index=0, - signature=unhexlify(b'30450221009a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b7077102202b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede781'), - serialized_tx=unhexlify(b'010000000182488650ef25a58fef6788bd71b8212038d7f2bbe4750bc7bcb44701e85ef6d5000000006b4830450221009a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b7077102202b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede7810121023230848585885f63803a0a8aecdd6538792d5c539215c91698e315bf0253b43dffffffff'))), + signature=unhexlify('30450221009a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b7077102202b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede781'), + serialized_tx=unhexlify('010000000182488650ef25a58fef6788bd71b8212038d7f2bbe4750bc7bcb44701e85ef6d5000000006b4830450221009a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b7077102202b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede7810121023230848585885f63803a0a8aecdd6538792d5c539215c91698e315bf0253b43dffffffff'))), TxAck(tx=TransactionType(outputs=[out1])), TxRequest(request_type=TXFINISHED, details=None, serialized=TxRequestSerializedType( signature_index=None, signature=None, - serialized_tx=unhexlify(b'0160cc0500000000001976a914de9b2a8da088824e8fe51debea566617d851537888ac00000000'), + serialized_tx=unhexlify('0160cc0500000000001976a914de9b2a8da088824e8fe51debea566617d851537888ac00000000'), )), ] diff --git a/tests/test_trezor.crypto.bip32.py b/tests/test_trezor.crypto.bip32.py index ddc0d79f4b..fcb71ff1fc 100644 --- a/tests/test_trezor.crypto.bip32.py +++ b/tests/test_trezor.crypto.bip32.py @@ -16,7 +16,7 @@ class TestCryptoBip32(unittest.TestCase): bip32.from_seed(bytearray(), c) with self.assertRaises(TypeError): bip32.from_seed(1, c) - s = unhexlify("000102030405060708090a0b0c0d0e0f") + s = unhexlify('000102030405060708090a0b0c0d0e0f') with self.assertRaises(ValueError): bip32.from_seed(s, '') with self.assertRaises(ValueError): @@ -31,19 +31,19 @@ class TestCryptoBip32(unittest.TestCase): # test vector 1 from https://en.bitcoin.it/wiki/BIP_0032_TestVectors # init m - n = bip32.from_seed(unhexlify("000102030405060708090a0b0c0d0e0f"), SECP256K1_NAME) + n = bip32.from_seed(unhexlify('000102030405060708090a0b0c0d0e0f'), SECP256K1_NAME) # [Chain m] self.assertEqual(n.fingerprint(), 0x00000000) - self.assertEqual(n.chain_code(), unhexlify("873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508")) - self.assertEqual(n.private_key(), unhexlify("e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35")) - self.assertEqual(n.public_key(), unhexlify("0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2")) + self.assertEqual(n.chain_code(), unhexlify('873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508')) + self.assertEqual(n.private_key(), unhexlify('e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35')) + self.assertEqual(n.public_key(), unhexlify('0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2')) ns = n.serialize_private() - self.assertEqual(ns, "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi") + self.assertEqual(ns, 'xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi') ns2 = bip32.deserialize(ns).serialize_private() self.assertEqual(ns2, ns) ns = n.serialize_public() - self.assertEqual(ns, "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8") + self.assertEqual(ns, 'xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8') n2 = bip32.deserialize(ns) self.assertEqual(n2.private_key(), bytes(32)) ns2 = n2.serialize_public() @@ -52,15 +52,15 @@ class TestCryptoBip32(unittest.TestCase): # [Chain m/0'] n.derive(0x80000000 | 0) self.assertEqual(n.fingerprint(), 0x3442193e) - self.assertEqual(n.chain_code(), unhexlify("47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141")) - self.assertEqual(n.private_key(), unhexlify("edb2e14f9ee77d26dd93b4ecede8d16ed408ce149b6cd80b0715a2d911a0afea")) - self.assertEqual(n.public_key(), unhexlify("035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56")) + self.assertEqual(n.chain_code(), unhexlify('47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141')) + self.assertEqual(n.private_key(), unhexlify('edb2e14f9ee77d26dd93b4ecede8d16ed408ce149b6cd80b0715a2d911a0afea')) + self.assertEqual(n.public_key(), unhexlify('035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56')) ns = n.serialize_private() - self.assertEqual(ns, "xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7") + self.assertEqual(ns, 'xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7') ns2 = bip32.deserialize(ns).serialize_private() self.assertEqual(ns2, ns) ns = n.serialize_public() - self.assertEqual(ns, "xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw") + self.assertEqual(ns, 'xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw') n2 = bip32.deserialize(ns) self.assertEqual(n2.private_key(), bytes(32)) ns2 = n2.serialize_public() @@ -69,15 +69,15 @@ class TestCryptoBip32(unittest.TestCase): # [Chain m/0'/1] n.derive(1) self.assertEqual(n.fingerprint(), 0x5c1bd648) - self.assertEqual(n.chain_code(), unhexlify("2a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c19")) - self.assertEqual(n.private_key(), unhexlify("3c6cb8d0f6a264c91ea8b5030fadaa8e538b020f0a387421a12de9319dc93368")) - self.assertEqual(n.public_key(), unhexlify("03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c")) + self.assertEqual(n.chain_code(), unhexlify('2a7857631386ba23dacac34180dd1983734e444fdbf774041578e9b6adb37c19')) + self.assertEqual(n.private_key(), unhexlify('3c6cb8d0f6a264c91ea8b5030fadaa8e538b020f0a387421a12de9319dc93368')) + self.assertEqual(n.public_key(), unhexlify('03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c')) ns = n.serialize_private() - self.assertEqual(ns, "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs") + self.assertEqual(ns, 'xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs') ns2 = bip32.deserialize(ns).serialize_private() self.assertEqual(ns2, ns) ns = n.serialize_public() - self.assertEqual(ns, "xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ") + self.assertEqual(ns, 'xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ') n2 = bip32.deserialize(ns) self.assertEqual(n2.private_key(), bytes(32)) ns2 = n2.serialize_public() @@ -86,15 +86,15 @@ class TestCryptoBip32(unittest.TestCase): # [Chain m/0'/1/2'] n.derive(0x80000000 | 2) self.assertEqual(n.fingerprint(), 0xbef5a2f9) - self.assertEqual(n.chain_code(), unhexlify("04466b9cc8e161e966409ca52986c584f07e9dc81f735db683c3ff6ec7b1503f")) - self.assertEqual(n.private_key(), unhexlify("cbce0d719ecf7431d88e6a89fa1483e02e35092af60c042b1df2ff59fa424dca")) - self.assertEqual(n.public_key(), unhexlify("0357bfe1e341d01c69fe5654309956cbea516822fba8a601743a012a7896ee8dc2")) + self.assertEqual(n.chain_code(), unhexlify('04466b9cc8e161e966409ca52986c584f07e9dc81f735db683c3ff6ec7b1503f')) + self.assertEqual(n.private_key(), unhexlify('cbce0d719ecf7431d88e6a89fa1483e02e35092af60c042b1df2ff59fa424dca')) + self.assertEqual(n.public_key(), unhexlify('0357bfe1e341d01c69fe5654309956cbea516822fba8a601743a012a7896ee8dc2')) ns = n.serialize_private() - self.assertEqual(ns, "xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM") + self.assertEqual(ns, 'xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM') ns2 = bip32.deserialize(ns).serialize_private() self.assertEqual(ns2, ns) ns = n.serialize_public() - self.assertEqual(ns, "xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5") + self.assertEqual(ns, 'xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5') n2 = bip32.deserialize(ns) self.assertEqual(n2.private_key(), bytes(32)) ns2 = n2.serialize_public() @@ -103,15 +103,15 @@ class TestCryptoBip32(unittest.TestCase): # [Chain m/0'/1/2'/2] n.derive(2) self.assertEqual(n.fingerprint(), 0xee7ab90c) - self.assertEqual(n.chain_code(), unhexlify("cfb71883f01676f587d023cc53a35bc7f88f724b1f8c2892ac1275ac822a3edd")) - self.assertEqual(n.private_key(), unhexlify("0f479245fb19a38a1954c5c7c0ebab2f9bdfd96a17563ef28a6a4b1a2a764ef4")) - self.assertEqual(n.public_key(), unhexlify("02e8445082a72f29b75ca48748a914df60622a609cacfce8ed0e35804560741d29")) + self.assertEqual(n.chain_code(), unhexlify('cfb71883f01676f587d023cc53a35bc7f88f724b1f8c2892ac1275ac822a3edd')) + self.assertEqual(n.private_key(), unhexlify('0f479245fb19a38a1954c5c7c0ebab2f9bdfd96a17563ef28a6a4b1a2a764ef4')) + self.assertEqual(n.public_key(), unhexlify('02e8445082a72f29b75ca48748a914df60622a609cacfce8ed0e35804560741d29')) ns = n.serialize_private() - self.assertEqual(ns, "xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334") + self.assertEqual(ns, 'xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334') ns2 = bip32.deserialize(ns).serialize_private() self.assertEqual(ns2, ns) ns = n.serialize_public() - self.assertEqual(ns, "xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV") + self.assertEqual(ns, 'xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV') n2 = bip32.deserialize(ns) self.assertEqual(n2.private_key(), bytes(32)) ns2 = n2.serialize_public() @@ -120,15 +120,15 @@ class TestCryptoBip32(unittest.TestCase): # [Chain m/0'/1/2'/2/1000000000] n.derive(1000000000) self.assertEqual(n.fingerprint(), 0xd880d7d8) - self.assertEqual(n.chain_code(), unhexlify("c783e67b921d2beb8f6b389cc646d7263b4145701dadd2161548a8b078e65e9e")) - self.assertEqual(n.private_key(), unhexlify("471b76e389e528d6de6d816857e012c5455051cad6660850e58372a6c3e6e7c8")) - self.assertEqual(n.public_key(), unhexlify("022a471424da5e657499d1ff51cb43c47481a03b1e77f951fe64cec9f5a48f7011")) + self.assertEqual(n.chain_code(), unhexlify('c783e67b921d2beb8f6b389cc646d7263b4145701dadd2161548a8b078e65e9e')) + self.assertEqual(n.private_key(), unhexlify('471b76e389e528d6de6d816857e012c5455051cad6660850e58372a6c3e6e7c8')) + self.assertEqual(n.public_key(), unhexlify('022a471424da5e657499d1ff51cb43c47481a03b1e77f951fe64cec9f5a48f7011')) ns = n.serialize_private() - self.assertEqual(ns, "xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76") + self.assertEqual(ns, 'xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76') ns2 = bip32.deserialize(ns).serialize_private() self.assertEqual(ns2, ns) ns = n.serialize_public() - self.assertEqual(ns, "xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy") + self.assertEqual(ns, 'xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy') n2 = bip32.deserialize(ns) self.assertEqual(n2.private_key(), bytes(32)) ns2 = n2.serialize_public() diff --git a/tests/test_trezor.crypto.der.py b/tests/test_trezor.crypto.der.py index 6bf9d1088e..5486c13f77 100644 --- a/tests/test_trezor.crypto.der.py +++ b/tests/test_trezor.crypto.der.py @@ -4,49 +4,50 @@ from trezor.crypto import der class TestCryptoDer(unittest.TestCase): - vectors_sig = [ - ("9a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b70771", - "2b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede781", - "30450221009a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b7077102202b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede781"), + vectors_seq = [ + (('9a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b70771', + '2b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede781'), + '30450221009a0b7be0d4ed3146ee262b42202841834698bb3ee39c24e7437df208b8b7077102202b79ab1e7736219387dffe8d615bbdba87e11477104b867ef47afed1a5ede781'), - ("6666666666666666666666666666666666666666666666666666666666666666", - "7777777777777777777777777777777777777777777777777777777777777777", - "30440220666666666666666666666666666666666666666666666666666666666666666602207777777777777777777777777777777777777777777777777777777777777777"), + (('6666666666666666666666666666666666666666666666666666666666666666', + '7777777777777777777777777777777777777777777777777777777777777777'), + '30440220666666666666666666666666666666666666666666666666666666666666666602207777777777777777777777777777777777777777777777777777777777777777'), - ("6666666666666666666666666666666666666666666666666666666666666666", - "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", - "304502206666666666666666666666666666666666666666666666666666666666666666022100eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"), + (('6666666666666666666666666666666666666666666666666666666666666666', + 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'), + '304502206666666666666666666666666666666666666666666666666666666666666666022100eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'), - ("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", - "7777777777777777777777777777777777777777777777777777777777777777", - "3045022100eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee02207777777777777777777777777777777777777777777777777777777777777777"), + (('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', + '7777777777777777777777777777777777777777777777777777777777777777'), + '3045022100eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee02207777777777777777777777777777777777777777777777777777777777777777'), - ("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", - "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - "3046022100eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee022100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), + (('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', + 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'), + '3046022100eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee022100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'), - ("0000000000000000000000000000000000000000000000000000000000000066", - "0000000000000000000000000000000000000000000000000000000000000077", - "3006020166020177"), + (('0000000000000000000000000000000000000000000000000000000000000066', + '0000000000000000000000000000000000000000000000000000000000000077'), + '3006020166020177'), - ("0000000000000000000000000000000000000000000000000000000000000066", - "00000000000000000000000000000000000000000000000000000000000000ee", - "3007020166020200ee"), + (('0000000000000000000000000000000000000000000000000000000000000066', + '00000000000000000000000000000000000000000000000000000000000000ee'), + '3007020166020200ee'), - ("00000000000000000000000000000000000000000000000000000000000000ee", - "0000000000000000000000000000000000000000000000000000000000000077", - "3007020200ee020177"), + (('00000000000000000000000000000000000000000000000000000000000000ee', + '0000000000000000000000000000000000000000000000000000000000000077'), + '3007020200ee020177'), - ("00000000000000000000000000000000000000000000000000000000000000ee", - "00000000000000000000000000000000000000000000000000000000000000ff", - "3008020200ee020200ff"), + (('00000000000000000000000000000000000000000000000000000000000000ee', + '00000000000000000000000000000000000000000000000000000000000000ff'), + '3008020200ee020200ff'), ] - def test_encode_sig_der(self): + def test_der_encode_seq(self): - for r, s, d in self.vectors_sig: - r, s, d = unhexlify(r), unhexlify(s), unhexlify(d) - d2 = der.convert_seq((r,s)) + for s, d in self.vectors_seq: + s = (unhexlify(i) for i in s) + d = unhexlify(d) + d2 = der.encode_seq(s) self.assertEqual(d, d2) if __name__ == '__main__':