Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
.vscode/
.cache
.env
Simplex.toml
config.toml

# Debugging data
Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions crates/artifacts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "simplex-build"
version = "0.1.0"
edition = "2024"
description = "Simplex Build"
license = "MIT OR Apache-2.0"
readme = "README.md"

[lints]
workspace = true

[dependencies]
thiserror = { workspace = true }

simplex-sdk = { workspace = true }
simplicityhl = { workspace = true }
89 changes: 89 additions & 0 deletions crates/artifacts/src/asset_auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use simplex_sdk::arguments::ArgumentsTrait;
use simplex_sdk::program::Program;

use simplicityhl::simplicity::bitcoin::XOnlyPublicKey;

// use simplex_macros::simplex_build;

// #[derive(SimplexBuild)]
// #[simplex("../../contracts/src/asset_auth/source_simf/asset_auth.simf")]
pub struct AssetAuth<'a> {
program: Program<'a>,
}

impl<'a> AssetAuth<'a> {
// the path is autogenerated
pub const SOURCE: &'static str = "";
// include_str!("../../contracts/src/asset_auth/source_simf/asset_auth.simf");

pub fn new(public_key: &'a XOnlyPublicKey, arguments: &'a impl ArgumentsTrait) -> Self {
Self {
program: Program::new(Self::SOURCE, public_key, arguments),
}
}

pub fn get_program(&self) -> &Program<'a> {
&self.program
}
}

// Expanded by macro

pub mod asset_auth_build {
use simplex_sdk::arguments::ArgumentsTrait;
use simplex_sdk::witness::WitnessTrait;
use simplicityhl::value::UIntValue;
use simplicityhl::value::ValueConstructible;
use simplicityhl::{Value, WitnessValues};
use std::collections::HashMap;

pub struct AssetAuthWitness {
pub path: (bool, u64, u64),
}

pub struct AssetAuthArguments {
pub first: u64,
pub second: bool,
}

impl WitnessTrait for AssetAuthWitness {
fn build_witness(&self) -> WitnessValues {
WitnessValues::from(HashMap::from([(
simplicityhl::str::WitnessName::from_str_unchecked("PATH"),
Value::tuple([
Value::from(self.path.0),
Value::from(UIntValue::U64(self.path.1)),
Value::from(UIntValue::U64(self.path.1)),
]),
)]))
}

// fn from_witness(_witness: &::simplicityhl::WitnessValues) -> Self {
// Self {
// path: (false, 0, 0),
// }
// }
}

impl ArgumentsTrait for AssetAuthArguments {
fn build_arguments(&self) -> simplicityhl::Arguments {
simplicityhl::Arguments::from(HashMap::from([
(
simplicityhl::str::WitnessName::from_str_unchecked("FIRST"),
Value::from(UIntValue::U64(self.first)),
),
(
simplicityhl::str::WitnessName::from_str_unchecked("SECOND"),
Value::from(self.second),
),
]))
}

// fn from_arguments(_args: &simplicityhl::Arguments) -> Self {
// Self {
// first: 0,
// second: false,
// }
// }
}
}
1 change: 1 addition & 0 deletions crates/artifacts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod asset_auth;
5 changes: 4 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ workspace = true
simplex-test = { workspace = true }
simplex-config = { workspace = true }

serde = { version = "1.0.228" }
toml = { version = "0.9.8" }

anyhow = "1"
dotenvy = "0.15"
clap = { version = "4", features = ["derive", "env"] }
Expand All @@ -27,4 +30,4 @@ tracing = { version = "0.1.44" }
thiserror = { workspace = true }
tracing-subscriber = { version = "0.3.22", features = ["env-filter"] }
ctrlc = { version = "3.5.2", features = ["termination"] }
electrsd = { workspace = true }
electrsd = { workspace = true }
157 changes: 157 additions & 0 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
use serde::{Deserialize, Serialize};
use simplex_core::SimplicityNetwork;
use std::fmt::Display;
use std::path::{Path, PathBuf};
use std::str::FromStr;

const MANIFEST_DIR: &str = "CARGO_MANIFEST_DIR";
const CONFIG_FILENAME: &str = "Simplex.toml";

