|
| 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, AssetIdentifier}, |
| 7 | + circuits::{MintCircuit, OutputCircuit, SpendCircuit, TransactionCircuits}, |
| 8 | + errors::IronfishError, |
| 9 | + keys::{PublicAddress, SaplingKey, ViewKey}, |
| 10 | + note::Note, |
| 11 | + primitives::Fr, |
| 12 | + transaction::Transaction, |
| 13 | + wasm_bindgen_wrapper, |
| 14 | + witness::Witness, |
| 15 | +}; |
| 16 | +use ironfish::transaction::TransactionVersion; |
| 17 | +use wasm_bindgen::prelude::*; |
| 18 | + |
| 19 | +wasm_bindgen_wrapper! { |
| 20 | + pub struct ProposedTransaction(ironfish::ProposedTransaction); |
| 21 | +} |
| 22 | + |
| 23 | +#[wasm_bindgen] |
| 24 | +impl ProposedTransaction { |
| 25 | + #[wasm_bindgen(constructor)] |
| 26 | + pub fn new() -> Self { |
| 27 | + Self(ironfish::ProposedTransaction::new(TransactionVersion::V1)) |
| 28 | + } |
| 29 | + |
| 30 | + #[wasm_bindgen(js_name = addSpend)] |
| 31 | + pub fn add_spend(&mut self, note: Note, witness: &Witness) -> Result<(), IronfishError> { |
| 32 | + self.0.add_spend(note.into(), witness.as_ref())?; |
| 33 | + Ok(()) |
| 34 | + } |
| 35 | + |
| 36 | + #[wasm_bindgen(js_name = addOutput)] |
| 37 | + pub fn add_output(&mut self, note: Note) -> Result<(), IronfishError> { |
| 38 | + self.0.add_output(note.into())?; |
| 39 | + Ok(()) |
| 40 | + } |
| 41 | + |
| 42 | + #[wasm_bindgen(js_name = addMint)] |
| 43 | + pub fn add_mint(&mut self, asset: Asset, value: u64) -> Result<(), IronfishError> { |
| 44 | + self.0.add_mint(asset.into(), value)?; |
| 45 | + Ok(()) |
| 46 | + } |
| 47 | + |
| 48 | + #[wasm_bindgen(js_name = addBurn)] |
| 49 | + pub fn add_burn(&mut self, asset_id: AssetIdentifier, value: u64) -> Result<(), IronfishError> { |
| 50 | + self.0.add_burn(asset_id.into(), value)?; |
| 51 | + Ok(()) |
| 52 | + } |
| 53 | + |
| 54 | + #[wasm_bindgen(getter)] |
| 55 | + pub fn expiration(&self) -> u32 { |
| 56 | + self.0.expiration() |
| 57 | + } |
| 58 | + |
| 59 | + #[wasm_bindgen(setter, js_name = expiration)] |
| 60 | + pub fn set_expiration(&mut self, sequence: u32) { |
| 61 | + self.0.set_expiration(sequence); |
| 62 | + } |
| 63 | + |
| 64 | + #[wasm_bindgen(js_name = buildCircuits)] |
| 65 | + pub fn build_circuits( |
| 66 | + &mut self, |
| 67 | + proof_authorizing_key: Fr, |
| 68 | + view_key: ViewKey, |
| 69 | + intended_transaction_fee: i64, |
| 70 | + change_goes_to: Option<PublicAddress>, |
| 71 | + ) -> Result<TransactionCircuits, IronfishError> { |
| 72 | + let (spend_circuits, output_circuits, mint_circuits) = self.0.build_circuits( |
| 73 | + proof_authorizing_key.into(), |
| 74 | + view_key.into(), |
| 75 | + intended_transaction_fee, |
| 76 | + change_goes_to.map(PublicAddress::into), |
| 77 | + )?; |
| 78 | + |
| 79 | + let spend_circuits = spend_circuits.into_iter().map(SpendCircuit::from).collect(); |
| 80 | + let output_circuits = output_circuits |
| 81 | + .into_iter() |
| 82 | + .map(OutputCircuit::from) |
| 83 | + .collect(); |
| 84 | + let mint_circuits = mint_circuits.into_iter().map(MintCircuit::from).collect(); |
| 85 | + Ok(TransactionCircuits { |
| 86 | + spend_circuits, |
| 87 | + output_circuits, |
| 88 | + mint_circuits, |
| 89 | + }) |
| 90 | + } |
| 91 | + |
| 92 | + #[wasm_bindgen] |
| 93 | + pub fn post( |
| 94 | + &mut self, |
| 95 | + spender_key: &SaplingKey, |
| 96 | + change_goes_to: Option<PublicAddress>, |
| 97 | + intended_transaction_fee: u64, |
| 98 | + ) -> Result<Transaction, IronfishError> { |
| 99 | + self.0 |
| 100 | + .post( |
| 101 | + spender_key.as_ref(), |
| 102 | + change_goes_to.map(PublicAddress::into), |
| 103 | + intended_transaction_fee, |
| 104 | + ) |
| 105 | + .map(Transaction::from) |
| 106 | + .map_err(IronfishError::from) |
| 107 | + } |
| 108 | + |
| 109 | + #[wasm_bindgen(js_name = postMinersFee)] |
| 110 | + pub fn post_miners_fee( |
| 111 | + &mut self, |
| 112 | + spender_key: &SaplingKey, |
| 113 | + ) -> Result<Transaction, IronfishError> { |
| 114 | + self.0 |
| 115 | + .post_miners_fee(spender_key.as_ref()) |
| 116 | + .map(Transaction::from) |
| 117 | + .map_err(IronfishError::from) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl Default for ProposedTransaction { |
| 122 | + fn default() -> Self { |
| 123 | + Self::new() |
| 124 | + } |
| 125 | +} |
0 commit comments