Skip to content

Commit

Permalink
Cleanup: make Value an implementation detail of Data
Browse files Browse the repository at this point in the history
  • Loading branch information
imikushin committed Jan 19, 2025
1 parent e586910 commit 6d737af
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 27 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ bitcoin = { workspace = true, features = ["rand", "rand-std"] }
bitcoincore-rpc = { version = "0.19.0" }
charms-data = { path = "./charms-data", version = "0.3.0" }
charms-spell-checker = { path = "charms-spell-checker", version = "0.3.0" }
ciborium = { workspace = true }
clap = { version = "4.5.26", features = ["derive"] }
hex = { workspace = true }
rand = { workspace = true }
Expand Down
5 changes: 2 additions & 3 deletions src/cli/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{app, spell::Spell};
use anyhow::{anyhow, ensure, Result};
use charms_data::{Data, B32};
use ciborium::Value;
use std::{
collections::BTreeMap,
env, fs, io,
Expand Down Expand Up @@ -131,9 +130,9 @@ pub fn run(spell: PathBuf, path: Option<PathBuf>) -> Result<()> {
Ok(())
}

fn data_for_key(inputs: &BTreeMap<String, Value>, k: &String) -> Data {
fn data_for_key(inputs: &BTreeMap<String, Data>, k: &String) -> Data {
match inputs.get(k) {
Some(v) => Data::from(v),
Some(v) => v.clone(),
None => Data::empty(),
}
}
7 changes: 3 additions & 4 deletions src/cli/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use bitcoin::{
absolute::LockTime, consensus::encode::serialize_hex, hashes::Hash, transaction::Version,
Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Txid,
};
use charms_data::{App, TxId, UtxoId};
use ciborium::Value;
use charms_data::{App, Data, TxId, UtxoId};
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, BTreeSet},
Expand All @@ -31,10 +30,10 @@ struct BListUnspentItem {
struct OutputWithCharms {
confirmations: u32,
sats: u64,
charms: BTreeMap<String, Value>,
charms: BTreeMap<String, Data>,
}

type ParsedCharms = BTreeMap<App, Value>;
type ParsedCharms = BTreeMap<App, Data>;

#[derive(Debug, Serialize)]
struct AppsAndCharmsOutputs {
Expand Down
30 changes: 12 additions & 18 deletions src/spell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ use crate::{app, SPELL_CHECKER_BINARY};
use anyhow::{anyhow, ensure, Error};
use bitcoin::{address::NetworkUnchecked, Address};
use charms_data::{util, App, Charms, Data, Transaction, UtxoId, B32};
use charms_spell_checker::{
pub use charms_spell_checker::{
NormalizedCharms, NormalizedSpell, NormalizedTransaction, Proof, SpellProverInput,
CURRENT_VERSION,
};
use ciborium::Value;
use serde::{Deserialize, Serialize};
use sp1_sdk::{HashableKey, ProverClient, SP1Stdin};
use std::collections::{BTreeMap, BTreeSet};

/// Charm as represented in a spell.
/// Map of `$KEY: data`.
pub type KeyedCharms = BTreeMap<String, Value>;
pub type KeyedCharms = BTreeMap<String, Data>;

/// UTXO as represented in a spell.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -43,10 +42,10 @@ pub struct Spell {
pub apps: BTreeMap<String, App>,

#[serde(skip_serializing_if = "Option::is_none")]
pub public_inputs: Option<BTreeMap<String, Value>>,
pub public_inputs: Option<BTreeMap<String, Data>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub private_inputs: Option<BTreeMap<String, Value>>,
pub private_inputs: Option<BTreeMap<String, Data>>,

pub ins: Vec<Input>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -258,17 +257,14 @@ pub fn str_index(i: &usize) -> String {

fn app_inputs(
keyed_apps: &BTreeMap<String, App>,
keyed_inputs: &BTreeMap<String, Value>,
keyed_inputs: &BTreeMap<String, Data>,
) -> BTreeMap<App, Data> {
keyed_apps
.iter()
.map(|(k, app)| {
(
app.clone(),
keyed_inputs
.get(k)
.map(|v| Data::from(v))
.unwrap_or_default(),
keyed_inputs.get(k).cloned().unwrap_or_default(),
)
})
.collect()
Expand Down Expand Up @@ -328,26 +324,24 @@ pub fn prove(
mod test {
use super::*;

use ciborium::Value;

#[test]
fn deserialize_keyed_charm() {
let y = r#"
$TOAD_SUB: 10
$TOAD: 9
"#;

let charms = serde_yaml::from_str::<KeyedCharms>(y).unwrap();
let charms: KeyedCharms = serde_yaml::from_str(y).unwrap();
dbg!(&charms);

let utxo_id =
let utxo_id_0 =
UtxoId::from_str("f72700ac56bd4dd61f2ccb4acdf21d0b11bb294fc3efa9012b77903932197d2f:2")
.unwrap();
let buf = util::write(&utxo_id).unwrap();
let buf = util::write(&utxo_id_0).unwrap();

let utxo_id_value: Value = util::read(buf.as_slice()).unwrap();
let utxo_id_data: Data = util::read(buf.as_slice()).unwrap();

let utxo_id: UtxoId = dbg!(utxo_id_value).deserialized().unwrap();
dbg!(utxo_id);
let utxo_id: UtxoId = utxo_id_data.value().unwrap();
assert_eq!(utxo_id_0, dbg!(utxo_id));
}
}

0 comments on commit 6d737af

Please sign in to comment.