Skip to content

Commit

Permalink
fix: remove enc-utils to remove buffer dep
Browse files Browse the repository at this point in the history
buffer is not allowed in browsers
  • Loading branch information
janek26 committed Oct 29, 2021
1 parent 82aa438 commit e08e4b5
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 40 deletions.
3 changes: 1 addition & 2 deletions __tests__/ec.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { removeHexPrefix } from 'enc-utils';

import { getKeyPair, getStarkKey, hashCalldata, hashMessage, pedersen, sign } from '../src';
import { removeHexPrefix } from '../src/utils/enc';
import { toBN, toHex } from '../src/utils/number';

test('does work with package', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { IS_BROWSER } = constants;
const {
json: { parse, stringify },
starknet: { compressProgram, getSelectorFromName, makeAddress },
enc: { hexToDecimalString },
number: { hexToDecimalString },
} = utils;

const compiledArgentAccount = parse(
Expand Down
3 changes: 1 addition & 2 deletions __tests__/wallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import fs from 'fs';

import { addHexPrefix } from 'enc-utils';

import {
CompiledContract,
Contract,
Expand All @@ -13,6 +11,7 @@ import {
utils,
waitForTx,
} from '../src';
import { addHexPrefix } from '../src/utils/enc';
import { toBN } from '../src/utils/number';

const {
Expand Down
31 changes: 8 additions & 23 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 @@ -61,7 +61,6 @@
"axios": "^0.23.0",
"bn.js": "^5.2.0",
"elliptic": "^6.5.4",
"enc-utils": "^3.0.0",
"ethereum-cryptography": "^0.2.0",
"hash.js": "^1.1.7",
"json-bigint": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { toBN } from './utils/number';

export const IS_BROWSER = typeof window !== 'undefined';
export { IS_BROWSER } from './utils/enc';

export const ZERO = toBN(0);
export const ONE = toBN(1);
Expand Down
2 changes: 1 addition & 1 deletion src/ec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ec as EC, curves } from 'elliptic';
import { addHexPrefix, removeHexPrefix, sanitizeBytes } from 'enc-utils';
import hashJS from 'hash.js';
import assert from 'minimalistic-assert';

import { CONSTANT_POINTS, EC_ORDER, FIELD_PRIME, MAX_ECDSA_VAL, ONE, ZERO } from './constants';
import { addHexPrefix, removeHexPrefix, sanitizeBytes } from './utils/enc';
import { BigNumberish, assertInRange, toBN, toHex } from './utils/number';

export const ec = new EC(
Expand Down
57 changes: 53 additions & 4 deletions src/utils/enc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { IS_BROWSER } from '../constants';
import { toBN } from './number';
/* eslint-disable no-param-reassign */
export const IS_BROWSER = typeof window !== 'undefined';

export const hexToDecimalString = (hex: string): string =>
toBN(`0x${hex.replace(/^0x/, '')}`).toString();
const STRING_ZERO = '0';

export const arrayBufferToString = (array: ArrayBuffer): string =>
String.fromCharCode.apply(null, array as unknown as number[]);
Expand All @@ -12,3 +11,53 @@ export const btoaUniversal = (b: ArrayBuffer): string =>

export const buf2hex = (buffer: Uint8Array) =>
[...buffer].map((x) => x.toString(16).padStart(2, '0')).join('');

/**
* Some function imported from https://github.com/pedrouid/enc-utils/blob/master/src/index.ts
* enc-utils is no dependency to avoid using `Buffer` which just works in node and no browsers
*/

export function removeHexPrefix(hex: string): string {
return hex.replace(/^0x/, '');
}

export function addHexPrefix(hex: string): string {
return `0x${removeHexPrefix(hex)}`;
}

function padString(str: string, length: number, left: boolean, padding = STRING_ZERO): string {
const diff = length - str.length;
let result = str;
if (diff > 0) {
const pad = padding.repeat(diff);
result = left ? pad + str : str + pad;
}
return result;
}

export function padLeft(str: string, length: number, padding = STRING_ZERO): string {
return padString(str, length, true, padding);
}

export function calcByteLength(length: number, byteSize = 8): number {
const remainder = length % byteSize;
return remainder ? ((length - remainder) / byteSize) * byteSize + byteSize : length;
}

export function sanitizeBytes(str: string, byteSize = 8, padding = STRING_ZERO): string {
return padLeft(str, calcByteLength(str.length, byteSize), padding);
}

export function sanitizeHex(hex: string): string {
hex = removeHexPrefix(hex);
hex = sanitizeBytes(hex, 2);
if (hex) {
hex = addHexPrefix(hex);
}
return hex;
}

// implemented using TextEncoder to make it isomorphic
export function utf8ToArray(str: string): Uint8Array {
return new TextEncoder().encode(str);
}
3 changes: 1 addition & 2 deletions src/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import BN from 'bn.js';
import { addHexPrefix, utf8ToArray } from 'enc-utils';
import { keccak256 } from 'ethereum-cryptography/keccak';
import assert from 'minimalistic-assert';

import { CONSTANT_POINTS, FIELD_PRIME, MASK_250, ONE, ZERO } from '../constants';
import { ec } from '../ec';
import { buf2hex } from './enc';
import { addHexPrefix, buf2hex, utf8ToArray } from './enc';
import { BigNumberish, toBN } from './number';

const keccakHex = (value: string): string => addHexPrefix(buf2hex(keccak256(utf8ToArray(value))));
Expand Down
6 changes: 5 additions & 1 deletion src/utils/number.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import BN from 'bn.js';
import { addHexPrefix, removeHexPrefix } from 'enc-utils';
import assert from 'minimalistic-assert';

import { addHexPrefix, removeHexPrefix } from './enc';

export type BigNumberish = string | number | BN;

export const isHex = (hex: string): boolean => {
Expand All @@ -18,6 +19,9 @@ export const toHex = (number: BN): string => {
return addHexPrefix(number.toString('hex'));
};

export const hexToDecimalString = (hex: string): string =>
toBN(`0x${hex.replace(/^0x/, '')}`).toString();

/*
Asserts input is equal to or greater then lowerBound and lower then upperBound.
Assert message specifies inputName.
Expand Down
3 changes: 1 addition & 2 deletions src/utils/starknet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { addHexPrefix } from 'enc-utils';
import { gzip } from 'pako';

import { genKeyPair, getStarkKey } from '../ec';
import { CompressedProgram, Program } from '../types';
import { btoaUniversal } from './enc';
import { addHexPrefix, btoaUniversal } from './enc';
import { starknetKeccak } from './hash';
import { stringify } from './json';
import { toHex } from './number';
Expand Down

0 comments on commit e08e4b5

Please sign in to comment.