Skip to content

Commit

Permalink
Switch isBytes to noble-hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed May 11, 2024
1 parent e57cbcc commit 998dd34
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 14 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions src/abi/decoder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { keccak_256 } from '@noble/hashes/sha3';
import { concatBytes } from '@noble/hashes/utils';
import { bytesToHex, concatBytes, hexToBytes } from '@noble/hashes/utils';
import * as P from 'micro-packed';
import { Web3CallArgs, Web3Provider, add0x, strip0x, omit, zip } from '../utils.js';
import { bytesToHex, hexToBytes } from '@noble/hashes/utils';

/*
There is NO network code in the file. However, a user can pass
Expand Down
6 changes: 3 additions & 3 deletions src/net/uniswap-common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Web3Provider, ethHex, ethDecimal, createDecimal } from '../utils.js';
import { Web3Provider, ethHex, ethDecimal, isBytes, createDecimal } from '../utils.js';
import { addr } from '../index.js';
import { tokenFromSymbol } from '../abi/index.js';

Expand Down Expand Up @@ -46,7 +46,7 @@ export async function awaitDeep<T, E extends boolean | undefined>(
let promises: Promise<any>[] = [];
const traverse = (o: any): any => {
if (Array.isArray(o)) return o.map((i) => traverse(i));
if (o instanceof Uint8Array) return o; // TODO: replace with isBytes
if (isBytes(o)) return o;
if (isPromise(o)) return { awaitDeep: promises.push(o) };
if (typeof o === 'object') {
let ret: Record<string, any> = {};
Expand All @@ -65,7 +65,7 @@ export async function awaitDeep<T, E extends boolean | undefined>(
}
const trBack = (o: any): any => {
if (Array.isArray(o)) return o.map((i) => trBack(i));
if (o instanceof Uint8Array) return o; // TODO: replace with isBytes
if (isBytes(o)) return o;
if (typeof o === 'object') {
if (typeof o === 'object' && o.awaitDeep) return values[o.awaitDeep - 1];
let ret: Record<string, any> = {};
Expand Down
1 change: 0 additions & 1 deletion src/net/uniswap-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ function basePaths(a: string, b: string, exactOutput: boolean = false) {
for (let c of BASES) {
for (let fee1 in Fee) {
for (let fee2 in Fee) {
// TODO: replace with ethHex
let path = [wA, packFee(fee1), c.contract, packFee(fee2), wB].map((i) => ethHex.decode(i));
if (exactOutput) path = path.reverse();
res.push({ path: concatBytes(...path) });
Expand Down
4 changes: 3 additions & 1 deletion src/rlp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { numberToVarBytesBE } from '@noble/curves/abstract/utils';
import * as P from 'micro-packed';
import { isBytes } from './utils.js';

// Spec-compliant RLP in 100 lines of code.
export type RLPInput = string | number | Uint8Array | bigint | RLPInput[] | null;
// length: first 3 bit !== 111 ? 6 bit length : 3bit lenlen
Expand Down Expand Up @@ -75,7 +77,7 @@ export const RLP = P.apply(rlpInner, {
if (data == null) return this.decode(empty);
switch (typeof data) {
case 'object':
if (P.isBytes(data)) {
if (isBytes(data)) {
if (data.length === 1) {
const head = data[0];
if (head < 128) return { TAG: 'byte', data: head };
Expand Down
3 changes: 2 additions & 1 deletion src/ssz.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as P from 'micro-packed';
import { sha256 } from '@noble/hashes/sha2';
import { isBytes } from './utils.js';
/*
Simple serialize (SSZ) is the serialization method used on the Beacon Chain.
Expand Down Expand Up @@ -410,7 +411,7 @@ export const union = (
*/
export const bytelist = (maxLen: number): SSZCoder<Uint8Array> => {
const coder = P.validate(P.bytes(null), (value) => {
if (!(value instanceof Uint8Array) || value.length > maxLen)
if (!isBytes(value) || value.length > maxLen)
throw new Error(`SSZ/bytelist: wrong value=${value}`);
return value;
});
Expand Down
4 changes: 2 additions & 2 deletions src/tx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as P from 'micro-packed';
import { addr } from './address.js';
import { RLP } from './rlp.js';
import { amounts, ethHex } from './utils.js';
import { isBytes, amounts, ethHex } from './utils.js';

// Transaction parsers

Expand Down Expand Up @@ -97,7 +97,7 @@ function ensure32<T>(b: any & { length: number }): T {
return b;
}
function ensureBlob(hash: Uint8Array): Uint8Array {
if (!P.isBytes(hash) || hash.length !== 32)
if (!isBytes(hash) || hash.length !== 32)
throw new Error('blobVersionedHashes must contain 32-byte Uint8Array-s');
return hash;
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { hexToBytes as _hexToBytes, bytesToHex } from '@noble/hashes/utils';
import { isBytes as _isBytes, hexToBytes as _hexToBytes, bytesToHex } from '@noble/hashes/utils';
import { Coder, coders } from 'micro-packed';

export const isBytes = _isBytes;

// There is no network code in the library.
// The types are used to check external network provider interfaces.
export type Web3CallArgs = Partial<{
Expand Down

0 comments on commit 998dd34

Please sign in to comment.