fix amount parser

This commit is contained in:
callebtc 2022-08-29 00:47:11 +02:00
parent ed6ea4d33c
commit 025261d595
2 changed files with 15 additions and 7 deletions

View file

@ -81,12 +81,18 @@ func (w Lnurl) Handle(writer http.ResponseWriter, request *http.Request) {
api.NotFoundHandler(writer, fmt.Errorf("[handleLnUrl] Form value 'amount' is not set"))
return
}
amount, err := telegram.GetAmount(stringAmount)
if err != nil {
api.NotFoundHandler(writer, fmt.Errorf("[handleLnUrl] Couldn't cast amount to int: %v", err))
return
var amount int64
if amount, err = strconv.ParseInt(stringAmount, 10, 64); err != nil {
// if the value wasn't a clean msat denomination, parse it
amount, err = telegram.GetAmount(stringAmount)
if err != nil {
api.NotFoundHandler(writer, fmt.Errorf("[handleLnUrl] Couldn't cast amount to int: %v", err))
return
}
// GetAmount returns sat, we need msat
amount *= 1000
}
amount = amount * 1000 // msat
comment := request.FormValue("comment")
if len(comment) > CommentAllowed {
@ -100,8 +106,8 @@ func (w Lnurl) Handle(writer http.ResponseWriter, request *http.Request) {
err = json.Unmarshal([]byte(payerdata), &payerData)
if err != nil {
// api.NotFoundHandler(writer, fmt.Errorf("[handleLnUrl] Couldn't parse payerdata: %v", err))
fmt.Errorf("[handleLnUrl] Couldn't parse payerdata: %v", err)
fmt.Errorf("[handleLnUrl] payerdata: %v", payerdata)
log.Errorf("[handleLnUrl] Couldn't parse payerdata: %v", err)
// log.Errorf("[handleLnUrl] payerdata: %v", payerdata)
}
response, err = w.serveLNURLpSecond(username, int64(amount), comment, payerData)

View file

@ -39,6 +39,8 @@ func decodeAmountFromCommand(input string) (amount int64, err error) {
return amount, err
}
// GetAmount parses an amount from a string like 1.2k or 3.50€
// and returns the value in satoshis
func GetAmount(input string) (amount int64, err error) {
// convert something like 1.2k into 1200
if strings.HasSuffix(strings.ToLower(input), "k") {