This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
paymail.go
159 lines (136 loc) · 5.03 KB
/
paymail.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
package bux
import (
"context"
"errors"
"time"
"github.com/mrz1836/go-cachestore"
"github.com/tonicpow/go-paymail"
)
// getCapabilities is a utility function to retrieve capabilities for a Paymail provider
func getCapabilities(ctx context.Context, cs cachestore.ClientInterface, client paymail.ClientInterface,
domain string) (*paymail.CapabilitiesPayload, error) {
// Attempt to get from cachestore
// todo: allow user to configure the time that they want to cache the capabilities (if they want to cache or not)
capabilities := new(paymail.CapabilitiesPayload)
if err := cs.GetModel(
ctx, cacheKeyCapabilities+domain, capabilities,
); err != nil && !errors.Is(err, cachestore.ErrKeyNotFound) {
return nil, err
} else if capabilities != nil && len(capabilities.Capabilities) > 0 {
return capabilities, nil
}
// Get SRV record (domain can be different!)
srv, err := client.GetSRVRecord(
paymail.DefaultServiceName, paymail.DefaultProtocol, domain,
)
if err != nil {
return nil, err
}
// Get the capabilities
var response *paymail.CapabilitiesResponse
if response, err = client.GetCapabilities(
srv.Target, int(srv.Port),
); err != nil {
return nil, err
}
// Save to cachestore
if cs != nil && !cs.Engine().IsEmpty() {
_ = cs.SetModel(
context.Background(), cacheKeyCapabilities+domain,
&response.CapabilitiesPayload, cacheTTLCapabilities,
)
}
return &response.CapabilitiesPayload, nil
}
// hasP2P will return the P2P urls and true if they are both found
func hasP2P(capabilities *paymail.CapabilitiesPayload) (success bool, p2pDestinationURL, p2pSubmitTxURL string) {
p2pDestinationURL = capabilities.GetString(paymail.BRFCP2PPaymentDestination, "")
p2pSubmitTxURL = capabilities.GetString(paymail.BRFCP2PTransactions, "")
if len(p2pSubmitTxURL) > 0 && len(p2pDestinationURL) > 0 {
success = true
}
return
}
// resolvePaymailAddress is an old way to resolve a Paymail address (if P2P is not supported)
//
// Deprecated: this is already deprecated by TSC, use P2P or the new P4
func resolvePaymailAddress(ctx context.Context, cs cachestore.ClientInterface, client paymail.ClientInterface,
capabilities *paymail.CapabilitiesPayload, alias, domain, purpose, senderPaymail string) (*paymail.ResolutionPayload, error) {
// Attempt to get from cachestore
// todo: allow user to configure the time that they want to cache the address resolution (if they want to cache or not)
resolution := new(paymail.ResolutionPayload)
if err := cs.GetModel(
ctx, cacheKeyAddressResolution+alias+"-"+domain, resolution,
); err != nil && !errors.Is(err, cachestore.ErrKeyNotFound) {
return nil, err
} else if resolution != nil && len(resolution.Output) > 0 {
return resolution, nil
}
// Get the URL
addressResolutionURL := capabilities.GetString(
paymail.BRFCBasicAddressResolution, paymail.BRFCPaymentDestination,
)
if len(addressResolutionURL) == 0 {
return nil, ErrMissingAddressResolutionURL
}
// Resolve address
response, err := client.ResolveAddress(
addressResolutionURL,
alias, domain,
&paymail.SenderRequest{
Dt: time.Now().UTC().Format(time.RFC3339), // UTC is assumed
Purpose: purpose, // Generic message about the resolution
SenderHandle: senderPaymail, // Assumed it's a paymail@domain.com
},
)
if err != nil {
return nil, err
}
// Save to cachestore
if cs != nil && !cs.Engine().IsEmpty() {
_ = cs.SetModel(
ctx, cacheKeyAddressResolution+alias+"-"+domain,
&response.ResolutionPayload, cacheTTLAddressResolution,
)
}
return &response.ResolutionPayload, nil
}
// startP2PTransaction will start the P2P transaction, returning the reference ID and outputs
func startP2PTransaction(client paymail.ClientInterface,
alias, domain, p2pDestinationURL string, satoshis uint64) (*paymail.PaymentDestinationPayload, error) {
// Start the P2P transaction request
response, err := client.GetP2PPaymentDestination(
p2pDestinationURL,
alias, domain,
&paymail.PaymentRequest{Satoshis: satoshis},
)
if err != nil {
return nil, err
}
return &response.PaymentDestinationPayload, nil
}
// finalizeP2PTransaction will notify the paymail provider about the transaction
func finalizeP2PTransaction(client paymail.ClientInterface,
alias, domain, p2pSubmitURL, referenceID, note, senderPaymailAddress, txHex string) (*paymail.P2PTransactionPayload, error) {
// Submit the P2P transaction
/*logger.Data(2, logger.DEBUG, "sending p2p tx...",
logger.MakeParameter("alias", alias),
logger.MakeParameter("p2pSubmitURL", p2pSubmitURL),
logger.MakeParameter("domain", domain),
logger.MakeParameter("note", note),
logger.MakeParameter("senderPaymailAddress", senderPaymailAddress),
logger.MakeParameter("referenceID", referenceID),
)*/
response, err := client.SendP2PTransaction(p2pSubmitURL, alias, domain, &paymail.P2PTransaction{
Hex: txHex,
MetaData: &paymail.P2PMetaData{
Note: note,
Sender: senderPaymailAddress,
},
Reference: referenceID,
})
if err != nil {
return nil, err
}
return &response.P2PTransactionPayload, nil
}