<commit_before>package route

import (
	"net/http" // Provides HTTP client and server implementations.
	"github.com/gorilla/mux" //Implements a request router and dispatcher for matching incoming requests to their respective handler
	"fmt"
	"encoding/json"
	//"github.com/blockfreight/go-bftx/config" //Package that handles with the application configutarions.
	"github.com/blockfreight/go-bftx/lib/app/bf_tx"         // Defines the Blockfreight™ Transaction (BF_TX) transaction standard and provides some useful functions to work with the BF_TX.
	
	// ===============
	// Tendermint Core
	// ===============
	"github.com/tendermint/abci/client"
)

// client is a global variable so it can be reused by the console
var TendermintClient abcicli.Client

func StartApi() error {
	//configuration, _ := config.LoadConfiguration()
	router := mux.NewRouter()
	router.HandleFunc("/transaction", apiConstructBfTx).Methods("POST")
	router.HandleFunc("/transaction", apiConstructBfTx).Methods("GET")
	return http.ListenAndServe(":12345", router)	
}


// Construct the Blockfreight™ Transaction [BF_TX]
func apiConstructBfTx(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
	
	var transaction bf_tx.BF_TX
	_ = json.NewDecoder(r.Body).Decode(&transaction)

	resInfo, err := TendermintClient.InfoSync()
	if err != nil {
		errorResponse(transaction, w, err)
		return
	}

	hash, err := bf_tx.HashBFTX(transaction)
	if err != nil {
		errorResponse(transaction, w, err)
		return
	}

	// Generate BF_TX id
	transaction.Id = fmt.Sprintf("%x", bf_tx.GenerateBFTXSalt(hash, resInfo.LastBlockAppHash))
	
	if err := json.NewEncoder(w).Encode(transaction); err != nil {
        panic(err)
	}
	
}

func errorResponse(transaction bf_tx.BF_TX, w http.ResponseWriter, err error) {
	w.WriteHeader(http.StatusInternalServerError)
    if err := json.NewEncoder(w).Encode(transaction); err != nil {
        panic(err)
    }
}<commit_msg>Routing is creating, signing and broacasting a transaction<commit_after>package route

import (
	"net/http" // Provides HTTP client and server implementations.
	"github.com/gorilla/mux" //Implements a request router and dispatcher for matching incoming requests to their respective handler
	"fmt"
	"encoding/json"
	//"github.com/blockfreight/go-bftx/config" //Package that handles with the application configutarions.
	"github.com/blockfreight/go-bftx/lib/app/bf_tx"         // Defines the Blockfreight™ Transaction (BF_TX) transaction standard and provides some useful functions to work with the BF_TX.
	"github.com/blockfreight/go-bftx/lib/app/validator"     // Provides functions to assure the input JSON is correct.
	"github.com/blockfreight/go-bftx/lib/pkg/leveldb"       // Provides some useful functions to work with LevelDB.
	"github.com/blockfreight/go-bftx/lib/pkg/crypto"        // Provides useful functions to sign BF_TX.

	// ===============
	// Tendermint Core
	// ===============
	"github.com/tendermint/abci/client"
)

// client is a global variable so it can be reused by the console
var TendermintClient abcicli.Client

func StartApi() error {
	//configuration, _ := config.LoadConfiguration()
	router := mux.NewRouter()
	router.HandleFunc("/fulltransaction", apiFullTransactionBfTx).Methods("POST")
	router.HandleFunc("/transaction/construct", apiConstructBfTx).Methods("POST")
	router.HandleFunc("/transaction/sign/{id}", apiSignBfTx).Methods("PUT")
	router.HandleFunc("/transaction/broadcast/{id}", apiBroadcastBfTx).Methods("PUT")
	return http.ListenAndServe(":12345", router)	
}


// Construct the Blockfreight™ Transaction [BF_TX]
func apiFullTransactionBfTx(w http.ResponseWriter, r *http.Request) {
	var transaction bf_tx.BF_TX
	_ = json.NewDecoder(r.Body).Decode(&transaction)

	resInfo, err := TendermintClient.InfoSync()
	if err != nil {
		errorResponse(w, err)
		return
	}

	hash, err := bf_tx.HashBFTX(transaction)
	if err != nil {
		errorResponse(w, err)
		return
	}

	// Generate BF_TX id
	transaction.Id = fmt.Sprintf("%x", bf_tx.GenerateBFTXSalt(hash, resInfo.LastBlockAppHash))

	// Re-validate a BF_TX before create a BF_TX
	_ , err = validator.ValidateBFTX(transaction)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		if err := json.NewEncoder(w).Encode(err); err != nil {
			panic(err)
		}
		return
	}	

	// Sign BF_TX
	transaction, err = crypto.SignBFTX(transaction)
	if err != nil {
		errorResponse(w, err)
		return
	}

	transaction.Transmitted = true

	// Get the BF_TX content in string format
	content, err := bf_tx.BFTXContent(transaction)
	if err != nil {
		errorResponse(w, err)
		return
	}

	// Save on DB
	err = leveldb.RecordOnDB(transaction.Id, content)
	if err != nil {
		errorResponse(w, err)
		return
	}

	// Deliver / Publish a BF_TX
	TendermintClient.DeliverTxSync([]byte(content))
	
	// Check the BF_TX hash
	TendermintClient.CommitSync()
	
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(transaction); err != nil {
        panic(err)
	}
	
}

