async def _save_payment_txn(self, event: jsonrpc.Event) -> None: # noqa: C901 metadata = txnmetadata.decode_structure(event.data.metadata) if isinstance(metadata, diem_types.GeneralMetadataV0): subaddress = utils.hex(metadata.to_subaddress) try: res = self.store.find(Subaddress, subaddress_hex=subaddress) except NotFoundError: self.logger.exception("invalid general metadata to_subaddress") return await self._refund(RefundReason.invalid_subaddress, event) return self._create_txn(res.account_id, event, subaddress_hex=res.subaddress_hex) elif isinstance(metadata, diem_types.TravelRuleMetadataV0): try: cmd = self.store.find(PaymentCommand, reference_id=metadata.off_chain_reference_id) except NotFoundError: self.logger.exception("invalid travel rule metadata off-chain reference id") return await self._refund(RefundReason.other, event) return self._create_txn(cmd.account_id, event, reference_id=cmd.reference_id) elif isinstance(metadata, diem_types.RefundMetadataV0): version = int(metadata.transaction_version) reason = RefundReason.from_diem_type(metadata.reason) account_id = await self._find_refund_account_id(version) self._create_txn(account_id, event, refund_diem_txn_version=version, refund_reason=reason) elif isinstance(metadata, diem_types.PaymentMetadataV0): reference_id = str(uuid.UUID(bytes=metadata.to_bytes())) try: ref_id = self.store.find(ReferenceID, reference_id=reference_id) except NotFoundError: self.logger.exception("Transaction with reference ID %r not found" % reference_id) return await self._refund(RefundReason.other, event) return self._create_txn(ref_id.account_id, event, reference_id=reference_id) else: raise ValueError("unrecognized metadata: %r" % event.data.metadata)
async def _find_refund_account_id(self, version: int) -> str: diem_txns = await self.client.get_transactions(version, 1, include_events=True) if diem_txns: diem_txn = diem_txns[0] self.logger.error("diem_txn script: %r", diem_txn.transaction.script) event = diem_txn.events[0] original_metadata = txnmetadata.decode_structure(event.data.metadata) if isinstance(original_metadata, diem_types.GeneralMetadataV0): original_sender = utils.hex(original_metadata.from_subaddress) try: return self.store.find(Subaddress, subaddress_hex=original_sender).account_id except NotFoundError: self.logger.exception("invalid original transaction metadata from_subaddress") else: self.logger.error("invalid original txn metadata %r", diem_txn.transaction.script.metadata) else: self.logger.error("could not find diem txn by version: %s", version) return PENDING_INBOUND_ACCOUNT_ID
async def _handle_offchain_payment_command( self, sender_address: str, request: CommandRequestObject) -> None: new_offchain_cmd = await self.app.offchain.process_inbound_payment_command_request( sender_address, request) try: cmd = self.app.store.find( PaymentCommand, reference_id=new_offchain_cmd.reference_id()) if new_offchain_cmd != cmd.to_offchain_command(): self._update_payment_command(cmd, new_offchain_cmd, validate=True) except NotFoundError: subaddress = utils.hex( new_offchain_cmd.my_subaddress(self.app.diem_account.hrp)) account_id = self.app.store.find( Subaddress, subaddress_hex=subaddress).account_id self._create_payment_command(account_id, new_offchain_cmd, validate=True)
def test_hex(): assert utils.hex(None) == "" assert utils.hex(b"abcd") == "61626364"
def subaddress(account_identifier: str) -> str: return utils.hex( identifier.decode_account_subaddress(account_identifier, hrp))
def subaddress(uri: str) -> str: return utils.hex(identifier.decode_intent(uri, hrp).subaddress)