Skip to content

Commit

Permalink
Merge pull request #140 from xch-dev/vaults
Browse files Browse the repository at this point in the history
Initial vault scaffolding
  • Loading branch information
Rigidity authored Dec 14, 2024
2 parents 75d6779 + 06223e5 commit 6e499a1
Show file tree
Hide file tree
Showing 64 changed files with 1,790 additions and 246 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ workspace = true

[features]
chip-0035 = ["chia-sdk-driver/chip-0035", "chia-sdk-types/chip-0035"]
vault = ["chia-sdk-driver/vault", "chia-sdk-types/vault"]
offers = ["chia-sdk-driver/offers"]
native-tls = ["chia-sdk-client/native-tls"]
rustls = ["chia-sdk-client/rustls"]
Expand Down Expand Up @@ -135,6 +136,7 @@ napi = { version = "2.12.2", default-features = false }
paste = "1.0.15"
bigdecimal = "0.4.6"
k256 = "0.13.4"
p256 = "0.13.2"

[profile.release]
lto = true
Expand Down
1 change: 1 addition & 0 deletions crates/chia-sdk-driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ workspace = true

[features]
chip-0035 = ["chia-sdk-types/chip-0035"]
vault = ["chia-sdk-types/vault"]
offers = [
"dep:bech32",
"dep:chia-traits",
Expand Down
9 changes: 9 additions & 0 deletions crates/chia-sdk-driver/src/driver_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ pub enum DriverError {

#[error("invalid merkle proof")]
InvalidMerkleProof,

#[error("unknown puzzle")]
UnknownPuzzle,

#[error("wrong number of spends")]
WrongSpendCount,

#[error("missing member spend")]
MissingMemberSpend,
}
2 changes: 1 addition & 1 deletion crates/chia-sdk-driver/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ where
}

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

