language changes
This commit is contained in:
parent
2f8f950679
commit
6fe6ff565d
@ -456,7 +456,7 @@ def verify_signature(msg, addr, sig_str):
|
||||
script = None
|
||||
hash160 = None
|
||||
invalid_addr_fmt_msg = "Invalid address format - must be one of p2pkh, p2sh-p2wpkh, or p2wpkh."
|
||||
invalid_addr = "Invalid signature for msg - address mismatch."
|
||||
invalid_addr = "Invalid signature for message."
|
||||
|
||||
if addr[0] in "1mn":
|
||||
addr_fmt = AF_CLASSIC
|
||||
@ -467,8 +467,7 @@ def verify_signature(msg, addr, sig_str):
|
||||
# p2wsh
|
||||
raise ValueError(invalid_addr_fmt_msg)
|
||||
addr_fmt = AF_P2WPKH
|
||||
decoded_addr = ngu.codecs.segwit_decode(addr)
|
||||
hash160 = decoded_addr[2]
|
||||
_, _, hash160 = ngu.codecs.segwit_decode(addr)
|
||||
elif addr[0] in "32":
|
||||
addr_fmt = AF_P2WPKH_P2SH
|
||||
decoded_addr = ngu.codecs.b58_decode(addr)
|
||||
@ -478,14 +477,16 @@ def verify_signature(msg, addr, sig_str):
|
||||
|
||||
try:
|
||||
sig_bytes = a2b_base64(sig_str)
|
||||
if not sig_bytes:
|
||||
if not sig_bytes or len(sig_bytes) != 65:
|
||||
# can return b'' in case of wrong, can also raise
|
||||
raise ValueError("invalid base64 signature")
|
||||
raise ValueError("invalid encoding")
|
||||
|
||||
header_byte = sig_bytes[0]
|
||||
header_base = chains.current_chain().sig_hdr_base(addr_fmt)
|
||||
if (header_byte - header_base) not in (0, 1, 2, 3):
|
||||
# wrong header value only - this can still verify OK
|
||||
warnings += "Specified address format does not match signature header byte format."
|
||||
|
||||
# least two significant bits
|
||||
rec_id = (header_byte - 27) & 0x03
|
||||
# need to normalize it to 31 base for ngu
|
||||
@ -493,13 +494,16 @@ def verify_signature(msg, addr, sig_str):
|
||||
sig = ngu.secp256k1.signature(bytes([new_header_byte]) + sig_bytes[1:])
|
||||
except ValueError as e:
|
||||
raise ValueError("Parsing signature failed - %s." % str(e))
|
||||
|
||||
digest = chains.current_chain().hash_message(msg.encode('ascii'))
|
||||
try:
|
||||
rec_pubkey = sig.verify_recover(digest)
|
||||
except ValueError as e:
|
||||
raise ValueError("Invalid signature for msg - %s." % str(e))
|
||||
|
||||
rec_pubkey_bytes = rec_pubkey.to_bytes()
|
||||
rec_hash160 = ngu.hash.hash160(rec_pubkey_bytes)
|
||||
|
||||
if script:
|
||||
target = bytes([0, 20]) + rec_hash160
|
||||
target = ngu.hash.hash160(target)
|
||||
@ -508,6 +512,7 @@ def verify_signature(msg, addr, sig_str):
|
||||
else:
|
||||
if rec_hash160 != hash160:
|
||||
raise ValueError(invalid_addr)
|
||||
|
||||
return warnings
|
||||
|
||||
async def verify_armored_signed_msg(contents):
|
||||
@ -524,35 +529,38 @@ async def verify_armored_signed_msg(contents):
|
||||
try:
|
||||
sig_warn = verify_signature(msg, addr, sig_str)
|
||||
except Exception as e:
|
||||
await ux_show_story(str(e), title="FAILURE")
|
||||
await ux_show_story(str(e), title="ERROR")
|
||||
return
|
||||
|
||||
title = "OK"
|
||||
title = "CORRECT"
|
||||
warn_msg = ""
|
||||
err_msg = ""
|
||||
story = "Signature verifies as signed by address:\n %s" % addr
|
||||
story = "Good signature by address:\n %s" % addr
|
||||
|
||||
digest_prob = verify_signed_file_digest(msg)
|
||||
if digest_prob:
|
||||
err, digest_warn = digest_prob
|
||||
if digest_warn:
|
||||
title = "WARNING"
|
||||
wmsg_base = "not present. SHA256SUM verification not possible."
|
||||
wmsg_base = "not present. Contents verification not possible."
|
||||
if len(digest_warn) == 1:
|
||||
fname = digest_warn[0][0]
|
||||
warn_msg += "\n\n'%s' is %s" % (fname, wmsg_base)
|
||||
warn_msg += "'%s' is %s" % (fname, wmsg_base)
|
||||
else:
|
||||
warn_msg += "\n\nFiles:\n" + "\n".join("> %s" % fname for fname, _ in digest_warn)
|
||||
warn_msg += "Files:\n" + "\n".join("> %s" % fname for fname, _ in digest_warn)
|
||||
warn_msg += "\nare %s" % wmsg_base
|
||||
|
||||
if err:
|
||||
title = "FAILURE"
|
||||
title = "ERROR"
|
||||
for fname, calc, got in err:
|
||||
err_msg += ("\n\nReferenced file '%s' has wrong contents.\n"
|
||||
"Got:\n%s\n\nCalculated:\n%s" % (fname, got, calc))
|
||||
err_msg += ("Referenced file '%s' has wrong contents.\n"
|
||||
"Got:\n%s\n\nExpected:\n%s" % (fname, got, calc))
|
||||
|
||||
if sig_warn:
|
||||
# we know not ours only because wrong recid header used & not BIP-137 compliant
|
||||
story = "Correctly signed, but not by this Coldcard. %s" % sig_warn
|
||||
await ux_show_story(story + err_msg + warn_msg, title=title)
|
||||
|
||||
await ux_show_story('\n\n'.join(m for m in [err_msg, story, warn_msg] if m), title=title)
|
||||
|
||||
async def verify_txt_sig_file(filename):
|
||||
from files import CardSlot, CardMissingError, needs_microsd
|
||||
|
||||
@ -389,8 +389,8 @@ def test_verify_signature_file(way, addr_fmt, path, msg, sign_on_microsd, goto_h
|
||||
got = f.read()
|
||||
assert should == got
|
||||
title, story = verify_armored_signature(way, fname, should)
|
||||
assert title == "OK"
|
||||
assert "Signature verifies as signed by address" in story
|
||||
assert title == "CORRECT"
|
||||
assert "Good signature" in story
|
||||
assert addr in story
|
||||
if addr_fmt == "p2pkh":
|
||||
res = bitcoind.rpc.verifymessage(addr, sig, msg)
|
||||
@ -416,7 +416,7 @@ def test_verify_signature_file_header_warning(way, addr_sig, microsd_path, verif
|
||||
with open(microsd_path(fname), "w") as f:
|
||||
f.write(tmplt)
|
||||
title, story = verify_armored_signature(way, fname, tmplt)
|
||||
assert title == "OK"
|
||||
assert title == "CORRECT"
|
||||
if (addr[0] + sig[0]) not in ("mH", "2I", "tJ"): # not in correct pair
|
||||
assert text in story
|
||||
assert warning in story
|
||||
@ -444,10 +444,10 @@ def test_verify_signature_file_fail(way, addr_sig, microsd_path, cap_story, goto
|
||||
error_map = {
|
||||
0: "Parsing signature failed",
|
||||
1: "Invalid address format - must be one of p2pkh, p2sh-p2wpkh, or p2wpkh.",
|
||||
2: "Parsing signature failed - sig len != 65.",
|
||||
3: "Invalid signature for msg - address mismatch."
|
||||
2: "Parsing signature failed - invalid encoding.",
|
||||
3: "Invalid signature for message."
|
||||
}
|
||||
tmplt = RFC_SIGNATURE_TEMPLATE.format(msg="aaaaaaaaa",addr=addr, sig=sig)
|
||||
tmplt = RFC_SIGNATURE_TEMPLATE.format(msg="aaaaaaaaa", addr=addr, sig=sig)
|
||||
|
||||
try:
|
||||
os.unlink(microsd_path(fname))
|
||||
@ -458,7 +458,7 @@ def test_verify_signature_file_fail(way, addr_sig, microsd_path, cap_story, goto
|
||||
f.write(tmplt)
|
||||
|
||||
title, story = verify_armored_signature(way, fname, tmplt)
|
||||
assert title == "FAILURE"
|
||||
assert title == "ERROR"
|
||||
assert error_map[err_no] in story
|
||||
|
||||
|
||||
@ -497,8 +497,8 @@ def test_verify_signature_file_digest_prob(binary, microsd_path, cap_story, pick
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
assert title == "OK"
|
||||
assert "Signature verifies as signed by address" in story
|
||||
assert title == "CORRECT"
|
||||
assert "Good signature" in story
|
||||
need_keypress("y") # back in File Management
|
||||
|
||||
# modify contents of the file
|
||||
@ -512,11 +512,11 @@ def test_verify_signature_file_digest_prob(binary, microsd_path, cap_story, pick
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
assert title == "FAILURE"
|
||||
assert "Signature verifies as signed by address" in story # sig is still correct
|
||||
assert title == "ERROR"
|
||||
assert "Good signature" in story # sig is still correct
|
||||
assert ("'%s' has wrong contents" % fname) in story
|
||||
assert ("Got:\n%s" % orig_digest) in story
|
||||
assert ("Calculated:\n%s" % mod_digest) in story
|
||||
assert ("Expected:\n%s" % mod_digest) in story
|
||||
need_keypress("y") # back in File Management
|
||||
|
||||
# remove file
|
||||
@ -527,8 +527,9 @@ def test_verify_signature_file_digest_prob(binary, microsd_path, cap_story, pick
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
assert title == "WARNING"
|
||||
assert "Signature verifies as signed by address" in story # sig is still correct
|
||||
assert ("'%s' is not present. SHA256SUM verification not possible." % fname) in story
|
||||
assert "Good signature" in story # sig is still correct
|
||||
assert ("'%s' is not present" % fname) in story
|
||||
assert 'Contents verification not possible' in story
|
||||
need_keypress("y") # back in File Management
|
||||
|
||||
|
||||
@ -570,8 +571,8 @@ def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story,
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
assert title == "OK"
|
||||
assert "Signature verifies as signed by address" in story
|
||||
assert title == "CORRECT"
|
||||
assert "Good signature" in story
|
||||
need_keypress("y") # back in File Management
|
||||
|
||||
# change contents of 0th file
|
||||
@ -586,11 +587,11 @@ def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story,
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
assert title == "FAILURE"
|
||||
assert "Signature verifies as signed by address" in story # sig is still correct
|
||||
assert title == "ERROR"
|
||||
assert "Good signature" in story # sig is still correct
|
||||
assert ("'%s' has wrong contents" % fname) in story
|
||||
assert ("Got:\n%s" % orig_digest) in story
|
||||
assert ("Calculated:\n%s" % mod_digest) in story
|
||||
assert ("Expected:\n%s" % mod_digest) in story
|
||||
need_keypress("y") # back in File Management
|
||||
|
||||
# change contents of 1st file remove 0th file
|
||||
@ -608,12 +609,13 @@ def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story,
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
assert title == "FAILURE"
|
||||
assert "Signature verifies as signed by address" in story # sig is still correct
|
||||
assert title == "ERROR"
|
||||
assert "Good signature" in story # sig is still correct
|
||||
assert ("'%s' has wrong contents" % fname1) in story
|
||||
assert ("Got:\n%s" % orig_digest) in story
|
||||
assert ("Calculated:\n%s" % mod_digest) in story
|
||||
assert ("'%s' is not present. SHA256SUM verification not possible." % fname0) in story
|
||||
assert ("Expected:\n%s" % mod_digest) in story
|
||||
assert ("'%s' is not present" % fname0) in story
|
||||
assert 'Contents verification not possible' in story
|
||||
need_keypress("y") # back in File Management
|
||||
|
||||
# remove 1st file too
|
||||
@ -624,10 +626,10 @@ def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story,
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
assert title == "WARNING"
|
||||
assert "Signature verifies as signed by address" in story # sig is still correct
|
||||
assert "Good signature" in story # sig is still correct
|
||||
warn_msg = "Files:\n" + "\n".join("> %s" % fname for fname in (fname0, fname1))
|
||||
assert warn_msg in story
|
||||
assert "are not present. SHA256SUM verification not possible." in story
|
||||
assert 'Contents verification not possible' in story
|
||||
need_keypress("y") # back in File Management
|
||||
|
||||
# reboult valid signed files
|
||||
@ -641,8 +643,8 @@ def test_verify_signature_file_digest_prob_multi(f_num, microsd_path, cap_story,
|
||||
pick_menu_item(sig_name)
|
||||
time.sleep(0.1)
|
||||
title, story = cap_story()
|
||||
assert title == "OK"
|
||||
assert "Signature verifies as signed by address" in story
|
||||
assert title == "CORRECT"
|
||||
assert "Good signature" in story
|
||||
need_keypress("y") # back in File Management
|
||||
|
||||
# EOF
|
||||
|
||||
Loading…
Reference in New Issue
Block a user