From e08e4b53bfbdece3f824fedf96101405219da4b5 Mon Sep 17 00:00:00 2001 From: Janek Rahrt Date: Thu, 28 Oct 2021 15:30:39 +0200 Subject: [PATCH] fix: remove enc-utils to remove buffer dep buffer is not allowed in browsers --- __tests__/ec.test.ts | 3 +-- __tests__/utils.test.ts | 2 +- __tests__/wallet.test.ts | 3 +-- package-lock.json | 31 ++++++---------------- package.json | 1 - src/constants.ts | 2 +- src/ec.ts | 2 +- src/utils/enc.ts | 57 +++++++++++++++++++++++++++++++++++++--- src/utils/hash.ts | 3 +-- src/utils/number.ts | 6 ++++- src/utils/starknet.ts | 3 +-- 11 files changed, 73 insertions(+), 40 deletions(-) diff --git a/__tests__/ec.test.ts b/__tests__/ec.test.ts index 1f9d53b64..ad6defdb8 100644 --- a/__tests__/ec.test.ts +++ b/__tests__/ec.test.ts @@ -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', () => { diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 162d95cd6..dfdf545cb 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -6,7 +6,7 @@ const { IS_BROWSER } = constants; const { json: { parse, stringify }, starknet: { compressProgram, getSelectorFromName, makeAddress }, - enc: { hexToDecimalString }, + number: { hexToDecimalString }, } = utils; const compiledArgentAccount = parse( diff --git a/__tests__/wallet.test.ts b/__tests__/wallet.test.ts index 71ea53c90..88e9830b0 100644 --- a/__tests__/wallet.test.ts +++ b/__tests__/wallet.test.ts @@ -1,7 +1,5 @@ import fs from 'fs'; -import { addHexPrefix } from 'enc-utils'; - import { CompiledContract, Contract, @@ -13,6 +11,7 @@ import { utils, waitForTx, } from '../src'; +import { addHexPrefix } from '../src/utils/enc'; import { toBN } from '../src/utils/number'; const { diff --git a/package-lock.json b/package-lock.json index d90671e71..0c38bfa44 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,17 @@ { "name": "starknet", - "version": "1.1.1", + "version": "1.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "starknet", - "version": "1.1.1", + "version": "1.2.0", "license": "MIT", "dependencies": { "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", @@ -4515,15 +4514,6 @@ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", "dev": true }, - "node_modules/enc-utils": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/enc-utils/-/enc-utils-3.0.0.tgz", - "integrity": "sha512-e57t/Z2HzWOLwOp7DZcV0VMEY8t7ptWwsxyp6kM2b2zrk6JqIpXxzkruHAMiBsy5wg9jp/183GdiRXCvBtzsYg==", - "dependencies": { - "is-typedarray": "1.0.0", - "typedarray-to-buffer": "3.1.5" - } - }, "node_modules/enquirer": { "version": "2.3.6", "resolved": "https://registry.npmjs.org/enquirer/-/enquirer-2.3.6.tgz", @@ -6404,7 +6394,8 @@ "node_modules/is-typedarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", - "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=" + "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=", + "dev": true }, "node_modules/is-weakref": { "version": "1.0.1", @@ -12477,6 +12468,7 @@ "version": "3.1.5", "resolved": "https://registry.npmjs.org/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz", "integrity": "sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==", + "dev": true, "dependencies": { "is-typedarray": "^1.0.0" } @@ -16259,15 +16251,6 @@ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", "dev": true }, - "enc-utils": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/enc-utils/-/enc-utils-3.0.0.tgz", - "integrity": "sha512-e57t/Z2HzWOLwOp7DZcV0VMEY8t7ptWwsxyp6kM2b2zrk6JqIpXxzkruHAMiBsy5wg9jp/183GdiRXCvBtzsYg==", - "requires": { - "is-typedarray": "1.0.0", - "typedarray-to-buffer": "3.1.5" - } - }, "enquirer": { "version": "2.3.6", "resolved": "https://registry.npmjs.org/enquirer/-/enquirer-2.3.6.tgz", @@ -17668,7 +17651,8 @@ "is-typedarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", - "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=" + "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=", + "dev": true }, "is-weakref": { "version": "1.0.1", @@ -22210,6 +22194,7 @@ "version": "3.1.5", "resolved": "https://registry.npmjs.org/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz", "integrity": "sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==", + "dev": true, "requires": { "is-typedarray": "^1.0.0" } diff --git a/package.json b/package.json index 36ac987f6..18637faec 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/constants.ts b/src/constants.ts index 418be7b9c..56ade877f 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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); diff --git a/src/ec.ts b/src/ec.ts index f3269e1df..7b7e6e30e 100644 --- a/src/ec.ts +++ b/src/ec.ts @@ -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( diff --git a/src/utils/enc.ts b/src/utils/enc.ts index 07a4052aa..3e2e7e235 100644 --- a/src/utils/enc.ts +++ b/src/utils/enc.ts @@ -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[]); @@ -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); +} diff --git a/src/utils/hash.ts b/src/utils/hash.ts index e8b662be8..7ca70a28b 100644 --- a/src/utils/hash.ts +++ b/src/utils/hash.ts @@ -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)))); diff --git a/src/utils/number.ts b/src/utils/number.ts index bc3339314..20781622e 100644 --- a/src/utils/number.ts +++ b/src/utils/number.ts @@ -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 => { @@ -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. diff --git a/src/utils/starknet.ts b/src/utils/starknet.ts index 33d718260..905b789a8 100644 --- a/src/utils/starknet.ts +++ b/src/utils/starknet.ts @@ -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';