Add base-x to libs directory

This commit is contained in:
Ian Coleman
2019-09-12 21:00:45 +10:00
parent 5b2edb3998
commit ac537983d9
5 changed files with 262 additions and 0 deletions

22
libs/base-x/LICENSE.md Normal file
View File

@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2018 base-x contributors
Copyright (c) 2014-2018 The Bitcoin Core developers
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.

44
libs/base-x/package.json Normal file
View File

@@ -0,0 +1,44 @@
{
"name": "base-x",
"version": "3.0.7",
"description": "Fast base encoding / decoding of any given alphabet",
"keywords": [
"base-x",
"base58",
"base62",
"base64",
"crypto",
"crytography",
"decode",
"decoding",
"encode",
"encoding"
],
"homepage": "https://github.com/cryptocoinjs/base-x",
"bugs": {
"url": "https://github.com/cryptocoinjs/base-x/issues"
},
"license": "MIT",
"author": "Daniel Cousens",
"files": [
"src"
],
"main": "src/index.js",
"types": "src/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/cryptocoinjs/base-x.git"
},
"scripts": {
"build": "tsc -p ./tsconfig.json ; standard --fix; browserify src/index.js --standalone basex > /tmp/base-x.js"
},
"devDependencies": {
"@types/node": "12.0.10",
"standard": "^10.0.3",
"tape": "^4.5.1",
"typescript": "3.5.2"
},
"dependencies": {
"safe-buffer": "^5.0.1"
}
}

4
libs/base-x/readme.md Normal file
View File

@@ -0,0 +1,4 @@
Build (will create a bundle and copy it to /tmp/base-x.js):
npm install
npm run build

161
libs/base-x/ts_src/index.ts Normal file
View File

@@ -0,0 +1,161 @@
// base-x encoding / decoding
// Copyright (c) 2018 base-x contributors
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
// @ts-ignore
const _Buffer = require('safe-buffer').Buffer;
function base (ALPHABET: string): base.BaseConverter {
if (ALPHABET.length >= 255) throw new TypeError('Alphabet too long')
const BASE_MAP = new Uint8Array(256)
BASE_MAP.fill(255)
for (let i = 0; i < ALPHABET.length; i++) {
const x = ALPHABET.charAt(i)
const xc = x.charCodeAt(0)
if (BASE_MAP[xc] !== 255) throw new TypeError(x + ' is ambiguous')
BASE_MAP[xc] = i
}
const BASE = ALPHABET.length
const LEADER = ALPHABET.charAt(0)
const FACTOR = Math.log(BASE) / Math.log(256) // log(BASE) / log(256), rounded up
const iFACTOR = Math.log(256) / Math.log(BASE) // log(256) / log(BASE), rounded up
function encode (source: Buffer): string {
if (!_Buffer.isBuffer(source)) throw new TypeError('Expected Buffer')
if (source.length === 0) return ''
// Skip & count leading zeroes.
let zeroes = 0
let length = 0
let pbegin = 0
const pend = source.length
while (pbegin !== pend && source[pbegin] === 0) {
pbegin++
zeroes++
}
// Allocate enough space in big-endian base58 representation.
const size = ((pend - pbegin) * iFACTOR + 1) >>> 0
const b58 = new Uint8Array(size)
// Process the bytes.
while (pbegin !== pend) {
let carry = source[pbegin]
// Apply "b58 = b58 * 256 + ch".
let i = 0
for (let it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
carry += (256 * b58[it1]) >>> 0
b58[it1] = (carry % BASE) >>> 0
carry = (carry / BASE) >>> 0
}
if (carry !== 0) throw new Error('Non-zero carry')
length = i
pbegin++
}
// Skip leading zeroes in base58 result.
let it2 = size - length
while (it2 !== size && b58[it2] === 0) {
it2++
}
// Translate the result into a string.
let str = LEADER.repeat(zeroes)
for (; it2 < size; ++it2) str += ALPHABET.charAt(b58[it2])
return str
}
function decodeUnsafe (source: string): Buffer | undefined {
if (typeof source !== 'string') throw new TypeError('Expected String')
if (source.length === 0) return _Buffer.alloc(0)
let psz = 0
// Skip leading spaces.
if (source[psz] === ' ') return
// Skip and count leading '1's.
let zeroes = 0
let length = 0
while (source[psz] === LEADER) {
zeroes++
psz++
}
// Allocate enough space in big-endian base256 representation.
const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.
const b256 = new Uint8Array(size)
// Process the characters.
while (source[psz]) {
// Decode character
let carry = BASE_MAP[source.charCodeAt(psz)]
// Invalid character
if (carry === 255) return
let i = 0
for (let it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
carry += (BASE * b256[it3]) >>> 0
b256[it3] = (carry % 256) >>> 0
carry = (carry / 256) >>> 0
}
if (carry !== 0) throw new Error('Non-zero carry')
length = i
psz++
}
// Skip trailing spaces.
if (source[psz] === ' ') return
// Skip leading zeroes in b256.
let it4 = size - length
while (it4 !== size && b256[it4] === 0) {
it4++
}
const vch = _Buffer.allocUnsafe(zeroes + (size - it4))
vch.fill(0x00, 0, zeroes)
let j = zeroes
while (it4 !== size) {
vch[j++] = b256[it4++]
}
return vch
}
function decode (string: string): Buffer {
const buffer = decodeUnsafe(string)
if (buffer) return buffer
throw new Error('Non-base' + BASE + ' character')
}
return {
encode: encode,
decodeUnsafe: decodeUnsafe,
decode: decode
}
}
export = base;
declare namespace base {
interface BaseConverter {
encode(buffer: Buffer): string;
decodeUnsafe(string: string): Buffer | undefined;
decode(string: string): Buffer;
}
}

31
libs/base-x/tsconfig.json Normal file
View File

@@ -0,0 +1,31 @@
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"outDir": "./src",
"declaration": true,
"rootDir": "./ts_src",
"types": [
"node"
],
"allowJs": false,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"esModuleInterop": false,
"noUnusedLocals": true,
"noUnusedParameters": true
},
"include": [
"ts_src/**/*.ts"
],
"exclude": [
"**/*.spec.ts",
"node_modules/**/*"
]
}