Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM: add bindings for unsigned transactions and descriptions #5674

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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