decodeVarint static method
Decodes a variable-length byte array into an integer value according to Bitcoin's variable-length integer encoding scheme.
Implementation
static (int, int) decodeVarint(List<int> byteint) {
final int ni = byteint[0];
int size = 0;
if (ni < 253) {
return (ni, 1);
}
if (ni == 253) {
size = 2;
} else if (ni == 254) {
size = 4;
} else {
size = 8;
}
final BigInt value = BigintUtils.fromBytes(
byteint.sublist(1, 1 + size),
byteOrder: Endian.little,
);
if (!value.isValidInt) {
throw ArgumentException.invalidOperationArguments(
"decodeVarint",
name: "byteint",
reason: "Unable to read variable-length in this environment.",
);
}
return (value.toInt(), size + 1);
}