diff --git a/.gitmodules b/.gitmodules index fc785e10ad17..f6897a53f119 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "l1-contracts"] path = l1-contracts url = git@github.com:AztecProtocol/aztec3-l1-contracts.git +[submodule "yarn-project/ethereum.js/foundation"] + path = yarn-project/ethereum.js/foundation + url = git@github.com:AztecProtocol/foundation.git diff --git a/yarn-project/ethereum.js/foundation b/yarn-project/ethereum.js/foundation new file mode 160000 index 000000000000..ccdbeea32270 --- /dev/null +++ b/yarn-project/ethereum.js/foundation @@ -0,0 +1 @@ +Subproject commit ccdbeea322705bf7aa59bd9fef44495e7a9d112b diff --git a/yarn-project/ethereum.js/src/bigint_buffer/index.ts b/yarn-project/ethereum.js/src/bigint_buffer/index.ts deleted file mode 100644 index b04faaaf8644..000000000000 --- a/yarn-project/ethereum.js/src/bigint_buffer/index.ts +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Convert a little-endian buffer into a BigInt. - * @param buf The little-endian buffer to convert - * @returns A BigInt with the little-endian representation of buf. - */ -export function toBigIntLE(buf: Buffer): bigint { - const reversed = Buffer.from(buf); - reversed.reverse(); - const hex = reversed.toString('hex'); - if (hex.length === 0) { - return BigInt(0); - } - return BigInt(`0x${hex}`); -} - -/** - * Convert a big-endian buffer into a BigInt - * @param buf The big-endian buffer to convert. - * @returns A BigInt with the big-endian representation of buf. - */ -export function toBigIntBE(buf: Buffer): bigint { - const hex = buf.toString('hex'); - if (hex.length === 0) { - return BigInt(0); - } - return BigInt(`0x${hex}`); -} - -/** - * Convert a BigInt to a little-endian buffer. - * @param num The BigInt to convert. - * @param width The number of bytes that the resulting buffer should be. - * @returns A little-endian buffer representation of num. - */ -export function toBufferLE(num: bigint, width: number): Buffer { - const hex = num.toString(16); - const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex'); - buffer.reverse(); - return buffer; -} - -/** - * Convert a BigInt to a big-endian buffer. - * @param num The BigInt to convert. - * @param width The number of bytes that the resulting buffer should be. - * @returns A big-endian buffer representation of num. - */ -export function toBufferBE(num: bigint, width: number): Buffer { - const hex = num.toString(16); - return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex'); -} diff --git a/yarn-project/ethereum.js/src/contract/abi/abi-coder/ethers/abi-coder.ts b/yarn-project/ethereum.js/src/contract/abi/abi-coder/ethers/abi-coder.ts index c79eedf686cb..cc81618d55b1 100644 --- a/yarn-project/ethereum.js/src/contract/abi/abi-coder/ethers/abi-coder.ts +++ b/yarn-project/ethereum.js/src/contract/abi/abi-coder/ethers/abi-coder.ts @@ -1,6 +1,6 @@ import * as errors from './errors.js'; import { EthAddress } from '../../../../eth_address/index.js'; -import { toBigIntBE, toBufferBE } from '../../../../bigint_buffer/index.js'; + import { bufferToHex, hexToBuffer } from '../../../../hex_string/index.js'; const NegativeOne = BigInt(-1); diff --git a/yarn-project/ethereum.js/src/crypto/random/index.test.ts b/yarn-project/ethereum.js/src/crypto/random/index.test.ts deleted file mode 100644 index 7aa2c7403744..000000000000 --- a/yarn-project/ethereum.js/src/crypto/random/index.test.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { randomBytes } from './index.js'; - -describe('random', () => { - it('randomBytes returns a filled byte array', () => { - const data = randomBytes(32); - expect(data.length).toEqual(32); - let identical = true; - for (let i = 1; i < data.length; ++i) { - identical = identical && data[i] == data[i - 1]; - } - expect(identical).toEqual(false); - }); -}); diff --git a/yarn-project/ethereum.js/src/crypto/random/index.ts b/yarn-project/ethereum.js/src/crypto/random/index.ts deleted file mode 100644 index 8b5df8f56439..000000000000 --- a/yarn-project/ethereum.js/src/crypto/random/index.ts +++ /dev/null @@ -1,38 +0,0 @@ -import isNode from 'detect-node'; -import nodeCrypto from 'crypto'; - -// limit of Crypto.getRandomValues() -// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues -const MAX_BYTES = 65536; - -const getWebCrypto = () => { - if (typeof window !== 'undefined' && window.crypto) return window.crypto; - if (typeof self !== 'undefined' && self.crypto) return self.crypto; - return undefined; -}; - -export const randomBytes = (len: number) => { - if (isNode) { - return nodeCrypto.randomBytes(len) as Buffer; - } - - const crypto = getWebCrypto(); - if (!crypto) { - throw new Error('randomBytes UnsupportedEnvironment'); - } - - const buf = Buffer.allocUnsafe(len); - if (len > MAX_BYTES) { - // this is the max bytes crypto.getRandomValues - // can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues - for (let generated = 0; generated < len; generated += MAX_BYTES) { - // buffer.slice automatically checks if the end is past the end of - // the buffer so we don't have to here - crypto.getRandomValues(buf.slice(generated, generated + MAX_BYTES)); - } - } else { - crypto.getRandomValues(buf); - } - - return buf; -}; diff --git a/yarn-project/ethereum.js/src/eth_account/eth_account.ts b/yarn-project/ethereum.js/src/eth_account/eth_account.ts index 50d2ba1b4ed5..fb291803dacd 100644 --- a/yarn-project/ethereum.js/src/eth_account/eth_account.ts +++ b/yarn-project/ethereum.js/src/eth_account/eth_account.ts @@ -2,7 +2,7 @@ import { EthAddress } from '../eth_address/index.js'; import { mnemonicToSeedSync } from 'bip39'; import hdkey from 'hdkey'; import { default as elliptic } from 'elliptic'; -import { keccak256, randomBytes } from '../crypto/index.js'; +import { keccak256 } from '../crypto/index.js'; import { decryptFromKeyStoreJson, encryptToKeyStoreJson, KeyStoreJson } from '../keystore/index.js'; import { EthSignature, hashMessage, recoverFromSignature, signMessage } from '../eth_sign/index.js'; import { EthTransaction, signedTransaction, signTransaction } from '../eth_transaction/index.js'; diff --git a/yarn-project/ethereum.js/src/eth_address/index.ts b/yarn-project/ethereum.js/src/eth_address/index.ts index 51101f4e2cb2..576fb4ec1bba 100644 --- a/yarn-project/ethereum.js/src/eth_address/index.ts +++ b/yarn-project/ethereum.js/src/eth_address/index.ts @@ -1,5 +1,4 @@ import { keccak256String } from '../crypto/index.js'; -import { randomBytes } from '../crypto/random/index.js'; export class EthAddress { public static ZERO = new EthAddress(Buffer.alloc(20)); diff --git a/yarn-project/ethereum.js/src/eth_rpc/tx_hash.ts b/yarn-project/ethereum.js/src/eth_rpc/tx_hash.ts index f5d07c8ab1b4..54954e037efc 100644 --- a/yarn-project/ethereum.js/src/eth_rpc/tx_hash.ts +++ b/yarn-project/ethereum.js/src/eth_rpc/tx_hash.ts @@ -1,4 +1,3 @@ -import { randomBytes } from '../crypto/random/index.js'; export class TxHash { constructor(private buffer: Buffer) { diff --git a/yarn-project/ethereum.js/src/keystore/index.ts b/yarn-project/ethereum.js/src/keystore/index.ts index 3e27cf54b683..f229a48cbe31 100644 --- a/yarn-project/ethereum.js/src/keystore/index.ts +++ b/yarn-project/ethereum.js/src/keystore/index.ts @@ -1,7 +1,7 @@ import aes from 'browserify-aes'; import { v4 } from 'uuid'; import { EthAddress } from '../eth_address/index.js'; -import { pbkdf2, scrypt, keccak256, randomBytes } from '../crypto/index.js'; +import { pbkdf2, scrypt, keccak256 } from '../crypto/index.js'; interface ScryptKdfParams { dklen: number; diff --git a/yarn-project/ethereum.js/src/log/console.ts b/yarn-project/ethereum.js/src/log/console.ts deleted file mode 100644 index 6315a10e0e75..000000000000 --- a/yarn-project/ethereum.js/src/log/console.ts +++ /dev/null @@ -1,17 +0,0 @@ -export type Logger = (...args: any[]) => void; - -class ConsoleLogger { - constructor(private prefix: string, private logger: (...args: any[]) => void = console.log) {} - - public log(...args: any[]) { - this.logger(`${this.prefix}:`, ...args); - } -} - -export function createLogger(prefix: string): Logger { - if (prefix) { - const logger = new ConsoleLogger(prefix, console.log); - return (...args: any[]) => logger.log(...args); - } - return console.log; -} diff --git a/yarn-project/ethereum.js/src/log/debug.ts b/yarn-project/ethereum.js/src/log/debug.ts deleted file mode 100644 index 812554cb7628..000000000000 --- a/yarn-project/ethereum.js/src/log/debug.ts +++ /dev/null @@ -1,38 +0,0 @@ -import debug from 'debug'; - -let preLogHook: ((...args: any[]) => void) | undefined; -let postLogHook: ((...args: any[]) => void) | undefined; - -function theFunctionThroughWhichAllLogsPass(logger: any, ...args: any[]) { - if (!debug.enabled(logger.namespace)) { - return; - } - if (preLogHook) { - preLogHook(logger.namespace, ...args); - } - logger(...args); - if (postLogHook) { - postLogHook(logger.namespace, ...args); - } -} - -export function createDebugLogger(name: string): any { - const logger = debug(name); - return (...args: any[]) => theFunctionThroughWhichAllLogsPass(logger, ...args); -} - -export function setPreDebugLogHook(fn: (...args: any[]) => void) { - preLogHook = fn; -} - -export function setPostDebugLogHook(fn: (...args: any[]) => void) { - postLogHook = fn; -} - -export function enableLogs(str: string) { - debug.enable(str); -} - -export function isLogEnabled(str: string) { - return debug.enabled(str); -} diff --git a/yarn-project/ethereum.js/src/log/index.ts b/yarn-project/ethereum.js/src/log/index.ts deleted file mode 100644 index cde309b575bb..000000000000 --- a/yarn-project/ethereum.js/src/log/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './console.js'; -export * from './debug.js'; diff --git a/yarn-project/ethereum.js/src/retry/index.ts b/yarn-project/ethereum.js/src/retry/index.ts deleted file mode 100644 index a293108ca6c7..000000000000 --- a/yarn-project/ethereum.js/src/retry/index.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { sleep } from '../sleep/index.js'; -import { Timer } from '../timer/index.js'; - -export function* backoffGenerator() { - const v = [1, 1, 1, 2, 4, 8, 16, 32, 64]; - let i = 0; - while (true) { - yield v[Math.min(i++, v.length - 1)]; - } -} - -export async function retry(fn: () => Promise, name = 'Operation', backoff = backoffGenerator()) { - while (true) { - try { - return await fn(); - } catch (err: any) { - const s = backoff.next().value; - if (s === undefined) { - throw err; - } - console.log(`${name} failed. Will retry in ${s}s...`); - console.log(err); - await sleep(s * 1000); - continue; - } - } -} - -// Call `fn` repeatedly until it returns true or timeout. -// Both `interval` and `timeout` are seconds. -// Will never timeout if the value is 0. -export async function retryUntil(fn: () => Promise, name = '', timeout = 0, interval = 1) { - const timer = new Timer(); - while (true) { - const result = await fn(); - if (result) { - return result; - } - - await sleep(interval * 1000); - - if (timeout && timer.s() > timeout) { - throw new Error(name ? `Timeout awaiting ${name}` : 'Timeout'); - } - } -} diff --git a/yarn-project/ethereum.js/src/serialize/deserializer.ts b/yarn-project/ethereum.js/src/serialize/deserializer.ts deleted file mode 100644 index 7ff25fd9c632..000000000000 --- a/yarn-project/ethereum.js/src/serialize/deserializer.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { - deserializeArrayFromVector, - deserializeBigInt, - deserializeBool, - deserializeBufferFromVector, - deserializeInt32, - deserializeUInt32, -} from './free_funcs.js'; - -export type DeserializeFn = (buf: Buffer, offset: number) => { elem: T; adv: number }; - -export class Deserializer { - constructor(private buf: Buffer, private offset = 0) {} - - public bool() { - return this.exec(deserializeBool) ? true : false; - } - - public uInt32() { - return this.exec(deserializeUInt32); - } - - public int32() { - return this.exec(deserializeInt32); - } - - public bigInt(width = 32) { - return this.exec((buf: Buffer, offset: number) => deserializeBigInt(buf, offset, width)); - } - - public vector() { - return this.exec(deserializeBufferFromVector); - } - - public buffer(width: number) { - const buf = this.buf.slice(this.offset, this.offset + width); - this.offset += width; - return buf; - } - - public string() { - return this.vector().toString(); - } - - public date() { - return new Date(Number(this.bigInt(8))); - } - - public deserializeArray(fn: DeserializeFn) { - return this.exec((buf: Buffer, offset: number) => deserializeArrayFromVector(fn, buf, offset)); - } - - public exec(fn: DeserializeFn): T { - const { elem, adv } = fn(this.buf, this.offset); - this.offset += adv; - return elem; - } - - public getOffset() { - return this.offset; - } -} diff --git a/yarn-project/ethereum.js/src/serialize/free_funcs.ts b/yarn-project/ethereum.js/src/serialize/free_funcs.ts deleted file mode 100644 index 1e03cd0914d8..000000000000 --- a/yarn-project/ethereum.js/src/serialize/free_funcs.ts +++ /dev/null @@ -1,106 +0,0 @@ -import { toBigIntBE, toBufferBE } from '../bigint_buffer/index.js'; - -// For serializing bool. -export function boolToByte(b: boolean) { - const buf = Buffer.alloc(1); - buf.writeUInt8(b ? 1 : 0); - return buf; -} - -// For serializing numbers to 32 bit little-endian form. -export function numToUInt32LE(n: number, bufferSize = 4) { - const buf = Buffer.alloc(bufferSize); - buf.writeUInt32LE(n, bufferSize - 4); - return buf; -} - -// For serializing numbers to 32 bit big-endian form. -export function numToUInt32BE(n: number, bufferSize = 4) { - const buf = Buffer.alloc(bufferSize); - buf.writeUInt32BE(n, bufferSize - 4); - return buf; -} - -// For serializing signed numbers to 32 bit big-endian form. -export function numToInt32BE(n: number, bufferSize = 4) { - const buf = Buffer.alloc(bufferSize); - buf.writeInt32BE(n, bufferSize - 4); - return buf; -} - -// For serializing numbers to 32 bit big-endian form. -export function numToUInt8(n: number) { - const bufferSize = 1; - const buf = Buffer.alloc(bufferSize); - buf.writeUInt8(n, 0); - return buf; -} - -// For serializing a buffer as a vector. -export function serializeBufferToVector(buf: Buffer) { - const lengthBuf = Buffer.alloc(4); - lengthBuf.writeUInt32BE(buf.length, 0); - return Buffer.concat([lengthBuf, buf]); -} - -export function serializeBigInt(n: bigint, width = 32) { - return toBufferBE(n, width); -} - -export function deserializeBigInt(buf: Buffer, offset = 0, width = 32) { - return { elem: toBigIntBE(buf.slice(offset, offset + width)), adv: width }; -} - -export function serializeDate(date: Date) { - return serializeBigInt(BigInt(date.getTime()), 8); -} - -export function deserializeBufferFromVector(vector: Buffer, offset = 0) { - const length = vector.readUInt32BE(offset); - const adv = 4 + length; - return { elem: vector.slice(offset + 4, offset + adv), adv }; -} - -export function deserializeBool(buf: Buffer, offset = 0) { - const adv = 1; - return { elem: buf.readUInt8(offset), adv }; -} - -export function deserializeUInt32(buf: Buffer, offset = 0) { - const adv = 4; - return { elem: buf.readUInt32BE(offset), adv }; -} - -export function deserializeInt32(buf: Buffer, offset = 0) { - const adv = 4; - return { elem: buf.readInt32BE(offset), adv }; -} - -export function deserializeField(buf: Buffer, offset = 0) { - const adv = 32; - return { elem: buf.slice(offset, offset + adv), adv }; -} - -// For serializing an array of fixed length elements. -export function serializeBufferArrayToVector(arr: Buffer[]) { - const lengthBuf = Buffer.alloc(4); - lengthBuf.writeUInt32BE(arr.length, 0); - return Buffer.concat([lengthBuf, ...arr]); -} - -export function deserializeArrayFromVector( - deserialize: (buf: Buffer, offset: number) => { elem: T; adv: number }, - vector: Buffer, - offset = 0, -) { - let pos = offset; - const size = vector.readUInt32BE(pos); - pos += 4; - const arr = new Array(size); - for (let i = 0; i < size; ++i) { - const { elem, adv } = deserialize(vector, pos); - pos += adv; - arr[i] = elem; - } - return { elem: arr, adv: pos - offset }; -} diff --git a/yarn-project/ethereum.js/src/serialize/index.ts b/yarn-project/ethereum.js/src/serialize/index.ts deleted file mode 100644 index fed2e36332bd..000000000000 --- a/yarn-project/ethereum.js/src/serialize/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './free_funcs.js'; -export * from './deserializer.js'; -export * from './serializer.js'; diff --git a/yarn-project/ethereum.js/src/serialize/serialize.test.ts b/yarn-project/ethereum.js/src/serialize/serialize.test.ts deleted file mode 100644 index 60c30af772c5..000000000000 --- a/yarn-project/ethereum.js/src/serialize/serialize.test.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { randomBytes } from '../crypto/index.js'; -import { - serializeBufferToVector, - deserializeBufferFromVector, - deserializeUInt32, - deserializeField, - serializeBufferArrayToVector, - deserializeArrayFromVector, -} from './index.js'; - -describe('serialize', () => { - it('serialize buffer to vector and deserialize it back', () => { - const data = randomBytes(32); - const vector = serializeBufferToVector(data); - expect(vector.length).toBe(36); - - const recovered = deserializeBufferFromVector(vector); - expect(recovered.elem).toEqual(data); - expect(recovered.adv).toEqual(4 + 32); - - const paddedVector = Buffer.concat([randomBytes(10), vector, randomBytes(20)]); - const recovered2 = deserializeBufferFromVector(paddedVector, 10); - expect(recovered2.elem).toEqual(data); - expect(recovered2.adv).toEqual(4 + 32); - }); - - it('deserialize uint32', () => { - const uintBuf = Buffer.alloc(4); - uintBuf.writeUInt32BE(19, 0); - - const recovered = deserializeUInt32(uintBuf); - expect(recovered.elem).toBe(19); - expect(recovered.adv).toBe(4); - - const paddedBuf = Buffer.concat([randomBytes(10), uintBuf, randomBytes(20)]); - const recovered2 = deserializeUInt32(paddedBuf, 10); - expect(recovered2.elem).toBe(19); - expect(recovered2.adv).toBe(4); - }); - - it('deserialize field', () => { - const fieldBuf = randomBytes(32); - - const recovered = deserializeField(fieldBuf); - expect(recovered.elem).toEqual(fieldBuf); - expect(recovered.adv).toBe(32); - - const paddedBuf = Buffer.concat([randomBytes(10), fieldBuf, randomBytes(20)]); - const recovered2 = deserializeField(paddedBuf, 10); - expect(recovered2.elem).toEqual(fieldBuf); - expect(recovered2.adv).toBe(32); - }); - - it('serialize buffer array to vector and deserialize it back', () => { - // Array of uint32 - const uintArr = [7, 13, 16]; - const uintBufArr = uintArr.map(num => { - const uintBuf = Buffer.alloc(4); - uintBuf.writeUInt32BE(num, 0); - return uintBuf; - }); - const uintArrVec = serializeBufferArrayToVector(uintBufArr); - expect(uintArrVec.length).toBe(4 + 4 * 3); - - const recoveredUintArr = deserializeArrayFromVector(deserializeUInt32, uintArrVec); - expect(recoveredUintArr.elem).toEqual(uintArr); - expect(recoveredUintArr.adv).toEqual(4 + 4 * 3); - - const paddedUintArrVec = Buffer.concat([randomBytes(10), uintArrVec, randomBytes(20)]); - const recoveredUintArr2 = deserializeArrayFromVector(deserializeUInt32, paddedUintArrVec, 10); - expect(recoveredUintArr2.elem).toEqual(uintArr); - expect(recoveredUintArr2.adv).toEqual(4 + 4 * 3); - - // Array of field - const fieldArr = [randomBytes(32), randomBytes(32), randomBytes(32)]; - const fieldArrVec = serializeBufferArrayToVector(fieldArr); - expect(fieldArrVec.length).toBe(4 + 32 * 3); - - const recoveredFieldArr = deserializeArrayFromVector(deserializeField, fieldArrVec); - expect(recoveredFieldArr.elem).toEqual(fieldArr); - expect(recoveredFieldArr.adv).toEqual(4 + 32 * 3); - - const paddedFieldVec = Buffer.concat([randomBytes(10), fieldArrVec, randomBytes(20)]); - const recoveredFieldArr2 = deserializeArrayFromVector(deserializeField, paddedFieldVec, 10); - expect(recoveredFieldArr2.elem).toEqual(fieldArr); - expect(recoveredFieldArr2.adv).toEqual(4 + 32 * 3); - }); -}); diff --git a/yarn-project/ethereum.js/src/serialize/serializer.ts b/yarn-project/ethereum.js/src/serialize/serializer.ts deleted file mode 100644 index 8bebb7ada961..000000000000 --- a/yarn-project/ethereum.js/src/serialize/serializer.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { serializeBufferArrayToVector } from './index.js'; -import { - boolToByte, - numToInt32BE, - numToUInt32BE, - serializeBigInt, - serializeBufferToVector, - serializeDate, -} from './free_funcs.js'; - -// export type DeserializeFn = (buf: Buffer, offset: number) => { elem: T; adv: number }; - -export class Serializer { - private buf: Buffer[] = []; - - constructor() {} - - public bool(bool: boolean) { - this.buf.push(boolToByte(bool)); - } - - public uInt32(num: number) { - this.buf.push(numToUInt32BE(num)); - } - - public int32(num: number) { - this.buf.push(numToInt32BE(num)); - } - - public bigInt(num: bigint) { - this.buf.push(serializeBigInt(num)); - } - - /** - * The given buffer is of variable length. Prefixes the buffer with its length. - */ - public vector(buf: Buffer) { - this.buf.push(serializeBufferToVector(buf)); - } - - /** - * Directly serializes a buffer that maybe of fixed, or variable length. - * It is assumed the corresponding deserialize function will handle variable length data, thus the length - * does not need to be prefixed here. - * If serializing a raw, variable length buffer, use vector(). - */ - public buffer(buf: Buffer) { - this.buf.push(buf); - } - - public string(str: string) { - this.vector(Buffer.from(str)); - } - - public date(date: Date) { - this.buf.push(serializeDate(date)); - } - - public getBuffer() { - return Buffer.concat(this.buf); - } - - public serializeArray(arr: T[]) { - this.buf.push(serializeBufferArrayToVector(arr.map((e: any) => e.toBuffer()))); - } -} diff --git a/yarn-project/ethereum.js/src/sleep/index.ts b/yarn-project/ethereum.js/src/sleep/index.ts deleted file mode 100644 index a218ae676dcb..000000000000 --- a/yarn-project/ethereum.js/src/sleep/index.ts +++ /dev/null @@ -1,28 +0,0 @@ -export class InterruptError extends Error {} - -export class InterruptableSleep { - private interruptResolve: (shouldThrow: boolean) => void = () => {}; - private interruptPromise = new Promise(resolve => (this.interruptResolve = resolve)); - private timeouts: NodeJS.Timeout[] = []; - - public async sleep(ms: number) { - let timeout!: NodeJS.Timeout; - const promise = new Promise(resolve => (timeout = setTimeout(() => resolve(false), ms))); - this.timeouts.push(timeout); - const shouldThrow = await Promise.race([promise, this.interruptPromise]); - clearTimeout(timeout); - this.timeouts.splice(this.timeouts.indexOf(timeout), 1); - if (shouldThrow) { - throw new InterruptError('Interrupted.'); - } - } - - public interrupt(sleepShouldThrow = false) { - this.interruptResolve(sleepShouldThrow); - this.interruptPromise = new Promise(resolve => (this.interruptResolve = resolve)); - } -} - -export function sleep(ms: number) { - return new Promise(resolve => setTimeout(resolve, ms)); -} diff --git a/yarn-project/ethereum.js/src/timer/index.ts b/yarn-project/ethereum.js/src/timer/index.ts deleted file mode 100644 index 3e1aac7c0f6d..000000000000 --- a/yarn-project/ethereum.js/src/timer/index.ts +++ /dev/null @@ -1,15 +0,0 @@ -export class Timer { - private start: number; - - constructor() { - this.start = new Date().getTime(); - } - - public ms() { - return new Date().getTime() - this.start; - } - - public s() { - return (new Date().getTime() - this.start) / 1000; - } -}