encode method
Step 1: Encode files into a quilt and compute metadata.
Implementation
Future<void> encode() async {
if (_step != _WriteFilesFlowStep.initial) {
throw StateError('encode() already called');
}
// Read all files' bytes, identifiers, and tags.
final blobs = <QuiltBlob>[];
for (var i = 0; i < _files.length; i++) {
final file = _files[i];
blobs.add(
QuiltBlob(
contents: await file.bytes(),
identifier: await file.getIdentifier() ?? 'file-$i',
tags: await file.getTags(),
),
);
}
// Determine shard count.
int numShards;
if (_committee != null) {
numShards = _committee.numShards;
} else if (_stateReader != null) {
final state = await _stateReader.systemState();
numShards = state.nShards;
} else {
numShards = 1000; // Testnet default.
}
// Encode quilt.
final quiltResult = encodeQuilt(blobs: blobs, numShards: numShards);
_quiltBytes = quiltResult.quilt;
_quiltIndex = quiltResult.index;
// Compute blob metadata for the quilt.
if (_encoder is WalrusBlobEncoder) {
final walrusEncoder = _encoder;
final encoded = walrusEncoder.encodeBlob(_quiltBytes!, numShards);
_encodedBlob = encoded;
_metadata = await walrusEncoder.computeMetadata(_quiltBytes!, numShards);
} else if (_encoder != null) {
_metadata = await _encoder.computeMetadata(_quiltBytes!, numShards);
} else if (_relayClient != null) {
throw StateError(
'Upload relay mode requires a BlobEncoder for quilt encoding.',
);
} else {
throw StateError('No encoder available for quilt encoding.');
}
_step = _WriteFilesFlowStep.encoded;
}