@vandeurenglenn/base-x 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
package/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # base-x
2
+ > module compatible base-x
3
+ ## usage
4
+
5
+ ```js
6
+ const base = require('base-x')
7
+ // or
8
+ import base from 'base-x'
9
+
10
+ const base32 = base('abcdefghijklmnopqrstuvwxyz234567') // base32 Alphabet
11
+
12
+ // encode
13
+ const uint8Array = new TextEncoder().encode('hi');
14
+ const bs32 = base32.encode(uint8Array)
15
+ console.log(bs32); // '2dj'
16
+
17
+ // decode
18
+ const hi = base32.decode(bs32)
19
+ const string = new TextDecoder().decode(hi);
20
+ console.log(string) // 'hi'
21
+
22
+ ```
package/dist/base-x.js ADDED
@@ -0,0 +1,123 @@
1
+ 'use strict';
2
+
3
+ // base-x encoding / decoding
4
+ // Copyright (c) 2018 base-x contributors
5
+ // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
6
+ // Distributed under the MIT software license, see the accompanying
7
+ // file LICENSE or http://www.opensource.org/licenses/mit-license.php.
8
+ const base = ALPHABET => {
9
+ if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }
10
+ const BASE_MAP = new Uint8Array(256);
11
+ for (let j = 0; j < BASE_MAP.length; j++) {
12
+ BASE_MAP[j] = 255;
13
+ }
14
+ for (let i = 0; i < ALPHABET.length; i++) {
15
+ const x = ALPHABET.charAt(i);
16
+ const xc = x.charCodeAt(0);
17
+ if (BASE_MAP[xc] !== 255) { throw new TypeError(x + ' is ambiguous') }
18
+ BASE_MAP[xc] = i;
19
+ }
20
+ const BASE = ALPHABET.length;
21
+ const LEADER = ALPHABET.charAt(0);
22
+ const FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up
23
+ const iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up
24
+
25
+ const encode = source => {
26
+ if (source instanceof Uint8Array) ; else if (ArrayBuffer.isView(source)) {
27
+ source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
28
+ } else if (Array.isArray(source)) {
29
+ source = Uint8Array.from(source);
30
+ }
31
+ if (!(source instanceof Uint8Array)) { throw new TypeError('Expected Uint8Array') }
32
+ if (source.length === 0) { return '' }
33
+ // Skip & count leading zeroes.
34
+ let zeroes = 0;
35
+ let length = 0;
36
+ let pbegin = 0;
37
+ const pend = source.length;
38
+ while (pbegin !== pend && source[pbegin] === 0) {
39
+ pbegin++;
40
+ zeroes++;
41
+ }
42
+ // Allocate enough space in big-endian base58 representation.
43
+ const size = ((pend - pbegin) * iFACTOR + 1) >>> 0;
44
+ const b58 = new Uint8Array(size);
45
+ // Process the bytes.
46
+ while (pbegin !== pend) {
47
+ let carry = source[pbegin];
48
+ // Apply "b58 = b58 * 256 + ch".
49
+ let i = 0;
50
+ for (let it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
51
+ carry += (256 * b58[it1]) >>> 0;
52
+ b58[it1] = (carry % BASE) >>> 0;
53
+ carry = (carry / BASE) >>> 0;
54
+ }
55
+ if (carry !== 0) { throw new Error('Non-zero carry') }
56
+ length = i;
57
+ pbegin++;
58
+ }
59
+ // Skip leading zeroes in base58 result.
60
+ let it2 = size - length;
61
+ while (it2 !== size && b58[it2] === 0) {
62
+ it2++;
63
+ }
64
+ // Translate the result into a string.
65
+ let str = LEADER.repeat(zeroes);
66
+ for (; it2 < size; ++it2) { str += ALPHABET.charAt(b58[it2]); }
67
+ return str
68
+ };
69
+ const decodeUnsafe = source => {
70
+ if (typeof source !== 'string') { throw new TypeError('Expected String') }
71
+ if (source.length === 0) { return new Uint8Array() }
72
+ let psz = 0;
73
+ // Skip and count leading '1's.
74
+ let zeroes = 0;
75
+ let length = 0;
76
+ while (source[psz] === LEADER) {
77
+ zeroes++;
78
+ psz++;
79
+ }
80
+ // Allocate enough space in big-endian base256 representation.
81
+ const size = (((source.length - psz) * FACTOR) + 1) >>> 0; // log(58) / log(256), rounded up.
82
+ let b256 = new Uint8Array(size);
83
+ // Process the characters.
84
+ while (source[psz]) {
85
+ // Decode character
86
+ let carry = BASE_MAP[source.charCodeAt(psz)];
87
+ // Invalid character
88
+ if (carry === 255) { return }
89
+ let i = 0;
90
+ for (let it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
91
+ carry += (BASE * b256[it3]) >>> 0;
92
+ b256[it3] = (carry % 256) >>> 0;
93
+ carry = (carry / 256) >>> 0;
94
+ }
95
+ if (carry !== 0) { throw new Error('Non-zero carry') }
96
+ length = i;
97
+ psz++;
98
+ }
99
+ // Skip leading zeroes in b256.
100
+ let it4 = size - length;
101
+ while (it4 !== size && b256[it4] === 0) {
102
+ it4++;
103
+ }
104
+ let vch = new Uint8Array(zeroes + (size - it4));
105
+ let j = zeroes;
106
+ while (it4 !== size) {
107
+ vch[j++] = b256[it4++];
108
+ }
109
+ return vch
110
+ };
111
+ const decode = string => {
112
+ const buffer = decodeUnsafe(string);
113
+ if (buffer) { return buffer }
114
+ throw new Error('Non-base' + BASE + ' character')
115
+ };
116
+ return {
117
+ encode: encode,
118
+ decodeUnsafe: decodeUnsafe,
119
+ decode: decode
120
+ }
121
+ };
122
+
123
+ module.exports = base;
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@vandeurenglenn/base-x",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "dist/base-x.js",
6
+ "module": "src/base-x.js",
7
+ "scripts": {
8
+ "c": "rollup -c",
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "keywords": [
12
+ "base",
13
+ "x",
14
+ "base-x",
15
+ "es"
16
+ ],
17
+ "author": "",
18
+ "license": "MIT",
19
+ "devDependencies": {
20
+ "rollup": "^2.70.2"
21
+ }
22
+ }
@@ -0,0 +1,7 @@
1
+ export default [{
2
+ input: ['./src/base-x.js'],
3
+ output: [{
4
+ dir: './dist',
5
+ format: 'cjs'
6
+ }]
7
+ }]
package/src/base-x.js ADDED
@@ -0,0 +1,122 @@
1
+ // base-x encoding / decoding
2
+ // Copyright (c) 2018 base-x contributors
3
+ // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
4
+ // Distributed under the MIT software license, see the accompanying
5
+ // file LICENSE or http://www.opensource.org/licenses/mit-license.php.
6
+ const base = ALPHABET => {
7
+ if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }
8
+ const BASE_MAP = new Uint8Array(256)
9
+ for (let j = 0; j < BASE_MAP.length; j++) {
10
+ BASE_MAP[j] = 255
11
+ }
12
+ for (let i = 0; i < ALPHABET.length; i++) {
13
+ const x = ALPHABET.charAt(i)
14
+ const xc = x.charCodeAt(0)
15
+ if (BASE_MAP[xc] !== 255) { throw new TypeError(x + ' is ambiguous') }
16
+ BASE_MAP[xc] = i
17
+ }
18
+ const BASE = ALPHABET.length
19
+ const LEADER = ALPHABET.charAt(0)
20
+ const FACTOR = Math.log(BASE) / Math.log(256) // log(BASE) / log(256), rounded up
21
+ const iFACTOR = Math.log(256) / Math.log(BASE) // log(256) / log(BASE), rounded up
22
+
23
+ const encode = source => {
24
+ if (source instanceof Uint8Array) {
25
+ } else if (ArrayBuffer.isView(source)) {
26
+ source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength)
27
+ } else if (Array.isArray(source)) {
28
+ source = Uint8Array.from(source)
29
+ }
30
+ if (!(source instanceof Uint8Array)) { throw new TypeError('Expected Uint8Array') }
31
+ if (source.length === 0) { return '' }
32
+ // Skip & count leading zeroes.
33
+ let zeroes = 0
34
+ let length = 0
35
+ let pbegin = 0
36
+ const pend = source.length
37
+ while (pbegin !== pend && source[pbegin] === 0) {
38
+ pbegin++
39
+ zeroes++
40
+ }
41
+ // Allocate enough space in big-endian base58 representation.
42
+ const size = ((pend - pbegin) * iFACTOR + 1) >>> 0
43
+ const b58 = new Uint8Array(size)
44
+ // Process the bytes.
45
+ while (pbegin !== pend) {
46
+ let carry = source[pbegin]
47
+ // Apply "b58 = b58 * 256 + ch".
48
+ let i = 0
49
+ for (let it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
50
+ carry += (256 * b58[it1]) >>> 0
51
+ b58[it1] = (carry % BASE) >>> 0
52
+ carry = (carry / BASE) >>> 0
53
+ }
54
+ if (carry !== 0) { throw new Error('Non-zero carry') }
55
+ length = i
56
+ pbegin++
57
+ }
58
+ // Skip leading zeroes in base58 result.
59
+ let it2 = size - length
60
+ while (it2 !== size && b58[it2] === 0) {
61
+ it2++
62
+ }
63
+ // Translate the result into a string.
64
+ let str = LEADER.repeat(zeroes)
65
+ for (; it2 < size; ++it2) { str += ALPHABET.charAt(b58[it2]) }
66
+ return str
67
+ }
68
+ const decodeUnsafe = source => {
69
+ if (typeof source !== 'string') { throw new TypeError('Expected String') }
70
+ if (source.length === 0) { return new Uint8Array() }
71
+ let psz = 0
72
+ // Skip and count leading '1's.
73
+ let zeroes = 0
74
+ let length = 0
75
+ while (source[psz] === LEADER) {
76
+ zeroes++
77
+ psz++
78
+ }
79
+ // Allocate enough space in big-endian base256 representation.
80
+ const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.
81
+ let b256 = new Uint8Array(size)
82
+ // Process the characters.
83
+ while (source[psz]) {
84
+ // Decode character
85
+ let carry = BASE_MAP[source.charCodeAt(psz)]
86
+ // Invalid character
87
+ if (carry === 255) { return }
88
+ let i = 0
89
+ for (let it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
90
+ carry += (BASE * b256[it3]) >>> 0
91
+ b256[it3] = (carry % 256) >>> 0
92
+ carry = (carry / 256) >>> 0
93
+ }
94
+ if (carry !== 0) { throw new Error('Non-zero carry') }
95
+ length = i
96
+ psz++
97
+ }
98
+ // Skip leading zeroes in b256.
99
+ let it4 = size - length
100
+ while (it4 !== size && b256[it4] === 0) {
101
+ it4++
102
+ }
103
+ let vch = new Uint8Array(zeroes + (size - it4))
104
+ let j = zeroes
105
+ while (it4 !== size) {
106
+ vch[j++] = b256[it4++]
107
+ }
108
+ return vch
109
+ }
110
+ const decode = string => {
111
+ const buffer = decodeUnsafe(string)
112
+ if (buffer) { return buffer }
113
+ throw new Error('Non-base' + BASE + ' character')
114
+ }
115
+ return {
116
+ encode: encode,
117
+ decodeUnsafe: decodeUnsafe,
118
+ decode: decode
119
+ }
120
+ }
121
+
122
+ export default base
package/test.js ADDED
@@ -0,0 +1,18 @@
1
+ const base = require('./dist/base-x')
2
+ const base32 = base('abcdefghijklmnopqrstuvwxyz234567')
3
+
4
+ // const uint8Array = new Uint8Array(2)
5
+ // uint8Array[0] = Buffer.from('h').toString('hex')
6
+ // uint8Array[1] = Buffer.from('i').toString('hex')
7
+ const uint8Array = new TextEncoder().encode('hi');
8
+ const bs32 = base32.encode(uint8Array)
9
+ console.log(bs32);
10
+ const hi = base32.decode(bs32)
11
+ const string = new TextDecoder().decode(hi);
12
+ // let str = ''
13
+ // hi.forEach((item, i) => {
14
+ // str+= Buffer.from(item.toString(), 'hex').toString()
15
+ // });
16
+ // console.log(hi);
17
+
18
+ console.log(string === 'hi');