serialize method Null safety

Uint8List serialize(
  1. {dynamic includeSignature = true}
)

Creates a Uint8List representation of this.

The Uint8List is built by a list of the transaction properties, prepended by its size obtained from CompactSize.toSize. Use with includeSignature to false, to sign and verify the signature.

Uint8List serialized = Uint8List.fromList([
  ...UtilsCompactSize.toSize(version),
  ...version,
  ...UtilsCompactSize.toSize(address),
  ...address,
  ...UtilsCompactSize.toSize(timestamp),
  ...timestamp,
  ...UtilsCompactSize.toSize(assetRef),
  ...assetRef,
  ...UtilsCompactSize.toSize(signature),
  ...signature,
  ...UtilsCompactSize.toSize(contents),
  ...contents,
]);

Implementation

Uint8List serialize({includeSignature = true}) {
  Uint8List versionBytes = Bytes.encodeBigInt(BigInt.from(version));
  Uint8List serializedVersion = (BytesBuilder()
        ..add(CompactSize.toSize(versionBytes))
        ..add(versionBytes))
      .toBytes();
  Uint8List serializedAddress = (BytesBuilder()
        ..add(CompactSize.toSize(address))
        ..add(address))
      .toBytes();
  Uint8List timestampBytes = Bytes.encodeBigInt(
      BigInt.from(timestamp.millisecondsSinceEpoch ~/ 1000));
  Uint8List serializedTimestamp = (BytesBuilder()
        ..add(CompactSize.toSize(timestampBytes))
        ..add(timestampBytes))
      .toBytes();
  Uint8List assetRefBytes = base64.decode(assetRef);
  Uint8List serializedAssetRef = (BytesBuilder()
        ..add(CompactSize.toSize(assetRefBytes))
        ..add(assetRefBytes))
      .toBytes();
  Uint8List serializedSignature = (BytesBuilder()
        ..add(CompactSize.toSize(includeSignature && signature != null
            ? signature!
            : Uint8List(0)))
        ..add(includeSignature && signature != null
            ? signature!
            : Uint8List(0)))
      .toBytes();
  Uint8List serializedContents = (BytesBuilder()
        ..add(CompactSize.toSize(contents))
        ..add(contents))
      .toBytes();
  return (BytesBuilder()
        ..add(serializedVersion)
        ..add(serializedAddress)
        ..add(serializedTimestamp)
        ..add(serializedAssetRef)
        ..add(serializedSignature)
        ..add(serializedContents))
      .toBytes();
}