Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace bs58 lib #155

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@toruslabs/http-helpers": "^7.0.0",
"bn.js": "^5.2.1",
"elliptic": "^6.5.6",
"bs58": "^6.0.0",
"ethereum-cryptography": "^2.2.1",
"json-stable-stringify": "^1.1.1",
"loglevel": "^1.9.1"
Expand Down
143 changes: 143 additions & 0 deletions src/helpers/bs58.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
const base = (ALPHABET: string) => {
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: Uint8Array | number[]) {
// eslint-disable-next-line no-empty
if (source instanceof Uint8Array) {
} else if (ArrayBuffer.isView(source)) {
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
} else if (Array.isArray(source)) {
source = Uint8Array.from(source);
}
if (!(source instanceof Uint8Array)) {
throw new TypeError("Expected Uint8Array");
}
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) {
if (typeof source !== "string") {
throw new TypeError("Expected String");
}
if (source.length === 0) {
return new Uint8Array();
}
let psz = 0;
// 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 leading zeroes in b256.
let it4 = size - length;
while (it4 !== size && b256[it4] === 0) {
it4++;
}
const vch = new Uint8Array(zeroes + (size - it4));
let j = zeroes;
while (it4 !== size) {
vch[j++] = b256[it4++];
}
return vch;
}
function decode(s: string) {
const buffer = decodeUnsafe(s);
if (buffer) {
return buffer;
}
throw new Error(`Non-base${BASE} character`);
}
return {
encode,
decodeUnsafe,
decode,
};
};

const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

const bs58 = base(ALPHABET);

export { bs58 };
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./bs58";
export * from "./common";
export * from "./errorUtils";
export * from "./keyUtils";
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/keyUtils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { INodePub, KEY_TYPE } from "@toruslabs/constants";
import { Ecies, encrypt } from "@toruslabs/eccrypto";
import BN from "bn.js";
import base58 from "bs58";
import { curve, ec as EC } from "elliptic";
import { keccak256 as keccakHash } from "ethereum-cryptography/keccak";
import { sha512 } from "ethereum-cryptography/sha512";
import stringify from "json-stable-stringify";
import log from "loglevel";

import { EncryptedSeed, ImportedShare, KeyType, PrivateKeyData } from "../interfaces";
import { bs58 } from "./bs58";
import { encParamsBufToHex, generatePrivateKey, getKeyCurve, keccak256 } from "./common";
import { generateRandomPolynomial } from "./langrangeInterpolatePoly";
import { generateNonceMetadataParams, getSecpKeyFromEd25519 } from "./metadataUtils";
Expand Down Expand Up @@ -142,7 +142,7 @@ function generateAddressFromEcKey(keyType: KeyType, key: EC.KeyPair): string {
return toChecksumAddress(evmAddressLower);
} else if (keyType === KEY_TYPE.ED25519) {
const publicKey = encodeEd25519Point(key.getPublic());
const address = base58.encode(publicKey);
const address = bs58.encode(publicKey);
return address;
}
throw new Error(`Invalid keyType: ${keyType}`);
Expand Down
3 changes: 1 addition & 2 deletions test/sapphire_devnet_ed25519.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { TORUS_SAPPHIRE_NETWORK } from "@toruslabs/constants";
import { NodeDetailManager } from "@toruslabs/fetch-node-details";
import BN from "bn.js";
import base58 from "bs58";
import { expect } from "chai";
import faker from "faker";

import { keccak256 } from "../src";
import { bs58 as base58, keccak256 } from "../src";
import TorusUtils from "../src/torus";
import { generateIdToken, getImportKeyParams, getRetrieveSharesParams, lookupVerifier } from "./helpers";

Expand Down