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

EIP-712 Stuff #124

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 33 additions & 6 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ napi-derive = "2.12.2"
napi = { version = "2.12.2", default-features = false }
paste = "1.0.15"
bigdecimal = "0.4.6"
k256 = "0.13.4"
sha3 = "0.10.8"

[profile.release]
lto = true
strip = "symbols"

[patch.crates-io]
Yakuhito marked this conversation as resolved.
Show resolved Hide resolved
clvmr = { git = "https://github.com/Chia-Network/clvm_rs", rev = "2f413e72fcf1bcafa4a3117f2c2a0a3a0e7e1c6b" }
2 changes: 2 additions & 0 deletions crates/chia-sdk-driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ hex-literal = { workspace = true }
num-bigint = { workspace = true}
hex = { workspace = true }
bigdecimal = { workspace = true }
k256 = { workspace = true }
sha3 = { workspace = true }

[dev-dependencies]
chia-sdk-test = { workspace = true }
Expand Down
4 changes: 4 additions & 0 deletions crates/chia-sdk-driver/src/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ mod cat_layer;
mod did_layer;
mod nft_ownership_layer;
mod nft_state_layer;
mod p2_controller_puzzle_layer;
mod p2_delegated_conditions_layer;
mod p2_delegated_singleton_layer;
mod p2_eip712_message_layer;
mod p2_one_of_many;
mod p2_singleton;
mod royalty_transfer_layer;
Expand All @@ -15,8 +17,10 @@ pub use cat_layer::*;
pub use did_layer::*;
pub use nft_ownership_layer::*;
pub use nft_state_layer::*;
pub use p2_controller_puzzle_layer::*;
pub use p2_delegated_conditions_layer::*;
pub use p2_delegated_singleton_layer::*;
pub use p2_eip712_message_layer::*;
pub use p2_one_of_many::*;
pub use p2_singleton::*;
pub use royalty_transfer_layer::*;
Expand Down
175 changes: 175 additions & 0 deletions crates/chia-sdk-driver/src/layers/p2_controller_puzzle_layer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
use chia_protocol::Bytes32;
use clvm_traits::{FromClvm, ToClvm};
use clvm_utils::{CurriedProgram, TreeHash};
use clvmr::{Allocator, NodePtr};
use hex_literal::hex;

use crate::{DriverError, Layer, Puzzle, Spend, SpendContext};

// https://github.com/Yakuhito/hermes/blob/master/clsp/p2_controller_puzzle.clsp
pub const P2_CONTROLLER_PUZZLE_PUZZLE: [u8; 151] = hex!("ff02ffff01ff04ffff04ff04ffff04ffff0117ffff04ffff02ff06ffff04ff02ffff04ff0bff80808080ffff04ff05ff8080808080ffff02ff0bff178080ffff04ffff01ff43ff02ffff03ffff07ff0580ffff01ff0bffff0102ffff02ff06ffff04ff02ffff04ff09ff80808080ffff02ff06ffff04ff02ffff04ff0dff8080808080ffff01ff0bffff0101ff058080ff0180ff018080");
pub const P2_CONTROLLER_PUZZLE_PUZZLE_HASH: TreeHash = TreeHash::new(hex!(
"
d5415713619e318bfa7820e06e2b163beef32d82294a5a7fcf9c3c69b0949c88
"
));

/// The p2 controller puzzle [`Layer`] allows a coin to be 'owned' by another puzzle,
/// which sends a message containing a delegated puzzle for the controlled coin to run.
///
/// This is useful since it enables wallets to only ask for one signature instead of
/// one signature/coin when using standard-like puzzles. Specifically, this puzzle
/// was created to avoid having to sign multiple EIP-712 messages when using the
/// p2 EIP-712 message puzzle.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct P2ControllerPuzzleLayer {
pub controller_puzzle_hash: Bytes32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(curry)]
pub struct P2ControllerPuzzleArgs {
pub controller_puzzle_hash: Bytes32,
}

#[derive(Debug, Clone, PartialEq, Eq, ToClvm, FromClvm)]
#[clvm(solution)]
pub struct P2ControllerPuzzleSolution<P, S> {
pub delegated_puzzle: P,
pub delegated_solution: S,
}

