Skip to content

Commit f57a007

Browse files
committed
WASM: add bindings for unsigned transactions and descriptions
Also add bindings to generate transaction signatures
1 parent aab7abe commit f57a007

File tree

7 files changed

+399
-8
lines changed

7 files changed

+399
-8
lines changed

ironfish-rust-wasm/src/transaction/mints.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use crate::{
66
assets::Asset,
77
errors::IronfishError,
8-
keys::PublicAddress,
9-
primitives::{PublicKey, Scalar},
8+
keys::{PublicAddress, SaplingKey},
9+
primitives::{PublicKey, Scalar, Signature},
1010
wasm_bindgen_wrapper,
1111
};
1212
use ironfish::{errors::IronfishErrorKind, transaction::TransactionVersion};
@@ -79,3 +79,50 @@ impl MintDescription {
7979
.collect()
8080
}
8181
}
82+
83+
wasm_bindgen_wrapper! {
84+
#[derive(Clone, Debug)]
85+
pub struct UnsignedMintDescription(ironfish::transaction::mints::UnsignedMintDescription);
86+
}
87+
88+
#[wasm_bindgen]
89+
impl UnsignedMintDescription {
90+
#[wasm_bindgen(constructor)]
91+
pub fn deserialize(bytes: &[u8]) -> Result<Self, IronfishError> {
92+
Ok(Self(
93+
ironfish::transaction::mints::UnsignedMintDescription::read(
94+
bytes,
95+
TransactionVersion::V1,
96+
)?,
97+
))
98+
}
99+
100+
#[wasm_bindgen]
101+
pub fn serialize(&self) -> Vec<u8> {
102+
let mut buf = Vec::new();
103+
self.0
104+
.write(&mut buf, TransactionVersion::V1)
105+
.expect("failed to serialize unsigned mint description");
106+
buf
107+
}
108+
109+
#[wasm_bindgen]
110+
pub fn sign(
111+
self,
112+
spender_key: &SaplingKey,
113+
signature_hash: &[u8],
114+
) -> Result<MintDescription, IronfishError> {
115+
let signature_hash: &[u8; 32] = signature_hash
116+
.try_into()
117+
.map_err(|_| IronfishErrorKind::InvalidData)?;
118+
self.0
119+
.sign(spender_key.as_ref(), signature_hash)
120+
.map(|d| d.into())
121+
.map_err(|e| e.into())
122+
}
123+
124+
#[wasm_bindgen(js_name = addSignature)]
125+
pub fn add_signature(self, signature: Signature) -> MintDescription {
126+
self.0.add_signature(signature.into()).into()
127+
}
128+
}

ironfish-rust-wasm/src/transaction/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ mod burns;
66
mod mints;
77
mod outputs;
88
mod spends;
9+
mod unsigned;
910

1011
use crate::{errors::IronfishError, primitives::PublicKey, wasm_bindgen_wrapper};
1112
use wasm_bindgen::prelude::*;
1213

1314
pub use burns::BurnDescription;
14-
pub use mints::MintDescription;
15+
pub use mints::{MintDescription, UnsignedMintDescription};
1516
pub use outputs::OutputDescription;
16-
pub use spends::SpendDescription;
17+
pub use spends::{SpendDescription, UnsignedSpendDescription};
18+
pub use unsigned::UnsignedTransaction;
1719

1820
wasm_bindgen_wrapper! {
1921
#[derive(Clone, Debug)]

ironfish-rust-wasm/src/transaction/spends.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
use crate::{
66
errors::IronfishError,
7-
primitives::{Nullifier, PublicKey, Scalar},
7+
keys::SaplingKey,
8+
primitives::{Nullifier, PublicKey, Scalar, Signature},
89
wasm_bindgen_wrapper,
910
};
1011
use ironfish::errors::IronfishErrorKind;
@@ -74,3 +75,47 @@ impl SpendDescription {
7475
.collect()
7576
}
7677
}
78+
79+
wasm_bindgen_wrapper! {
80+
#[derive(Clone, Debug)]
81+
pub struct UnsignedSpendDescription(ironfish::transaction::spends::UnsignedSpendDescription);
82+
}
83+
84+
#[wasm_bindgen]
85+
impl UnsignedSpendDescription {
86+
#[wasm_bindgen(constructor)]
87+
pub fn deserialize(bytes: &[u8]) -> Result<Self, IronfishError> {
88+
Ok(Self(
89+
ironfish::transaction::spends::UnsignedSpendDescription::read(bytes)?,
90+
))
91+
}
92+
93+
#[wasm_bindgen]
94+
pub fn serialize(&self) -> Vec<u8> {
95+
let mut buf = Vec::new();
96+
self.0
97+
.write(&mut buf)
98+
.expect("failed to serialize unsigned spend description");
99+
buf
100+
}
101+
102+
#[wasm_bindgen]
103+
pub fn sign(
104+
self,
105+
spender_key: &SaplingKey,
106+
signature_hash: &[u8],
107+
) -> Result<SpendDescription, IronfishError> {
108+
let signature_hash: &[u8; 32] = signature_hash
109+
.try_into()
110+
.map_err(|_| IronfishErrorKind::InvalidData)?;
111+
self.0
112+
.sign(spender_key.as_ref(), signature_hash)
113+
.map(|d| d.into())
114+
.map_err(|e| e.into())
115+
}
116+
117+
#[wasm_bindgen(js_name = addSignature)]
118+
pub fn add_signature(self, signature: Signature) -> SpendDescription {
119+
self.0.add_signature(signature.into()).into()
120+
}
121+
}

0 commit comments

Comments
 (0)