-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
verify.go
57 lines (48 loc) · 1.82 KB
/
verify.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
package server
import (
"net/http"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
"github.com/tonicpow/go-paymail"
)
// verifyPubKey will return a response if the pubkey matches the paymail given
//
// Specs: https://bsvalias.org/05-verify-public-key-owner.html
func (c *Configuration) verifyPubKey(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// Get the params submitted via URL request
params := apirouter.GetParams(req)
incomingPaymail := params.GetString("paymailAddress")
incomingPubKey := params.GetString("pubKey")
// Parse, sanitize and basic validation
alias, domain, address := paymail.SanitizePaymail(incomingPaymail)
if len(address) == 0 {
ErrorResponse(w, req, ErrorInvalidParameter, "invalid paymail: "+incomingPaymail, http.StatusBadRequest)
return
} else if !c.IsAllowedDomain(domain) {
ErrorResponse(w, req, ErrorUnknownDomain, "domain unknown: "+domain, http.StatusBadRequest)
return
}
// Basic validation on pubkey
if len(incomingPubKey) != paymail.PubKeyLength {
ErrorResponse(w, req, ErrorInvalidPubKey, "invalid pubkey: "+incomingPubKey, http.StatusBadRequest)
return
}
// Create the metadata struct
md := CreateMetadata(req, alias, domain, "")
// Get from the data layer
foundPaymail, err := c.actions.GetPaymailByAlias(req.Context(), alias, domain, md)
if err != nil {
ErrorResponse(w, req, ErrorFindingPaymail, err.Error(), http.StatusExpectationFailed)
return
} else if foundPaymail == nil {
ErrorResponse(w, req, ErrorPaymailNotFound, "paymail not found", http.StatusNotFound)
return
}
// Return the response
apirouter.ReturnResponse(w, req, http.StatusOK, &paymail.VerificationPayload{
BsvAlias: c.BSVAliasVersion,
Handle: address,
PubKey: foundPaymail.PubKey,
Match: foundPaymail.PubKey == incomingPubKey,
})
}