Coverage for tests/comm/test_protocol.py: 100%

172 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-07-10 13:43 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2021 RSK Labs Ltd 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining a copy of 

6# this software and associated documentation files (the "Software"), to deal in 

7# the Software without restriction, including without limitation the rights to 

8# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 

9# of the Software, and to permit persons to whom the Software is furnished to do 

10# so, subject to the following conditions: 

11# 

12# The above copyright notice and this permission notice shall be included in all 

13# copies or substantial portions of the Software. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

21# SOFTWARE. 

22 

23from unittest import TestCase 

24from comm.protocol import HSM2Protocol 

25 

26import logging 

27 

28logging.disable(logging.CRITICAL) 

29 

30 

31class TestHSM2Protocol(TestCase): 

32 def setUp(self): 

33 self.protocol = HSM2Protocol() 

34 

35 def test_format_error(self): 

36 self.assertEqual(self.protocol.format_error(), {"errorcode": -901}) 

37 

38 def test_format_error_type(self): 

39 self.assertEqual( 

40 self.protocol.handle_request('"{"not": "json", "only": "astring"}"'), 

41 {"errorcode": -901}, 

42 ) 

43 

44 def test_invalid_request(self): 

45 self.assertEqual(self.protocol.handle_request({"any": "thing"}), 

46 {"errorcode": -902}) 

47 

48 def test_invalid_request_no_command(self): 

49 self.assertEqual(self.protocol.handle_request({"version": 5}), 

50 {"errorcode": -902}) 

51 

52 def test_invalid_request_no_version(self): 

53 self.assertEqual(self.protocol.handle_request({"command": "sign"}), 

54 {"errorcode": -902}) 

55 

56 def test_wrong_version(self): 

57 self.assertEqual( 

58 self.protocol.handle_request({ 

59 "version": "somethingelse", 

60 "command": "whatever" 

61 }), 

62 {"errorcode": -904}, 

63 ) 

64 

65 def test_version_2_not_supported(self): 

66 self.assertEqual( 

67 self.protocol.handle_request({ 

68 "command": "whatever", 

69 "version": 2 

70 }), 

71 {"errorcode": -904}, 

72 ) 

73 self.assertEqual( 

74 self.protocol.handle_request({ 

75 "command": "whatever", 

76 "version": 5 

77 }), 

78 {"errorcode": -903}, 

79 ) 

80 

81 def test_version_3_not_supported(self): 

82 self.assertEqual( 

83 self.protocol.handle_request({ 

84 "command": "whatever", 

85 "version": 3 

86 }), 

87 {"errorcode": -904}, 

88 ) 

89 self.assertEqual( 

90 self.protocol.handle_request({ 

91 "command": "whatever", 

92 "version": 5 

93 }), 

94 {"errorcode": -903}, 

95 ) 

96 

97 def test_invalid_command(self): 

98 self.assertEqual( 

99 self.protocol.handle_request({ 

100 "command": "invalid", 

101 "version": 5 

102 }), 

103 {"errorcode": -903}, 

104 ) 

105 

106 def test_version(self): 

107 self.assertEqual( 

108 self.protocol.handle_request({"command": "version"}), 

109 { 

110 "errorcode": 0, 

111 "version": 5 

112 }, 

113 ) 

114 

115 def test_initialize_device_notimplemented(self): 

116 with self.assertRaises(NotImplementedError): 

117 self.protocol.initialize_device() 

118 

119 def test_getpubkey_keyId_presence(self): 

120 self.assertEqual( 

121 self.protocol.handle_request({ 

122 "command": "getPubKey", 

123 "version": 5 

124 }), 

125 {"errorcode": -103}, 

126 ) 

127 

128 def test_getpubkey_keyId_notastring(self): 

129 self.assertEqual( 

130 self.protocol.handle_request({ 

131 "command": "getPubKey", 

132 "version": 5, 

133 "keyId": 123 

134 }), 

135 {"errorcode": -103}, 

136 ) 

137 

138 def test_getpubkey_keyId_invalid(self): 

139 self.assertEqual( 

140 self.protocol.handle_request({ 

141 "command": "getPubKey", 

142 "version": 5, 

143 "keyId": "not-a-key-id" 

144 }), 

145 {"errorcode": -103}, 

146 ) 

147 

148 def test_getpubkey_notimplemented(self): 

149 with self.assertRaises(NotImplementedError): 

150 self.protocol.handle_request({ 

151 "command": "getPubKey", 

152 "version": 5, 

153 "keyId": "m/0/0/0/0/0" 

154 }) 

155 

156 def test_sign_keyId_presence(self): 

157 self.assertEqual( 

158 self.protocol.handle_request({ 

159 "version": 5, 

160 "command": "sign" 

161 }), 

162 {"errorcode": -103}, 

163 ) 

164 

165 def test_sign_keyId_not_a_string(self): 

166 self.assertEqual( 

167 self.protocol.handle_request({ 

168 "version": 5, 

169 "command": "sign", 

170 "keyId": 1234 

171 }), 

172 {"errorcode": -103}, 

173 ) 

