-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
verify_pubkey.go
105 lines (88 loc) · 2.86 KB
/
verify_pubkey.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
package paymail
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
/*
Default:
{
"handle":"[email protected]",
"match": true,
"pubkey":"<consulted pubkey>"
}
*/
// VerificationResponse is the result returned from the VerifyPubKey() request
type VerificationResponse struct {
StandardResponse
VerificationPayload
}
// VerificationPayload is the payload from the response
type VerificationPayload struct {
BsvAlias string `json:"bsvalias"` // Version of the bsvalias
Handle string `json:"handle"` // The <alias>@<domain>.<tld>
Match bool `json:"match"` // If the match was successful or not
PubKey string `json:"pubkey"` // The related PubKey
}
// VerifyPubKey will try to match a handle and pubkey
//
// Specs: https://bsvalias.org/05-verify-public-key-owner.html
func (c *Client) VerifyPubKey(verifyURL, alias, domain, pubKey string) (response *VerificationResponse, err error) {
// Require a valid url
if len(verifyURL) == 0 || !strings.Contains(verifyURL, "https://") {
err = fmt.Errorf("invalid url: %s", verifyURL)
return
}
// Basic requirements for request
if len(alias) == 0 {
err = fmt.Errorf("missing alias")
return
} else if len(domain) == 0 {
err = fmt.Errorf("missing domain")
return
} else if len(pubKey) == 0 {
err = fmt.Errorf("missing pubKey")
return
}
// Set the base url and path, assuming the url is from the prior GetCapabilities() request
// https://<host-discovery-target>/verifypubkey/{alias}@{domain.tld}/{pubkey}
reqURL := replacePubKey(replaceAliasDomain(verifyURL, alias, domain), pubKey)
// Fire the GET request
var resp StandardResponse
if resp, err = c.getRequest(reqURL); err != nil {
return
}
// Start the response
response = &VerificationResponse{StandardResponse: resp}
// Test the status code (200 or 304 is valid)
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotModified {
serverError := &ServerError{}
if err = json.Unmarshal(resp.Body, serverError); err != nil {
return
}
err = fmt.Errorf("bad response from paymail provider: code %d, message: %s", response.StatusCode, serverError.Message)
return
}
// Decode the body of the response
if err = json.Unmarshal(resp.Body, &response); err != nil {
return
}
// Invalid version?
if len(response.BsvAlias) == 0 {
err = fmt.Errorf("missing bsvalias version")
return
}
// Check basic requirements ([email protected])
if response.Handle != alias+"@"+domain {
err = fmt.Errorf("verify response handle %s does not match paymail address: %s", response.Handle, alias+"@"+domain)
return
}
// Check the PubKey length
if len(response.PubKey) == 0 {
err = fmt.Errorf("pki response is missing a PubKey value")
} else if len(response.PubKey) != PubKeyLength {
err = fmt.Errorf("returned pubkey is not the required length of %d, got: %d", PubKeyLength, len(response.PubKey))
}
return
}