async def get_public_key(ctx, msg, keychain): await paths.validate_path( ctx, address.validate_path_for_get_public_key, keychain, msg.address_n ) node = keychain.derive(msg.address_n) # we use the Bitcoin format for Ethereum xpubs btc = coins.by_name("Bitcoin") node_xpub = node.serialize_public(btc.xpub_magic) pubkey = node.public_key() if pubkey[0] == 1: pubkey = b"\x00" + pubkey[1:] node_type = HDNodeType( depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=pubkey, ) if msg.show_display: await layout.show_pubkey(ctx, pubkey) return EthereumPublicKey(node=node_type, xpub=node_xpub)
async def get_public_key(ctx, msg): coin_name = msg.coin_name or 'Bitcoin' curve_name = msg.ecdsa_curve_name if not curve_name: node = await seed.derive_node(ctx, msg.address_n) else: node = await seed.derive_node(ctx, msg.address_n, curve_name=curve_name) coin = coins.by_name(coin_name) node_xpub = node.serialize_public(coin.xpub_magic) pubkey = node.public_key() if pubkey[0] == 1: pubkey = b'\x00' + pubkey[1:] node_type = HDNodeType(depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=pubkey) if msg.show_display: await _show_pubkey(ctx, pubkey) return PublicKey(node=node_type, xpub=node_xpub)
def test_p2sh_gen_proof(self): coin = coins.by_name('Bitcoin') seed = bip39.seed(' '.join(['all'] * 12), '') keychain = Keychain(seed, [[coin.curve_name, [48 | HARDENED]], ["slip21", [b"SLIP-0019"]]]) commitment_data = b"TREZOR" nodes = [] for index in range(1, 3): node = keychain.derive([48 | HARDENED, 0 | HARDENED, index | HARDENED]) nodes.append(HDNodeType( depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=node.public_key(), )) multisig = MultisigRedeemScriptType( nodes=nodes, address_n=[0, 0], signatures=[b"", b""], m=2, ) pubkeys = multisig_get_pubkeys(multisig) address = address_multisig_p2sh(pubkeys, multisig.m, coin) script_pubkey = scripts.output_derive_script(address, coin) ownership_id = ownership.get_identifier(script_pubkey, keychain) ownership_ids = [b'\x00' * 32, ownership_id] self.assertEqual(ownership_id, unhexlify("ce4ee8298ad105c3495a1d2b620343133521ab34de2450deeb32eec39475fef4")) # Sign with the first key. _, signature = ownership.generate_proof( node=keychain.derive([48 | HARDENED, 0 | HARDENED, 1 | HARDENED, 0, 0]), script_type=InputScriptType.SPENDMULTISIG, multisig=multisig, coin=coin, user_confirmed=False, ownership_ids=ownership_ids, script_pubkey=script_pubkey, commitment_data=commitment_data, ) self.assertEqual(signature, unhexlify("3045022100bc63486f167b911dc8ef2414c4bca6dcfac999797b67159957802a9c49c2179402201cec0d53fee78fcfde496e30be35bd855d93a5be89604c55dcfdbdc515fbb41a")) multisig.signatures[0] = signature # Sign with the third key. proof, signature = ownership.generate_proof( node=keychain.derive([48 | HARDENED, 0 | HARDENED, 2 | HARDENED, 0, 0]), script_type=InputScriptType.SPENDMULTISIG, multisig=multisig, coin=coin, user_confirmed=False, ownership_ids=ownership_ids, script_pubkey=script_pubkey, commitment_data=commitment_data, ) self.assertEqual(signature, unhexlify("3045022100d9d5966eb7858cc1a600a9c05be252c1df11d662f319a107d04e219a27c1386c02200674523e50e89164d6d5683dfbe9a50594b08011e11c18813b56cf855755afde")) self.assertEqual(proof, unhexlify("534c001900020000000000000000000000000000000000000000000000000000000000000000ce4ee8298ad105c3495a1d2b620343133521ab34de2450deeb32eec39475fef4db00483045022100bc63486f167b911dc8ef2414c4bca6dcfac999797b67159957802a9c49c2179402201cec0d53fee78fcfde496e30be35bd855d93a5be89604c55dcfdbdc515fbb41a01483045022100d9d5966eb7858cc1a600a9c05be252c1df11d662f319a107d04e219a27c1386c02200674523e50e89164d6d5683dfbe9a50594b08011e11c18813b56cf855755afde014752210203ed6187880ae932660086e55d4561a57952dd200aa3ed2aa66b73e5723a0ce7210360e7f32fd3c8dee27a166f6614c598929699ee66acdcbda5fb24571bf2ae1ca052ae00")) self.assertFalse(ownership.verify_nonownership(proof, script_pubkey, commitment_data, keychain, coin))
def test_p2wsh_gen_proof(self): coin = coins.by_name('Bitcoin') seed = bip39.seed(' '.join(['all'] * 12), '') keychain = Keychain(seed, [[coin.curve_name, [84 | HARDENED]], ["slip21", [b"SLIP-0019"]]]) commitment_data = b"TREZOR" nodes = [] for index in range(1, 4): node = keychain.derive([84 | HARDENED, 0 | HARDENED, index | HARDENED]) nodes.append(HDNodeType( depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=node.public_key(), )) multisig = MultisigRedeemScriptType( nodes=nodes, address_n=[0, 1], signatures=[b"", b"", b""], m=2, ) pubkeys = multisig_get_pubkeys(multisig) address = address_multisig_p2wsh(pubkeys, multisig.m, coin.bech32_prefix) script_pubkey = scripts.output_derive_script(address, coin) ownership_id = ownership.get_identifier(script_pubkey, keychain) ownership_ids = [b'\x00' * 32, ownership_id, b'\x01' * 32] self.assertEqual(ownership_id, unhexlify("9c27411da79a23811856f897da890452ab9e17086038c4a3e70e9efa875cb3ef")) # Sign with the first key. _, signature = ownership.generate_proof( node=keychain.derive([84 | HARDENED, 0 | HARDENED, 1 | HARDENED, 0, 1]), script_type=InputScriptType.SPENDWITNESS, multisig=multisig, coin=coin, user_confirmed=False, ownership_ids=ownership_ids, script_pubkey=script_pubkey, commitment_data=commitment_data, ) self.assertEqual(signature, unhexlify("304402207568cf003ff548c52ce8e3a46a1c1e681462ca8f1651b0c82f688d41280753b4022024f977fa96fd23cf71e35d4d3c5087c375fcf1b6eed6d11ab00d552817d39ba4")) multisig.signatures[0] = signature # Sign with the third key. proof, signature = ownership.generate_proof( node=keychain.derive([84 | HARDENED, 0 | HARDENED, 3 | HARDENED, 0, 1]), script_type=InputScriptType.SPENDWITNESS, multisig=multisig, coin=coin, user_confirmed=False, ownership_ids=ownership_ids, script_pubkey=script_pubkey, commitment_data=commitment_data, ) self.assertEqual(signature, unhexlify("304402203c4fedba34aebd213aba5b5af1ae26240a10a05cfc1c5b75c629275aa21560bb02203b90b4079c20f792f4ec533c72af31435b1e5f648ca8302730c309690133a710")) self.assertEqual(proof, unhexlify("534c0019000300000000000000000000000000000000000000000000000000000000000000009c27411da79a23811856f897da890452ab9e17086038c4a3e70e9efa875cb3ef010101010101010101010101010101010101010101010101010101010101010100040047304402207568cf003ff548c52ce8e3a46a1c1e681462ca8f1651b0c82f688d41280753b4022024f977fa96fd23cf71e35d4d3c5087c375fcf1b6eed6d11ab00d552817d39ba40147304402203c4fedba34aebd213aba5b5af1ae26240a10a05cfc1c5b75c629275aa21560bb02203b90b4079c20f792f4ec533c72af31435b1e5f648ca8302730c309690133a71001695221022aff3e39acd2d510c661e097a9657962ad6bf75a977c2c905152d2eb2cd58c7b210241ec073f3bb3f701a87b78fbc5f7b4daec140b87da38303173eddd0860ac55e321030205585a3eb01cbebbbb7b9138f7796117cca8e30eba5cd143ff4e3e617d221553ae")) self.assertFalse(ownership.verify_nonownership(proof, script_pubkey, commitment_data, keychain, coin))
async def get_public_key(ctx, msg): script_type = msg.script_type or InputScriptType.SPENDADDRESS if msg.ecdsa_curve_name is not None: # If a curve name is provided, disallow coin-specific features. if ( msg.coin_name is not None or msg.script_type is not InputScriptType.SPENDADDRESS ): raise wire.DataError( "Cannot use coin_name or script_type with ecdsa_curve_name" ) coin = coins.by_name("Bitcoin") # only allow SLIP-13/17 namespaces keychain = await get_keychain_for_curve(ctx, msg.ecdsa_curve_name) else: # select curve and namespaces based on the requested coin properties keychain, coin = await get_keychain_for_coin(ctx, msg.coin_name) node = keychain.derive(msg.address_n) if ( script_type in [InputScriptType.SPENDADDRESS, InputScriptType.SPENDMULTISIG] and coin.xpub_magic is not None ): node_xpub = node.serialize_public(coin.xpub_magic) elif ( coin.segwit and script_type == InputScriptType.SPENDP2SHWITNESS and coin.xpub_magic_segwit_p2sh is not None ): node_xpub = node.serialize_public(coin.xpub_magic_segwit_p2sh) elif ( coin.segwit and script_type == InputScriptType.SPENDWITNESS and coin.xpub_magic_segwit_native is not None ): node_xpub = node.serialize_public(coin.xpub_magic_segwit_native) else: raise wire.DataError("Invalid combination of coin and script_type") pubkey = node.public_key() if pubkey[0] == 1: pubkey = b"\x00" + pubkey[1:] node_type = HDNodeType( depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=pubkey, ) if msg.show_display: await layout.show_pubkey(ctx, pubkey) return PublicKey(node=node_type, xpub=node_xpub)
async def get_public_key(ctx: wire.Context, msg: GetPublicKey) -> PublicKey: coin_name = msg.coin_name or "Bitcoin" script_type = msg.script_type or InputScriptType.SPENDADDRESS coin = coininfo.by_name(coin_name) curve_name = msg.ecdsa_curve_name or coin.curve_name keychain = await get_keychain(ctx, curve_name, [paths.AlwaysMatchingSchema]) node = keychain.derive(msg.address_n) if ( script_type in (InputScriptType.SPENDADDRESS, InputScriptType.SPENDMULTISIG) and coin.xpub_magic is not None ): node_xpub = node.serialize_public(coin.xpub_magic) elif ( coin.segwit and script_type == InputScriptType.SPENDP2SHWITNESS and (msg.ignore_xpub_magic or coin.xpub_magic_segwit_p2sh is not None) ): # TODO: resolve type: ignore below node_xpub = node.serialize_public( coin.xpub_magic if msg.ignore_xpub_magic else coin.xpub_magic_segwit_p2sh # type: ignore ) elif ( coin.segwit and script_type == InputScriptType.SPENDWITNESS and (msg.ignore_xpub_magic or coin.xpub_magic_segwit_native is not None) ): # TODO: resolve type: ignore below node_xpub = node.serialize_public( coin.xpub_magic if msg.ignore_xpub_magic else coin.xpub_magic_segwit_native # type: ignore ) else: raise wire.DataError("Invalid combination of coin and script_type") pubkey = node.public_key() if pubkey[0] == 1: pubkey = b"\x00" + pubkey[1:] node_type = HDNodeType( depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=pubkey, ) if msg.show_display: from trezor.ui.layouts import show_xpub await show_xpub(ctx, node_xpub, "XPUB", "Cancel") return PublicKey( node=node_type, xpub=node_xpub, root_fingerprint=keychain.root_fingerprint(), )
async def get_public_key(ctx, msg, keychain): await paths.validate_path(ctx, validate_full_path, keychain, msg.address_n, CURVE) node_type = HDNodeType( depth=0, child_num=0, fingerprint=0, chain_code=bytes(0), public_key=bytes(0), ) return PolisPublicKey(node=node_type, xpub="xpub")
async def get_public_key(ctx, msg): coin_name = msg.coin_name or 'Bitcoin' node = await seed.derive_node(ctx, msg.address_n) coin = coins.by_name(coin_name) node_xpub = node.serialize_public(coin.xpub_magic) node_type = HDNodeType(depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=node.public_key()) return PublicKey(node=node_type, xpub=node_xpub)
async def layout_get_public_key(session_id, msg): from trezor.messages.HDNodeType import HDNodeType from trezor.messages.PublicKey import PublicKey from ..common import seed node = await seed.get_root(session_id) node.derive_path(msg.address_n or ()) node_xpub = node.serialize_public() node_type = HDNodeType(depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=node.public_key()) return PublicKey(node=node_type, xpub=node_xpub)
def _get_public_key(keychain, derivation_path: list): _, node = derive_address_and_node(keychain, derivation_path) public_key = hexlify(remove_ed25519_prefix(node.public_key())).decode() chain_code = hexlify(node.chain_code()).decode() xpub_key = public_key + chain_code node_type = HDNodeType( depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=remove_ed25519_prefix(node.public_key()), ) return CardanoPublicKey(node=node_type, xpub=xpub_key)
def _get_public_key(keychain: seed.Keychain, derivation_path: list[int]) -> CardanoPublicKey: node = keychain.derive(derivation_path) public_key = hexlify(derive_public_key(keychain, derivation_path)).decode() chain_code = hexlify(node.chain_code()).decode() xpub_key = public_key + chain_code node_type = HDNodeType( depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=derive_public_key(keychain, derivation_path), ) return CardanoPublicKey(node=node_type, xpub=xpub_key)
async def layout_get_public_key(ctx, msg): from trezor.messages.HDNodeType import HDNodeType from trezor.messages.PublicKey import PublicKey from ..common import coins from ..common import seed address_n = msg.address_n or () coin_name = msg.coin_name or 'Bitcoin' node = await seed.derive_node(ctx, address_n) coin = coins.by_name(coin_name) node_xpub = node.serialize_public(coin.xpub_magic) node_type = HDNodeType( depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=node.public_key()) return PublicKey(node=node_type, xpub=node_xpub)
def _get_public_key(root_node, derivation_path: list): _, node = derive_address_and_node(root_node, derivation_path) public_key = hexlify(seed.remove_ed25519_prefix( node.public_key())).decode() chain_code = hexlify(node.chain_code()).decode() xpub_key = public_key + chain_code root_hd_passphrase = hexlify(_derive_hd_passphrase(root_node)).decode() node_type = HDNodeType( depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=seed.remove_ed25519_prefix(node.public_key()), ) return CardanoPublicKey(node=node_type, xpub=xpub_key, root_hd_passphrase=root_hd_passphrase)
async def get_public_key(ctx: wire.Context, msg: GetPublicKey) -> PublicKey: coin_name = msg.coin_name or "Bitcoin" script_type = msg.script_type or InputScriptType.SPENDADDRESS coin = coins.by_name(coin_name) curve_name = msg.ecdsa_curve_name or coin.curve_name keychain = await get_keychain(ctx, curve_name, [paths.AlwaysMatchingSchema]) node = keychain.derive(msg.address_n) if (script_type in [InputScriptType.SPENDADDRESS, InputScriptType.SPENDMULTISIG] and coin.xpub_magic is not None): node_xpub = node.serialize_public(coin.xpub_magic) elif (coin.segwit and script_type == InputScriptType.SPENDP2SHWITNESS and coin.xpub_magic_segwit_p2sh is not None): node_xpub = node.serialize_public(coin.xpub_magic_segwit_p2sh) elif (coin.segwit and script_type == InputScriptType.SPENDWITNESS and coin.xpub_magic_segwit_native is not None): node_xpub = node.serialize_public(coin.xpub_magic_segwit_native) else: raise wire.DataError("Invalid combination of coin and script_type") pubkey = node.public_key() if pubkey[0] == 1: pubkey = b"\x00" + pubkey[1:] node_type = HDNodeType( depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=pubkey, ) if msg.show_display: await layout.show_pubkey(ctx, pubkey) return PublicKey(node=node_type, xpub=node_xpub)
async def get_public_key(ctx, msg): coin_name = msg.coin_name or "Bitcoin" coin = coins.by_name(coin_name) script_type = msg.script_type or InputScriptType.SPENDADDRESS curve_name = msg.ecdsa_curve_name if not curve_name: curve_name = coin.curve_name node = await seed.derive_node(ctx, msg.address_n, curve_name=curve_name) if (script_type in [InputScriptType.SPENDADDRESS, InputScriptType.SPENDMULTISIG] and coin.xpub_magic is not None): node_xpub = node.serialize_public(coin.xpub_magic) elif (coin.segwit and script_type == InputScriptType.SPENDP2SHWITNESS and coin.xpub_magic_segwit_p2sh is not None): node_xpub = node.serialize_public(coin.xpub_magic_segwit_p2sh) elif (coin.segwit and script_type == InputScriptType.SPENDWITNESS and coin.xpub_magic_segwit_native is not None): node_xpub = node.serialize_public(coin.xpub_magic_segwit_native) else: raise wire.DataError("Invalid combination of coin and script_type") pubkey = node.public_key() if pubkey[0] == 1: pubkey = b"\x00" + pubkey[1:] node_type = HDNodeType( depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=pubkey, ) if msg.show_display: await layout.show_pubkey(ctx, pubkey) return PublicKey(node=node_type, xpub=node_xpub)
def test_p2wsh_in_p2sh_gen_proof(self): coin = coins.by_name('Bitcoin') seed = bip39.seed(' '.join(['all'] * 12), '') keychain = Keychain(seed, [[coin.curve_name, [49 | HARDENED]], ["slip21", [b"SLIP-0019"]]]) commitment_data = b"" nodes = [] for index in range(1, 6): node = keychain.derive([49 | HARDENED, 0 | HARDENED, index | HARDENED]) nodes.append(HDNodeType( depth=node.depth(), child_num=node.child_num(), fingerprint=node.fingerprint(), chain_code=node.chain_code(), public_key=node.public_key(), )) multisig = MultisigRedeemScriptType( nodes=nodes, address_n=[0, 1], signatures=[b"", b"", b"", b"", b""], m=3, ) pubkeys = multisig_get_pubkeys(multisig) address = address_multisig_p2wsh_in_p2sh(pubkeys, multisig.m, coin) script_pubkey = scripts.output_derive_script(address, coin) ownership_id = ownership.get_identifier(script_pubkey, keychain) ownership_ids = [b'\x00' * 32, b'\x01' * 32, b'\x02' * 32, ownership_id] self.assertEqual(ownership_id, unhexlify("66f99db388dfa7ae137f7bdb5f0004b4d6968014921cfaff1fec042e3bb83ae0")) # Sign with the second key. _, signature = ownership.generate_proof( node=keychain.derive([49 | HARDENED, 0 | HARDENED, 2 | HARDENED, 0, 1]), script_type=InputScriptType.SPENDP2SHWITNESS, multisig=multisig, coin=coin, user_confirmed=False, ownership_ids=ownership_ids, script_pubkey=script_pubkey, commitment_data=commitment_data, ) self.assertEqual(signature, unhexlify("3045022100deccf7735da7a8236efd59d5759c4cbe9fa32d567bcd57d8d718cc689bc6972402202ce7fe49b0f0caea049be69c91bca9c9397d693d79388f1cfb65d51deadfb3d8")) multisig.signatures[1] = signature # Sign with the fourth key. proof, signature = ownership.generate_proof( node=keychain.derive([49 | HARDENED, 0 | HARDENED, 4 | HARDENED, 0, 1]), script_type=InputScriptType.SPENDP2SHWITNESS, multisig=multisig, coin=coin, user_confirmed=False, ownership_ids=ownership_ids, script_pubkey=script_pubkey, commitment_data=commitment_data, ) self.assertEqual(signature, unhexlify("304402206e8219a013e94de493c4ff50b44d31f443d37a2c4dbcba6af1ac825b28cc631202200741a72035acd122a6f4fdb994c15ab19aa20cecdfdb19aa37490e7bb011a617")) multisig.signatures[3] = signature # Sign with the fifth key. proof, signature = ownership.generate_proof( node=keychain.derive([49 | HARDENED, 0 | HARDENED, 5 | HARDENED, 0, 1]), script_type=InputScriptType.SPENDP2SHWITNESS, multisig=multisig, coin=coin, user_confirmed=False, ownership_ids=ownership_ids, script_pubkey=script_pubkey, commitment_data=commitment_data, ) self.assertEqual(signature, unhexlify("304402202f6066733abf4671b74f1f883dd3c8d4810aa71b7b7b5f6196b1ceff83d5370e022053aad3bde0fe6ce6c4553dd72ddf07e7f06447a7bd35edf6f0b4e9690ee7ce79")) self.assertEqual(proof, unhexlify("534c0019000400000000000000000000000000000000000000000000000000000000000000000101010101010101010101010101010101010101010101010101010101010101020202020202020202020202020202020202020202020202020202020202020266f99db388dfa7ae137f7bdb5f0004b4d6968014921cfaff1fec042e3bb83ae0232200208c256ed80a97a421656daa1468f6d4d43f475cb52ed79532d8bcb315518298120500483045022100deccf7735da7a8236efd59d5759c4cbe9fa32d567bcd57d8d718cc689bc6972402202ce7fe49b0f0caea049be69c91bca9c9397d693d79388f1cfb65d51deadfb3d80147304402206e8219a013e94de493c4ff50b44d31f443d37a2c4dbcba6af1ac825b28cc631202200741a72035acd122a6f4fdb994c15ab19aa20cecdfdb19aa37490e7bb011a6170147304402202f6066733abf4671b74f1f883dd3c8d4810aa71b7b7b5f6196b1ceff83d5370e022053aad3bde0fe6ce6c4553dd72ddf07e7f06447a7bd35edf6f0b4e9690ee7ce7901ad5321032922ce9b0b71ae2d2d8a7f239610ae8226e0fb8c0f445ec4c88cf9aa4787f44b21028373a1cdb9a1afbc67e57f75eeea1f53e7210ae8ec4b3441a5f2bc4a250b663c21028ab4c06e3ad19053b370eff097697d4cb6d3738712ebcdcdc27c58a5639ac3aa2103e3247fab300aeba459257e4605245f85378ecbfe092ca3bc55ec1259baa456f521023b0d8d97398d97c4dba10f788344abd4bd1058ad3959724d32079ad04bdbde8a55ae")) self.assertFalse(ownership.verify_nonownership(proof, script_pubkey, commitment_data, keychain, coin))