diff --git a/packages/shared-base/src/NextID/type.ts b/packages/shared-base/src/NextID/type.ts index 90e920bd141b..6b31c372d3e4 100644 --- a/packages/shared-base/src/NextID/type.ts +++ b/packages/shared-base/src/NextID/type.ts @@ -35,6 +35,14 @@ export interface BindingProof { is_valid: boolean } +interface Pagination { + total: number + per: number + current: number + next: number +} + export interface NextIDBindings { + pagination: Pagination ids: NextIDPersonaBindings[] } diff --git a/packages/web3-providers/src/NextID/index.ts b/packages/web3-providers/src/NextID/index.ts index ae1365ca5030..3893ece3b76f 100644 --- a/packages/web3-providers/src/NextID/index.ts +++ b/packages/web3-providers/src/NextID/index.ts @@ -1,4 +1,12 @@ -import { fromHex, toBase64, NextIDBindings, NextIDPlatform, NextIDPayload, NextIDAction } from '@masknet/shared-base' +import { + BindingProof, + fromHex, + NextIDAction, + NextIDBindings, + NextIDPayload, + NextIDPlatform, + toBase64, +} from '@masknet/shared-base' import urlcat from 'urlcat' import { first } from 'lodash-unified' @@ -14,6 +22,20 @@ interface CreatePayloadBody { public_key: string } +interface CreatePayloadResponse { + post_content: string + sign_payload: string +} + +export async function fetchJSON(requestInfo: RequestInfo, requestInit?: RequestInit): Promise { + const res = await globalThis.fetch(requestInfo, { mode: 'cors', ...requestInit }) + const result = await res.json() + + if (result.message || !res.ok) throw new Error(result.message) + return result +} + +// TODO: remove 'bind' in project for business context. export async function bindProof( personaPublicKey: string, action: NextIDAction, @@ -37,48 +59,46 @@ export async function bindProof( }, } - const response = await fetch(urlcat(BASE_URL, '/v1/proof'), { + return fetchJSON(urlcat(BASE_URL, '/v1/proof'), { body: JSON.stringify(requestBody), method: 'POST', - mode: 'cors', }) - - const result = (await response.json()) as { message: string } - - if (!response.ok) throw new Error(result.message) - return response } export async function queryExistedBindingByPersona(personaPublicKey: string) { - const response = await fetch(urlcat(BASE_URL, '/v1/proof', { platform: 'nextid', identity: personaPublicKey }), { - mode: 'cors', - }) - - const result = (await response.json()) as NextIDBindings + const response = await fetchJSON( + urlcat(BASE_URL, '/v1/proof', { platform: NextIDPlatform.NextId, identity: personaPublicKey }), + ) // Will have only one item when query by personaPublicKey - return first(result.ids) + return first(response.ids) } -export async function queryExistedBindingByPlatform(platform: NextIDPlatform, identity: string) { +export async function queryExistedBindingByPlatform(platform: NextIDPlatform, identity: string, page?: number) { if (!platform && !identity) return [] - const response = await fetch(urlcat(BASE_URL, '/v1/proof', { platform: platform, identity: identity }), { - mode: 'cors', - }) - - const result = (await response.json()) as NextIDBindings - return result.ids -} + const response = await fetchJSON( + urlcat(BASE_URL, '/v1/proof', { platform: platform, identity: identity }), + ) -export async function queryExistedBinding(platform: NextIDPlatform, identity: string): Promise { - throw new Error('To be implemented.') + // TODO: merge Pagination into this + return response.ids } export async function queryIsBound(personaPublicKey: string, platform: NextIDPlatform, identity: string) { if (!platform && !identity) return false - const ids = await queryExistedBindingByPlatform(platform, identity) - return ids.some((x) => x.persona.toLowerCase() === personaPublicKey.toLowerCase()) + try { + await fetchJSON( + urlcat(BASE_URL, '/v1/proof/exists', { + platform: platform, + identity: identity, + public_key: personaPublicKey, + }), + ) + return true + } catch { + return false + } } export async function createPersonaPayload( @@ -94,15 +114,13 @@ export async function createPersonaPayload( public_key: personaPublicKey, } - const response = await fetch(urlcat(BASE_URL, '/v1/proof/payload'), { + const response = await fetchJSON(urlcat(BASE_URL, '/v1/proof/payload'), { body: JSON.stringify(requestBody), method: 'POST', - mode: 'cors', }) - const result = await response.json() return { - postContent: result.post_content, - signPayload: JSON.stringify(JSON.parse(result.sign_payload)), + postContent: response.post_content, + signPayload: JSON.stringify(JSON.parse(response.sign_payload)), } }