<commit_before>import {CommonConstants} from "../../../lib/common-libs/constants"
import {GenericParser} from "./GenericParser"
import {rawer} from "../../../lib/common-libs/index"

export class TransactionParser extends GenericParser {

  constructor() {
    super([
      {prop: "version",    regexp: /Version: (.*)/},
      {prop: "currency",   regexp: /Currency: (.*)/},
      {prop: "issuers",    regexp: /Issuers:\n([\s\S]*)Inputs/, parser: extractIssuers },
      {prop: "inputs",     regexp: /Inputs:\n([\s\S]*)Unlocks/, parser: extractInputs },
      {prop: "unlocks",    regexp: /Unlocks:\n([\s\S]*)Outputs/,parser: extractUnlocks },
      {prop: "outputs",    regexp: /Outputs:\n([\s\S]*)/,       parser: extractOutputs },
      {prop: "comment",    regexp: CommonConstants.TRANSACTION.COMMENT },
      {prop: "locktime",   regexp: CommonConstants.TRANSACTION.LOCKTIME },
      {prop: "blockstamp", regexp: CommonConstants.TRANSACTION.BLOCKSTAMP },
      {prop: "signatures", regexp: /Outputs:\n([\s\S]*)/,       parser: extractSignatures }
    ], rawer.getTransaction)
  }

  _clean(obj:any) {
    obj.comment = obj.comment || "";
    obj.locktime = parseInt(obj.locktime) || 0;
    obj.signatures.push(obj.signature);
    const compactSize = 2 // Header + blockstamp
      + obj.issuers.length
      + obj.inputs.length
      + obj.unlocks.length
      + obj.outputs.length
      + (obj.comment ? 1 : 0)
      + obj.signatures;
    if (compactSize > 100) {
      throw 'A transaction has a maximum size of 100 lines';
    }
  }

  _verify(obj:any) {
    let err = null;
    const codes = {
      'BAD_VERSION': 150,
      'NO_BLOCKSTAMP': 151
    };
    if(!err){
      // Version
      if(!obj.version || !obj.version.match(CommonConstants.DOCUMENTS_TRANSACTION_VERSION_REGEXP))
        err = {code: codes.BAD_VERSION, message: "Version unknown"};
      // Blockstamp
      if(!obj.blockstamp || !obj.blockstamp.match(CommonConstants.BLOCKSTAMP_REGEXP))<commit_msg>[fix] #1139 Transaction parsing does not clean its arrays<commit_after>import {CommonConstants} from "../../../lib/common-libs/constants"
import {GenericParser} from "./GenericParser"
import {rawer} from "../../../lib/common-libs/index"

export class TransactionParser extends GenericParser {

  constructor() {
    super([
      {prop: "version",    regexp: /Version: (.*)/},
      {prop: "currency",   regexp: /Currency: (.*)/},
      {prop: "issuers",    regexp: /Issuers:\n([\s\S]*)Inputs/, parser: extractIssuers },
      {prop: "inputs",     regexp: /Inputs:\n([\s\S]*)Unlocks/, parser: extractInputs },
      {prop: "unlocks",    regexp: /Unlocks:\n([\s\S]*)Outputs/,parser: extractUnlocks },
      {prop: "outputs",    regexp: /Outputs:\n([\s\S]*)/,       parser: extractOutputs },
      {prop: "comment",    regexp: CommonConstants.TRANSACTION.COMMENT },
      {prop: "locktime",   regexp: CommonConstants.TRANSACTION.LOCKTIME },
      {prop: "blockstamp", regexp: CommonConstants.TRANSACTION.BLOCKSTAMP },
      {prop: "signatures", regexp: /Outputs:\n([\s\S]*)/,       parser: extractSignatures }
    ], rawer.getTransaction)
  }

  _clean(obj:any) {
    obj.comment = obj.comment || "";
    obj.locktime = parseInt(obj.locktime) || 0;
    obj.signatures = obj.signatures || []
    obj.issuers = obj.issuers || []
    obj.inputs = obj.inputs || []
    obj.outputs = obj.outputs || []
    obj.signatures.push(obj.signature);
    const compactSize = 2 // Header + blockstamp
      + obj.issuers.length
      + obj.inputs.length
      + obj.unlocks.length
      + obj.outputs.length
      + (obj.comment ? 1 : 0)
      + obj.signatures;
    if (compactSize > 100) {
      throw 'A transaction has a maximum size of 100 lines';
    }
  }

  _verify(obj:any) {
    let err = null;
    const codes = {
      'BAD_VERSION': 150,
      'NO_BLOCKSTAMP': 151
    };
    if(!err){
      // Version
      if(!obj.version || !obj.version.match(CommonConstants.DOCUMENTS_TRANSACTION_VERSION_REGEXP))
        err = {code: codes.BAD_VERSION, message: "Version unknown"};
      // Blockstamp
      if(!obj.blockstamp || !obj.blockstamp.match(CommonConstants.BLOCKSTAMP_REGEXP))
{'fraction_non_alphanumeric': 0.1689156626506024, 'fraction_numerical': 0.008674698795180723, 'mean_word_length': 2.883068288119738, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5778664', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1369, 'n_words': 265}