1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use crate::box_coll::ErgoBoxCandidates;
use crate::data_input::DataInputs;
use crate::input::{Inputs, UnsignedInputs};
use ergo_lib::chain;
use std::convert::TryFrom;
use std::convert::TryInto;
use wasm_bindgen::prelude::*;
extern crate derive_more;
use derive_more::{From, Into};
#[wasm_bindgen]
#[derive(PartialEq, Eq, Debug, Clone, From, Into)]
pub struct TxId(chain::transaction::TxId);
#[wasm_bindgen]
impl TxId {
pub fn zero() -> TxId {
chain::transaction::TxId::zero().into()
}
pub fn to_str(&self) -> String {
let base16_bytes = ergo_lib::chain::Base16EncodedBytes::new(self.0 .0 .0.as_ref());
base16_bytes.into()
}
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &str) -> Result<TxId, JsValue> {
let bytes = ergo_lib::chain::Base16DecodedBytes::try_from(s.to_string())
.map_err(|e| JsValue::from_str(&format!("{}", e)))?;
bytes
.try_into()
.map(|digest| chain::transaction::TxId(digest).into())
.map_err(|_e| {
JsValue::from_str(&format!(
"Expected a Vec of length {} but it was {}",
chain::Digest32::SIZE,
s.len()
))
})
}
}
#[wasm_bindgen]
pub struct Transaction(chain::transaction::Transaction);
#[wasm_bindgen]
impl Transaction {
pub fn id(&self) -> TxId {
self.0.id().into()
}
pub fn to_json(&self) -> Result<JsValue, JsValue> {
JsValue::from_serde(&self.0.clone()).map_err(|e| JsValue::from_str(&format!("{}", e)))
}
pub fn inputs(&self) -> Inputs {
self.0.inputs.clone().into()
}
pub fn data_inputs(&self) -> DataInputs {
self.0.data_inputs.clone().into()
}
pub fn outputs(&self) -> ErgoBoxCandidates {
self.0.output_candidates.clone().into()
}
}
impl From<chain::transaction::Transaction> for Transaction {
fn from(t: chain::transaction::Transaction) -> Self {
Transaction(t)
}
}
#[wasm_bindgen]
#[derive(PartialEq, Debug, Clone)]
pub struct UnsignedTransaction(chain::transaction::unsigned::UnsignedTransaction);
#[wasm_bindgen]
impl UnsignedTransaction {
pub fn id(&self) -> TxId {
self.0.id().into()
}
pub fn inputs(&self) -> UnsignedInputs {
self.0.inputs.clone().into()
}
pub fn data_inputs(&self) -> DataInputs {
self.0.data_inputs.clone().into()
}
pub fn outputs(&self) -> ErgoBoxCandidates {
self.0.output_candidates.clone().into()
}
pub fn to_json(&self) -> Result<JsValue, JsValue> {
JsValue::from_serde(&self.0.clone()).map_err(|e| JsValue::from_str(&format!("{}", e)))
}
}
impl From<chain::transaction::unsigned::UnsignedTransaction> for UnsignedTransaction {
fn from(t: chain::transaction::unsigned::UnsignedTransaction) -> Self {
UnsignedTransaction(t)
}
}
impl From<UnsignedTransaction> for chain::transaction::unsigned::UnsignedTransaction {
fn from(t: UnsignedTransaction) -> Self {
t.0
}
}