import {z} from 'zod' export type BlockchainAddress = string; export type CommonDID = string; export type Asset = string; export type GetApiV1AddressesAddressOwnershipsAddressArgs = {address: BlockchainAddress, vasp_did: CommonDID, asset: Asset}; export type GetApiV1AddressesAddressOwnershipsAddressOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const blockchainAddress = z.string(); export const asset = z.string(); export const blockchainAddressInfoSource = z.enum(["UNKNOWN", "ADDRESS_HASH", "ADDRESS_GRAPH", "CHAINALYSIS", "ELLIPTIC"]); export const addressOwnershipResponse = z.object({owner_vasp_did: z.string().optional(), owner_vasp_name: z.string().optional(), address: blockchainAddress.optional(), address_type: z.enum(["HOSTED", "UNHOSTED"]).optional(), asset: asset.optional(), source: blockchainAddressInfoSource.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get address ownership information * * GET /v1/addresses/address-ownerships/{address} */ export const getApiV1AddressesAddressOwnershipsAddress = async (args: GetApiV1AddressesAddressOwnershipsAddressArgs, options?: GetApiV1AddressesAddressOwnershipsAddressOptions) => { const {baseUrl = "https://api.eu1.notabene.id", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.vasp_did != null) query.append("vasp_did", String(args.vasp_did)) if (args.asset != null) query.append("asset", String(args.asset)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/addresses/address-ownerships/${encodeURIComponent(String(args.address))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(addressOwnershipResponse, await res.json()) };