Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run spend #400

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 98 additions & 67 deletions chia-tools/src/bin/run-spend.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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::cat::{CatArgs, CatSolution, CAT_PUZZLE_HASH};
use chia_wallet::did::{DidArgs, DidSolution, DID_INNER_PUZZLE_HASH};
use chia_wallet::singleton::{SingletonArgs, SingletonSolution, SINGLETON_TOP_LAYER_PUZZLE_HASH};
use chia_wallet::standard::{StandardArgs, StandardSolution, STANDARD_PUZZLE_HASH};
use chia_wallet::Proof;

/// Run a puzzle given a solution and print the resulting conditions
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -117,55 +121,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,12 +130,87 @@ fn print_puzzle_info(a: &Allocator, puzzle: NodePtr, solution: NodePtr) {
};

match tree_hash(a, uncurried.program) {
SINGLETON_MOD_HASH => {
STANDARD_PUZZLE_HASH => {
println!("p2_delegated_puzzle_or_hidden_puzzle.clsp");
let Ok(uncurried) = CurriedProgram::<NodePtr, StandardArgs>::from_clvm(a, puzzle)
else {
println!("-- failed to uncurry standard transaction");
return;
};
println!(" synthetic-key: {:?}", uncurried.args.synthetic_key);
let Ok(sol) = StandardSolution::<NodePtr, NodePtr>::from_clvm(a, solution) else {
println!("-- failed to parse solution");
return;
};
println!(" solution");
println!(" original-public-key: {:?}", sol.original_public_key);
println!("\nDelegated Puzzle\n");
print_puzzle_info(a, sol.delegated_puzzle, sol.solution);
}
CAT_PUZZLE_HASH => {
println!("cat_v2.clsp");
let Ok(uncurried) = CurriedProgram::<NodePtr, CatArgs<NodePtr>>::from_clvm(a, puzzle)
else {
println!("-- failed to uncurry CAT transaction");
return;
};
println!(" mod-hash: {:?}", uncurried.args.mod_hash);
println!(
" tail-program-hash: {:?}",
uncurried.args.tail_program_hash
);
let Ok(sol) = CatSolution::<NodePtr>::from_clvm(a, solution) else {
println!("-- failed to parse solution");
return;
};

println!(" solution");
println!(" lineage-proof: {:?}", sol.lineage_proof);
println!(" prev-coin-id: {:?}", sol.prev_coin_id);
println!(" this-coin-info: {:?}", sol.this_coin_info);
println!(" next-coin-proof: {:?}", sol.next_coin_proof);
println!(" prev-subtotal: {:?}", sol.prev_subtotal);
println!(" extra-delta: {:?}", sol.extra_delta);

println!("\nInner Puzzle\n");
print_puzzle_info(a, uncurried.args.inner_puzzle, sol.inner_puzzle_solution);
}
DID_INNER_PUZZLE_HASH => {
println!("did_innerpuz.clsp");
let Ok(uncurried) =
CurriedProgram::<NodePtr, DidArgs<NodePtr, NodePtr>>::from_clvm(a, puzzle)
else {
println!("-- failed to uncurry DID transaction");
return;
};
println!(
" recovery_did_list_hash: {:?}",
uncurried.args.recovery_did_list_hash
);
println!(
" num_verifications_required: {:?}",
uncurried.args.num_verifications_required
);
println!(
" singleton_struct: {:?}",
uncurried.args.singleton_struct
);
println!(" metadata: {:?}", uncurried.args.metadata);
let Ok(sol) = DidSolution::<NodePtr>::from_clvm(a, solution) else {
println!("-- failed to parse solution");
return;
};

println!("\nInner Puzzle\n");
let DidSolution::InnerSpend(inner_sol) = sol;
print_puzzle_info(a, uncurried.args.inner_puzzle, inner_sol);
}
SINGLETON_TOP_LAYER_PUZZLE_HASH => {
println!("singleton_top_layer_1_1.clsp");
let Ok(uncurried) =
CurriedProgram::<NodePtr, SingletonArgs<NodePtr>>::from_clvm(a, puzzle)
else {
println!("failed to uncurry singleton");
println!("-- failed to uncurry singleton");
return;
};
println!(" singleton-struct:");
Expand All @@ -197,24 +227,25 @@ 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 {
println!("-- failed to parse singleton solution");
let Ok(sol) = SingletonSolution::<NodePtr>::from_clvm(a, solution) else {
println!("-- failed to parse 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);
}
// TODO: NFT puzzles

// Unknown puzzle
n => {
Expand Down
10 changes: 5 additions & 5 deletions chia-wallet/src/puzzles/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn cat_puzzle_hash(asset_id: [u8; 32], inner_puzzle_hash: [u8; 32]) -> [u8;
}

/// This is the puzzle reveal of the [CAT2 standard](https://chialisp.com/cats) puzzle.
pub static CAT_PUZZLE: [u8; 1672] = hex!(
pub const CAT_PUZZLE: [u8; 1672] = hex!(
"
ff02ffff01ff02ff5effff04ff02ffff04ffff04ff05ffff04ffff0bff34ff05
80ffff04ff0bff80808080ffff04ffff02ff17ff2f80ffff04ff5fffff04ffff
Expand Down Expand Up @@ -109,14 +109,14 @@ pub static CAT_PUZZLE: [u8; 1672] = hex!(
);

/// This is the puzzle hash of the [CAT2 standard](https://chialisp.com/cats) puzzle.
pub static CAT_PUZZLE_HASH: [u8; 32] = hex!(
pub const CAT_PUZZLE_HASH: [u8; 32] = hex!(
"
37bef360ee858133b69d595a906dc45d01af50379dad515eb9518abb7c1d2a7a
"
);

/// This is the puzzle reveal of the [CAT2 multi-issuance TAIL](https://chialisp.com/cats#multi) puzzle.
pub static EVERYTHING_WITH_SIGNATURE_TAIL_PUZZLE: [u8; 41] = hex!(
pub const EVERYTHING_WITH_SIGNATURE_TAIL_PUZZLE: [u8; 41] = hex!(
"
ff02ffff01ff04ffff04ff02ffff04ff05ffff04ff5fff80808080ff8080ffff
04ffff0132ff018080
Expand All @@ -129,7 +129,7 @@ pub static EVERYTHING_WITH_SIGNATURE_TAIL_PUZZLE: [u8; 41] = hex!(
/// It is recommended not to use CAT1 for anything other than backwards compatibility (e.g. offer compression),
/// due to security issues uncovered in an audit. You can read more about the vulnerability that prompted the creation
/// of CAT2 in the [CATbleed Post Mortem](https://github.com/Chia-Network/post-mortem/blob/main/2022-08/2022-08-19-CATbleed.md).
pub static CAT_PUZZLE_V1: [u8; 1420] = hex!(
pub const CAT_PUZZLE_V1: [u8; 1420] = hex!(
"
ff02ffff01ff02ff5effff04ff02ffff04ffff04ff05ffff04ffff0bff2cff05
80ffff04ff0bff80808080ffff04ffff02ff17ff2f80ffff04ff5fffff04ffff
Expand Down Expand Up @@ -185,7 +185,7 @@ pub static CAT_PUZZLE_V1: [u8; 1420] = hex!(
/// It is recommended not to use CAT1 for anything other than backwards compatibility (e.g. offer compression),
/// due to security issues uncovered in an audit. You can read more about the vulnerability that prompted the creation
/// of CAT2 in the [CATbleed Post Mortem](https://github.com/Chia-Network/post-mortem/blob/main/2022-08/2022-08-19-CATbleed.md).
pub static CAT_PUZZLE_HASH_V1: [u8; 32] = hex!(
pub const CAT_PUZZLE_HASH_V1: [u8; 32] = hex!(
"
72dec062874cd4d3aab892a0906688a1ae412b0109982e1797a170add88bdcdc
"
Expand Down
4 changes: 2 additions & 2 deletions chia-wallet/src/puzzles/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where
}

/// This is the puzzle reveal of the [DID1 standard](https://chialisp.com/dids) puzzle.
pub static DID_INNER_PUZZLE: [u8; 1012] = hex!(
pub const DID_INNER_PUZZLE: [u8; 1012] = hex!(
"
ff02ffff01ff02ffff03ff81bfffff01ff02ff05ff82017f80ffff01ff02ffff
03ffff22ffff09ffff02ff7effff04ff02ffff04ff8217ffff80808080ff0b80
Expand Down Expand Up @@ -90,7 +90,7 @@ pub static DID_INNER_PUZZLE: [u8; 1012] = hex!(
);

/// This is the puzzle hash of the [DID1 standard](https://chialisp.com/dids) puzzle.
pub static DID_INNER_PUZZLE_HASH: [u8; 32] = hex!(
pub const DID_INNER_PUZZLE_HASH: [u8; 32] = hex!(
"
33143d2bef64f14036742673afd158126b94284b4530a28c354fac202b0c910e
"
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
8 changes: 4 additions & 4 deletions chia-wallet/src/puzzles/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn standard_puzzle_hash(synthetic_key: &PublicKey) -> [u8; 32] {
}

/// This is the puzzle reveal of the [standard transaction](https://chialisp.com/standard-transactions) puzzle.
pub static STANDARD_PUZZLE: [u8; 227] = hex!(
pub const STANDARD_PUZZLE: [u8; 227] = hex!(
"
ff02ffff01ff02ffff03ff0bffff01ff02ffff03ffff09ff05ffff1dff0bffff
1effff0bff0bffff02ff06ffff04ff02ffff04ff17ff8080808080808080ffff
Expand All @@ -37,17 +37,17 @@ pub static STANDARD_PUZZLE: [u8; 227] = hex!(
);

/// This is the puzzle hash of the [standard transaction](https://chialisp.com/standard-transactions) puzzle.
pub static STANDARD_PUZZLE_HASH: [u8; 32] = hex!(
pub const STANDARD_PUZZLE_HASH: [u8; 32] = hex!(
"
e9aaa49f45bad5c889b86ee3341550c155cfdd10c3a6757de618d20612fffd52
"
);

/// This is the puzzle reveal of the [default hidden puzzle](https://chialisp.com/standard-transactions#default-hidden-puzzle).
pub static DEFAULT_HIDDEN_PUZZLE: [u8; 3] = hex!("ff0980");
pub const DEFAULT_HIDDEN_PUZZLE: [u8; 3] = hex!("ff0980");

/// This is the puzzle hash of the [default hidden puzzle](https://chialisp.com/standard-transactions#default-hidden-puzzle).
pub static DEFAULT_HIDDEN_PUZZLE_HASH: [u8; 32] = hex!(
pub const DEFAULT_HIDDEN_PUZZLE_HASH: [u8; 32] = hex!(
"
711d6c4e32c92e53179b199484cf8c897542bc57f2b22582799f9d657eec4699
"
Expand Down
Loading