174 

175 def test_sign_keyId_invalid(self): 

176 self.assertEqual( 

177 self.protocol.handle_request({ 

178 "version": 5, 

179 "command": "sign", 

180 "keyId": "not-a-key-id" 

181 }), 

182 {"errorcode": -103}, 

183 ) 

184 

185 def test_sign_auth_type_components(self): 

186 self.assertEqual( 

187 self.protocol.handle_request({ 

188 "version": 5, 

189 "command": "sign", 

190 "keyId": "m/0/0/0/0/0", 

191 "auth": 123 

192 }), 

193 {"errorcode": -101}, 

194 ) 

195 

196 self.assertEqual( 

197 self.protocol.handle_request({ 

198 "version": 5, 

199 "command": "sign", 

200 "keyId": "m/0/0/0/0/0", 

201 "auth": "" 

202 }), 

203 {"errorcode": -101}, 

204 ) 

205 

206 self.assertEqual( 

207 self.protocol.handle_request({ 

208 "version": 5, 

209 "command": "sign", 

210 "keyId": "m/0/0/0/0/0", 

211 "auth": { 

212 "any": "thing" 

213 }, 

214 }), 

215 {"errorcode": -101}, 

216 ) 

217 

218 def test_sign_receipt_presence_type(self): 

219 self.assertEqual( 

220 self.protocol.handle_request({ 

221 "version": 5, 

222 "command": "sign", 

223 "keyId": "m/0/0/0/0/0", 

224 "auth": { 

225 "receipt_merkle_proof": [] 

226 }, 

227 }), 

228 {"errorcode": -101}, 

229 ) 

230 

231 self.assertEqual( 

232 self.protocol.handle_request({ 

233 "version": 5, 

234 "command": "sign", 

235 "keyId": "m/0/0/0/0/0", 

236 "auth": { 

237 "receipt": 123, 

238 "receipt_merkle_proof": [] 

239 }, 

240 }), 

241 {"errorcode": -101}, 

242 ) 

243 

244 self.assertEqual( 

245 self.protocol.handle_request({ 

246 "version": 5, 

247 "command": "sign", 

248 "keyId": "m/0/0/0/0/0", 

249 "auth": { 

250 "receipt": "", 

251 "receipt_merkle_proof": [] 

252 }, 

253 }), 

254 {"errorcode": -101}, 

255 ) 

256 

257 self.assertEqual( 

258 self.protocol.handle_request({ 

259 "version": 5, 

260 "command": "sign", 

261 "keyId": "m/0/0/0/0/0", 

262 "auth": { 

263 "receipt": "not-a-hex", 

264 "receipt_merkle_proof": [] 

265 }, 

266 }), 

267 {"errorcode": -101}, 

268 ) 

269 

270 def test_sign_receipt_merkle_proof_presence_type(self): 

271 self.assertEqual( 

272 self.protocol.handle_request({ 

273 "version": 5, 

274 "command": "sign", 

275 "keyId": "m/0/0/0/0/0", 

276 "auth": { 

277 "receipt": "aabbcc" 

278 }, 

279 }), 

280 {"errorcode": -101}, 

281 ) 

282 

283 self.assertEqual( 

284 self.protocol.handle_request({ 

285 "version": 5, 

286 "command": "sign", 

287 "keyId": "m/0/0/0/0/0", 

288 "auth": { 

289 "receipt": "aabbcc", 

290 "receipt_merkle_proof": "notalist" 

291 }, 

292 }), 

293 {"errorcode": -101}, 

294 ) 

295 

296 self.assertEqual( 

297 self.protocol.handle_request({ 

298 "version": 5, 

299 "command": "sign", 

300 "keyId": "m/0/0/0/0/0", 

301 "auth": { 

302 "receipt": "aabbcc", 

303 "receipt_merkle_proof": [1, 2, 3] 

304 }, 

305 }), 

306 {"errorcode": -101}, 

307 ) 

308 

309 self.assertEqual( 

310 self.protocol.handle_request({ 

311 "version": 5, 

312 "command": "sign", 

313 "keyId": "m/0/0/0/0/0", 

314 "auth": { 

315 "receipt": "aabbcc", 

316 "receipt_merkle_proof": ["aa", "", "bb"], 

317 }, 

318 }), 

319 {"errorcode": -101}, 

320 ) 

321 

322 self.assertEqual( 

323 self.protocol.handle_request({ 

324 "version": 5, 

325 "command": "sign", 

326 "keyId": "m/0/0/0/0/0", 

327 "auth": { 

328 "receipt": "aabbcc", 

329 "receipt_merkle_proof": ["aa", "not-a-hex", "bb"], 

330 }, 

331 }), 

332 {"errorcode": -101}, 

333 ) 

334 

335 self.assertEqual( 

336 self.protocol.handle_request({ 

337 "version": 5, 

338 "command": "sign", 

339 "keyId": "m/0/0/0/0/0", 

340 "auth": { 

341 "receipt": "aabbcc", 

342 "receipt_merkle_proof": [] 

343 }, 

344 }), 

345 {"errorcode": -101}, 

346 ) 

