Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 26
Diem ID Payment in Miniwallet with E2E Test#309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| def to_bytes(self) -> bytes: | ||
| """Convert reference_id to bytes.""" | ||
| return bytes(typing.cast(typing.Iterable[int], self.reference_id)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Copyright (c) The Diem Core Contributors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| def create_diem_id(user_identifier: str, vasp_domain_identifier: str) -> str: | ||
| diem_id = user_identifier + "@" + vasp_domain_identifier | ||
| if not is_diem_id(diem_id): | ||
| raise ValueError(f"{diem_id} is not a valid DiemID.") | ||
| return diem_id | ||
| def is_diem_id(account_identifier: str) -> bool: | ||
| if "@" not in account_identifier: | ||
| return False | ||
| user_identifier = account_identifier.split("@", 1)[0] | ||
| vasp_identifier = account_identifier.split("@", 1)[1] | ||
| if len(user_identifier) > 64 or len(vasp_identifier) > 63: | ||
| return False | ||
| for ch in user_identifier: | ||
| if not ch.isalnum() and ch != ".": | ||
| return False | ||
| for ch in vasp_identifier: | ||
| if not ch.isalnum() and ch != ".": | ||
| return False | ||
| return True | ||
| def get_user_identifier_from_diem_id(diem_id: str) -> str: | ||
sunmilee marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if not is_diem_id(diem_id): | ||
| raise ValueError(f"{diem_id} is not a valid DiemID.") | ||
| return diem_id.split("@", 1)[0] | ||
| def get_vasp_identifier_from_diem_id(diem_id: str) -> str: | ||
sunmilee marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if not is_diem_id(diem_id): | ||
| raise ValueError(f"{diem_id} is not a valid DiemID.") | ||
| return diem_id.split("@", 1)[1] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,16 +2,17 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from typing import List, Tuple, Dict, Optional, Any, Callable | ||
| import uuid | ||
| from json.decoder import JSONDecodeError | ||
| from .store import InMemoryStore | ||
| from .store import InMemoryStore, NotFoundError | ||
| from .pending_account import PENDING_INBOUND_ACCOUNT_ID | ||
| from .diem_account import DiemAccount | ||
| from .models import Subaddress, Account, Transaction, Event, KycSample, PaymentCommand | ||
| from .models import Subaddress, Account, Transaction, Event, KycSample, PaymentCommand, ReferenceID | ||
| from .event_puller import EventPuller | ||
| from .json_input import JsonInput | ||
| from ... import LocalAccount | ||
| from .... import jsonrpc, offchain, utils | ||
| from ....offchain import KycDataObject | ||
| from .... import jsonrpc, offchain, utils, identifier | ||
| from ....offchain import KycDataObject, ErrorCode | ||
| import threading, logging, numpy, time | ||
| @@ -46,6 +47,9 @@ def create_account(self, data: JsonInput) -> Dict[str, str]: | ||
| # it is used for testing offchain api. | ||
| disable_background_tasks=data.get_nullable("disable_outbound_request", bool), | ||
| ) | ||
| if len(self.diem_account.diem_id_domains()) != 0: | ||
| diem_id = identifier.diem_id.create_diem_id(str(account.id), self.diem_account.diem_id_domains()[0]) | ||
| self.store.update(account, diem_id=diem_id) | ||
| balances = data.get_nullable("balances", dict) | ||
| if balances: | ||
| for c, a in balances.items(): | ||
| @@ -59,8 +63,14 @@ def create_account(self, data: JsonInput) -> Dict[str, str]: | ||
| def create_account_payment(self, account_id: str, data: JsonInput) -> Dict[str, Any]: | ||
| self.store.find(Account, id=account_id) | ||
| payee = data.get("payee", str, self._validate_account_identifier) | ||
| subaddress_hex = None if self.offchain.is_my_account_id(payee) else self._gen_subaddress(account_id).hex() | ||
| subaddress_hex = None | ||
| payee = data.get("payee", str) | ||
| if identifier.diem_id.is_diem_id(payee): | ||
| self._validate_diem_id(payee) | ||
| else: | ||
| self._validate_account_identifier("payee", payee) | ||
| subaddress_hex = None if self.offchain.is_my_account_id(payee) else self._gen_subaddress(account_id).hex() | ||
| txn = self._create_transaction( | ||
| account_id, | ||
| Transaction.Status.pending, | ||
| @@ -129,7 +139,13 @@ def _send_pending_payments(self) -> None: | ||
| continue | ||
| self.logger.info("processing %s", txn) | ||
| try: | ||
| if self.offchain.is_my_account_id(str(txn.payee)): | ||
| if identifier.diem_id.is_diem_id(str(txn.payee)): | ||
| vasp_identifier = identifier.diem_id.get_vasp_identifier_from_diem_id(str(txn.payee)) | ||
| if vasp_identifier in self.diem_account.diem_id_domains(): | ||
| self._send_internal_payment_txn(txn) | ||
| else: | ||
| self._send_external_payment_txn(txn) | ||
| elif self.offchain.is_my_account_id(str(txn.payee)): | ||
| self._send_internal_payment_txn(txn) | ||
| else: | ||
| self._send_external_payment_txn(txn) | ||
| @@ -142,6 +158,7 @@ def _send_pending_payments(self) -> None: | ||
| self.store.update(txn, status=Transaction.Status.canceled, cancel_reason=str(e)) | ||
| def _send_internal_payment_txn(self, txn: Transaction) -> None: | ||
| # TODO diem_id: handle DiemID case for internal | ||
| _, payee_subaddress = self.diem_account.decode_account_identifier(str(txn.payee)) | ||
| subaddress = self.store.find(Subaddress, subaddress_hex=utils.hex(payee_subaddress)) | ||
| self.store.create( | ||
| @@ -171,7 +188,33 @@ def _send_external_payment_txn(self, txn: Transaction) -> None: | ||
| else: | ||
| self._start_external_payment_txn(txn) | ||
| def _start_external_payment_txn(self, txn: Transaction) -> None: | ||
| def _start_external_payment_txn(self, txn: Transaction) -> None: # noqa: C901 | ||
| if identifier.diem_id.is_diem_id(str(txn.payee)): | ||
| reference_id = self._create_reference_id(txn) | ||
| try: | ||
| vasp_identifier = identifier.diem_id.get_vasp_identifier_from_diem_id(str(txn.payee)) | ||
| domain_map = self.diem_client.get_diem_id_domain_map() | ||
| diem_id_address = domain_map.get(vasp_identifier) | ||
| if diem_id_address is None: | ||
| raise ValueError(f"Diem ID domain {vasp_identifier} was not found") | ||
sunmilee marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| self.store.update(txn, reference_id=reference_id) | ||
| response = self.offchain.ref_id_exchange_request( | ||
| sender=str(self.store.find(Account, id=txn.account_id).diem_id), | ||
| sender_address=self.diem_account.account_identifier(), | ||
| receiver=str(txn.payee), | ||
| reference_id=reference_id, | ||
| counterparty_account_identifier=identifier.encode_account( | ||
| diem_id_address, None, self.diem_account.hrp | ||
| ), | ||
| sign=self.diem_account.sign_by_compliance_key, | ||
| ) | ||
| receiver_address, _ = self.diem_account.decode_account_identifier( | ||
| response.result.get("receiver_address") # pyre-ignore | ||
| ) | ||
| self.store.update(txn, payee_onchain_address=receiver_address.to_hex()) | ||
| except offchain.Error as e: | ||
| if e.obj.code == ErrorCode.duplicate_reference_id: | ||
| return | ||
| if self.offchain.is_under_dual_attestation_limit(txn.currency, txn.amount): | ||
| if not txn.signed_transaction: | ||
| signed_txn = self.diem_account.submit_p2p(txn, self.txn_metadata(txn)) | ||
| @@ -244,3 +287,35 @@ def _gen_subaddress(self, account_id: str) -> bytes: | ||
| sub = self.store.next_id().to_bytes(8, byteorder="big") | ||
| self.store.create(Subaddress, account_id=account_id, subaddress_hex=sub.hex()) | ||
| return sub | ||
| def validate_unique_reference_id(self, reference_id: str) -> None: | ||
| try: | ||
| duplicate_ref_id = self.store.find(ReferenceID, reference_id=reference_id) | ||
| raise ValueError(f"{reference_id} exists for account ID {duplicate_ref_id.account_id}") | ||
| except NotFoundError: | ||
| return | ||
| # Check the payee diem ID can be found in the domain map | ||
| # Check if payee is in the same wallet, the receiver account exists | ||
| def _validate_diem_id(self, diem_id: str) -> None: | ||
| payee_vasp_identifier = identifier.diem_id.get_vasp_identifier_from_diem_id(diem_id) | ||
| domain_map = self.diem_client.get_diem_id_domain_map() | ||
| payee_onchain_address = domain_map.get(payee_vasp_identifier) | ||
| if payee_onchain_address is None: | ||
| raise ValueError(f"Diem ID domain {payee_vasp_identifier} was not found") | ||
| if payee_vasp_identifier in self.diem_account.diem_id_domains(): | ||
| self.store.find(Account, diem_id=diem_id) | ||
| def _create_reference_id(self, txn: Transaction) -> str: | ||
| while True: | ||
| reference_id = str(uuid.uuid4()) | ||
| try: | ||
| self.store.create( | ||
| ReferenceID, | ||
| before_create=lambda data: self.validate_unique_reference_id(data["reference_id"]), | ||
| account_id=txn.account_id, | ||
| reference_id=reference_id, | ||
| ) | ||
| return reference_id | ||
| except ValueError: | ||
| continue | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.