-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinvoice_test.go
executable file
·215 lines (204 loc) · 6.36 KB
/
invoice_test.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
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
package libwallet
import (
"encoding/hex"
"reflect"
"testing"
)
func TestParseInvoice(t *testing.T) {
const (
invoice = "lnbcrt1pwtpd4xpp55meuklpslk5jtxytyh7u2q490c2xhm68dm3a94486zntsg7ad4vsdqqcqzys763w70h39ze44ngzhdt2mag84wlkefqkphuy7ssg4la5gt9vcpmqts00fnapf8frs928mc5ujfutzyu8apkezhrfvydx82l40w0fckqqmerzjc"
invoiceWithAmount = "lnbcrt10u1pwtpd4jpp5lh0p9amq02xel0gduna95ta5ve9q5dwyk8tglvpa258yzzvcgynsdqqcqzysrukfteknjzcqpu8kfnm76dhdtnkmyr3j42xrl89axhqxmpgusyqhn28u2uaave3nr8sk3mg5nug6t8hcnj2aw8t2l5wtksh6w0yyntgqjrrgqk"
invoiceWithDescription = "lnbcrt1pwtpdh7pp5celcayxvuw9pm9f8420n2dyd3css8ahzlr4nl69uczhf2sf99ydqdqswe5hvcfqwpjhymmwcqzysx7gwcf9a559rxrah9yp0u7dnk4vuvq2ywy6dyqtwzna9c92q058qppmv9p094vq9g6nv46d3sc7jd8faglzjj2h0w7j06wcu2h3e27cqc5zm4d"
invoiceWithFallbackAdrr = "lnbcrt1pwtpduxpp57xglq4thtrerzzxt8wzg4wresfclewh8pk8xghahwq8kgek3qslqdqqcqzysfppqhv0a0uhrt2crdehgfge8e8e6texw3q4hpmge888yuu6076utcrhgc97wu7vydmudyagkz25ahuyp4fqrc9e945ff248cpa3krn7vvgcqq6spyuqltd245sjvwh23gz220cegadspkn3lx0"
invoiceHashHex = "a6f3cb7c30fda925988b25fdc502a57e146bef476ee3d2d6a7d0a6b823dd6d59"
invoiceWithAmountHashHex = "fdde12f7607a8d9fbd0de4fa5a2fb4664a0a35c4b1d68fb03d550e4109984127"
invoiceWithDescriptionHashHex = "c67f8e90cce38a1d9527aa9f35348d8e2103f6e2f8eb3fe8bcc0ae954125291a"
invoiceWithFallbackAddrHashHex = "f191f0557758f23108cb3b848ab8798271fcbae70d8e645fb7700f6466d1043e"
invoiceDestinationHex = "028cfad4e092191a41f081bedfbe5a6e8f441603c78bf9001b8fb62ac0858f20edasd"
)
invoiceDestination, _ := hex.DecodeString(invoiceDestinationHex)
invoicePaymentHash := make([]byte, 32)
hex.Decode(invoicePaymentHash[:], []byte(invoiceHashHex))
invoiceWithAmountPaymentHash := make([]byte, 32)
hex.Decode(invoiceWithAmountPaymentHash[:], []byte(invoiceWithAmountHashHex))
invoiceWithDescriptionPaymentHash := make([]byte, 32)
hex.Decode(invoiceWithDescriptionPaymentHash[:], []byte(invoiceWithDescriptionHashHex))
invoiceWithFallbackAddrPaymentHash := make([]byte, 32)
hex.Decode(invoiceWithFallbackAddrPaymentHash[:], []byte(invoiceWithFallbackAddrHashHex))
fallbackAddr, _ := GetPaymentURI("bcrt1qhv0a0uhrt2crdehgfge8e8e6texw3q4has8jh7", network)
type args struct {
invoice string
network *Network
}
tests := []struct {
name string
args args
want *Invoice
wantErr bool
}{
{
name: "simple invoice",
args: args{
invoice: invoice,
network: network,
},
want: &Invoice{
RawInvoice: invoice,
FallbackAddress: nil,
Network: network,
MilliSat: "",
Destination: invoiceDestination,
PaymentHash: invoicePaymentHash,
Description: "",
},
},
{
name: "simple invoice with scheme",
args: args{
invoice: lightningScheme + invoice,
network: network,
},
want: &Invoice{
RawInvoice: invoice,
FallbackAddress: nil,
Network: network,
MilliSat: "",
Destination: invoiceDestination,
PaymentHash: invoicePaymentHash,
Description: "",
},
},
{
name: "simple invoice with uppercased scheme",
args: args{
invoice: "LIGHTNING:" + invoice,
network: network,
},
want: &Invoice{
RawInvoice: invoice,
FallbackAddress: nil,
Network: network,
MilliSat: "",
Destination: invoiceDestination,
PaymentHash: invoicePaymentHash,
Description: "",
},
},
{
// -amt 1000
name: "invoice with amount",
args: args{
invoice: invoiceWithAmount,
network: network,
},
want: &Invoice{
RawInvoice: invoiceWithAmount,
FallbackAddress: nil,
Network: network,
MilliSat: "1000000",
Sats: 1000,
Destination: invoiceDestination,
PaymentHash: invoiceWithAmountPaymentHash,
Description: "",
},
},
{
// "viva peron"
name: "invoice with description",
args: args{
invoice: invoiceWithDescription,
network: network,
},
want: &Invoice{
RawInvoice: invoiceWithDescription,
FallbackAddress: nil,
Network: network,
MilliSat: "",
Destination: invoiceDestination,
PaymentHash: invoiceWithDescriptionPaymentHash,
Description: "viva peron",
},
},
{
// addr bcrt1qhv0a0uhrt2crdehgfge8e8e6texw3q4has8jh7
name: "invoice with fallback address",
args: args{
invoice: invoiceWithFallbackAdrr,
network: network,
},
want: &Invoice{
RawInvoice: invoiceWithFallbackAdrr,
FallbackAddress: fallbackAddr,
Network: network,
MilliSat: "",
Destination: invoiceDestination,
PaymentHash: invoiceWithFallbackAddrPaymentHash,
Description: "",
},
},
{
name: "invoice with invalid fallback address",
args: args{
invoice: "lnbcrt1pwtpduxpp57xglq4thtrerzzxt8wzg4wresfclewh8pk8xghahwq8kgek3qslqdqqcqzysfppqhv0a0uhrt2crdehgfge8e8e6texw3q4hpmge888yuu6076utcrhgc97wu7vydmudyagkz25ahuyp4fqrc9e945ff248cpa3krn7vvgcqq6spyuqltd245sjvwh23gz220cegadspkn3lx0",
network: Mainnet(),
},
wantErr: true,
},
{
name: "malformed invoice",
args: args{
invoice: "asdasd",
network: network,
},
wantErr: true,
},
{
name: "simple invoice with muun scheme",
args: args{
invoice: muunScheme + invoice,
network: network,
},
want: &Invoice{
RawInvoice: invoice,
FallbackAddress: nil,
Network: network,
MilliSat: "",
Destination: invoiceDestination,
PaymentHash: invoicePaymentHash,
Description: "",
},
},
{
name: "simple invoice with muun:// scheme",
args: args{
invoice: muunScheme + "//" + invoice,
network: network,
},
want: &Invoice{
RawInvoice: invoice,
FallbackAddress: nil,
Network: network,
MilliSat: "",
Destination: invoiceDestination,
PaymentHash: invoicePaymentHash,
Description: "",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseInvoice(tt.args.invoice, tt.args.network)
if (err != nil) != tt.wantErr {
t.Errorf("ParseInvoice() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != nil {
// expiry is relative to now, so ignore it
got.Expiry = 0
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseInvoice() = %v, want %v", got, tt.want)
}
})
}
}