|
| 1 | +use std::rc::Rc; |
| 2 | + |
| 3 | +use clvm_traits::ToClvm; |
| 4 | + |
| 5 | +use log::debug; |
| 6 | + |
| 7 | +use rand::prelude::*; |
| 8 | + |
| 9 | +use crate::channel_handler::types::{prepend_rem_conditions, ChannelHandlerEnv, UnrollCoin}; |
| 10 | +use crate::common::constants::CREATE_COIN; |
| 11 | +use crate::common::standard_coin::standard_solution_partial; |
| 12 | +use crate::common::types::{ |
| 13 | + Aggsig, Amount, BrokenOutCoinSpendInfo, CoinID, CoinString, Error, GameID, IntoErr, Node, |
| 14 | + PrivateKey, Program, PublicKey, Sha256tree, Spend, |
| 15 | +}; |
| 16 | + |
| 17 | +/// Describes all aspects of the channel coin spend. |
| 18 | +/// Allows the user to get the solution, conditions, quoted condition program |
| 19 | +/// and signature for the channel coin spend. |
| 20 | +pub struct ChannelCoin { |
| 21 | + state_channel_coin: CoinString, |
| 22 | +} |
| 23 | + |
| 24 | +impl ChannelCoin { |
| 25 | + pub fn new(state_channel_coin: CoinString) -> Self { |
| 26 | + ChannelCoin { state_channel_coin } |
| 27 | + } |
| 28 | + |
| 29 | + pub fn coin_string(&self) -> &CoinString { |
| 30 | + &self.state_channel_coin |
| 31 | + } |
| 32 | + pub fn to_coin_id(&self) -> CoinID { |
| 33 | + self.state_channel_coin.to_coin_id() |
| 34 | + } |
| 35 | + |
| 36 | + pub fn get_solution_and_signature_from_conditions<R: Rng>( |
| 37 | + &self, |
| 38 | + env: &mut ChannelHandlerEnv<R>, |
| 39 | + private_key: &PrivateKey, |
| 40 | + aggregate_public_key: &PublicKey, |
| 41 | + conditions: Rc<Program>, |
| 42 | + ) -> Result<BrokenOutCoinSpendInfo, Error> { |
| 43 | + debug!("STATE CONDITONS: {conditions:?}"); |
| 44 | + let conditions_nodeptr = conditions.to_nodeptr(env.allocator)?; |
| 45 | + let spend = standard_solution_partial( |
| 46 | + env.allocator, |
| 47 | + private_key, |
| 48 | + &self.state_channel_coin.to_coin_id(), |
| 49 | + conditions_nodeptr, |
| 50 | + aggregate_public_key, |
| 51 | + &env.agg_sig_me_additional_data, |
| 52 | + true, |
| 53 | + )?; |
| 54 | + Ok(spend) |
| 55 | + } |
| 56 | + |
| 57 | + pub fn get_solution_and_signature<R: Rng>( |
| 58 | + &self, |
| 59 | + env: &mut ChannelHandlerEnv<R>, |
| 60 | + private_key: &PrivateKey, |
| 61 | + aggregate_channel_public_key: &PublicKey, |
| 62 | + aggregate_unroll_public_key: &PublicKey, |
| 63 | + amount: &Amount, |
| 64 | + unroll_coin: &UnrollCoin, |
| 65 | + ) -> Result<BrokenOutCoinSpendInfo, Error> { |
| 66 | + debug!( |
| 67 | + "making solution for channel coin with unroll state {}", |
| 68 | + unroll_coin.state_number |
| 69 | + ); |
| 70 | + let unroll_puzzle = |
| 71 | + unroll_coin.make_curried_unroll_puzzle(env, aggregate_unroll_public_key)?; |
| 72 | + let unroll_puzzle_hash = Node(unroll_puzzle).sha256tree(env.allocator); |
| 73 | + let create_conditions = vec![Node( |
| 74 | + ( |
| 75 | + CREATE_COIN, |
| 76 | + (unroll_puzzle_hash.clone(), (amount.clone(), ())), |
| 77 | + ) |
| 78 | + .to_clvm(env.allocator) |
| 79 | + .into_gen()?, |
| 80 | + )]; |
| 81 | + let create_conditions_obj = create_conditions.to_clvm(env.allocator).into_gen()?; |
| 82 | + let create_conditions_with_rem = |
| 83 | + prepend_rem_conditions(env, unroll_coin.state_number, create_conditions_obj)?; |
| 84 | + let ccrem_program = Program::from_nodeptr(env.allocator, create_conditions_with_rem)?; |
| 85 | + self.get_solution_and_signature_from_conditions( |
| 86 | + env, |
| 87 | + private_key, |
| 88 | + aggregate_channel_public_key, |
| 89 | + Rc::new(ccrem_program), |
| 90 | + ) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[derive(Clone, Debug)] |
| 95 | +pub struct ChannelCoinSpentResult { |
| 96 | + pub transaction: Spend, |
| 97 | + pub timeout: bool, |
| 98 | + pub games_canceled: Vec<GameID>, |
| 99 | +} |
| 100 | + |
| 101 | +#[derive(Clone, Debug)] |
| 102 | +pub struct ChannelCoinSpendInfo { |
| 103 | + pub solution: Rc<Program>, |
| 104 | + pub conditions: Rc<Program>, |
| 105 | + pub aggsig: Aggsig, |
| 106 | +} |
| 107 | + |
| 108 | +pub struct ChannelCoinInfo { |
| 109 | + pub coin: ChannelCoin, |
| 110 | + pub amount: Amount, |
| 111 | + // Used in unrolling. |
| 112 | + pub spend: Spend, |
| 113 | +} |
0 commit comments