pyln-proto: Use only coincurve for libsecp256k1 bindings

secp256k1 Python library is not maintained anymore and coincurve was
already used in the `wire` module.

Changelog-None
Signed-off-by: Michal Rostecki <mrostecki@mailfence.com>
This commit is contained in:
Michal Rostecki
2020-04-26 19:55:15 +02:00
committed by Christian Decker
parent 66fb7d6026
commit db0a2c082a
3 changed files with 23 additions and 16 deletions

View File

@@ -6,7 +6,7 @@ import base58
import bitstring
import hashlib
import re
import secp256k1
import coincurve
import time
import struct
@@ -180,14 +180,14 @@ class Invoice(object):
def __str__(self):
return "Invoice[{}, amount={}{} tags=[{}]]".format(
hexlify(self.pubkey.serialize()).decode('utf-8'),
hexlify(self.pubkey.format()).decode('utf-8'),
self.amount, self.currency,
", ".join([k + '=' + str(v) for k, v in self.tags])
)
@property
def hexpubkey(self):
return hexlify(self.pubkey.serialize()).decode('ASCII')
return hexlify(self.pubkey.format()).decode('ASCII')
@property
def hexpaymenthash(self):
@@ -273,11 +273,8 @@ class Invoice(object):
raise ValueError("Must include either 'd' or 'h'")
# We actually sign the hrp, then data (padded to 8 bits with zeroes).
privkey = secp256k1.PrivateKey(bytes(unhexlify(privkey)))
sig = privkey.ecdsa_sign_recoverable(bytearray([ord(c) for c in hrp]) + data.tobytes())
# This doesn't actually serialize, but returns a pair of values :(
sig, recid = privkey.ecdsa_recoverable_serialize(sig)
data += bytes(sig) + bytes([recid])
privkey = coincurve.PrivateKey(secret=bytes(unhexlify(privkey)))
data += privkey.sign_recoverable(bytearray([ord(c) for c in hrp]) + data.tobytes())
return bech32_encode(hrp, bitarray_to_u5(data))
@@ -374,8 +371,7 @@ class Invoice(object):
if data_length != 53:
inv.unknown_tags.append((tag, tagdata))
continue
inv.pubkey = secp256k1.PublicKey(flags=secp256k1.ALL_FLAGS)
inv.pubkey.deserialize(trim_to_bytes(tagdata))
inv.pubkey = coincurve.PublicKey(trim_to_bytes(tagdata))
elif tag == 'c':
inv.min_final_cltv_expiry = tagdata.uint
@@ -395,11 +391,11 @@ class Invoice(object):
if not inv.pubkey.ecdsa_verify(bytearray([ord(c) for c in hrp]) + data.tobytes(), inv.signature):
raise ValueError('Invalid signature')
else: # Recover pubkey from signature.
inv.pubkey = secp256k1.PublicKey(flags=secp256k1.ALL_FLAGS)
inv.signature = inv.pubkey.ecdsa_recoverable_deserialize(
sigdecoded[0:64], sigdecoded[64])
inv.pubkey.public_key = inv.pubkey.ecdsa_recover(
bytearray([ord(c) for c in hrp]) + data.tobytes(), inv.signature)
inv.signature = coincurve.ecdsa.deserialize_recoverable(
sigdecoded[0:65])
inv.pubkey = coincurve.PublicKey.from_signature_and_message(
sigdecoded[0:65],
bytearray([ord(c) for c in hrp]) + data.tobytes())
return inv