Skip to content

Commit

Permalink
Fix some PSKT typings and ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
KaffinPX committed Sep 30, 2024
1 parent 254cd93 commit 408b070
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions wallet/pskt/src/wasm/pskt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl PSKT {
Some(payload) => {
PSKT::try_owned_from(payload.unchecked_into::<JsValue>().as_ref()).map_err(|err| Error::Ctor(err.to_string()))
}
None => Ok(PSKT::from(State::NoOp(Some(Inner::default())))),
None => Ok(PSKT::from(State::NoOp(None))),
}
}

Expand All @@ -126,14 +126,14 @@ impl PSKT {
}

#[wasm_bindgen(getter, js_name = "payload")]
pub fn payload(&self) -> JsValue { // TODO: correctly typing
pub fn payload(&self) -> JsValue {
// TODO: correctly typing
let state = self.state();
serde_wasm_bindgen::to_value(state.as_ref().unwrap()).unwrap()
}

/// Changes role to `CREATOR`. This initializes a PSKT in the Creator role,
/// which is responsible for generating a new transaction without any signatures.
/// The creator typically defines the transaction inputs and outputs.
#[wasm_bindgen(js_name = "toCreator")]
pub fn creator(&self) -> Result<PSKT> {
let state = match self.take() {
Expand All @@ -151,6 +151,7 @@ impl PSKT {
/// adding the necessary witness data, scripts, or other PSKT fields required
/// to build the transaction. This role extends the creation phase, filling in
/// additional transaction details.
/// The constructor typically defines the transaction inputs and outputs.
#[wasm_bindgen(js_name = "toConstructor")]
pub fn constructor(&self) -> Result<PSKT> {
let state = match self.take() {
Expand All @@ -162,6 +163,28 @@ impl PSKT {
self.replace(state)
}

#[wasm_bindgen(js_name = "addInput")]
pub fn input(&self, input: &TransactionInputT) -> Result<PSKT> {
let input = TransactionInput::try_owned_from(input)?;
let state = match self.take() {
State::Constructor(pskt) => State::Constructor(pskt.input(input.try_into()?)),
state => Err(Error::state(state))?,
};

self.replace(state)
}

#[wasm_bindgen(js_name = "addOutput")]
pub fn output(&self, output: &TransactionOutputT) -> Result<PSKT> {
let output = TransactionOutput::try_owned_from(output)?;
let state = match self.take() {
State::Constructor(pskt) => State::Constructor(pskt.output(output.try_into()?)),
state => Err(Error::state(state))?,
};

self.replace(state)
}

/// Changes role to `UPDATER`. The updater is responsible for filling in more
/// specific information into the PSKT, such as completing any missing fields
/// like sequence, and ensuring inputs are correctly referenced.
Expand All @@ -176,7 +199,7 @@ impl PSKT {
self.replace(state)
}

#[wasm_bindgen(js_name = setSequence)]
#[wasm_bindgen(js_name = "setSequence")]
pub fn set_sequence(&self, n: u64, input_index: usize) -> Result<PSKT> {
let state = match self.take() {
State::Updater(pskt) => State::Updater(pskt.set_sequence(n, input_index)?),
Expand Down Expand Up @@ -293,7 +316,7 @@ impl PSKT {
self.replace(state)
}

#[wasm_bindgen(js_name = inputsModifiable)]
#[wasm_bindgen(js_name = "inputsModifiable")]
pub fn inputs_modifiable(&self) -> Result<PSKT> {
let state = match self.take() {
State::Creator(pskt) => State::Creator(pskt.inputs_modifiable()),
Expand All @@ -303,7 +326,7 @@ impl PSKT {
self.replace(state)
}

#[wasm_bindgen(js_name = outputsModifiable)]
#[wasm_bindgen(js_name = "outputsModifiable")]
pub fn outputs_modifiable(&self) -> Result<PSKT> {
let state = match self.take() {
State::Creator(pskt) => State::Creator(pskt.outputs_modifiable()),
Expand All @@ -313,7 +336,7 @@ impl PSKT {
self.replace(state)
}

#[wasm_bindgen(js_name = noMoreInputs)]
#[wasm_bindgen(js_name = "noMoreInputs")]
pub fn no_more_inputs(&self) -> Result<PSKT> {
let state = match self.take() {
State::Constructor(pskt) => State::Constructor(pskt.no_more_inputs()),
Expand All @@ -323,7 +346,7 @@ impl PSKT {
self.replace(state)
}

#[wasm_bindgen(js_name = noMoreOutputs)]
#[wasm_bindgen(js_name = "noMoreOutputs")]
pub fn no_more_outputs(&self) -> Result<PSKT> {
let state = match self.take() {
State::Constructor(pskt) => State::Constructor(pskt.no_more_outputs()),
Expand All @@ -333,26 +356,6 @@ impl PSKT {
self.replace(state)
}

pub fn input(&self, input: &TransactionInputT) -> Result<PSKT> {
let input = TransactionInput::try_owned_from(input)?;
let state = match self.take() {
State::Constructor(pskt) => State::Constructor(pskt.input(input.try_into()?)),
state => Err(Error::state(state))?,
};

self.replace(state)
}

pub fn output(&self, output: &TransactionOutputT) -> Result<PSKT> {
let output = TransactionOutput::try_owned_from(output)?;
let state = match self.take() {
State::Constructor(pskt) => State::Constructor(pskt.output(output.try_into()?)),
state => Err(Error::state(state))?,
};

self.replace(state)
}

fn state(&self) -> MutexGuard<Option<State>> {
self.state.lock().unwrap()
}
Expand Down

0 comments on commit 408b070

Please sign in to comment.