You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 12_reading_inputs_and_type_coercion.md
+12-11
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,17 @@
1
1
# Reading Inputs and Type Coercion
2
2
3
3
Each input in a transaction contains the following information:
4
-
* previous output txid (32 bytes length)
5
-
* previous output index (4 bytes length, represented as a u32 integer)
6
-
* ScriptSig (variable length preceded by compact size integer)
7
-
* sequence (4 bytes length, represented as a u32 integer)
4
+
*the previous output txid (32 bytes length)
5
+
*the previous output index (4 bytes length, represented as a u32 integer)
6
+
*a ScriptSig (variable length preceded by compact size integer)
7
+
*a sequence number (4 bytes length, represented as a u32 integer).
8
8
9
-
The ScriptSig can be a variable length and so is preceded by a compact size integer which indicates the length of the field in bytes.
10
-
Prior to Segwit, the ScriptSig was where the signature would be provided for unlocking the funds of the referenced output (as indicated by the previous output txid and previous output index).
9
+
The ScriptSig can be have variable length and so is preceded by a compact size integer which indicates the length of the field in bytes.
10
+
Prior to Segwit, the ScriptSig was where a digital signature would be provided for unlocking the funds of the referenced output (as indicated by the previous output txid and previous output index).
11
11
Now, for Segwit transactions, this field is empty with a compact size length of 0x00 as the signature is no longer contained in the input data, but is instead "*segregated*" from the rest of the transaction in a separate witness field.
12
12
For more information on SegWit, see [this section](https://github.com/bitcoinbook/bitcoinbook/blob/6d1c26e1640ae32b28389d5ae4caf1214c2be7db/ch06_transactions.adoc#segregated-witness) from Mastering Bitcoin, Chapter 6.
13
13
14
-
So let's update our code.
15
-
We have the input length now so we know how many times to read the input information.
14
+
We already have the input length so we know how many times to read the input information.
16
15
We'll start by using a for loop and iterate over a range.
17
16
Since we don't need the range index number, we can just replace the unused variable with an underscore, `_`.
18
17
More details on loops in Rust can be [found here](https://doc.rust-lang.org/book/ch03-05-control-flow.html#looping-through-a-collection-with-for).
Technically, it only accepts a mutable reference to a slice.
120
-
But we've actually been passing in a mutable reference to an array! Remember an array is a fixed size of type `[u8; n]` and not a slice of type `[u8]`.
119
+
But we've actually been passing in a mutable reference to an array!
120
+
Remember an array is a fixed size of type `[u8; n]` and not a slice of type `[u8]`.
121
121
So how has this been working at all?
122
122
I thought we had to be explicit with types in Rust?
Lastly, we need to read the sequence, which are the last 4 bytes.
151
+
Lastly, we need to read the the last 4 bytes for the sequence number.
152
152
A description of what the sequence number represents can be found in Mastering Bitcoin, Chapter 6.
153
153
154
154
```rust
@@ -162,7 +162,8 @@ A description of what the sequence number represents can be found in Mastering B
162
162
...
163
163
```
164
164
165
-
Alright, now that we have each of the components of an input, what should we do with it? It makes sense to collect all this data together into one unified structure rather than just separate variables.
165
+
Alright, now that we have each of the components of an input, what should we do with it?
166
+
It makes sense to collect all this data together into one unified structure rather than just separate variables.
166
167
The right type for this is Rust's `Struct` type, which we'll explore in the next lesson.
0 commit comments