|
| 1 | +/** |
| 2 | + * var basex = require('base-x'); |
| 3 | + * |
| 4 | + * This implementation is heavily based on base-x. The main reason to |
| 5 | + * deviate was to prevent the dependency of Buffer. |
| 6 | + * |
| 7 | + * Contributors: |
| 8 | + * |
| 9 | + * base-x encoding |
| 10 | + * Forked from https://github.com/cryptocoinjs/bs58 |
| 11 | + * Originally written by Mike Hearn for BitcoinJ |
| 12 | + * Copyright (c) 2011 Google Inc |
| 13 | + * Ported to JavaScript by Stefan Thomas |
| 14 | + * Merged Buffer refactorings from base58-native by Stephen Pair |
| 15 | + * Copyright (c) 2013 BitPay Inc |
| 16 | + * |
| 17 | + * The MIT License (MIT) |
| 18 | + * |
| 19 | + * Copyright base-x contributors (c) 2016 |
| 20 | + * |
| 21 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 22 | + * copy of this software and associated documentation files (the "Software"), |
| 23 | + * to deal in the Software without restriction, including without limitation |
| 24 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 25 | + * and/or sell copies of the Software, and to permit persons to whom the |
| 26 | + * Software is furnished to do so, subject to the following conditions: |
| 27 | + * |
| 28 | + * The above copyright notice and this permission notice shall be included in |
| 29 | + * all copies or substantial portions of the Software. |
| 30 | +
|
| 31 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 32 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 33 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 34 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 35 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 36 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 37 | + * IN THE SOFTWARE. |
| 38 | + * |
| 39 | + */ |
| 40 | + |
| 41 | +import { arrayify, Arrayish } from "./bytes"; |
| 42 | +import { defineReadOnly } from "./properties"; |
| 43 | + |
| 44 | +export class BaseX { |
| 45 | + readonly alphabet: string; |
| 46 | + readonly base: number; |
| 47 | + |
| 48 | + private _alphabetMap: { [ character: string ]: number }; |
| 49 | + private _leader: string; |
| 50 | + |
| 51 | + constructor(alphabet: string) { |
| 52 | + defineReadOnly(this, "alphabet", alphabet); |
| 53 | + defineReadOnly(this, "base", alphabet.length); |
| 54 | + |
| 55 | + defineReadOnly(this, "_alphabetMap", { }); |
| 56 | + defineReadOnly(this, "_leader", alphabet.charAt(0)); |
| 57 | + |
| 58 | + // pre-compute lookup table |
| 59 | + for (let i = 0; i < alphabet.length; i++) { |
| 60 | + this._alphabetMap[alphabet.charAt(i)] = i; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + encode(value: Arrayish | string): string { |
| 65 | + let source = arrayify(value); |
| 66 | + |
| 67 | + if (source.length === 0) { return ''; } |
| 68 | + |
| 69 | + let digits = [ 0 ] |
| 70 | + for (let i = 0; i < source.length; ++i) { |
| 71 | + let carry = source[i]; |
| 72 | + for (let j = 0; j < digits.length; ++j) { |
| 73 | + carry += digits[j] << 8; |
| 74 | + digits[j] = carry % this.base; |
| 75 | + carry = (carry / this.base) | 0; |
| 76 | + } |
| 77 | + |
| 78 | + while (carry > 0) { |
| 79 | + digits.push(carry % this.base); |
| 80 | + carry = (carry / this.base) | 0; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + let string = '' |
| 85 | + |
| 86 | + // deal with leading zeros |
| 87 | + for (let k = 0; source[k] === 0 && k < source.length - 1; ++k) { |
| 88 | + string += this._leader; |
| 89 | + } |
| 90 | + |
| 91 | + // convert digits to a string |
| 92 | + for (let q = digits.length - 1; q >= 0; --q) { |
| 93 | + string += this.alphabet[digits[q]]; |
| 94 | + } |
| 95 | + |
| 96 | + return string; |
| 97 | + } |
| 98 | + |
| 99 | + decode(value: string): Uint8Array { |
| 100 | + if (typeof(value) !== 'string') { |
| 101 | + throw new TypeError('Expected String'); |
| 102 | + } |
| 103 | + |
| 104 | + let bytes: Array<number> = []; |
| 105 | + if (value.length === 0) { return new Uint8Array(bytes); } |
| 106 | + |
| 107 | + bytes.push(0); |
| 108 | + for (let i = 0; i < value.length; i++) { |
| 109 | + let byte = this._alphabetMap[value[i]]; |
| 110 | + |
| 111 | + if (byte === undefined) { |
| 112 | + throw new Error('Non-base' + this.base + ' character'); |
| 113 | + } |
| 114 | + |
| 115 | + let carry = byte; |
| 116 | + for (let j = 0; j < bytes.length; ++j) { |
| 117 | + carry += bytes[j] * this.base; |
| 118 | + bytes[j] = carry & 0xff; |
| 119 | + carry >>= 8; |
| 120 | + } |
| 121 | + |
| 122 | + while (carry > 0) { |
| 123 | + bytes.push(carry & 0xff); |
| 124 | + carry >>= 8; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // deal with leading zeros |
| 129 | + for (let k = 0; value[k] === this._leader && k < value.length - 1; ++k) { |
| 130 | + bytes.push(0) |
| 131 | + } |
| 132 | + |
| 133 | + return new Uint8Array(bytes.reverse()) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +const Base32 = new BaseX("abcdefghijklmnopqrstuvwxyz234567"); |
| 138 | +const Base58 = new BaseX("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"); |
| 139 | + |
| 140 | +export { Base32, Base58 }; |
| 141 | + |
| 142 | +//console.log(Base58.decode("Qmd2V777o5XvJbYMeMb8k2nU5f8d3ciUQ5YpYuWhzv8iDj")) |
| 143 | +//console.log(Base58.encode(Base58.decode("Qmd2V777o5XvJbYMeMb8k2nU5f8d3ciUQ5YpYuWhzv8iDj"))) |
0 commit comments