347 

348 def test_sign_message_presence_type(self): 

349 self.assertEqual( 

350 self.protocol.handle_request({ 

351 "version": 5, 

352 "command": "sign", 

353 "keyId": "m/0/0/0/0/0", 

354 "auth": { 

355 "receipt": "aabbcc", 

356 "receipt_merkle_proof": ["aa"] 

357 }, 

358 }), 

359 {"errorcode": -102}, 

360 ) 

361 

362 self.assertEqual( 

363 self.protocol.handle_request({ 

364 "version": 5, 

365 "command": "sign", 

366 "keyId": "m/0/0/0/0/0", 

367 "auth": { 

368 "receipt": "aabbcc", 

369 "receipt_merkle_proof": ["aa"] 

370 }, 

371 "message": "", 

372 }), 

373 {"errorcode": -102}, 

374 ) 

375 

376 self.assertEqual( 

377 self.protocol.handle_request({ 

378 "version": 5, 

379 "command": "sign", 

380 "keyId": "m/0/0/0/0/0" 

381 }), 

382 {"errorcode": -102}, 

383 ) 

384 

385 self.assertEqual( 

386 self.protocol.handle_request({ 

387 "version": 5, 

388 "command": "sign", 

389 "keyId": "m/0/0/0/0/0", 

390 "message": "" 

391 }), 

392 {"errorcode": -102}, 

393 ) 

394 

395 def test_sign_legacy_message_value(self): 

396 self.assertEqual( 

397 self.protocol.handle_request({ 

398 "version": 5, 

399 "command": "sign", 

400 "keyId": "m/0/0/0/0/0", 

401 "auth": { 

402 "receipt": "aabbcc", 

403 "receipt_merkle_proof": ["aa"] 

404 }, 

405 "message": { 

406 "any": "thing" 

407 }, 

408 }), 

409 {"errorcode": -102}, 

410 ) 

411 

412 self.assertEqual( 

413 self.protocol.handle_request({ 

414 "version": 5, 

415 "command": "sign", 

416 "keyId": "m/0/0/0/0/0", 

417 "auth": { 

418 "receipt": "aabbcc", 

419 "receipt_merkle_proof": ["aa"] 

420 }, 

421 "message": { 

422 "sighashComputationMode": "legacy" 

423 }, 

424 }), 

425 {"errorcode": -102}, 

426 ) 

427 

428 self.assertEqual( 

429 self.protocol.handle_request({ 

430 "version": 5, 

431 "command": "sign", 

432 "keyId": "m/0/0/0/0/0", 

433 "auth": { 

434 "receipt": "aabbcc", 

435 "receipt_merkle_proof": ["aa"] 

436 }, 

437 "message": { 

438 "sighashComputationMode": "legacy", 

439 "tx": "001122" 

440 }, 

441 }), 

442 {"errorcode": -102}, 

443 ) 

444 

445 self.assertEqual( 

446 self.protocol.handle_request({ 

447 "version": 5, 

448 "command": "sign", 

449 "keyId": "m/0/0/0/0/0", 

450 "auth": { 

451 "receipt": "aabbcc", 

452 "receipt_merkle_proof": ["aa"] 

453 }, 

454 "message": { 

455 "sighashComputationMode": "legacy", 

456 "input": 123 

457 }, 

458 }), 

459 {"errorcode": -102}, 

460 ) 

461 

462 self.assertEqual( 

463 self.protocol.handle_request({ 

464 "version": 5, 

465 "command": "sign", 

466 "keyId": "m/0/0/0/0/0", 

467 "auth": { 

468 "receipt": "aabbcc", 

469 "receipt_merkle_proof": ["aa"] 

470 }, 

471 "message": { 

472 "tx": "001122", 

473 "input": 123 

474 }, 

475 }), 

476 {"errorcode": -102}, 

477 ) 

478 

479 self.assertEqual( 

480 self.protocol.handle_request({ 

481 "version": 5, 

482 "command": "sign", 

483 "keyId": "m/0/0/0/0/0", 

484 "auth": { 

485 "receipt": "aabbcc", 

486 "receipt_merkle_proof": ["aa"] 

487 }, 

488 "message": { 

489 "sighashComputationMode": "legacy", 

490 "tx": "001122", 

491 "input": "not-an-input" 

492 }, 

493 }), 

494 {"errorcode": -102}, 

495 ) 

496 

497 self.assertEqual( 

498 self.protocol.handle_request({ 

499 "version": 5, 

500 "command": "sign", 

501 "keyId": "m/0/0/0/0/0", 

502 "auth": { 

503 "receipt": "aabbcc", 

504 "receipt_merkle_proof": ["aa"] 

505 }, 

506 "message": { 

507 "sighashComputationMode": "legacy", 

508 "tx": "", 

509 "input": 123 

510 }, 

511 }), 

512 {"errorcode": -102}, 

513 ) 

