Skip to content

Commit

Permalink
WASM: add bindings for unsigned transactions and descriptions
Browse files Browse the repository at this point in the history
Also add bindings to generate transaction signatures
  • Loading branch information
andiflabs committed Nov 26, 2024
1 parent 971ad1e commit 2d46bc6
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 8 deletions.
51 changes: 49 additions & 2 deletions ironfish-rust-wasm/src/transaction/mints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use crate::{
assets::Asset,
errors::IronfishError,
keys::PublicAddress,
primitives::{PublicKey, Scalar},
keys::{PublicAddress, SaplingKey},
primitives::{PublicKey, Scalar, Signature},
wasm_bindgen_wrapper,
};
use ironfish::{errors::IronfishErrorKind, transaction::TransactionVersion};
Expand Down Expand Up @@ -79,3 +79,50 @@ impl MintDescription {
.collect()
}
}

wasm_bindgen_wrapper! {
#[derive(Clone, Debug)]
pub struct UnsignedMintDescription(ironfish::transaction::mints::UnsignedMintDescription);
}

#[wasm_bindgen]
impl UnsignedMintDescription {
#[wasm_bindgen(constructor)]
pub fn deserialize(bytes: &[u8]) -> Result<Self, IronfishError> {
Ok(Self(
ironfish::transaction::mints::UnsignedMintDescription::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 unsigned mint description");
buf
}

#[wasm_bindgen]
pub fn sign(
self,
spender_key: &SaplingKey,
signature_hash: &[u8],
) -> Result<MintDescription, IronfishError> {
let signature_hash: &[u8; 32] = signature_hash
.try_into()
.map_err(|_| IronfishErrorKind::InvalidData)?;
self.0
.sign(spender_key.as_ref(), signature_hash)
.map(|d| d.into())
.map_err(|e| e.into())
}

#[wasm_bindgen(js_name = addSignature)]
pub fn add_signature(self, signature: Signature) -> MintDescription {
self.0.add_signature(signature.into()).into()
}
}
6 changes: 4 additions & 2 deletions ironfish-rust-wasm/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ mod burns;
mod mints;
mod outputs;
mod spends;
mod unsigned;

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

pub use burns::BurnDescription;
pub use mints::MintDescription;
pub use mints::{MintDescription, UnsignedMintDescription};
pub use outputs::OutputDescription;
pub use spends::SpendDescription;
pub use spends::{SpendDescription, UnsignedSpendDescription};
pub use unsigned::UnsignedTransaction;

wasm_bindgen_wrapper! {
#[derive(Clone, Debug)]
Expand Down
47 changes: 46 additions & 1 deletion ironfish-rust-wasm/src/transaction/spends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use crate::{
errors::IronfishError,
primitives::{Nullifier, PublicKey, Scalar},
keys::SaplingKey,
primitives::{Nullifier, PublicKey, Scalar, Signature},
wasm_bindgen_wrapper,
};
use ironfish::errors::IronfishErrorKind;
Expand Down Expand Up @@ -74,3 +75,47 @@ impl SpendDescription {
.collect()
}
}

wasm_bindgen_wrapper! {
#[derive(Clone, Debug)]
pub struct UnsignedSpendDescription(ironfish::transaction::spends::UnsignedSpendDescription);
}

#[wasm_bindgen]
impl UnsignedSpendDescription {
#[wasm_bindgen(constructor)]
pub fn deserialize(bytes: &[u8]) -> Result<Self, IronfishError> {
Ok(Self(
ironfish::transaction::spends::UnsignedSpendDescription::read(bytes)?,
))
}

#[wasm_bindgen]
pub fn serialize(&self) -> Vec<u8> {
let mut buf = Vec::new();
self.0
.write(&mut buf)
.expect("failed to serialize unsigned spend description");
buf
}

#[wasm_bindgen]
pub fn sign(
self,
spender_key: &SaplingKey,
signature_hash: &[u8],
) -> Result<SpendDescription, IronfishError> {
let signature_hash: &[u8; 32] = signature_hash
.try_into()
.map_err(|_| IronfishErrorKind::InvalidData)?;
self.0
.sign(spender_key.as_ref(), signature_hash)
.map(|d| d.into())
.map_err(|e| e.into())
}

#[wasm_bindgen(js_name = addSignature)]
pub fn add_signature(self, signature: Signature) -> SpendDescription {
self.0.add_signature(signature.into()).into()
}
}
Loading

0 comments on commit 2d46bc6

Please sign in to comment.