-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
resolve_address.go
176 lines (150 loc) · 5.56 KB
/
resolve_address.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package server
import (
"net"
"net/http"
"time"
"github.com/bitcoinschema/go-bitcoin/v2"
"github.com/julienschmidt/httprouter"
"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bt/v2/bscript"
apirouter "github.com/mrz1836/go-api-router"
"github.com/tonicpow/go-paymail"
)
/*
Incoming Data Object Example:
{
"senderName": "UserName",
"senderHandle": "[email protected]",
"dt": "2020-04-09T16:08:06.419Z",
"amount": 551,
"purpose": "message to receiver",
"signature": "SIGNATURE-IF-REQUIRED-IN-CONFIG"
}
*/
// resolveAddress will return the payment destination (bitcoin address) for the corresponding paymail address
//
// Specs: http://bsvalias.org/04-01-basic-address-resolution.html
func (c *Configuration) resolveAddress(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, paymailAddress := paymail.SanitizePaymail(incomingPaymail)
if len(paymailAddress) == 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
}
// Start the SenderRequest
senderRequest := &paymail.SenderRequest{
Amount: params.GetUint64("amount"),
Dt: params.GetString("dt"),
Purpose: params.GetString("purpose"),
SenderHandle: params.GetString("senderHandle"),
SenderName: params.GetString("senderName"),
Signature: params.GetString("signature"),
}
// Check for required fields
if len(senderRequest.SenderHandle) == 0 {
ErrorResponse(w, req, ErrorInvalidSenderHandle, "senderHandle is empty", http.StatusBadRequest)
return
} else if len(senderRequest.Dt) == 0 {
ErrorResponse(w, req, ErrorInvalidDt, "dt is empty", http.StatusBadRequest)
return
}
// Validate the timestamp
if err := paymail.ValidateTimestamp(senderRequest.Dt); err != nil {
ErrorResponse(w, req, ErrorInvalidDt, "invalid dt: "+err.Error(), http.StatusBadRequest)
return
}
// Basic validation on sender handle
if err := paymail.ValidatePaymail(senderRequest.SenderHandle); err != nil {
ErrorResponse(w, req, ErrorInvalidSenderHandle, "invalid senderHandle: "+err.Error(), http.StatusBadRequest)
return
}
// Only validate signatures if sender validation is enabled (skip if disabled)
if c.SenderValidationEnabled {
if len(senderRequest.Signature) > 0 {
// Get the pubKey from the corresponding sender paymail address
senderPubKey, err := getSenderPubKey(senderRequest.SenderHandle)
if err != nil {
ErrorResponse(w, req, ErrorInvalidSenderHandle, "invalid senderHandle: "+err.Error(), http.StatusBadRequest)
return
}
// Derive address from pubKey
var rawAddress *bscript.Address
if rawAddress, err = bitcoin.GetAddressFromPubKey(senderPubKey, true); err != nil {
ErrorResponse(w, req, ErrorInvalidSenderHandle, "invalid senderHandle: "+err.Error(), http.StatusBadRequest)
return
}
// Verify the signature
if err = senderRequest.Verify(rawAddress.AddressString, senderRequest.Signature); err != nil {
ErrorResponse(w, req, ErrorInvalidSignature, "invalid signature: "+err.Error(), http.StatusBadRequest)
return
}
} else {
ErrorResponse(w, req, ErrorInvalidSignature, "missing required signature", http.StatusBadRequest)
return
}
}
// Create the metadata struct
md := CreateMetadata(req, alias, domain, "")
md.ResolveAddress = senderRequest
// 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
}
// Get the resolution information
var response *paymail.ResolutionPayload
if response, err = c.actions.CreateAddressResolutionResponse(
req.Context(), alias, domain, c.SenderValidationEnabled, md,
); err != nil {
ErrorResponse(w, req, ErrorScript, "error creating output script: "+err.Error(), http.StatusExpectationFailed)
return
}
// Return the response
apirouter.ReturnResponse(w, req, http.StatusOK, response)
}
// getSenderPubKey will fetch the pubKey from a PKI request for the sender handle
func getSenderPubKey(senderPaymailAddress string) (*bec.PublicKey, error) {
// Sanitize and break apart
alias, domain, _ := paymail.SanitizePaymail(senderPaymailAddress)
// Load the client
client, err := paymail.NewClient(paymail.WithHTTPTimeout(15 * time.Second))
if err != nil {
return nil, err
}
// Get the SRV record
var srv *net.SRV
if srv, err = client.GetSRVRecord(
paymail.DefaultServiceName, paymail.DefaultProtocol, domain,
); err != nil {
return nil, err
}
// Get the capabilities
// This is required first to get the corresponding PKI endpoint url
var capabilities *paymail.CapabilitiesResponse
if capabilities, err = client.GetCapabilities(
srv.Target, paymail.DefaultPort,
); err != nil {
return nil, err
}
// Extract the PKI URL from the capabilities response
pkiURL := capabilities.GetString(paymail.BRFCPki, paymail.BRFCPkiAlternate)
// Get the actual PKI
var pki *paymail.PKIResponse
if pki, err = client.GetPKI(
pkiURL, alias, domain,
); err != nil {
return nil, err
}
// Convert the string pubKey to a bec.PubKey
return bitcoin.PubKeyFromString(pki.PubKey)
}