-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WASM: expose the transaction description types
`SpendDescription`, `OutputDescription`, `MintDescription`, and `BurnDescription`
- Loading branch information
Showing
8 changed files
with
306 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
use crate::{assets::AssetIdentifier, errors::IronfishError}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
#[derive(Clone, PartialEq, Eq, Debug)] | ||
pub struct BurnDescription(ironfish::transaction::burns::BurnDescription); | ||
|
||
#[wasm_bindgen] | ||
impl BurnDescription { | ||
#[wasm_bindgen(constructor)] | ||
pub fn deserialize(bytes: &[u8]) -> Result<BurnDescription, IronfishError> { | ||
Ok(Self(ironfish::transaction::burns::BurnDescription::read( | ||
bytes, | ||
)?)) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn serialize(&self) -> Vec<u8> { | ||
let mut buf = Vec::new(); | ||
self.0 | ||
.write(&mut buf) | ||
.expect("failed to serialize mint description"); | ||
buf | ||
} | ||
|
||
#[wasm_bindgen(getter, js_name = assetId)] | ||
pub fn asset_id(&self) -> AssetIdentifier { | ||
self.0.asset_id.into() | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn value(&self) -> u64 { | ||
self.0.value | ||
} | ||
} | ||
|
||
impl From<ironfish::transaction::burns::BurnDescription> for BurnDescription { | ||
fn from(d: ironfish::transaction::burns::BurnDescription) -> Self { | ||
Self(d) | ||
} | ||
} | ||
|
||
impl AsRef<ironfish::transaction::burns::BurnDescription> for BurnDescription { | ||
fn as_ref(&self) -> &ironfish::transaction::burns::BurnDescription { | ||
&self.0 | ||
} | ||
} |
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,91 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
use crate::{ | ||
assets::Asset, | ||
errors::IronfishError, | ||
keys::PublicAddress, | ||
primitives::{PublicKey, Scalar}, | ||
}; | ||
use ironfish::{errors::IronfishErrorKind, transaction::TransactionVersion}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
#[derive(Clone, Debug)] | ||
pub struct MintDescription(ironfish::transaction::mints::MintDescription); | ||
|
||
#[wasm_bindgen] | ||
impl MintDescription { | ||
#[wasm_bindgen(constructor)] | ||
pub fn deserialize(bytes: &[u8]) -> Result<MintDescription, IronfishError> { | ||
Ok(Self(ironfish::transaction::mints::MintDescription::read( | ||
bytes, | ||
TransactionVersion::V1, | ||
)?)) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn serialize(&self) -> Vec<u8> { | ||
let mut buf = Vec::new(); | ||
self.0 | ||
.write(&mut buf, TransactionVersion::V1) | ||
.expect("failed to serialize mint description"); | ||
buf | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn assets(&self) -> Asset { | ||
self.0.asset.into() | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn value(&self) -> u64 { | ||
self.0.value | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn owner(&self) -> PublicAddress { | ||
self.0.owner.into() | ||
} | ||
|
||
#[wasm_bindgen(js_name = verifySignature)] | ||
pub fn verify_signature( | ||
&self, | ||
signature: &[u8], | ||
randomized_public_key: &PublicKey, | ||
) -> Result<(), IronfishError> { | ||
let signature = signature | ||
.try_into() | ||
.map_err(|_| IronfishErrorKind::InvalidSignature)?; | ||
self.0 | ||
.verify_signature(signature, randomized_public_key.as_ref()) | ||
.map_err(|e| e.into()) | ||
} | ||
|
||
#[wasm_bindgen(js_name = partialVerify)] | ||
pub fn partial_verify(&self) -> Result<(), IronfishError> { | ||
self.0.partial_verify().map_err(|e| e.into()) | ||
} | ||
|
||
#[wasm_bindgen(js_name = publicInputs)] | ||
pub fn public_inputs(&self, randomized_public_key: &PublicKey) -> Vec<Scalar> { | ||
self.0 | ||
.public_inputs(randomized_public_key.as_ref()) | ||
.into_iter() | ||
.map(Scalar::from) | ||
.collect() | ||
} | ||
} | ||
|
||
impl From<ironfish::transaction::mints::MintDescription> for MintDescription { | ||
fn from(d: ironfish::transaction::mints::MintDescription) -> Self { | ||
Self(d) | ||
} | ||
} | ||
|
||
impl AsRef<ironfish::transaction::mints::MintDescription> for MintDescription { | ||
fn as_ref(&self) -> &ironfish::transaction::mints::MintDescription { | ||
&self.0 | ||
} | ||
} |
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,13 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
mod burns; | ||
mod mints; | ||
mod outputs; | ||
mod spends; | ||
|
||
pub use burns::BurnDescription; | ||
pub use mints::MintDescription; | ||
pub use outputs::OutputDescription; | ||
pub use spends::SpendDescription; |
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,62 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
use crate::{ | ||
errors::IronfishError, | ||
merkle_note::MerkleNote, | ||
primitives::{PublicKey, Scalar}, | ||
}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
#[derive(Clone, PartialEq, Debug)] | ||
pub struct OutputDescription(ironfish::OutputDescription); | ||
|
||
#[wasm_bindgen] | ||
impl OutputDescription { | ||
#[wasm_bindgen(constructor)] | ||
pub fn deserialize(bytes: &[u8]) -> Result<OutputDescription, IronfishError> { | ||
Ok(Self(ironfish::OutputDescription::read(bytes)?)) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn serialize(&self) -> Vec<u8> { | ||
let mut buf = Vec::new(); | ||
self.0 | ||
.write(&mut buf) | ||
.expect("failed to serialize output description"); | ||
buf | ||
} | ||
|
||
#[wasm_bindgen(js_name = partialVerify)] | ||
pub fn partial_verify(&self) -> Result<(), IronfishError> { | ||
self.0.partial_verify().map_err(|e| e.into()) | ||
} | ||
|
||
#[wasm_bindgen(js_name = publicInputs)] | ||
pub fn public_inputs(&self, randomized_public_key: &PublicKey) -> Vec<Scalar> { | ||
self.0 | ||
.public_inputs(randomized_public_key.as_ref()) | ||
.into_iter() | ||
.map(Scalar::from) | ||
.collect() | ||
} | ||
|
||
#[wasm_bindgen(getter, js_name = merkleNote)] | ||
pub fn merkle_note(&self) -> MerkleNote { | ||
self.0.merkle_note().into() | ||
} | ||
} | ||
|
||
impl From<ironfish::OutputDescription> for OutputDescription { | ||
fn from(d: ironfish::OutputDescription) -> Self { | ||
Self(d) | ||
} | ||
} | ||
|
||
impl AsRef<ironfish::OutputDescription> for OutputDescription { | ||
fn as_ref(&self) -> &ironfish::OutputDescription { | ||
&self.0 | ||
} | ||
} |
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,86 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
use crate::{ | ||
errors::IronfishError, | ||
primitives::{Nullifier, PublicKey, Scalar}, | ||
}; | ||
use ironfish::errors::IronfishErrorKind; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
#[derive(Clone, Debug)] | ||
pub struct SpendDescription(ironfish::SpendDescription); | ||
|
||
#[wasm_bindgen] | ||
impl SpendDescription { | ||
#[wasm_bindgen(constructor)] | ||
pub fn deserialize(bytes: &[u8]) -> Result<SpendDescription, IronfishError> { | ||
Ok(Self(ironfish::SpendDescription::read(bytes)?)) | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn serialize(&self) -> Vec<u8> { | ||
let mut buf = Vec::new(); | ||
self.0 | ||
.write(&mut buf) | ||
.expect("failed to serialize spend description"); | ||
buf | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn nullifier(&self) -> Nullifier { | ||
self.0.nullifier().into() | ||
} | ||
|
||
#[wasm_bindgen(getter, js_name = rootHash)] | ||
pub fn root_hash(&self) -> Scalar { | ||
self.0.root_hash().into() | ||
} | ||
|
||
#[wasm_bindgen(getter, js_name = treeSize)] | ||
pub fn tree_size(&self) -> u32 { | ||
self.0.tree_size() | ||
} | ||
|
||
#[wasm_bindgen(js_name = verifySignature)] | ||
pub fn verify_signature( | ||
&self, | ||
signature: &[u8], | ||
randomized_public_key: &PublicKey, | ||
) -> Result<(), IronfishError> { | ||
let signature = signature | ||
.try_into() | ||
.map_err(|_| IronfishErrorKind::InvalidSignature)?; | ||
self.0 | ||
.verify_signature(signature, randomized_public_key.as_ref()) | ||
.map_err(|e| e.into()) | ||
} | ||
|
||
#[wasm_bindgen(js_name = partialVerify)] | ||
pub fn partial_verify(&self) -> Result<(), IronfishError> { | ||
self.0.partial_verify().map_err(|e| e.into()) | ||
} | ||
|
||
#[wasm_bindgen(js_name = publicInputs)] | ||
pub fn public_inputs(&self, randomized_public_key: &PublicKey) -> Vec<Scalar> { | ||
self.0 | ||
.public_inputs(randomized_public_key.as_ref()) | ||
.into_iter() | ||
.map(Scalar::from) | ||
.collect() | ||
} | ||
} | ||
|
||
impl From<ironfish::SpendDescription> for SpendDescription { | ||
fn from(d: ironfish::SpendDescription) -> Self { | ||
Self(d) | ||
} | ||
} | ||
|
||
impl AsRef<ironfish::SpendDescription> for SpendDescription { | ||
fn as_ref(&self) -> &ironfish::SpendDescription { | ||
&self.0 | ||
} | ||
} |
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