import {z} from 'zod' export type Id = string; export type KeyShareSource = 'user' | 'backup' | 'bitgo'; export type SignatureShare = {from: KeyShareSource, to: KeyShareSource, share: string}; export type CreateSignatureShareRequest = {signatureShare?: SignatureShare | undefined, signatureShares?: Array | undefined, signerShare?: string | undefined, userPublicGpgKey?: string | undefined, attestation?: ({signature?: string | undefined, credentialId?: string | undefined, clientDataJSON?: string | undefined, authenticatorData?: string | undefined}) | undefined}; export type CreateApiApiV2WalletWalletIdTxrequestsIdTransactionsTransactionIdxSignaturesharesArgs = {walletId: Id, id: Id, transactionIdx: string, body: CreateSignatureShareRequest}; export type CreateApiApiV2WalletWalletIdTxrequestsIdTransactionsTransactionIdxSignaturesharesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const keyShareSource = z.enum(["user", "backup", "bitgo"]); export const signatureShare = z.object({from: keyShareSource, to: keyShareSource, share: z.string()}); 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 }; /** * Create a signature share for a transaction on a transaction request * * POST /api/v2/wallet/{walletId}/txrequests/{id}/transactions/{transactionIdx}/signatureshares */ export const createApiApiV2WalletWalletIdTxrequestsIdTransactionsTransactionIdxSignatureshares = async (args: CreateApiApiV2WalletWalletIdTxrequestsIdTransactionsTransactionIdxSignaturesharesArgs, options: CreateApiApiV2WalletWalletIdTxrequestsIdTransactionsTransactionIdxSignaturesharesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/wallet/${encodeURIComponent(String(args.walletId))}/txrequests/${encodeURIComponent(String(args.id))}/transactions/${encodeURIComponent(String(args.transactionIdx))}/signatureshares${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(signatureShare, await res.json()) };