This repository was 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 7
/
Copy pathpaymail.go
161 lines (137 loc) · 4.71 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
160
161
package bux
import (
"context"
"errors"
"fmt"
"strings"
"github.com/bitcoin-sv/go-paymail"
"github.com/mrz1836/go-cachestore"
)
// 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!)
var response *paymail.CapabilitiesResponse
srv, err := client.GetSRVRecord(
paymail.DefaultServiceName, paymail.DefaultProtocol, domain,
)
if err != nil {
// Error returned was a real error
if !strings.Contains(err.Error(), "zero SRV records found") { // This error is from no SRV record being found
return nil, err
}
// Try to get capabilities without the SRV record
// 'Should no record be returned, a paymail client should assume a host of <domain>.<tld> and a port of 443.'
// http://bsvalias.org/02-01-host-discovery.html
// Get the capabilities via target
if response, err = client.GetCapabilities(
domain, paymail.DefaultPort,
); err != nil {
return nil, err
}
} else {
// Get the capabilities via SRV record
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, format PaymailPayloadFormat) {
p2pDestinationURL = capabilities.GetString(paymail.BRFCP2PPaymentDestination, "")
p2pSubmitTxURL = capabilities.GetString(paymail.BRFCP2PTransactions, "")
p2pBeefSubmitTxURL := capabilities.GetString(paymail.BRFCBeefTransaction, "")
if len(p2pBeefSubmitTxURL) > 0 {
p2pSubmitTxURL = p2pBeefSubmitTxURL
format = BeefPaymailPayloadFormat
}
if len(p2pSubmitTxURL) > 0 && len(p2pDestinationURL) > 0 {
success = true
}
return
}
// 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(ctx context.Context, client paymail.ClientInterface, p4 *PaymailP4, transaction *Transaction) (*paymail.P2PTransactionPayload, error) {
if transaction.client != nil {
transaction.client.Logger().Info().
Str("txID", transaction.ID).
Msgf("start %s", p4.Format)
}
p2pTransaction, err := buildP2pTx(ctx, p4, transaction)
if err != nil {
return nil, err
}
response, err := client.SendP2PTransaction(p4.ReceiveEndpoint, p4.Alias, p4.Domain, p2pTransaction)
if err != nil {
if transaction.client != nil {
transaction.client.Logger().Info().
Str("txID", transaction.ID).
Msgf("finalizeerror %s, reason: %s", p4.Format, err.Error())
}
return nil, err
}
if transaction.client != nil {
transaction.client.Logger().Info().
Str("txID", transaction.ID).
Msgf("successfully finished %s", p4.Format)
}
return &response.P2PTransactionPayload, nil
}
func buildP2pTx(ctx context.Context, p4 *PaymailP4, transaction *Transaction) (*paymail.P2PTransaction, error) {
p2pTransaction := &paymail.P2PTransaction{
MetaData: &paymail.P2PMetaData{
Note: p4.Note,
Sender: p4.FromPaymail,
},
Reference: p4.ReferenceID,
}
switch p4.Format {
case BeefPaymailPayloadFormat:
beef, err := ToBeef(ctx, transaction, transaction.client)
if err != nil {
return nil, err
}
p2pTransaction.Beef = beef
case BasicPaymailPayloadFormat:
p2pTransaction.Hex = transaction.Hex
default:
return nil, fmt.Errorf("%s is unknown format", p4.Format)
}
return p2pTransaction, nil
}