tfix lnaddr.py following rebase - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit f66377604d5802b9cf870ab3c5563949a2b18c95 DIR parent 5666188e9e75ff4b697acb3015b46ffb8d265f30 HTML Author: ThomasV <thomasv@electrum.org> Date: Sat, 26 May 2018 18:04:11 +0200 fix lnaddr.py following rebase Diffstat: M electrum/ecc.py | 10 +++++----- M lib/lightning_payencode/lnaddr.py | 15 ++++++++------- 2 files changed, 13 insertions(+), 12 deletions(-) --- DIR diff --git a/electrum/ecc.py b/electrum/ecc.py t@@ -256,9 +256,9 @@ class ECPubkey(object): def __ne__(self, other): return not (self == other) - def verify_message_for_address(self, sig65: bytes, message: bytes) -> None: + def verify_message_for_address(self, sig65: bytes, message: bytes, algo=lambda x: sha256d(msg_magic(x))) -> None: assert_bytes(message) - h = sha256d(msg_magic(message)) + h = algo(message) public_key, compressed = self.from_signature65(sig65, h) # check public key if public_key != self: t@@ -403,12 +403,12 @@ class ECPrivkey(ECPubkey): sigencode=der_sig_from_r_and_s, sigdecode=get_r_and_s_from_der_sig) - def sign_message(self, message: bytes, is_compressed: bool) -> bytes: + def sign_message(self, message: bytes, is_compressed: bool, algo=lambda x: sha256d(msg_magic(x))) -> bytes: def bruteforce_recid(sig_string): for recid in range(4): sig65 = construct_sig65(sig_string, recid, is_compressed) try: - self.verify_message_for_address(sig65, message) + self.verify_message_for_address(sig65, message, algo) return sig65, recid except Exception as e: continue t@@ -416,7 +416,7 @@ class ECPrivkey(ECPubkey): raise Exception("error: cannot sign message. no recid fits..") message = to_bytes(message, 'utf8') - msg_hash = sha256d(msg_magic(message)) + msg_hash = algo(message) sig_string = self.sign(msg_hash, sigencode=sig_string_from_r_and_s, sigdecode=get_r_and_s_from_sig_string) DIR diff --git a/lib/lightning_payencode/lnaddr.py b/lib/lightning_payencode/lnaddr.py t@@ -1,7 +1,7 @@ #! /usr/bin/env python3 import ecdsa.curves from ecdsa.ecdsa import generator_secp256k1 -from ..bitcoin import MyVerifyingKey, GetPubKey, regenerate_key, hash160_to_b58_address, b58_address_to_hash160, ser_to_point, verify_signature +from ..bitcoin import hash160_to_b58_address, b58_address_to_hash160 from hashlib import sha256 from ..segwit_addr import bech32_encode, bech32_decode, CHARSET from binascii import hexlify, unhexlify t@@ -9,6 +9,7 @@ from bitstring import BitArray from decimal import Decimal from .. import constants +from .. import ecc import bitstring import hashlib import re t@@ -222,8 +223,8 @@ def lnencode(addr, privkey): # We actually sign the hrp, then data (padded to 8 bits with zeroes). msg = hrp.encode("ascii") + data.tobytes() - privkey = regenerate_key(privkey) - sig = privkey.sign_message(msg, is_compressed=False, algo=lambda x: sha256(x).digest()) + privkey = ecc.ECPrivkey(privkey) + sig = privkey.sign_message(msg, is_compressed=False, algo=lambda x:sha256(x).digest()) recovery_flag = bytes([sig[0] - 27]) sig = bytes(sig[1:]) + recovery_flag data += sig t@@ -369,19 +370,19 @@ def lndecode(a, verbose=False, expected_hrp=constants.net.SEGWIT_HRP): # A reader MUST check that the `signature` is valid (see the `n` tagged # field specified below). addr.signature = sigdecoded[:65] + hrp_hash = sha256(hrp.encode("ascii") + data.tobytes()).digest() if addr.pubkey: # Specified by `n` # BOLT #11: # # A reader MUST use the `n` field to validate the signature instead of # performing signature recovery if a valid `n` field is provided. - if not verify_signature(addr.pubkey, sigdecoded[:64], sha256(hrp.encode("ascii") + data.tobytes()).digest()): - raise ValueError('Invalid signature') + ecc.ECPubkey(addr.pubkey).verify_message_hash(sigdecoded[:64], hrp_hash) pubkey_copy = addr.pubkey class WrappedBytesKey: serialize = lambda: pubkey_copy addr.pubkey = WrappedBytesKey else: # Recover pubkey from signature. - addr.pubkey = SerializableKey(MyVerifyingKey.from_signature(sigdecoded[:64], sigdecoded[64], sha256(hrp.encode("ascii") + data.tobytes()).digest(), curve = ecdsa.curves.SECP256k1)) + addr.pubkey = SerializableKey(ecc.ECPubkey.from_sig_string(sigdecoded[:64], sigdecoded[64], hrp_hash)) return addr t@@ -389,4 +390,4 @@ class SerializableKey: def __init__(self, pubkey): self.pubkey = pubkey def serialize(self): - return GetPubKey(self.pubkey.pubkey, True) + return self.pubkey.get_public_key_bytes(True)