514 

515 self.assertEqual( 

516 self.protocol.handle_request({ 

517 "version": 5, 

518 "command": "sign", 

519 "keyId": "m/0/0/0/0/0", 

520 "auth": { 

521 "receipt": "aabbcc", 

522 "receipt_merkle_proof": ["aa"] 

523 }, 

524 "message": { 

525 "sighashComputationMode": "legacy", 

526 "tx": "not-a-hex", 

527 "input": 123 

528 }, 

529 }), 

530 {"errorcode": -102}, 

531 ) 

532 

533 self.assertEqual( 

534 self.protocol.handle_request({ 

535 "version": 5, 

536 "command": "sign", 

537 "keyId": "m/0/0/0/0/0", 

538 "auth": { 

539 "receipt": "aabbcc", 

540 "receipt_merkle_proof": ["aa"] 

541 }, 

542 "message": { 

543 "sighashComputationMode": "somethingelse", 

544 "tx": "001122", 

545 "input": 123 

546 }, 

547 }), 

548 {"errorcode": -102}, 

549 ) 

550 

551 def test_sign_segwit_message_value(self): 

552 self.assertEqual( 

553 self.protocol.handle_request({ 

554 "version": 5, 

555 "command": "sign", 

556 "keyId": "m/0/0/0/0/0", 

557 "auth": { 

558 "receipt": "aabbcc", 

559 "receipt_merkle_proof": ["aa"] 

560 }, 

561 "message": { 

562 "sighashComputationMode": "segwit" 

563 }, 

564 }), 

565 {"errorcode": -102}, 

566 ) 

567 

568 self.assertEqual( 

569 self.protocol.handle_request({ 

570 "version": 5, 

571 "command": "sign", 

572 "keyId": "m/0/0/0/0/0", 

573 "auth": { 

574 "receipt": "aabbcc", 

575 "receipt_merkle_proof": ["aa"] 

576 }, 

577 "message": { 

578 "sighashComputationMode": "segwit", 

579 "tx": "001122", 

580 "witnessScript": "33445566", 

581 "outpointValue": 123000 

582 }, 

583 }), 

584 {"errorcode": -102}, 

585 ) 

586 

587 self.assertEqual( 

588 self.protocol.handle_request({ 

589 "version": 5, 

590 "command": "sign", 

591 "keyId": "m/0/0/0/0/0", 

592 "auth": { 

593 "receipt": "aabbcc", 

594 "receipt_merkle_proof": ["aa"] 

595 }, 

596 "message": { 

597 "sighashComputationMode": "segwit", 

598 "input": 123, 

599 "witnessScript": "33445566", 

600 "outpointValue": 123000 

601 }, 

602 }), 

603 {"errorcode": -102}, 

604 ) 

605 

606 self.assertEqual( 

607 self.protocol.handle_request({ 

608 "version": 5, 

609 "command": "sign", 

610 "keyId": "m/0/0/0/0/0", 

611 "auth": { 

612 "receipt": "aabbcc", 

613 "receipt_merkle_proof": ["aa"] 

614 }, 

615 "message": { 

616 "sighashComputationMode": "segwit", 

617 "tx": "001122", 

618 "input": 123, 

619 "outpointValue": 123000 

620 }, 

621 }), 

622 {"errorcode": -102}, 

623 ) 

624 

625 self.assertEqual( 

626 self.protocol.handle_request({ 

627 "version": 5, 

628 "command": "sign", 

629 "keyId": "m/0/0/0/0/0", 

630 "auth": { 

631 "receipt": "aabbcc", 

632 "receipt_merkle_proof": ["aa"] 

633 }, 

634 "message": { 

635 "sighashComputationMode": "segwit", 

636 "tx": "001122", 

637 "input": 123, 

638 "witnessScript": "33445566", 

639 }, 

640 }), 

641 {"errorcode": -102}, 

642 ) 

643 

644 self.assertEqual( 

645 self.protocol.handle_request({ 

646 "version": 5, 

647 "command": "sign", 

648 "keyId": "m/0/0/0/0/0", 

649 "auth": { 

650 "receipt": "aabbcc", 

651 "receipt_merkle_proof": ["aa"] 

652 }, 

653 "message": { 

654 "tx": "001122", 

655 "input": 123, 

656 "witnessScript": "33445566", 

657 "outpointValue": 123000 

658 }, 

659 }), 

660 {"errorcode": -102}, 

661 ) 

662 

663 self.assertEqual( 

664 self.protocol.handle_request({ 

665 "version": 5, 

666 "command": "sign", 

667 "keyId": "m/0/0/0/0/0", 

668 "auth": { 

669 "receipt": "aabbcc", 

670 "receipt_merkle_proof": ["aa"] 

671 }, 

672 "message": { 

673 "sighashComputationMode": "segwit", 

674 "tx": "001122", 

675 "input": "not-an-input", 

676 "witnessScript": "33445566", 

677 "outpointValue": 123000 

678 }, 

679 }), 

680 {"errorcode": -102}, 

681 ) 

