2021-12-08 09:10:58 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2020-05-15 18:35:09 +00:00
|
|
|
from trezor import wire
|
|
|
|
from trezor.crypto import base58, cashaddr
|
2021-10-16 08:07:19 +00:00
|
|
|
from trezor.crypto.curve import bip340
|
2018-10-10 11:40:55 +00:00
|
|
|
from trezor.crypto.hashlib import sha256
|
2021-03-23 12:35:27 +00:00
|
|
|
from trezor.enums import InputScriptType
|
2021-03-19 20:22:30 +00:00
|
|
|
from trezor.utils import HashWriter
|
2020-05-15 18:35:09 +00:00
|
|
|
|
2020-07-23 11:58:19 +00:00
|
|
|
from apps.common import address_type
|
2020-05-18 11:17:34 +00:00
|
|
|
from apps.common.coininfo import CoinInfo
|
|
|
|
|
2020-05-15 18:35:09 +00:00
|
|
|
from .common import ecdsa_hash_pubkey, encode_bech32_address
|
|
|
|
from .multisig import multisig_get_pubkeys, multisig_pubkey_index
|
2021-10-14 11:39:47 +00:00
|
|
|
from .scripts import output_script_native_segwit, write_output_script_multisig
|
2017-11-23 16:30:43 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
if TYPE_CHECKING:
|
2022-09-15 16:30:05 +00:00
|
|
|
from trezor.messages import MultisigRedeemScriptType
|
2020-04-08 15:37:40 +00:00
|
|
|
from trezor.crypto import bip32
|
2019-08-21 08:49:38 +00:00
|
|
|
|
2017-11-23 16:30:43 +00:00
|
|
|
|
2018-07-03 14:20:58 +00:00
|
|
|
def get_address(
|
2021-03-23 12:35:27 +00:00
|
|
|
script_type: InputScriptType,
|
2020-04-08 15:37:40 +00:00
|
|
|
coin: CoinInfo,
|
|
|
|
node: bip32.HDNode,
|
2021-03-18 09:48:50 +00:00
|
|
|
multisig: MultisigRedeemScriptType | None = None,
|
2018-07-03 14:20:58 +00:00
|
|
|
) -> str:
|
2020-10-26 15:20:59 +00:00
|
|
|
if multisig:
|
|
|
|
# Ensure that our public key is included in the multisig.
|
|
|
|
multisig_pubkey_index(multisig, node.public_key())
|
2018-07-03 14:20:58 +00:00
|
|
|
|
multi: Add decred staking.
Add two new input and four output script types.
Decred ticket purchases consist of a stake submission, op returns, and
change addresses. Although change addresses are allowed by consensus,
they are no longer used in practice and so have been given the
restrictions of a null pubkey and no value. Stake scripts are almost
identical to p2pkh or p2sh except for an extra opcode in front. Inputs
are currently only used in the form of one input three outputs with the
first output, or stake submission, paying to a public key hash, or with
two inputs and five outputs with the stake submission paying to a
multisig script hash. The op returns are directed to the user in the
case of one and the voting service provider and user in the case of two.
One of the sstx commitment for a ticket must pay back to the trezor
wallet. This is checked and an error is thrown if we don't find the
expected public key hash.
Because this adds the ability to create new types of outputs once the
ticket votes, two new input script types are also needed. A successful
vote will lead to a stake generation script that must be spent, and an
unsuccessful vote will lead to a revocation script that must be spent.
If we allowed stake change scripts to have a valid pubkey, that too
would require another op code, but we disallow those for output.
2020-09-26 09:30:56 +00:00
|
|
|
if script_type in (InputScriptType.SPENDADDRESS, InputScriptType.SPENDMULTISIG):
|
2018-02-24 17:32:36 +00:00
|
|
|
if multisig: # p2sh multisig
|
|
|
|
if coin.address_type_p2sh is None:
|
2020-05-15 18:35:09 +00:00
|
|
|
raise wire.ProcessError("Multisig not enabled on this coin")
|
2018-02-24 17:32:36 +00:00
|
|
|
|
2018-02-24 22:23:19 +00:00
|
|
|
pubkeys = multisig_get_pubkeys(multisig)
|
2018-07-01 16:47:43 +00:00
|
|
|
address = address_multisig_p2sh(pubkeys, multisig.m, coin)
|
2018-05-24 14:38:56 +00:00
|
|
|
if coin.cashaddr_prefix is not None:
|
|
|
|
address = address_to_cashaddr(address, coin)
|
|
|
|
return address
|
2018-02-24 17:32:36 +00:00
|
|
|
if script_type == InputScriptType.SPENDMULTISIG:
|
2020-05-15 18:35:09 +00:00
|
|
|
raise wire.ProcessError("Multisig details required")
|
2018-02-24 17:32:36 +00:00
|
|
|
|
|
|
|
# p2pkh
|
2018-05-24 14:38:56 +00:00
|
|
|
address = node.address(coin.address_type)
|
|
|
|
if coin.cashaddr_prefix is not None:
|
|
|
|
address = address_to_cashaddr(address, coin)
|
|
|
|
return address
|
2017-11-23 16:30:43 +00:00
|
|
|
|
2018-02-12 13:08:43 +00:00
|
|
|
elif script_type == InputScriptType.SPENDWITNESS: # native p2wpkh or native p2wsh
|
2017-11-23 16:30:43 +00:00
|
|
|
if not coin.segwit or not coin.bech32_prefix:
|
2020-05-15 18:35:09 +00:00
|
|
|
raise wire.ProcessError("Segwit not enabled on this coin")
|
2018-02-12 13:08:43 +00:00
|
|
|
# native p2wsh multisig
|
|
|
|
if multisig is not None:
|
2018-02-24 22:23:19 +00:00
|
|
|
pubkeys = multisig_get_pubkeys(multisig)
|
|
|
|
return address_multisig_p2wsh(pubkeys, multisig.m, coin.bech32_prefix)
|
2018-02-12 13:08:43 +00:00
|
|
|
|
|
|
|
# native p2wpkh
|
2018-10-08 12:34:51 +00:00
|
|
|
return address_p2wpkh(node.public_key(), coin)
|
2017-11-23 16:30:43 +00:00
|
|
|
|
2021-10-16 08:07:19 +00:00
|
|
|
elif script_type == InputScriptType.SPENDTAPROOT: # taproot
|
|
|
|
if not coin.taproot or not coin.bech32_prefix:
|
|
|
|
raise wire.ProcessError("Taproot not enabled on this coin")
|
|
|
|
|
|
|
|
if multisig is not None:
|
|
|
|
raise wire.ProcessError("Multisig not supported for taproot")
|
|
|
|
|
|
|
|
return address_p2tr(node.public_key(), coin)
|
|
|
|
|
2018-07-03 14:20:58 +00:00
|
|
|
elif (
|
|
|
|
script_type == InputScriptType.SPENDP2SHWITNESS
|
|
|
|
): # p2wpkh or p2wsh nested in p2sh
|
2018-01-24 10:07:31 +00:00
|
|
|
if not coin.segwit or coin.address_type_p2sh is None:
|
2020-05-15 18:35:09 +00:00
|
|
|
raise wire.ProcessError("Segwit not enabled on this coin")
|
2018-02-12 13:08:43 +00:00
|
|
|
# p2wsh multisig nested in p2sh
|
2018-02-12 12:35:41 +00:00
|
|
|
if multisig is not None:
|
2018-02-24 22:23:19 +00:00
|
|
|
pubkeys = multisig_get_pubkeys(multisig)
|
2018-07-01 16:47:43 +00:00
|
|
|
return address_multisig_p2wsh_in_p2sh(pubkeys, multisig.m, coin)
|
2018-02-12 12:35:41 +00:00
|
|
|
|
2018-02-12 13:08:43 +00:00
|
|
|
# p2wpkh nested in p2sh
|
2018-07-01 16:47:43 +00:00
|
|
|
return address_p2wpkh_in_p2sh(node.public_key(), coin)
|
2017-11-23 16:30:43 +00:00
|
|
|
|
|
|
|
else:
|
2020-05-15 18:35:09 +00:00
|
|
|
raise wire.ProcessError("Invalid script type")
|
2017-11-23 16:30:43 +00:00
|
|
|
|
|
|
|
|
2021-03-18 09:48:50 +00:00
|
|
|
def address_multisig_p2sh(pubkeys: list[bytes], m: int, coin: CoinInfo) -> str:
|
2018-07-01 16:47:43 +00:00
|
|
|
if coin.address_type_p2sh is None:
|
2020-05-15 18:35:09 +00:00
|
|
|
raise wire.ProcessError("Multisig not enabled on this coin")
|
2021-03-19 20:22:30 +00:00
|
|
|
redeem_script = HashWriter(coin.script_hash())
|
|
|
|
write_output_script_multisig(redeem_script, pubkeys, m)
|
|
|
|
return address_p2sh(redeem_script.get_digest(), coin)
|
2018-01-24 15:12:54 +00:00
|
|
|
|
|
|
|
|
2021-03-18 09:48:50 +00:00
|
|
|
def address_multisig_p2wsh_in_p2sh(pubkeys: list[bytes], m: int, coin: CoinInfo) -> str:
|
2018-07-01 16:47:43 +00:00
|
|
|
if coin.address_type_p2sh is None:
|
2020-05-15 18:35:09 +00:00
|
|
|
raise wire.ProcessError("Multisig not enabled on this coin")
|
2021-03-19 20:22:30 +00:00
|
|
|
witness_script_h = HashWriter(sha256())
|
|
|
|
write_output_script_multisig(witness_script_h, pubkeys, m)
|
|
|
|
return address_p2wsh_in_p2sh(witness_script_h.get_digest(), coin)
|
2018-01-24 15:12:54 +00:00
|
|
|
|
|
|
|
|
2021-03-18 09:48:50 +00:00
|
|
|
def address_multisig_p2wsh(pubkeys: list[bytes], m: int, hrp: str) -> str:
|
2018-02-24 22:23:19 +00:00
|
|
|
if not hrp:
|
2020-05-15 18:35:09 +00:00
|
|
|
raise wire.ProcessError("Multisig not enabled on this coin")
|
2021-03-19 20:22:30 +00:00
|
|
|
witness_script_h = HashWriter(sha256())
|
|
|
|
write_output_script_multisig(witness_script_h, pubkeys, m)
|
|
|
|
return address_p2wsh(witness_script_h.get_digest(), hrp)
|
2018-01-24 15:12:54 +00:00
|
|
|
|
|
|
|
|
2018-07-01 16:47:43 +00:00
|
|
|
def address_pkh(pubkey: bytes, coin: CoinInfo) -> str:
|
2021-03-19 20:26:52 +00:00
|
|
|
s = address_type.tobytes(coin.address_type) + coin.script_hash(pubkey).digest()
|
2018-07-01 16:47:43 +00:00
|
|
|
return base58.encode_check(bytes(s), coin.b58_hash)
|
2018-02-27 16:25:09 +00:00
|
|
|
|
|
|
|
|
2018-07-01 16:47:43 +00:00
|
|
|
def address_p2sh(redeem_script_hash: bytes, coin: CoinInfo) -> str:
|
2018-07-04 12:32:34 +00:00
|
|
|
s = address_type.tobytes(coin.address_type_p2sh) + redeem_script_hash
|
2018-07-01 16:47:43 +00:00
|
|
|
return base58.encode_check(bytes(s), coin.b58_hash)
|
2017-11-23 16:30:43 +00:00
|
|
|
|
|
|
|
|
2018-07-01 16:47:43 +00:00
|
|
|
def address_p2wpkh_in_p2sh(pubkey: bytes, coin: CoinInfo) -> str:
|
2018-10-08 12:34:51 +00:00
|
|
|
pubkey_hash = ecdsa_hash_pubkey(pubkey, coin)
|
2021-10-14 11:39:47 +00:00
|
|
|
redeem_script = output_script_native_segwit(0, pubkey_hash)
|
2021-03-19 20:26:52 +00:00
|
|
|
redeem_script_hash = coin.script_hash(redeem_script).digest()
|
2018-07-01 16:47:43 +00:00
|
|
|
return address_p2sh(redeem_script_hash, coin)
|
2018-01-24 10:07:31 +00:00
|
|
|
|
|
|
|
|
2018-07-01 16:47:43 +00:00
|
|
|
def address_p2wsh_in_p2sh(witness_script_hash: bytes, coin: CoinInfo) -> str:
|
2021-10-14 11:39:47 +00:00
|
|
|
redeem_script = output_script_native_segwit(0, witness_script_hash)
|
2021-03-19 20:26:52 +00:00
|
|
|
redeem_script_hash = coin.script_hash(redeem_script).digest()
|
2018-07-01 16:47:43 +00:00
|
|
|
return address_p2sh(redeem_script_hash, coin)
|
2018-01-24 10:07:31 +00:00
|
|
|
|
|
|
|
|
2018-10-08 12:34:51 +00:00
|
|
|
def address_p2wpkh(pubkey: bytes, coin: CoinInfo) -> str:
|
2020-09-14 11:54:09 +00:00
|
|
|
assert coin.bech32_prefix is not None
|
2018-10-08 12:34:51 +00:00
|
|
|
pubkeyhash = ecdsa_hash_pubkey(pubkey, coin)
|
2021-08-11 12:07:21 +00:00
|
|
|
return encode_bech32_address(coin.bech32_prefix, 0, pubkeyhash)
|
2017-11-23 16:30:43 +00:00
|
|
|
|
|
|
|
|
2018-02-24 22:23:19 +00:00
|
|
|
def address_p2wsh(witness_script_hash: bytes, hrp: str) -> str:
|
2021-08-11 12:07:21 +00:00
|
|
|
return encode_bech32_address(hrp, 0, witness_script_hash)
|
2017-11-23 16:30:43 +00:00
|
|
|
|
|
|
|
|
2021-10-16 08:07:19 +00:00
|
|
|
def address_p2tr(pubkey: bytes, coin: CoinInfo) -> str:
|
|
|
|
assert coin.bech32_prefix is not None
|
|
|
|
output_pubkey = bip340.tweak_public_key(pubkey[1:])
|
|
|
|
return encode_bech32_address(coin.bech32_prefix, 1, output_pubkey)
|
|
|
|
|
|
|
|
|
2018-05-24 14:38:56 +00:00
|
|
|
def address_to_cashaddr(address: str, coin: CoinInfo) -> str:
|
2020-09-14 11:54:09 +00:00
|
|
|
assert coin.cashaddr_prefix is not None
|
2018-07-01 16:47:43 +00:00
|
|
|
raw = base58.decode_check(address, coin.b58_hash)
|
2018-05-24 14:38:56 +00:00
|
|
|
version, data = raw[0], raw[1:]
|
|
|
|
if version == coin.address_type:
|
|
|
|
version = cashaddr.ADDRESS_TYPE_P2KH
|
|
|
|
elif version == coin.address_type_p2sh:
|
|
|
|
version = cashaddr.ADDRESS_TYPE_P2SH
|
|
|
|
else:
|
2018-07-03 14:20:58 +00:00
|
|
|
raise ValueError("Unknown cashaddr address type")
|
2018-05-24 14:38:56 +00:00
|
|
|
return cashaddr.encode(coin.cashaddr_prefix, version, data)
|
|
|
|
|
|
|
|
|
2018-06-28 09:21:07 +00:00
|
|
|
def address_short(coin: CoinInfo, address: str) -> str:
|
2018-07-03 14:20:58 +00:00
|
|
|
if coin.cashaddr_prefix is not None and address.startswith(
|
|
|
|
coin.cashaddr_prefix + ":"
|
|
|
|
):
|
|
|
|
return address[len(coin.cashaddr_prefix) + 1 :]
|
2018-06-28 09:21:07 +00:00
|
|
|
else:
|
|
|
|
return address
|