Transaction.go
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
)
// Configurations and Constants
const (
TimestampErrorLimit = 300 // μμ κ°, μ€μ κ°μΌλ‘ λ³κ²½
ResultFail = "FAIL"
)
// Mocked configuration structure
var Config = struct {
Database bool
}{
Database: true, // μ€μ μ€μ μ λ§κ² λ³κ²½
}
// Mocked utility functions and structures for demonstration purposes
func Utime() int64 {
return time.Now().Unix()
}
func SignatureValidity(data int64, publicKey, signature string) bool {
// μ€μ μλͺ
μ ν¨μ± κ²μ¬ λ‘μ§ κ΅¬ν
return true
}
func HashValidity(cid string) bool {
// μ€μ ν΄μ μ ν¨μ± κ²μ¬ λ‘μ§ κ΅¬ν
return true
}
func AddressValidity(address string) bool {
// μ€μ μ£Όμ μ ν¨μ± κ²μ¬ λ‘μ§ κ΅¬ν
return true
}
func TimeHashValidity(timehash string) bool {
// μ€μ μκ° ν΄μ μ ν¨μ± κ²μ¬ λ‘μ§ κ΅¬ν
return true
}
// TransactionHandler handles the transactions.
type TransactionHandler struct{}
func (t *TransactionHandler) Fail(w http.ResponseWriter, result, errMsg string) {
http.Error(w, fmt.Sprintf("%s: %s", result, errMsg), http.StatusBadRequest)
}
func (t *TransactionHandler) Main(w http.ResponseWriter, req *http.Request) {
if !Config.Database {
t.Fail(w, ResultFail, "The database usage setting is set to false.")
return
}
now := Utime()
timestampStr := req.FormValue("timestamp")
timestamp, err := strconv.ParseInt(timestampStr, 10, 64)
if err != nil {
t.Fail(w, ResultFail, "Invalid timestamp.")
return
}
publicKey := req.FormValue("public_key")
signature := req.FormValue("signature")
timeValidity := abs(now-timestamp) < TimestampErrorLimit
signatureValidity := SignatureValidity(timestamp, publicKey, signature)
if !timeValidity || !signatureValidity {
t.Fail(w, ResultFail, "Invalid request.")
return
}
data := req.FormValue("data")
switch data {
case "get":
response := t.GetTransaction(req)
json.NewEncoder(w).Encode(response)
case "list":
fallthrough
default:
response := t.ListTransaction(req, false)
json.NewEncoder(w).Encode(response)
case "fullList":
response := t.ListTransaction(req, true)
json.NewEncoder(w).Encode(response)
}
}
func (t *TransactionHandler) ListTransaction(req *http.Request, full bool) []map[string]interface{} {
pageStr := req.FormValue("page")
page, _ := strconv.Atoi(pageStr)
countStr := req.FormValue("count")
count, _ := strconv.Atoi(countStr)
cid := req.FormValue("cid")
typ := req.FormValue("type")
address := req.FormValue("address")
from := req.FormValue("from")
to := req.FormValue("to")
timehash := req.FormValue("timehash")
query := map[string]string{}
if HashValidity(cid) {
query["cid"] = cid
}
if match, _ := regexp.MatchString(`^[A-Za-z_0-9]+$`, typ); match {
query["type"] = typ // μ€μ λ‘λ SQL μΈμ μ
λ°©μ§λ₯Ό μν΄ proper escaping νμ
}
if AddressValidity(address) {
query["address"] = address
}
if AddressValidity(from) {
query["from"] = from
}
if AddressValidity(to) {
query["to"] = to
}
if TimeHashValidity(timehash) {
query["timehash"] = timehash
}
chainDB := ChainDB{}
return chainDB.ListData(page, count, query, full)
}
func (t *TransactionHandler) GetTransaction(req *http.Request) map[string]interface{} {
target := req.FormValue("target")
if target == "" {
target = "05df2656734c9e326b71a0d256e39c01054f89f83027c7179fd9fe942a0d6270875c2d16f41732"
}
mainChain := MainChainInstance()
return mainChain.Transaction(target)
}
// ChainDB is a mocked structure for demonstration purposes
type ChainDB struct{}
func (db *ChainDB) ListData(page, count int, query map[string]string, full bool) []map[string]interface{} {
// μ€μ λ°μ΄ν°λ² μ΄μ€ 쿼리 λ‘μ§ κ΅¬ν
return []map[string]interface{}{
{"example_key": "example_value"},
}
}
// MainChain is a mocked structure for demonstration purposes
type MainChain struct{}
func MainChainInstance() *MainChain {
return &MainChain{}
}
func (mc *MainChain) Transaction(target string) map[string]interface{} {
// μ€μ νΈλμμ
μ‘°ν λ‘μ§ κ΅¬ν
return map[string]interface{}{
"transaction_key": "transaction_value",
}
}
func abs(x int64) int64 {
if x < 0 {
return -x
}
return x
}
func main() {
http.HandleFunc("/transaction", func(w http.ResponseWriter, req *http.Request) {
handler := &TransactionHandler{}
handler.Main(w, req)
})
http.ListenAndServe(":8080", nil)
}μ£Όμ κ΅¬μ± μμμ ν¨μ
μμ½
Last updated