|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using NBitcoin.DataEncoders; |
| 5 | + |
| 6 | +namespace NBitcoin.Altcoins.Elements |
| 7 | +{ |
| 8 | + public class Blech32Encoder : Bech32Encoder |
| 9 | + { |
| 10 | + protected static readonly ulong[] Generator = { 0x7d52fba40bd886, 0x5e8dbf1a03950c, 0x1c3a3c74072a18, 0x385d72fa0e5139, 0x7093e5a608865b }; |
| 11 | + |
| 12 | + private static ulong Polymod(byte[] values) |
| 13 | + { |
| 14 | + ulong chk = 1; |
| 15 | + foreach (var value in values) |
| 16 | + { |
| 17 | + var top = chk >> 55; |
| 18 | + chk = value ^ ((chk & 0x7fffffffffffff) << |
| 19 | + 5); |
| 20 | + foreach (var i in Enumerable.Range(0, 5)) |
| 21 | + { |
| 22 | + chk ^= ((top >> i) & 1) == 1 ? Generator[i] : 0; |
| 23 | + } |
| 24 | + } |
| 25 | + return chk; |
| 26 | + } |
| 27 | + |
| 28 | + protected override bool VerifyChecksum(byte[] data, int bechStringLen, out int[] errorPosition) |
| 29 | + { |
| 30 | + var values = _HrpExpand.Concat(data); |
| 31 | + errorPosition = new int[0]; |
| 32 | + return Polymod(values) == 1; |
| 33 | + } |
| 34 | + |
| 35 | + private byte[] CreateChecksum(byte[] data, int offset, int count) |
| 36 | + { |
| 37 | + var values = new byte[_HrpExpand.Length + count + 12]; |
| 38 | + var valuesOffset = 0; |
| 39 | + Array.Copy(_HrpExpand, 0, values, valuesOffset, _HrpExpand.Length); |
| 40 | + valuesOffset += _HrpExpand.Length; |
| 41 | + Array.Copy(data, offset, values, valuesOffset, count); |
| 42 | + valuesOffset += count; |
| 43 | + var polymod = Polymod(values) ^ 1; |
| 44 | + var ret = new byte[12]; |
| 45 | + foreach (var i in Enumerable.Range(0, 12)) |
| 46 | + { |
| 47 | + ret[i] = (byte)((polymod >> 5 * (11 - i)) & 31); |
| 48 | + } |
| 49 | + return ret; |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public override string EncodeData(byte[] data, int offset, int count) |
| 54 | + { |
| 55 | + var combined = new byte[_Hrp.Length + 1 + count + 12]; |
| 56 | + int combinedOffset = 0; |
| 57 | + Array.Copy(_Hrp, 0, combined, 0, _Hrp.Length); |
| 58 | + combinedOffset += _Hrp.Length; |
| 59 | + combined[combinedOffset] = 49; |
| 60 | + combinedOffset++; |
| 61 | + Array.Copy(data, offset, combined, combinedOffset, count); |
| 62 | + combinedOffset += count; |
| 63 | + var checkSum = CreateChecksum(data, offset, count); |
| 64 | + Array.Copy(checkSum, 0, combined, combinedOffset, 12); |
| 65 | + for (int i = 0; i < count + 12; i++) |
| 66 | + { |
| 67 | + combined[_Hrp.Length + 1 + i] = Byteset[combined[_Hrp.Length + 1 + i]]; |
| 68 | + } |
| 69 | + return DataEncoders.Encoders.ASCII.EncodeData(combined); |
| 70 | + } |
| 71 | + |
| 72 | + public new static Blech32Encoder ExtractEncoderFromString(string test) |
| 73 | + { |
| 74 | + var i = test.IndexOf('1'); |
| 75 | + if (i == -1) |
| 76 | + throw new FormatException("Invalid Blech32 string"); |
| 77 | + return Encoders.Blech32(test.Substring(0, i)); |
| 78 | + } |
| 79 | + |
| 80 | + protected override void CheckCase(string hrp) |
| 81 | + { |
| 82 | + if (hrp.ToLowerInvariant().Equals(hrp)) |
| 83 | + return; |
| 84 | + if (hrp.ToUpperInvariant().Equals(hrp)) |
| 85 | + return; |
| 86 | + throw new FormatException("Invalid blech32 string, mixed case detected"); |
| 87 | + } |
| 88 | + |
| 89 | + protected override byte[] DecodeDataCore(string encoded) |
| 90 | + { |
| 91 | + if (encoded == null) |
| 92 | + throw new ArgumentNullException(nameof(encoded)); |
| 93 | + CheckCase(encoded); |
| 94 | + var buffer = DataEncoders.Encoders.ASCII.DecodeData(encoded); |
| 95 | + if (buffer.Any(b => b < 33 || b > 126)) |
| 96 | + { |
| 97 | + throw new FormatException("bech chars are out of range"); |
| 98 | + } |
| 99 | + encoded = encoded.ToLowerInvariant(); |
| 100 | + buffer = DataEncoders.Encoders.ASCII.DecodeData(encoded); |
| 101 | + var pos = encoded.LastIndexOf("1", StringComparison.OrdinalIgnoreCase); |
| 102 | + if (encoded.Length > 1000 || pos == -1 || pos == 0 || pos + 13 > encoded.Length) |
| 103 | + { // ELEMENTS: 90->1000, 7->13 |
| 104 | + |
| 105 | + throw new FormatException("blech missing separator, separator misplaced or too long input"); |
| 106 | + } |
| 107 | + if (buffer.Skip(pos + 1).Any(x => !Byteset.Contains(x))) |
| 108 | + { |
| 109 | + throw new FormatException("bech chars are out of range"); |
| 110 | + } |
| 111 | + |
| 112 | + buffer = DataEncoders.Encoders.ASCII.DecodeData(encoded); |
| 113 | + var hrp = DataEncoders.Encoders.ASCII.DecodeData(encoded.Substring(0, pos)); |
| 114 | + if (!hrp.SequenceEqual(_Hrp)) |
| 115 | + { |
| 116 | + throw new FormatException("Mismatching human readable part"); |
| 117 | + } |
| 118 | + var data = new byte[encoded.Length - pos - 1]; |
| 119 | + for (int j = 0, i = pos + 1; i < encoded.Length; i++, j++) |
| 120 | + { |
| 121 | + data[j] = (byte)Array.IndexOf(Byteset, buffer[i]); |
| 122 | + } |
| 123 | + |
| 124 | + if (!VerifyChecksum(data, encoded.Length, out var _)) |
| 125 | + { |
| 126 | + throw new FormatException("Error while verifying Blech32 checksum"); |
| 127 | + } |
| 128 | + return data.Take(data.Length - 12).ToArray(); |
| 129 | + } |
| 130 | + |
| 131 | + protected override byte[] ConvertBits(IEnumerable<byte> data, int fromBits, int toBits, bool pad = true) |
| 132 | + { |
| 133 | + var acc = 0; |
| 134 | + var bits = 0; |
| 135 | + var maxv = (1 << toBits) - 1; |
| 136 | + var ret = new List<byte>(); |
| 137 | + foreach (var value in data) |
| 138 | + { |
| 139 | + if ((value >> fromBits) > 0) |
| 140 | + throw new FormatException("Invalid Blech32 string"); |
| 141 | + acc = (acc << fromBits) | value; |
| 142 | + bits += fromBits; |
| 143 | + while (bits >= toBits) |
| 144 | + { |
| 145 | + bits -= toBits; |
| 146 | + ret.Add((byte)((acc >> bits) & maxv)); |
| 147 | + } |
| 148 | + } |
| 149 | + if (pad) |
| 150 | + { |
| 151 | + if (bits > 0) |
| 152 | + { |
| 153 | + ret.Add((byte)((acc << (toBits - bits)) & maxv)); |
| 154 | + } |
| 155 | + } |
| 156 | + else if (bits >= fromBits || (byte)(((acc << (toBits - bits)) & maxv)) != 0) |
| 157 | + { |
| 158 | + throw new FormatException("Invalid Blech32 string"); |
| 159 | + } |
| 160 | + return ret.ToArray(); |
| 161 | + } |
| 162 | + |
| 163 | + public override byte[] Decode(string addr, out byte witnessVerion) |
| 164 | + { |
| 165 | + if (addr == null) |
| 166 | + throw new ArgumentNullException(nameof(addr)); |
| 167 | + CheckCase(addr); |
| 168 | + var data = DecodeDataCore(addr); |
| 169 | + witnessVerion = data[0]; |
| 170 | + |
| 171 | + var decoded = ConvertBits(data.Skip(1), 5, 8, false); |
| 172 | + if (decoded.Length < 34) |
| 173 | + throw new FormatException("Invalid decoded data length"); |
| 174 | + return decoded; |
| 175 | + } |
| 176 | + |
| 177 | + internal Blech32Encoder(string hrp) : base(hrp) |
| 178 | + { |
| 179 | + } |
| 180 | + |
| 181 | + public Blech32Encoder(byte[] hrp) : base(hrp) |
| 182 | + { |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments