initi
This commit is contained in:
@@ -116,6 +116,139 @@ def decode(pr: str) -> Invoice:
|
||||
return invoice
|
||||
|
||||
|
||||
|
||||
def encode(options):
|
||||
""" Convert options into LnAddr and pass it to the encoder
|
||||
"""
|
||||
addr = LnAddr()
|
||||
addr.currency = options.currency
|
||||
addr.fallback = options.fallback if options.fallback else None
|
||||
if options.amount:
|
||||
addr.amount = options.amount
|
||||
if options.timestamp:
|
||||
addr.date = int(options.timestamp)
|
||||
|
||||
addr.paymenthash = unhexlify(options.paymenthash)
|
||||
|
||||
if options.description:
|
||||
addr.tags.append(('d', options.description))
|
||||
if options.description_hashed:
|
||||
addr.tags.append(('h', options.description_hashed))
|
||||
if options.expires:
|
||||
addr.tags.append(('x', options.expires))
|
||||
|
||||
if options.fallback:
|
||||
addr.tags.append(('f', options.fallback))
|
||||
|
||||
for r in options.route:
|
||||
splits = r.split('/')
|
||||
route=[]
|
||||
while len(splits) >= 5:
|
||||
route.append((unhexlify(splits[0]),
|
||||
unhexlify(splits[1]),
|
||||
int(splits[2]),
|
||||
int(splits[3]),
|
||||
int(splits[4])))
|
||||
splits = splits[5:]
|
||||
assert(len(splits) == 0)
|
||||
addr.tags.append(('r', route))
|
||||
return lnencode(addr, options.privkey)
|
||||
|
||||
|
||||
def lnencode(addr, privkey):
|
||||
if addr.amount:
|
||||
amount = Decimal(str(addr.amount))
|
||||
# We can only send down to millisatoshi.
|
||||
if amount * 10**12 % 10:
|
||||
raise ValueError("Cannot encode {}: too many decimal places".format(
|
||||
addr.amount))
|
||||
|
||||
amount = addr.currency + shorten_amount(amount)
|
||||
else:
|
||||
amount = addr.currency if addr.currency else ''
|
||||
|
||||
hrp = 'ln' + amount
|
||||
|
||||
# Start with the timestamp
|
||||
data = bitstring.pack('uint:35', addr.date)
|
||||
|
||||
# Payment hash
|
||||
data += tagged_bytes('p', addr.paymenthash)
|
||||
tags_set = set()
|
||||
|
||||
for k, v in addr.tags:
|
||||
|
||||
# BOLT #11:
|
||||
#
|
||||
# A writer MUST NOT include more than one `d`, `h`, `n` or `x` fields,
|
||||
if k in ('d', 'h', 'n', 'x'):
|
||||
if k in tags_set:
|
||||
raise ValueError("Duplicate '{}' tag".format(k))
|
||||
|
||||
if k == 'r':
|
||||
route = bitstring.BitArray()
|
||||
for step in v:
|
||||
pubkey, channel, feebase, feerate, cltv = step
|
||||
route.append(bitstring.BitArray(pubkey) + bitstring.BitArray(channel) + bitstring.pack('intbe:32', feebase) + bitstring.pack('intbe:32', feerate) + bitstring.pack('intbe:16', cltv))
|
||||
data += tagged('r', route)
|
||||
elif k == 'f':
|
||||
data += encode_fallback(v, addr.currency)
|
||||
elif k == 'd':
|
||||
data += tagged_bytes('d', v.encode())
|
||||
elif k == 'x':
|
||||
# Get minimal length by trimming leading 5 bits at a time.
|
||||
expirybits = bitstring.pack('intbe:64', v)[4:64]
|
||||
while expirybits.startswith('0b00000'):
|
||||
expirybits = expirybits[5:]
|
||||
data += tagged('x', expirybits)
|
||||
elif k == 'h':
|
||||
data += tagged_bytes('h', hashlib.sha256(v.encode('utf-8')).digest())
|
||||
elif k == 'n':
|
||||
data += tagged_bytes('n', v)
|
||||
else:
|
||||
# FIXME: Support unknown tags?
|
||||
raise ValueError("Unknown tag {}".format(k))
|
||||
|
||||
tags_set.add(k)
|
||||
|
||||
# BOLT #11:
|
||||
#
|
||||
# A writer MUST include either a `d` or `h` field, and MUST NOT include
|
||||
# both.
|
||||
if 'd' in tags_set and 'h' in tags_set:
|
||||
raise ValueError("Cannot include both 'd' and 'h'")
|
||||
if not 'd' in tags_set and not 'h' in tags_set:
|
||||
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])
|
||||
|
||||
return bech32_encode(hrp, bitarray_to_u5(data))
|
||||
|
||||
class LnAddr(object):
|
||||
def __init__(self, paymenthash=None, amount=None, currency='bc', tags=None, date=None):
|
||||
self.date = int(time.time()) if not date else int(date)
|
||||
self.tags = [] if not tags else tags
|
||||
self.unknown_tags = []
|
||||
self.paymenthash=paymenthash
|
||||
self.signature = None
|
||||
self.pubkey = None
|
||||
self.currency = currency
|
||||
self.amount = amount
|
||||
|
||||
def __str__(self):
|
||||
return "LnAddr[{}, amount={}{} tags=[{}]]".format(
|
||||
hexlify(self.pubkey.serialize()).decode('utf-8'),
|
||||
self.amount, self.currency,
|
||||
", ".join([k + '=' + str(v) for k, v in self.tags])
|
||||
)
|
||||
|
||||
|
||||
|
||||
def _unshorten_amount(amount: str) -> int:
|
||||
"""Given a shortened amount, return millisatoshis"""
|
||||
# BOLT #11:
|
||||
|
||||
Reference in New Issue
Block a user