Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit 0743f03

Browse files
committed
Merge branch 'master' of github.com:stellar/gateway-server
2 parents a6b8b4c + d599fec commit 0743f03

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package handlers
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"io/ioutil"
7+
"net/http"
8+
"strings"
9+
10+
"github.com/BurntSushi/toml"
11+
)
12+
13+
type StellarToml struct {
14+
FederationServer *string `toml:"FEDERATION_SERVER"`
15+
}
16+
17+
type StellarDestination struct {
18+
AccountId string `json:"account_id"`
19+
MemoType *string `json:"memo_type"`
20+
Memo *string `json:"memo"`
21+
}
22+
23+
type AddressResolverHelperInterface interface {
24+
GetStellarToml(domain string) (stellarToml StellarToml, err error)
25+
GetDestination(federationUrl, address string) (destination StellarDestination, err error)
26+
}
27+
28+
type AddressResolver struct {
29+
helper AddressResolverHelperInterface
30+
}
31+
32+
func (ar AddressResolver) Resolve(address string) (destination StellarDestination, err error) {
33+
tokens := strings.Split(address, "*")
34+
if len(tokens) == 1 {
35+
destination.AccountId = address
36+
} else if len(tokens) == 2 {
37+
var stellarToml StellarToml
38+
stellarToml, err = ar.helper.GetStellarToml(tokens[1])
39+
if err != nil {
40+
return
41+
}
42+
43+
if stellarToml.FederationServer == nil {
44+
err = errors.New("stellar.toml does not contain FEDERATION_SERVER value")
45+
return
46+
}
47+
48+
destination, err = ar.helper.GetDestination(*stellarToml.FederationServer, address)
49+
return
50+
} else {
51+
err = errors.New("Malformed Stellar address")
52+
}
53+
54+
return
55+
}
56+
57+
type AddressResolverHelper struct{}
58+
59+
func (ar AddressResolverHelper) GetStellarToml(domain string) (stellarToml StellarToml, err error) {
60+
var resp *http.Response
61+
resp, err = http.Get("https://www." + domain + "/.well-known/stellar.toml")
62+
if err != nil {
63+
return
64+
}
65+
if resp.StatusCode != 200 {
66+
err = errors.New("stellar.toml response status code indicates error")
67+
return
68+
}
69+
70+
_, err = toml.DecodeReader(resp.Body, &stellarToml)
71+
return
72+
}
73+
74+
func (ar AddressResolverHelper) GetDestination(federationUrl, address string) (destination StellarDestination, err error) {
75+
if !strings.HasPrefix(federationUrl, "https://") {
76+
err = errors.New("Only HTTPS federation servers allowed")
77+
return
78+
}
79+
80+
resp, err := http.Get(federationUrl + "?type=name&q=" + address)
81+
if err != nil {
82+
return
83+
}
84+
if resp.StatusCode != 200 {
85+
err = errors.New("Federation response status code indicates error")
86+
return
87+
}
88+
89+
var bs []byte
90+
bs, err = ioutil.ReadAll(resp.Body)
91+
err = json.Unmarshal(bs, &destination)
92+
if err != nil {
93+
return
94+
}
95+
96+
if (destination.MemoType != nil) && (destination.Memo == nil) {
97+
err = errors.New("Invalid federation response (memo).")
98+
}
99+
return
100+
}

0 commit comments

Comments
 (0)