Skip to content

Commit

Permalink
Coin and coin spend
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Sep 24, 2024
1 parent ae35d0d commit 5f2d81f
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 19 deletions.
38 changes: 36 additions & 2 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@ clvm-traits = { workspace = true, features = ["derive"] }
clvmr = { workspace = true }

[workspace.dependencies]
chia-wallet-sdk = { version = "0.14.0", path = "." }
chia-sdk-client = { version = "0.14.0", path = "./crates/chia-sdk-client" }
chia-sdk-derive = { version = "0.14.0", path = "./crates/chia-sdk-derive" }
chia-sdk-driver = { version = "0.14.0", path = "./crates/chia-sdk-driver" }
chia-sdk-offers = { version = "0.14.0", path = "./crates/chia-sdk-offers" }
chia-sdk-signer = { version = "0.14.0", path = "./crates/chia-sdk-signer" }
chia-sdk-test = { version = "0.14.0", path = "./crates/chia-sdk-test" }
chia-sdk-types = { version = "0.14.0", path = "./crates/chia-sdk-types" }
chia = "0.13.0"
chia-ssl = "0.11.0"
chia-protocol = "0.13.0"
chia-consensus = "0.13.0"
Expand Down Expand Up @@ -131,3 +133,9 @@ syn = "2.0.76"
quote = "1.0.37"
convert_case = "0.6.0"
fastrand = "2.1.1"
napi-derive = "2.12.2"
napi = { version = "2.12.2", default-features = false }

[profile.release]
lto = true
strip = "symbols"
11 changes: 4 additions & 7 deletions napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ workspace = true
crate-type = ["cdylib"]

[dependencies]
napi = { version = "2.12.2", default-features = false, features = ["napi4"] }
napi-derive = "2.12.2"
chia-wallet-sdk = { path = ".." }
napi = { workspace = true, features = ["napi6"] }
napi-derive = { workspace = true }
chia-wallet-sdk = { workspace = true }
chia = { workspace = true }

[build-dependencies]
napi-build = "2.0.1"

[profile.release]
lto = true
strip = "symbols"
12 changes: 11 additions & 1 deletion napi/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@

/* auto-generated by NAPI-RS */

export declare function sum(a: number, b: number): number
export interface Coin {
parentCoinInfo: Uint8Array
puzzleHash: Uint8Array
amount: bigint
}
export declare function toCoinId(coin: Coin): Uint8Array
export interface CoinSpend {
coin: Coin
puzzleReveal: Uint8Array
solution: Uint8Array
}
4 changes: 2 additions & 2 deletions napi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,6 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { sum } = nativeBinding
const { toCoinId } = nativeBinding

module.exports.sum = sum
module.exports.toCoinId = toCoinId
2 changes: 0 additions & 2 deletions napi/rustfmt.toml

This file was deleted.

36 changes: 36 additions & 0 deletions napi/src/coin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use chia::protocol;
use napi::bindgen_prelude::*;

use crate::traits::{FromJs, IntoJs, IntoRust};

#[napi(object)]
pub struct Coin {
pub parent_coin_info: Uint8Array,
pub puzzle_hash: Uint8Array,
pub amount: BigInt,
}

impl IntoJs<Coin> for protocol::Coin {
fn into_js(self) -> Result<Coin> {
Ok(Coin {
parent_coin_info: self.parent_coin_info.into_js()?,
puzzle_hash: self.puzzle_hash.into_js()?,
amount: self.amount.into_js()?,
})
}
}

impl FromJs<Coin> for protocol::Coin {
fn from_js(value: Coin) -> Result<Self> {
Ok(Self {
parent_coin_info: value.parent_coin_info.into_rust()?,
puzzle_hash: value.puzzle_hash.into_rust()?,
amount: value.amount.into_rust()?,
})
}
}

#[napi]
pub fn to_coin_id(coin: Coin) -> Result<Uint8Array> {
protocol::Coin::from_js(coin)?.coin_id().into_js()
}
10 changes: 10 additions & 0 deletions napi/src/coin_spend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use napi::bindgen_prelude::*;

use crate::Coin;

#[napi(object)]
pub struct CoinSpend {
pub coin: Coin,
pub puzzle_reveal: Uint8Array,
pub solution: Uint8Array,
}
12 changes: 7 additions & 5 deletions napi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![deny(clippy::all)]
#![allow(missing_debug_implementations)]

#[macro_use]
extern crate napi_derive;

#[napi]
pub fn sum(a: i32, b: i32) -> i32 {
a + b
}
mod coin;
mod coin_spend;
mod traits;

pub use coin::*;
pub use coin_spend::*;
61 changes: 61 additions & 0 deletions napi/src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use chia::protocol::BytesImpl;
use napi::bindgen_prelude::*;

pub(crate) trait IntoJs<T> {
fn into_js(self) -> Result<T>;
}

pub(crate) trait FromJs<T> {
fn from_js(js_value: T) -> Result<Self>
where
Self: Sized;
}

pub(crate) trait IntoRust<T> {
fn into_rust(self) -> Result<T>;
}

// Implement ToRust for every type that implements FromJs

impl<T, U> IntoRust<U> for T
where
U: FromJs<T>,
{
fn into_rust(self) -> Result<U> {
U::from_js(self)
}
}

impl<const N: usize> IntoJs<Uint8Array> for BytesImpl<N> {
fn into_js(self) -> Result<Uint8Array> {
Ok(Uint8Array::new(self.to_vec()))
}
}

impl<const N: usize> FromJs<Uint8Array> for BytesImpl<N> {
fn from_js(js_value: Uint8Array) -> Result<Self> {
Ok(Self::new(js_value.to_vec().try_into().map_err(
|bytes: Vec<u8>| {
Error::from_reason(format!("Expected length {N}, found {}", bytes.len()))
},
)?))
}
}

impl IntoJs<BigInt> for u64 {
fn into_js(self) -> Result<BigInt> {
Ok(BigInt::from(self))
}
}

impl FromJs<BigInt> for u64 {
fn from_js(js_value: BigInt) -> Result<Self> {
let (signed, value, lossless) = js_value.get_u64();

if signed || !lossless {
return Err(Error::from_reason("Expected u64"));
}

Ok(value)
}
}

0 comments on commit 5f2d81f

Please sign in to comment.