impl P2ControllerPuzzleLayer {
pub fn new(controller_puzzle_hash: Bytes32) -> Self {
Self {
controller_puzzle_hash,
}
}

pub fn spend(
&self,
ctx: &mut SpendContext,
delegated_spend: Spend,
) -> Result<Spend, DriverError> {
self.construct_spend(
ctx,
P2ControllerPuzzleSolution {
delegated_puzzle: delegated_spend.puzzle,
delegated_solution: delegated_spend.solution,
},
)
}
}

impl Layer for P2ControllerPuzzleLayer {
type Solution = P2ControllerPuzzleSolution<NodePtr, NodePtr>;

fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
let curried = CurriedProgram {
program: ctx.p2_controller_puzzle_puzzle()?,
args: P2ControllerPuzzleArgs {
controller_puzzle_hash: self.controller_puzzle_hash,
},
};
ctx.alloc(&curried)
}

fn construct_solution(
&self,
ctx: &mut SpendContext,
solution: Self::Solution,
) -> Result<NodePtr, DriverError> {
ctx.alloc(&solution)
}

fn parse_puzzle(allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError> {
let Some(puzzle) = puzzle.as_curried() else {
return Ok(None);
};

if puzzle.mod_hash != P2_CONTROLLER_PUZZLE_PUZZLE_HASH {
return Ok(None);
}

let args = P2ControllerPuzzleArgs::from_clvm(allocator, puzzle.args)?;

Ok(Some(Self {
controller_puzzle_hash: args.controller_puzzle_hash,
}))
}

fn parse_solution(
allocator: &Allocator,
solution: NodePtr,
) -> Result<Self::Solution, DriverError> {
Ok(Self::Solution::from_clvm(allocator, solution)?)
}
}

#[cfg(test)]
mod tests {
use crate::assert_puzzle_hash;

use super::*;
use chia_protocol::{Bytes, CoinSpend};
use chia_sdk_test::Simulator;
use chia_sdk_types::Conditions;
use clvm_traits::clvm_quote;

#[test]
fn test_puzzle_hash() -> anyhow::Result<()> {
assert_puzzle_hash!(P2_CONTROLLER_PUZZLE_PUZZLE => P2_CONTROLLER_PUZZLE_PUZZLE_HASH);

Ok(())
}

#[test]
fn test_p2_controller_puzzle() -> anyhow::Result<()> {
let mut sim = Simulator::new();
let ctx = &mut SpendContext::new();

let controller_puzzle = ctx.allocator.one();
let controller_puzzle_hash = ctx.tree_hash(controller_puzzle);
Yakuhito marked this conversation as resolved.
Show resolved Hide resolved

let layer = P2ControllerPuzzleLayer::new(controller_puzzle_hash.into());
let coin_puzzle = layer.construct_puzzle(ctx)?;
let coin_puzzle_hash = ctx.tree_hash(coin_puzzle);

let controller_coin = sim.new_coin(controller_puzzle_hash.into(), 42);
let coin = sim.new_coin(coin_puzzle_hash.into(), 69);

let delegated_puzzle =
clvm_quote!(Conditions::new().reserve_fee(42 + 69)).to_clvm(&mut ctx.allocator)?;
let delegated_solution = ctx.allocator.nil();

let delegated_puzzle_hash = ctx.tree_hash(delegated_puzzle);

let coin_spend = layer.construct_coin_spend(
ctx,
coin,
P2ControllerPuzzleSolution {
delegated_puzzle,
delegated_solution,
},
)?;
ctx.insert(coin_spend);

let controller_solution = Conditions::new().send_message(
23,
Bytes::from(delegated_puzzle_hash.to_vec()),
vec![coin.coin_id().to_clvm(&mut ctx.allocator)?],
);
let controller_solution = controller_solution.to_clvm(&mut ctx.allocator)?;

let controller_coin_spend = CoinSpend::new(
controller_coin,
ctx.serialize(&controller_puzzle)?,
ctx.serialize(&controller_solution)?,
);
ctx.insert(controller_coin_spend);

sim.spend_coins(ctx.take(), &[])?;

Ok(())
}
}
Loading