Skip to content

Commit 2be661f

Browse files
committed
feat: add basic commands implementation, reworked contracts params storage
chore: * fix storing of entropies instead of asset ids * reworked contracts storage * assign constants to fixed values in library * align todos with one style
1 parent 61aaa8d commit 2be661f

File tree

24 files changed

+1438
-1071
lines changed

24 files changed

+1438
-1071
lines changed

crates/cli-client/src/cli/basic.rs

Lines changed: 347 additions & 96 deletions
Large diffs are not rendered by default.

crates/cli-client/src/cli/commands.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ pub enum HelperCommand {
7676
Balance,
7777

7878
/// List all UTXOs stored in wallet
79-
Utxos {
80-
/// Sets script for filtering in wallet
81-
#[arg(long, long = "mine", default_value_t = false)]
82-
is_mine: bool,
83-
},
79+
Utxos,
8480

8581
/// Import a UTXO into the wallet
8682
Import {

crates/cli-client/src/cli/common.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use crate::error::Error;
2-
use crate::wallet::Wallet;
3-
use coin_store::utxo_store::UtxoStore;
2+
use simplicityhl::elements::Transaction;
43
use simplicityhl::elements::pset::serialize::Serialize;
5-
use simplicityhl::elements::{OutPoint, Transaction};
64
use simplicityhl::simplicity::hex::DisplayHex;
75

86
#[derive(Debug, Clone, Copy)]
@@ -18,31 +16,14 @@ impl From<bool> for Broadcaster {
1816
}
1917

2018
impl Broadcaster {
21-
/// Broadcasts created transaction or returns raw transaction as hex string
22-
pub async fn broadcast_tx(&self, wallet: &Wallet, tx: &Transaction, outpoints: &[OutPoint]) -> Result<(), Error> {
19+
pub async fn broadcast_tx(&self, tx: &Transaction) -> Result<(), Error> {
2320
match self {
2421
Broadcaster::Offline => {
2522
println!("{}", tx.serialize().to_lower_hex_string());
2623
}
2724
Broadcaster::Online => {
2825
cli_helper::explorer::broadcast_tx(tx).await?;
29-
30-
for outpoint in outpoints {
31-
wallet.store().mark_as_spent(*outpoint).await?;
32-
}
33-
3426
let txid = tx.txid();
35-
for (vout, output) in tx.output.iter().enumerate() {
36-
if output.is_fee() {
37-
continue;
38-
}
39-
40-
#[allow(clippy::cast_possible_truncation)]
41-
let new_outpoint = simplicityhl::elements::OutPoint::new(txid, vout as u32);
42-
43-
wallet.store().insert(new_outpoint, output.clone(), None).await?;
44-
}
45-
4627
println!("Broadcasted: {txid}");
4728
}
4829
}

crates/cli-client/src/cli/helper.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ use crate::config::Config;
33
use crate::error::Error;
44
use crate::wallet::Wallet;
55
use coin_store::utxo_store::UtxoStore;
6+
use simplicityhl::elements::bitcoin::secp256k1;
67

78
impl Cli {
8-
const BLINDING_KEY_LEN: usize = 32;
9-
109
pub(crate) async fn run_helper(&self, config: Config, command: &HelperCommand) -> Result<(), Error> {
1110
match command {
1211
HelperCommand::Init => {
@@ -31,7 +30,7 @@ impl Cli {
3130

3231
let filter = coin_store::UtxoFilter::new()
3332
.script_pubkey(wallet.signer().p2pk_address(config.address_params())?.script_pubkey());
34-
let results = <_ as UtxoStore>::query(wallet.store(), &[filter]).await?;
33+
let results = <_ as UtxoStore>::query_utxos(wallet.store(), &[filter]).await?;
3534

3635
let mut balances: std::collections::HashMap<simplicityhl::elements::AssetId, u64> =
3736
std::collections::HashMap::new();
@@ -59,16 +58,11 @@ impl Cli {
5958
}
6059
Ok(())
6160
}
62-
HelperCommand::Utxos { is_mine } => {
61+
HelperCommand::Utxos => {
6362
let wallet = self.get_wallet(&config).await?;
6463

65-
let filter = if *is_mine {
66-
coin_store::UtxoFilter::new()
67-
.script_pubkey(wallet.signer().p2pk_address(config.address_params())?.script_pubkey())
68-
} else {
69-
coin_store::UtxoFilter::new()
70-
};
71-
let results = wallet.store().query(&[filter]).await?;
64+
let filter = coin_store::UtxoFilter::new();
65+
let results = wallet.store().query_utxos(&[filter]).await?;
7266

7367
if let Some(coin_store::UtxoQueryResult::Found(entries)) = results.into_iter().next() {
7468
for entry in &entries {
@@ -96,7 +90,7 @@ impl Cli {
9690

9791
let blinder = match blinding_key {
9892
Some(key_hex) => {
99-
let bytes: [u8; Self::BLINDING_KEY_LEN] = hex::decode(key_hex)
93+
let bytes: [u8; secp256k1::constants::SECRET_KEY_SIZE] = hex::decode(key_hex)
10094
.map_err(|e| Error::Config(format!("Invalid blinding key hex: {e}")))?
10195
.try_into()
10296
.map_err(|_| Error::Config("Blinding key must be 32 bytes".to_string()))?;

crates/coin-store/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ categories.workspace = true
1414

1515
[dependencies]
1616
simplicityhl = { workspace = true }
17+
simplicityhl-core = { workspace = true }
18+
contracts = { workspace = true }
19+
1720

1821
futures = { version = "0.3" }
1922

@@ -22,3 +25,8 @@ thiserror = { version = "2" }
2225
async-trait = { version = "0.1.89" }
2326
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
2427
sqlx = { version = "0.8", features = ["runtime-tokio", "sqlite", "migrate"] }
28+
29+
bincode = { version = "2.0.1", features = ["alloc", "derive"] }
30+
31+
[dev-dependencies]
32+
hex = { version = "0.4.3" }

crates/coin-store/migrations/001_initial.sql

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CREATE TABLE utxos(
55
asset_id BLOB NOT NULL,
66
value INTEGER NOT NULL,
77
serialized BLOB NOT NULL,
8+
serialized_witness BLOB,
89
is_confidential INTEGER NOT NULL,
910
is_spent INTEGER DEFAULT 0,
1011
PRIMARY KEY (txid, vout)
@@ -21,28 +22,31 @@ CREATE TABLE blinder_keys (
2122
CREATE TABLE assets
2223
(
2324
asset_name TEXT NOT NULL,
24-
asset_id BLOB NOT NULL,
25+
asset_entropy BLOB NOT NULL,
2526
PRIMARY KEY (asset_name)
2627
);
2728

28-
CREATE TABLE option_params
29-
(
30-
script_pubkey BLOB NOT NULL PRIMARY KEY,
31-
start_time BLOB NOT NULL,
32-
expiry_time BLOB NOT NULL,
33-
collateral_per_contract BLOB NOT NULL,
34-
settlement_per_contract BLOB NOT NULL,
35-
collateral_asset_id BLOB NOT NULL,
36-
settlement_asset_id BLOB NOT NULL,
37-
option_token_entropy BLOB NOT NULL,
38-
grantor_token_entropy BLOB NOT NULL
39-
);
40-
4129
CREATE INDEX idx_utxos_asset_id ON utxos (asset_id);
4230
CREATE INDEX idx_utxos_is_spent ON utxos (is_spent);
4331
CREATE INDEX idx_utxos_script_pubkey ON utxos (script_pubkey);
4432
CREATE INDEX idx_utxos_asset_spent_value ON utxos (asset_id, is_spent, value DESC);
4533

46-
CREATE INDEX idx_option_params_script_pubkey ON option_params (script_pubkey);
47-
4834
CREATE INDEX idx_assets_asset_name ON assets (asset_name);
35+
36+
CREATE TABLE simplicity_contracts
37+
(
38+
x_only_pubkey BLOB NOT NULL,
39+
taproot_pubkey_gen BLOB NOT NULL,
40+
contract_type INTEGER NOT NULL,
41+
PRIMARY KEY (taproot_pubkey_gen)
42+
);
43+
44+
CREATE TABLE contracts
45+
(
46+
taproot_pubkey_gen BLOB NOT NULL,
47+
params BLOB NOT NULL,
48+
FOREIGN KEY (taproot_pubkey_gen) REFERENCES simplicity_contracts (taproot_pubkey_gen),
49+
PRIMARY KEY (taproot_pubkey_gen)
50+
);
51+
52+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::asset_entropy_store::AssetEntropy;
2+
3+
#[derive(Debug)]
4+
pub struct AssetEntropyEntry {
5+
pub name: String,
6+
pub asset_entropy: AssetEntropy,
7+
}
8+
9+
#[derive(Debug)]
10+
pub enum QueryResult {
11+
Found(Vec<AssetEntropyEntry>),
12+
Empty,
13+
}
File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod entry;
2+
pub mod filter;
3+
mod store_impl;
4+
5+
pub use store_impl::*;

0 commit comments

Comments
 (0)