From 6d737af4451b67af336ee03eb6534b3f5780c0ba Mon Sep 17 00:00:00 2001 From: Ivan Mikushin Date: Sat, 18 Jan 2025 21:03:27 -0800 Subject: [PATCH] Cleanup: make Value an implementation detail of Data --- Cargo.lock | 1 - Cargo.toml | 1 - src/cli/app.rs | 5 ++--- src/cli/wallet.rs | 7 +++---- src/spell.rs | 30 ++++++++++++------------------ 5 files changed, 17 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d10705..ffdfd29 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1211,7 +1211,6 @@ dependencies = [ "bitcoincore-rpc", "charms-data", "charms-spell-checker", - "ciborium", "clap", "hex", "proptest", diff --git a/Cargo.toml b/Cargo.toml index b22c750..3c160f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/cli/app.rs b/src/cli/app.rs index c8f014f..9a1a79e 100644 --- a/src/cli/app.rs +++ b/src/cli/app.rs @@ -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, @@ -131,9 +130,9 @@ pub fn run(spell: PathBuf, path: Option) -> Result<()> { Ok(()) } -fn data_for_key(inputs: &BTreeMap, k: &String) -> Data { +fn data_for_key(inputs: &BTreeMap, k: &String) -> Data { match inputs.get(k) { - Some(v) => Data::from(v), + Some(v) => v.clone(), None => Data::empty(), } } diff --git a/src/cli/wallet.rs b/src/cli/wallet.rs index 39b452a..b9a4b30 100644 --- a/src/cli/wallet.rs +++ b/src/cli/wallet.rs @@ -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}, @@ -31,10 +30,10 @@ struct BListUnspentItem { struct OutputWithCharms { confirmations: u32, sats: u64, - charms: BTreeMap, + charms: BTreeMap, } -type ParsedCharms = BTreeMap; +type ParsedCharms = BTreeMap; #[derive(Debug, Serialize)] struct AppsAndCharmsOutputs { diff --git a/src/spell.rs b/src/spell.rs index 04ffb0b..3192655 100644 --- a/src/spell.rs +++ b/src/spell.rs @@ -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; +pub type KeyedCharms = BTreeMap; /// UTXO as represented in a spell. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -43,10 +42,10 @@ pub struct Spell { pub apps: BTreeMap, #[serde(skip_serializing_if = "Option::is_none")] - pub public_inputs: Option>, + pub public_inputs: Option>, #[serde(skip_serializing_if = "Option::is_none")] - pub private_inputs: Option>, + pub private_inputs: Option>, pub ins: Vec, #[serde(skip_serializing_if = "Option::is_none")] @@ -258,17 +257,14 @@ pub fn str_index(i: &usize) -> String { fn app_inputs( keyed_apps: &BTreeMap, - keyed_inputs: &BTreeMap, + keyed_inputs: &BTreeMap, ) -> BTreeMap { 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() @@ -328,8 +324,6 @@ pub fn prove( mod test { use super::*; - use ciborium::Value; - #[test] fn deserialize_keyed_charm() { let y = r#" @@ -337,17 +331,17 @@ $TOAD_SUB: 10 $TOAD: 9 "#; - let charms = serde_yaml::from_str::(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)); } }