fn construct_solution(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Layer for OracleLayer {
}

let conditions: Vec<Condition<NodePtr>> = vec![
Condition::create_coin(self.oracle_puzzle_hash, self.oracle_fee, vec![]),
Condition::create_coin(self.oracle_puzzle_hash, self.oracle_fee, None),
Condition::create_puzzle_announcement(Bytes::new("$".into())),
];

Expand Down
6 changes: 4 additions & 2 deletions crates/chia-sdk-driver/src/layers/p2_singleton_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,22 @@ mod tests {
let p2_singleton = P2SingletonLayer::new(launcher_id);
let p2_singleton_hash = p2_singleton.tree_hash().into();

let memos = ctx.hint(launcher_id)?;
p2.spend(
ctx,
coin,
create_singleton.create_coin(p2_singleton_hash, 1, vec![launcher_id.into()]),
create_singleton.create_coin(p2_singleton_hash, 1, Some(memos)),
)?;

let p2_coin = Coin::new(coin.coin_id(), p2_singleton_hash, 1);
p2_singleton.spend_coin(ctx, p2_coin, puzzle_hash)?;

let memos = ctx.hint(launcher_id)?;
let inner_solution = p2
.spend_with_conditions(
ctx,
Conditions::new()
.create_coin(puzzle_hash, 1, vec![launcher_id.into()])
.create_coin(puzzle_hash, 1, Some(memos))
.create_puzzle_announcement(p2_coin.coin_id().into()),
)?
.solution;
Expand Down
4 changes: 2 additions & 2 deletions crates/chia-sdk-driver/src/layers/standard_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ mod tests {
p2.spend(
ctx,
coin,
Conditions::new().create_coin(puzzle_hash, u64::MAX, Vec::new()),
Conditions::new().create_coin(puzzle_hash, u64::MAX, None),
)?;

p2.spend(
ctx,
Coin::new(coin.coin_id(), puzzle_hash, u64::MAX),
Conditions::new().create_coin(puzzle_hash, 1, Vec::new()),
Conditions::new().create_coin(puzzle_hash, 1, None),
)?;

sim.spend_coins(ctx.take(), &[sk])?;
Expand Down
6 changes: 6 additions & 0 deletions crates/chia-sdk-driver/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ mod datalayer;

#[cfg(feature = "chip-0035")]
pub use datalayer::*;

#[cfg(feature = "vault")]
mod vault;

#[cfg(feature = "vault")]
pub use vault::*;
34 changes: 20 additions & 14 deletions crates/chia-sdk-driver/src/primitives/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Cat {
)?;

Ok((
Conditions::new().create_coin(puzzle_hash, amount, Vec::new()),
Conditions::new().create_coin(puzzle_hash, amount, None),
eve,
))
}
Expand All @@ -134,7 +134,7 @@ impl Cat {

let create_coins = conditions
.into_iter()
.filter_map(|ptr| ctx.extract::<CreateCoin>(ptr).ok());
.filter_map(|ptr| ctx.extract::<CreateCoin<NodePtr>>(ptr).ok());

let delta = create_coins.fold(
i128::from(cat.coin.amount) - i128::from(*extra_delta),
Expand Down Expand Up @@ -283,11 +283,12 @@ mod tests {
let (sk, pk, puzzle_hash, coin) = sim.new_p2(1)?;
let p2 = StandardLayer::new(pk);

let memos = ctx.hint(puzzle_hash)?;
let (issue_cat, cat) = Cat::single_issuance_eve(
ctx,
coin.coin_id(),
1,
Conditions::new().create_coin(puzzle_hash, 1, vec![puzzle_hash.into()]),
Conditions::new().create_coin(puzzle_hash, 1, Some(memos)),
)?;
p2.spend(ctx, coin, issue_cat)?;

Expand All @@ -311,12 +312,13 @@ mod tests {
let (sk, pk, puzzle_hash, coin) = sim.new_p2(1)?;
let p2 = StandardLayer::new(pk);

let memos = ctx.hint(puzzle_hash)?;
let (issue_cat, cat) = Cat::multi_issuance_eve(
ctx,
coin.coin_id(),
pk,
1,
Conditions::new().create_coin(puzzle_hash, 1, vec![puzzle_hash.into()]),
Conditions::new().create_coin(puzzle_hash, 1, Some(memos)),
)?;
p2.spend(ctx, coin, issue_cat)?;
sim.spend_coins(ctx.take(), &[sk])?;
Expand Down Expand Up @@ -358,11 +360,12 @@ mod tests {
let (sk, pk, puzzle_hash, coin) = sim.new_p2(2)?;
let p2 = StandardLayer::new(pk);

let memos = ctx.hint(puzzle_hash)?;
let (issue_cat, _cat) = Cat::single_issuance_eve(
ctx,
coin.coin_id(),
1,
Conditions::new().create_coin(puzzle_hash, 2, vec![puzzle_hash.into()]),
Conditions::new().create_coin(puzzle_hash, 2, Some(memos)),
)?;
p2.spend(ctx, coin, issue_cat)?;

Expand Down Expand Up @@ -398,8 +401,9 @@ mod tests {
// Issue the CAT coins with those amounts.
let mut conditions = Conditions::new();

let memos = ctx.hint(puzzle_hash)?;
for &amount in &amounts {
conditions = conditions.create_coin(puzzle_hash, amount, vec![puzzle_hash.into()]);
conditions = conditions.create_coin(puzzle_hash, amount, Some(memos));
}

let (issue_cat, cat) = Cat::single_issuance_eve(ctx, coin.coin_id(), sum, conditions)?;
Expand All @@ -424,7 +428,7 @@ mod tests {
Conditions::new().create_coin(
puzzle_hash,
cat.coin.amount,
vec![puzzle_hash.into()],
Some(memos),
),
)?,
))
Expand Down Expand Up @@ -455,13 +459,15 @@ mod tests {
let custom_p2 = ctx.alloc(&1)?;
let custom_p2_puzzle_hash = ctx.tree_hash(custom_p2).into();

let memos = ctx.hint(puzzle_hash)?;
let custom_memos = ctx.hint(custom_p2_puzzle_hash)?;
let (issue_cat, cat) = Cat::single_issuance_eve(
ctx,
coin.coin_id(),
2,
Conditions::new()
.create_coin(puzzle_hash, 1, vec![puzzle_hash.into()])
.create_coin(custom_p2_puzzle_hash, 1, vec![custom_p2_puzzle_hash.into()]),
.create_coin(puzzle_hash, 1, Some(memos))
.create_coin(custom_p2_puzzle_hash, 1, Some(custom_memos)),
)?;
p2.spend(ctx, coin, issue_cat)?;
sim.spend_coins(ctx.take(), &[sk.clone()])?;
Expand All @@ -471,7 +477,7 @@ mod tests {
cat.wrapped_child(puzzle_hash, 1),
p2.spend_with_conditions(
ctx,
Conditions::new().create_coin(puzzle_hash, 1, vec![puzzle_hash.into()]),
Conditions::new().create_coin(puzzle_hash, 1, Some(memos)),
)?,
),
CatSpend::new(
Expand All @@ -481,7 +487,7 @@ mod tests {
ctx.alloc(&[CreateCoin::new(
custom_p2_puzzle_hash,
1,
vec![custom_p2_puzzle_hash.into()],
Some(custom_memos),
)])?,
),
),
Expand All @@ -500,8 +506,8 @@ mod tests {
let (sk, pk, puzzle_hash, coin) = sim.new_p2(10000)?;
let p2 = StandardLayer::new(pk);

let conditions =
Conditions::new().create_coin(puzzle_hash, 10000, vec![puzzle_hash.into()]);
let memos = ctx.hint(puzzle_hash)?;
let conditions = Conditions::new().create_coin(puzzle_hash, 10000, Some(memos));
let (issue_cat, cat) = Cat::multi_issuance_eve(ctx, coin.coin_id(), pk, 10000, conditions)?;
p2.spend(ctx, coin, issue_cat)?;

Expand All @@ -512,7 +518,7 @@ mod tests {
p2.spend_with_conditions(
ctx,
Conditions::new()
.create_coin(puzzle_hash, 7000, vec![puzzle_hash.into()])
.create_coin(puzzle_hash, 7000, Some(memos))
.run_cat_tail(tail, NodePtr::NIL),
)?,
-3000,
Expand Down
4 changes: 2 additions & 2 deletions crates/chia-sdk-driver/src/primitives/clawback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mod tests {
alice.spend(
ctx,
alice_coin,
Conditions::new().create_coin(clawback_puzzle_hash, 1, Vec::new()),
Conditions::new().create_coin(clawback_puzzle_hash, 1, None),
)?;
let clawback_coin = Coin::new(alice_coin.coin_id(), clawback_puzzle_hash, 1);

Expand Down Expand Up @@ -166,7 +166,7 @@ mod tests {
p2.spend(
ctx,
coin,
Conditions::new().create_coin(clawback_puzzle_hash, 1, Vec::new()),
Conditions::new().create_coin(clawback_puzzle_hash, 1, None),
)?;
let clawback_coin = Coin::new(coin.coin_id(), clawback_puzzle_hash, 1);

Expand Down
34 changes: 20 additions & 14 deletions crates/chia-sdk-driver/src/primitives/datalayer/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,21 @@ where

// if the coin was re-created with memos, there is a delegation layer
// and delegated puzzles have been updated (we can rebuild the list from memos)
if inner_create_coin_condition.memos.len() > 1 {

let inner_memos = Vec::<Bytes>::from_clvm(
allocator,
inner_create_coin_condition.memos.unwrap_or_default().value,
)?;

if inner_memos.len() > 1 {
// keep in mind that there's always the launcher id memo being added
return Ok(Some(Self::build_datastore(
new_coin,
singleton_layer.launcher_id,
Proof::Lineage(singleton_layer.lineage_proof(cs.coin)),
new_metadata,
state_layer.inner_puzzle.tree_hash().into(),
inner_create_coin_condition.memos,
inner_memos,
)?));
}

Expand Down Expand Up @@ -545,15 +551,15 @@ impl<M> DataStore<M> {
Ok(Condition::CreateCoin(CreateCoin {
amount: 1,
puzzle_hash: new_puzzle_hash,
memos: if hint_delegated_puzzles {
memos: Some(ctx.memos(&if hint_delegated_puzzles {
Self::get_recreation_memos(
launcher_id,
new_inner_puzzle_hash.into(),
new_delegated_puzzles,
)
} else {
vec![launcher_id.into()]
},
})?),
}))
}

Expand Down Expand Up @@ -590,7 +596,7 @@ pub mod tests {
use chia_bls::{PublicKey, SecretKey};
use chia_puzzles::standard::StandardArgs;
use chia_sdk_test::{test_secret_keys, Simulator};
use chia_sdk_types::{Conditions, MeltSingleton, UpdateDataStoreMerkleRoot};
use chia_sdk_types::{Conditions, MeltSingleton, Memos, UpdateDataStoreMerkleRoot};
use clvmr::sha2::Sha256;
use rstest::rstest;

Expand Down Expand Up @@ -709,7 +715,7 @@ pub mod tests {
}

let datastore_inner_spend = StandardLayer::new(pk)
.spend_with_conditions(ctx, Conditions::new().create_coin(puzzle_hash, 1, vec![]))?;
.spend_with_conditions(ctx, Conditions::new().create_coin(puzzle_hash, 1, None))?;

let old_datastore_coin = datastore.coin;
let new_spend = datastore.spend(ctx, datastore_inner_spend)?;
Expand Down Expand Up @@ -1757,7 +1763,7 @@ pub mod tests {
Conditions::new().with(Condition::CreateCoin(CreateCoin {
puzzle_hash: attacker_puzzle_hash.into(),
amount: 1,
memos: vec![],
memos: None,
})),
)?;

Expand Down Expand Up @@ -1907,10 +1913,10 @@ pub mod tests {
new_updater_puzzle_hash: new_updater_ph.into(),
},
conditions: if output_conditions {
vec![CreateCoin {
vec![CreateCoin::<NodePtr> {
puzzle_hash: [0; 32].into(),
amount: 1,
memos: vec![],
memos: None,
}]
} else {
vec![]
Expand Down Expand Up @@ -2100,11 +2106,11 @@ pub mod tests {
inner_spend_conditions = inner_spend_conditions.with(Condition::CreateCoin(CreateCoin {
puzzle_hash: new_inner_ph,
amount: 1,
memos: vec![
launcher_coin.coin_id().into(),
second_root_hash.value().into(),
new_inner_ph.into(),
],
memos: Memos::some(ctx.alloc(&[
launcher_coin.coin_id(),
second_root_hash.value(),
new_inner_ph,
])?),
}));

let inner_spend =
Expand Down
Loading

0 comments on commit 6e499a1

Please sign in to comment.