|
| 1 | +""" |
| 2 | +Ward Protocol - Permissioned Domains (XLS-80) |
| 3 | +
|
| 4 | +Institutional compliance layer for insurance pools using XRPL Permissioned Domains. |
| 5 | +Enables KYC/AML-compliant access control through credential-based permissioning. |
| 6 | +""" |
| 7 | + |
| 8 | +import hashlib |
| 9 | +import structlog |
| 10 | +from typing import Dict, List, Optional, Any |
| 11 | +from xrpl.models import PermissionedDomainSet, PermissionedDomainDelete |
| 12 | +from xrpl.models.transactions.deposit_preauth import Credential |
| 13 | +from xrpl.wallet import Wallet |
| 14 | +from xrpl.asyncio.transaction import submit_and_wait |
| 15 | +from xrpl.asyncio.clients import AsyncWebsocketClient |
| 16 | + |
| 17 | +logger = structlog.get_logger() |
| 18 | + |
| 19 | + |
| 20 | +class PermissionedDomainManager: |
| 21 | + """ |
| 22 | + Manages XLS-80 Permissioned Domains for institutional access control. |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self, xrpl_client: AsyncWebsocketClient): |
| 26 | + self.client = xrpl_client |
| 27 | + self.logger = logger.bind(module="permissioned_domains") |
| 28 | + |
| 29 | + def generate_domain_id(self, owner: str, sequence: int) -> str: |
| 30 | + """Generate DomainID hash per XLS-80 specification.""" |
| 31 | + space_key = "PD" |
| 32 | + data = f"{space_key}{owner}{sequence}".encode() |
| 33 | + domain_hash = hashlib.sha512(data).hexdigest() |
| 34 | + return domain_hash[:64] |
| 35 | + |
| 36 | + def _encode_credential_type(self, credential_type: str) -> str: |
| 37 | + """ |
| 38 | + Encode credential type to hex string. |
| 39 | + |
| 40 | + Per XLS-70/80 spec, CredentialType must be hex-encoded. |
| 41 | + """ |
| 42 | + return credential_type.encode('utf-8').hex().upper() |
| 43 | + |
| 44 | + async def create_domain( |
| 45 | + self, |
| 46 | + wallet: Wallet, |
| 47 | + accepted_credentials: List[Dict[str, str]], |
| 48 | + domain_id: Optional[str] = None |
| 49 | + ) -> Dict[str, Any]: |
| 50 | + """ |
| 51 | + Create or modify a permissioned domain on XRPL. |
| 52 | + |
| 53 | + Args: |
| 54 | + wallet: Wallet to sign transaction |
| 55 | + accepted_credentials: List with format [{"issuer": "rAddr", "credential_type": "TYPE"}] |
| 56 | + domain_id: Optional existing domain to modify |
| 57 | + |
| 58 | + Returns: |
| 59 | + Dict with domain_id, transaction hash, and result |
| 60 | + """ |
| 61 | + self.logger.info( |
| 62 | + "creating_permissioned_domain", |
| 63 | + owner=wallet.classic_address, |
| 64 | + num_credentials=len(accepted_credentials) |
| 65 | + ) |
| 66 | + |
| 67 | + if not accepted_credentials or len(accepted_credentials) > 10: |
| 68 | + raise ValueError("AcceptedCredentials must have 1-10 entries") |
| 69 | + |
| 70 | + # Create Credential objects with hex-encoded types |
| 71 | + formatted_credentials = [] |
| 72 | + for cred in accepted_credentials: |
| 73 | + if "issuer" not in cred or "credential_type" not in cred: |
| 74 | + raise ValueError("Each credential needs issuer and credential_type") |
| 75 | + |
| 76 | + # Encode credential type to hex |
| 77 | + hex_type = self._encode_credential_type(cred["credential_type"]) |
| 78 | + |
| 79 | + # Check max length (64 bytes = 128 hex chars) |
| 80 | + if len(hex_type) > 128: |
| 81 | + raise ValueError(f"CredentialType too long: {cred['credential_type']}") |
| 82 | + |
| 83 | + # Create Credential object |
| 84 | + credential_obj = Credential( |
| 85 | + issuer=cred["issuer"], |
| 86 | + credential_type=hex_type |
| 87 | + ) |
| 88 | + formatted_credentials.append(credential_obj) |
| 89 | + |
| 90 | + # Build transaction |
| 91 | + tx_params = { |
| 92 | + "account": wallet.classic_address, |
| 93 | + "accepted_credentials": formatted_credentials |
| 94 | + } |
| 95 | + |
| 96 | + if domain_id: |
| 97 | + tx_params["domain_id"] = domain_id |
| 98 | + |
| 99 | + tx = PermissionedDomainSet(**tx_params) |
| 100 | + |
| 101 | + try: |
| 102 | + result = await submit_and_wait(tx, self.client, wallet) |
| 103 | + |
| 104 | + if result.is_successful(): |
| 105 | + sequence = result.result.get("Sequence", 0) |
| 106 | + generated_domain_id = self.generate_domain_id( |
| 107 | + wallet.classic_address, |
| 108 | + sequence |
| 109 | + ) |
| 110 | + |
| 111 | + self.logger.info( |
| 112 | + "domain_created", |
| 113 | + domain_id=generated_domain_id, |
| 114 | + tx_hash=result.result["hash"] |
| 115 | + ) |
| 116 | + |
| 117 | + return { |
| 118 | + "success": True, |
| 119 | + "domain_id": generated_domain_id, |
| 120 | + "tx_hash": result.result["hash"], |
| 121 | + "owner": wallet.classic_address, |
| 122 | + "sequence": sequence, |
| 123 | + "accepted_credentials": accepted_credentials |
| 124 | + } |
| 125 | + else: |
| 126 | + error = result.result.get("engine_result", "Unknown error") |
| 127 | + self.logger.error("domain_creation_failed", error=error) |
| 128 | + return { |
| 129 | + "success": False, |
| 130 | + "error": error |
| 131 | + } |
| 132 | + |
| 133 | + except Exception as e: |
| 134 | + self.logger.error("domain_creation_exception", error=str(e)) |
| 135 | + raise |
| 136 | + |
| 137 | + async def delete_domain( |
| 138 | + self, |
| 139 | + wallet: Wallet, |
| 140 | + domain_id: str |
| 141 | + ) -> Dict[str, Any]: |
| 142 | + """Delete a permissioned domain (only owner can delete).""" |
| 143 | + self.logger.info("deleting_domain", domain_id=domain_id) |
| 144 | + |
| 145 | + tx = PermissionedDomainDelete( |
| 146 | + account=wallet.classic_address, |
| 147 | + domain_id=domain_id |
| 148 | + ) |
| 149 | + |
| 150 | + try: |
| 151 | + result = await submit_and_wait(tx, self.client, wallet) |
| 152 | + |
| 153 | + if result.is_successful(): |
| 154 | + return { |
| 155 | + "success": True, |
| 156 | + "tx_hash": result.result["hash"], |
| 157 | + "domain_id": domain_id |
| 158 | + } |
| 159 | + else: |
| 160 | + return { |
| 161 | + "success": False, |
| 162 | + "error": result.result.get("engine_result", "Transaction failed") |
| 163 | + } |
| 164 | + |
| 165 | + except Exception as e: |
| 166 | + self.logger.error("domain_deletion_exception", error=str(e)) |
| 167 | + raise |
| 168 | + |
| 169 | + |
| 170 | +def log_domain_configuration(): |
| 171 | + """Log permissioned domain configuration on startup""" |
| 172 | + logger.info( |
| 173 | + "permissioned_domains_configured", |
| 174 | + xls_standard="XLS-80", |
| 175 | + status="enabled" |
| 176 | + ) |
0 commit comments