Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Oct 6, 2024
1 parent 8bf4e3e commit d10c45c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
11 changes: 10 additions & 1 deletion napi/__test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,14 @@ test("mint and spend nft", (t) => {

simulator.spend(coinSpends, [p2.secretKey]);

t.pass();
t.true(
compareBytes(
clvm
.nftMetadata(
clvm.parseNftMetadata(clvm.deserialize(result.nfts[0].info.metadata))
)
.serialize(),
result.nfts[0].info.metadata
)
);
});
6 changes: 5 additions & 1 deletion napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface Output {
cost: bigint
}
export declare function curryTreeHash(treeHash: Uint8Array, args: Array<Uint8Array>): Uint8Array
export declare function intToSignedBytes(bigInt: bigint): Uint8Array
export declare function signedBytesToInt(bytes: Uint8Array): bigint
export interface Coin {
parentCoinInfo: Uint8Array
puzzleHash: Uint8Array
Expand Down Expand Up @@ -35,7 +37,7 @@ export interface Nft {
}
export interface NftInfo {
launcherId: Uint8Array
metadata: NftMetadata
metadata: Uint8Array
metadataUpdaterPuzzleHash: Uint8Array
currentOwner?: Uint8Array
royaltyPuzzleHash: Uint8Array
Expand Down Expand Up @@ -92,6 +94,8 @@ export declare class ClvmAllocator {
curry(program: Program, args: Array<Program>): Program
pair(first: ClvmValue, rest: ClvmValue): Program
alloc(value: ClvmValue): Program
nftMetadata(value: NftMetadata): Program
parseNftMetadata(value: Program): NftMetadata
delegatedSpendForConditions(conditions: Array<Program>): Spend
spendP2Standard(syntheticKey: Uint8Array, delegatedSpend: Spend): Spend
spendP2DelegatedSingleton(launcherId: Uint8Array, coinId: Uint8Array, singletonInnerPuzzleHash: Uint8Array, delegatedSpend: Spend): Spend
Expand Down
4 changes: 3 additions & 1 deletion napi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,12 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { ClvmAllocator, curryTreeHash, toCoinId, Program, Simulator, compareBytes, sha256, fromHexRaw, fromHex, toHex } = nativeBinding
const { ClvmAllocator, curryTreeHash, intToSignedBytes, signedBytesToInt, toCoinId, Program, Simulator, compareBytes, sha256, fromHexRaw, fromHex, toHex } = nativeBinding

module.exports.ClvmAllocator = ClvmAllocator
module.exports.curryTreeHash = curryTreeHash
module.exports.intToSignedBytes = intToSignedBytes
module.exports.signedBytesToInt = signedBytesToInt
module.exports.toCoinId = toCoinId
module.exports.Program = Program
module.exports.Simulator = Simulator
Expand Down
64 changes: 54 additions & 10 deletions napi/src/clvm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use chia::{
bls::PublicKey,
clvm_traits::{clvm_quote, ClvmEncoder, ToClvm},
clvm_traits::{clvm_quote, ClvmEncoder, FromClvm, ToClvm},
clvm_utils::{self, CurriedProgram, TreeHash},
protocol::Bytes32,
protocol::{self, Bytes32},
puzzles::nft::{self, NFT_METADATA_UPDATER_PUZZLE_HASH},
};
use chia_wallet_sdk::{
Expand All @@ -13,8 +13,8 @@ use chia_wallet_sdk::{
AssertHeightAbsolute, AssertHeightRelative, AssertMyAmount, AssertMyBirthHeight,
AssertMyBirthSeconds, AssertMyCoinId, AssertMyParentId, AssertMyPuzzleHash,
AssertPuzzleAnnouncement, AssertSecondsAbsolute, AssertSecondsRelative, CreateCoin,
CreateCoinAnnouncement, CreatePuzzleAnnouncement, ReceiveMessage, Remark, ReserveFee,
SendMessage, Softfork, SpendContext,
CreateCoinAnnouncement, CreatePuzzleAnnouncement, HashedPtr, ReceiveMessage, Remark,
ReserveFee, SendMessage, Softfork, SpendContext,
};
use clvmr::{
run_program,
Expand All @@ -26,7 +26,7 @@ use napi::bindgen_prelude::*;
use crate::{
clvm_value::{Allocate, ClvmValue},
traits::{FromJs, IntoJs, IntoRust},
Coin, CoinSpend, MintedNfts, Nft, NftMint, ParsedNft, Program, Spend,
Coin, CoinSpend, MintedNfts, Nft, NftMetadata, NftMint, ParsedNft, Program, Spend,
};

type Clvm = Reference<ClvmAllocator>;
Expand Down Expand Up @@ -144,6 +144,25 @@ impl ClvmAllocator {
Ok(Program::new(this, ptr))
}

#[napi(ts_args_type = "value: NftMetadata")]
pub fn nft_metadata(&mut self, this: This<Clvm>, value: NftMetadata) -> Result<Program> {
let metadata = nft::NftMetadata::from_js(value)?;

let ptr = metadata
.to_clvm(&mut self.0.allocator)
.map_err(|error| Error::from_reason(error.to_string()))?;

Ok(Program::new(this, ptr))
}

#[napi(ts_args_type = "value: Program")]
pub fn parse_nft_metadata(&mut self, value: &Program) -> Result<NftMetadata> {
let metadata = nft::NftMetadata::from_clvm(&self.0.allocator, value.ptr)
.map_err(|error| Error::from_reason(error.to_string()))?;

metadata.into_js()
}

#[napi(ts_args_type = "conditions: Array<Program>")]
pub fn delegated_spend_for_conditions(
&mut self,
Expand Down Expand Up @@ -252,7 +271,14 @@ impl ClvmAllocator {
)
.map_err(|error| Error::from_reason(error.to_string()))?;

result.nfts.push(nft.into_js()?);
let serialized_metadata = self
.0
.serialize(&nft.info.metadata)
.map_err(|error| Error::from_reason(error.to_string()))?;

result
.nfts
.push(nft.with_metadata(serialized_metadata).into_js()?);

for condition in conditions {
let condition = condition
Expand Down Expand Up @@ -286,7 +312,7 @@ impl ClvmAllocator {
let puzzle = sdk::Puzzle::parse(&self.0.allocator, puzzle.ptr);

let Some((nft_info, inner_puzzle)) =
sdk::NftInfo::<nft::NftMetadata>::parse(&self.0.allocator, puzzle)
sdk::NftInfo::<protocol::Program>::parse(&self.0.allocator, puzzle)
.map_err(|error| Error::from_reason(error.to_string()))?
else {
return Ok(None);
Expand All @@ -307,7 +333,7 @@ impl ClvmAllocator {
) -> Result<Option<Nft>> {
let parent_puzzle = sdk::Puzzle::parse(&self.0.allocator, parent_puzzle.ptr);

let Some(nft) = sdk::Nft::<nft::NftMetadata>::parse_child(
let Some(nft) = sdk::Nft::<HashedPtr>::parse_child(
&mut self.0.allocator,
parent_coin.into_rust()?,
parent_puzzle,
Expand All @@ -318,13 +344,18 @@ impl ClvmAllocator {
return Ok(None);
};

Ok(Some(nft.into_js()?))
let serialized_metadata = self
.0
.serialize(&nft.info.metadata)
.map_err(|error| Error::from_reason(error.to_string()))?;

Ok(Some(nft.with_metadata(serialized_metadata).into_js()?))
}

#[napi]
pub fn spend_nft(&mut self, nft: Nft, inner_spend: Spend) -> Result<Vec<CoinSpend>> {
let ctx = &mut self.0;
let nft = sdk::Nft::<nft::NftMetadata>::from_js(nft)?;
let nft = sdk::Nft::<protocol::Program>::from_js(nft)?;

nft.spend(
ctx,
Expand Down Expand Up @@ -354,6 +385,19 @@ pub fn curry_tree_hash(tree_hash: Uint8Array, args: Vec<Uint8Array>) -> Result<U
.into_js()
}

#[napi]
pub fn int_to_signed_bytes(big_int: BigInt) -> Result<Uint8Array> {
let number: num_bigint::BigInt = big_int.into_rust()?;
number.to_signed_bytes_be().into_js()
}

#[napi]
pub fn signed_bytes_to_int(bytes: Uint8Array) -> Result<BigInt> {
let bytes: Vec<u8> = bytes.into_rust()?;
let number = num_bigint::BigInt::from_signed_bytes_be(&bytes);
number.into_js()
}

macro_rules! conditions {
( $( $condition:ident { $hint:literal $function:ident( $( $name:ident: $ty:ty $( => $remap:ty )? ),* ) }, )* ) => {
$( #[napi]
Expand Down
11 changes: 6 additions & 5 deletions napi/src/nft.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chia::protocol;
use chia::puzzles::nft;
use chia_wallet_sdk as sdk;
use napi::bindgen_prelude::*;
Expand All @@ -14,7 +15,7 @@ pub struct Nft {
pub info: NftInfo,
}

impl IntoJs<Nft> for sdk::Nft<nft::NftMetadata> {
impl IntoJs<Nft> for sdk::Nft<protocol::Program> {
fn into_js(self) -> Result<Nft> {
Ok(Nft {
coin: self.coin.into_js()?,
Expand All @@ -24,7 +25,7 @@ impl IntoJs<Nft> for sdk::Nft<nft::NftMetadata> {
}
}

impl FromJs<Nft> for sdk::Nft<nft::NftMetadata> {
impl FromJs<Nft> for sdk::Nft<protocol::Program> {
fn from_js(nft: Nft) -> Result<Self> {
Ok(sdk::Nft {
coin: nft.coin.into_rust()?,
Expand All @@ -37,15 +38,15 @@ impl FromJs<Nft> for sdk::Nft<nft::NftMetadata> {
#[napi(object)]
pub struct NftInfo {
pub launcher_id: Uint8Array,
pub metadata: NftMetadata,
pub metadata: Uint8Array,
pub metadata_updater_puzzle_hash: Uint8Array,
pub current_owner: Option<Uint8Array>,
pub royalty_puzzle_hash: Uint8Array,
pub royalty_ten_thousandths: u16,
pub p2_puzzle_hash: Uint8Array,
}

impl IntoJs<NftInfo> for sdk::NftInfo<nft::NftMetadata> {
impl IntoJs<NftInfo> for sdk::NftInfo<protocol::Program> {
fn into_js(self) -> Result<NftInfo> {
Ok(NftInfo {
launcher_id: self.launcher_id.into_js()?,
Expand All @@ -59,7 +60,7 @@ impl IntoJs<NftInfo> for sdk::NftInfo<nft::NftMetadata> {
}
}

impl FromJs<NftInfo> for sdk::NftInfo<nft::NftMetadata> {
impl FromJs<NftInfo> for sdk::NftInfo<protocol::Program> {
fn from_js(info: NftInfo) -> Result<Self> {
Ok(sdk::NftInfo {
launcher_id: info.launcher_id.into_rust()?,
Expand Down

0 comments on commit d10c45c

Please sign in to comment.