diff --git a/napi/__test__/index.spec.ts b/napi/__test__/index.spec.ts index d84a6978..9a6a629c 100644 --- a/napi/__test__/index.spec.ts +++ b/napi/__test__/index.spec.ts @@ -4,6 +4,7 @@ import { ClvmAllocator, ClvmPtr, compareBytes, + curryTreeHash, fromHex, toCoinId, toHex, @@ -175,3 +176,18 @@ test("clvm serialization", (t) => { t.is(hex as string, toHex(serialized)); } }); + +test('curry tree hash', (t) => { + const clvm = new ClvmAllocator(); + + const items = Array.from({ length: 10 }, (_, i) => i); + const ptr = clvm.curry( + ClvmPtr.nil(), + items.map((i) => clvm.newSmallNumber(i)) + ); + + const treeHash = curryTreeHash(clvm.treeHash(ClvmPtr.nil()), items.map((i) => clvm.treeHash(clvm.newSmallNumber(i)))); + const expected = clvm.treeHash(ptr); + + t.true(compareBytes(treeHash, expected)); +}) diff --git a/napi/index.d.ts b/napi/index.d.ts index 94fcf391..1dd5d0a9 100644 --- a/napi/index.d.ts +++ b/napi/index.d.ts @@ -15,6 +15,7 @@ export interface Curry { program: ClvmPtr args: Array } +export declare function curryTreeHash(treeHash: Uint8Array, args: Array): Uint8Array export interface Coin { parentCoinInfo: Uint8Array puzzleHash: Uint8Array diff --git a/napi/index.js b/napi/index.js index b858d3fe..f0602f11 100644 --- a/napi/index.js +++ b/napi/index.js @@ -310,10 +310,11 @@ if (!nativeBinding) { throw new Error(`Failed to load native binding`) } -const { ClvmAllocator, ClvmPtr, toCoinId, parseNftInfo, parseUnspentNft, spendNft, mintNfts, spendP2Standard, spendP2Singleton, compareBytes, sha256, fromHexRaw, fromHex, toHex } = nativeBinding +const { ClvmAllocator, ClvmPtr, curryTreeHash, toCoinId, parseNftInfo, parseUnspentNft, spendNft, mintNfts, spendP2Standard, spendP2Singleton, compareBytes, sha256, fromHexRaw, fromHex, toHex } = nativeBinding module.exports.ClvmAllocator = ClvmAllocator module.exports.ClvmPtr = ClvmPtr +module.exports.curryTreeHash = curryTreeHash module.exports.toCoinId = toCoinId module.exports.parseNftInfo = parseNftInfo module.exports.parseUnspentNft = parseUnspentNft diff --git a/napi/src/clvm.rs b/napi/src/clvm.rs index cd007122..b18096a4 100644 --- a/napi/src/clvm.rs +++ b/napi/src/clvm.rs @@ -2,7 +2,8 @@ use std::mem; use chia::{ clvm_traits::{ClvmDecoder, ClvmEncoder, FromClvm, ToClvm}, - clvm_utils::{tree_hash, CurriedProgram}, + clvm_utils::{self, tree_hash, CurriedProgram, TreeHash}, + protocol::Bytes32, }; use chia_wallet_sdk::SpendContext; use clvmr::{ @@ -299,3 +300,15 @@ pub struct Curry { pub program: ClassInstance, pub args: Vec>, } + +#[napi] +pub fn curry_tree_hash(tree_hash: Uint8Array, args: Vec) -> Result { + let tree_hash: Bytes32 = tree_hash.into_rust()?; + let args: Vec = args + .into_iter() + .map(|arg| Ok(TreeHash::new(arg.into_rust()?))) + .collect::>>()?; + clvm_utils::curry_tree_hash(tree_hash.into(), &args) + .to_bytes() + .into_js() +}