mod client;
mod contract;
mod node;
mod schema;
mod state;
mod wallet;
pub use self::client::{ClientConfig, RGBClient, RGBClientBuilder};
pub use self::contract::{Contract, ContractBuilder, ContractType, Witness};
pub use self::node::{NodeConfig, RGBNode};
pub use self::schema::{Field, FieldType, Schema, SchemaType, Validation};
pub use self::state::{StateTransfer, StateTransition, StateValidator};
pub use self::wallet::{AssetBalance, RGBWallet};
use bitcoin::Txid;
use std::collections::HashMap;
use std::path::PathBuf;
use crate::bitcoin::wallet::transactions::TxOptions;
use crate::AnyaResult;
#[derive(Debug, Clone)]
pub struct RGBAsset {
pub id: String,
pub name: String,
pub description: Option<String>,
pub total_supply: u64,
pub precision: u8,
pub metadata: HashMap<String, String>,
pub contract_id: String,
pub schema_id: String,
}
#[derive(Debug, Clone)]
pub struct AssetTransfer {
pub asset_id: String,
pub amount: u64,
pub recipient: String,
pub change_address: Option<String>,
pub fee_rate: u64,
pub tx_options: Option<TxOptions>,
}
#[async_trait::async_trait]
pub trait RGBManager: Send + Sync {
async fn create_asset(&self, params: AssetCreationParams) -> AnyaResult<RGBAsset>;
async fn transfer_asset(&self, transfer: AssetTransfer) -> AnyaResult<TransferStatus>;
async fn get_asset(&self, asset_id: &str) -> AnyaResult<Option<RGBAsset>>;
async fn list_assets(&self) -> AnyaResult<Vec<RGBAsset>>;
async fn get_balance(&self, asset_id: &str) -> AnyaResult<u64>;
async fn get_history(&self, asset_id: &str) -> AnyaResult<Vec<HistoryEntry>>;
async fn validate_asset(&self, asset_id: &str) -> AnyaResult<bool>;
async fn import_asset(&self, contract_data: &[u8]) -> AnyaResult<RGBAsset>;
async fn export_asset(&self, asset_id: &str) -> AnyaResult<Vec<u8>>;
}
pub struct RGBFactory;
impl RGBFactory {
pub fn new_manager(config: RGBConfig) -> Box<dyn RGBManager> {
Box::new(DefaultRGBManager::new(config))
}
pub fn default_manager() -> Box<dyn RGBManager> {
Box::new(DefaultRGBManager::default())
}
}
#[derive(Debug, Clone)]
pub struct RGBConfig {
pub data_dir: PathBuf,
pub network: String,
pub debug: bool,
pub timeout: u64,
pub node_endpoint: Option<String>,
}
impl Default for RGBConfig {
fn default() -> Self {
Self {
data_dir: PathBuf::from("~/.rgb"),
network: "bitcoin".to_string(),
debug: false,
timeout: 30,
node_endpoint: None,
}
}
}
#[derive(Debug, Clone)]
pub struct AssetCreationParams {
pub name: String,
pub description: Option<String>,
pub total_supply: u64,
pub precision: u8,
pub metadata: HashMap<String, String>,
pub schema_id: String,
pub issuer: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransferStatus {
Pending,
Confirmed,
Failed(String),
Rejected(String),
}
#[derive(Debug, Clone)]
pub struct HistoryEntry {
pub txid: Txid,
pub operation: OperationType,
pub amount: u64,
pub timestamp: u64,
pub confirmed: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OperationType {
Issue,
Transfer,
Burn,
Reissue,
}
#[allow(dead_code)]
struct DefaultRGBManager {
client: Option<RGBClient>,
config: RGBConfig,
}
impl DefaultRGBManager {
pub fn new(config: RGBConfig) -> Self {
Self {
client: None,
config,
}
}
async fn _init_client(&mut self) -> AnyaResult<()> {
use crate::bitcoin::layer2::rgb::RGBWallet;
if self.client.is_none() {
let wallet = RGBWallet::new("dummy-address");
let client = RGBClient::new(wallet);
self.client = Some(client);
}
Ok(())
}
}
impl Default for DefaultRGBManager {
fn default() -> Self {
Self::new(RGBConfig::default())
}
}
#[async_trait::async_trait]
impl RGBManager for DefaultRGBManager {
async fn create_asset(&self, _params: AssetCreationParams) -> AnyaResult<RGBAsset> {
Ok(RGBAsset {
id: "placeholder_asset".to_string(),
name: "Placeholder Asset".to_string(),
description: Some("Placeholder RGB asset".to_string()),
total_supply: 1000000,
precision: 8,
metadata: HashMap::new(),
contract_id: "placeholder_contract".to_string(),
schema_id: "placeholder_schema".to_string(),
})
}
async fn transfer_asset(&self, _transfer: AssetTransfer) -> AnyaResult<TransferStatus> {
Ok(TransferStatus::Pending)
}
async fn get_asset(&self, _asset_id: &str) -> AnyaResult<Option<RGBAsset>> {
Ok(None)
}
async fn list_assets(&self) -> AnyaResult<Vec<RGBAsset>> {
Ok(Vec::new())
}
async fn get_balance(&self, _asset_id: &str) -> AnyaResult<u64> {
Ok(0)
}
async fn get_history(&self, _asset_id: &str) -> AnyaResult<Vec<HistoryEntry>> {
Ok(Vec::new())
}
async fn validate_asset(&self, _asset_id: &str) -> AnyaResult<bool> {
Ok(true)
}
async fn import_asset(&self, _contract_data: &[u8]) -> AnyaResult<RGBAsset> {
Ok(RGBAsset {
id: "imported_asset".to_string(),
name: "Imported Asset".to_string(),
description: Some("Imported RGB asset".to_string()),
total_supply: 1000000,
precision: 8,
metadata: HashMap::new(),
contract_id: "imported_contract".to_string(),
schema_id: "imported_schema".to_string(),
})
}
async fn export_asset(&self, _asset_id: &str) -> AnyaResult<Vec<u8>> {
Ok(Vec::new())
}
}