anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// [AIR-3][AIS-3][BPC-3][AIT-3] BIP353 API Handler Implementation

use std::sync::{Arc, Mutex};
use axum::{
    extract::{Json, Path, State, Query},
    http::StatusCode,
    response::{Response, IntoResponse},
    routing::{get, post, put},
    Router,
};
use serde::{Serialize, Deserialize};
use tracing::{info, error, warn};

use crate::bip::{
    Bip353, Bip353Config, Bip353Status, Bip353Error, PaymentRecipient,
    BetaAccessManager, BetaAccessConfig, AuthSession, BetaAuthToken, BetaAccessError
};

/// BIP353 API state
pub struct Bip353ApiState {
    pub bip353: Arc<Mutex<Bip353>>,
    pub beta_access: Arc<Mutex<BetaAccessManager>>,
}

/// API response for BIP353 status
#[derive(Debug, Serialize, Deserialize)]
pub struct Bip353StatusResponse {
    pub status: String,
    pub features: Vec<FeatureStatus>,
    pub config: Bip353Config,
}

/// Feature status
#[derive(Debug, Serialize, Deserialize)]
pub struct FeatureStatus {
    pub name: String,
    pub enabled: bool,
    pub tier: String,
    pub description: String,
}

/// Request to parse address
#[derive(Debug, Serialize, Deserialize)]
pub struct ParseAddressRequest {
    pub address: String,
}

/// Response for parsed address
#[derive(Debug, Serialize, Deserialize)]
pub struct ParseAddressResponse {
    pub user: String,
    pub domain: String,
    pub full_address: String,
    pub valid: bool,
    pub error: Option<String>,
}

/// Request to resolve address
#[derive(Debug, Serialize, Deserialize)]
pub struct ResolveAddressRequest {
    pub address: String,
}

/// Response for resolved address
#[derive(Debug, Serialize, Deserialize)]
pub struct ResolveAddressResponse {
    pub user: String,
    pub domain: String,
    pub full_address: String,
    pub payment_instruction: Option<String>,
    pub resolved: bool,
    pub error: Option<String>,
}

/// Request to update BIP353 configuration
#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateBip353ConfigRequest {
    pub status: Bip353Status,
    pub default_resolver: Option<String>,
    pub cache_duration: Option<u64>,
    pub validate_dnssec: Option<bool>,
    pub beta_features: Option<BetaFeaturesRequest>,
}

/// Beta features request
#[derive(Debug, Serialize, Deserialize)]
pub struct BetaFeaturesRequest {
    pub non_ascii_identifiers: Option<bool>,
    pub wildcard_records: Option<bool>,
    pub oob_notifications: Option<bool>,
    pub enhanced_privacy: Option<bool>,
}

/// Response for auth session
#[derive(Debug, Serialize, Deserialize)]
pub struct AuthSessionResponse {
    pub session_id: String,
    pub k1: String,
    pub auth_url: Option<String>,
    pub expires_at: u64,
}

/// Request to verify auth
#[derive(Debug, Serialize, Deserialize)]
pub struct VerifyAuthRequest {
    pub session_id: String,
    pub signature: String,
    pub pubkey: String,
}

/// Response for auth token
#[derive(Debug, Serialize, Deserialize)]
pub struct AuthTokenResponse {
    pub token: String,
    pub expires_at: u64,
}

/// Beta status response
#[derive(Debug, Serialize, Deserialize)]
pub struct BetaStatusResponse {
    pub enabled: bool,
    pub auth_required: bool,
    pub auth_url: Option<String>,
    pub authorized_pubkeys_count: usize,
}

/// Create BIP353 API routes
pub fn bip353_routes(state: Arc<Bip353ApiState>) -> Router {
    Router::new()
        .route("/api/bip353/status", get(get_status))
        .route("/api/bip353/config", get(get_config))
        .route("/api/bip353/config", put(update_config))
        .route("/api/bip353/parse", post(parse_address))
        .route("/api/bip353/resolve", post(resolve_address))
        .route("/api/bip353/beta/status", get(get_beta_status))
        .route("/api/bip353/beta/auth", post(create_auth_session))
        .route("/api/bip353/beta/verify", post(verify_auth))
        .route("/api/bip353/beta/check", get(check_auth))
        .with_state(state)
}

