tlnbase: verify signature in node_announcement - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 969708316caa5d4a7f711cbe638354ee28accf5d DIR parent b662a9d87bb84759dd07b4879eae055d432a2698 HTML Author: ThomasV <thomasv@electrum.org> Date: Sun, 15 Apr 2018 15:25:31 +0200 lnbase: verify signature in node_announcement Diffstat: M lib/lnbase.py | 41 +++++++++++++++++++++++++++++++ M lib/tests/test_lnbase.py | 16 +++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) --- DIR diff --git a/lib/lnbase.py b/lib/lnbase.py t@@ -5,6 +5,7 @@ """ from ecdsa.util import sigdecode_der, sigencode_string_canonize +from ecdsa import VerifyingKey from ecdsa.curves import SECP256k1 import subprocess import queue t@@ -382,6 +383,9 @@ class Peer(PrintError): f = getattr(self, 'on_' + message_type) except AttributeError: return + # raw message is needed to check signature + if message_type=='node_announcement': + payload['raw'] = message f(payload) def on_error(self, payload): t@@ -409,6 +413,43 @@ class Peer(PrintError): def on_funding_locked(self, payload): pass + def on_node_announcement(self, payload): + pubkey = payload['node_id'] + signature = payload['signature'] + h = bitcoin.Hash(payload['raw'][66:]) + if not bitcoin.verify_signature(pubkey, signature, h): + return False + self.s = payload['addresses'] + def read(n): + data, self.s = self.s[0:n], self.s[n:] + return data + while self.s: + atype = ord(read(1)) + if atype == 0: + pass + elif atype == 1: + ipv4_addr = '.'.join(map(lambda x: '%d'%x, read(4))) + port = int.from_bytes(read(2), byteorder="big") + x = ipv4_addr, port, binascii.hexlify(pubkey) + self.print_error('node announcement', x) + node_list.append(x) + elif atype == 2: + ipv6_addr = read(16) + port = read(2) + print(ipv6_addr, port) + else: + pass + continue + + def on_init(self, payload): + pass + + def on_channel_update(self, payload): + pass + + def on_channel_announcement(self, payload): + pass + #def open_channel(self, funding_sat, push_msat): # self.send_message(gen_msg('open_channel', funding_satoshis=funding_sat, push_msat=push_msat)) DIR diff --git a/lib/tests/test_lnbase.py b/lib/tests/test_lnbase.py t@@ -1,7 +1,7 @@ import json import unittest from lib.util import bh2u -from lib.lnbase import make_commitment, get_obscured_ctn +from lib.lnbase import make_commitment, get_obscured_ctn, Peer from lib.transaction import Transaction from lib import bitcoin t@@ -25,6 +25,8 @@ local_delayedpubkey = bytes.fromhex('03fd5960528dc152014952efdb702a88f71e3c1653b local_revocation_pubkey = bytes.fromhex('0212a140cd0c6539d07cd08dfe09984dec3251ea808b892efeac3ede9402bf2b19') # funding wscript = 5221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae +peer = Peer('ecdsa.net', 9735, '038370f0e7a03eded3e1d41dc081084a87f0afa1c5b22090b4f3abb391eb15d8ff') + class Test_LNBase(unittest.TestCase): def test_simple_commitment_tx_with_no_HTLCs(self): t@@ -95,3 +97,15 @@ class Test_LNBase(unittest.TestCase): output_htlc_timeout_tx_3 = "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219703000000000000000001b80b0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100daee1808f9861b6c3ecd14f7b707eca02dd6bdfc714ba2f33bc8cdba507bb182022026654bf8863af77d74f51f4e0b62d461a019561bb12acb120d3f7195d148a554014730440220643aacb19bbb72bd2b635bc3f7375481f5981bace78cdd8319b2988ffcc6704202203d27784ec8ad51ed3bd517a05525a5139bb0b755dd719e0054332d186ac0872701008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000" # local_signature = 30440220549e80b4496803cbc4a1d09d46df50109f546d43fbbf86cd90b174b1484acd5402205f12a4f995cb9bded597eabfee195a285986aa6d93ae5bb72507ebc6a4e2349e output_htlc_success_tx_4 = "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219704000000000000000001a00f0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402207e0410e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625153f8596692a801d014730440220549e80b4496803cbc4a1d09d46df50109f546d43fbbf86cd90b174b1484acd5402205f12a4f995cb9bded597eabfee195a285986aa6d93ae5bb72507ebc6a4e2349e012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000" + + def test_message_channel_update(self): + message = b'\x01\x02\x0e\xd2\x86&\x10\xd8\xf8\xd9\xa6\x19\xfbK}\xa69W\xd5\xb7\xd0=\xbaV\xfa\x93K\xb8/V\xa2\x821_#y9\xb4\xd9\x88mt\x98\xf2\x9f\x92w\xa3"6\x1c\xcb\xb2\xca\xa7\xc5/A\xa05a8J\x8a\xbe?CI\x7f\xd7\xf8&\x95q\x08\xf4\xa3\x0f\xd9\xce\xc3\xae\xbay\x97 \x84\xe9\x0e\xad\x01\xea3\t\x00\x00\x00\x00\x13\xb86\x00\x00Z\x00\x00Z\xd3@]\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x03\xe8\x00\x00\x00\x01\x00\x00\x00\n' + peer.process_message(message) + + def test_message_channel_announcement(self): parazyd.org:70 /git/electrum/commit/969708316caa5d4a7f711cbe638354ee28accf5d.gph:113: line too long