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