/// Get BIP353 status
async fn get_status(
    State(state): State<Arc<Bip353ApiState>>,
) -> impl IntoResponse {
    let bip353 = state.bip353.lock().unwrap();
    
    let features = vec![
        FeatureStatus {
            name: "basic_resolution".to_string(),
            enabled: bip353.is_feature_enabled("basic_resolution"),
            tier: "stable".to_string(),
            description: "Basic DNS Payment Instructions resolution".to_string(),
        },
        FeatureStatus {
            name: "dnssec_validation".to_string(),
            enabled: bip353.is_feature_enabled("dnssec_validation"),
            tier: "stable".to_string(),
            description: "DNSSEC validation for secure resolution".to_string(),
        },
        FeatureStatus {
            name: "non_ascii_identifiers".to_string(),
            enabled: bip353.is_feature_enabled("non_ascii_identifiers"),
            tier: "beta".to_string(),
            description: "Support for non-ASCII identifiers (punycode)".to_string(),
        },
        FeatureStatus {
            name: "wildcard_records".to_string(),
            enabled: bip353.is_feature_enabled("wildcard_records"),
            tier: "beta".to_string(),
            description: "Support for wildcard DNS records".to_string(),
        },
        FeatureStatus {
            name: "oob_notifications".to_string(),
            enabled: bip353.is_feature_enabled("oob_notifications"),
            tier: "beta".to_string(),
            description: "Support for out-of-band notifications".to_string(),
        },
        FeatureStatus {
            name: "enhanced_privacy".to_string(),
            enabled: bip353.is_feature_enabled("enhanced_privacy"),
            tier: "beta".to_string(),
            description: "Enhanced privacy routing".to_string(),
        },
    ];
    
    let response = Bip353StatusResponse {
        status: bip353.status().to_string(),
        features,
        config: bip353.config.clone(),
    };
    
    (StatusCode::OK, Json(response))
}

/// Get BIP353 configuration
async fn get_config(
    State(state): State<Arc<Bip353ApiState>>,
) -> impl IntoResponse {
    let bip353 = state.bip353.lock().unwrap();
    
    (StatusCode::OK, Json(bip353.config.clone()))
}

/// Update BIP353 configuration
async fn update_config(
    State(state): State<Arc<Bip353ApiState>>,
    Json(request): Json<UpdateBip353ConfigRequest>,
) -> impl IntoResponse {
    let mut bip353 = state.bip353.lock().unwrap();
    
    // Update config
    let mut config = bip353.config.clone();
    
    config.status = request.status;
    
    if let Some(resolver) = request.default_resolver {
        config.default_resolver = resolver;
    }
    
    if let Some(cache_duration) = request.cache_duration {
        config.cache_duration = cache_duration;
    }
    
    if let Some(validate_dnssec) = request.validate_dnssec {
        config.validate_dnssec = validate_dnssec;
    }
    
    if let Some(beta_features) = request.beta_features {
        if let Some(non_ascii) = beta_features.non_ascii_identifiers {
            config.beta_features.non_ascii_identifiers = non_ascii;
        }
        
        if let Some(wildcard) = beta_features.wildcard_records {
            config.beta_features.wildcard_records = wildcard;
        }
        
        if let Some(oob) = beta_features.oob_notifications {
            config.beta_features.oob_notifications = oob;
        }
        
        if let Some(privacy) = beta_features.enhanced_privacy {
            config.beta_features.enhanced_privacy = privacy;
        }
    }
    
    // Update BIP353 with new config
    match bip353.update_config(config.clone()) {
        Ok(_) => {
            info!("BIP353 configuration updated: {:?}", config);
            (StatusCode::OK, Json(config))
        },
        Err(e) => {
            error!("Failed to update BIP353 configuration: {:?}", e);
            (StatusCode::INTERNAL_SERVER_ERROR, Json(serde_json::json!({
                "error": format!("Failed to update configuration: {}", e)
            })))
        }
    }
}

/// Parse BIP353 address
async fn parse_address(
    State(state): State<Arc<Bip353ApiState>>,
    Json(request): Json<ParseAddressRequest>,
) -> impl IntoResponse {
    let bip353 = state.bip353.lock().unwrap();
    
    match bip353.parse_address(&request.address) {
        Ok(recipient) => {
            (StatusCode::OK, Json(ParseAddressResponse {
                user: recipient.user,
                domain: recipient.domain,
                full_address: recipient.full_address,
                valid: true,
                error: None,
            }))
        },
        Err(e) => {
            (StatusCode::OK, Json(ParseAddressResponse {
                user: "".to_string(),
                domain: "".to_string(),
                full_address: request.address,
                valid: false,
                error: Some(e.to_string()),
            }))
        }
    }
}