682 

683 self.assertEqual( 

684 self.protocol.handle_request({ 

685 "version": 5, 

686 "command": "sign", 

687 "keyId": "m/0/0/0/0/0", 

688 "auth": { 

689 "receipt": "aabbcc", 

690 "receipt_merkle_proof": ["aa"] 

691 }, 

692 "message": { 

693 "sighashComputationMode": "segwit", 

694 "tx": "", 

695 "input": 123, 

696 "witnessScript": "33445566", 

697 "outpointValue": 123000 

698 }, 

699 }), 

700 {"errorcode": -102}, 

701 ) 

702 

703 self.assertEqual( 

704 self.protocol.handle_request({ 

705 "version": 5, 

706 "command": "sign", 

707 "keyId": "m/0/0/0/0/0", 

708 "auth": { 

709 "receipt": "aabbcc", 

710 "receipt_merkle_proof": ["aa"] 

711 }, 

712 "message": { 

713 "sighashComputationMode": "segwit", 

714 "tx": "not-a-hex", 

715 "input": 123, 

716 "witnessScript": "33445566", 

717 "outpointValue": 123000 

718 }, 

719 }), 

720 {"errorcode": -102}, 

721 ) 

722 

723 self.assertEqual( 

724 self.protocol.handle_request({ 

725 "version": 5, 

726 "command": "sign", 

727 "keyId": "m/0/0/0/0/0", 

728 "auth": { 

729 "receipt": "aabbcc", 

730 "receipt_merkle_proof": ["aa"] 

731 }, 

732 "message": { 

733 "sighashComputationMode": "segwit", 

734 "tx": "001122", 

735 "input": 123, 

736 "witnessScript": "not-a-hex", 

737 "outpointValue": 123000 

738 }, 

739 }), 

740 {"errorcode": -102}, 

741 ) 

742 

743 self.assertEqual( 

744 self.protocol.handle_request({ 

745 "version": 5, 

746 "command": "sign", 

747 "keyId": "m/0/0/0/0/0", 

748 "auth": { 

749 "receipt": "aabbcc", 

750 "receipt_merkle_proof": ["aa"] 

751 }, 

752 "message": { 

753 "sighashComputationMode": "segwit", 

754 "tx": "001122", 

755 "input": 123, 

756 "witnessScript": "33445566", 

757 "outpointValue": "not-an-int" 

758 }, 

759 }), 

760 {"errorcode": -102}, 

761 ) 

762 

763 self.assertEqual( 

764 self.protocol.handle_request({ 

765 "version": 5, 

766 "command": "sign", 

767 "keyId": "m/0/0/0/0/0", 

768 "auth": { 

769 "receipt": "aabbcc", 

770 "receipt_merkle_proof": ["aa"] 

771 }, 

772 "message": { 

773 "sighashComputationMode": "segwit", 

774 "tx": "001122", 

775 "input": 123, 

776 "witnessScript": "33445566", 

777 "outpointValue": "1122334455667788" 

778 }, 

779 }), 

780 {"errorcode": -102}, 

781 ) 

782 

783 self.assertEqual( 

784 self.protocol.handle_request({ 

785 "version": 5, 

786 "command": "sign", 

787 "keyId": "m/0/0/0/0/0", 

788 "auth": { 

789 "receipt": "aabbcc", 

790 "receipt_merkle_proof": ["aa"] 

791 }, 

792 "message": { 

793 "sighashComputationMode": "segwit", 

794 "tx": "001122", 

795 "input": 123, 

796 "witnessScript": "33445566", 

797 "outpointValue": -5 

798 }, 

799 }), 

800 {"errorcode": -102}, 

801 ) 

802 

803 self.assertEqual( 

804 self.protocol.handle_request({ 

805 "version": 5, 

806 "command": "sign", 

807 "keyId": "m/0/0/0/0/0", 

808 "auth": { 

809 "receipt": "aabbcc", 

810 "receipt_merkle_proof": ["aa"] 

811 }, 

812 "message": { 

813 "sighashComputationMode": "segwit", 

814 "tx": "001122", 

815 "input": 123, 

816 "witnessScript": "33445566", 

817 "outpointValue": 0xffffffffffffffff + 1 

818 }, 

819 }), 

820 {"errorcode": -102}, 

821 ) 

822 

823 def test_sign_hash_message_value(self): 

824 

825 self.assertEqual( 

826 self.protocol.handle_request({ 

827 "version": 5, 

828 "command": "sign", 

829 "keyId": "m/0/0/0/0/0", 

830 "auth": { 

831 "receipt": "aabbcc", 

832 "receipt_merkle_proof": ["aa"] 

833 }, 

834 "message": { 

835 "hash": 123 

836 }, 

837 }), 

838 {"errorcode": -102}, 

839 ) 

840 

