Ejemplo n.º 1
0
    async def payment_pre_processing(self, other_address, seq, command,
                                     payment):
        """An async method to let VASP perform custom business logic to a
        successsful (sequenced & ACKed) command prior to normal processing.
        For example it can be used to check whether the payment is in terminal
        status. The command could have originated either from the other VASP
        or this VASP (see `command.origin` to determine this).
        Args:
            other_address (str): the encoded Diem Blockchain address of the other VASP.
            seq (int): the sequence number into the shared command sequence.
            command (ProtocolCommand): the command that lead to the new or
                updated payment.
            payment (PaymentObject): the payment resulting from this command.
        Returns None or a context objext that will be passed on the
        other business context functions.
        """

        if (payment.sender.status.as_status() == Status.ready_for_settlement
                and payment.receiver.status.as_status()
                == Status.ready_for_settlement):
            if self.is_sender(payment):
                ref_id = payment.reference_id
                transaction_id = get_transaction_id_from_reference_id(ref_id)
                transaction = get_single_transaction(transaction_id)
                add_metadata_signature(ref_id, payment.recipient_signature)

                if transaction.status == TransactionStatus.COMPLETED:
                    return None
                if transaction.status == TransactionStatus.READY_FOR_ON_CHAIN:
                    start_settle_offchain(transaction_id=transaction.id)

                # TODO: What should happen in this case?
                if transaction.status == TransactionStatus.CANCELED:
                    raise ValueError("what should happen in this case?")

            else:
                receiver_subaddress = LibraAddress.from_encoded_str(
                    payment.receiver.address).get_subaddress_hex()
                txn_id = add_transaction(
                    amount=payment.action.amount,
                    currency=DiemCurrency(payment.action.currency),
                    payment_type=TransactionType.OFFCHAIN,
                    status=TransactionStatus.READY_FOR_ON_CHAIN,
                    source_id=None,
                    source_address=LibraAddress.from_encoded_str(
                        payment.sender.address).get_onchain_address_hex(),
                    source_subaddress=LibraAddress.from_encoded_str(
                        payment.sender.address).get_subaddress_hex(),
                    destination_id=get_account_id_from_subaddr(
                        receiver_subaddress),
                    destination_address=LibraAddress.from_encoded_str(
                        payment.receiver.address).get_onchain_address_hex(),
                    destination_subaddress=receiver_subaddress,
                    sequence=None,
                    blockchain_version=None,
                    reference_id=payment.reference_id,
                    metadata_signature=payment.recipient_signature,
                )
Ejemplo n.º 2
0
 def get_account_id(self, payment) -> int:
     my_actor = payment.sender if self.is_sender(
         payment) else payment.receiver
     address = LibraAddress.from_encoded_str(my_actor.address)
     subaddress = address.get_subaddress_hex()
     account_id = get_account_id_from_subaddr(subaddress)
     if account_id is None:
         role = self.get_my_role(payment)
         raise BusinessForceAbort(
             OffChainErrorCode.payment_invalid_libra_subaddress,
             f"Subaccount {subaddress} does not exist in {role}.",
         )
     return account_id
Ejemplo n.º 3
0
 def get_peer_compliance_verification_key(self,
                                          other_addr: str) -> ComplianceKey:
     address = LibraAddress.from_encoded_str(
         other_addr).get_onchain_address_hex()
     return self.context.get_vasp_public_compliance_key(address)
Ejemplo n.º 4
0
def actor_to_libra_address(actor: PaymentActor) -> LibraAddress:
    return LibraAddress.from_encoded_str(actor.address)