toCbor method

  1. @override
CborObject toCbor()
override

Converts the object to a CBOR object.

Implementation

@override
CborObject toCbor() {
  if (!preferAlonzoFormat && metadata != null && plutusScripts == null) {
    if (nativeScripts != null) {
      return CborListValue.fixedLength([
        metadata!.toCbor(),
        CborListValue.fixedLength(
            nativeScripts!.map((e) => e.toCbor()).toList())
      ]);
    }
    return metadata!.toCbor();
  }
  final plutusV2 = CborListValue.fixedLength(plutusScripts
          ?.where((element) => element.language == Language.plutusV2)
          .map((e) => e.toCbor())
          .toList() ??
      []);
  return CborTagValue(
      CborMapValue.fixedLength({
        if (metadata != null) ...{const CborIntValue(0): metadata!.toCbor()},
        if (nativeScripts?.isNotEmpty ?? false) ...{
          const CborIntValue(1): CborListValue.fixedLength(
              nativeScripts!.map((e) => e.toCbor()).toList())
        },
        if (plutusScripts != null) ...{
          const CborIntValue(2): CborListValue.fixedLength(plutusScripts!
              .where((element) => element.language == Language.plutusV1)
              .map((e) => e.toCbor())
              .toList())
        },
        if (plutusV2.value.isNotEmpty) ...{const CborIntValue(3): plutusV2},
      }),
      TransactionMetadataUtils.auxiliaryDataCborTag);
}