diff --git a/crates/chia-sdk-driver/src/puzzles/cat/issue_cat.rs b/crates/chia-sdk-driver/src/puzzles/cat/issue_cat.rs index f0a88cf6..ef196bfc 100644 --- a/crates/chia-sdk-driver/src/puzzles/cat/issue_cat.rs +++ b/crates/chia-sdk-driver/src/puzzles/cat/issue_cat.rs @@ -54,11 +54,8 @@ impl IssueCat { })?; let asset_id = ctx.tree_hash(tail).into(); - self.raw_condition(ctx.alloc(&RunTail { - program: tail, - solution: NodePtr::NIL, - })?) - .finish_raw(ctx, asset_id, amount) + self.raw_condition(ctx.alloc(&RunTail::new(tail, ()))?) + .finish_raw(ctx, asset_id, amount) } pub fn multi_issuance( @@ -75,11 +72,8 @@ impl IssueCat { })?; let asset_id = ctx.tree_hash(tail).into(); - self.raw_condition(ctx.alloc(&RunTail { - program: tail, - solution: NodePtr::NIL, - })?) - .finish_raw(ctx, asset_id, amount) + self.raw_condition(ctx.alloc(&RunTail::new(tail, ()))?) + .finish_raw(ctx, asset_id, amount) } pub fn finish_raw( diff --git a/crates/chia-sdk-driver/src/spend_builder.rs b/crates/chia-sdk-driver/src/spend_builder.rs index 24298262..c98ec5a6 100644 --- a/crates/chia-sdk-driver/src/spend_builder.rs +++ b/crates/chia-sdk-driver/src/spend_builder.rs @@ -40,11 +40,7 @@ pub trait P2Spend: Sized { amount: u64, hint: Bytes32, ) -> Result { - Ok(self.raw_condition(ctx.alloc(&CreateCoin::with_custom_hint( - puzzle_hash, - amount, - hint, - ))?)) + Ok(self.raw_condition(ctx.alloc(&CreateCoin::with_hint(puzzle_hash, amount, hint))?)) } fn create_coin_announcement( diff --git a/crates/chia-sdk-parser/src/puzzles/nft.rs b/crates/chia-sdk-parser/src/puzzles/nft.rs index ab5f0a52..abd99cdb 100644 --- a/crates/chia-sdk-parser/src/puzzles/nft.rs +++ b/crates/chia-sdk-parser/src/puzzles/nft.rs @@ -105,17 +105,24 @@ impl NftPuzzle { ownership_solution.inner_solution, )?; - let create_coin = conditions.iter().find_map(|condition| match condition { - Condition::CreateCoin(create_coin) if create_coin.amount % 2 == 1 => Some(create_coin), - _ => None, - }); - - let new_owner = conditions.iter().find_map(|condition| match condition { - Condition::NewNftOwner(new_owner) => Some(new_owner), - _ => None, - }); + let mut create_coin = None; + let mut new_nft_owner = None; + + for condition in conditions { + match condition { + Condition::CreateCoin(condition) => { + create_coin = Some(condition); + } + Condition::Other(condition) => { + if let Ok(condition) = NewNftOwner::from_clvm(allocator, condition) { + new_nft_owner = Some(condition); + } + } + _ => {} + } + } - Ok((create_coin.cloned(), new_owner.cloned())) + Ok((create_coin, new_nft_owner)) } pub fn child_coin_info( diff --git a/crates/chia-sdk-signer/src/required_signature.rs b/crates/chia-sdk-signer/src/required_signature.rs index 53e9625d..61d3df02 100644 --- a/crates/chia-sdk-signer/src/required_signature.rs +++ b/crates/chia-sdk-signer/src/required_signature.rs @@ -32,13 +32,11 @@ impl RequiredSignature { let appended_info = match condition.kind { AggSigKind::Parent => { hasher.update([43]); - let parent = coin.parent_coin_info; - parent.to_vec() + coin.parent_coin_info.to_vec() } AggSigKind::Puzzle => { hasher.update([44]); - let puzzle = coin.puzzle_hash; - puzzle.to_vec() + coin.puzzle_hash.to_vec() } AggSigKind::Amount => { hasher.update([45]); @@ -56,9 +54,7 @@ impl RequiredSignature { } AggSigKind::ParentPuzzle => { hasher.update([48]); - let parent = coin.parent_coin_info; - let puzzle = coin.puzzle_hash; - [parent.to_vec(), puzzle.to_vec()].concat() + [coin.parent_coin_info.to_vec(), coin.puzzle_hash.to_vec()].concat() } AggSigKind::Unsafe => { return Self { diff --git a/crates/chia-sdk-test/src/simulator.rs b/crates/chia-sdk-test/src/simulator.rs index 95e9e9d2..98db75c3 100644 --- a/crates/chia-sdk-test/src/simulator.rs +++ b/crates/chia-sdk-test/src/simulator.rs @@ -150,7 +150,7 @@ impl Drop for Simulator { mod tests { use chia_bls::{PublicKey, Signature}; use chia_protocol::{ - CoinSpend, CoinStateFilters, CoinStateUpdate, RejectCoinState, RejectPuzzleState, + Bytes, CoinSpend, CoinStateFilters, CoinStateUpdate, RejectCoinState, RejectPuzzleState, RequestCoinState, RequestPuzzleState, RespondCoinState, RespondPuzzleState, SpendBundle, }; use chia_sdk_types::conditions::{AggSigMe, CreateCoin, Remark}; @@ -245,10 +245,7 @@ mod tests { vec![CoinSpend::new( coin, puzzle_reveal, - to_program([AggSigMe { - public_key, - message: Vec::new().into(), - }])?, + to_program([AggSigMe::new(public_key, Bytes::default())])?, )], Signature::default(), ); @@ -272,10 +269,7 @@ mod tests { vec![CoinSpend::new( coin, puzzle_reveal, - to_program([AggSigMe { - public_key: PublicKey::default(), - message: Vec::new().into(), - }])?, + to_program([AggSigMe::new(PublicKey::default(), Bytes::default())])?, )], Signature::default(), ); @@ -302,10 +296,7 @@ mod tests { vec![CoinSpend::new( coin, puzzle_reveal, - to_program([AggSigMe { - public_key: pk, - message: b"Hello, world!".to_vec().into(), - }])?, + to_program([AggSigMe::new(pk, b"Hello, world!".to_vec().into())])?, )], &[sk], sim.config.genesis_challenge, @@ -336,14 +327,8 @@ mod tests { coin, puzzle_reveal, to_program([ - AggSigMe { - public_key: pk1, - message: b"Hello, world!".to_vec().into(), - }, - AggSigMe { - public_key: pk2, - message: b"Goodbye, world!".to_vec().into(), - }, + AggSigMe::new(pk1, b"Hello, world!".to_vec().into()), + AggSigMe::new(pk2, b"Goodbye, world!".to_vec().into()), ])?, )], &[sk1, sk2], @@ -485,7 +470,7 @@ mod tests { let peer = sim.connect().await?; let (puzzle_hash, puzzle_reveal) = to_puzzle(1)?; - let solution = to_program([Remark {}])?; + let solution = to_program([Remark::new(())])?; let coin = sim.mint_coin(puzzle_hash, 0).await; @@ -734,7 +719,7 @@ mod tests { vec![CoinSpend::new( coin, puzzle_reveal, - to_program([CreateCoin::with_custom_hint(puzzle_hash, 0, hint)])?, + to_program([CreateCoin::with_hint(puzzle_hash, 0, hint)])?, )], Signature::default(), ); diff --git a/crates/chia-sdk-types/src/conditions.rs b/crates/chia-sdk-types/src/conditions.rs index a0c9789e..b1216240 100644 --- a/crates/chia-sdk-types/src/conditions.rs +++ b/crates/chia-sdk-types/src/conditions.rs @@ -14,12 +14,13 @@ pub use output::*; pub use puzzles::*; pub use time::*; -use clvm_traits::{apply_constants, FromClvm, ToClvm}; +use clvm_traits::{FromClvm, ToClvm}; +use clvmr::NodePtr; #[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)] #[clvm(transparent)] -pub enum Condition { - Remark(Remark), +pub enum Condition { + Remark(Remark), AggSig(AggSig), CreateCoin(CreateCoin), ReserveFee(ReserveFee), @@ -45,19 +46,5 @@ pub enum Condition { AssertBeforeHeightRelative(AssertBeforeHeightRelative), AssertBeforeHeightAbsolute(AssertBeforeHeightAbsolute), Softfork(Softfork), - RunTail(RunTail), - MeltSingleton(MeltSingleton), - NewNftOwner(NewNftOwner), -} - -#[derive(ToClvm, FromClvm)] -#[apply_constants] -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[clvm(list)] -pub struct Softfork { - #[clvm(constant = 90)] - pub opcode: u8, - pub cost: u64, - #[clvm(rest)] - pub rest: T, + Other(T), } diff --git a/crates/chia-sdk-types/src/conditions/agg_sig.rs b/crates/chia-sdk-types/src/conditions/agg_sig.rs index 184a1e94..3a21fa5d 100644 --- a/crates/chia-sdk-types/src/conditions/agg_sig.rs +++ b/crates/chia-sdk-types/src/conditions/agg_sig.rs @@ -10,6 +10,16 @@ pub struct AggSig { pub message: Bytes, } +impl AggSig { + pub fn new(kind: AggSigKind, public_key: PublicKey, message: Bytes) -> Self { + Self { + kind, + public_key, + message, + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm, Hash)] #[repr(u8)] #[clvm(atom)] @@ -35,6 +45,15 @@ pub struct AggSigParent { pub message: Bytes, } +impl AggSigParent { + pub fn new(public_key: PublicKey, message: Bytes) -> Self { + Self { + public_key, + message, + } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, PartialEq, Eq)] @@ -46,6 +65,15 @@ pub struct AggSigPuzzle { pub message: Bytes, } +impl AggSigPuzzle { + pub fn new(public_key: PublicKey, message: Bytes) -> Self { + Self { + public_key, + message, + } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, PartialEq, Eq)] @@ -57,6 +85,15 @@ pub struct AggSigAmount { pub message: Bytes, } +impl AggSigAmount { + pub fn new(public_key: PublicKey, message: Bytes) -> Self { + Self { + public_key, + message, + } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, PartialEq, Eq)] @@ -68,6 +105,15 @@ pub struct AggSigPuzzleAmount { pub message: Bytes, } +impl AggSigPuzzleAmount { + pub fn new(public_key: PublicKey, message: Bytes) -> Self { + Self { + public_key, + message, + } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, PartialEq, Eq)] @@ -79,6 +125,15 @@ pub struct AggSigParentAmount { pub message: Bytes, } +impl AggSigParentAmount { + pub fn new(public_key: PublicKey, message: Bytes) -> Self { + Self { + public_key, + message, + } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, PartialEq, Eq)] @@ -90,6 +145,15 @@ pub struct AggSigParentPuzzle { pub message: Bytes, } +impl AggSigParentPuzzle { + pub fn new(public_key: PublicKey, message: Bytes) -> Self { + Self { + public_key, + message, + } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, PartialEq, Eq)] @@ -101,6 +165,15 @@ pub struct AggSigUnsafe { pub message: Bytes, } +impl AggSigUnsafe { + pub fn new(public_key: PublicKey, message: Bytes) -> Self { + Self { + public_key, + message, + } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, PartialEq, Eq)] @@ -111,3 +184,12 @@ pub struct AggSigMe { pub public_key: PublicKey, pub message: Bytes, } + +impl AggSigMe { + pub fn new(public_key: PublicKey, message: Bytes) -> Self { + Self { + public_key, + message, + } + } +} diff --git a/crates/chia-sdk-types/src/conditions/announcements.rs b/crates/chia-sdk-types/src/conditions/announcements.rs index a7623ed7..63b3f38b 100644 --- a/crates/chia-sdk-types/src/conditions/announcements.rs +++ b/crates/chia-sdk-types/src/conditions/announcements.rs @@ -12,6 +12,12 @@ pub struct CreateCoinAnnouncement { pub message: Bytes, } +impl CreateCoinAnnouncement { + pub fn new(message: Bytes) -> Self { + Self { message } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -22,6 +28,12 @@ pub struct AssertCoinAnnouncement { pub announcement_id: Bytes32, } +impl AssertCoinAnnouncement { + pub fn new(announcement_id: Bytes32) -> Self { + Self { announcement_id } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, PartialEq, Eq)] @@ -32,6 +44,12 @@ pub struct CreatePuzzleAnnouncement { pub message: Bytes, } +impl CreatePuzzleAnnouncement { + pub fn new(message: Bytes) -> Self { + Self { message } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -42,6 +60,12 @@ pub struct AssertPuzzleAnnouncement { pub announcement_id: Bytes32, } +impl AssertPuzzleAnnouncement { + pub fn new(announcement_id: Bytes32) -> Self { + Self { announcement_id } + } +} + pub fn announcement_id(coin_info: Bytes32, message: impl AsRef<[u8]>) -> Bytes32 { let mut hasher = Sha256::new(); hasher.update(coin_info); diff --git a/crates/chia-sdk-types/src/conditions/coin_info.rs b/crates/chia-sdk-types/src/conditions/coin_info.rs index e74fc3aa..fb4847e4 100644 --- a/crates/chia-sdk-types/src/conditions/coin_info.rs +++ b/crates/chia-sdk-types/src/conditions/coin_info.rs @@ -41,6 +41,12 @@ pub struct AssertMyAmount { pub amount: u64, } +impl AssertMyAmount { + pub fn new(amount: u64) -> Self { + Self { amount } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -51,6 +57,12 @@ pub struct AssertMyBirthSeconds { pub seconds: u64, } +impl AssertMyBirthSeconds { + pub fn new(seconds: u64) -> Self { + Self { seconds } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -60,3 +72,9 @@ pub struct AssertMyBirthHeight { pub opcode: u8, pub height: u32, } + +impl AssertMyBirthHeight { + pub fn new(height: u32) -> Self { + Self { height } + } +} diff --git a/crates/chia-sdk-types/src/conditions/concurrent.rs b/crates/chia-sdk-types/src/conditions/concurrent.rs index 57579a30..293233b3 100644 --- a/crates/chia-sdk-types/src/conditions/concurrent.rs +++ b/crates/chia-sdk-types/src/conditions/concurrent.rs @@ -11,6 +11,12 @@ pub struct AssertConcurrentSpend { pub coin_id: Bytes32, } +impl AssertConcurrentSpend { + pub fn new(coin_id: Bytes32) -> Self { + Self { coin_id } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -21,11 +27,23 @@ pub struct AssertConcurrentPuzzle { pub puzzle_hash: Bytes32, } +impl AssertConcurrentPuzzle { + pub fn new(puzzle_hash: Bytes32) -> Self { + Self { puzzle_hash } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] #[clvm(list)] pub struct AssertEphemeral { #[clvm(constant = 76)] pub opcode: u8, } + +impl AssertEphemeral { + pub fn new() -> Self { + Self::default() + } +} diff --git a/crates/chia-sdk-types/src/conditions/output.rs b/crates/chia-sdk-types/src/conditions/output.rs index f319456b..882de095 100644 --- a/crates/chia-sdk-types/src/conditions/output.rs +++ b/crates/chia-sdk-types/src/conditions/output.rs @@ -15,11 +15,11 @@ pub struct CreateCoin { } impl CreateCoin { - pub const fn new(puzzle_hash: Bytes32, amount: u64) -> Self { + pub fn new(puzzle_hash: Bytes32, amount: u64) -> Self { Self::with_memos(puzzle_hash, amount, Vec::new()) } - pub const fn with_memos(puzzle_hash: Bytes32, amount: u64, memos: Vec) -> Self { + pub fn with_memos(puzzle_hash: Bytes32, amount: u64, memos: Vec) -> Self { Self { puzzle_hash, amount, @@ -27,11 +27,7 @@ impl CreateCoin { } } - pub fn with_hint(puzzle_hash: Bytes32, amount: u64) -> Self { - Self::with_custom_hint(puzzle_hash, amount, puzzle_hash) - } - - pub fn with_custom_hint(puzzle_hash: Bytes32, amount: u64, hint: Bytes32) -> Self { + pub fn with_hint(puzzle_hash: Bytes32, amount: u64, hint: Bytes32) -> Self { Self::with_memos(puzzle_hash, amount, vec![hint.into()]) } } @@ -45,3 +41,9 @@ pub struct ReserveFee { pub opcode: u8, pub amount: u64, } + +impl ReserveFee { + pub fn new(amount: u64) -> Self { + Self { amount } + } +} diff --git a/crates/chia-sdk-types/src/conditions/puzzles.rs b/crates/chia-sdk-types/src/conditions/puzzles.rs index b978e6e8..f7a94ba6 100644 --- a/crates/chia-sdk-types/src/conditions/puzzles.rs +++ b/crates/chia-sdk-types/src/conditions/puzzles.rs @@ -1,13 +1,40 @@ use chia_protocol::Bytes32; use clvm_traits::{apply_constants, FromClvm, ToClvm}; +use clvmr::NodePtr; #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[clvm(list)] -pub struct Remark { +pub struct Softfork { + #[clvm(constant = 90)] + pub opcode: u8, + pub cost: u64, + #[clvm(rest)] + pub rest: T, +} + +impl Softfork { + pub fn new(cost: u64, rest: T) -> Self { + Self { cost, rest } + } +} + +#[derive(ToClvm, FromClvm)] +#[apply_constants] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +#[clvm(list)] +pub struct Remark { #[clvm(constant = 1)] pub opcode: u8, + #[clvm(rest)] + pub rest: T, +} + +impl Remark { + pub fn new(rest: T) -> Self { + Self { rest } + } } #[derive(ToClvm, FromClvm)] @@ -25,9 +52,15 @@ pub struct RunTail { pub solution: S, } +impl RunTail { + pub fn new(program: P, solution: S) -> Self { + Self { program, solution } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] #[clvm(list)] pub struct MeltSingleton { #[clvm(constant = 51)] @@ -38,6 +71,12 @@ pub struct MeltSingleton { pub magic_amount: i8, } +impl MeltSingleton { + pub fn new() -> Self { + Self::default() + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Default, Clone, PartialEq, Eq)] @@ -50,9 +89,32 @@ pub struct NewNftOwner { pub new_did_p2_puzzle_hash: Option, } +impl NewNftOwner { + pub fn new( + new_owner: Option, + trade_prices_list: Vec, + new_did_p2_puzzle_hash: Option, + ) -> Self { + Self { + new_owner, + trade_prices_list, + new_did_p2_puzzle_hash, + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)] #[clvm(list)] pub struct NftTradePrice { pub trade_price: u16, pub puzzle_hash: Bytes32, } + +impl NftTradePrice { + pub fn new(trade_price: u16, puzzle_hash: Bytes32) -> Self { + Self { + trade_price, + puzzle_hash, + } + } +} diff --git a/crates/chia-sdk-types/src/conditions/time.rs b/crates/chia-sdk-types/src/conditions/time.rs index f5096993..0d64f02c 100644 --- a/crates/chia-sdk-types/src/conditions/time.rs +++ b/crates/chia-sdk-types/src/conditions/time.rs @@ -10,6 +10,12 @@ pub struct AssertSecondsRelative { pub seconds: u64, } +impl AssertSecondsRelative { + pub fn new(seconds: u64) -> Self { + Self { seconds } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -20,6 +26,12 @@ pub struct AssertSecondsAbsolute { pub seconds: u64, } +impl AssertSecondsAbsolute { + pub fn new(seconds: u64) -> Self { + Self { seconds } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -30,6 +42,12 @@ pub struct AssertHeightRelative { pub height: u32, } +impl AssertHeightRelative { + pub fn new(height: u32) -> Self { + Self { height } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -40,6 +58,12 @@ pub struct AssertHeightAbsolute { pub height: u32, } +impl AssertHeightAbsolute { + pub fn new(height: u32) -> Self { + Self { height } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -50,6 +74,12 @@ pub struct AssertBeforeSecondsRelative { pub seconds: u64, } +impl AssertBeforeSecondsRelative { + pub fn new(seconds: u64) -> Self { + Self { seconds } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -60,6 +90,12 @@ pub struct AssertBeforeSecondsAbsolute { pub seconds: u64, } +impl AssertBeforeSecondsAbsolute { + pub fn new(seconds: u64) -> Self { + Self { seconds } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -70,6 +106,12 @@ pub struct AssertBeforeHeightRelative { pub height: u32, } +impl AssertBeforeHeightRelative { + pub fn new(height: u32) -> Self { + Self { height } + } +} + #[derive(ToClvm, FromClvm)] #[apply_constants] #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -79,3 +121,9 @@ pub struct AssertBeforeHeightAbsolute { pub opcode: u8, pub height: u32, } + +impl AssertBeforeHeightAbsolute { + pub fn new(height: u32) -> Self { + Self { height } + } +}