Skip to content

Commit

Permalink
refactor(api): move base to separate file with no eslint validation
Browse files Browse the repository at this point in the history
  • Loading branch information
djabarovgeorge committed Oct 22, 2024
1 parent 8a52d8a commit 17a9ce9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// 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.
// "version": "5.0.0",
// cspell:disable
/* eslint-disable */
/* cspell:disable */

/*
* 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.
* "version": "5.0.0",
*/

function base(ALPHABET) {
if (ALPHABET.length >= 255) {
Expand All @@ -18,7 +22,7 @@ function base(ALPHABET) {
const x = ALPHABET.charAt(i);
const xc = x.charCodeAt(0);
if (BASE_MAP[xc] !== 255) {
throw new TypeError(x + ' is ambiguous');
throw new TypeError(`${x} is ambiguous`);
}
BASE_MAP[xc] = i;
}
Expand All @@ -27,7 +31,6 @@ function base(ALPHABET) {
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) {
// eslint-disable-next-line no-empty
if (source instanceof Uint8Array) {
} else if (ArrayBuffer.isView(source)) {
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
Expand Down Expand Up @@ -78,6 +81,7 @@ function base(ALPHABET) {
for (; it2 < size; ++it2) {
str += ALPHABET.charAt(b58[it2]);
}

return str;
}
function decodeUnsafe(source) {
Expand Down Expand Up @@ -128,35 +132,19 @@ function base(ALPHABET) {
while (it4 !== size) {
vch[j++] = b256[it4++];
}

return vch;
}
function decode(string) {
const buffer = decodeUnsafe(string);
if (buffer) {
return buffer;
}
throw new Error('Non-base' + BASE + ' character');
throw new Error(`Non-base${BASE} character`);
}

return {
encode,
decode,
};
}

/**
* Base62 alphabet
* Modifying this alphabet is prohibited as it would invalidate existing encoded data
*/
const BASE62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const { encode, decode } = base(BASE62);
const ENCODING = 'hex' satisfies BufferEncoding;

export function encodeBase62(value: string): string {
const buffer = Buffer.from(value, ENCODING);
return encode(buffer);
}

export function decodeBase62(encoded: string): string {
const uint8Array = decode(encoded);
return Buffer.from(uint8Array).toString(ENCODING);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Base62 alphabet
* Modifying this alphabet is prohibited as it would invalidate existing encoded data
*/
const BASE62_ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
14 changes: 14 additions & 0 deletions apps/api/src/app/shared/helpers/base62/base62.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { encode, decode } = base(BASE62_ALPHABET);
const ENCODING = 'hex' satisfies BufferEncoding;

export function encodeBase62(value: string): string {
const buffer = Buffer.from(value, ENCODING);

return encode(buffer);
}

export function decodeBase62(encoded: string): string {
const uint8Array = decode(encoded);

return Buffer.from(uint8Array).toString(ENCODING);
}
1 change: 1 addition & 0 deletions apps/api/src/app/shared/helpers/base62/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './base62';

0 comments on commit 17a9ce9

Please sign in to comment.