-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclient.go
123 lines (111 loc) · 2.67 KB
/
client.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package blockstackclient
import (
"encoding/json"
"errors"
"golang.org/x/net/proxy"
"net"
"net/http"
"net/url"
"path"
"strings"
"sync"
"time"
"context"
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
)
type httpClient interface {
Get(string) (*http.Response, error)
}
type BlockstackClient struct {
resolverURL string
httpClient httpClient
sync.Mutex
}
func NewBlockStackClient(resolverURL string, dialer proxy.Dialer) *BlockstackClient {
dial := net.Dial
if dialer != nil {
dial = dialer.Dial
}
tbTransport := &http.Transport{Dial: dial}
client := &http.Client{Transport: tbTransport, Timeout: time.Minute}
b := &BlockstackClient{
resolverURL: resolverURL,
httpClient: client,
}
return b
}
type lookupRes struct {
pid peer.ID
error error
}
func (b *BlockstackClient) Resolve(ctx context.Context, name string) (pid peer.ID, err error) {
if !strings.HasSuffix(name, ".id") {
return pid, errors.New("Domain is not a blockstack domain")
}
split := strings.Split(name, ".")
name = split[0]
rootChan := make(chan lookupRes, 1)
go workDomain(b, name, rootChan)
var rootRes lookupRes
select {
case rootRes = <-rootChan:
case <-ctx.Done():
return pid, ctx.Err()
}
if rootRes.error == nil {
pid = rootRes.pid
} else {
return pid, errors.New("Not found")
}
return pid, nil
}
func (b *BlockstackClient) Domains() []string {
return []string{"id"}
}
func workDomain(b *BlockstackClient, name string, res chan lookupRes) {
var pid peer.ID
defer func() {
if rerr := recover(); rerr != nil {
res <- lookupRes{pid, errors.New("Couldn't parse blockchainID json")}
return
}
}()
resolver, err := url.Parse(b.resolverURL)
if err != nil {
res <- lookupRes{pid, err}
return
}
resolver.Path = path.Join(resolver.Path, "v2", "users", name)
resp, err := b.httpClient.Get(resolver.String())
if err != nil {
res <- lookupRes{pid, errors.New("Error querying resolver")}
return
}
if resp.StatusCode == http.StatusNotFound {
res <- lookupRes{pid, errors.New("Handle not found")}
}
decoder := json.NewDecoder(resp.Body)
var data map[string]interface{}
err = decoder.Decode(&data)
if err != nil {
res <- lookupRes{pid, err}
return
}
obj := data[name].(map[string]interface{})
profile := obj["profile"].(map[string]interface{})
account := profile["account"].([]interface{})
for _, a := range account {
acc := a.(map[string]interface{})
service := strings.ToLower(acc["service"].(string))
identifier := acc["identifier"].(string)
if service == "openbazaar" {
pid, err := peer.IDB58Decode(identifier)
if err != nil {
res <- lookupRes{pid, err}
return
}
res <- lookupRes{pid, nil}
return
}
}
}