#[derive(thiserror::Error, Debug)]
pub enum ConfigError {
/// Standard I/O errors.
#[error("IO error: {0}")]
Io(#[from] std::io::Error),

/// Errors when parsing TOML configuration files.
#[error("TOML parse error: {0}")]
TomlParse(#[from] toml::de::Error),

/// Errors when parsing TOML configuration files.
#[error("Unable to deserialize config: {0}")]
UnableToDeserialize(toml::de::Error),

/// Errors when parsing env variable.
#[error("Unable to get env variable: {0}")]
UnableToGetEnv(#[from] std::env::VarError),

/// Errors when getting a path to config.
#[error("Path doesn't a file: '{0}'")]
PathIsNotFile(PathBuf),

/// Errors when getting a path to config.
#[error("Path doesn't exist: '{0}'")]
PathIsNotEsixt(PathBuf),
}

#[derive(Debug, Default, Clone)]
pub struct Config {
pub provider_config: ProviderConfig,
pub test_config: TestConfig,
}

#[derive(Debug, Clone)]
pub struct ProviderConfig {
simplicity_network: SimplicityNetwork,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct TestConfig {
pub rpc_creds: RpcCreds,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub enum RpcCreds {
Auth {
rpc_username: String,
rpc_password: String,
},
#[default]
None,
}

#[derive(Debug, Default, Clone)]
pub struct ConfigOverride {
pub rpc_creds: Option<TestConfig>,
pub network: Option<SimplicityNetwork>,
}

impl Default for ProviderConfig {
fn default() -> Self {
ProviderConfig {
simplicity_network: SimplicityNetwork::LiquidTestnet,
}
}
}

impl Config {
pub fn discover(cfg_override: &ConfigOverride) -> Result<Option<Config>, ConfigError> {
Config::_discover().map(|opt| {
opt.map(|mut cfg| {
if let Some(test_conf) = cfg_override.rpc_creds.clone() {
cfg.test_config = test_conf;
}
if let Some(network) = cfg_override.network {
cfg.provider_config.simplicity_network = network;
}
cfg
})
})
}

pub fn load_or_default(path_buf: impl AsRef<Path>) -> Self {
Self::from_path(path_buf).unwrap_or_else(|_| {
if let Ok(Some(conf)) = Self::_discover() {
conf
} else {
Self::default()
}
})
}

fn _discover() -> Result<Option<Config>, ConfigError> {
let cwd = std::env::current_dir()?;
let path = cwd.join(CONFIG_FILENAME);
dbg!(&path);
if !path.is_file() {
return Err(ConfigError::PathIsNotFile(path));
}
if !path.exists() {
return Err(ConfigError::PathIsNotEsixt(path));
}
Ok(Some(Config::from_path(&path)?))
}

fn from_path(p: impl AsRef<Path>) -> Result<Self, ConfigError> {
std::fs::read_to_string(p.as_ref())?.parse()
}
}

impl FromStr for Config {
type Err = ConfigError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let cfg: _Config = toml::from_str(s).map_err(ConfigError::UnableToDeserialize)?;
Ok(Config {
provider_config: ProviderConfig {
simplicity_network: cfg.network.unwrap_or_default().into(),
},
test_config: cfg.test.unwrap_or_default(),
})
}
}

#[derive(Debug, Serialize, Deserialize)]
struct _Config {
network: Option<_NetworkName>,
test: Option<TestConfig>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
enum _NetworkName {
#[default]
Liquid,
LiquidTestnet,
ElementsRegtest,
}

impl Into<SimplicityNetwork> for _NetworkName {
fn into(self) -> SimplicityNetwork {
match self {
_NetworkName::Liquid => SimplicityNetwork::Liquid,
_NetworkName::LiquidTestnet => SimplicityNetwork::LiquidTestnet,
_NetworkName::ElementsRegtest => SimplicityNetwork::default_regtest(),
}
}
}
2 changes: 1 addition & 1 deletion crates/simplex/tests/simplex_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn test_invocation_tx_tracking() -> anyhow::Result<()> {

// broadcast, fetch fee transaction

ElementsRpcClient::sendtoaddress(
let result = ElementsRpcClient::sendtoaddress(
rpc.as_ref(),
&p2pk,
DEFAULT_SAT_AMOUNT_FAUCET,
Expand Down
Loading