base-x-3.0.8/000077500000000000000000000000001362512603400127035ustar00rootroot00000000000000base-x-3.0.8/.gitignore000066400000000000000000000000561362512603400146740ustar00rootroot00000000000000node_modules npm-debug.log package-lock.json base-x-3.0.8/.travis.yml000066400000000000000000000003161362512603400150140ustar00rootroot00000000000000sudo: false language: node_js node_js: - "lts/*" - "9" - "10" matrix: include: - node_js: "lts/*" env: TEST_SUITE=standard env: matrix: - TEST_SUITE=unit script: npm run $TEST_SUITE base-x-3.0.8/LICENSE.md000066400000000000000000000021621362512603400143100ustar00rootroot00000000000000The 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. base-x-3.0.8/README.md000066400000000000000000000052241362512603400141650ustar00rootroot00000000000000# base-x [![NPM Package](https://img.shields.io/npm/v/base-x.svg?style=flat-square)](https://www.npmjs.org/package/base-x) [![Build Status](https://img.shields.io/travis/cryptocoinjs/base-x.svg?branch=master&style=flat-square)](https://travis-ci.org/cryptocoinjs/base-x) [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) Fast base encoding / decoding of any given alphabet using bitcoin style leading zero compression. **WARNING:** This module is **NOT RFC3548** compliant, it cannot be used for base16 (hex), base32, or base64 encoding in a standards compliant manner. ## Example Base58 ``` javascript var BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' var bs58 = require('base-x')(BASE58) var decoded = bs58.decode('5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr') console.log(decoded) // => console.log(bs58.encode(decoded)) // => 5Kd3NBUAdUnhyzenEwVLy9pBKxSwXvE9FMPyR4UKZvpe6E3AgLr ``` ### Alphabets See below for a list of commonly recognized alphabets, and their respective base. Base | Alphabet ------------- | ------------- 2 | `01` 8 | `01234567` 11 | `0123456789a` 16 | `0123456789abcdef` 32 | `0123456789ABCDEFGHJKMNPQRSTVWXYZ` 32 | `ybndrfg8ejkmcpqxot1uwisza345h769` (z-base-32) 36 | `0123456789abcdefghijklmnopqrstuvwxyz` 58 | `123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz` 62 | `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ` 64 | `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/` 66 | `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.!~` ## How it works It encodes octet arrays by doing long divisions on all significant digits in the array, creating a representation of that number in the new base. Then for every leading zero in the input (not significant as a number) it will encode as a single leader character. This is the first in the alphabet and will decode as 8 bits. The other characters depend upon the base. For example, a base58 alphabet packs roughly 5.858 bits per character. This means the encoded string 000f (using a base16, 0-f alphabet) will actually decode to 4 bytes unlike a canonical hex encoding which uniformly packs 4 bits into each character. While unusual, this does mean that no padding is required and it works for bases like 43. ## LICENSE [MIT](LICENSE) A direct derivation of the base58 implementation from [`bitcoin/bitcoin`](https://github.com/bitcoin/bitcoin/blob/f1e2f2a85962c1664e4e55471061af0eaa798d40/src/base58.cpp), generalized for variable length alphabets. base-x-3.0.8/benchmark/000077500000000000000000000000001362512603400146355ustar00rootroot00000000000000base-x-3.0.8/benchmark/index.js000066400000000000000000000032721362512603400163060ustar00rootroot00000000000000'use strict' var crypto = require('crypto') var benchmark = require('benchmark') var XorShift128Plus = require('xorshift.js').XorShift128Plus var bs58ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' var bs58 = require('../')(bs58ALPHABET) var fixtureIndex = 0 var resetFixtureIndex = function () { fixtureIndex = 0 } var fixtures = new Array(10000) var getNextFixture = function () { var fixture = fixtures[fixtureIndex++] if (fixtureIndex === fixtures.length) { fixtureIndex = 0 } return fixture } var seed = process.env.SEED || crypto.randomBytes(16).toString('hex') console.log('Seed: ' + seed) var prng = new XorShift128Plus(seed) for (var i = 0; i < fixtures.length; ++i) { let source = prng.randomBytes(32) fixtures[i] = { source, string: bs58.encode(source) } } if (/fast/i.test(process.argv[2])) { console.log('Running in fast mode...') benchmark.options.minTime = 0.3 benchmark.options.maxTime = 1 benchmark.options.minSamples = 3 } else { benchmark.options.minTime = 1 } new benchmark.Suite({ onStart: function () { console.log('--------------------------------------------------') }, onCycle: function (event) { console.log(String(event.target)) }, onError: function (event) { console.error(event.target.error) }, onComplete: function () { console.log('==================================================') } }) .add('encode', function () { var fixture = getNextFixture() bs58.encode(fixture.source) }, {onStart: resetFixtureIndex, onCycle: resetFixtureIndex}) .add('decode', function () { var fixture = getNextFixture() bs58.decode(fixture.string) }, {onStart: resetFixtureIndex, onCycle: resetFixtureIndex}) .run() base-x-3.0.8/benchmark/package.json000066400000000000000000000004541362512603400171260ustar00rootroot00000000000000{ "name": "base-x-benchmark", "version": "0.0.0", "description": "", "scripts": { "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "MIT", "dependencies": { "benchmark": "^1.0.0", "xorshift.js": "^1.0.3" } } base-x-3.0.8/package.json000066400000000000000000000021251362512603400151710ustar00rootroot00000000000000{ "name": "base-x", "version": "3.0.8", "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", "gitdiff": "npm run build && git diff --exit-code", "prepublish": "npm run gitdiff", "standard": "standard", "test": "npm run unit && npm run standard", "unit": "tape test/*.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" } } base-x-3.0.8/test/000077500000000000000000000000001362512603400136625ustar00rootroot00000000000000base-x-3.0.8/test/fixtures.json000066400000000000000000000331701362512603400164320ustar00rootroot00000000000000{ "alphabets": { "base2": "01", "base16": "0123456789abcdef", "base58": "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" }, "valid": [ { "alphabet": "base2", "hex": "000f", "string": "01111" }, { "alphabet": "base2", "hex": "00ff", "comment": "Note the first leading zero byte is compressed into 1 char", "string": "011111111" }, { "alphabet": "base2", "hex": "0fff", "string": "111111111111" }, { "alphabet": "base2", "hex": "ff00ff00", "string": "11111111000000001111111100000000" }, { "alphabet": "base16", "hex": "0000000f", "string": "000f" }, { "alphabet": "base16", "hex": "000fff", "string": "0fff" }, { "alphabet": "base16", "hex": "ffff", "string": "ffff" }, { "alphabet": "base58", "hex": "", "string": "" }, { "alphabet": "base58", "hex": "61", "string": "2g" }, { "alphabet": "base58", "hex": "626262", "string": "a3gV" }, { "alphabet": "base58", "hex": "636363", "string": "aPEr" }, { "alphabet": "base58", "hex": "73696d706c792061206c6f6e6720737472696e67", "string": "2cFupjhnEsSn59qHXstmK2ffpLv2" }, { "alphabet": "base58", "hex": "00eb15231dfceb60925886b67d065299925915aeb172c06647", "string": "1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L" }, { "alphabet": "base58", "hex": "516b6fcd0f", "string": "ABnLTmg" }, { "alphabet": "base58", "hex": "bf4f89001e670274dd", "string": "3SEo3LWLoPntC" }, { "alphabet": "base58", "hex": "572e4794", "string": "3EFU7m" }, { "alphabet": "base58", "hex": "ecac89cad93923c02321", "string": "EJDM8drfXA6uyA" }, { "alphabet": "base58", "hex": "10c8511e", "string": "Rt5zm" }, { "alphabet": "base58", "hex": "00000000000000000000", "string": "1111111111" }, { "alphabet": "base58", "hex": "801184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd206ec97e", "string": "5Hx15HFGyep2CfPxsJKe2fXJsCVn5DEiyoeGGF6JZjGbTRnqfiD" }, { "alphabet": "base58", "hex": "003c176e659bea0f29a3e9bf7880c112b1b31b4dc826268187", "string": "16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS" }, { "alphabet": "base58", "hex": "ffffffffffffffffffff", "string": "FPBt6CHo3fovdL" }, { "alphabet": "base58", "hex": "ffffffffffffffffffffffffff", "string": "NKioeUVktgzXLJ1B3t" }, { "alphabet": "base58", "hex": "ffffffffffffffffffffffffffffffff", "string": "YcVfxkQb6JRzqk5kF2tNLv" }, { "alphabet": "base2", "hex": "fb6f9ac3", "string": "11111011011011111001101011000011" }, { "alphabet": "base2", "hex": "179eea7a", "string": "10111100111101110101001111010" }, { "alphabet": "base2", "hex": "6db825db", "string": "1101101101110000010010111011011" }, { "alphabet": "base2", "hex": "93976aa7", "string": "10010011100101110110101010100111" }, { "alphabet": "base58", "hex": "ef41b9ce7e830af7", "string": "h26E62FyLQN" }, { "alphabet": "base58", "hex": "606cbc791036d2e9", "string": "H8Sa62HVULG" }, { "alphabet": "base58", "hex": "bdcb0ea69c2c8ec8", "string": "YkESUPpnfoD" }, { "alphabet": "base58", "hex": "1a2358ba67fb71d5", "string": "5NaBN89ajtQ" }, { "alphabet": "base58", "hex": "e6173f0f4d5fb5d7", "string": "fVAoezT1ZkS" }, { "alphabet": "base58", "hex": "91c81cbfdd58bbd2", "string": "RPGNSU3bqTX" }, { "alphabet": "base58", "hex": "329e0bf0e388dbfe", "string": "9U41ZkwwysT" }, { "alphabet": "base58", "hex": "30b10393210fa65b", "string": "99NMW3WHjjY" }, { "alphabet": "base58", "hex": "ab3bdd18e3623654", "string": "VeBbqBb4rCT" }, { "alphabet": "base58", "hex": "fe29d1751ec4af8a", "string": "jWhmYLN9dUm" }, { "alphabet": "base58", "hex": "c1273ab5488769807d", "string": "3Tbh4kL3WKW6g" }, { "alphabet": "base58", "hex": "6c7907904de934f852", "string": "2P5jNYhfpTJxy" }, { "alphabet": "base58", "hex": "05f0be055db47a0dc9", "string": "5PN768Kr5oEp" }, { "alphabet": "base58", "hex": "3511e6206829b35b12", "string": "gBREojGaJ6DF" }, { "alphabet": "base58", "hex": "d1c7c2ddc4a459d503", "string": "3fsekq5Esq2KC" }, { "alphabet": "base58", "hex": "1f88efd17ab073e9a1", "string": "QHJbmW9ZY7jn" }, { "alphabet": "base58", "hex": "0f45dadf4e64c5d5c2", "string": "CGyVUMmCKLRf" }, { "alphabet": "base58", "hex": "de1e5c5f718bb7fafa", "string": "3pyy8U7w3KUa5" }, { "alphabet": "base58", "hex": "123190b93e9a49a46c", "string": "ES3DeFrG1zbd" }, { "alphabet": "base58", "hex": "8bee94a543e7242e5a", "string": "2nJnuWyLpGf6y" }, { "alphabet": "base58", "hex": "9fd5f2285362f5cfd834", "string": "9yqFhqeewcW3pF" }, { "alphabet": "base58", "hex": "6987bac63ad23828bb31", "string": "6vskE5Y1LhS3U4" }, { "alphabet": "base58", "hex": "19d4a0f9d459cc2a08b0", "string": "2TAsHPuaLhh5Aw" }, { "alphabet": "base58", "hex": "a1e47ffdbea5a807ab26", "string": "A6XzPgSUJDf1W5" }, { "alphabet": "base58", "hex": "35c231e5b3a86a9b83db", "string": "42B8reRwPAAoAa" }, { "alphabet": "base58", "hex": "b2351012a48b8347c351", "string": "B1hPyomGx4Vhqa" }, { "alphabet": "base58", "hex": "71d402694dd9517ea653", "string": "7Pv2SyAQx2Upu8" }, { "alphabet": "base58", "hex": "55227c0ec7955c2bd6e8", "string": "5nR64BkskyjHMq" }, { "alphabet": "base58", "hex": "17b3d8ee7907c1be34df", "string": "2LEg7TxosoxTGS" }, { "alphabet": "base58", "hex": "7e7bba7b68bb8e95827f", "string": "879o2ATGnmYyAW" }, { "alphabet": "base58", "hex": "db9c13f5ba7654b01407fb", "string": "wTYfxjDVbiks874" }, { "alphabet": "base58", "hex": "6186449d20f5fd1e6c4393", "string": "RBeiWhzZNL6VtMG" }, { "alphabet": "base58", "hex": "5248751cebf4ad1c1a83c3", "string": "MQSVNnc8ehFCqtW" }, { "alphabet": "base58", "hex": "32090ef18cd479fc376a74", "string": "DQdu351ExDaeYeX" }, { "alphabet": "base58", "hex": "7cfa5d6ed1e467d986c426", "string": "XzW67T5qfEnFcaZ" }, { "alphabet": "base58", "hex": "9d8707723c7ede51103b6d", "string": "g4eTCg6QJnB1UU4" }, { "alphabet": "base58", "hex": "6f4d1e392d6a9b4ed8b223", "string": "Ubo7kZY5aDpAJp2" }, { "alphabet": "base58", "hex": "38057d98797cd39f80a0c9", "string": "EtjQ2feamJvuqse" }, { "alphabet": "base58", "hex": "de7e59903177e20880e915", "string": "xB2N7yRBnDYEoT2" }, { "alphabet": "base58", "hex": "b2ea24a28bc4a60b5c4b8d", "string": "mNFMpJ2P3TGYqhv" }, { "alphabet": "base58", "hex": "cf84938958589b6ffba6114d", "string": "4v8ZbsGh2ePz5sipt" }, { "alphabet": "base58", "hex": "dee13be7b8d8a08c94a3c02a", "string": "5CwmE9jQqwtHkTF45" }, { "alphabet": "base58", "hex": "14cb9c6b3f8cd2e02710f569", "string": "Pm85JHVAAdeUdxtp" }, { "alphabet": "base58", "hex": "ca3f2d558266bdcc44c79cb5", "string": "4pMwomBAQHuUnoLUC" }, { "alphabet": "base58", "hex": "c031215be44cbad745f38982", "string": "4dMeTrcxiVw9RWvj3" }, { "alphabet": "base58", "hex": "1435ab1dbc403111946270a5", "string": "P7wX3sCWNrbqhBEC" }, { "alphabet": "base58", "hex": "d8c6e4d775e7a66a0d0f9f41", "string": "56GLoRDGWGuGJJwPN" }, { "alphabet": "base58", "hex": "dcee35e74f0fd74176fce2f4", "string": "5Ap1zyuYiJJFwWcMR" }, { "alphabet": "base58", "hex": "bfcc0ca4b4855d1cf8993fc0", "string": "4cvafQW4PEhARKv9D" }, { "alphabet": "base58", "hex": "e02a3ac25ece7b54584b670a", "string": "5EMM28xkpxZ1kkVUM" }, { "alphabet": "base58", "hex": "fe4d938fc3719f064cabb4bfff", "string": "NBXKkbHwrAsiWTLAk6" }, { "alphabet": "base58", "hex": "9289cb4f6b15c57e6086b87ea5", "string": "DCvDpjEXEbHjZqskKv" }, { "alphabet": "base58", "hex": "fc266f35626b3612bfe978537b", "string": "N186PVoBWrNre35BGE" }, { "alphabet": "base58", "hex": "33ff08c06d92502bf258c07166", "string": "5LC4SoW6jmTtbkbePw" }, { "alphabet": "base58", "hex": "6a81cac1f3666bc59dc67b1c3c", "string": "9sXgUySUzwiqDU5WHy" }, { "alphabet": "base58", "hex": "9dfb8e7e744c544c0f323ea729", "string": "EACsmGmkgcwsrPFzLg" }, { "alphabet": "base58", "hex": "1e7a1e284f70838b38442b682b", "string": "3YEVk9bE7rw5qExMkv" }, { "alphabet": "base58", "hex": "2a862ad57901a8235f5dc74eaf", "string": "4YS259nuTLfeXa5Wuc" }, { "alphabet": "base58", "hex": "74c82096baef21f9d3089e5462", "string": "AjAcKEhUfrqm8smvM7" }, { "alphabet": "base58", "hex": "7a3edbc23d7b600263920261cc", "string": "BBZXyRgey5S5DDZkcK" }, { "alphabet": "base58", "hex": "20435664c357d25a9c8df751cf4f", "string": "CrwNL6Fbv4pbRx1zd9g" }, { "alphabet": "base58", "hex": "51a7aa87cf5cb1c12d045ec3422d", "string": "X27NHGgKXmGzzQvDtpC" }, { "alphabet": "base58", "hex": "344d2e116aa26f1062a2cb6ebbef", "string": "LEDLDvL1Hg4qt1efVXt" }, { "alphabet": "base58", "hex": "6941add7be4c0b5c7163e4928f8e", "string": "fhMyN6gwoxE3uYraVzV" }, { "alphabet": "base58", "hex": "10938fcbb7c4ab991649734a14bf", "string": "76TPrSDxzGQfSzMu974" }, { "alphabet": "base58", "hex": "eafe04d944ba504e9af9117b07de", "string": "2VPgov563ryfe4L2Bj6M" }, { "alphabet": "base58", "hex": "58d0aeed4d35da20b6f052127edf", "string": "ZenZhXF9YwP8nQvNtNz" }, { "alphabet": "base58", "hex": "d734984e2f5aecf25f7a3e353f8a", "string": "2N7n3jFsTdyN49Faoq6h" }, { "alphabet": "base58", "hex": "57d873fdb405b7daf4bafa62068a", "string": "ZJ7NwoP4wHvwyZg3Wjs" }, { "alphabet": "base58", "hex": "bda4ec7b40d0d65ca95dec4c4d3b", "string": "2CijxjsNyvqTwPCfDcpA" }, { "alphabet": "base58", "hex": "826c4abdceb1b91f0d4ad665f86d2e", "string": "4edfvuDQu9KzVxLuXHfMo" }, { "alphabet": "base58", "hex": "e7ecb35d07e65b960cb10574a4f51a", "string": "7VLRYdB4cToipp2J2p3v9" }, { "alphabet": "base58", "hex": "4f2d72ead87b31d6869fba39eac6dc", "string": "3DUjqJRcfdWhpsrLrGcQs" }, { "alphabet": "base58", "hex": "8b4f5788d60030950d5dfbf94c585d", "string": "4u44JSRH5jP5X39YhPsmE" }, { "alphabet": "base58", "hex": "ee4c0a0025d1a74ace9fe349355cc5", "string": "7fgACjABRQUGUEpN6VBBA" }, { "alphabet": "base58", "hex": "58ac05b9a0b4b66083ff1d489b8d84", "string": "3UtJPyTwGXapcxHx8Rom5" }, { "alphabet": "base58", "hex": "1aa35c05e1132e8e049aafaef035d8", "string": "kE2eSU7gM2619pT82iGP" }, { "alphabet": "base58", "hex": "771b0c28608484562a292e5d5d2b30", "string": "4LGYeWhyfrjUByibUqdVR" }, { "alphabet": "base58", "hex": "78ff9a0e56f9e88dc1cd654b40d019", "string": "4PLggs66qAdbmZgkaPihe" }, { "alphabet": "base58", "hex": "6d691bdd736346aa5a0a95b373b2ab", "string": "44Y6qTgSvRMkdqpQ5ufkN" } ], "invalid": [ { "alphabet": "base58", "description": "non-base58 string", "exception": "^TypeError: Expected String$", "string": {} }, { "alphabet": "base58", "description": "non-base58 string", "exception": "^Error: Non-base58 character$", "string": "#####" }, { "alphabet": "base58", "description": "non-base58 string", "exception": "^Error: Non-base58 character$", "string": "invalid" }, { "alphabet": "base58", "description": "non-base58 alphabet", "exception": "^Error: Non-base58 character$", "string": "c2F0b3NoaQo=" }, { "alphabet": "base58", "description": "leading whitespace", "exception": "^Error: Non-base58 character$", "string": " 1111111111" }, { "alphabet": "base58", "description": "trailing whitespace", "exception": "^Error: Non-base58 character$", "string": "1111111111 " }, { "alphabet": "base58", "description": "unexpected character after whitespace", "exception": "^Error: Non-base58 character$", "string": " \t\n\u000b\f\r skip \r\f\u000b\n\t a" }, { "alphabet": "0123456789fabcdef", "description": "poorly formed alphabet", "exception": "^TypeError: f is ambiguous$" } ] } base-x-3.0.8/test/index.js000066400000000000000000000032171362512603400153320ustar00rootroot00000000000000var Buffer = require('safe-buffer').Buffer var basex = require('../') var tape = require('tape') var fixtures = require('./fixtures.json') var bases = Object.keys(fixtures.alphabets).reduce(function (bases, alphabetName) { bases[alphabetName] = basex(fixtures.alphabets[alphabetName]) return bases }, {}) fixtures.valid.forEach(function (f) { tape.test('can encode ' + f.alphabet + ': ' + f.hex, function (t) { var base = bases[f.alphabet] var actual = base.encode(Buffer.from(f.hex, 'hex')) t.plan(1) t.same(actual, f.string) }) }) fixtures.valid.forEach(function (f) { tape.test('can decode ' + f.alphabet + ': ' + f.string, function (t) { var base = bases[f.alphabet] var actual = base.decode(f.string).toString('hex') t.plan(1) t.same(actual, f.hex) }) }) fixtures.invalid.forEach(function (f) { tape.test('decode throws on ' + f.description, function (t) { var base = bases[f.alphabet] t.plan(1) t.throws(function () { if (!base) base = basex(f.alphabet) base.decode(f.string) }, new RegExp(f.exception)) }) }) tape.test('decode should return Buffer', function (t) { t.plan(2) t.true(Buffer.isBuffer(bases.base2.decode(''))) t.true(Buffer.isBuffer(bases.base2.decode('01'))) }) tape.test('encode throws on string', function (t) { var base = bases.base58 t.plan(1) t.throws(function () { base.encode('a') }, new RegExp('^TypeError: Expected Buffer$')) }) tape.test('encode not throw on Array or Uint8Array', function (t) { var base = bases.base58 t.plan(2) t.same(base.encode([42, 12, 34]), 'F89f') t.same(base.encode(new Uint8Array([42, 12, 34])), 'F89f') }) base-x-3.0.8/ts_src/000077500000000000000000000000001362512603400142005ustar00rootroot00000000000000base-x-3.0.8/ts_src/index.ts000066400000000000000000000105651362512603400156660ustar00rootroot00000000000000// 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) for (let j = 0; j < BASE_MAP.length; j++) { BASE_MAP[j] = 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 | number[] | Uint8Array): string { if (Array.isArray(source) || source instanceof Uint8Array) source = _Buffer.from(source) 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 | number[] | Uint8Array): string; decodeUnsafe(string: string): Buffer | undefined; decode(string: string): Buffer; } } base-x-3.0.8/tsconfig.json000066400000000000000000000012111362512603400154050ustar00rootroot00000000000000{ "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/**/*" ] }