taldir

Directory service to resolve wallet mailboxes by messenger addresses
Log | Files | Refs | Submodules | README | LICENSE

commit 02ffc8a6db99a67a3e0f126f20f1c77cd9621896
parent 3d4923ec4c606cda8343c8bd3b30777f3a3d9a3c
Author: Martin Schanzenbach <schanzen@gnunet.org>
Date:   Sun, 17 Jul 2022 13:16:20 +0200

use taler-go amount

Diffstat:
Mgo.mod | 1+
Mpkg/rest/taldir.go | 16+++++++++-------
Mpkg/taler/merchant.go | 5+++--
Mpkg/util/helper.go | 69++++++++++++++++++++-------------------------------------------------
4 files changed, 33 insertions(+), 58 deletions(-)

diff --git a/go.mod b/go.mod @@ -16,4 +16,5 @@ require ( gopkg.in/ini.v1 v1.66.4 gorm.io/driver/postgres v1.3.4 gorm.io/gorm v1.23.4 + taler.net/taler-go.git v0.0.0-20220717105356-903c2fdca4c5 // indirect ) diff --git a/pkg/rest/taldir.go b/pkg/rest/taldir.go @@ -506,16 +506,16 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r *http.Request){ // FIXME: integer arithmetic fixedCost := t.Cfg.Section("taldir-" + vars["method"]).Key("challenge_fee").MustString("KUDOS:0") sliceDuration := time.Duration(validation.Duration * 1000) - cost, currency, err := util.CalculateCost(t.MonthlyFee, - fixedCost, - sliceDuration, - monthDuration) + cost, err := util.CalculateCost(t.MonthlyFee, + fixedCost, + sliceDuration, + monthDuration) if err != nil { + fmt.Println(err) w.WriteHeader(http.StatusInternalServerError) return } - - if cost > 0 { + if !cost.IsZero() { if validationExists { if order.ID != validation.OrderID { w.WriteHeader(http.StatusConflict) @@ -524,8 +524,9 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r *http.Request){ } if len(validation.OrderID) == 0 { // Add new order for new validations - orderID, newOrderErr := t.Merchant.AddNewOrder(cost, currency) + orderID, newOrderErr := t.Merchant.AddNewOrder(*cost) if newOrderErr != nil { + fmt.Println(newOrderErr) w.WriteHeader(http.StatusInternalServerError) return } @@ -537,6 +538,7 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r *http.Request){ // FIXME: Remember that it was activated and paid payto, paytoErr := t.Merchant.IsOrderPaid(validation.OrderID) if paytoErr != nil { + fmt.Println(paytoErr) w.WriteHeader(http.StatusInternalServerError) log.Println(paytoErr) return diff --git a/pkg/taler/merchant.go b/pkg/taler/merchant.go @@ -7,6 +7,7 @@ import ( "fmt" "errors" "io/ioutil" + talerutil "taler.net/taler-go.git/pkg/util" ) type PostOrderRequest struct { @@ -136,11 +137,11 @@ func (m *Merchant) IsOrderPaid(orderId string) (string, error) { return "", nil } -func (m *Merchant) AddNewOrder(cost float64, currency string) (string, error) { +func (m *Merchant) AddNewOrder(cost talerutil.Amount) (string, error) { var newOrder PostOrderRequest var orderDetail MinimalOrderDetail var orderResponse PostOrderResponse - orderDetail.Amount = fmt.Sprintf("%s:%f", currency, cost) + orderDetail.Amount = cost.String() // FIXME get from cfg orderDetail.Summary = "This is an order to a TalDir registration" newOrder.order = orderDetail diff --git a/pkg/util/helper.go b/pkg/util/helper.go @@ -23,11 +23,9 @@ import ( "fmt" "crypto/sha512" "math/rand" - "errors" - "strings" - "strconv" "time" gnunetutil "git.gnunet.org/gnunet-go.git/pkg/util" + talerutil "taler.net/taler-go.git/pkg/util" ) @@ -55,57 +53,30 @@ func GenerateChallenge(bytes int) string { } // Check if this is a non-zero, positive amount -func AmountIsNonZero(amount string) bool { - amountFloat, _ := AmountToFloat(amount) - return amountFloat > 0 -} - -func AmountCurrency(amount string) (string, error) { - s := strings.Split(amount, ":") - if len(s) != 2 { - return "", errors.New("Malformed amount") - } - return s[0], nil -} - - -func AmountToFloat(amount string) (float64, error) { - s := strings.Split(amount, ":") - if len(s) != 2 { - return 0.0, errors.New("Malformed amount") - } - amountFloat, err := strconv.ParseFloat(s[1], 64) - if err != nil { - return 0.0, errors.New("Malformed value in amount") +func CalculateCost(sliceCostAmount string, fixedCostAmount string, howLong time.Duration, sliceDuration time.Duration) (*talerutil.Amount, error) { + sliceCount := int(float64(howLong.Microseconds()) / float64(sliceDuration.Microseconds())) + sliceCost, err := talerutil.ParseAmount(sliceCostAmount) + if nil != err { + return nil, err } - return amountFloat, nil -} - -func ParseAmount(amount string) (float64, string, error) { - cur, err := AmountCurrency(amount) + fixedCost, err := talerutil.ParseAmount(fixedCostAmount) if nil != err { - return 0.0, "", errors.New("Currency in amount malformed") + return nil, err } - val, err := AmountToFloat(amount) - if err != nil { - return 0.0, "", err + sum := &talerutil.Amount{ + Currency: sliceCost.Currency, + Value: 0, + Fraction: 0, } - return val, cur, nil -} - -// Check if this is a non-zero, positive amount -func CalculateCost(sliceCostAmount string, fixedCostAmount string, howLong time.Duration, sliceDuration time.Duration) (float64, string, error) { - sliceCount := float64(howLong.Microseconds()) / float64(sliceDuration.Microseconds()) - sliceCost, curA, err := ParseAmount(sliceCostAmount) - if nil != err { - return 0.0, "", errors.New("Currency in amount malformed") + for i := 0; i < sliceCount; i++ { + sum, err = sum.Add(*sliceCost) + if nil != err { + return nil, err + } } - fixedCost, curB, err := ParseAmount(fixedCostAmount) + sum, err = sum.Add(*fixedCost) if nil != err { - return 0.0, "", errors.New("Currency in amount malformed") - } - if curA != curB { - return 0.0, "", errors.New("Currency in amounts different") + return nil, err } - return (sliceCount * sliceCost) + fixedCost, curA, nil + return sum, nil }