payment_identifier: imports, typing declarations
This commit is contained in:
@@ -11,12 +11,12 @@ from .contacts import AliasNotFoundException
|
|||||||
from .i18n import _
|
from .i18n import _
|
||||||
from .invoices import Invoice
|
from .invoices import Invoice
|
||||||
from .logging import Logger
|
from .logging import Logger
|
||||||
from .util import parse_max_spend, format_satoshis_plain, InvoiceError
|
from .util import parse_max_spend, InvoiceError
|
||||||
from .util import get_asyncio_loop, log_exceptions
|
from .util import get_asyncio_loop, log_exceptions
|
||||||
from .transaction import PartialTxOutput
|
from .transaction import PartialTxOutput
|
||||||
from .lnurl import decode_lnurl, request_lnurl, callback_lnurl, LNURLError, lightning_address_to_url
|
from .lnurl import decode_lnurl, request_lnurl, callback_lnurl, LNURLError, lightning_address_to_url
|
||||||
from .bitcoin import COIN, TOTAL_COIN_SUPPLY_LIMIT_IN_BTC, opcodes, construct_script
|
from .bitcoin import opcodes, construct_script
|
||||||
from .lnaddr import lndecode, LnDecodeException, LnInvoiceException
|
from .lnaddr import LnInvoiceException
|
||||||
from .lnutil import IncompatibleOrInsaneFeatures
|
from .lnutil import IncompatibleOrInsaneFeatures
|
||||||
from .bip21 import parse_bip21_URI, InvalidBitcoinURI, LIGHTNING_URI_SCHEME, BITCOIN_BIP21_URI_SCHEME
|
from .bip21 import parse_bip21_URI, InvalidBitcoinURI, LIGHTNING_URI_SCHEME, BITCOIN_BIP21_URI_SCHEME
|
||||||
from . import paymentrequest
|
from . import paymentrequest
|
||||||
@@ -52,21 +52,21 @@ RE_SCRIPT_FN = r'script\((.*)\)'
|
|||||||
|
|
||||||
|
|
||||||
class PaymentIdentifierState(IntEnum):
|
class PaymentIdentifierState(IntEnum):
|
||||||
EMPTY = 0 # Initial state.
|
EMPTY = 0 # Initial state.
|
||||||
INVALID = 1 # Unrecognized PI
|
INVALID = 1 # Unrecognized PI
|
||||||
AVAILABLE = 2 # PI contains a payable destination
|
AVAILABLE = 2 # PI contains a payable destination
|
||||||
# payable means there's enough addressing information to submit to one
|
# payable means there's enough addressing information to submit to one
|
||||||
# of the channels Electrum supports (on-chain, lightning)
|
# of the channels Electrum supports (on-chain, lightning)
|
||||||
NEED_RESOLVE = 3 # PI contains a recognized destination format, but needs an online resolve step
|
NEED_RESOLVE = 3 # PI contains a recognized destination format, but needs an online resolve step
|
||||||
LNURLP_FINALIZE = 4 # PI contains a resolved LNURLp, but needs amount and comment to resolve to a bolt11
|
LNURLP_FINALIZE = 4 # PI contains a resolved LNURLp, but needs amount and comment to resolve to a bolt11
|
||||||
MERCHANT_NOTIFY = 5 # PI contains a valid payment request and on-chain destination. It should notify
|
MERCHANT_NOTIFY = 5 # PI contains a valid payment request and on-chain destination. It should notify
|
||||||
# the merchant payment processor of the tx after on-chain broadcast,
|
# the merchant payment processor of the tx after on-chain broadcast,
|
||||||
# and supply a refund address (bip70)
|
# and supply a refund address (bip70)
|
||||||
MERCHANT_ACK = 6 # PI notified merchant. nothing to be done.
|
MERCHANT_ACK = 6 # PI notified merchant. nothing to be done.
|
||||||
ERROR = 50 # generic error
|
ERROR = 50 # generic error
|
||||||
NOT_FOUND = 51 # PI contains a recognized destination format, but resolve step was unsuccessful
|
NOT_FOUND = 51 # PI contains a recognized destination format, but resolve step was unsuccessful
|
||||||
MERCHANT_ERROR = 52 # PI failed notifying the merchant after broadcasting onchain TX
|
MERCHANT_ERROR = 52 # PI failed notifying the merchant after broadcasting onchain TX
|
||||||
INVALID_AMOUNT = 53 # Specified amount not accepted
|
INVALID_AMOUNT = 53 # Specified amount not accepted
|
||||||
|
|
||||||
|
|
||||||
class PaymentIdentifierType(IntEnum):
|
class PaymentIdentifierType(IntEnum):
|
||||||
@@ -504,7 +504,7 @@ class PaymentIdentifier(Logger):
|
|||||||
self.logger.debug(f'multiline: {outputs!r}, {self.error}')
|
self.logger.debug(f'multiline: {outputs!r}, {self.error}')
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def parse_address_and_amount(self, line: str) -> 'PartialTxOutput':
|
def parse_address_and_amount(self, line: str) -> PartialTxOutput:
|
||||||
try:
|
try:
|
||||||
x, y = line.split(',')
|
x, y = line.split(',')
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@@ -515,7 +515,7 @@ class PaymentIdentifier(Logger):
|
|||||||
amount = self.parse_amount(y)
|
amount = self.parse_amount(y)
|
||||||
return PartialTxOutput(scriptpubkey=scriptpubkey, value=amount)
|
return PartialTxOutput(scriptpubkey=scriptpubkey, value=amount)
|
||||||
|
|
||||||
def parse_output(self, x: str) -> Tuple[bytes, bool]:
|
def parse_output(self, x: str) -> Tuple[Optional[bytes], bool]:
|
||||||
try:
|
try:
|
||||||
address = self.parse_address(x)
|
address = self.parse_address(x)
|
||||||
return bytes.fromhex(bitcoin.address_to_script(address)), True
|
return bytes.fromhex(bitcoin.address_to_script(address)), True
|
||||||
@@ -530,7 +530,7 @@ class PaymentIdentifier(Logger):
|
|||||||
|
|
||||||
return None, False
|
return None, False
|
||||||
|
|
||||||
def parse_script(self, x: str):
|
def parse_script(self, x: str) -> str:
|
||||||
script = ''
|
script = ''
|
||||||
for word in x.split():
|
for word in x.split():
|
||||||
if word[0:3] == 'OP_':
|
if word[0:3] == 'OP_':
|
||||||
@@ -541,7 +541,7 @@ class PaymentIdentifier(Logger):
|
|||||||
script += construct_script([word])
|
script += construct_script([word])
|
||||||
return script
|
return script
|
||||||
|
|
||||||
def parse_amount(self, x: str):
|
def parse_amount(self, x: str) -> str | int:
|
||||||
x = x.strip()
|
x = x.strip()
|
||||||
if not x:
|
if not x:
|
||||||
raise Exception("Amount is empty")
|
raise Exception("Amount is empty")
|
||||||
@@ -652,7 +652,7 @@ class PaymentIdentifier(Logger):
|
|||||||
if parts and len(parts) > 0 and bitcoin.is_address(parts[0]):
|
if parts and len(parts) > 0 and bitcoin.is_address(parts[0]):
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
data = self.contacts.resolve(key) # TODO: don't use contacts as delegate to resolve openalias, separate.
|
data = self.contacts.resolve(key) # TODO: don't use contacts as delegate to resolve openalias, separate.
|
||||||
return data
|
return data
|
||||||
except AliasNotFoundException as e:
|
except AliasNotFoundException as e:
|
||||||
self.logger.info(f'OpenAlias not found: {repr(e)}')
|
self.logger.info(f'OpenAlias not found: {repr(e)}')
|
||||||
|
|||||||
Reference in New Issue
Block a user