Skip to content

Commit

Permalink
Merge pull request #14 from Rigidity/p2-fusion
Browse files Browse the repository at this point in the history
Write p2 fusion puzzle in Rue (example purposes only)
  • Loading branch information
Rigidity authored Jun 26, 2024
2 parents 75e3576 + 920c4da commit 94d32ec
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
64 changes: 64 additions & 0 deletions examples/p2_fusion.rue
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// This puzzle has not been audited or tested, and is for example purposes only.

// Also known as a "singleton struct".
struct SingletonInfo {
mod_hash: Bytes32,
launcher_id: Bytes32,
...launcher_puzzle_hash: Bytes32,
}

// Calculate the full puzzle hash for a singleton.
inline fun singleton_puzzle_hash(singleton_info: SingletonInfo, inner_puzzle_hash: Bytes32) -> Bytes32 {
curry_tree_hash(singleton_info.mod_hash, tree_hash(singleton_info), inner_puzzle_hash)
}

fun main(
fusion_singleton: SingletonInfo,
fusion_inner_puzzle_hash: Bytes32,
fusion_coin_id: Bytes32,
my_launcher_id: Bytes32,
my_inner_puzzle_hash: Bytes32,
my_amount: Int,
p2_puzzle_hash: Bytes32,
) -> Condition[] {
// The NFT singleton has the same mod hash and launcher puzzle hash as the fusion singleton.
let nft_singleton = SingletonInfo {
mod_hash: fusion_singleton.mod_hash,
launcher_id: my_launcher_id,
launcher_puzzle_hash: fusion_singleton.launcher_puzzle_hash,
};

// Calculate the full puzzle hashes for the NFT and fusion singletons.
let fusion_puzzle_hash = singleton_puzzle_hash(fusion_singleton, fusion_inner_puzzle_hash);
let nft_puzzle_hash = singleton_puzzle_hash(nft_singleton, my_inner_puzzle_hash);

// Calculate the announcement message.
let announcement_message = sha256(fusion_coin_id + my_launcher_id + p2_puzzle_hash);

[
// Make sure that the amount in the solution is correct.
Condition::AssertMyAmount { amount: my_amount },

// Prove supplied NFT coin matches the expected derived puzzle hash.
Condition::AssertMyPuzzleHash { puzzle_hash: nft_puzzle_hash },


// Assert that the fusion singleton announced this NFT spend.
Condition::AssertPuzzleAnnouncement {
announcement_id: sha256(fusion_puzzle_hash + announcement_message),
},

// Unlock the NFT to the new p2 puzzle hash, with a hint.
Condition::CreateCoin {
puzzle_hash: p2_puzzle_hash,
amount: my_amount,
memos: [p2_puzzle_hash],
},

// Announce that a specific singleton is being spent to
// help prevent ephemeral singleton spends from influencing.
Condition::CreateCoinAnnouncement {
message: announcement_message,
},
]
}
7 changes: 7 additions & 0 deletions tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,10 @@ cost = 1332
input = "()"
output = "5"
hash = "32ed6e4964102d7093ef350019dfd14c99a3ea9feac1f3502194384b8976f7c1"

[p2_fusion]
bytes = 743
cost = 87452
input = "((0x4696e7a2b7682e2df01ab47e6e002d0dca895f99c6172e4a55a3e033499532b7 0x32ed6e4964102d7093ef350019dfd14c99a3ea9feac1f3502194384b8976f7c1 0x770bb2c0b2582924adf403e62374f8424a2ed510eef70b5f450eccab238a4911) 0x8d496dc7cdbc417db2132eda6894b29a91511f176e93a1ea943d210cd27822b2 0x3878fc2ed6b703c7c3d00f9f5d8f170ec252ca439be6e5b5b516e1dce1adc0d7 0x3fe4d62a7db919b25377cb207f16fa0fb6519e123366eaa23014dd5d7c967ca2 0x837525fb91b88dbf02d9ef5a0b322f6943f93424b6e8fe6d26dd1be6d3311871 1 0xc19110971a0cea01368f0c7599b8984f74e301b7cda20bd7f86eb2296bbf16c5)"
output = "((73 1) (72 0x6155f55414a5cd9193bef33744095a78d57852cf24241b25edeffad6e544c499) (63 0x9d6824bfdfb4c726334a210b657a5e4126c3fbb378598bb3a5b7a210bb75cdb8) (g1_negate 0xc19110971a0cea01368f0c7599b8984f74e301b7cda20bd7f86eb2296bbf16c5 1 (0xc19110971a0cea01368f0c7599b8984f74e301b7cda20bd7f86eb2296bbf16c5)) (60 0x81fdd3fbc407906bc0875522e7a2e77409ee5ef17d3eaa611b505b344588f6b6))"
hash = "4696e7a2b7682e2df01ab47e6e002d0dca895f99c6172e4a55a3e033499532b7"

0 comments on commit 94d32ec

Please sign in to comment.