ergo_lib_wasm/
data_input.rs

1//! DataInput type
2
3use crate::ergo_box::BoxId;
4use ergo_lib::chain;
5use wasm_bindgen::prelude::*;
6
7extern crate derive_more;
8use derive_more::{From, Into};
9
10/// Inputs, that are used to enrich script context, but won't be spent by the transaction
11#[wasm_bindgen]
12#[derive(PartialEq, Eq, Debug, Clone, From, Into)]
13pub struct DataInput(chain::transaction::DataInput);
14
15#[wasm_bindgen]
16impl DataInput {
17    /// Parse box id (32 byte digest) from base16-encoded string
18    #[wasm_bindgen(constructor)]
19    pub fn new(box_id: BoxId) -> Self {
20        chain::transaction::DataInput {
21            box_id: box_id.into(),
22        }
23        .into()
24    }
25
26    /// Get box id
27    pub fn box_id(&self) -> BoxId {
28        self.0.box_id.into()
29    }
30}
31
32/// DataInput collection
33#[wasm_bindgen]
34pub struct DataInputs(Vec<DataInput>);
35
36#[wasm_bindgen]
37impl DataInputs {
38    /// Create empty DataInputs
39    #[wasm_bindgen(constructor)]
40    pub fn new() -> Self {
41        DataInputs(vec![])
42    }
43
44    /// Returns the number of elements in the collection
45    pub fn len(&self) -> usize {
46        self.0.len()
47    }
48
49    /// Returns the element of the collection with a given index
50    pub fn get(&self, index: usize) -> DataInput {
51        self.0[index].clone()
52    }
53
54    /// Adds an elements to the collection
55    pub fn add(&mut self, elem: &DataInput) {
56        self.0.push(elem.clone());
57    }
58}
59
60impl From<&DataInputs> for Vec<chain::transaction::DataInput> {
61    fn from(v: &DataInputs) -> Self {
62        v.0.clone().iter().map(|i| i.0.clone()).collect()
63    }
64}
65impl From<Vec<chain::transaction::DataInput>> for DataInputs {
66    fn from(v: Vec<chain::transaction::DataInput>) -> Self {
67        DataInputs(v.into_iter().map(DataInput::from).collect())
68    }
69}