/// Resolve BIP353 address
async fn resolve_address(
    State(state): State<Arc<Bip353ApiState>>,
    Json(request): Json<ResolveAddressRequest>,
) -> impl IntoResponse {
    let bip353 = state.bip353.lock().unwrap();
    
    // First parse the address
    let mut recipient = match bip353.parse_address(&request.address) {
        Ok(r) => r,
        Err(e) => {
            return (StatusCode::BAD_REQUEST, Json(ResolveAddressResponse {
                user: "".to_string(),
                domain: "".to_string(),
                full_address: request.address,
                payment_instruction: None,
                resolved: false,
                error: Some(format!("Failed to parse address: {}", e)),
            }));
        }
    };
    
    // Now resolve it
    match bip353.resolve(&mut recipient).await {
        Ok(_) => {
            (StatusCode::OK, Json(ResolveAddressResponse {
                user: recipient.user,
                domain: recipient.domain,
                full_address: recipient.full_address,
                payment_instruction: recipient.payment_instruction,
                resolved: true,
                error: None,
            }))
        },
        Err(e) => {
            (StatusCode::OK, Json(ResolveAddressResponse {
                user: recipient.user,
                domain: recipient.domain,
                full_address: recipient.full_address,
                payment_instruction: None,
                resolved: false,
                error: Some(format!("Failed to resolve: {}", e)),
            }))
        }
    }
}

/// Get beta access status
async fn get_beta_status(
    State(state): State<Arc<Bip353ApiState>>,
) -> impl IntoResponse {
    let beta_access = state.beta_access.lock().unwrap();
    let config = beta_access.get_config();
    
    let response = BetaStatusResponse {
        enabled: config.enabled,
        auth_required: config.enabled,
        auth_url: config.auth_url.clone(),
        authorized_pubkeys_count: config.authorized_pubkeys.len(),
    };
    
    (StatusCode::OK, Json(response))
}

/// Create LNURL auth session for beta access
async fn create_auth_session(
    State(state): State<Arc<Bip353ApiState>>,
) -> impl IntoResponse {
    let beta_access = state.beta_access.lock().unwrap();
    
    if !beta_access.get_config().enabled {
        return (StatusCode::BAD_REQUEST, Json(serde_json::json!({
            "error": "Beta access auth not enabled"
        })));
    }
    
    let session = beta_access.create_session();
    let auth_url = beta_access.get_auth_url(&session);
    
    let response = AuthSessionResponse {
        session_id: session.id,
        k1: session.k1,
        auth_url,
        expires_at: session.expires_at,
    };
    
    (StatusCode::OK, Json(response))
}

/// Verify LNURL auth for beta access
async fn verify_auth(
    State(state): State<Arc<Bip353ApiState>>,
    Json(request): Json<VerifyAuthRequest>,
) -> impl IntoResponse {
    let beta_access = state.beta_access.lock().unwrap();
    
    match beta_access.verify_auth(&request.session_id, &request.signature, &request.pubkey) {
        Ok(session) => {
            // Create auth token
            let token = BetaAuthToken::new(session.id, session.expires_at);
            
            let response = AuthTokenResponse {
                token: token.encode(),
                expires_at: token.expires_at,
            };
            
            (StatusCode::OK, Json(response))
        },
        Err(e) => {
            let status = match e {
                BetaAccessError::InvalidSession(_) => StatusCode::BAD_REQUEST,
                BetaAccessError::ExpiredSession => StatusCode::UNAUTHORIZED,
                BetaAccessError::NotAuthorized => StatusCode::FORBIDDEN,
                _ => StatusCode::INTERNAL_SERVER_ERROR,
            };
            
            (status, Json(serde_json::json!({
                "error": e.to_string()
            })))
        }
    }
}

/// Check auth token for beta access
async fn check_auth(
    State(state): State<Arc<Bip353ApiState>>,
    Query(params): Query<std::collections::HashMap<String, String>>,
) -> impl IntoResponse {
    let token = match params.get("token") {
        Some(t) => t,
        None => {
            return (StatusCode::BAD_REQUEST, Json(serde_json::json!({
                "error": "Missing token parameter"
            })));
        }
    };
    
    // Decode token
    let token = match BetaAuthToken::decode(token) {
        Ok(t) => t,
        Err(e) => {
            return (StatusCode::UNAUTHORIZED, Json(serde_json::json!({
                "error": format!("Invalid token: {}", e)
            })));
        }
    };
    
    // Check if session is valid
    let beta_access = state.beta_access.lock().unwrap();
    if !beta_access.is_authenticated(&token.session_id) {
        return (StatusCode::UNAUTHORIZED, Json(serde_json::json!({
            "error": "Invalid or expired session"
        })));
    }
    
    (StatusCode::OK, Json(serde_json::json!({
        "authenticated": true,
        "expires_at": token.expires_at
    })))
}