def get_coin_by_name(coin_name: Optional[str]) -> coininfo.CoinInfo: if coin_name is None: coin_name = "Bitcoin" try: return coininfo.by_name(coin_name) except ValueError: raise wire.DataError("Unsupported coin type")
async def sign_identity(ctx: wire.Context, msg: SignIdentity) -> SignedIdentity: if msg.ecdsa_curve_name is None: msg.ecdsa_curve_name = "secp256k1" keychain = await get_keychain(ctx, msg.ecdsa_curve_name, [AlwaysMatchingSchema]) identity = serialize_identity(msg.identity) await require_confirm_sign_identity(ctx, msg.identity, msg.challenge_visual) address_n = get_identity_path(identity, msg.identity.index or 0) node = keychain.derive(address_n) coin = coininfo.by_name("Bitcoin") if msg.ecdsa_curve_name == "secp256k1": # hardcoded bitcoin address type address: Optional[str] = node.address(coin.address_type) else: address = None pubkey = node.public_key() if pubkey[0] == 0x01: pubkey = b"\x00" + pubkey[1:] seckey = node.private_key() if msg.identity.proto == "gpg": signature = sign_challenge( seckey, msg.challenge_hidden, msg.challenge_visual, "gpg", msg.ecdsa_curve_name, ) elif msg.identity.proto == "signify": signature = sign_challenge( seckey, msg.challenge_hidden, msg.challenge_visual, "signify", msg.ecdsa_curve_name, ) elif msg.identity.proto == "ssh": signature = sign_challenge( seckey, msg.challenge_hidden, msg.challenge_visual, "ssh", msg.ecdsa_curve_name, ) else: signature = sign_challenge( seckey, msg.challenge_hidden, msg.challenge_visual, coin, msg.ecdsa_curve_name, ) return SignedIdentity(address=address, public_key=pubkey, signature=signature)
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_keychain_for_coin( ctx: wire.Context, coin_name: Optional[str] ) -> Tuple[Keychain, coininfo.CoinInfo]: if coin_name is None: coin_name = "Bitcoin" try: coin = coininfo.by_name(coin_name) except ValueError: raise wire.DataError("Unsupported coin type") namespaces = get_namespaces_for_coin(coin) keychain = await get_keychain(ctx, namespaces) return keychain, coin
from common import * from trezor.messages import TxInput, SignTx, PrevOutput from trezor.enums import InputScriptType from apps.zcash.hasher import ZcashHasher from apps.bitcoin.common import SigHashType from apps.common.coininfo import by_name ZCASH_COININFO = by_name("Zcash") @unittest.skipUnless(not utils.BITCOIN_ONLY, "altcoin") class TestZcashSigHasher(unittest.TestCase): def test_zcash_hasher(self): # this test vector was generated using # https://github.com/zcash-hackworks/zcash-test-vectors tx = SignTx( coin_name="Zcash", version=5, version_group_id=648488714, branch_id=928093729, lock_time=2591264634, expiry=36466477, inputs_count=3, outputs_count=3, ) inputs = [ TxInput( prev_hash=unhexlify( "4f61d91843ccb386dd1c482169eef62efaaf9d9364b1666e4d4c299e04a852e1" ), prev_index=1569726664,