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

Fix at+get fns #1290

Merged
merged 10 commits into from
Jun 27, 2024
2 changes: 1 addition & 1 deletion packages/contract/src/contract/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export function getStorageKeyAndType(
}

const rootKeyReversed = reverseHexString(rootKey.slice(2))
const item = get<PortableType>(abi.registry.lookup.types, storage.layout.leaf.ty)
const item = at(abi.registry.lookup.types, parseInt(storage.layout.leaf.ty))
return {
storageType: item,
storageKey: rootKeyReversed,
Expand Down
13 changes: 13 additions & 0 deletions packages/detector/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
export function isBot(): Promise<{
fingerprint: {
resistance:
Expand All @@ -6,7 +19,7 @@
security: undefined
mode: undefined
extension: undefined
engine: any

Check warning on line 22 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
}
| undefined
headlessFeaturesFingerprint:
Expand All @@ -15,7 +28,7 @@
headlessRating: number
stealthRating: number
systemFonts: string
platformEstimate: any[]

Check warning on line 31 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
chromium: boolean
likeHeadless: {
noChrome: boolean
Expand All @@ -24,28 +37,28 @@
noMimeTypes: boolean
notificationIsDenied: boolean
hasKnownBgColor: boolean
prefersLightColor: any

Check warning on line 40 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
uaDataIsBlank: boolean
pdfIsDisabled: boolean
noTaskbar: boolean
hasVvpScreenRes: boolean
hasSwiftShader: any

Check warning on line 45 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
noWebShare: any

Check warning on line 46 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
noContentIndex: boolean
noContactsManager: boolean
noDownlinkMax: boolean
}
headless: {
webDriverIsOn: any

Check warning on line 52 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
hasHeadlessUA: any

Check warning on line 53 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
hasHeadlessWorkerUA: any

Check warning on line 54 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
}
stealth: {
hasIframeProxy: boolean
hasHighChromeIndex: any

Check warning on line 58 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
hasBadChromeRuntime: boolean
hasToStringProxy: boolean
hasBadWebGL: any

Check warning on line 61 in packages/detector/src/index.d.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
}
}
| undefined
Expand Down
4 changes: 2 additions & 2 deletions packages/procaptcha/src/modules/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export function Manager(
const blockNumber = getBlockNumber()
const signer = getExtension(account).signer

const first = at<CaptchaWithProof>(challenge.captchas, 0)
const first = at(challenge.captchas, 0)
if (!first.captcha.datasetId) {
throw new ProsopoDatasetError('CAPTCHA.INVALID_CAPTCHA_ID', {
context: { error: 'No datasetId set for challenge' },
Expand Down Expand Up @@ -384,7 +384,7 @@ export function Manager(
}
const index = state.index
const solutions = state.solutions
const solution = at<string[]>(solutions, index)
const solution = at(solutions, index)
if (solution.includes(hash)) {
// remove the hash from the solution
solution.splice(solution.indexOf(hash), 1)
Expand Down
20 changes: 14 additions & 6 deletions packages/util/src/at.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,28 @@ export type AtOptions = {
noWrap?: boolean // whether to wrap the index around the bounds of the array (true == no wrap, false == wrap indices)
}
// Get an element from an array, throwing an error if it's index is out of bounds or if the element is undefined or null (can be overridden with the options)

export function at(str: string, index: number, options: AtOptions & { optional: true }): string | undefined
export function at(str: string, index: number, options?: AtOptions): string
export function at<T>(items: T[] | string, index: number, options: AtOptions & { optional: false }): T
export function at<T>(
items: (T | undefined)[] | string,
export function at<T extends readonly unknown[]>(
items: T,
index: number,
options: AtOptions & { optional: true }
): T | undefined
export function at<T>(items: T[], index: number, options?: AtOptions): T
export function at<T>(items: T[] | string, index: number, options?: AtOptions): T {
): T[number] | undefined
export function at<T extends readonly unknown[]>(
items: T,
index: number,
options?: AtOptions
): Exclude<T[number], undefined>
export function at<T extends readonly unknown[]>(items: T | string, index: number, options?: AtOptions): T[number] {
if (items.length === 0) {
throw new Error('Array is empty')
}

if (!isFinite(index)) {
throw new Error(`Index ${index} is not a finite number`)
}

if (!options?.noWrap) {
if (index > 0) {
index = index % items.length
Expand Down
7 changes: 3 additions & 4 deletions packages/util/src/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
export function get<T>(obj: T, key: unknown, required?: true): Exclude<T[keyof T], undefined>
export function get<T>(obj: T, key: unknown, required: false): T[keyof T] | undefined
export function get<T>(obj: unknown, key: string | number | symbol, required?: true): Exclude<T, undefined>
export function get<T>(obj: unknown, key: string | number | symbol, required: false): T | undefined

export function get<T>(obj: T, key: string | number | symbol, required?: true): Exclude<T[keyof T], undefined>
export function get<T>(obj: T, key: string | number | symbol, required: false): T[keyof T] | undefined
export function get<T, V>(obj: T, key: unknown, required = true): V {
const value = obj[key as unknown as keyof T]
if (required && value === undefined) {
Expand Down
10 changes: 9 additions & 1 deletion packages/util/src/tests/at.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ describe('at', () => {
const a5: string = at('abc', 0)
const a10: string = at('abc', 0, { optional: false })
const a11: string | undefined = at('abc', 0, { optional: true })
const a12: undefined = at([undefined, undefined, undefined], 0)
const a12: never = at([undefined, undefined, undefined], 0)
const a13: undefined = at([undefined, undefined, undefined], 0, { optional: true })
const a14: undefined = at([undefined, undefined, undefined], 0, { optional: false })

const a15: string = at(['a', 'b', 'c'] as readonly string[], 0)
})

test('infinite index', () => {
expect(() => at([1, 2, 3], Infinity)).to.throw()
expect(() => at([1, 2, 3], -Infinity)).to.throw()
expect(() => at([1, 2, 3], NaN)).to.throw()
})

test('compatible with string', () => {
Expand Down
Loading