-
Notifications
You must be signed in to change notification settings - Fork 2
/
did_resolver.go
67 lines (56 loc) · 1.29 KB
/
did_resolver.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
package verifiable
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/iden3/go-iden3-core/v2/w3c"
"github.com/pkg/errors"
)
type DIDResolver interface {
Resolve(ctx context.Context, did *w3c.DID) (DIDDocument, error)
}
type HTTPDIDResolver struct {
resolverURL string
customHTTPClient *http.Client
}
func (r HTTPDIDResolver) Resolve(ctx context.Context, did *w3c.DID) (out DIDDocument, err error) {
type didResolutionResult struct {
DIDDocument DIDDocument `json:"didDocument"`
}
res := &didResolutionResult{}
var (
resp *http.Response
httpClient *http.Client
)
httpClient = http.DefaultClient
if r.customHTTPClient != nil {
httpClient = r.customHTTPClient
}
didStr := did.String()
didParts := strings.Split(didStr, "?")
if len(didParts) == 2 {
didEscaped := url.QueryEscape(didParts[0])
didStr = fmt.Sprintf("%s?%s", didEscaped, didParts[1])
}
if err != nil {
return out, err
}
resp, err = httpClient.Get(fmt.Sprintf("%s/%s", strings.Trim(r.resolverURL, "/"), didStr))
if err != nil {
return out, err
}
defer func() {
err2 := resp.Body.Close()
if err == nil {
err = errors.WithStack(err2)
}
}()
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return out, err
}
return res.DIDDocument, nil
}