841 self.assertEqual( 

842 self.protocol.handle_request({ 

843 "version": 5, 

844 "command": "sign", 

845 "keyId": "m/0/0/0/0/0", 

846 "auth": { 

847 "receipt": "aabbcc", 

848 "receipt_merkle_proof": ["aa"] 

849 }, 

850 "message": { 

851 "hash": "" 

852 }, 

853 }), 

854 {"errorcode": -102}, 

855 ) 

856 

857 self.assertEqual( 

858 self.protocol.handle_request({ 

859 "version": 5, 

860 "command": "sign", 

861 "keyId": "m/0/0/0/0/0", 

862 "auth": { 

863 "receipt": "aabbcc", 

864 "receipt_merkle_proof": ["aa"] 

865 }, 

866 "message": { 

867 "hash": "not-a-hex" 

868 }, 

869 }), 

870 {"errorcode": -102}, 

871 ) 

872 

873 self.assertEqual( 

874 self.protocol.handle_request({ 

875 "version": 5, 

876 "command": "sign", 

877 "keyId": "m/0/0/0/0/0", 

878 "auth": { 

879 "receipt": "aabbcc", 

880 "receipt_merkle_proof": ["aa"] 

881 }, 

882 "message": { 

883 "hash": "aa"*33 

884 }, 

885 }), 

886 {"errorcode": -102}, 

887 ) 

888 

889 def test_sign_notimplemented(self): 

890 with self.assertRaises(NotImplementedError): 

891 self.protocol.handle_request({ 

892 "command": "sign", 

893 "version": 5, 

894 "keyId": "m/0/0/0/0/0", 

895 "auth": { 

896 "receipt": "ddeeff", 

897 "receipt_merkle_proof": ["aa"] 

898 }, 

899 "message": { 

900 "sighashComputationMode": "legacy", 

901 "tx": "001122", 

902 "input": 123 

903 }, 

904 }) 

905 

906 with self.assertRaises(NotImplementedError): 

907 self.protocol.handle_request({ 

908 "command": "sign", 

909 "version": 5, 

910 "keyId": "m/0/0/0/0/0", 

911 "auth": { 

912 "receipt": "ddeeff", 

913 "receipt_merkle_proof": ["aa"] 

914 }, 

915 "message": { 

916 "hash": "bb"*32 

917 }, 

918 }) 

919 

920 with self.assertRaises(NotImplementedError): 

921 self.protocol.handle_request({ 

922 "version": 5, 

923 "command": "sign", 

924 "keyId": "m/0/0/0/0/0", 

925 "message": { 

926 "sighashComputationMode": "legacy", 

927 "tx": "001122", 

928 "input": 123 

929 }, 

930 }) 

931 

932 with self.assertRaises(NotImplementedError): 

933 self.protocol.handle_request({ 

934 "version": 5, 

935 "command": "sign", 

936 "keyId": "m/0/0/0/0/0", 

937 "message": { 

938 "sighashComputationMode": "segwit", 

939 "tx": "001122", 

940 "input": 123, 

941 "witnessScript": "3344556677", 

942 "outpointValue": 123000, 

943 }, 

944 }) 

945 

946 with self.assertRaises(NotImplementedError): 

947 self.protocol.handle_request({ 

948 "version": 5, 

949 "command": "sign", 

950 "keyId": "m/0/0/0/0/0", 

951 "message": { 

952 "hash": "bb"*32 

953 }, 

954 }) 

955 

956 def test_sign_noauth_message_presence(self): 

957 self.assertEqual( 

958 self.protocol.handle_request({ 

959 "version": 5, 

960 "command": "sign", 

961 "keyId": "m/0/0/0/0/0" 

962 }), 

963 {"errorcode": -102}, 

964 ) 

965 

966 def test_sign_noauth_message_notobject(self): 

967 self.assertEqual( 

968 self.protocol.handle_request({ 

969 "version": 5, 

970 "command": "sign", 

971 "keyId": "m/0/0/0/0/0", 

972 "message": 123 

973 }), 

974 {"errorcode": -102}, 

975 ) 

976 

977 def test_sign_noauth_message_hash_notpresent(self): 

978 self.assertEqual( 

979 self.protocol.handle_request({ 

980 "version": 5, 

981 "command": "sign", 

982 "keyId": "m/0/0/0/0/0", 

983 "message": { 

984 "something": "else" 

985 }, 

986 }), 

987 {"errorcode": -102}, 

988 ) 

989 

990 def test_sign_noauth_message_hash_invalid(self): 

991 self.assertEqual( 

992 self.protocol.handle_request({ 

993 "version": 5, 

994 "command": "sign", 

995 "keyId": "m/0/0/0/0/0", 

996 "message": { 

997 "hash": 123 

998 }, 

999 }), 

1000 {"errorcode": -102}, 

1001 ) 

1002 

1003 def test_sign_noauth_notimplemented(self): 

1004 with self.assertRaises(NotImplementedError): 

1005 self.protocol.handle_request({ 

1006 "version": 5, 

1007 "command": "sign", 

1008 "keyId": "m/0/0/0/0/0", 

1009 "message": { 

1010 "hash": "bb"*32 

1011 }, 

1012 }) 

1013 

