Skip to content

Commit 20ace56

Browse files
committed
Merge branch 'master' of https://github.com/codebude/QRCoder
2 parents 9b80f16 + 254f3b7 commit 20ace56

File tree

3 files changed

+216
-7
lines changed

3 files changed

+216
-7
lines changed

QRCoder/PayloadGenerator.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,20 +590,23 @@ public enum AddressOrder
590590
}
591591
}
592592

593-
public class BitcoinAddress : Payload
593+
public class BitcoinLikeCryptoCurrencyAddress : Payload
594594
{
595+
private readonly BitcoinLikeCryptoCurrencyType currencyType;
595596
private readonly string address, label, message;
596597
private readonly double? amount;
597598

598599
/// <summary>
599-
/// Generates a Bitcoin payment payload. QR Codes with this payload can open a Bitcoin payment app.
600+
/// Generates a Bitcoin like cryptocurrency payment payload. QR Codes with this payload can open a payment app.
600601
/// </summary>
601-
/// <param name="address">Bitcoin address of the payment receiver</param>
602-
/// <param name="amount">Amount of Bitcoins to transfer</param>
602+
/// <param name="currencyName">Bitcoin like cryptocurrency address of the payment receiver</param>
603+
/// <param name="address">Bitcoin like cryptocurrency address of the payment receiver</param>
604+
/// <param name="amount">Amount of coins to transfer</param>
603605
/// <param name="label">Reference label</param>
604606
/// <param name="message">Referece text aka message</param>
605-
public BitcoinAddress(string address, double? amount, string label = null, string message = null)
607+
public BitcoinLikeCryptoCurrencyAddress(BitcoinLikeCryptoCurrencyType currencyType, string address, double? amount, string label = null, string message = null)
606608
{
609+
this.currencyType = currencyType;
607610
this.address = address;
608611

609612
if (!string.IsNullOrEmpty(label))
@@ -637,10 +640,35 @@ public override string ToString()
637640
.ToArray());
638641
}
639642

640-
return $"bitcoin:{address}{query}";
643+
return $"{Enum.GetName(typeof(BitcoinLikeCryptoCurrencyType), currencyType).ToLower()}:{address}{query}";
644+
}
645+
646+
public enum BitcoinLikeCryptoCurrencyType
647+
{
648+
Bitcoin,
649+
BitcoinCash,
650+
Litecoin
641651
}
642652
}
643653

654+
public class BitcoinAddress : BitcoinLikeCryptoCurrencyAddress
655+
{
656+
public BitcoinAddress(string address, double? amount, string label = null, string message = null)
657+
: base(BitcoinLikeCryptoCurrencyType.Bitcoin, address, amount, label, message) { }
658+
}
659+
660+
public class BitcoinCashAddress : BitcoinLikeCryptoCurrencyAddress
661+
{
662+
public BitcoinCashAddress(string address, double? amount, string label = null, string message = null)
663+
: base(BitcoinLikeCryptoCurrencyType.BitcoinCash, address, amount, label, message) { }
664+
}
665+
666+
public class LitecoinAddress : BitcoinLikeCryptoCurrencyAddress
667+
{
668+
public LitecoinAddress(string address, double? amount, string label = null, string message = null)
669+
: base(BitcoinLikeCryptoCurrencyType.Litecoin, address, amount, label, message) { }
670+
}
671+
644672
public class SwissQrCode : Payload
645673
{
646674
//Keep in mind, that the ECC level has to be set to "M" when generating a SwissQrCode!

QRCoderTests/PayloadGeneratorTests.cs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,187 @@ public void bitcoin_address_generator_disregards_current_culture()
107107
#endif
108108
}
109109

