forked from tonicpow/go-paymail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpki.go
More file actions
47 lines (38 loc) · 1.48 KB
/
pki.go
File metadata and controls
47 lines (38 loc) · 1.48 KB
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
package server
import (
"net/http"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
"github.com/tonicpow/go-paymail"
)
// showPKI will return the public key information for the corresponding paymail address
//
// Specs: http://bsvalias.org/03-public-key-infrastructure.html
func showPKI(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// Get the params & paymail address submitted via URL request
params := apirouter.GetParams(req)
incomingPaymail := params.GetString("paymailAddress")
// 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 domain != paymailDomain {
ErrorResponse(w, req, ErrorUnknownDomain, "domain unknown: "+domain, http.StatusBadRequest)
return
}
// todo: lookup the paymail address in a data-store, database, etc - get the PubKey (return 404 if not found)
// todo: add caching for fast responses since the pubkey will not change
// Find in mock database
foundPaymail := getPaymailByAlias(alias)
if foundPaymail == nil {
ErrorResponse(w, req, ErrorPaymailNotFound, "paymail not found", http.StatusNotFound)
return
}
// Return the response
apirouter.ReturnResponse(w, req, http.StatusOK, &paymail.PKI{
BsvAlias: paymail.DefaultBsvAliasVersion,
Handle: address,
PubKey: foundPaymail.PubKey,
})
}