Skip to content

Commit

Permalink
Curry tree hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Sep 25, 2024
1 parent db8eb4b commit 2e8eb34
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
16 changes: 16 additions & 0 deletions napi/__test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ClvmAllocator,
ClvmPtr,
compareBytes,
curryTreeHash,
fromHex,
toCoinId,
toHex,
Expand Down Expand Up @@ -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));
})
1 change: 1 addition & 0 deletions napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Curry {
program: ClvmPtr
args: Array<ClvmPtr>
}
export declare function curryTreeHash(treeHash: Uint8Array, args: Array<Uint8Array>): Uint8Array
export interface Coin {
parentCoinInfo: Uint8Array
puzzleHash: Uint8Array
Expand Down
3 changes: 2 additions & 1 deletion napi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion napi/src/clvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -299,3 +300,15 @@ pub struct Curry {
pub program: ClassInstance<ClvmPtr>,
pub args: Vec<ClassInstance<ClvmPtr>>,
}

#[napi]
pub fn curry_tree_hash(tree_hash: Uint8Array, args: Vec<Uint8Array>) -> Result<Uint8Array> {
let tree_hash: Bytes32 = tree_hash.into_rust()?;
let args: Vec<TreeHash> = args
.into_iter()
.map(|arg| Ok(TreeHash::new(arg.into_rust()?)))
.collect::<Result<Vec<_>>>()?;
clvm_utils::curry_tree_hash(tree_hash.into(), &args)
.to_bytes()
.into_js()
}

0 comments on commit 2e8eb34

Please sign in to comment.