110+
[Fact]
111+
[Category("PayloadGenerator/BitcoinCashAddress")]
112+
public void bitcoincash_address_generator_can_generate_address()
113+
{
114+
var address = "qqtlfk37qyey50f4wfuhc7jw85zsdp8s2swffjk890";
115+
var amount = .123;
116+
var label = "Some Label to Encode";
117+
var message = "Some Message to Encode";
118+
119+
var generator = new PayloadGenerator.BitcoinCashAddress(address, amount, label, message);
120+
121+
generator
122+
.ToString()
123+
.ShouldBe("bitcoincash:qqtlfk37qyey50f4wfuhc7jw85zsdp8s2swffjk890?label=Some%20Label%20to%20Encode&message=Some%20Message%20to%20Encode&amount=.123");
124+
}
125+
126+
[Fact]
127+
[Category("PayloadGenerator/BitcoinCashAddress")]
128+
public void bitcoincash_address_generator_should_skip_missing_label()
129+
{
130+
var address = "qqtlfk37qyey50f4wfuhc7jw85zsdp8s2swffjk890";
131+
var amount = .123;
132+
var message = "Some Message to Encode";
133+
134+
135+
var generator = new PayloadGenerator.BitcoinCashAddress(address, amount, null, message);
136+
137+
generator
138+
.ToString()
139+
.ShouldNotContain("label");
140+
}
141+
142+
[Fact]
143+
[Category("PayloadGenerator/BitcoinCashAddress")]
144+
public void bitcoincash_address_generator_should_skip_missing_message()
145+
{
146+
var address = "qqtlfk37qyey50f4wfuhc7jw85zsdp8s2swffjk890";
147+
var amount = .123;
148+
149+
150+
var generator = new PayloadGenerator.BitcoinCashAddress(address, amount);
151+
152+
generator
153+
.ToString()
154+
.ShouldNotContain("message");
155+
}
156+
157+
[Fact]
158+
[Category("PayloadGenerator/BitcoinCashAddress")]
159+
public void bitcoincash_address_generator_should_round_to_satoshi()
160+
{
161+
var address = "qqtlfk37qyey50f4wfuhc7jw85zsdp8s2swffjk890";
162+
var amount = .123456789;
163+
164+
165+
var generator = new PayloadGenerator.BitcoinCashAddress(address, amount);
166+
167+
generator
168+
.ToString()
169+
.ShouldContain("amount=.12345679");
170+
}
171+
172+
[Fact]
173+
[Category("PayloadGenerator/BitcoinCashAddress")]
174+
public void bitcoincash_address_generator_disregards_current_culture()
175+
{
176+
#if NETCOREAPP1_1
177+
var currentCulture = CultureInfo.DefaultThreadCurrentCulture;
178+
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("de-DE");
179+
#else
180+
var currentCulture = Thread.CurrentThread.CurrentCulture;
181+
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
182+
#endif
183+
184+
var address = "qqtlfk37qyey50f4wfuhc7jw85zsdp8s2swffjk890";
185+
var amount = .123;
186+
187+
188+
var generator = new PayloadGenerator.BitcoinCashAddress(address, amount);
189+
190+
generator
191+
.ToString()
192+
.ShouldBe("bitcoincash:qqtlfk37qyey50f4wfuhc7jw85zsdp8s2swffjk890?amount=.123");
193+
194+
#if NETCOREAPP1_1
195+
CultureInfo.DefaultThreadCurrentCulture = currentCulture;
196+
#else
197+
Thread.CurrentThread.CurrentCulture = currentCulture;
198+
#endif
199+
}
200+
201+
[Fact]
202+
[Category("PayloadGenerator/LitecoinAddress")]
203+
public void litecoin_address_generator_can_generate_address()
204+
{
205+
var address = "LY1t7iLnwtPCb1DPZP38FA835XzFqXBq54";
206+
var amount = .123;
207+
var label = "Some Label to Encode";
208+
var message = "Some Message to Encode";
209+
210+
var generator = new PayloadGenerator.LitecoinAddress(address, amount, label, message);
211+
212+
generator
213+
.ToString()
214+
.ShouldBe("litecoin:LY1t7iLnwtPCb1DPZP38FA835XzFqXBq54?label=Some%20Label%20to%20Encode&message=Some%20Message%20to%20Encode&amount=.123");
215+
}
216+
217+
[Fact]
218+
[Category("PayloadGenerator/LitecoinAddress")]
219+
public void litecoin_address_generator_should_skip_missing_label()
220+
{
221+
var address = "LY1t7iLnwtPCb1DPZP38FA835XzFqXBq54";
222+
var amount = .123;
223+
var message = "Some Message to Encode";
224+
225+
226+
var generator = new PayloadGenerator.LitecoinAddress(address, amount, null, message);
227+
228+
generator
229+
.ToString()
230+
.ShouldNotContain("label");
231+
}
232+
233+
[Fact]
234+
[Category("PayloadGenerator/LitecoinAddress")]
235+
public void litecoin_address_generator_should_skip_missing_message()
236+
{
237+
var address = "LY1t7iLnwtPCb1DPZP38FA835XzFqXBq54";
238+
var amount = .123;
239+
240+
241+
var generator = new PayloadGenerator.LitecoinAddress(address, amount);
242+
243+
generator
244+
.ToString()
245+
.ShouldNotContain("message");
246+
}
247+
248+
[Fact]
249+
[Category("PayloadGenerator/LitecoinAddress")]
250+
public void litecoin_address_generator_should_round_to_satoshi()
251+
{
252+
var address = "LY1t7iLnwtPCb1DPZP38FA835XzFqXBq54";
253+
var amount = .123456789;
254+
255+
256+
var generator = new PayloadGenerator.LitecoinAddress(address, amount);
257+
258+
generator
259+
.ToString()
260+
.ShouldContain("amount=.12345679");
261+
}
262+
263+
[Fact]
264+
[Category("PayloadGenerator/LitecoinAddress")]
265+
public void litecoin_address_generator_disregards_current_culture()
266+
{
267+
#if NETCOREAPP1_1
268+
var currentCulture = CultureInfo.DefaultThreadCurrentCulture;
269+
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("de-DE");
270+
#else
271+
var currentCulture = Thread.CurrentThread.CurrentCulture;
272+
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
273+
#endif
274+
275+
var address = "LY1t7iLnwtPCb1DPZP38FA835XzFqXBq54";
276+
var amount = .123;
277+
278+
279+
var generator = new PayloadGenerator.LitecoinAddress(address, amount);
280+
281+
generator
282+
.ToString()
283+
.ShouldBe("litecoin:LY1t7iLnwtPCb1DPZP38FA835XzFqXBq54?amount=.123");
284+
285+
#if NETCOREAPP1_1
286+
CultureInfo.DefaultThreadCurrentCulture = currentCulture;
287+
#else
288+
Thread.CurrentThread.CurrentCulture = currentCulture;
289+
#endif
290+
}
110291

