2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
2019-05-29 16:44:09 +00:00
|
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
2018-06-21 14:28:34 +00:00
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
2017-01-03 18:40:05 +00:00
|
|
|
|
2018-10-22 13:59:07 +00:00
|
|
|
import pytest
|
2018-04-18 12:00:11 +00:00
|
|
|
|
2020-10-26 12:34:02 +00:00
|
|
|
from trezorlib import btc, device, messages
|
2020-03-24 13:18:08 +00:00
|
|
|
from trezorlib.exceptions import TrezorFailure
|
2020-10-26 12:32:03 +00:00
|
|
|
from trezorlib.messages import SafetyCheckLevel
|
2020-05-13 09:36:03 +00:00
|
|
|
from trezorlib.tools import parse_path
|
2013-01-17 12:37:48 +00:00
|
|
|
|
2021-11-29 11:00:01 +00:00
|
|
|
from ... import bip32
|
2018-04-18 13:53:40 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2021-10-13 13:45:53 +00:00
|
|
|
def getmultisig(chain, nr, xpubs):
|
2020-05-13 09:36:03 +00:00
|
|
|
return messages.MultisigRedeemScriptType(
|
2019-02-04 00:13:31 +00:00
|
|
|
nodes=[bip32.deserialize(xpub) for xpub in xpubs],
|
|
|
|
address_n=[chain, nr],
|
2021-10-13 13:45:53 +00:00
|
|
|
signatures=[b"", b"", b""],
|
2018-10-22 13:59:07 +00:00
|
|
|
m=2,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
def test_btc(client):
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Bitcoin", parse_path("m/44'/0'/0'/0/0"))
|
|
|
|
== "1JAd7XCBzGudGpJQSDSfpmJhiygtLQWaGL"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Bitcoin", parse_path("m/44'/0'/0'/0/1"))
|
|
|
|
== "1GWFxtwWmNVqotUPXLcKVL2mUKpshuJYo"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Bitcoin", parse_path("m/44'/0'/0'/1/0"))
|
|
|
|
== "1DyHzbQUoQEsLxJn6M7fMD8Xdt1XvNiwNE"
|
|
|
|
)
|
2013-01-17 12:37:48 +00:00
|
|
|
|
2020-05-13 09:36:03 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
@pytest.mark.altcoin
|
|
|
|
def test_ltc(client):
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Litecoin", parse_path("m/44'/2'/0'/0/0"))
|
|
|
|
== "LcubERmHD31PWup1fbozpKuiqjHZ4anxcL"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Litecoin", parse_path("m/44'/2'/0'/0/1"))
|
|
|
|
== "LVWBmHBkCGNjSPHucvL2PmnuRAJnucmRE6"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Litecoin", parse_path("m/44'/2'/0'/1/0"))
|
|
|
|
== "LWj6ApswZxay4cJEJES2sGe7fLMLRvvv8h"
|
|
|
|
)
|
2014-01-14 13:29:18 +00:00
|
|
|
|
2017-08-12 19:10:12 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
def test_tbtc(client):
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Testnet", parse_path("m/44'/1'/0'/0/0"))
|
|
|
|
== "mvbu1Gdy8SUjTenqerxUaZyYjmveZvt33q"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Testnet", parse_path("m/44'/1'/0'/0/1"))
|
|
|
|
== "mopZWqZZyQc3F2Sy33cvDtJchSAMsnLi7b"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Testnet", parse_path("m/44'/1'/0'/1/0"))
|
|
|
|
== "mm6kLYbGEL1tGe4ZA8xacfgRPdW1NLjCbZ"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.altcoin
|
|
|
|
def test_bch(client):
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Bcash", parse_path("44'/145'/0'/0/0"))
|
|
|
|
== "bitcoincash:qr08q88p9etk89wgv05nwlrkm4l0urz4cyl36hh9sv"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Bcash", parse_path("44'/145'/0'/0/1"))
|
|
|
|
== "bitcoincash:qr23ajjfd9wd73l87j642puf8cad20lfmqdgwvpat4"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Bcash", parse_path("44'/145'/0'/1/0"))
|
|
|
|
== "bitcoincash:qzc5q87w069lzg7g3gzx0c8dz83mn7l02scej5aluw"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.altcoin
|
|
|
|
def test_grs(client):
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Groestlcoin", parse_path("44'/17'/0'/0/0"))
|
|
|
|
== "Fj62rBJi8LvbmWu2jzkaUX1NFXLEqDLoZM"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Groestlcoin", parse_path("44'/17'/0'/1/0"))
|
|
|
|
== "FmRaqvVBRrAp2Umfqx9V1ectZy8gw54QDN"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Groestlcoin", parse_path("44'/17'/0'/1/1"))
|
|
|
|
== "Fmhtxeh7YdCBkyQF7AQG4QnY8y3rJg89di"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-12-20 14:52:33 +00:00
|
|
|
@pytest.mark.altcoin
|
|
|
|
def test_tgrs(client):
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Groestlcoin Testnet", parse_path("44'/1'/0'/0/0"))
|
|
|
|
== "mvbu1Gdy8SUjTenqerxUaZyYjmvedc787y"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Groestlcoin Testnet", parse_path("44'/1'/0'/1/0"))
|
|
|
|
== "mm6kLYbGEL1tGe4ZA8xacfgRPdW1LMq8cN"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Groestlcoin Testnet", parse_path("44'/1'/0'/1/1"))
|
|
|
|
== "mjXZwmEi1z1MzveZrKUAo4DBgbdq6ZhGD6"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
@pytest.mark.altcoin
|
|
|
|
def test_elements(client):
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Elements", parse_path("m/44'/1'/0'/0/0"))
|
|
|
|
== "2dpWh6jbhAowNsQ5agtFzi7j6nKscj6UnEr"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-12-10 17:42:13 +00:00
|
|
|
@pytest.mark.skip_t1
|
|
|
|
def test_address_mac(client):
|
|
|
|
resp = btc.get_authenticated_address(
|
|
|
|
client, "Bitcoin", parse_path("m/44'/0'/0'/1/0")
|
|
|
|
)
|
|
|
|
assert resp.address == "1DyHzbQUoQEsLxJn6M7fMD8Xdt1XvNiwNE"
|
|
|
|
assert (
|
|
|
|
resp.mac.hex()
|
|
|
|
== "9cf7c230041d6ed95b8273bd32e023d3f227ec8c44257f6463c743a4b4add028"
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = btc.get_authenticated_address(
|
|
|
|
client, "Testnet", parse_path("m/44'/1'/0'/1/0")
|
|
|
|
)
|
|
|
|
assert resp.address == "mm6kLYbGEL1tGe4ZA8xacfgRPdW1NLjCbZ"
|
|
|
|
assert (
|
|
|
|
resp.mac.hex()
|
|
|
|
== "4375089e50423505dc3480e6e85b0ba37a52bd1e009db5d260b6329f22c950d9"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Script type mismatch.
|
|
|
|
resp = btc.get_authenticated_address(
|
|
|
|
client, "Bitcoin", parse_path("84'/0'/0'/0/0"), show_display=False
|
|
|
|
)
|
|
|
|
assert resp.mac is None
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip_t1
|
|
|
|
@pytest.mark.altcoin
|
|
|
|
def test_altcoin_address_mac(client):
|
|
|
|
resp = btc.get_authenticated_address(
|
|
|
|
client, "Litecoin", parse_path("m/44'/2'/0'/1/0")
|
|
|
|
)
|
|
|
|
assert resp.address == "LWj6ApswZxay4cJEJES2sGe7fLMLRvvv8h"
|
|
|
|
assert (
|
|
|
|
resp.mac.hex()
|
|
|
|
== "eaf47182d7ae17d2046ec2e204bc5b67477db20a5eaea3cec5393c25664bc4d2"
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = btc.get_authenticated_address(client, "Bcash", parse_path("44'/145'/0'/1/0"))
|
|
|
|
assert resp.address == "bitcoincash:qzc5q87w069lzg7g3gzx0c8dz83mn7l02scej5aluw"
|
|
|
|
assert (
|
|
|
|
resp.mac.hex()
|
|
|
|
== "46d8e369b499a9dc62eb9e4472f4a12640ae0fb7a63c1a4dde6752123b2b7274"
|
|
|
|
)
|
|
|
|
|
|
|
|
resp = btc.get_authenticated_address(
|
|
|
|
client, "Groestlcoin", parse_path("44'/17'/0'/1/1")
|
|
|
|
)
|
|
|
|
assert resp.address == "Fmhtxeh7YdCBkyQF7AQG4QnY8y3rJg89di"
|
|
|
|
assert (
|
|
|
|
resp.mac.hex()
|
|
|
|
== "08d67c5f1ee20fd03f3e5aa26f798574716c122238ac280e33a6f3787d531552"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
@pytest.mark.multisig
|
|
|
|
def test_multisig(client):
|
|
|
|
xpubs = []
|
|
|
|
for n in range(1, 4):
|
|
|
|
node = btc.get_public_node(client, parse_path(f"44'/0'/{n}'"))
|
|
|
|
xpubs.append(node.xpub)
|
|
|
|
|
|
|
|
for nr in range(1, 4):
|
2019-03-15 13:44:06 +00:00
|
|
|
assert (
|
2021-11-10 21:19:53 +00:00
|
|
|
btc.get_address(
|
|
|
|
client,
|
|
|
|
"Bitcoin",
|
|
|
|
parse_path(f"44'/0'/{nr}'/0/0"),
|
|
|
|
show_display=(nr == 1),
|
|
|
|
multisig=getmultisig(0, 0, xpubs=xpubs),
|
|
|
|
)
|
|
|
|
== "3Pdz86KtfJBuHLcSv4DysJo4aQfanTqCzG"
|
2019-03-15 13:44:06 +00:00
|
|
|
)
|
|
|
|
assert (
|
2021-11-10 21:19:53 +00:00
|
|
|
btc.get_address(
|
|
|
|
client,
|
|
|
|
"Bitcoin",
|
|
|
|
parse_path(f"44'/0'/{nr}'/1/0"),
|
|
|
|
show_display=(nr == 1),
|
|
|
|
multisig=getmultisig(1, 0, xpubs=xpubs),
|
|
|
|
)
|
|
|
|
== "36gP3KVx1ooStZ9quZDXbAF3GCr42b2zzd"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.multisig
|
|
|
|
@pytest.mark.parametrize("show_display", (True, False))
|
|
|
|
def test_multisig_missing(client, show_display):
|
|
|
|
# Multisig with global suffix specification.
|
|
|
|
# Use account numbers 1, 2 and 3 to create a valid multisig,
|
|
|
|
# but not containing the keys from account 0 used below.
|
|
|
|
nodes = [
|
|
|
|
btc.get_public_node(client, parse_path(f"44'/0'/{i}'")).node
|
|
|
|
for i in range(1, 4)
|
|
|
|
]
|
|
|
|
multisig1 = messages.MultisigRedeemScriptType(
|
|
|
|
nodes=nodes, address_n=[0, 0], signatures=[b"", b"", b""], m=2
|
|
|
|
)
|
2019-03-15 13:44:06 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
# Multisig with per-node suffix specification.
|
|
|
|
node = btc.get_public_node(
|
|
|
|
client, parse_path("44h/0h/0h/0"), coin_name="Bitcoin"
|
|
|
|
).node
|
|
|
|
|
|
|
|
multisig2 = messages.MultisigRedeemScriptType(
|
|
|
|
pubkeys=[
|
|
|
|
messages.HDNodePathType(node=node, address_n=[1]),
|
|
|
|
messages.HDNodePathType(node=node, address_n=[2]),
|
|
|
|
messages.HDNodePathType(node=node, address_n=[3]),
|
|
|
|
],
|
|
|
|
signatures=[b"", b"", b""],
|
|
|
|
m=2,
|
|
|
|
)
|
2019-06-08 14:39:12 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
for multisig in (multisig1, multisig2):
|
|
|
|
with pytest.raises(TrezorFailure):
|
|
|
|
btc.get_address(
|
|
|
|
client,
|
|
|
|
"Bitcoin",
|
|
|
|
parse_path("44'/0'/0'/0/0"),
|
|
|
|
show_display=show_display,
|
|
|
|
multisig=multisig,
|
2017-08-12 19:10:12 +00:00
|
|
|
)
|
2018-08-13 16:21:24 +00:00
|
|
|
|
2020-10-26 15:35:55 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
@pytest.mark.altcoin
|
|
|
|
@pytest.mark.multisig
|
|
|
|
def test_bch_multisig(client):
|
|
|
|
xpubs = []
|
|
|
|
for n in range(1, 4):
|
2020-10-26 15:35:55 +00:00
|
|
|
node = btc.get_public_node(
|
2021-11-10 21:19:53 +00:00
|
|
|
client, parse_path(f"44'/145'/{n}'"), coin_name="Bcash"
|
2020-10-26 15:35:55 +00:00
|
|
|
)
|
2021-11-10 21:19:53 +00:00
|
|
|
xpubs.append(node.xpub)
|
2020-10-26 15:35:55 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
for nr in range(1, 4):
|
|
|
|
assert (
|
|
|
|
btc.get_address(
|
|
|
|
client,
|
|
|
|
"Bcash",
|
|
|
|
parse_path(f"44'/145'/{nr}'/0/0"),
|
|
|
|
show_display=(nr == 1),
|
|
|
|
multisig=getmultisig(0, 0, xpubs=xpubs),
|
2018-08-13 16:21:24 +00:00
|
|
|
)
|
2021-11-10 21:19:53 +00:00
|
|
|
== "bitcoincash:pqguz4nqq64jhr5v3kvpq4dsjrkda75hwy86gq0qzw"
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
btc.get_address(
|
|
|
|
client,
|
|
|
|
"Bcash",
|
|
|
|
parse_path(f"44'/145'/{nr}'/1/0"),
|
|
|
|
show_display=(nr == 1),
|
|
|
|
multisig=getmultisig(1, 0, xpubs=xpubs),
|
2018-08-13 16:21:24 +00:00
|
|
|
)
|
2021-11-10 21:19:53 +00:00
|
|
|
== "bitcoincash:pp6kcpkhua7789g2vyj0qfkcux3yvje7euhyhltn0a"
|
|
|
|
)
|
|
|
|
|
2017-08-12 19:10:12 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
def test_public_ckd(client):
|
|
|
|
node = btc.get_public_node(client, parse_path("m/44'/0'/0'")).node
|
|
|
|
node_sub1 = btc.get_public_node(client, parse_path("m/44'/0'/0'/1/0")).node
|
|
|
|
node_sub2 = bip32.public_ckd(node, [1, 0])
|
2014-01-14 13:29:18 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
assert node_sub1.chain_code == node_sub2.chain_code
|
|
|
|
assert node_sub1.public_key == node_sub2.public_key
|
2014-01-14 13:29:18 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
address1 = btc.get_address(client, "Bitcoin", parse_path("m/44'/0'/0'/1/0"))
|
|
|
|
address2 = bip32.get_address(node_sub2, 0)
|
2014-01-14 13:29:18 +00:00
|
|
|
|
2021-11-10 21:19:53 +00:00
|
|
|
assert address2 == "1DyHzbQUoQEsLxJn6M7fMD8Xdt1XvNiwNE"
|
|
|
|
assert address1 == address2
|
2020-05-13 09:36:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_invalid_path(client):
|
|
|
|
with pytest.raises(TrezorFailure, match="Forbidden key path"):
|
|
|
|
# slip44 id mismatch
|
2021-05-21 18:05:19 +00:00
|
|
|
btc.get_address(
|
|
|
|
client, "Bitcoin", parse_path("m/44'/111'/0'/0/0"), show_display=True
|
|
|
|
)
|
2020-05-13 09:36:03 +00:00
|
|
|
|
|
|
|
|
2021-05-21 18:05:19 +00:00
|
|
|
def test_unknown_path(client):
|
2020-10-26 12:34:02 +00:00
|
|
|
UNKNOWN_PATH = parse_path("m/44'/9'/0'/0/0")
|
2021-05-21 18:05:19 +00:00
|
|
|
with client:
|
|
|
|
client.set_expected_responses([messages.Failure])
|
|
|
|
|
|
|
|
with pytest.raises(TrezorFailure, match="Forbidden key path"):
|
|
|
|
# account number is too high
|
|
|
|
btc.get_address(client, "Bitcoin", UNKNOWN_PATH, show_display=True)
|
2020-10-26 12:32:03 +00:00
|
|
|
|
|
|
|
# disable safety checks
|
|
|
|
device.apply_settings(client, safety_checks=SafetyCheckLevel.PromptTemporarily)
|
|
|
|
|
2020-05-13 09:36:03 +00:00
|
|
|
with client:
|
|
|
|
client.set_expected_responses(
|
|
|
|
[
|
|
|
|
messages.ButtonRequest(
|
|
|
|
code=messages.ButtonRequestType.UnknownDerivationPath
|
|
|
|
),
|
2020-10-26 12:34:02 +00:00
|
|
|
messages.ButtonRequest(code=messages.ButtonRequestType.Address),
|
2020-09-15 11:06:41 +00:00
|
|
|
messages.Address,
|
2020-05-13 09:36:03 +00:00
|
|
|
]
|
|
|
|
)
|
2020-10-26 12:32:03 +00:00
|
|
|
# try again with a warning
|
2020-10-26 12:34:02 +00:00
|
|
|
btc.get_address(client, "Bitcoin", UNKNOWN_PATH, show_display=True)
|
|
|
|
|
|
|
|
with client:
|
|
|
|
# no warning is displayed when the call is silent
|
|
|
|
client.set_expected_responses([messages.Address])
|
|
|
|
btc.get_address(client, "Bitcoin", UNKNOWN_PATH, show_display=False)
|
|
|
|
|
|
|
|
|
2020-07-28 08:38:50 +00:00
|
|
|
@pytest.mark.altcoin
|
|
|
|
def test_crw(client):
|
|
|
|
assert (
|
|
|
|
btc.get_address(client, "Crown", parse_path("44'/72'/0'/0/0"))
|
|
|
|
== "CRWYdvZM1yXMKQxeN3hRsAbwa7drfvTwys48"
|
|
|
|
)
|