Skip to content

Commit

Permalink
Merge pull request #50 from Rigidity/updates
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
Rigidity authored Jun 23, 2024
2 parents d817e16 + d9540f2 commit a243b6d
Show file tree
Hide file tree
Showing 31 changed files with 565 additions and 331 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ hex = { workspace = true }
bech32 = { workspace = true }
rand = { workspace = true }
rand_chacha = { workspace = true }
hex-literal = { workspace = true }
indexmap = { workspace = true }
chia-sdk-client = { workspace = true }
chia-sdk-driver = { workspace = true }
Expand All @@ -70,6 +69,16 @@ chia-sdk-signer = { workspace = true }
chia-sdk-test = { workspace = true }
chia-sdk-types = { workspace = true }

[dev-dependencies]
anyhow = { workspace = true }
hex-literal = { workspace = true }
chia-puzzles = { workspace = true }
chia-bls = { workspace = true }
clvm-utils = { workspace = true }
clvm-traits = { workspace = true, features = ["derive"] }
clvmr = { workspace = true }
tokio = { workspace = true, features = ["full"] }

[workspace.dependencies]
chia-sdk-client = { version = "0.10.3", path = "./crates/chia-sdk-client" }
chia-sdk-driver = { version = "0.10.0", path = "./crates/chia-sdk-driver" }
Expand Down
9 changes: 3 additions & 6 deletions crates/chia-sdk-driver/src/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use clvmr::{

use crate::{Spend, SpendContext, SpendError};

#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[must_use]
pub struct Conditions {
conditions: Vec<Condition>,
Expand Down Expand Up @@ -151,7 +151,7 @@ impl Conditions {

pub fn p2_spend(
self,
ctx: &mut SpendContext<'_>,
ctx: &mut SpendContext,
synthetic_key: PublicKey,
) -> Result<Spend, SpendError> {
let standard_puzzle = ctx.standard_puzzle()?;
Expand Down Expand Up @@ -194,24 +194,21 @@ impl ToClvm<NodePtr> for Conditions {
#[cfg(test)]
mod tests {
use chia_sdk_test::{secret_key, test_transaction, Simulator};
use clvmr::Allocator;

use super::*;

#[tokio::test]
async fn test_standard_spend() -> anyhow::Result<()> {
let sim = Simulator::new().await?;
let peer = sim.connect().await?;
let ctx = &mut SpendContext::new();

let sk = secret_key()?;
let pk = sk.public_key();

let puzzle_hash = StandardArgs::curry_tree_hash(pk).into();
let coin = sim.mint_coin(puzzle_hash, 1).await;

let mut allocator = Allocator::new();
let ctx = &mut SpendContext::new(&mut allocator);

ctx.spend_p2_coin(coin, pk, Conditions::new().create_coin(puzzle_hash, 1))?;

test_transaction(
Expand Down
15 changes: 4 additions & 11 deletions crates/chia-sdk-driver/src/puzzles/cat/cat_spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl CatSpend {
self
}

pub fn finish(self, ctx: &mut SpendContext<'_>) -> Result<(), SpendError> {
pub fn finish(self, ctx: &mut SpendContext) -> Result<(), SpendError> {
let cat_puzzle_ptr = ctx.cat_puzzle()?;
let len = self.cat_spends.len();

Expand Down Expand Up @@ -117,7 +117,6 @@ mod tests {
use chia_puzzles::{cat::EverythingWithSignatureTailArgs, standard::StandardArgs};
use chia_sdk_test::{secret_key, test_transaction, Simulator};
use chia_sdk_types::conditions::{Condition, RunTail};
use clvmr::Allocator;

use crate::{issue_cat_from_coin, issue_cat_from_key, Conditions};

Expand All @@ -127,16 +126,14 @@ mod tests {
async fn test_cat_spend_multi() -> anyhow::Result<()> {
let sim = Simulator::new().await?;
let peer = sim.connect().await?;
let ctx = &mut SpendContext::new();

let sk = secret_key()?;
let pk = sk.public_key();

let puzzle_hash = StandardArgs::curry_tree_hash(pk).into();
let coin = sim.mint_coin(puzzle_hash, 6).await;

let mut allocator = Allocator::new();
let ctx = &mut SpendContext::new(&mut allocator);

let (issue_cat, issuance) = issue_cat_from_coin(
ctx,
coin.coin_id(),
Expand Down Expand Up @@ -194,16 +191,14 @@ mod tests {
async fn test_cat_spend() -> anyhow::Result<()> {
let sim = Simulator::new().await?;
let peer = sim.connect().await?;
let ctx = &mut SpendContext::new();

let sk = secret_key()?;
let pk = sk.public_key();

let puzzle_hash = StandardArgs::curry_tree_hash(pk).into();
let coin = sim.mint_coin(puzzle_hash, 1).await;

let mut allocator = Allocator::new();
let ctx = &mut SpendContext::new(&mut allocator);

let conditions = Conditions::new().create_hinted_coin(puzzle_hash, 1, puzzle_hash);
let (issue_cat, issuance) = issue_cat_from_coin(ctx, coin.coin_id(), 1, conditions)?;

Expand Down Expand Up @@ -239,16 +234,14 @@ mod tests {
async fn test_cat_melt() -> anyhow::Result<()> {
let sim = Simulator::new().await?;
let peer = sim.connect().await?;
let ctx = &mut SpendContext::new();

let sk = secret_key()?;
let pk = sk.public_key();

let puzzle_hash = StandardArgs::curry_tree_hash(pk).into();
let coin = sim.mint_coin(puzzle_hash, 10000).await;

let mut allocator = Allocator::new();
let ctx = &mut SpendContext::new(&mut allocator);

let conditions = Conditions::new().create_hinted_coin(puzzle_hash, 10000, puzzle_hash);
let (issue_cat, issuance) = issue_cat_from_key(ctx, coin.coin_id(), pk, 10000, conditions)?;

Expand Down
15 changes: 5 additions & 10 deletions crates/chia-sdk-driver/src/puzzles/cat/issue_cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct IssueCat {
}

pub fn issue_cat_from_coin(
ctx: &mut SpendContext<'_>,
ctx: &mut SpendContext,
parent_coin_id: Bytes32,
amount: u64,
extra_conditions: Conditions,
Expand All @@ -45,7 +45,7 @@ pub fn issue_cat_from_coin(
}

pub fn issue_cat_from_key(
ctx: &mut SpendContext<'_>,
ctx: &mut SpendContext,
parent_coin_id: Bytes32,
public_key: PublicKey,
amount: u64,
Expand All @@ -69,7 +69,7 @@ pub fn issue_cat_from_key(
}

pub fn issue_cat<P, S>(
ctx: &mut SpendContext<'_>,
ctx: &mut SpendContext,
parent_coin_id: Bytes32,
asset_id: Bytes32,
amount: u64,
Expand Down Expand Up @@ -133,24 +133,21 @@ where
mod tests {
use chia_puzzles::standard::StandardArgs;
use chia_sdk_test::{secret_key, test_transaction, Simulator};
use clvmr::Allocator;

use super::*;

#[tokio::test]
async fn test_single_issuance_cat() -> anyhow::Result<()> {
let sim = Simulator::new().await?;
let peer = sim.connect().await?;
let ctx = &mut SpendContext::new();

let sk = secret_key()?;
let pk = sk.public_key();

let puzzle_hash = StandardArgs::curry_tree_hash(pk).into();
let coin = sim.mint_coin(puzzle_hash, 1).await;

let mut allocator = Allocator::new();
let ctx = &mut SpendContext::new(&mut allocator);

let conditions = Conditions::new().create_hinted_coin(puzzle_hash, 1, puzzle_hash);
let (issue_cat, _cat_info) = issue_cat_from_coin(ctx, coin.coin_id(), 1, conditions)?;

Expand All @@ -171,16 +168,14 @@ mod tests {
async fn test_multi_issuance_cat() -> anyhow::Result<()> {
let sim = Simulator::new().await?;
let peer = sim.connect().await?;
let ctx = &mut SpendContext::new();

let sk = secret_key()?;
let pk = sk.public_key();

let puzzle_hash = StandardArgs::curry_tree_hash(pk).into();
let coin = sim.mint_coin(puzzle_hash, 1).await;

let mut allocator = Allocator::new();
let ctx = &mut SpendContext::new(&mut allocator);

let conditions = Conditions::new().create_hinted_coin(puzzle_hash, 1, puzzle_hash);
let (issue_cat, _cat_info) = issue_cat_from_key(ctx, coin.coin_id(), pk, 1, conditions)?;

Expand Down
7 changes: 2 additions & 5 deletions crates/chia-sdk-driver/src/puzzles/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,21 @@ mod tests {

use chia_puzzles::standard::StandardArgs;
use chia_sdk_test::{secret_key, test_transaction, Simulator};
use clvmr::Allocator;

#[tokio::test]
async fn test_create_did() -> anyhow::Result<()> {
let sim = Simulator::new().await?;
let peer = sim.connect().await?;
let ctx = &mut SpendContext::new();

let sk = secret_key()?;
let pk = sk.public_key();

let puzzle_hash = StandardArgs::curry_tree_hash(pk).into();
let coin = sim.mint_coin(puzzle_hash, 1).await;

let mut allocator = Allocator::new();
let ctx = &mut SpendContext::new(&mut allocator);

let (launch_singleton, did_info) =
Launcher::new(coin.coin_id(), 1).create_standard_did(ctx, pk)?;
Launcher::new(coin.coin_id(), 1).create_simple_did(ctx, pk)?;

ctx.spend_p2_coin(coin, pk, launch_singleton)?;

Expand Down
23 changes: 12 additions & 11 deletions crates/chia-sdk-driver/src/puzzles/did/create_did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{Conditions, Launcher, SpendContext, SpendError};
impl Launcher {
pub fn create_eve_did<M>(
self,
ctx: &mut SpendContext<'_>,
ctx: &mut SpendContext,
p2_puzzle_hash: Bytes32,
recovery_did_list_hash: Bytes32,
num_verifications_required: u64,
Expand All @@ -28,7 +28,7 @@ impl Launcher {
let metadata_ptr = ctx.alloc(&metadata)?;
let metadata_hash = ctx.tree_hash(metadata_ptr);

let did_inner_puzzle_hash = CurriedProgram {
let inner_puzzle_hash = CurriedProgram {
program: DID_INNER_PUZZLE_HASH,
args: DidArgs {
inner_puzzle: TreeHash::from(p2_puzzle_hash),
Expand All @@ -42,7 +42,7 @@ impl Launcher {
.into();

let launcher_coin = self.coin();
let (chained_spend, eve_coin) = self.spend(ctx, did_inner_puzzle_hash, ())?;
let (launch_singleton, eve_coin) = self.spend(ctx, inner_puzzle_hash, ())?;

let proof = Proof::Eve(EveProof {
parent_coin_info: launcher_coin.parent_coin_info,
Expand All @@ -52,20 +52,21 @@ impl Launcher {
let did_info = DidInfo {
launcher_id: launcher_coin.coin_id(),
coin: eve_coin,
did_inner_puzzle_hash,
inner_puzzle_hash,
p2_puzzle_hash,
proof,
recovery_did_list_hash,
num_verifications_required,
metadata,
metadata_hash,
};

Ok((chained_spend, did_info))
Ok((launch_singleton, did_info))
}

pub fn create_custom_standard_did<M>(
pub fn create_did<M>(
self,
ctx: &mut SpendContext<'_>,
ctx: &mut SpendContext,
recovery_did_list_hash: Bytes32,
num_verifications_required: u64,
metadata: M,
Expand All @@ -85,19 +86,19 @@ impl Launcher {
metadata,
)?;

let did_info = ctx.spend_standard_did(&did_info, synthetic_key, Conditions::new())?;
let did_info = ctx.spend_standard_did(did_info, synthetic_key, Conditions::new())?;

Ok((create_did, did_info))
}

pub fn create_standard_did(
pub fn create_simple_did(
self,
ctx: &mut SpendContext<'_>,
ctx: &mut SpendContext,
synthetic_key: PublicKey,
) -> Result<(Conditions, DidInfo<()>), SpendError>
where
Self: Sized,
{
self.create_custom_standard_did(ctx, tree_hash_atom(&[]).into(), 1, (), synthetic_key)
self.create_did(ctx, tree_hash_atom(&[]).into(), 1, (), synthetic_key)
}
}
Loading

0 comments on commit a243b6d

Please sign in to comment.