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

refactor: squash proof api #9351

Merged
merged 4 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions packages/web3-providers/src/NextID/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class NextIDStorageAPI implements NextIDBaseAPI.Storage {
urlcat(BASE_URL, '/v1/kv', { persona: personaPublicKey }),
undefined,
{
enableCache: true,
enableSquash: true,
},
)
Expand Down Expand Up @@ -80,7 +79,6 @@ export class NextIDStorageAPI implements NextIDBaseAPI.Storage {
urlcat(BASE_URL, '/v1/kv/by_identity', { platform, identity }),
undefined,
{
enableCache: true,
enableSquash: true,
},
)
Expand All @@ -91,7 +89,6 @@ export class NextIDStorageAPI implements NextIDBaseAPI.Storage {
}
async get<T>(personaPublicKey: string): Promise<T> {
return fetchJSON<T>(urlcat(BASE_URL, '/v1/kv', { persona: personaPublicKey }), undefined, {
enableCache: true,
enableSquash: true,
})
}
Expand Down
30 changes: 16 additions & 14 deletions packages/web3-providers/src/NextID/proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ import {
import { staleNextIDCached } from './helpers.js'
import PRESET_LENS from './preset-lens.json'

type PresetLensTwitter = keyof typeof PRESET_LENS

const BASE_URL =
process.env.channel === 'stable' && process.env.NODE_ENV === 'production' ? PROOF_BASE_URL_PROD : PROOF_BASE_URL_DEV

interface CreatePayloadBody {
action: string
platform: string
Expand Down Expand Up @@ -74,6 +73,10 @@ const getExistedBindingQueryURL = (platform: string, identity: string, personaPu
})

export class NextIDProofAPI implements NextIDBaseAPI.Proof {
fetchFromProofService<T>(request: Request | RequestInfo, init?: RequestInit) {
return fetchJSON<T>(request, init, { enableSquash: true, enableCache: true })
}

async bindProof(
uuid: string,
personaPublicKey: string,
Expand Down Expand Up @@ -119,16 +122,17 @@ export class NextIDProofAPI implements NextIDBaseAPI.Proof {
}

async queryExistedBindingByPersona(personaPublicKey: string) {
const url = getPersonaQueryURL(NextIDPlatform.NextID, personaPublicKey)
const { ids } = await fetchJSON<NextIDBindings>(url, undefined, { enableSquash: true })
const { ids } = await this.fetchFromProofService<NextIDBindings>(
getPersonaQueryURL(NextIDPlatform.NextID, personaPublicKey),
)
// Will have only one item when query by personaPublicKey
return first(ids)
}

async queryExistedBindingByPlatform(platform: NextIDPlatform, identity: string, page = 1) {
if (!platform && !identity) return []

const response = await fetchJSON<NextIDBindings>(
const response = await this.fetchFromProofService<NextIDBindings>(
urlcat(BASE_URL, '/v1/proof', {
platform,
identity,
Expand All @@ -138,8 +142,6 @@ export class NextIDProofAPI implements NextIDBaseAPI.Proof {
// sort: 'activated_at',
// order: 'desc',
}),
undefined,
{ enableSquash: true },
)

return sortBy(response.ids, (x) => -x.activated_at)
Expand All @@ -163,16 +165,14 @@ export class NextIDProofAPI implements NextIDBaseAPI.Proof {
const nextIDPersonaBindings: NextIDPersonaBindings[] = []
let page = 1
do {
const result = await fetchJSON<NextIDBindings>(
const result = await this.fetchFromProofService<NextIDBindings>(
urlcat(BASE_URL, '/v1/proof', {
platform,
identity,
exact,
page,
order: 'desc',
}),
undefined,
{ enableSquash: true },
)

const personaBindings = result.ids
Expand All @@ -191,8 +191,9 @@ export class NextIDProofAPI implements NextIDBaseAPI.Proof {
try {
if (!platform && !identity) return false

const url = getExistedBindingQueryURL(platform, identity, personaPublicKey)
const result = await fetchJSON<BindingProof | undefined>(url, undefined, { enableSquash: true })
const result = await this.fetchFromProofService<BindingProof | undefined>(
getExistedBindingQueryURL(platform, identity, personaPublicKey),
)
return !!result?.is_valid
} catch {
return false
Expand Down Expand Up @@ -332,9 +333,10 @@ query GET_LENS_PROFILES($platform: String, $identity: String) {
)

const connections = data.identity?.neighbor.filter((x) => x.identity.platform === NextIDPlatform.LENS) || []
const id = lowerCaseId as keyof typeof PRESET_LENS

if (connections.length === 0 && PRESET_LENS[lowerCaseId as PresetLensTwitter]) {
return PRESET_LENS[lowerCaseId as PresetLensTwitter]
if (connections.length === 0 && PRESET_LENS[id]) {
return PRESET_LENS[id]
}

return connections.map(({ identity }) => ({
Expand Down
3 changes: 0 additions & 3 deletions packages/web3-providers/src/helpers/fetchCached.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import type { Fetcher } from './fetch.js'
const { fetch: originalFetch } = globalThis

export enum Duration {
IMMEDIATELY = 0,
SHORT = 60000, // 1 min
MEDIUM = 1800000, // 30 mins
LONG = 43200000, // 12 hours
}

const RULES = {
'https://kv-service.nextnext.id/': Duration.IMMEDIATELY,
'https://kv-service.next.id/': Duration.IMMEDIATELY,
// twitter shorten links
'https://t.co': Duration.MEDIUM,
'https://gitcoin.co/grants/v1/api/grant': Duration.SHORT,
Expand Down