-
Notifications
You must be signed in to change notification settings - Fork 0
/
toml.go
75 lines (61 loc) · 2.25 KB
/
toml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package serve
import (
"fmt"
"net/http"
"github.com/shantanu-hashcash/go/services/regulated-assets-approval-server/internal/serve/httperror"
"github.com/shantanu-hashcash/go/strkey"
"github.com/shantanu-hashcash/go/support/errors"
"github.com/shantanu-hashcash/go/support/log"
)
type hcnetTOMLHandler struct {
assetCode string
approvalServer string
issuerAddress string
networkPassphrase string
kycThreshold int64
}
func (h hcnetTOMLHandler) validate() error {
if h.networkPassphrase == "" {
return errors.New("network passphrase cannot be empty")
}
if h.assetCode == "" {
return errors.New("asset code cannot be empty")
}
if h.issuerAddress == "" {
return errors.New("asset issuer address cannot be empty")
}
if !strkey.IsValidEd25519PublicKey(h.issuerAddress) {
return errors.New("asset issuer address is not a valid public key")
}
if h.approvalServer == "" {
return errors.New("approval server cannot be empty")
}
if h.kycThreshold <= 0 {
return errors.New("kyc threshold cannot be less than or equal to zero")
}
return nil
}
func (h hcnetTOMLHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
err := h.validate()
if err != nil {
log.Ctx(ctx).Error(errors.Wrap(err, "validating tomlHandler"))
httperror.InternalServer.Render(rw)
return
}
// Convert kycThreshold value to human readable string; from amount package's int64 5000000000 to 500.00.
kycThreshold, err := convertAmountToReadableString(h.kycThreshold)
if err != nil {
log.Ctx(ctx).Error(errors.Wrap(err, "converting kycThreshold value to human readable string"))
httperror.InternalServer.Render(rw)
return
}
// Generate toml content.
fmt.Fprintf(rw, "NETWORK_PASSPHRASE=%q\n", h.networkPassphrase)
fmt.Fprintf(rw, "[[CURRENCIES]]\n")
fmt.Fprintf(rw, "code=%q\n", h.assetCode)
fmt.Fprintf(rw, "issuer=%q\n", h.issuerAddress)
fmt.Fprintf(rw, "regulated=true\n")
fmt.Fprintf(rw, "approval_server=%q\n", h.approvalServer)
fmt.Fprintf(rw, "approval_criteria=\"The approval server currently only accepts payments. The transaction must have exactly one operation of type payment. If the payment amount exceeds %s %s it will need KYC approval if the account hasn’t been previously approved.\"", kycThreshold, h.assetCode)
}