111292
[Fact]
112293
[Category("PayloadGenerator/WiFi")]

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ You can learn more about the payload generator [in our Wiki](https://github.com/
127127
The PayloadGenerator supports the following types of payloads:
128128

129129
* [BezahlCode](https://github.com/codebude/QRCoder/wiki/Advanced-usage---Payload-generators#31-bezahlcode)
130-
* [Bitcoin Payment Address](https://github.com/codebude/QRCoder/wiki/Advanced-usage---Payload-generators#32-bitcoin-payment-address)
130+
* [Bitcoin-Like cryptocurrency (Bitcoin, Bitcoin Cash, Litecoin) payment address](https://github.com/codebude/QRCoder/wiki/Advanced-usage---Payload-generators#32-bitcoin-like-crypto-currency-payment-address)
131131
* [Bookmark](https://github.com/codebude/QRCoder/wiki/Advanced-usage---Payload-generators#33-bookmark)
132132
* [Calendar events (iCal/vEvent)](https://github.com/codebude/QRCoder/wiki/Advanced-usage---Payload-generators#34-calendar-events-icalvevent)
133133
* [ContactData (MeCard/vCard)](https://github.com/codebude/QRCoder/wiki/Advanced-usage---Payload-generators#35-contactdata-mecardvcard)

0 commit comments

Comments
 (0)