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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use std::io::Cursor;
use std::io::Read;
use std::io::Write;
use crate::{Script, VarInt, utils::{to_hex, from_hex}};
use wasm_bindgen::{JsValue, prelude::*, throw_str};
use serde::*;
use snafu::*;
use anyhow::*;
use byteorder::*;
#[derive(Debug, Snafu)]
pub enum TxInErrors {
#[snafu(display("Error deserialising TxIn field {:?}: {}", field, error))]
Deserialise {
field: Option<String>,
error: anyhow::Error
},
#[snafu(display("Error serialising TxIn field {:?}: {}", field, error))]
Serialise {
field: Option<String>,
error: anyhow::Error
},
}
#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TxIn {
#[serde(serialize_with = "to_hex", deserialize_with = "from_hex")]
prev_tx_id: Vec<u8>,
vout: u32,
script_sig: Script,
sequence: u32,
}
impl From<JsValue> for TxIn {
fn from<'a>(x: JsValue) -> Self {
match x.into_serde::<TxIn>() {
Ok(v) => v,
Err(_) => TxIn{prev_tx_id: vec![], script_sig: Script::default(), sequence: 0, vout:0}
}
}
}
impl TxIn {
pub(crate) fn from_hex_impl(hex_str: String) -> Result<TxIn, TxInErrors> {
let txin_bytes = match hex::decode(&hex_str) {
Ok(v) => v,
Err(e) => return Err(TxInErrors::Deserialise { field: None, error: anyhow!(e) }),
};
let mut cursor = Cursor::new(txin_bytes);
TxIn::read_in(&mut cursor)
}
pub(crate) fn read_in(
cursor: &mut Cursor<Vec<u8>>
) -> Result<TxIn, TxInErrors> {
let mut prev_tx_id = vec![0; 32];
match cursor.read(&mut prev_tx_id) {
Err(e) => return Err(TxInErrors::Deserialise { field: Some("prev_tx_id".to_string()), error: anyhow!(e) }),
Ok(0) => return Err(TxInErrors::Deserialise { field: Some("prev_tx_id".to_string()), error: anyhow!("Read zero bytes for Prev TX Id!") }),
Ok(v) => ()
};
prev_tx_id.reverse();
let vout = match cursor.read_u32::<LittleEndian>() {
Ok(v) => v,
Err(e) => return Err(TxInErrors::Deserialise { field: Some("vout".to_string()), error: anyhow!(e) })
};
let script_sig_size = match cursor.read_varint() {
Ok(v) => v,
Err(e) => return Err(TxInErrors::Deserialise { field: Some("script_sig_size".to_string()), error: anyhow!(e) }),
};
let mut script_sig = vec![0; script_sig_size as usize];
match cursor.read(&mut script_sig) {
Err(e) => return Err(TxInErrors::Deserialise { field: Some("script_sig".to_string()), error: anyhow!(e) }),
_ => ()
};
let sequence = match cursor.read_u32::<LittleEndian>() {
Ok(v) => v,
Err(e) => return Err(TxInErrors::Deserialise { field: Some("sequence".to_string()), error: anyhow!(e) })
};
Ok(TxIn {
prev_tx_id,
vout,
script_sig: Script(script_sig),
sequence,
})
}
pub(crate) fn to_bytes_impl(&self) -> Result<Vec<u8>, TxInErrors> {
let mut cursor = Cursor::new(Vec::new());
let mut txid = self.prev_tx_id.clone();
txid.reverse();
match cursor.write(&txid) {
Err(e) => return Err(TxInErrors::Serialise { field: Some("prev_tx_id".to_string()), error: anyhow!(e) }),
Ok(0) => return Err(TxInErrors::Serialise { field: Some("prev_tx_id".to_string()), error: anyhow!("Wrote zero bytes for Prev TX Id!") }),
Ok(_) => ()
};
match cursor.write_u32::<LittleEndian>(self.vout) {
Err(e) => return Err(TxInErrors::Serialise { field: Some("vout".to_string()), error: anyhow!(e) }),
_ => ()
};
match cursor.write_varint(self.get_script_sig_size()) {
Ok(v) => v,
Err(e) => return Err(TxInErrors::Serialise { field: Some("script_sig_size".to_string()), error: anyhow!(e) }),
};
match cursor.write(&self.script_sig.0) {
Err(e) => return Err(TxInErrors::Serialise { field: Some("script_sig".to_string()), error: anyhow!(e) }),
_ => ()
};
match cursor.write_u32::<LittleEndian>(self.sequence) {
Ok(v) => v,
Err(e) => return Err(TxInErrors::Serialise { field: Some("sequence".to_string()), error: anyhow!(e) })
};
let mut bytes: Vec<u8> = Vec::new();
cursor.set_position(0);
match cursor.read_to_end(&mut bytes) {
Err(e) => return Err(TxInErrors::Serialise{ field: None, error: anyhow!(e) }),
_ => ()
};
Ok(bytes)
}
pub(crate) fn to_hex_impl(&self) -> Result<String, TxInErrors> {
Ok(hex::encode(&self.to_bytes_impl()?))
}
}
#[wasm_bindgen]
impl TxIn {
#[wasm_bindgen(constructor)]
pub fn new(
prev_tx_id: Vec<u8>,
vout: u32,
script_sig: Vec<u8>,
sequence: u32,
) -> TxIn {
TxIn {
prev_tx_id,
vout,
script_sig: Script(script_sig),
sequence,
}
}
#[wasm_bindgen(js_name = getPrevTxId)]
pub fn get_prev_tx_id(&self) -> Vec<u8> {
self.prev_tx_id.clone()
}
#[wasm_bindgen(js_name = getPrevTxIdHex)]
pub fn get_prev_tx_id_hex(&self) -> String {
hex::encode(self.prev_tx_id.clone())
}
#[wasm_bindgen(js_name = getVOut)]
pub fn get_vout(&self) -> u32 {
self.vout
}
#[wasm_bindgen(js_name = getScriptSigSize)]
pub fn get_script_sig_size(&self) -> u64 {
self.script_sig.0.len() as u64
}
#[wasm_bindgen(js_name = getScriptSig)]
pub fn get_script_sig(&self) -> Script {
self.script_sig.clone()
}
#[wasm_bindgen(js_name = getScriptSigHex)]
pub fn get_script_sig_hex(&self) -> String {
hex::encode(self.script_sig.0.clone())
}
#[wasm_bindgen(js_name = getSequence)]
pub fn get_sequence(&self) -> u32 {
self.sequence
}
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
impl TxIn {
#[wasm_bindgen(js_name = fromHex)]
pub fn from_hex(hex_str: String) -> Result<TxIn, JsValue> {
match TxIn::from_hex_impl(hex_str) {
Ok(v) => Ok(v),
Err(e) => throw_str(&e.to_string()),
}
}
#[wasm_bindgen(js_name = toBuffer)]
pub fn to_bytes(&self) -> Result<Vec<u8>, JsValue> {
match TxIn::to_bytes_impl(&self) {
Ok(v) => Ok(v),
Err(e) => throw_str(&e.to_string()),
}
}
#[wasm_bindgen(js_name = toHex)]
pub fn to_hex(&self) -> Result<String, JsValue> {
match TxIn::to_hex_impl(&self) {
Ok(v) => Ok(v),
Err(e) => throw_str(&e.to_string()),
}
}
}
#[cfg(not(target_arch = "wasm32"))]
impl TxIn {
pub fn from_hex(hex_str: String) -> Result<TxIn, TxInErrors> {
TxIn::from_hex_impl(hex_str)
}
pub fn to_bytes(&self) -> Result<Vec<u8>, TxInErrors> {
TxIn::to_bytes_impl(&self)
}
pub fn to_hex(&self) -> Result<String, TxInErrors> {
TxIn::to_hex_impl(&self)
}
}