ergo_lib_wasm/
input.rs

1//! Ergo input
2
3use crate::context_extension::ContextExtension;
4use crate::ergo_box::BoxId;
5use crate::prover_result;
6use ergo_lib::chain;
7use wasm_bindgen::prelude::*;
8
9extern crate derive_more;
10use derive_more::{From, Into};
11
12/// Unsigned inputs used in constructing unsigned transactions
13#[wasm_bindgen]
14#[derive(PartialEq, Eq, Debug, Clone, From, Into)]
15pub struct UnsignedInput(chain::transaction::UnsignedInput);
16
17#[wasm_bindgen]
18impl UnsignedInput {
19    /// Create new unsigned input instance from box id and extension
20    #[wasm_bindgen(constructor)]
21    pub fn new(box_id: &BoxId, ext: &ContextExtension) -> Self {
22        UnsignedInput(chain::transaction::UnsignedInput {
23            box_id: box_id.clone().into(),
24            extension: ext.clone().into(),
25        })
26    }
27
28    /// Create a new unsigned input from the provided box id
29    /// using an empty context extension
30    pub fn from_box_id(box_id: &BoxId) -> Self {
31        UnsignedInput(chain::transaction::UnsignedInput {
32            box_id: box_id.clone().into(),
33            extension: ContextExtension::new().into(),
34        })
35    }
36
37    /// Get box id
38    pub fn box_id(&self) -> BoxId {
39        self.0.box_id.into()
40    }
41
42    /// Get extension
43    pub fn extension(&self) -> ContextExtension {
44        self.0.extension.clone().into()
45    }
46}
47
48/// Collection of unsigned signed inputs
49#[wasm_bindgen]
50#[derive(PartialEq, Eq, Debug, Clone)]
51pub struct UnsignedInputs(Vec<UnsignedInput>);
52
53#[wasm_bindgen]
54impl UnsignedInputs {
55    /// Create empty UnsignedInputs
56    #[wasm_bindgen(constructor)]
57    pub fn new() -> Self {
58        UnsignedInputs(vec![])
59    }
60
61    /// Returns the number of elements in the collection
62    pub fn len(&self) -> usize {
63        self.0.len()
64    }
65
66    /// Returns the element of the collection with a given index
67    pub fn get(&self, index: usize) -> UnsignedInput {
68        self.0[index].clone()
69    }
70
71    /// Add an element to the collection
72    pub fn add(&mut self, b: &UnsignedInput) {
73        self.0.push(b.clone());
74    }
75}
76
77impl From<&UnsignedInputs> for Vec<chain::transaction::UnsignedInput> {
78    fn from(v: &UnsignedInputs) -> Self {
79        v.0.clone().iter().map(|i| i.0.clone()).collect()
80    }
81}
82impl From<Vec<chain::transaction::UnsignedInput>> for UnsignedInputs {
83    fn from(v: Vec<chain::transaction::UnsignedInput>) -> Self {
84        UnsignedInputs(v.into_iter().map(UnsignedInput::from).collect())
85    }
86}
87
88/// Signed inputs used in signed transactions
89#[wasm_bindgen]
90#[derive(PartialEq, Eq, Debug, Clone, From, Into)]
91pub struct Input(chain::transaction::Input);
92
93#[wasm_bindgen]
94impl Input {
95    /// Get box id
96    pub fn box_id(&self) -> BoxId {
97        self.0.box_id.into()
98    }
99
100    /// Get the spending proof
101    pub fn spending_proof(&self) -> prover_result::ProverResult {
102        self.0.spending_proof.clone().into()
103    }
104}
105
106/// Collection of signed inputs
107#[wasm_bindgen]
108#[derive(PartialEq, Eq, Debug, Clone)]
109pub struct Inputs(Vec<Input>);
110
111#[wasm_bindgen]
112impl Inputs {
113    /// Create empty Inputs
114    #[wasm_bindgen(constructor)]
115    pub fn new() -> Self {
116        Inputs(vec![])
117    }
118
119    /// Returns the number of elements in the collection
120    pub fn len(&self) -> usize {
121        self.0.len()
122    }
123
124    /// Returns the element of the collection with a given index
125    pub fn get(&self, index: usize) -> Input {
126        self.0[index].clone()
127    }
128}
129
130impl From<&Inputs> for Vec<chain::transaction::Input> {
131    fn from(v: &Inputs) -> Self {
132        v.0.clone().iter().map(|i| i.0.clone()).collect()
133    }
134}
135impl From<Vec<chain::transaction::Input>> for Inputs {
136    fn from(v: Vec<chain::transaction::Input>) -> Self {
137        Inputs(v.into_iter().map(Input::from).collect())
138    }
139}