forked from LedgerHQ/app-bitcoin-new
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sign_message.py
More file actions
153 lines (113 loc) · 8.34 KB
/
Copy pathtest_sign_message.py
File metadata and controls
153 lines (113 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pytest
from ledger_bitcoin.exception.errors import DenyError
from ledger_bitcoin.exception.device_exception import DeviceException
from ragger.navigator import Navigator
from ragger.firmware import Firmware
from ragger.error import ExceptionRAPDU
from ragger_bitcoin import RaggerClient
from .instructions import message_instruction_approve, message_instruction_approve_long, message_instruction_reject
def test_sign_message(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
msg = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks."
path = "m/44'/1'/0'/0/0"
result = client.sign_message(msg, path, navigator,
instructions=message_instruction_approve(firmware),
testname=test_name)
assert result == "IOR4YRVlmJGMx+H7PgQvHzWAF0HAgrUggQeRdnoWKpypfaAberpvF+XbOCM5Cd/ljogNyU3w2OIL8eYCyZ6Ru2k="
def test_sign_message_eol(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
msg = "\nThe Times 03/Jan/2009 Chancellor\non brink of second bailout for banks."
path = "m/44'/1'/0'/0/0"
result = client.sign_message(msg, path, navigator,
instructions=message_instruction_approve(firmware),
testname=test_name)
assert result == "ILMgIpFxqZwrB5bgR/seis2N48jS7pmHViKcRYisIPBdDjhAzGDGP3HiLPcrYMlEOXJvdJ9/Mud/+fslM2KtKaM="
def test_sign_message_64bytes(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
# Version 2.2.2 introduced a bug where signing a 64 bytes message would fail; this test is to avoid regressions
msg = "a" * 64
path = "m/44'/1'/0'/0/0"
client.sign_message(msg, path, navigator,
instructions=message_instruction_approve(firmware, save_screenshot=False),
testname=test_name)
def test_sign_message_accept(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
message = "Hello world!"
res = client.sign_message(
message,
"m/84'/1'/0'/0/0",
navigator,
instructions=message_instruction_approve(firmware),
testname=test_name
)
assert res == 'IEOK4+JMK7FToR7XMzFCoAYh1nud1IKm9Wq3vXLSVk/lBay8rHCRp9bP6riyR5NDqXYyYf7cXgMQTHNz3SemwZI='
def test_sign_message_accept_long(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
# Test with a long message that is split in multiple leaves in the Merkle tree
message = "The root problem with conventional currency is all the trust that's required to make it work. The central bank must be trusted not to debase the currency, but the history of fiat currencies is full of breaches of that trust. Banks must be trusted to hold our money and transfer it electronically, but they lend it out in waves of credit bubbles with barely a fraction in reserve. We have to trust them with our privacy, trust them not to let identity thieves drain our accounts. Their massive overhead costs make micropayments impossible."
res = client.sign_message(
message,
"m/84'/1'/0'/0/8",
navigator,
instructions=message_instruction_approve_long(firmware),
testname=test_name
)
assert res == 'H4frM6TYm5ty1MAf9o/Zz9Qiy3VEldAYFY91SJ/5nYMAZY1UUB97fiRjKW8mJit2+V4OCa1YCqjDqyFnD9Fw75k='
def test_sign_message_reject(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
with pytest.raises(ExceptionRAPDU) as e:
client.sign_message("Anything", "m/44'/1'/0'/0/0",
navigator,
instructions=message_instruction_reject(firmware),
testname=test_name
)
assert DeviceException.exc.get(e.value.status) == DenyError
assert len(e.value.data) == 0
def test_sign_message_accept_non_ascii(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
# Test with a message that contains non ascii char
message = "Hello\rworld!"
res = client.sign_message(
message,
"m/84'/1'/0'/0/8",
navigator,
instructions=message_instruction_approve(firmware),
testname=test_name
)
assert res == 'ILXQPJapQ/OEy9f/ggRouI6HuAleQveIOBphNUCLlWNVQAml2Yx+885tsU0DKIxggsVq53o/fQRlosTPTQE/keE='
def test_sign_message_accept_too_long(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
# Test with a message that is too long to be displayed
message = "The root problem with conventional currency is all the trust that's required to make it work. The central bank must be trusted not to debase the currency, but the history of fiat currencies is full of breaches of that trust. Banks must be trusted to hold our money and transfer it electronically, but they lend it out in waves of credit bubbles with barely a fraction in reserve. We have to trust them with our privacy, trust them not to let identity thieves drain our accounts. Their massive overhead costs make micropayments impossible. The root problem with conventional currency is all the trust that's required to make it work. The central bank must be trusted not to debase the currency, but the history of fiat currencies is full of breaches of that trust. Banks must be trusted to hold our money and transfer it electronically, but they lend it out in waves of credit bubbles with barely a fraction in reserve. We have to trust them with our privacy, trust them not to let identity thieves drain our accounts. Their massive overhead costs make micropayments impossible. The root problem with conventional currency is all the trust that's required to make it work. The central bank must be trusted not to debase the currency, but the history of fiat currencies is full of breaches of that trust. Banks must be trusted to hold our money and transfer it electronically, but they lend it out in waves of credit bubbles with barely a fraction in reserve. We have to trust them with our privacy, trust them not to let identity thieves drain our accounts. Their massive overhead costs make micropayments impossible."
res = client.sign_message(
message,
"m/84'/1'/0'/0/8",
navigator,
instructions=message_instruction_approve(firmware),
testname=test_name
)
assert res == 'IDAl9RThAyunmYuol9DaDs/CScUpiol3FDSjIjyK9y0tc/x1HWrbT/ufdkPFY1Bmi+L9hc3ip1me2RmufprVuNk='
def test_sign_message_hash_reject(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
with pytest.raises(ExceptionRAPDU) as e:
client.sign_message("Hello\rworld!",
"m/44'/1'/0'/0/0",
navigator,
instructions=message_instruction_reject(firmware),
testname=test_name
)
assert DeviceException.exc.get(e.value.status) == DenyError
assert len(e.value.data) == 0
def test_sign_message_with_max_bip32_path(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
# Test signing with MAX_BIP32_PATH_STEPS (10) derivation steps
# Path: m/44'/1'/0'/0'/1'/2'/3'/4'/5/6 = 10 steps
message = "Maximum derivation path test"
path = "m/44'/1'/0'/0'/1'/2'/3'/4'/5/6"
result = client.sign_message(message, path, navigator,
instructions=message_instruction_approve(firmware, save_screenshot=False),
testname=test_name)
# Just verify it succeeds and returns a signature
assert result is not None
assert len(result) > 0
def test_sign_message_fail_too_long_bip32_path(navigator: Navigator, firmware: Firmware, client: RaggerClient, test_name: str):
# Test signing with MAX_BIP32_PATH_STEPS + 1 (11) derivation steps should fail
# Path: m/44'/1'/0'/0'/1'/2'/3'/4'/5/6/7 = 11 steps
message = "Too long derivation path test"
path = "m/44'/1'/0'/0'/1'/2'/3'/4'/5/6/7"
from ledger_bitcoin.exception.errors import WrongDataLengthError
with pytest.raises(ExceptionRAPDU) as e:
client.sign_message(message, path, navigator,
instructions=message_instruction_approve(firmware, save_screenshot=False),
testname=test_name)
assert DeviceException.exc.get(e.value.status) == WrongDataLengthError