Transaction.fromReader constructor
Transaction.fromReader(
- BytesReader reader, {
- bool? expectWitness,
Reads a transaction from a BytesReader, which may throw
TransactionTooLarge or InvalidTransaction if the data doesn't
represent a complete transaction within maxSize (1MB).
If expectWitness
is true, the transaction is assumed to be a witness
transaction. If it is false, the transction is assumed to be a legacy
non-witness transaction.
If expectWitness
is omitted or null, then this method will determine the
correct transaction type from the data, starting with a witness type.
Implementation
factory Transaction.fromReader(BytesReader reader, { bool? expectWitness }) {
bool tooLarge = false;
final start = reader.offset;
Transaction? tryReadAndSetTooLarge(bool witness) {
try {
return _tryRead(reader, witness);
} on TransactionTooLarge {
tooLarge = true;
} on Exception catch(_) {}
return null;
}
if (expectWitness != false) { // Includes null condition
final witnessTx = tryReadAndSetTooLarge(true);
if (witnessTx != null) return witnessTx;
}
// Reset offset of reader
reader.offset = start;
if (expectWitness != true) { // Includes null condition
final legacyTx = tryReadAndSetTooLarge(false);
if (legacyTx != null) return legacyTx;
}
throw tooLarge ? TransactionTooLarge() : InvalidTransaction();
}