-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaddress.go
More file actions
executable file
·226 lines (186 loc) · 5.47 KB
/
Copy pathaddress.go
File metadata and controls
executable file
·226 lines (186 loc) · 5.47 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package libwallet
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/muun/libwallet/addresses"
"github.com/muun/libwallet/btcsuitew/btcutilw"
"github.com/muun/libwallet/errors"
"github.com/btcsuite/btcd/txscript"
"google.golang.org/protobuf/proto"
)
// These constants are here for clients usage.
const (
AddressVersionV1 = addresses.V1
AddressVersionV2 = addresses.V2
AddressVersionV3 = addresses.V3
AddressVersionV4 = addresses.V4
AddressVersionV5 = addresses.V5
AddressVersionSwapsV1 = addresses.SubmarineSwapV1
AddressVersionSwapsV2 = addresses.SubmarineSwapV2
)
// MuunPaymentURI is muun's uri struct
type MuunPaymentURI struct {
Address string
Label string
Message string
Amount string
Uri string
Bip70Url string
CreationTime string
ExpiresTime string
Invoice *Invoice
}
const (
bitcoinScheme = "bitcoin:"
muunScheme = "muun:"
)
// GetPaymentURI builds a MuunPaymentURI from text (Bitcoin Uri, Muun Uri or address) and a network
func GetPaymentURI(rawInput string, network *Network) (*MuunPaymentURI, error) {
bitcoinUri, components := buildUriFromString(rawInput, bitcoinScheme)
if components == nil {
return nil, errors.Errorf(ErrInvalidURI, "failed to parse uri %v", rawInput)
}
if components.Scheme != "bitcoin" {
return nil, errors.New(ErrInvalidURI, "Invalid scheme")
}
address := components.Opaque
// When URIs are bitcoin:// the address comes in host
// this happens in iOS that mostly ignores bitcoin: format
if len(address) == 0 {
address = components.Host
}
queryValues, err := url.ParseQuery(components.RawQuery)
if err != nil {
return nil, errors.Errorf(ErrInvalidURI, "Couldn't parse query: %v", err)
}
var label, message, amount string
if len(queryValues["label"]) != 0 {
label = queryValues["label"][0]
}
if len(queryValues["message"]) != 0 {
message = queryValues["message"][0]
}
if len(queryValues["amount"]) != 0 {
amount = queryValues["amount"][0]
}
if len(queryValues["lightning"]) != 0 {
invoice, err := ParseInvoice(queryValues["lightning"][0], network)
if err == nil {
return &MuunPaymentURI{Invoice: invoice}, nil
}
}
// legacy Apollo P2P/contacts check
if (strings.Contains(rawInput, "contacts/")) {
return &MuunPaymentURI{
Label: label,
Message: message,
Amount: amount,
Uri: bitcoinUri,
}, nil
}
//BIP70 check
if len(queryValues["r"]) != 0 {
if len(address) > 0 {
return &MuunPaymentURI{
Address: address,
Label: label,
Message: message,
Amount: amount,
Uri: bitcoinUri,
Bip70Url: queryValues["r"][0],
}, nil
}
return &MuunPaymentURI{
Label: label,
Message: message,
Amount: amount,
Uri: bitcoinUri,
Bip70Url: queryValues["r"][0],
}, nil
}
// Bech32 check
decodedAddress, err := btcutilw.DecodeAddress(address, network.network)
if err != nil {
return nil, fmt.Errorf("invalid address: %w", err)
}
if !decodedAddress.IsForNet(network.network) {
return nil, errors.New(ErrInvalidURI, "Network mismatch")
}
return &MuunPaymentURI{
Address: decodedAddress.String(),
Label: label,
Message: message,
Amount: amount,
Uri: bitcoinUri,
}, nil
}
// DoPaymentRequestCall builds a MuunPaymentUri from a url and a network. Handling BIP70 to 72
func DoPaymentRequestCall(url string, network *Network) (*MuunPaymentURI, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request to: %s", url)
}
req.Header.Set("Accept", "application/bitcoin-paymentrequest")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, errors.Errorf(ErrNetwork, "failed to make request to: %s", url)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Errorf(ErrNetwork, "Failed to read body response: %w", err)
}
payReq := &PaymentRequest{}
err = proto.Unmarshal(body, payReq)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal payment request: %w", err)
}
payDetails := &PaymentDetails{}
err = proto.Unmarshal(payReq.SerializedPaymentDetails, payDetails)
if err != nil {
return nil, fmt.Errorf("failed to unmarshall payment details: %w", err)
}
if len(payDetails.Outputs) == 0 {
return nil, fmt.Errorf("no outputs provided")
}
address, err := getAddressFromScript(payDetails.Outputs[0].Script, network)
if err != nil {
return nil, fmt.Errorf("failed to get address: %w", err)
}
amount := float64(payDetails.Outputs[0].Amount) / 100_000_000
return &MuunPaymentURI{
Address: address,
Message: payDetails.Memo,
Amount: strconv.FormatFloat(amount, 'f', -1, 64),
Bip70Url: url,
CreationTime: strconv.FormatUint(payDetails.Time, 10),
ExpiresTime: strconv.FormatUint(payDetails.Expires, 10),
}, nil
}
func getAddressFromScript(script []byte, network *Network) (string, error) {
pkScript, err := txscript.ParsePkScript(script)
if err != nil {
return "", err
}
address, err := pkScript.Address(network.network)
if err != nil {
return "", err
}
return address.String(), nil
}
func buildUriFromString(rawInput string, targetScheme string) (string, *url.URL) {
newUri := strings.Replace(rawInput, muunScheme, targetScheme, 1)
if !strings.HasPrefix(strings.ToLower(newUri), targetScheme) {
newUri = targetScheme + rawInput
}
components, err := url.Parse(newUri)
if err != nil {
return "", nil
}
return newUri, components
}