|
3 | 3 | // Distributed under the MIT software license, see the accompanying |
4 | 4 | // file LICENCE or http://www.opensource.org/licenses/mit-license.php. |
5 | 5 |
|
| 6 | +using BitcoinTransactionTool.Backend; |
| 7 | +using BitcoinTransactionTool.Backend.Encoders; |
6 | 8 | using BitcoinTransactionTool.Backend.MVVM; |
| 9 | +using QRCoder; |
| 10 | +using System; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.Drawing; |
| 13 | +using System.IO; |
| 14 | +using System.Linq; |
| 15 | +using System.Text; |
7 | 16 | using System.Windows.Media.Imaging; |
8 | 17 |
|
9 | 18 | namespace BitcoinTransactionTool.ViewModels |
10 | 19 | { |
11 | | - public class QrViewModel : InpcBase |
| 20 | + public class QrViewModel : ViewModelBase |
12 | 21 | { |
13 | | - private BitmapImage qRCode; |
| 22 | + public QrViewModel() |
| 23 | + { |
| 24 | + EncodingList = Enum.GetValues(typeof(Encoders)).Cast<Encoders>(); |
| 25 | + ShowCommand = new RelayCommand(ShowQr, () => !string.IsNullOrEmpty(RawTx)); |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + public enum Encoders |
| 31 | + { |
| 32 | + UTF8, |
| 33 | + Base16, |
| 34 | + Base58, |
| 35 | + Base58Check, |
| 36 | + Base43, |
| 37 | + Base64 |
| 38 | + } |
| 39 | + |
| 40 | + private readonly Base58 b58enc = new Base58(); |
| 41 | + private readonly Base43 b43enc = new Base43(); |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + public IEnumerable<Encoders> EncodingList { get; private set; } |
| 46 | + |
| 47 | + |
| 48 | + private Encoders _encIn; |
| 49 | + public Encoders SelectedInEncoder |
| 50 | + { |
| 51 | + get => _encIn; |
| 52 | + set => SetField(ref _encIn, value); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + private Encoders _encOut; |
| 57 | + public Encoders SelectedOutEncoder |
| 58 | + { |
| 59 | + get => _encOut; |
| 60 | + set => SetField(ref _encOut, value); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + private string _tx; |
| 65 | + public string RawTx |
| 66 | + { |
| 67 | + get => _tx; |
| 68 | + set |
| 69 | + { |
| 70 | + if (SetField(ref _tx, value)) |
| 71 | + { |
| 72 | + ShowCommand.RaiseCanExecuteChanged(); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + private BitmapImage _qr; |
14 | 79 | public BitmapImage QRCode |
15 | 80 | { |
16 | | - get { return qRCode; } |
17 | | - set { SetField(ref qRCode, value); } |
| 81 | + get => _qr; |
| 82 | + private set => SetField(ref _qr, value); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + public RelayCommand ShowCommand { get; private set; } |
| 87 | + private void ShowQr() |
| 88 | + { |
| 89 | + try |
| 90 | + { |
| 91 | + Errors = string.Empty; |
| 92 | + |
| 93 | + byte[] input = GetInput(); |
| 94 | + string converted = (SelectedInEncoder == SelectedOutEncoder) ? RawTx : GetOutput(input); |
| 95 | + |
| 96 | + |
| 97 | + QRCodeGenerator qrGenerator = new QRCodeGenerator(); |
| 98 | + QRCodeData qrCodeData = qrGenerator.CreateQrCode(converted, QRCodeGenerator.ECCLevel.Q); |
| 99 | + QRCode qrCode = new QRCode(qrCodeData); |
| 100 | + Bitmap qrCodeImage = qrCode.GetGraphic(20); |
| 101 | + |
| 102 | + using (MemoryStream memory = new MemoryStream()) |
| 103 | + { |
| 104 | + qrCodeImage.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp); |
| 105 | + memory.Position = 0; |
| 106 | + BitmapImage bitmapimage = new BitmapImage(); |
| 107 | + bitmapimage.BeginInit(); |
| 108 | + bitmapimage.StreamSource = memory; |
| 109 | + bitmapimage.CacheOption = BitmapCacheOption.OnLoad; |
| 110 | + bitmapimage.EndInit(); |
| 111 | + |
| 112 | + QRCode = bitmapimage; |
| 113 | + } |
| 114 | + } |
| 115 | + catch (FormatException ex) |
| 116 | + { |
| 117 | + Errors = $"Invalid input format. Exception details: {ex.Message}"; |
| 118 | + QRCode = null; |
| 119 | + } |
| 120 | + catch (Exception ex) |
| 121 | + { |
| 122 | + Errors = $"An unexpected exception of type {ex.GetType()} was thrown with message: {ex.Message}"; |
| 123 | + QRCode = null; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private byte[] GetInput() |
| 128 | + { |
| 129 | + switch (SelectedInEncoder) |
| 130 | + { |
| 131 | + case Encoders.UTF8: |
| 132 | + return Encoding.UTF8.GetBytes(RawTx); |
| 133 | + case Encoders.Base16: |
| 134 | + return Base16.ToByteArray(RawTx); |
| 135 | + case Encoders.Base58: |
| 136 | + return b58enc.Decode(RawTx); |
| 137 | + case Encoders.Base58Check: |
| 138 | + return b58enc.DecodeWithCheckSum(RawTx); |
| 139 | + case Encoders.Base43: |
| 140 | + return b43enc.Decode(RawTx); |
| 141 | + case Encoders.Base64: |
| 142 | + return Convert.FromBase64String(RawTx); |
| 143 | + default: |
| 144 | + throw new ArgumentException($"Encoder type {SelectedInEncoder} is not defined."); |
| 145 | + } |
18 | 146 | } |
| 147 | + private string GetOutput(byte[] input) |
| 148 | + { |
| 149 | + switch (SelectedOutEncoder) |
| 150 | + { |
| 151 | + case Encoders.UTF8: |
| 152 | + return Encoding.UTF8.GetString(input); |
| 153 | + case Encoders.Base16: |
| 154 | + return input.ToBase16(); |
| 155 | + case Encoders.Base58: |
| 156 | + return b58enc.Encode(input); |
| 157 | + case Encoders.Base58Check: |
| 158 | + return b58enc.EncodeWithCheckSum(input); |
| 159 | + case Encoders.Base43: |
| 160 | + return b43enc.Encode(input); |
| 161 | + case Encoders.Base64: |
| 162 | + return Convert.ToBase64String(input); |
| 163 | + default: |
| 164 | + throw new ArgumentException($"Encoder type {SelectedInEncoder} is not defined."); |
| 165 | + } |
| 166 | + } |
| 167 | + |
19 | 168 | } |
20 | 169 | } |
0 commit comments