func apiConstructBfTx(w http.ResponseWriter, r *http.Request) {
	var transaction bf_tx.BF_TX
	_ = json.NewDecoder(r.Body).Decode(&transaction)

	resInfo, err := TendermintClient.InfoSync()
	if err != nil {
		errorResponse(w, err)
		return
	}

	hash, err := bf_tx.HashBFTX(transaction)
	if err != nil {
		errorResponse(w, err)
		return
	}

	// Generate BF_TX id
	transaction.Id = fmt.Sprintf("%x", bf_tx.GenerateBFTXSalt(hash, resInfo.LastBlockAppHash))

	// Re-validate a BF_TX before create a BF_TX
	_ , err = validator.ValidateBFTX(transaction)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		if err := json.NewEncoder(w).Encode(err); err != nil {
			panic(err)
		}
		return
	}	

	// Get the BF_TX content in string format
	content, err := bf_tx.BFTXContent(transaction)
	if err != nil {
		errorResponse(w, err)
		return
	}

	// Save on DB
	err = leveldb.RecordOnDB(transaction.Id, content)
	if err != nil {
		errorResponse(w, err)
		return
	}

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(transaction); err != nil {
        panic(err)
	}
}

func apiSignBfTx(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	fmt.Println(params["id"])
	// Get a BF_TX by id
	transaction, err := leveldb.GetBfTx(params["id"])
	if err != nil {
		errorResponse(w, err)
		return
	}
	if transaction.Verified {
		w.WriteHeader(http.StatusConflict)
		if err := json.NewEncoder(w).Encode(transaction); err != nil {
			panic(err)
		}
		return
	}

	// Sign BF_TX
	transaction, err = crypto.SignBFTX(transaction)
	if err != nil {
		errorResponse(w, err)
		return
	}

	// Get the BF_TX content in string format
	content, err := bf_tx.BFTXContent(transaction)
	if err != nil {
		errorResponse(w, err)
		return
	}

	// Update on DB
	err = leveldb.RecordOnDB(string(transaction.Id), content)
	if err != nil {
		errorResponse(w, err)
		return
	}

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(transaction); err != nil {
        panic(err)
	}
}

func apiBroadcastBfTx(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	fmt.Println(params["id"])
	// Get a BF_TX by id
	transaction, err := leveldb.GetBfTx(params["id"])
	if err != nil {
		errorResponse(w, err)
		return
	}

	if !transaction.Verified {
		w.WriteHeader(http.StatusConflict)
		if err := json.NewEncoder(w).Encode(transaction); err != nil {
			panic(err)
		}
		return
	}
	if transaction.Transmitted {
		w.WriteHeader(http.StatusConflict)
		if err := json.NewEncoder(w).Encode(transaction); err != nil {
			panic(err)
		}
		return
	}

	// Change the boolean valud for Transmitted attribute
	transaction.Transmitted = true

	// Get the BF_TX content in string format
	content, err := bf_tx.BFTXContent(transaction)
	if err != nil {
		errorResponse(w, err)
		return
	}

	// Update on DB
	err = leveldb.RecordOnDB(string(transaction.Id), content)
	if err != nil {
		errorResponse(w, err)
		return
	}

	// Deliver / Publish a BF_TX
	TendermintClient.DeliverTxSync([]byte(content))

	// Check the BF_TX hash
	TendermintClient.CommitSync()

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
	if err := json.NewEncoder(w).Encode(transaction); err != nil {
        panic(err)
	}	
}

func errorResponse(w http.ResponseWriter, err error) {
	w.WriteHeader(http.StatusInternalServerError)
    if err := json.NewEncoder(w).Encode(err); err != nil {
        panic(err)
    }
}
{'fraction_non_alphanumeric': 0.13532269257460097, 'fraction_numerical': 0.0017349063150589867, 'mean_word_length': 4.05672514619883, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '6392455', 'n_tokens_mistral': 3092, 'n_tokens_neox': 2944, 'n_words': 678}