improved ledger test
@@ -13,6 +13,7 @@ import unittest
from os import path
from btclib import der, dsa
+from btclib.address import p2pkh_address
from btclib.signmessage import (_deserialize, _serialize, _verify, msgsign,
verify)
from btclib.utils import bytes_from_hexstring
@@ -321,20 +322,23 @@ class TestSignMessage(unittest.TestCase):
def test_ledger(self):
"""Hybrid ECDSA Bicoin message signature generated by Ledger Nano S"""
- pubkey = '04ae3d0d5c669ed364636e79e72abc012a33be63e537babddf56bfd393256acf6dba0fac21da6386513674573a2d7baff4375c9b6d2498383853c52f0565f97f1a'
- addr = '1J6674MtZBpfHytdNofLUX6sLHAUaG33uK'
- msg = "hello world".encode()
- msg = b'\x18Bitcoin Signed Message:\n' + len(msg).to_bytes(1, 'big') + msg
# customized Ledger DER encoding, with no sighash
# first byte = 0x30 + key_id
DER = "3044022044487c80833b7025739f450751c1d6624118e32e5f922b5a40a407efb48382e202200f2b6e53448f8e219ee1c2f109fa5b0a2b8bae482a4a81cf8c54f8c168260886"
+
+ pubkey = '022374b3b6b06b65a3b831f857634ea135bf10b014d5bba0f935cb9eb26a4b6547'
+ msg = "hello world".encode()
+ msg = b'\x18Bitcoin Signed Message:\n' + len(msg).to_bytes(1, 'big') + msg
+
+ # ECDSA signature verification
+ dsa._verify(msg, pubkey, DER)
ecdsasig, sighash = der.deserialize(DER)
self.assertIsNone(sighash)
- dsa._verify(msg, pubkey, DER)
- dsa._verify(msg, pubkey, ecdsasig)
+ # equivalent Bitcoin Message Signature verification
rec_flag = 27 + bytes_from_hexstring(DER)[0] - 48
msgsig = _serialize(rec_flag, ecdsasig[0], ecdsasig[1])
+ addr = p2pkh_address(pubkey)
_verify(msg, addr, msgsig)
if __name__ == '__main__':
