-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
175 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |