Skip to content

Commit

Permalink
use singleton constants and types from chia-wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Feb 11, 2024
1 parent 4aec2bc commit 8cb9ad6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 69 deletions.
78 changes: 13 additions & 65 deletions chia-tools/src/bin/run-spend.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use chia::gen::conditions::Condition;
use chia_protocol::Bytes32;
use chia_traits::Streamable;
use clap::Parser;
use clvm_traits::{FromClvm, ToClvm, ToNodePtr};
use clvm_traits::{FromClvm, ToNodePtr};
use clvm_utils::tree_hash;
use clvm_utils::CurriedProgram;
use clvmr::{allocator::NodePtr, Allocator};
use hex_literal::hex;

use chia_wallet::singleton::{SINGLETON_TOP_LAYER_PUZZLE_HASH, SingletonArgs, SingletonSolution};
use chia_wallet::Proof;

/// Run a puzzle given a solution and print the resulting conditions
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -117,55 +118,6 @@ impl DebugPrint for Condition {
}
}

const SINGLETON_MOD_HASH: [u8; 32] =
hex!("7faa3253bfddd1e0decb0906b2dc6247bbc4cf608f58345d173adb63e8b47c9f");

#[derive(FromClvm, ToClvm, Debug)]
#[clvm(tuple)]
pub struct SingletonStruct {
pub mod_hash: Bytes32,
pub launcher_id: Bytes32,
pub launcher_puzzle_hash: Bytes32,
}

#[derive(FromClvm, ToClvm, Debug)]
#[clvm(curry)]
pub struct SingletonArgs<I> {
pub singleton_struct: SingletonStruct,
pub inner_puzzle: I,
}

#[derive(FromClvm, ToClvm, Debug)]
#[clvm(list)]
pub struct LineageProof {
pub parent_parent_coin_id: Bytes32,
pub parent_inner_puzzle_hash: Bytes32,
pub parent_amount: u64,
}

#[derive(FromClvm, ToClvm, Debug)]
#[clvm(list)]
pub struct EveProof {
pub parent_parent_coin_id: Bytes32,
pub parent_amount: u64,
}

#[derive(FromClvm, ToClvm, Debug)]
#[clvm(list)]
pub struct SingletonSolution<I> {
pub lineage_proof: LineageProof,
pub amount: u64,
pub inner_solution: I,
}

#[derive(FromClvm, ToClvm, Debug)]
#[clvm(list)]
pub struct EveSingletonSolution<I> {
pub lineage_proof: EveProof,
pub amount: u64,
pub inner_solution: I,
}

fn print_puzzle_info(a: &Allocator, puzzle: NodePtr, solution: NodePtr) {
println!("Puzzle: {}", hex::encode(tree_hash(a, puzzle)));
// exit if this puzzle is not curried
Expand All @@ -175,7 +127,7 @@ fn print_puzzle_info(a: &Allocator, puzzle: NodePtr, solution: NodePtr) {
};

match tree_hash(a, uncurried.program) {
SINGLETON_MOD_HASH => {
SINGLETON_TOP_LAYER_PUZZLE_HASH => {
println!("singleton_top_layer_1_1.clsp");
let Ok(uncurried) =
CurriedProgram::<NodePtr, SingletonArgs<NodePtr>>::from_clvm(a, puzzle)
Expand All @@ -197,23 +149,19 @@ fn print_puzzle_info(a: &Allocator, puzzle: NodePtr, solution: NodePtr) {
uncurried.args.singleton_struct.launcher_puzzle_hash
);

let inner_solution = if let Ok(sol) = SingletonSolution::from_clvm(a, solution) {
println!(" solution");
println!(" lineage-proof: {:?}", sol.lineage_proof);
println!(" amount: {}", sol.amount);
sol.inner_solution
} else if let Ok(sol) = EveSingletonSolution::from_clvm(a, solution) {
println!(" eve-solution:");
println!(" lineage-proof:: {:?}", sol.lineage_proof);
println!(" amount: {}", sol.amount);
sol.inner_solution
} else {
let Ok(sol) = SingletonSolution::<NodePtr>::from_clvm(a, solution) else {
println!("-- failed to parse singleton solution");
return;
};
println!(" solution");
match sol.proof {
Proof::Lineage(lp) => { println!(" lineage-proof: {lp:?}"); }
Proof::Eve(ep) => { println!(" eve-proof: {ep:?}"); }
}
println!(" amount: {}", sol.amount);

println!("\nInner Puzzle:\n");
print_puzzle_info(a, uncurried.args.inner_puzzle, inner_solution);
print_puzzle_info(a, uncurried.args.inner_puzzle, sol.inner_solution);
}

// Unknown puzzle
Expand Down
8 changes: 4 additions & 4 deletions chia-wallet/src/puzzles/singleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl SingletonStruct {
}

/// This is the puzzle reveal of the [singleton launcher](https://chialisp.com/singletons#launcher) puzzle.
pub static SINGLETON_LAUNCHER_PUZZLE: [u8; 175] = hex!(
pub const SINGLETON_LAUNCHER_PUZZLE: [u8; 175] = hex!(
"
ff02ffff01ff04ffff04ff04ffff04ff05ffff04ff0bff80808080ffff04ffff
04ff0affff04ffff02ff0effff04ff02ffff04ffff04ff05ffff04ff0bffff04
Expand All @@ -58,14 +58,14 @@ pub static SINGLETON_LAUNCHER_PUZZLE: [u8; 175] = hex!(
);

/// This is the puzzle hash of the [singleton launcher](https://chialisp.com/singletons#launcher) puzzle.
pub static SINGLETON_LAUNCHER_PUZZLE_HASH: [u8; 32] = hex!(
pub const SINGLETON_LAUNCHER_PUZZLE_HASH: [u8; 32] = hex!(
"
eff07522495060c066f66f32acc2a77e3a3e737aca8baea4d1a64ea4cdc13da9
"
);

/// This is the puzzle reveal of the [singleton](https://chialisp.com/singletons) puzzle.
pub static SINGLETON_TOP_LAYER_PUZZLE: [u8; 967] = hex!(
pub const SINGLETON_TOP_LAYER_PUZZLE: [u8; 967] = hex!(
"
ff02ffff01ff02ffff03ffff18ff2fff3480ffff01ff04ffff04ff20ffff04ff
2fff808080ffff04ffff02ff3effff04ff02ffff04ff05ffff04ffff02ff2aff
Expand Down Expand Up @@ -102,7 +102,7 @@ pub static SINGLETON_TOP_LAYER_PUZZLE: [u8; 967] = hex!(
);

/// This is the puzzle hash of the [singleton](https://chialisp.com/singletons) puzzle.
pub static SINGLETON_TOP_LAYER_PUZZLE_HASH: [u8; 32] = hex!(
pub const SINGLETON_TOP_LAYER_PUZZLE_HASH: [u8; 32] = hex!(
"
7faa3253bfddd1e0decb0906b2dc6247bbc4cf608f58345d173adb63e8b47c9f
"
Expand Down

0 comments on commit 8cb9ad6

Please sign in to comment.