1014 def test_advance_blockchain_blocks_presence(self): 

1015 self.assertEqual( 

1016 self.protocol.handle_request({ 

1017 "version": 5, 

1018 "command": "advanceBlockchain", 

1019 "brothers": [], 

1020 }), 

1021 {"errorcode": -204}, 

1022 ) 

1023 

1024 self.assertEqual( 

1025 self.protocol.handle_request({ 

1026 "version": 5, 

1027 "command": "advanceBlockchain", 

1028 "blocks": 123, 

1029 "brothers": [], 

1030 }), 

1031 {"errorcode": -204}, 

1032 ) 

1033 

1034 self.assertEqual( 

1035 self.protocol.handle_request({ 

1036 "version": 5, 

1037 "command": "advanceBlockchain", 

1038 "blocks": [], 

1039 "brothers": [], 

1040 }), 

1041 {"errorcode": -204}, 

1042 ) 

1043 

1044 self.assertEqual( 

1045 self.protocol.handle_request({ 

1046 "version": 5, 

1047 "command": "advanceBlockchain", 

1048 "blocks": ["ok", 333, "another-ok"], 

1049 "brothers": [[], [], []], 

1050 }), 

1051 {"errorcode": -204}, 

1052 ) 

1053 

1054 def test_advance_blockchain_brothers_presence(self): 

1055 self.assertEqual( 

1056 self.protocol.handle_request({ 

1057 "version": 5, 

1058 "command": "advanceBlockchain", 

1059 "blocks": ["ok", "another-ok", "yet-another-ok"], 

1060 }), 

1061 {"errorcode": -205}, 

1062 ) 

1063 

1064 self.assertEqual( 

1065 self.protocol.handle_request({ 

1066 "version": 5, 

1067 "command": "advanceBlockchain", 

1068 "blocks": ["ok", "another-ok", "yet-another-ok"], 

1069 "brothers": "notalist", 

1070 }), 

1071 {"errorcode": -205}, 

1072 ) 

1073 

1074 self.assertEqual( 

1075 self.protocol.handle_request({ 

1076 "version": 5, 

1077 "command": "advanceBlockchain", 

1078 "blocks": ["ok", "another-ok", "yet-another-ok"], 

1079 "brothers": [[], 123, []], 

1080 }), 

1081 {"errorcode": -205}, 

1082 ) 

1083 

1084 self.assertEqual( 

1085 self.protocol.handle_request({ 

1086 "version": 5, 

1087 "command": "advanceBlockchain", 

1088 "blocks": ["ok", "another-ok", "yet-another-ok"], 

1089 "brothers": [["bb11", "bb12"], ["bb21", 123], ["bb31", "bb32", "bb33"]], 

1090 }), 

1091 {"errorcode": -205}, 

1092 ) 

1093 

1094 self.assertEqual( 

1095 self.protocol.handle_request({ 

1096 "version": 5, 

1097 "command": "advanceBlockchain", 

1098 "blocks": ["ok", "another-ok", "yet-another-ok"], 

1099 "brothers": [["bb11", "bb12"], ["bb21"]], 

1100 }), 

1101 {"errorcode": -205}, 

1102 ) 

1103 

1104 self.assertEqual( 

1105 self.protocol.handle_request({ 

1106 "version": 5, 

1107 "command": "advanceBlockchain", 

1108 "blocks": ["ok", "another-ok", "yet-another-ok"], 

1109 "brothers": [["bb11", "bb12"], ["bb21"], ["bb31", "bb32", "bb33"], []], 

1110 }), 

1111 {"errorcode": -205}, 

1112 ) 

1113 

1114 self.assertEqual( 

1115 self.protocol.handle_request({ 

1116 "version": 5, 

1117 "command": "advanceBlockchain", 

1118 "blocks": ["ok"], 

1119 "brothers": [[""]], 

1120 }), 

1121 {"errorcode": -205}, 

1122 ) 

1123 

1124 self.assertEqual( 

1125 self.protocol.handle_request({ 

1126 "version": 5, 

1127 "command": "advanceBlockchain", 

1128 "blocks": ["ok", "another-ok", "yet-another-ok"], 

1129 "brothers": [["bb11", "bb12"], ["bb21"], ["bb31", "bb32", "not-hex"]], 

1130 }), 

1131 {"errorcode": -205}, 

1132 ) 

1133 

1134 def test_advance_blockchain_notimplemented(self): 

1135 with self.assertRaises(NotImplementedError): 

1136 self.protocol.handle_request({ 

1137 "command": "advanceBlockchain", 

1138 "version": 5, 

1139 "blocks": ["fist-block", "second-block"], 

1140 "brothers": [["bb11", "bb12"], ["bb21", "bb22", "bb23"]], 

1141 }) 

1142 

1143 def test_reset_advance_blockchain_notimplemented(self): 

1144 with self.assertRaises(NotImplementedError): 

1145 self.protocol.handle_request({ 

1146 "command": "resetAdvanceBlockchain", 

1147 "version": 5 

1148 }) 

