Skip to content

Commit 71cae38

Browse files
committed
WASM: expose the transaction description types
`SpendDescription`, `OutputDescription`, `MintDescription`, and `BurnDescription`
1 parent f310cce commit 71cae38

File tree

8 files changed

+306
-2
lines changed

8 files changed

+306
-2
lines changed

ironfish-rust-wasm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod errors;
1818
pub mod keys;
1919
pub mod merkle_note;
2020
pub mod primitives;
21+
pub mod transaction;
2122

2223
#[cfg(test)]
2324
mod tests {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
5+
use crate::{assets::AssetIdentifier, errors::IronfishError};
6+
use wasm_bindgen::prelude::*;
7+
8+
#[wasm_bindgen]
9+
#[derive(Clone, PartialEq, Eq, Debug)]
10+
pub struct BurnDescription(ironfish::transaction::burns::BurnDescription);
11+
12+
#[wasm_bindgen]
13+
impl BurnDescription {
14+
#[wasm_bindgen(constructor)]
15+
pub fn deserialize(bytes: &[u8]) -> Result<BurnDescription, IronfishError> {
16+
Ok(Self(ironfish::transaction::burns::BurnDescription::read(
17+
bytes,
18+
)?))
19+
}
20+
21+
#[wasm_bindgen]
22+
pub fn serialize(&self) -> Vec<u8> {
23+
let mut buf = Vec::new();
24+
self.0
25+
.write(&mut buf)
26+
.expect("failed to serialize mint description");
27+
buf
28+
}
29+
30+
#[wasm_bindgen(getter, js_name = assetId)]
31+
pub fn asset_id(&self) -> AssetIdentifier {
32+
self.0.asset_id.into()
33+
}
34+
35+
#[wasm_bindgen(getter)]
36+
pub fn value(&self) -> u64 {
37+
self.0.value
38+
}
39+
}
40+
41+
impl From<ironfish::transaction::burns::BurnDescription> for BurnDescription {
42+
fn from(d: ironfish::transaction::burns::BurnDescription) -> Self {
43+
Self(d)
44+
}
45+
}
46+
47+
impl AsRef<ironfish::transaction::burns::BurnDescription> for BurnDescription {
48+
fn as_ref(&self) -> &ironfish::transaction::burns::BurnDescription {
49+
&self.0
50+
}
51+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
5+
use crate::{
6+
assets::Asset,
7+
errors::IronfishError,
8+
keys::PublicAddress,
9+
primitives::{PublicKey, Scalar},
10+
};
11+
use ironfish::{errors::IronfishErrorKind, transaction::TransactionVersion};
12+
use wasm_bindgen::prelude::*;
13+
14+
#[wasm_bindgen]
15+
#[derive(Clone, Debug)]
16+
pub struct MintDescription(ironfish::transaction::mints::MintDescription);
17+
18+
#[wasm_bindgen]
19+
impl MintDescription {
20+
#[wasm_bindgen(constructor)]
21+
pub fn deserialize(bytes: &[u8]) -> Result<MintDescription, IronfishError> {
22+
Ok(Self(ironfish::transaction::mints::MintDescription::read(
23+
bytes,
24+
TransactionVersion::V1,
25+
)?))
26+
}
27+
28+
#[wasm_bindgen]
29+
pub fn serialize(&self) -> Vec<u8> {
30+
let mut buf = Vec::new();
31+
self.0
32+
.write(&mut buf, TransactionVersion::V1)
33+
.expect("failed to serialize mint description");
34+
buf
35+
}
36+
37+
#[wasm_bindgen(getter)]
38+
pub fn assets(&self) -> Asset {
39+
self.0.asset.into()
40+
}
41+
42+
#[wasm_bindgen(getter)]
43+
pub fn value(&self) -> u64 {
44+
self.0.value
45+
}
46+
47+
#[wasm_bindgen(getter)]
48+
pub fn owner(&self) -> PublicAddress {
49+
self.0.owner.into()
50+
}
51+
52+
#[wasm_bindgen(js_name = verifySignature)]
53+
pub fn verify_signature(
54+
&self,
55+
signature: &[u8],
56+
randomized_public_key: &PublicKey,
57+
) -> Result<(), IronfishError> {
58+
let signature = signature
59+
.try_into()
60+
.map_err(|_| IronfishErrorKind::InvalidSignature)?;
61+
self.0
62+
.verify_signature(signature, randomized_public_key.as_ref())
63+
.map_err(|e| e.into())
64+
}
65+
66+
#[wasm_bindgen(js_name = partialVerify)]
67+
pub fn partial_verify(&self) -> Result<(), IronfishError> {
68+
self.0.partial_verify().map_err(|e| e.into())
69+
}
70+
71+
#[wasm_bindgen(js_name = publicInputs)]
72+
pub fn public_inputs(&self, randomized_public_key: &PublicKey) -> Vec<Scalar> {
73+
self.0
74+
.public_inputs(randomized_public_key.as_ref())
75+
.into_iter()
76+
.map(Scalar::from)
77+
.collect()
78+
}
79+
}
80+
81+
impl From<ironfish::transaction::mints::MintDescription> for MintDescription {
82+
fn from(d: ironfish::transaction::mints::MintDescription) -> Self {
83+
Self(d)
84+
}
85+
}
86+
87+
impl AsRef<ironfish::transaction::mints::MintDescription> for MintDescription {
88+
fn as_ref(&self) -> &ironfish::transaction::mints::MintDescription {
89+
&self.0
90+
}
91+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
5+
mod burns;
6+
mod mints;
7+
mod outputs;
8+
mod spends;
9+
10+
pub use burns::BurnDescription;
11+
pub use mints::MintDescription;
12+
pub use outputs::OutputDescription;
13+
pub use spends::SpendDescription;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
5+
use crate::{
6+
errors::IronfishError,
7+
merkle_note::MerkleNote,
8+
primitives::{PublicKey, Scalar},
9+
};
10+
use wasm_bindgen::prelude::*;
11+
12+
#[wasm_bindgen]
13+
#[derive(Clone, PartialEq, Debug)]
14+
pub struct OutputDescription(ironfish::OutputDescription);
15+
16+
#[wasm_bindgen]
17+
impl OutputDescription {
18+
#[wasm_bindgen(constructor)]
19+
pub fn deserialize(bytes: &[u8]) -> Result<OutputDescription, IronfishError> {
20+
Ok(Self(ironfish::OutputDescription::read(bytes)?))
21+
}
22+
23+
#[wasm_bindgen]
24+
pub fn serialize(&self) -> Vec<u8> {
25+
let mut buf = Vec::new();
26+
self.0
27+
.write(&mut buf)
28+
.expect("failed to serialize output description");
29+
buf
30+
}
31+
32+
#[wasm_bindgen(js_name = partialVerify)]
33+
pub fn partial_verify(&self) -> Result<(), IronfishError> {
34+
self.0.partial_verify().map_err(|e| e.into())
35+
}
36+
37+
#[wasm_bindgen(js_name = publicInputs)]
38+
pub fn public_inputs(&self, randomized_public_key: &PublicKey) -> Vec<Scalar> {
39+
self.0
40+
.public_inputs(randomized_public_key.as_ref())
41+
.into_iter()
42+
.map(Scalar::from)
43+
.collect()
44+
}
45+
46+
#[wasm_bindgen(getter, js_name = merkleNote)]
47+
pub fn merkle_note(&self) -> MerkleNote {
48+
self.0.merkle_note().into()
49+
}
50+
}
51+
52+
impl From<ironfish::OutputDescription> for OutputDescription {
53+
fn from(d: ironfish::OutputDescription) -> Self {
54+
Self(d)
55+
}
56+
}
57+
58+
impl AsRef<ironfish::OutputDescription> for OutputDescription {
59+
fn as_ref(&self) -> &ironfish::OutputDescription {
60+
&self.0
61+
}
62+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
5+
use crate::{
6+
errors::IronfishError,
7+
primitives::{Nullifier, PublicKey, Scalar},
8+
};
9+
use ironfish::errors::IronfishErrorKind;
10+
use wasm_bindgen::prelude::*;
11+
12+
#[wasm_bindgen]
13+
#[derive(Clone, Debug)]
14+
pub struct SpendDescription(ironfish::SpendDescription);
15+
16+
#[wasm_bindgen]
17+
impl SpendDescription {
18+
#[wasm_bindgen(constructor)]
19+
pub fn deserialize(bytes: &[u8]) -> Result<SpendDescription, IronfishError> {
20+
Ok(Self(ironfish::SpendDescription::read(bytes)?))
21+
}
22+
23+
#[wasm_bindgen]
24+
pub fn serialize(&self) -> Vec<u8> {
25+
let mut buf = Vec::new();
26+
self.0
27+
.write(&mut buf)
28+
.expect("failed to serialize spend description");
29+
buf
30+
}
31+
32+
#[wasm_bindgen(getter)]
33+
pub fn nullifier(&self) -> Nullifier {
34+
self.0.nullifier().into()
35+
}
36+
37+
#[wasm_bindgen(getter, js_name = rootHash)]
38+
pub fn root_hash(&self) -> Scalar {
39+
self.0.root_hash().into()
40+
}
41+
42+
#[wasm_bindgen(getter, js_name = treeSize)]
43+
pub fn tree_size(&self) -> u32 {
44+
self.0.tree_size()
45+
}
46+
47+
#[wasm_bindgen(js_name = verifySignature)]
48+
pub fn verify_signature(
49+
&self,
50+
signature: &[u8],
51+
randomized_public_key: &PublicKey,
52+
) -> Result<(), IronfishError> {
53+
let signature = signature
54+
.try_into()
55+
.map_err(|_| IronfishErrorKind::InvalidSignature)?;
56+
self.0
57+
.verify_signature(signature, randomized_public_key.as_ref())
58+
.map_err(|e| e.into())
59+
}
60+
61+
#[wasm_bindgen(js_name = partialVerify)]
62+
pub fn partial_verify(&self) -> Result<(), IronfishError> {
63+
self.0.partial_verify().map_err(|e| e.into())
64+
}
65+
66+
#[wasm_bindgen(js_name = publicInputs)]
67+
pub fn public_inputs(&self, randomized_public_key: &PublicKey) -> Vec<Scalar> {
68+
self.0
69+
.public_inputs(randomized_public_key.as_ref())
70+
.into_iter()
71+
.map(Scalar::from)
72+
.collect()
73+
}
74+
}
75+
76+
impl From<ironfish::SpendDescription> for SpendDescription {
77+
fn from(d: ironfish::SpendDescription) -> Self {
78+
Self(d)
79+
}
80+
}
81+
82+
impl AsRef<ironfish::SpendDescription> for SpendDescription {
83+
fn as_ref(&self) -> &ironfish::SpendDescription {
84+
&self.0
85+
}
86+
}

ironfish-rust/src/transaction/burns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl BurnBuilder {
3232

3333
/// This description represents an action to decrease the supply of an existing
3434
/// asset on Iron Fish
35-
#[derive(Clone, Debug)]
35+
#[derive(Clone, PartialEq, Eq, Debug)]
3636
pub struct BurnDescription {
3737
/// Identifier for the Asset which is being burned
3838
pub asset_id: AssetIdentifier,

ironfish-rust/src/transaction/outputs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl OutputBuilder {
131131
///
132132
/// This is the variation of an Output that gets serialized to bytes and can
133133
/// be loaded from bytes.
134-
#[derive(Clone, Debug)]
134+
#[derive(Clone, PartialEq, Debug)]
135135
pub struct OutputDescription {
136136
/// Proof that the output circuit was valid and successful
137137
pub(crate) proof: groth16::Proof<Bls12>,

0 commit comments

Comments
 (0)