NAC_Blockchain/nac-asset-onboarding/src/dna.rs

86 lines
2.2 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! DNA生成适配器
//!
//! 调用nac-udm模块生成资产DNA和GNACS编码
use crate::error::{OnboardingError, Result};
use crate::types::{AssetSubmission, DNAResult};
use nac_udm::asset_dna::DNAGenerator;
use nac_udm::l1_protocol::gnacs::GNACSCode;
use chrono::Utc;
use tracing::{info, error};
/// DNA生成适配器
pub struct DNAAdapter {
generator: DNAGenerator,
}
impl DNAAdapter {
/// 创建新的适配器
pub fn new() -> Result<Self> {
let generator = DNAGenerator::new();
Ok(Self { generator })
}
/// 生成资产DNA
pub async fn generate_dna(&self, submission: &AssetSubmission) -> Result<DNAResult> {
info!("开始生成资产DNA: {}", submission.asset_name);
// 生成GNACS编码
let gnacs_code = self.generate_gnacs(submission)?;
// 生成DNA
let dna = self.generator.generate(
&submission.asset_name,
&submission.asset_type,
&submission.jurisdiction,
&gnacs_code.to_string(),
)
.map_err(|e| OnboardingError::DNAGenerationError(format!("DNA生成失败: {}", e)))?;
// 获取DNA哈希48字节SHA3-384
let dna_hash = hex::encode(dna.hash());
// 获取资产实例ID
let asset_instance_id = dna.instance_id().to_string();
info!("DNA生成完成: hash={}, gnacs={}", dna_hash, gnacs_code.to_string());
Ok(DNAResult {
dna_hash,
gnacs_code: gnacs_code.to_string(),
asset_instance_id,
timestamp: Utc::now(),
})
}
/// 生成GNACS编码
fn generate_gnacs(&self, submission: &AssetSubmission) -> Result<GNACSCode> {
// 调用GNACS编码器
let code = GNACSCode::generate(
&submission.asset_type,
&submission.jurisdiction,
&submission.asset_name,
)
.map_err(|e| OnboardingError::DNAGenerationError(format!("GNACS编码失败: {}", e)))?;
Ok(code)
}
}
impl Default for DNAAdapter {
fn default() -> Self {
Self::new().unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_adapter_creation() {
let adapter = DNAAdapter::new();
assert!(adapter.is_ok());
}
}