You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

204 lines
4.9 KiB

package settlers
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
)
func Hash(toHash string) string {
hash := sha256.Sum256([]byte(toHash))
return hex.EncodeToString(hash[:])
}
func ValidateToken(token string) (err error) {
err = ErrorInvalidToken
if len(token) != 64 {
return
}
for _, character := range token {
if !strings.Contains("abcdef01234567890", string(character)) {
return
}
}
err = nil
return
}
func CombineToken(customerToken, businessToken string) string {
return Hash(customerToken + " for " + businessToken)
}
func DepositToken(customerToken string) string {
return Hash(customerToken + " deposit")
}
func Enable(endpoint, adminToken, customerToken, businessToken string) (err error) {
if err = ValidateToken(adminToken); err != nil {
return
}
if err = ValidateToken(customerToken); err != nil {
return
}
if err = ValidateToken(businessToken); err != nil {
return
}
combinedToken := CombineToken(customerToken, businessToken)
depositOnlyToken := DepositToken(customerToken)
combinedDepositOnlyToken := CombineToken(depositOnlyToken, businessToken)
requestJson, err := json.Marshal(EnableRequest{AdminToken: adminToken, CombinedToken: combinedToken, CombinedDepositOnlyToken: combinedDepositOnlyToken})
if err != nil {
return
}
ourURL := endpoint + "/enable"
ourHTTP, err := clearnetOrOnionHTTP(ourURL)
if err != nil {
return
}
_, err = handleResponse(ourHTTP.Post(ourURL, "application/json", bytes.NewReader(requestJson)))
return
}
func Balance(endpoint, customerToken, businessToken string) (amount uint64, err error) {
if err = ValidateToken(customerToken); err != nil {
return
}
if err = ValidateToken(businessToken); err != nil {
return
}
combinedToken := CombineToken(customerToken, businessToken)
ourURL := endpoint + "/balance/" + combinedToken
ourHTTP, err := clearnetOrOnionHTTP(ourURL)
if err != nil {
return
}
body, err := handleResponse(ourHTTP.Get(ourURL))
if err != nil {
return
}
var jsonResponse BalanceResponse
decoder := json.NewDecoder(bytes.NewReader(body))
decoder.DisallowUnknownFields()
err = decoder.Decode(&jsonResponse)
if err != nil {
return
}
amount = jsonResponse.Amount
return
}
func DepositOnlyTokenEnabled(endpoint, depositToken, businessToken string) (err error) {
if err = ValidateToken(depositToken); err != nil {
return
}
if err = ValidateToken(businessToken); err != nil {
return
}
combinedToken := CombineToken(depositToken, businessToken)
ourURL := endpoint + "/depositOnlyTokenEnabled/" + combinedToken
ourHTTP, err := clearnetOrOnionHTTP(ourURL)
if err != nil {
return
}
_, err = handleResponse(ourHTTP.Get(ourURL))
if err != nil {
return
}
return
}
func Deposit(endpoint, depositToken, businessToken string, amount uint64) (err error) {
if err = ValidateToken(depositToken); err != nil {
return
}
if err = ValidateToken(businessToken); err != nil {
return
}
combinedToken := CombineToken(depositToken, businessToken)
requestJson, err := json.Marshal(DepositRequest{CombinedDepositOnlyToken: combinedToken, Amount: amount})
if err != nil {
return
}
ourURL := endpoint + "/deposit"
ourHTTP, err := clearnetOrOnionHTTP(ourURL)
if err != nil {
return
}
_, err = handleResponse(ourHTTP.Post(ourURL, "application/json", bytes.NewReader(requestJson)))
return
}
func Add(endpoint, customerToken, businessToken string, amount uint64) (err error) {
if err = ValidateToken(customerToken); err != nil {
return
}
if err = ValidateToken(businessToken); err != nil {
return
}
combinedToken := CombineToken(customerToken, businessToken)
requestJson, err := json.Marshal(AddRequest{CombinedToken: combinedToken, Amount: amount})
if err != nil {
return
}
ourURL := endpoint + "/add"
ourHTTP, err := clearnetOrOnionHTTP(ourURL)
if err != nil {
return
}
_, err = handleResponse(ourHTTP.Post(ourURL, "application/json", bytes.NewReader(requestJson)))
return
}
func Subtract(endpoint, customerToken, businessToken string, amount uint64) (err error) {
if err = ValidateToken(customerToken); err != nil {
return
}
if err = ValidateToken(businessToken); err != nil {
return
}
combinedToken := CombineToken(customerToken, businessToken)
requestJson, err := json.Marshal(SubtractRequest{CombinedToken: combinedToken, Amount: amount})
if err != nil {
return
}
ourURL := endpoint + "/subtract"
ourHTTP, err := clearnetOrOnionHTTP(ourURL)
if err != nil {
return
}
_, err = handleResponse(ourHTTP.Post(ourURL, "application/json", bytes.NewReader(requestJson)))
return
}
func handleResponse(response *http.Response, httpError error) (body []byte, err error) {
if httpError != nil {
err = httpError
return
}
body, err = ioutil.ReadAll(response.Body)
if err != nil {
return
}
response.Body.Close()
if response.StatusCode/100 != 2 {
err = errors.New(string(body))
}
return
}