reformat with gofmt
This commit is contained in:
parent
d82c17e0a1
commit
7816566d59
9 changed files with 221 additions and 216 deletions
|
|
@ -27,25 +27,25 @@ func db_open() (*sql.DB, error) {
|
|||
return db, nil
|
||||
}
|
||||
|
||||
func db_get_setting(setting_name string) (string) {
|
||||
func db_get_setting(setting_name string) string {
|
||||
|
||||
setting_value := ""
|
||||
setting_value := ""
|
||||
|
||||
db, err := db_open()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer db.Close()
|
||||
db, err := db_open()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
sqlStatement := `select value from settings where name=$1;`
|
||||
sqlStatement := `select value from settings where name=$1;`
|
||||
|
||||
row := db.QueryRow(sqlStatement, setting_name)
|
||||
err = row.Scan(&setting_value)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
row := db.QueryRow(sqlStatement, setting_name)
|
||||
err = row.Scan(&setting_value)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return setting_value
|
||||
return setting_value
|
||||
}
|
||||
|
||||
func db_get_card_name_count(card_name string) (card_count int, err error) {
|
||||
|
|
@ -74,13 +74,19 @@ func db_insert_card(one_time_code string, k0_auth_key string, k2_cmac_key string
|
|||
allow_neg_bal_ptr bool) error {
|
||||
|
||||
lnurlw_enable_yn := "N"
|
||||
if lnurlw_enable { lnurlw_enable_yn = "Y" }
|
||||
if lnurlw_enable {
|
||||
lnurlw_enable_yn = "Y"
|
||||
}
|
||||
|
||||
uid_privacy_yn := "N"
|
||||
if uid_privacy { uid_privacy_yn = "Y" }
|
||||
if uid_privacy {
|
||||
uid_privacy_yn = "Y"
|
||||
}
|
||||
|
||||
allow_neg_bal_yn := "N"
|
||||
if allow_neg_bal_ptr { allow_neg_bal_yn = "Y" }
|
||||
if allow_neg_bal_ptr {
|
||||
allow_neg_bal_yn = "Y"
|
||||
}
|
||||
|
||||
db, err := db_open()
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ func db_open() (*sql.DB, error) {
|
|||
return db, nil
|
||||
}
|
||||
|
||||
func db_get_setting(setting_name string) (string) {
|
||||
func db_get_setting(setting_name string) string {
|
||||
|
||||
setting_value := ""
|
||||
|
||||
|
|
|
|||
96
lightning.go
96
lightning.go
|
|
@ -2,14 +2,14 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"strconv"
|
||||
"time"
|
||||
"crypto/sha256"
|
||||
|
||||
decodepay "github.com/fiatjaf/ln-decodepay"
|
||||
lnrpc "github.com/lightningnetwork/lnd/lnrpc"
|
||||
|
|
@ -92,9 +92,9 @@ func add_invoice(amount_sat int64, metadata string) (payment_request string, r_h
|
|||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result, err := l_client.AddInvoice(ctx, &lnrpc.Invoice {
|
||||
Value: amount_sat,
|
||||
DescriptionHash: dh[:],
|
||||
result, err := l_client.AddInvoice(ctx, &lnrpc.Invoice{
|
||||
Value: amount_sat,
|
||||
DescriptionHash: dh[:],
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -106,7 +106,7 @@ func add_invoice(amount_sat int64, metadata string) (payment_request string, r_h
|
|||
|
||||
// https://api.lightning.community/?shell#subscribesingleinvoice
|
||||
|
||||
func monitor_invoice_state(r_hash []byte) () {
|
||||
func monitor_invoice_state(r_hash []byte) {
|
||||
|
||||
// SubscribeSingleInvoice
|
||||
|
||||
|
|
@ -114,7 +114,7 @@ func monitor_invoice_state(r_hash []byte) () {
|
|||
|
||||
ln_port, err := strconv.Atoi(db_get_setting("LN_PORT"))
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
log.Warn(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -130,9 +130,9 @@ func monitor_invoice_state(r_hash []byte) () {
|
|||
defer cancel()
|
||||
|
||||
stream, err := i_client.SubscribeSingleInvoice(ctx, &invoicesrpc.SubscribeSingleInvoiceRequest{
|
||||
RHash: r_hash})
|
||||
RHash: r_hash})
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"r_hash": hex.EncodeToString(r_hash)}).Warn(err)
|
||||
log.WithFields(log.Fields{"r_hash": hex.EncodeToString(r_hash)}).Warn(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -144,38 +144,38 @@ func monitor_invoice_state(r_hash []byte) () {
|
|||
}
|
||||
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"r_hash": hex.EncodeToString(r_hash)}).Warn(err)
|
||||
log.WithFields(log.Fields{"r_hash": hex.EncodeToString(r_hash)}).Warn(err)
|
||||
return
|
||||
}
|
||||
|
||||
invoice_state := lnrpc.Invoice_InvoiceState_name[int32(update.State)]
|
||||
|
||||
log.WithFields(
|
||||
log.Fields{
|
||||
"r_hash": hex.EncodeToString(r_hash),
|
||||
"invoice_state": invoice_state,
|
||||
},).Info("invoice state updated")
|
||||
log.WithFields(
|
||||
log.Fields{
|
||||
"r_hash": hex.EncodeToString(r_hash),
|
||||
"invoice_state": invoice_state,
|
||||
}).Info("invoice state updated")
|
||||
|
||||
db_update_receipt_state(hex.EncodeToString(r_hash), invoice_state)
|
||||
}
|
||||
|
||||
connection.Close()
|
||||
|
||||
// send email
|
||||
// send email
|
||||
|
||||
card_id, err := db_get_card_id_for_r_hash(hex.EncodeToString(r_hash))
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"r_hash": hex.EncodeToString(r_hash)}).Warn(err)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"r_hash": hex.EncodeToString(r_hash)}).Warn(err)
|
||||
return
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{"r_hash": hex.EncodeToString(r_hash), "card_id": card_id}).Debug("card found")
|
||||
log.WithFields(log.Fields{"r_hash": hex.EncodeToString(r_hash), "card_id": card_id}).Debug("card found")
|
||||
|
||||
c, err := db_get_card_from_card_id(card_id)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"r_hash": hex.EncodeToString(r_hash)}).Warn(err)
|
||||
return
|
||||
}
|
||||
c, err := db_get_card_from_card_id(card_id)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"r_hash": hex.EncodeToString(r_hash)}).Warn(err)
|
||||
return
|
||||
}
|
||||
|
||||
if c.email_enable != "Y" {
|
||||
log.Debug("email is not enabled for the card")
|
||||
|
|
@ -197,7 +197,7 @@ func pay_invoice(card_payment_id int, invoice string) {
|
|||
|
||||
ln_port, err := strconv.Atoi(db_get_setting("LN_PORT"))
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -212,7 +212,7 @@ func pay_invoice(card_payment_id int, invoice string) {
|
|||
fee_limit_sat_str := db_get_setting("FEE_LIMIT_SAT")
|
||||
fee_limit_sat, err := strconv.ParseInt(fee_limit_sat_str, 10, 64)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -238,7 +238,7 @@ func pay_invoice(card_payment_id int, invoice string) {
|
|||
FeeLimitSat: fee_limit_sat + fee_limit_product})
|
||||
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -250,40 +250,40 @@ func pay_invoice(card_payment_id int, invoice string) {
|
|||
}
|
||||
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
return
|
||||
}
|
||||
|
||||
payment_status := lnrpc.Payment_PaymentStatus_name[int32(update.Status)]
|
||||
failure_reason := lnrpc.PaymentFailureReason_name[int32(update.FailureReason)]
|
||||
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Info("payment failure reason : ", failure_reason)
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Info("payment status : ", payment_status)
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Info("payment failure reason : ", failure_reason)
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Info("payment status : ", payment_status)
|
||||
|
||||
err = db_update_payment_status(card_payment_id, payment_status, failure_reason)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
return
|
||||
}
|
||||
err = db_update_payment_status(card_payment_id, payment_status, failure_reason)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
connection.Close()
|
||||
|
||||
// send email
|
||||
// send email
|
||||
|
||||
card_id, err := db_get_card_id_for_card_payment_id(card_payment_id)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
return
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id, "card_id": card_id}).Debug("card found")
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id, "card_id": card_id}).Debug("card found")
|
||||
|
||||
c, err := db_get_card_from_card_id(card_id)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
return
|
||||
}
|
||||
c, err := db_get_card_from_card_id(card_id)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err)
|
||||
return
|
||||
}
|
||||
|
||||
if c.email_enable != "Y" {
|
||||
log.Debug("email is not enabled for the card")
|
||||
|
|
|
|||
|
|
@ -1,21 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"encoding/hex"
|
||||
"github.com/gorilla/mux"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func lnurlp_callback(w http.ResponseWriter, r *http.Request) {
|
||||
if db_get_setting("FUNCTION_LNURLP") != "ENABLE" {
|
||||
if db_get_setting("FUNCTION_LNURLP") != "ENABLE" {
|
||||
log.Debug("LNURLp function is not enabled")
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
name := mux.Vars(r)["name"]
|
||||
amount := r.URL.Query().Get("amount")
|
||||
name := mux.Vars(r)["name"]
|
||||
amount := r.URL.Query().Get("amount")
|
||||
|
||||
card_id, err := db_get_card_id_for_name(name)
|
||||
if err != nil {
|
||||
|
|
@ -24,38 +24,38 @@ func lnurlp_callback(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
log.WithFields(
|
||||
log.Fields{
|
||||
"url_path": r.URL.Path,
|
||||
"name": name,
|
||||
"card_id": card_id,
|
||||
"amount": amount,
|
||||
"req.Host": r.Host,
|
||||
},).Info("lnurlp_callback")
|
||||
log.WithFields(
|
||||
log.Fields{
|
||||
"url_path": r.URL.Path,
|
||||
"name": name,
|
||||
"card_id": card_id,
|
||||
"amount": amount,
|
||||
"req.Host": r.Host,
|
||||
}).Info("lnurlp_callback")
|
||||
|
||||
domain := db_get_setting("HOST_DOMAIN")
|
||||
if r.Host != domain {
|
||||
log.Warn("wrong host domain")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
domain := db_get_setting("HOST_DOMAIN")
|
||||
if r.Host != domain {
|
||||
log.Warn("wrong host domain")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
|
||||
amount_msat, err := strconv.ParseInt(amount, 10, 64)
|
||||
amount_msat, err := strconv.ParseInt(amount, 10, 64)
|
||||
if err != nil {
|
||||
log.Warn("amount is not a valid integer")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
log.Warn("amount is not a valid integer")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
|
||||
amount_sat := amount_msat / 1000;
|
||||
amount_sat := amount_msat / 1000
|
||||
|
||||
metadata := "[[\"text/identifier\",\"" + name + "@" + domain + "\"],[\"text/plain\",\"bolt card deposit\"]]"
|
||||
pr, r_hash, err := add_invoice(amount_sat, metadata)
|
||||
metadata := "[[\"text/identifier\",\"" + name + "@" + domain + "\"],[\"text/plain\",\"bolt card deposit\"]]"
|
||||
pr, r_hash, err := add_invoice(amount_sat, metadata)
|
||||
if err != nil {
|
||||
log.Warn("could not add_invoice")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
log.Warn("could not add_invoice")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
|
||||
err = db_insert_receipt(card_id, pr, hex.EncodeToString(r_hash), amount_msat)
|
||||
if err != nil {
|
||||
|
|
@ -66,15 +66,15 @@ func lnurlp_callback(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
go monitor_invoice_state(r_hash)
|
||||
|
||||
log.Debug("sending 'status OK' response");
|
||||
log.Debug("sending 'status OK' response")
|
||||
|
||||
jsonData := []byte(`{` +
|
||||
`"status":"OK",` +
|
||||
`"routes":[],` +
|
||||
jsonData := []byte(`{` +
|
||||
`"status":"OK",` +
|
||||
`"routes":[],` +
|
||||
`"pr":"` + pr + `"` +
|
||||
`}`)
|
||||
`}`)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonData)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonData)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
"github.com/gorilla/mux"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func lnurlp_response(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
@ -12,51 +12,51 @@ func lnurlp_response(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
name := mux.Vars(r)["name"]
|
||||
name := mux.Vars(r)["name"]
|
||||
|
||||
log.WithFields(
|
||||
log.Fields{
|
||||
"url_path": r.URL.Path,
|
||||
"name": name,
|
||||
"r.Host": r.Host,
|
||||
},).Info("lnurlp_response")
|
||||
log.WithFields(
|
||||
log.Fields{
|
||||
"url_path": r.URL.Path,
|
||||
"name": name,
|
||||
"r.Host": r.Host,
|
||||
}).Info("lnurlp_response")
|
||||
|
||||
// look up domain setting (HOST_DOMAIN)
|
||||
// look up domain setting (HOST_DOMAIN)
|
||||
|
||||
domain := db_get_setting("HOST_DOMAIN")
|
||||
if r.Host != domain {
|
||||
log.Warn("wrong host domain")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
domain := db_get_setting("HOST_DOMAIN")
|
||||
if r.Host != domain {
|
||||
log.Warn("wrong host domain")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
|
||||
// look up name in database (table cards, field card_name)
|
||||
// look up name in database (table cards, field card_name)
|
||||
|
||||
card_count, err := db_get_card_count_for_name_lnurlp(name)
|
||||
if err != nil {
|
||||
log.Warn("could not get card count for name")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
card_count, err := db_get_card_count_for_name_lnurlp(name)
|
||||
if err != nil {
|
||||
log.Warn("could not get card count for name")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
|
||||
if card_count != 1 {
|
||||
log.Info("not one enabled card with that name")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
if card_count != 1 {
|
||||
log.Info("not one enabled card with that name")
|
||||
write_error(w)
|
||||
return
|
||||
}
|
||||
|
||||
metadata := "[[\\\"text/identifier\\\",\\\"" + name + "@" + domain + "\\\"],[\\\"text/plain\\\",\\\"bolt card deposit\\\"]]"
|
||||
metadata := "[[\\\"text/identifier\\\",\\\"" + name + "@" + domain + "\\\"],[\\\"text/plain\\\",\\\"bolt card deposit\\\"]]"
|
||||
|
||||
jsonData := []byte(`{"status":"OK",` +
|
||||
`"callback":"https://` + domain + `/lnurlp/` + name + `",` +
|
||||
`"tag":"payRequest",` +
|
||||
`"maxSendable":1000000000,` +
|
||||
`"minSendable":1000,` +
|
||||
`"metadata":"` + metadata + `",` +
|
||||
`"commentAllowed":0` +
|
||||
`}`)
|
||||
jsonData := []byte(`{"status":"OK",` +
|
||||
`"callback":"https://` + domain + `/lnurlp/` + name + `",` +
|
||||
`"tag":"payRequest",` +
|
||||
`"maxSendable":1000000000,` +
|
||||
`"minSendable":1000,` +
|
||||
`"metadata":"` + metadata + `",` +
|
||||
`"commentAllowed":0` +
|
||||
`}`)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonData)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonData)
|
||||
}
|
||||
|
|
|
|||
26
main.go
26
main.go
|
|
@ -1,8 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/gorilla/mux"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -27,26 +27,26 @@ func main() {
|
|||
log_level := db_get_setting("LOG_LEVEL")
|
||||
|
||||
switch log_level {
|
||||
case "DEBUG":
|
||||
log.SetLevel(log.DebugLevel)
|
||||
log.Info("bolt card service started - debug log level")
|
||||
case "PRODUCTION":
|
||||
log.Info("bolt card service started - production log level")
|
||||
default:
|
||||
// log.Fatal calls os.Exit(1) after logging the error
|
||||
log.Fatal("error getting a valid LOG_LEVEL setting from the database")
|
||||
case "DEBUG":
|
||||
log.SetLevel(log.DebugLevel)
|
||||
log.Info("bolt card service started - debug log level")
|
||||
case "PRODUCTION":
|
||||
log.Info("bolt card service started - production log level")
|
||||
default:
|
||||
// log.Fatal calls os.Exit(1) after logging the error
|
||||
log.Fatal("error getting a valid LOG_LEVEL setting from the database")
|
||||
}
|
||||
|
||||
log.SetFormatter(&log.JSONFormatter{
|
||||
DisableHTMLEscape: true,
|
||||
})
|
||||
|
||||
// createboltcard
|
||||
// createboltcard
|
||||
router.Path("/new").Methods("GET").HandlerFunc(new_card_request)
|
||||
// lnurlw for pos
|
||||
// lnurlw for pos
|
||||
router.Path("/ln").Methods("GET").HandlerFunc(lnurlw_response)
|
||||
router.Path("/cb").Methods("GET").HandlerFunc(lnurlw_callback)
|
||||
// lnurlp for lightning address
|
||||
// lnurlp for lightning address
|
||||
router.Path("/.well-known/lnurlp/{name}").Methods("GET").HandlerFunc(lnurlp_response)
|
||||
router.Path("/lnurlp/{name}").Methods("GET").HandlerFunc(lnurlp_callback)
|
||||
|
||||
|
|
@ -55,7 +55,7 @@ func main() {
|
|||
port = "9000"
|
||||
}
|
||||
|
||||
srv := &http.Server {
|
||||
srv := &http.Server{
|
||||
Handler: router,
|
||||
Addr: ":" + port, // consider adding host
|
||||
WriteTimeout: 30 * time.Second,
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ type NewCardResponse struct {
|
|||
K2 string `json:"k2"`
|
||||
K3 string `json:"k3"`
|
||||
K4 string `json:"k4"`
|
||||
UID_PRIVACY string `json:"uid_privacy"`
|
||||
UID_PRIVACY string `json:"uid_privacy"`
|
||||
}
|
||||
|
||||
func new_card_request(w http.ResponseWriter, req *http.Request) {
|
||||
|
|
|
|||
|
|
@ -27,46 +27,46 @@ func db_open() (*sql.DB, error) {
|
|||
return db, nil
|
||||
}
|
||||
|
||||
func db_get_setting(setting_name string) (string) {
|
||||
func db_get_setting(setting_name string) string {
|
||||
|
||||
setting_value := ""
|
||||
setting_value := ""
|
||||
|
||||
db, err := db_open()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer db.Close()
|
||||
db, err := db_open()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
sqlStatement := `select value from settings where name=$1;`
|
||||
sqlStatement := `select value from settings where name=$1;`
|
||||
|
||||
row := db.QueryRow(sqlStatement, setting_name)
|
||||
err = row.Scan(&setting_value)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
row := db.QueryRow(sqlStatement, setting_name)
|
||||
err = row.Scan(&setting_value)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return setting_value
|
||||
return setting_value
|
||||
}
|
||||
|
||||
func db_get_card_name_count(card_name string) (card_count int, err error) {
|
||||
|
||||
card_count = 0
|
||||
card_count = 0
|
||||
|
||||
db, err := db_open()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer db.Close()
|
||||
db, err := db_open()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
sqlStatement := `SELECT COUNT(card_id) FROM cards WHERE card_name = $1;`
|
||||
sqlStatement := `SELECT COUNT(card_id) FROM cards WHERE card_name = $1;`
|
||||
|
||||
row := db.QueryRow(sqlStatement, card_name)
|
||||
err = row.Scan(&card_count)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
row := db.QueryRow(sqlStatement, card_name)
|
||||
err = row.Scan(&card_count)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return card_count, nil
|
||||
return card_count, nil
|
||||
}
|
||||
|
||||
func db_wipe_card(card_name string) (*card_wipe_info, error) {
|
||||
|
|
|
|||
|
|
@ -3,19 +3,19 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"strconv"
|
||||
log "github.com/sirupsen/logrus"
|
||||
qrcode "github.com/skip2/go-qrcode"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type card_wipe_info struct {
|
||||
id int
|
||||
k0 string
|
||||
k1 string
|
||||
k2 string
|
||||
k3 string
|
||||
k4 string
|
||||
uid string
|
||||
id int
|
||||
k0 string
|
||||
k1 string
|
||||
k2 string
|
||||
k3 string
|
||||
k4 string
|
||||
uid string
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
@ -29,27 +29,26 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
// check if card_name exists
|
||||
|
||||
// check if card_name exists
|
||||
card_count, err := db_get_card_name_count(*card_name_ptr)
|
||||
if err != nil {
|
||||
log.Warn(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
card_count, err := db_get_card_name_count(*card_name_ptr)
|
||||
if err != nil {
|
||||
log.Warn(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if card_count == 0 {
|
||||
fmt.Println("the card name does not exist in the database")
|
||||
return
|
||||
}
|
||||
if card_count == 0 {
|
||||
fmt.Println("the card name does not exist in the database")
|
||||
return
|
||||
}
|
||||
|
||||
// set the card as wiped and disabled, get the keys
|
||||
|
||||
card_wipe_info_values, err := db_wipe_card(*card_name_ptr)
|
||||
if err != nil {
|
||||
log.Warn(err.Error())
|
||||
return
|
||||
}
|
||||
card_wipe_info_values, err := db_wipe_card(*card_name_ptr)
|
||||
if err != nil {
|
||||
log.Warn(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// show a QR code on the console
|
||||
|
||||
|
|
@ -63,7 +62,7 @@ func main() {
|
|||
`"k4": "` + card_wipe_info_values.k4 + `",` +
|
||||
`"uid": "` + card_wipe_info_values.uid + `",` +
|
||||
`"version": 1` +
|
||||
`}`;
|
||||
`}`
|
||||
|
||||
fmt.Println()
|
||||
q, err := qrcode.New(qr, qrcode.Medium)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue