@vandeurenglenn/base-x 1.1.0 → 1.1.2
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/index.d.ts +11 -0
- package/{dist/base-x.js → index.js} +39 -39
- package/package.json +5 -2
- package/src/{base-x.ts → index.ts} +14 -3
- package/test.js +4 -4
- package/tsconfig.json +13 -0
- package/src/baseX.ts +0 -5
package/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* string containging only given ALPHABET letters/numbers
|
|
3
|
+
*/
|
|
4
|
+
type baseString = string;
|
|
5
|
+
type baseX = {
|
|
6
|
+
encode: (uint8Array: Uint8Array) => baseString;
|
|
7
|
+
decode: (string: baseString) => Uint8Array;
|
|
8
|
+
decodeUnsafe: (string: baseString) => Uint8Array;
|
|
9
|
+
};
|
|
10
|
+
declare const base: (ALPHABET: string) => baseX;
|
|
11
|
+
export { base as default };
|
|
@@ -3,27 +3,27 @@
|
|
|
3
3
|
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
|
|
4
4
|
// Distributed under the MIT software license, see the accompanying
|
|
5
5
|
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
|
6
|
-
|
|
6
|
+
const base = (ALPHABET) => {
|
|
7
7
|
if (ALPHABET.length >= 255) {
|
|
8
8
|
throw new TypeError('Alphabet too long');
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
for (
|
|
10
|
+
const BASE_MAP = new Uint8Array(256);
|
|
11
|
+
for (let j = 0; j < BASE_MAP.length; j++) {
|
|
12
12
|
BASE_MAP[j] = 255;
|
|
13
13
|
}
|
|
14
|
-
for (
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
for (let i = 0; i < ALPHABET.length; i++) {
|
|
15
|
+
const x = ALPHABET.charAt(i);
|
|
16
|
+
const xc = x.charCodeAt(0);
|
|
17
17
|
if (BASE_MAP[xc] !== 255) {
|
|
18
18
|
throw new TypeError(x + ' is ambiguous');
|
|
19
19
|
}
|
|
20
20
|
BASE_MAP[xc] = i;
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
const BASE = ALPHABET.length;
|
|
23
|
+
const LEADER = ALPHABET.charAt(0);
|
|
24
|
+
const FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up
|
|
25
|
+
const iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up
|
|
26
|
+
const encode = (source) => {
|
|
27
27
|
if (source instanceof Uint8Array) ;
|
|
28
28
|
else if (ArrayBuffer.isView(source)) {
|
|
29
29
|
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
@@ -38,23 +38,23 @@ var base = function (ALPHABET) {
|
|
|
38
38
|
return '';
|
|
39
39
|
}
|
|
40
40
|
// Skip & count leading zeroes.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
let zeroes = 0;
|
|
42
|
+
let length = 0;
|
|
43
|
+
let pbegin = 0;
|
|
44
|
+
const pend = source.length;
|
|
45
45
|
while (pbegin !== pend && source[pbegin] === 0) {
|
|
46
46
|
pbegin++;
|
|
47
47
|
zeroes++;
|
|
48
48
|
}
|
|
49
49
|
// Allocate enough space in big-endian base58 representation.
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
const size = ((pend - pbegin) * iFACTOR + 1) >>> 0;
|
|
51
|
+
const b58 = new Uint8Array(size);
|
|
52
52
|
// Process the bytes.
|
|
53
53
|
while (pbegin !== pend) {
|
|
54
|
-
|
|
54
|
+
let carry = source[pbegin];
|
|
55
55
|
// Apply "b58 = b58 * 256 + ch".
|
|
56
|
-
|
|
57
|
-
for (
|
|
56
|
+
let i = 0;
|
|
57
|
+
for (let it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
|
|
58
58
|
carry += (256 * b58[it1]) >>> 0;
|
|
59
59
|
b58[it1] = (carry % BASE) >>> 0;
|
|
60
60
|
carry = (carry / BASE) >>> 0;
|
|
@@ -66,45 +66,45 @@ var base = function (ALPHABET) {
|
|
|
66
66
|
pbegin++;
|
|
67
67
|
}
|
|
68
68
|
// Skip leading zeroes in base58 result.
|
|
69
|
-
|
|
69
|
+
let it2 = size - length;
|
|
70
70
|
while (it2 !== size && b58[it2] === 0) {
|
|
71
71
|
it2++;
|
|
72
72
|
}
|
|
73
73
|
// Translate the result into a string.
|
|
74
|
-
|
|
74
|
+
let str = LEADER.repeat(zeroes);
|
|
75
75
|
for (; it2 < size; ++it2) {
|
|
76
76
|
str += ALPHABET.charAt(b58[it2]);
|
|
77
77
|
}
|
|
78
78
|
return str;
|
|
79
79
|
};
|
|
80
|
-
|
|
80
|
+
const decodeUnsafe = (source) => {
|
|
81
81
|
if (typeof source !== 'string') {
|
|
82
82
|
throw new TypeError('Expected String');
|
|
83
83
|
}
|
|
84
84
|
if (source.length === 0) {
|
|
85
85
|
return new Uint8Array();
|
|
86
86
|
}
|
|
87
|
-
|
|
87
|
+
let psz = 0;
|
|
88
88
|
// Skip and count leading '1's.
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
let zeroes = 0;
|
|
90
|
+
let length = 0;
|
|
91
91
|
while (source[psz] === LEADER) {
|
|
92
92
|
zeroes++;
|
|
93
93
|
psz++;
|
|
94
94
|
}
|
|
95
95
|
// Allocate enough space in big-endian base256 representation.
|
|
96
|
-
|
|
97
|
-
|
|
96
|
+
const size = (((source.length - psz) * FACTOR) + 1) >>> 0; // log(58) / log(256), rounded up.
|
|
97
|
+
let b256 = new Uint8Array(size);
|
|
98
98
|
// Process the characters.
|
|
99
99
|
while (source[psz]) {
|
|
100
100
|
// Decode character
|
|
101
|
-
|
|
101
|
+
let carry = BASE_MAP[source.charCodeAt(psz)];
|
|
102
102
|
// Invalid character
|
|
103
103
|
if (carry === 255) {
|
|
104
104
|
return;
|
|
105
105
|
}
|
|
106
|
-
|
|
107
|
-
for (
|
|
106
|
+
let i = 0;
|
|
107
|
+
for (let it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
|
|
108
108
|
carry += (BASE * b256[it3]) >>> 0;
|
|
109
109
|
b256[it3] = (carry % 256) >>> 0;
|
|
110
110
|
carry = (carry / 256) >>> 0;
|
|
@@ -116,28 +116,28 @@ var base = function (ALPHABET) {
|
|
|
116
116
|
psz++;
|
|
117
117
|
}
|
|
118
118
|
// Skip leading zeroes in b256.
|
|
119
|
-
|
|
119
|
+
let it4 = size - length;
|
|
120
120
|
while (it4 !== size && b256[it4] === 0) {
|
|
121
121
|
it4++;
|
|
122
122
|
}
|
|
123
|
-
|
|
124
|
-
|
|
123
|
+
let vch = new Uint8Array(zeroes + (size - it4));
|
|
124
|
+
let j = zeroes;
|
|
125
125
|
while (it4 !== size) {
|
|
126
126
|
vch[j++] = b256[it4++];
|
|
127
127
|
}
|
|
128
128
|
return vch;
|
|
129
129
|
};
|
|
130
|
-
|
|
131
|
-
|
|
130
|
+
const decode = (string) => {
|
|
131
|
+
const buffer = decodeUnsafe(string);
|
|
132
132
|
if (buffer) {
|
|
133
133
|
return buffer;
|
|
134
134
|
}
|
|
135
135
|
throw new Error('Non-base' + BASE + ' character');
|
|
136
136
|
};
|
|
137
137
|
return {
|
|
138
|
-
encode
|
|
139
|
-
decodeUnsafe
|
|
140
|
-
decode
|
|
138
|
+
encode,
|
|
139
|
+
decodeUnsafe,
|
|
140
|
+
decode
|
|
141
141
|
};
|
|
142
142
|
};
|
|
143
143
|
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vandeurenglenn/base-x",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "",
|
|
5
|
-
"
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./index.js"
|
|
7
|
+
},
|
|
6
8
|
"type": "module",
|
|
9
|
+
"types": "./index.d.ts",
|
|
7
10
|
"scripts": {
|
|
8
11
|
"build": "rollup -c",
|
|
9
12
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
@@ -1,9 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* string containing only given ALPHABET letters/numbers
|
|
3
|
+
*/
|
|
4
|
+
type baseString = string
|
|
5
|
+
|
|
6
|
+
type baseX = {
|
|
7
|
+
encode: (uint8Array: Uint8Array) => baseString,
|
|
8
|
+
decode: (string: baseString) => Uint8Array,
|
|
9
|
+
decodeUnsafe: (string: baseString) => Uint8Array,
|
|
10
|
+
}
|
|
11
|
+
|
|
1
12
|
// base-x encoding / decoding
|
|
2
13
|
// Copyright (c) 2018 base-x contributors
|
|
3
14
|
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
|
|
4
15
|
// Distributed under the MIT software license, see the accompanying
|
|
5
16
|
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
|
6
|
-
const base = (ALPHABET: string):
|
|
17
|
+
const base = (ALPHABET: string):baseX => {
|
|
7
18
|
if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }
|
|
8
19
|
const BASE_MAP = new Uint8Array(256)
|
|
9
20
|
for (let j = 0; j < BASE_MAP.length; j++) {
|
|
@@ -66,7 +77,7 @@ const base = (ALPHABET: string): baseX => {
|
|
|
66
77
|
return str
|
|
67
78
|
}
|
|
68
79
|
|
|
69
|
-
const decodeUnsafe = (source: string):
|
|
80
|
+
const decodeUnsafe = (source: string):Uint8Array | undefined => {
|
|
70
81
|
if (typeof source !== 'string') { throw new TypeError('Expected String') }
|
|
71
82
|
if (source.length === 0) { return new Uint8Array() }
|
|
72
83
|
let psz = 0
|
|
@@ -109,7 +120,7 @@ const base = (ALPHABET: string): baseX => {
|
|
|
109
120
|
return vch
|
|
110
121
|
}
|
|
111
122
|
|
|
112
|
-
const decode = (string: string):
|
|
123
|
+
const decode = (string: string):Uint8Array => {
|
|
113
124
|
const buffer = decodeUnsafe(string)
|
|
114
125
|
if (buffer) { return buffer }
|
|
115
126
|
throw new Error('Non-base' + BASE + ' character')
|
package/test.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import base from './
|
|
2
|
-
const base32 = base('
|
|
1
|
+
import base from './index.js'
|
|
2
|
+
const base32 = base('a1')
|
|
3
3
|
|
|
4
4
|
// const uint8Array = new Uint8Array(2)
|
|
5
5
|
// uint8Array[0] = Buffer.from('h').toString('hex')
|
|
6
6
|
// uint8Array[1] = Buffer.from('i').toString('hex')
|
|
7
|
-
const uint8Array = new TextEncoder().encode('hi');
|
|
7
|
+
const uint8Array = new TextEncoder().encode('hi hello, hi');
|
|
8
8
|
const bs32 = base32.encode(uint8Array)
|
|
9
9
|
console.log(bs32);
|
|
10
10
|
const hi = base32.decode(bs32)
|
|
@@ -15,4 +15,4 @@ const string = new TextDecoder().decode(hi);
|
|
|
15
15
|
// });
|
|
16
16
|
// console.log(hi);
|
|
17
17
|
|
|
18
|
-
console.log(string === 'hi');
|
|
18
|
+
console.log(string === 'hi hello, hi');
|
package/tsconfig.json
ADDED