ScV2TransactionSemantics.fromJson constructor

ScV2TransactionSemantics.fromJson(
  1. Map<String, dynamic> json
)

Implementation

factory ScV2TransactionSemantics.fromJson(Map<String, dynamic> json) {
  final profile = json['profile'];
  final semanticsHex = json['semantics'];
  final inputSigHash = json['inputSigHash'];
  final inputCount = json['inputCount'];
  final byteLength = json['byteLength'];
  final parentIdsJson = json['parentIds'];
  final outputsJson = json['outputs'];
  final minerFee = json['minerFee'];
  if (profile != scV2SiacoinTransferProfile ||
      semanticsHex is! String ||
      inputSigHash is! String ||
      inputCount is! int ||
      byteLength is! int ||
      parentIdsJson is! List ||
      outputsJson is! List ||
      minerFee is! String) {
    throw const FormatException('Malformed SC V2 semantics response');
  }

  final bytes = Uint8List.fromList(hex.decode(semanticsHex));
  final parentIds = parentIdsJson.map((value) => value as String).toList();
  final outputs = outputsJson
      .map(
        (value) => ScV2SemanticOutput.fromJson(
          Map<String, dynamic>.from(value as Map),
        ),
      )
      .toList();
  final digestPattern = RegExp(r'^[0-9a-f]{64}$');
  final parentPattern = RegExp(r'^[0-9a-f]{64}$');
  if (bytes.length != byteLength ||
      bytes.length > scV2MaxSemanticTransactionBytes ||
      inputCount < 1 ||
      inputCount > _scV2MaxTransferInputs ||
      inputCount != parentIds.length ||
      outputs.isEmpty ||
      outputs.length > _scV2MaxTransferOutputs ||
      !digestPattern.hasMatch(inputSigHash) ||
      parentIds.any(
        (id) =>
            !parentPattern.hasMatch(id) || RegExp(r'^0{64}$').hasMatch(id),
      ) ||
      parentIds.toSet().length != parentIds.length ||
      !RegExp(r'^(0|[1-9][0-9]*)$').hasMatch(minerFee)) {
    throw const FormatException('Inconsistent SC V2 semantics response');
  }

  return ScV2TransactionSemantics(
    profile: profile,
    bytes: bytes,
    inputSigHash: inputSigHash,
    inputCount: inputCount,
    parentIds: parentIds,
    outputs: outputs,
    minerFee: BigInt.parse(minerFee),
  );
}