Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 23x 23x 799x 23x 70x 70x 70x 2435x 2435x 70x 2x 68x 68x 68x 8x 60x 2x 58x 58x 274x 274x 6x 52x 52x 1882x 1882x 6x 1876x 46x 46x 4x 42x 25x 11x 9x 8x 10x 5x 36x 36x 36x 36x 36x 1059x 1059x 1059x 857x 857x 36x 12x 4x 24x 1x 23x 2x 33x 69x 69x 3623x 3623x 3623x 18115x 7988x 69x 69x 69x 472x 69x 69x 472x 69x 46x 46x 23x 23x 23x 23x 138x 23x | import { BitcoinError } from "./BitcoinError"; import { BitcoinErrorCode } from "./BitcoinErrorCode"; const ALPHABET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; const GENERATOR = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]; export enum Bech32Version { Bech32 = 1, Bech32m = 0x2bc830a3, } /** * Encoding utilities for Bech32 encodings as defined in BIP173: * https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#Bech32 * * This encoding uses a human readable part that contain between 1-83 * US-ASCII characters with char values in the range [33-126]. * * The encoding includes a separator, which is the last "1" in the encoding. * * Data must be at least 6-characters and is either lowercase or uppercase * and excludes the characters "1", "b", "i", and "o". */ export class Bech32 { /** * Encodes the HRP and data according and appends a 6-character * checksum. * @param hrp human readable part * @param words 5-bit words * @param version bech32 or bech32m * @returns BECH32 encoded string */ public static encode( hrp: string, words: number[], version: Bech32Version = Bech32Version.Bech32, ): string { const checksum = createChecksum(hrp, words, version); const combined = words.concat(checksum); let result = hrp + "1"; for (let i = 0; i < combined.length; i++) { result += ALPHABET[combined[i]]; } return result; } /** * Decodes the BECH32 string into the human readable part and a * sequence of 5-bit words. The input can be either upper case or * lower case. The last 6-characters are the checksum. * @param encoded * @returns */ public static decode( encoded: string, ): { hrp: string; words: number[]; version: Bech32Version } { // validate either uppercase or lowercase let hasUpper = false; let hasLower = false; for (const char of encoded) { if (char >= "A" && char <= "Z") hasUpper = true; if (char >= "a" && char <= "z") hasLower = true; } if (hasUpper && hasLower) { throw new BitcoinError(BitcoinErrorCode.InvalidBech32Encoding, encoded); } // convert to lowercase for processing encoded = encoded.toLowerCase(); // validate HRP length const hrpIdx = encoded.lastIndexOf("1"); if (hrpIdx < 1) { throw new BitcoinError(BitcoinErrorCode.InvalidBech32Hrp, encoded); } if (hrpIdx + 7 > encoded.length) { throw new BitcoinError(BitcoinErrorCode.InvalidBech32Hrp, encoded); } const hrp = encoded.substring(0, hrpIdx); // validate hrp characters for (const char of hrp) { const code = char.charCodeAt(0); if (code < 33 || code > 126) { throw new BitcoinError(BitcoinErrorCode.InvalidBech32Hrp, encoded); } } const words: number[] = []; for (let i = hrpIdx + 1; i < encoded.length; i++) { const word = ALPHABET.indexOf(encoded[i]); if (word === -1) { throw new BitcoinError(BitcoinErrorCode.InvalidBech32Encoding, encoded); } words.push(word); } const checksum = calculateChecksum(hrp, words); if (checksum !== Bech32Version.Bech32 && checksum !== Bech32Version.Bech32m) { throw new BitcoinError(BitcoinErrorCode.InvalidBech32Checksum, encoded); } return { hrp, words: words.slice(0, words.length - 6), version: checksum, }; } /** * Converts 5-bit words into 8-byte words in a Buffer. When padding * is enabled incomplete input words are padded to make a complete * output byte. For example, if there are 4-words, this makes 20-bits * but requires 3-bytes (24-bits) of data. Padding includes this 3rd * byte but include zero bits. * @param words * @param pad * @returns */ public static wordsToBuffer(words: number[], pad: boolean): Buffer { return Buffer.from(convertWords(words, 5, 8, pad)); } /** * Converts a buffer of 8-bit words into 5-bit words. Padding is * defaulted to false. For instance if there are 3-bytes (24-bits) * we only have 4 5-bit words (20-bits). The remaining 4-bits will * be dropped when pad is false. Otherwise you will get a zero * word. * @param buffer * @param pad default=false * @returns */ public static bufferToWords(buffer: Buffer, pad: boolean): number[] { return convertWords(buffer, 8, 5, pad); } /** * Calculates the number of words needed to store the number. * In bech32, each word is 5 bits (2^5 = 32). To properly * encode a number, we need to determine how many words * are necessary to encode the value. * * @param num integer value * @returns number of words */ public static sizeofNum(num: number): number { if (num === 0) return 1; return Math.floor(Math.log(num) / Math.log(2) / 5) + 1; } /** * Calculates the number of words needed to store * the supplied number of bits. In bech32, each words is 5 bits * (2^5 = 32). * * @param bits number of bits * @returns number of words */ public static sizeofBits(bits: number): number { return Math.ceil(bits / 5); } /** * Calculates the number of words needeed to store * the supplied number of bytes. In bech32, each words is 5 bits * (2^5 = 32). We first convert the bytes to bits and then * convert to words. * * @param bytes * @returns number of words */ public static sizeofBytes(bytes: number): number { return Bech32.sizeofBits(bytes * 8); } } /** * Converts a Buffer into a a word array and optionally pads bits. * * @param data input data * @param inBits number of bits in, usually 5 or 8 * @param outBits number of bits out, usualy 8 or 5 * @param pad indicates if output should be padded * @return converted words */ function convertWords( data: number[] | Buffer, inBits: number, outBits: number, pad: boolean, ): number[] { let value = 0; let bits = 0; const maxV = (1 << outBits) - 1; const result: number[] = []; for (let i = 0; i < data.length; ++i) { value = (value << inBits) | data[i]; bits += inBits; while (bits >= outBits) { bits -= outBits; result.push((value >> bits) & maxV); } } if (pad) { if (bits > 0) { result.push((value << (outBits - bits)) & maxV); } } else { if (bits >= inBits) { throw new BitcoinError(BitcoinErrorCode.InvalidBech32Encoding, { data, inBits, outBits, pad, reason: "excess padding found", }); } if ((value << (outBits - bits)) & maxV) { throw new BitcoinError(BitcoinErrorCode.InvalidBech32Encoding, { data, inBits, outBits, pad, reason: "failed to encode all data", }); } } return result; } function polymod(values: number[]): number { let chk: number = 1; for (const value of values) { const top = chk >> 25; chk = ((chk & 0x1ffffff) << 5) ^ value; for (let i = 0; i < 5; i++) { if ((top >> i) & 1) { chk ^= GENERATOR[i]; } } } return chk; } function hrpExpand(hrp: string): number[] { const results: number[] = []; for (let i = 0; i < hrp.length; i++) { results.push(hrp.charCodeAt(i) >> 5); } results.push(0); for (let i = 0; i < hrp.length; i++) { results.push(hrp.charCodeAt(i) & 31); } return results; } function calculateChecksum(hrp: string, data: number[]): number { const combined = hrpExpand(hrp).concat(data); return polymod(combined); } function createChecksum(hrp: string, data: number[], constant: Bech32Version): number[] { const values = hrpExpand(hrp) .concat(data) .concat([0, 0, 0, 0, 0, 0]); const mod = polymod(values) ^ constant; const results: number[] = []; for (let i = 0; i < 6; i++) { results.push((mod >> (5 * (5 - i))) & 31); } return results; } |