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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use crate::BSVErrors;
use std::io::Cursor;
use std::io::Read;
use std::io::Write;
use crate::{
utils::{from_hex, to_hex},
Script, VarInt,
};
use serde::*;
use wasm_bindgen::{prelude::*, throw_str, JsValue};
use byteorder::*;
use thiserror::*;
#[wasm_bindgen]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TxIn {
#[serde(serialize_with = "to_hex", deserialize_with = "from_hex")]
pub(crate) prev_tx_id: Vec<u8>,
pub(crate) vout: u32,
pub(crate) script_sig: Script,
pub(crate) sequence: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) unlocking_script: Option<Script>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) satoshis: Option<u64>,
}
impl TxIn {
pub(crate) fn from_hex_impl(hex_str: &str) -> Result<TxIn, BSVErrors> {
let txin_bytes = hex::decode(hex_str)?;
let mut cursor = Cursor::new(txin_bytes);
TxIn::read_in(&mut cursor)
}
pub(crate) fn read_in(cursor: &mut Cursor<Vec<u8>>) -> Result<TxIn, BSVErrors> {
let mut prev_tx_id = vec![0; 32];
if let Err(e) = cursor.read(&mut prev_tx_id) {
return Err(BSVErrors::DeserialiseTxIn("prev_tx_id".to_string(), e));
}
prev_tx_id.reverse();
let vout = match cursor.read_u32::<LittleEndian>() {
Ok(v) => v,
Err(e) => return Err(BSVErrors::DeserialiseTxIn("vout".to_string(), e)),
};
let script_sig_size = match cursor.read_varint() {
Ok(v) => v,
Err(e) => return Err(BSVErrors::DeserialiseTxIn("script_sig_size".to_string(), e)),
};
let mut script_sig = vec![0; script_sig_size as usize];
if let Err(e) = cursor.read(&mut script_sig) {
return Err(BSVErrors::DeserialiseTxIn("script_sig".to_string(), e));
}
let sequence = match cursor.read_u32::<LittleEndian>() {
Ok(v) => v,
Err(e) => return Err(BSVErrors::DeserialiseTxIn("sequence".to_string(), e)),
};
Ok(TxIn {
prev_tx_id,
vout,
script_sig: Script(script_sig),
sequence,
satoshis: None,
unlocking_script: None,
})
}
pub(crate) fn to_bytes_impl(&self) -> Result<Vec<u8>, BSVErrors> {
let mut buffer = vec![];
let mut prev_tx_id = self.prev_tx_id.clone();
prev_tx_id.reverse();
if let Err(e) = buffer.write(&prev_tx_id) {
return Err(BSVErrors::SerialiseTxIn("prev_tx_id".to_string(), e));
}
if let Err(e) = buffer.write_u32::<LittleEndian>(self.vout) {
return Err(BSVErrors::SerialiseTxIn("vout".to_string(), e));
}
let finalised_script = match self.unlocking_script.as_ref() {
Some(us) => Script::from_asm_string_impl(&format!("{} {}", us.to_asm_string_impl(false)?, self.script_sig.to_asm_string_impl(false)?))?,
None => self.script_sig.clone(),
};
if let Err(e) = buffer.write_varint(finalised_script.get_script_length() as u64) {
return Err(BSVErrors::SerialiseTxIn("script_sig_size".to_string(), e));
}
if let Err(e) = buffer.write(&finalised_script.0) {
return Err(BSVErrors::SerialiseTxIn("script_sig".to_string(), e));
}
if let Err(e) = buffer.write_u32::<LittleEndian>(self.sequence) {
return Err(BSVErrors::SerialiseTxIn("sequence".to_string(), e));
}
Ok(buffer)
}
pub(crate) fn to_hex_impl(&self) -> Result<String, BSVErrors> {
Ok(hex::encode(&self.to_bytes_impl()?))
}
pub(crate) fn to_json_string_impl(&self) -> Result<String, BSVErrors> {
let json = serde_json::to_string_pretty(self)?;
Ok(json)
}
}
#[wasm_bindgen]
impl TxIn {
#[wasm_bindgen(constructor)]
pub fn new(prev_tx_id: &[u8], vout: u32, script_sig: &Script, sequence: Option<u32>) -> TxIn {
TxIn {
prev_tx_id: prev_tx_id.to_vec(),
vout,
script_sig: script_sig.clone(),
sequence: match sequence {
Some(v) => v,
None => u32::MAX,
},
satoshis: None,
unlocking_script: None,
}
}
#[wasm_bindgen(js_name = getPrevTxId)]
pub fn get_prev_tx_id(&self, little_endian: Option<bool>) -> Vec<u8> {
match little_endian {
Some(true) => {
let mut reversed_tx = self.prev_tx_id.clone();
reversed_tx.reverse();
reversed_tx
}
_ => self.prev_tx_id.clone(),
}
}
#[wasm_bindgen(js_name = getPrevTxIdHex)]
pub fn get_prev_tx_id_hex(&self, little_endian: Option<bool>) -> String {
hex::encode(self.get_prev_tx_id(little_endian))
}
#[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
}
#[wasm_bindgen(js_name = getSequenceAsBytes)]
pub fn get_sequence_as_bytes(&self) -> Vec<u8> {
self.sequence.to_be_bytes().to_vec()
}
#[wasm_bindgen(js_name = getOutpointBytes)]
pub fn get_outpoint_bytes(&self, little_endian: Option<bool>) -> Vec<u8> {
let mut outpoint_bytes = self.get_prev_tx_id(little_endian);
outpoint_bytes.extend_from_slice(&self.vout.to_le_bytes());
outpoint_bytes
}
#[wasm_bindgen(js_name = getOutpointHex)]
pub fn get_outpoint_hex(&self, little_endian: Option<bool>) -> String {
hex::encode(self.get_outpoint_bytes(little_endian))
}
#[wasm_bindgen(js_name = setScript)]
pub fn set_script(&mut self, script: &Script) {
self.script_sig = script.clone();
}
#[wasm_bindgen(js_name = setPrevTxId)]
pub fn set_prev_tx_id(&mut self, txid: &[u8]) {
self.prev_tx_id = txid.to_vec();
}
#[wasm_bindgen(js_name = setVOut)]
pub fn set_vout(&mut self, vout: u32) {
self.vout = vout;
}
#[wasm_bindgen(js_name = setSequence)]
pub fn set_sequence(&mut self, sequence: u32) {
self.sequence = sequence;
}
#[wasm_bindgen(js_name = setSatoshis)]
pub fn set_satoshis(&mut self, satoshis: u64) {
self.satoshis = Some(satoshis);
}
#[wasm_bindgen(js_name = getSatoshis)]
pub fn get_satoshis(&self) -> Option<u64> {
self.satoshis
}
#[wasm_bindgen(js_name = setUnlockingScript)]
pub fn set_unlocking_script(&mut self, unlocking_script: &Script) {
self.unlocking_script = Some(unlocking_script.clone());
}
#[wasm_bindgen(js_name = getUnlockingScriptBytes)]
pub fn get_unlocking_script_bytes(&self) -> Option<Vec<u8>> {
self.unlocking_script.as_ref().map(|v| v.to_bytes())
}
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
impl TxIn {
#[wasm_bindgen(js_name = fromHex)]
pub fn from_hex(hex_str: &str) -> 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 = toJSON)]
pub fn to_json(&self) -> Result<JsValue, JsValue> {
match JsValue::from_serde(&self) {
Ok(v) => Ok(v),
Err(e) => throw_str(&e.to_string()),
}
}
#[wasm_bindgen(js_name = toString)]
pub fn to_json_string(&self) -> Result<String, JsValue> {
match TxIn::to_json_string_impl(&self) {
Ok(v) => Ok(v),
Err(e) => throw_str(&e.to_string()),
}
}
#[wasm_bindgen(js_name = toBytes)]
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: &str) -> Result<TxIn, BSVErrors> {
TxIn::from_hex_impl(hex_str)
}
pub fn to_bytes(&self) -> Result<Vec<u8>, BSVErrors> {
TxIn::to_bytes_impl(self)
}
pub fn to_hex(&self) -> Result<String, BSVErrors> {
TxIn::to_hex_impl(self)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn to_json_string(&self) -> Result<String, BSVErrors> {
TxIn::to_json_string_impl(self)
}
#[cfg(not(target_arch = "wasm32"))]
pub fn to_json(&self) -> Result<serde_json::Value, BSVErrors> {
let json = serde_json::to_value(self)?;
Ok(json)
}
}