1149 

1150 def test_blockchain_status_notimplemented(self): 

1151 with self.assertRaises(NotImplementedError): 

1152 self.protocol.handle_request({"command": "blockchainState", "version": 5}) 

1153 

1154 def test_update_ancestor_block_blocks_presence(self): 

1155 self.assertEqual( 

1156 self.protocol.handle_request({ 

1157 "version": 5, 

1158 "command": "updateAncestorBlock" 

1159 }), 

1160 {"errorcode": -204}, 

1161 ) 

1162 

1163 self.assertEqual( 

1164 self.protocol.handle_request({ 

1165 "version": 5, 

1166 "command": "updateAncestorBlock", 

1167 "blocks": 123 

1168 }), 

1169 {"errorcode": -204}, 

1170 ) 

1171 

1172 self.assertEqual( 

1173 self.protocol.handle_request({ 

1174 "version": 5, 

1175 "command": "updateAncestorBlock", 

1176 "blocks": [] 

1177 }), 

1178 {"errorcode": -204}, 

1179 ) 

1180 

1181 self.assertEqual( 

1182 self.protocol.handle_request({ 

1183 "version": 5, 

1184 "command": "updateAncestorBlock", 

1185 "blocks": ["ok", 333, "another-ok"], 

1186 }), 

1187 {"errorcode": -204}, 

1188 ) 

1189 

1190 def test_update_ancestor_block_notimplemented(self): 

1191 with self.assertRaises(NotImplementedError): 

1192 self.protocol.handle_request({ 

1193 "command": "updateAncestorBlock", 

1194 "version": 5, 

1195 "blocks": ["a-block"] 

1196 }) 

1197 

1198 with self.assertRaises(NotImplementedError): 

1199 self.protocol.handle_request({ 

1200 "command": 

1201 "updateAncestorBlock", 

1202 "version": 5, 

1203 "blocks": ["first-block", "second-block", "third-block"], 

1204 }) 

1205 

1206 def test_blockchain_parameters_notimplemented(self): 

1207 with self.assertRaises(NotImplementedError): 

1208 self.protocol.handle_request({ 

1209 "command": "blockchainParameters", 

1210 "version": 5 

1211 }) 

1212 

1213 def test_signer_heartbeat_invalid_ud_value(self): 

1214 self.assertEqual( 

1215 self.protocol.handle_request({ 

1216 "command": "signerHeartbeat", 

1217 "version": 5 

1218 }), 

1219 {"errorcode": -301}, 

1220 ) 

1221 

1222 self.assertEqual( 

1223 self.protocol.handle_request({ 

1224 "command": "signerHeartbeat", 

1225 "udValue": 123, 

1226 "version": 5 

1227 }), 

1228 {"errorcode": -301}, 

1229 ) 

1230 

1231 self.assertEqual( 

1232 self.protocol.handle_request({ 

1233 "command": "signerHeartbeat", 

1234 "udValue": "notahex", 

1235 "version": 5 

1236 }), 

1237 {"errorcode": -301}, 

1238 ) 

1239 

1240 self.assertEqual( 

1241 self.protocol.handle_request({ 

1242 "command": "signerHeartbeat", 

1243 "udValue": "aabbcc", 

1244 "version": 5 

1245 }), 

1246 {"errorcode": -301}, 

1247 ) 

1248 

1249 def test_signer_heartbeat_notimplemented(self): 

1250 with self.assertRaises(NotImplementedError): 

1251 self.protocol.handle_request({ 

1252 "command": "signerHeartbeat", 

1253 "udValue": "aa"*16, 

1254 "version": 5 

1255 }) 

1256 

1257 def test_ui_heartbeat_invalid_ud_value(self): 

1258 self.assertEqual( 

1259 self.protocol.handle_request({ 

1260 "command": "uiHeartbeat", 

1261 "version": 5 

1262 }), 

1263 {"errorcode": -301}, 

1264 ) 

1265 

1266 self.assertEqual( 

1267 self.protocol.handle_request({ 

1268 "command": "uiHeartbeat", 

1269 "udValue": 123, 

1270 "version": 5 

1271 }), 

1272 {"errorcode": -301}, 

1273 ) 

1274 

1275 self.assertEqual( 

1276 self.protocol.handle_request({ 

1277 "command": "uiHeartbeat", 

1278 "udValue": "notahex", 

1279 "version": 5 

1280 }), 

1281 {"errorcode": -301}, 

1282 ) 

1283 

1284 self.assertEqual( 

1285 self.protocol.handle_request({ 

1286 "command": "uiHeartbeat", 

1287 "udValue": "aabbcc", 

1288 "version": 5 

1289 }), 

1290 {"errorcode": -301}, 

1291 ) 

1292 

1293 def test_ui_heartbeat_notimplemented(self): 

1294 with self.assertRaises(NotImplementedError): 

1295 self.protocol.handle_request({ 

1296 "command": "uiHeartbeat", 

1297 "udValue": "aa"*32, 

1298 "version": 5 

1299 })