2019-05-15 01:48:48 +03:00
|
|
|
/**
|
|
|
|
* var basex = require("base-x");
|
|
|
|
*
|
|
|
|
* This implementation is heavily based on base-x. The main reason to
|
|
|
|
* deviate was to prevent the dependency of Buffer.
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
*
|
|
|
|
* base-x encoding
|
|
|
|
* Forked from https://github.com/cryptocoinjs/bs58
|
|
|
|
* Originally written by Mike Hearn for BitcoinJ
|
|
|
|
* Copyright (c) 2011 Google Inc
|
|
|
|
* Ported to JavaScript by Stefan Thomas
|
|
|
|
* Merged Buffer refactorings from base58-native by Stephen Pair
|
|
|
|
* Copyright (c) 2013 BitPay Inc
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright base-x contributors (c) 2016
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
2019-08-25 09:39:20 +03:00
|
|
|
import { arrayify } from "@ethersproject/bytes";
|
|
|
|
import { defineReadOnly } from "@ethersproject/properties";
|
|
|
|
export class BaseX {
|
|
|
|
constructor(alphabet) {
|
|
|
|
defineReadOnly(this, "alphabet", alphabet);
|
|
|
|
defineReadOnly(this, "base", alphabet.length);
|
|
|
|
defineReadOnly(this, "_alphabetMap", {});
|
|
|
|
defineReadOnly(this, "_leader", alphabet.charAt(0));
|
2019-05-15 01:48:48 +03:00
|
|
|
// pre-compute lookup table
|
2019-08-25 09:39:20 +03:00
|
|
|
for (let i = 0; i < alphabet.length; i++) {
|
2019-05-15 01:48:48 +03:00
|
|
|
this._alphabetMap[alphabet.charAt(i)] = i;
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
encode(value) {
|
|
|
|
let source = arrayify(value);
|
2019-05-15 01:48:48 +03:00
|
|
|
if (source.length === 0) {
|
|
|
|
return "";
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let digits = [0];
|
|
|
|
for (let i = 0; i < source.length; ++i) {
|
|
|
|
let carry = source[i];
|
|
|
|
for (let j = 0; j < digits.length; ++j) {
|
2019-05-15 01:48:48 +03:00
|
|
|
carry += digits[j] << 8;
|
|
|
|
digits[j] = carry % this.base;
|
|
|
|
carry = (carry / this.base) | 0;
|
|
|
|
}
|
|
|
|
while (carry > 0) {
|
|
|
|
digits.push(carry % this.base);
|
|
|
|
carry = (carry / this.base) | 0;
|
|
|
|
}
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let string = "";
|
2019-05-15 01:48:48 +03:00
|
|
|
// deal with leading zeros
|
2019-08-25 09:39:20 +03:00
|
|
|
for (let k = 0; source[k] === 0 && k < source.length - 1; ++k) {
|
2019-05-15 01:48:48 +03:00
|
|
|
string += this._leader;
|
|
|
|
}
|
|
|
|
// convert digits to a string
|
2019-08-25 09:39:20 +03:00
|
|
|
for (let q = digits.length - 1; q >= 0; --q) {
|
2019-05-15 01:48:48 +03:00
|
|
|
string += this.alphabet[digits[q]];
|
|
|
|
}
|
|
|
|
return string;
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
decode(value) {
|
2019-05-15 01:48:48 +03:00
|
|
|
if (typeof (value) !== "string") {
|
|
|
|
throw new TypeError("Expected String");
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let bytes = [];
|
2019-05-15 01:48:48 +03:00
|
|
|
if (value.length === 0) {
|
|
|
|
return new Uint8Array(bytes);
|
|
|
|
}
|
|
|
|
bytes.push(0);
|
2019-08-25 09:39:20 +03:00
|
|
|
for (let i = 0; i < value.length; i++) {
|
|
|
|
let byte = this._alphabetMap[value[i]];
|
2019-05-15 01:48:48 +03:00
|
|
|
if (byte === undefined) {
|
|
|
|
throw new Error("Non-base" + this.base + " character");
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
let carry = byte;
|
|
|
|
for (let j = 0; j < bytes.length; ++j) {
|
2019-05-15 01:48:48 +03:00
|
|
|
carry += bytes[j] * this.base;
|
|
|
|
bytes[j] = carry & 0xff;
|
|
|
|
carry >>= 8;
|
|
|
|
}
|
|
|
|
while (carry > 0) {
|
|
|
|
bytes.push(carry & 0xff);
|
|
|
|
carry >>= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// deal with leading zeros
|
2019-08-25 09:39:20 +03:00
|
|
|
for (let k = 0; value[k] === this._leader && k < value.length - 1; ++k) {
|
2019-05-15 01:48:48 +03:00
|
|
|
bytes.push(0);
|
|
|
|
}
|
2019-08-25 09:39:20 +03:00
|
|
|
return arrayify(new Uint8Array(bytes.reverse()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const Base32 = new BaseX("abcdefghijklmnopqrstuvwxyz234567");
|
|
|
|
const Base58 = new BaseX("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz");
|
|
|
|
export { Base32, Base58 };
|
2019-05-15 01:48:48 +03:00
|
|
|
//console.log(Base58.decode("Qmd2V777o5XvJbYMeMb8k2nU5f8d3ciUQ5YpYuWhzv8iDj"))
|
|
|
|
//console.log(Base58.encode(Base58.decode("Qmd2V777o5XvJbYMeMb8k2nU5f8d3ciUQ5YpYuWhzv8iDj")))
|