Browse Source

invoices: make sure that OnchainInvoice .exp and .time are not None

related: #6284
bip39-recovery
SomberNight 5 years ago
parent
commit
dee5d52948
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 6
      electrum/invoices.py
  2. 12
      electrum/wallet.py
  3. 18
      electrum/wallet_db.py

6
electrum/invoices.py

@ -113,9 +113,9 @@ class Invoice(StoredObject):
@attr.s
class OnchainInvoice(Invoice):
message = attr.ib(type=str, kw_only=True)
amount_sat = attr.ib(kw_only=True) # type: Union[None, int, str] # in satoshis. can be '!'
exp = attr.ib(type=int, kw_only=True)
time = attr.ib(type=int, kw_only=True)
amount_sat = attr.ib(kw_only=True) # type: Union[int, str] # in satoshis. can be '!'
exp = attr.ib(type=int, kw_only=True, validator=attr.validators.instance_of(int))
time = attr.ib(type=int, kw_only=True, validator=attr.validators.instance_of(int))
id = attr.ib(type=str, kw_only=True)
outputs = attr.ib(kw_only=True, converter=_decode_outputs) # type: List[PartialTxOutput]
bip70 = attr.ib(type=str, kw_only=True) # type: Optional[str]

12
electrum/wallet.py

@ -691,14 +691,21 @@ class Abstract_Wallet(AddressSynchronizer, ABC):
amount = '!'
else:
amount = sum(x.value for x in outputs)
timestamp = None
exp = None
if URI:
timestamp = URI.get('time')
exp = URI.get('exp')
timestamp = timestamp or int(time.time())
exp = exp or 0
invoice = OnchainInvoice(
type=PR_TYPE_ONCHAIN,
amount_sat=amount,
outputs=outputs,
message=message,
id=bh2u(sha256(repr(outputs))[0:16]),
time=URI.get('time') if URI else int(time.time()),
exp=URI.get('exp') if URI else 0,
time=timestamp,
exp=exp,
bip70=None,
requestor=None,
)
@ -1776,6 +1783,7 @@ class Abstract_Wallet(AddressSynchronizer, ABC):
amount_sat = amount_sat or 0
timestamp = int(time.time())
_id = bh2u(sha256d(address + "%d"%timestamp))[0:10]
expiration = expiration or 0
return OnchainInvoice(
type=PR_TYPE_ONCHAIN,
outputs=[(TYPE_ADDRESS, address, amount_sat)],

18
electrum/wallet_db.py

@ -52,7 +52,7 @@ if TYPE_CHECKING:
OLD_SEED_VERSION = 4 # electrum versions < 2.0
NEW_SEED_VERSION = 11 # electrum versions >= 2.0
FINAL_SEED_VERSION = 30 # electrum >= 2.7 will set this to prevent
FINAL_SEED_VERSION = 31 # electrum >= 2.7 will set this to prevent
# old versions from overwriting new format
@ -178,6 +178,7 @@ class WalletDB(JsonDB):
self._convert_version_28()
self._convert_version_29()
self._convert_version_30()
self._convert_version_31()
self.put('seed_version', FINAL_SEED_VERSION) # just to be sure
self._after_upgrade_tasks()
@ -667,6 +668,21 @@ class WalletDB(JsonDB):
raise Exception(f"unknown invoice type: {_type}")
self.data['seed_version'] = 30
def _convert_version_31(self):
if not self._is_upgrade_method_needed(30, 30):
return
from .invoices import PR_TYPE_ONCHAIN
requests = self.data.get('payment_requests', {})
invoices = self.data.get('invoices', {})
for d in [invoices, requests]:
for key, item in list(d.items()):
if item['type'] == PR_TYPE_ONCHAIN:
item['amount_sat'] = item['amount_sat'] or 0
item['exp'] = item['exp'] or 0
item['time'] = item['time'] or 0
self.data['seed_version'] = 31
def _convert_imported(self):
if not self._is_upgrade_method_needed(0, 13):
return

Loading…
Cancel
Save