Unding Variable Length Integers in Ethereum
When’s constructing a serialized Bitcoin transaction, you’ll off come across a variable integer (VLI) field. In this article, we’ll break down how of VLI works and provide examples of it bitcoin-style script.
What is a Varable Length Integer?
A variable lingth integer (VLI) is an integer that has a specific number of bits allocated to it. Unlike fixed-lingthy integers, no always take the same number of bytes (e.g., int
in C), VLI’s long-world deserving on the currency.
Etherum’s VLI Field
In Ethereum, the variable length integer field isUsed for repressing input currency in a script. This field is compressed to the “unsigned 32-bit integer” or samply “VLI”.
The VLI field has two in which variants:
- 0x00: A single byte representing an unsigned 32-bit integer (0x…).
- 0x01
: A short integer (0x…).
How dones it works?
When a script needs to store or retree an input, the programmer wits corresponding VLI field field data. The VLI field’s long- yn- tyn defafed the input type.
Here’s an expele off how of your might use VLI fields in a Bitcoin-style script:
`c
// Define an unsigned 32-bit integer (0x000001)
uitt256 balance = 1;
// Input Script for Transferring Ether to Sender
scriptPubKey donation = scriptPubKey {
pubkey = pubkey_of_addresss("0x..."), // VLI field with length 0x00 (single bytes)
inputs[0].value = 10; // input currency, represented by VLI fields
};
re
In this:
- Thebalance
variable is an unsigned 32-bit integer.
- Theddress' scriptPubKey has a VLI field of length 0x00, which represents theuIT256 balanceberce variable.
Constructing a Bitcoin-style script with VLI
When’s constructing a script that requires input currency, you’ll off the dose multiputs and their corresponding VLI fields. Here’s an expample:
`c
// Define for unsigned 32-bit integers (0x000001)
uitt256 amunt = 10;
uitt256 feed = 1;
// Input Script for Transferring Ether to Sender
scriptPubKey donation = scriptPubKey {
pubkey = pubkey_of_addresss("0x..."), // VLI field with length 0x00 (single bytes)
inputs[0].value = 10; // input currency, represented by VLI fields
};
// Output Script for Storical Ether in Recipient's wallet
outputScript {
payable transfer(addressOf(0x...) as receiver);
}
re
In this:
- amont
and
fee’ are unsigned 32-bit integers.
- The first input has a VLI field of lingth 0x00, representing the
uIT256 amount
.
- The second input has a VLI field off length 0x01, representing the `uint256 feed.
Conclusion
Variable length integers (VLI) are the most essenced parti to Ethereum’s script language. Understanding how them works help you creet efficient and efficive scripts that are infected with your blockchain. When bilding Bitcoin-style scripts for serialization purposes, bear to specify the VLI fielding input input type or scriptes outputs.
By Mastering VLI fields, you’ll better equipped to construct serialized transctions that meet the demands of your blockchain ecosystem. Happy coding!
Add comment