Skip to content

Commit

Permalink
FEAT: Backwards compatibility for amount_msat and date (#15)
Browse files Browse the repository at this point in the history
* refactor to use amount_msat
* change timestamp to date
  • Loading branch information
dni authored Aug 7, 2023
1 parent 1eb1588 commit 5af269a
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 102 deletions.
14 changes: 7 additions & 7 deletions bolt11/decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def decode(pr: str) -> Bolt11:
tag, tagdata, data_part = _pull_tagged(data_part)
data_length = len(tagdata or []) / 5 # type: ignore

# MUST skip over unknown fields, OR an f field with unknown version, OR p, h, s or n
# fields that do NOT have data_lengths of 52, 52, 52 or 53, respectively.
# MUST skip over unknown fields, OR an f field with unknown version, OR p, h,
# s or n fields that do NOT have data_lengths of 52, 52, 52 or 53, respectively.

if tag == "p" and data_length == 52 and not hasattr(tags, "p"):
tags["p"] = trim_to_bytes(tagdata).hex() # type: ignore
Expand Down Expand Up @@ -85,9 +85,9 @@ def decode(pr: str) -> Bolt11:

signature = Signature(signature_data=signature_data, signing_data=hrp.encode() + data_part.tobytes())

# A reader MUST check that the `signature` is valid (see the `n` tagged field specified below).
# A reader MUST use the `n` field to validate the signature instead of
# performing signature recovery if a valid `n` field is provided.
# A reader MUST check that the `signature` is valid (see the `n` tagged field
# specified below). A reader MUST use the `n` field to validate the signature
# instead of performing signature recovery if a valid `n` field is provided.
if hasattr(tags, "n"):
# TODO: research why no test runs this?
signature.verify(tags["n"])
Expand All @@ -96,8 +96,8 @@ def decode(pr: str) -> Bolt11:

return Bolt11(
currency=currency,
amount=amount_msat,
timestamp=timestamp,
amount_msat=amount_msat,
date=timestamp,
signature=signature,
tags=tags,
)
10 changes: 5 additions & 5 deletions bolt11/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def _tagged_int(tag: bytes):
return value


def _create_hrp(currency: str, amount: Optional[MilliSatoshi]) -> str:
def _create_hrp(currency: str, amount_msat: Optional[MilliSatoshi]) -> str:
hrp = "ln" + currency
if amount:
hrp += msat_to_amount(amount)
if amount_msat:
hrp += msat_to_amount(amount_msat)
return hrp


Expand All @@ -51,7 +51,7 @@ def encode(invoice: Bolt11, private_key: Optional[str] = None) -> str:
if not invoice.description and not invoice.description_hash:
raise ValueError("Must include either 'd' or 'h'")

timestamp = BitArray(uint=invoice.timestamp, length=35)
timestamp = BitArray(uint=invoice.date, length=35)
tags = BitArray()

for k, tag in invoice.tags.items():
Expand Down Expand Up @@ -79,7 +79,7 @@ def encode(invoice: Bolt11, private_key: Optional[str] = None) -> str:
elif k == "r":
tags += _tagged("r", tag.data)

hrp = _create_hrp(invoice.currency, invoice.amount)
hrp = _create_hrp(invoice.currency, invoice.amount_msat)

if private_key:
invoice.signature = Signature.from_private_key(private_key, hrp, timestamp + tags)
Expand Down
12 changes: 6 additions & 6 deletions bolt11/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class Bolt11:
"""Bolt11 Lightning invoice."""

currency: str
timestamp: int
date: int
tags: Dict[str, Any]
amount: Optional[MilliSatoshi] = None
amount_msat: Optional[MilliSatoshi] = None
signature: Optional[Signature] = None

@property
Expand All @@ -51,7 +51,7 @@ def metadata(self) -> Optional[str]:

@property
def dt(self) -> datetime:
return datetime.fromtimestamp(self.timestamp)
return datetime.fromtimestamp(self.date)

@property
def expiry(self) -> Optional[int]:
Expand Down Expand Up @@ -89,8 +89,8 @@ def payee(self) -> Optional[str]:
def json(self) -> str:
json_data = {
"currency": self.currency,
"amount": int(self.amount) if self.amount else 0,
"timestamp": self.timestamp,
"amount_msat": int(self.amount_msat) if self.amount_msat else 0,
"date": self.date,
"signature": self.signature.hex if self.signature else "",
}
if self.description:
Expand Down Expand Up @@ -120,7 +120,7 @@ def json(self) -> str:
def has_expired(self) -> bool:
if self.expiry is None:
return False
return time.time() > self.timestamp + self.expiry
return time.time() > self.date + self.expiry

def is_mainnet(self) -> bool:
return self.currency == "bc"
Expand Down
Loading

0 comments on commit 5af269a

Please sign in to comment.