335 lines
8.9 KiB
Rust
335 lines
8.9 KiB
Rust
//! NAC ACC-1400 Protocol Implementation
|
||
//! NAC ACC-1400协议实现 - 证券型资产协议
|
||
//!
|
||
//! ACC-1400继承ACC-1410,专门用于证券型资产(Security Token),
|
||
//! 增加了证券特有的功能,如股息分配、投票权管理、转让限制等。
|
||
|
||
pub use nac_acc_1410::*;
|
||
|
||
/// ACC-1400证券型资产协议
|
||
#[derive(Debug)]
|
||
pub struct Acc1400 {
|
||
base: Acc1410,
|
||
}
|
||
|
||
impl Acc1400 {
|
||
/// 创建新的ACC-1400实例
|
||
pub fn new() -> Self {
|
||
Self {
|
||
base: Acc1410::new(),
|
||
}
|
||
}
|
||
|
||
/// 创建证券型资产分区
|
||
pub fn create_security_partition(
|
||
&mut self,
|
||
name: String,
|
||
extended_gnacs: ExtendedGNACS,
|
||
partition_type: PartitionType,
|
||
) -> Result<[u8; 32]> {
|
||
self.base
|
||
.create_partition(name, extended_gnacs, partition_type)
|
||
}
|
||
|
||
/// 发行证券代币
|
||
pub fn issue_security(
|
||
&mut self,
|
||
partition_id: &[u8; 32],
|
||
to: &str,
|
||
amount: u64,
|
||
) -> Result<()> {
|
||
self.base.issue_to_partition(partition_id, to, amount)
|
||
}
|
||
|
||
/// 转让证券
|
||
pub fn transfer_security(
|
||
&mut self,
|
||
from: &str,
|
||
to: &str,
|
||
amount: u64,
|
||
partition_id: &[u8; 32],
|
||
) -> Result<nac_acc_1410::TransferResult> {
|
||
self.base
|
||
.transfer_by_partition(from, to, amount, partition_id)
|
||
}
|
||
|
||
/// 获取证券余额
|
||
pub fn balance_of_security(
|
||
&self,
|
||
partition_id: &[u8; 32],
|
||
account: &str,
|
||
) -> Result<u64> {
|
||
self.base.balance_of_by_partition(partition_id, account)
|
||
}
|
||
|
||
/// 获取账户持有的所有证券分区
|
||
pub fn securities_of(&self, account: &str) -> Vec<[u8; 32]> {
|
||
self.base.partitions_of(account)
|
||
}
|
||
|
||
/// 授权证券操作员
|
||
pub fn authorize_security_operator(&mut self, account: &str, operator: &str) {
|
||
self.base.authorize_operator(account, operator);
|
||
}
|
||
|
||
/// 撤销证券操作员
|
||
pub fn revoke_security_operator(&mut self, account: &str, operator: &str) {
|
||
self.base.revoke_operator(account, operator);
|
||
}
|
||
|
||
/// 锁定证券账户
|
||
pub fn lock_security_account(&mut self, account: &str, unlock_time: u64) {
|
||
self.base.lock_account(account, unlock_time);
|
||
}
|
||
|
||
/// 解锁证券账户
|
||
pub fn unlock_security_account(&mut self, account: &str) {
|
||
self.base.unlock_account(account);
|
||
}
|
||
|
||
/// 暂停证券转账
|
||
pub fn halt_security_transfers(&mut self) {
|
||
self.base.halt_transfers();
|
||
}
|
||
|
||
/// 恢复证券转账
|
||
pub fn resume_security_transfers(&mut self) {
|
||
self.base.resume_transfers();
|
||
}
|
||
}
|
||
|
||
impl Default for Acc1400 {
|
||
fn default() -> Self {
|
||
Self::new()
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
use nac_acc_1410::GNACSExtension;
|
||
|
||
#[test]
|
||
fn test_acc1400_security_issuance() {
|
||
let mut acc1400 = Acc1400::new();
|
||
|
||
// 创建普通股证券
|
||
let common_stock_gnacs = ExtendedGNACS {
|
||
base_gnacs: vec![0x94, 0x01, 0x00, 0x04, 0x02, 0x01],
|
||
extension: GNACSExtension {
|
||
partition_type: 0x01,
|
||
vesting_years: 0,
|
||
voting_multiplier: 1,
|
||
dividend_priority: 1,
|
||
},
|
||
};
|
||
|
||
let security_id = acc1400
|
||
.create_security_partition(
|
||
"ACME Corp Common Stock".to_string(),
|
||
common_stock_gnacs,
|
||
PartitionType::CommonStock,
|
||
)
|
||
.unwrap();
|
||
|
||
// 发行证券
|
||
acc1400
|
||
.issue_security(&security_id, "investor1", 10000)
|
||
.unwrap();
|
||
|
||
// 检查余额
|
||
assert_eq!(
|
||
acc1400
|
||
.balance_of_security(&security_id, "investor1")
|
||
.unwrap(),
|
||
10000
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn test_acc1400_security_transfer() {
|
||
let mut acc1400 = Acc1400::new();
|
||
|
||
let gnacs = ExtendedGNACS {
|
||
base_gnacs: vec![0x94, 0x01, 0x00, 0x04, 0x02, 0x01],
|
||
extension: GNACSExtension {
|
||
partition_type: 0x01,
|
||
vesting_years: 0,
|
||
voting_multiplier: 1,
|
||
dividend_priority: 1,
|
||
},
|
||
};
|
||
|
||
let security_id = acc1400
|
||
.create_security_partition(
|
||
"Test Security".to_string(),
|
||
gnacs,
|
||
PartitionType::CommonStock,
|
||
)
|
||
.unwrap();
|
||
|
||
acc1400
|
||
.issue_security(&security_id, "investor1", 5000)
|
||
.unwrap();
|
||
|
||
// 转让证券
|
||
acc1400
|
||
.transfer_security("investor1", "investor2", 2000, &security_id)
|
||
.unwrap();
|
||
|
||
assert_eq!(
|
||
acc1400
|
||
.balance_of_security(&security_id, "investor1")
|
||
.unwrap(),
|
||
3000
|
||
);
|
||
assert_eq!(
|
||
acc1400
|
||
.balance_of_security(&security_id, "investor2")
|
||
.unwrap(),
|
||
2000
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn test_acc1400_operator_authorization() {
|
||
let mut acc1400 = Acc1400::new();
|
||
|
||
// 授权操作员
|
||
acc1400.authorize_security_operator("investor1", "broker1");
|
||
|
||
// 创建证券
|
||
let gnacs = ExtendedGNACS {
|
||
base_gnacs: vec![0x94, 0x01, 0x00, 0x04, 0x02, 0x01],
|
||
extension: GNACSExtension {
|
||
partition_type: 0x01,
|
||
vesting_years: 0,
|
||
voting_multiplier: 1,
|
||
dividend_priority: 1,
|
||
},
|
||
};
|
||
|
||
let security_id = acc1400
|
||
.create_security_partition(
|
||
"Test Security".to_string(),
|
||
gnacs,
|
||
PartitionType::CommonStock,
|
||
)
|
||
.unwrap();
|
||
|
||
acc1400
|
||
.issue_security(&security_id, "investor1", 1000)
|
||
.unwrap();
|
||
|
||
// 操作员代理转账
|
||
let result = acc1400.base.operator_transfer_by_partition(
|
||
"broker1",
|
||
"investor1",
|
||
"investor2",
|
||
500,
|
||
&security_id,
|
||
);
|
||
|
||
assert!(result.is_ok());
|
||
assert_eq!(
|
||
acc1400
|
||
.balance_of_security(&security_id, "investor1")
|
||
.unwrap(),
|
||
500
|
||
);
|
||
assert_eq!(
|
||
acc1400
|
||
.balance_of_security(&security_id, "investor2")
|
||
.unwrap(),
|
||
500
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn test_acc1400_account_locking() {
|
||
let mut acc1400 = Acc1400::new();
|
||
|
||
let gnacs = ExtendedGNACS {
|
||
base_gnacs: vec![0x94, 0x01, 0x00, 0x04, 0x02, 0x01],
|
||
extension: GNACSExtension {
|
||
partition_type: 0x01,
|
||
vesting_years: 0,
|
||
voting_multiplier: 1,
|
||
dividend_priority: 1,
|
||
},
|
||
};
|
||
|
||
let security_id = acc1400
|
||
.create_security_partition(
|
||
"Locked Security".to_string(),
|
||
gnacs,
|
||
PartitionType::RestrictedStock,
|
||
)
|
||
.unwrap();
|
||
|
||
acc1400
|
||
.issue_security(&security_id, "investor1", 1000)
|
||
.unwrap();
|
||
|
||
// 锁定账户
|
||
let future_time = std::time::SystemTime::now()
|
||
.duration_since(std::time::UNIX_EPOCH)
|
||
.unwrap()
|
||
.as_secs()
|
||
+ 3600;
|
||
acc1400.lock_security_account("investor1", future_time);
|
||
|
||
// 尝试转账应该失败
|
||
let result = acc1400.transfer_security("investor1", "investor2", 500, &security_id);
|
||
assert!(result.is_err());
|
||
|
||
// 解锁账户
|
||
acc1400.unlock_security_account("investor1");
|
||
|
||
// 现在转账应该成功
|
||
let result = acc1400.transfer_security("investor1", "investor2", 500, &security_id);
|
||
assert!(result.is_ok());
|
||
}
|
||
|
||
#[test]
|
||
fn test_acc1400_transfer_halt() {
|
||
let mut acc1400 = Acc1400::new();
|
||
|
||
let gnacs = ExtendedGNACS {
|
||
base_gnacs: vec![0x94, 0x01, 0x00, 0x04, 0x02, 0x01],
|
||
extension: GNACSExtension {
|
||
partition_type: 0x01,
|
||
vesting_years: 0,
|
||
voting_multiplier: 1,
|
||
dividend_priority: 1,
|
||
},
|
||
};
|
||
|
||
let security_id = acc1400
|
||
.create_security_partition(
|
||
"Halted Security".to_string(),
|
||
gnacs,
|
||
PartitionType::CommonStock,
|
||
)
|
||
.unwrap();
|
||
|
||
acc1400
|
||
.issue_security(&security_id, "investor1", 1000)
|
||
.unwrap();
|
||
|
||
// 暂停转账
|
||
acc1400.halt_security_transfers();
|
||
|
||
// 尝试转账应该失败
|
||
let result = acc1400.transfer_security("investor1", "investor2", 500, &security_id);
|
||
assert!(result.is_err());
|
||
|
||
// 恢复转账
|
||
acc1400.resume_security_transfers();
|
||
|
||
// 现在转账应该成功
|
||
let result = acc1400.transfer_security("investor1", "investor2", 500, &security_id);
|
||
assert!(result.is_ok());
|
||
}
|
||
}
|