diff --git a/src/transactions/sendBtcTransaction.ts b/src/transactions/sendBtcTransaction.ts index a9b154a..e4e0ad8 100644 --- a/src/transactions/sendBtcTransaction.ts +++ b/src/transactions/sendBtcTransaction.ts @@ -2,7 +2,22 @@ import type { Json } from 'jsontokens'; import { createUnsecuredToken } from 'jsontokens'; import { getDefaultProvider } from '../provider'; -import type { SendBtcTransactionOptions } from './types'; +import type { + Recipient, + SendBtcTransactionOptions, + SerializedRecipient, + SerializedSendBtcTransactionPayload, +} from './types'; + +const serializer = (recipient: Recipient[]): SerializedRecipient[] => { + return recipient.map((value) => { + const { address, amountSats } = value; + return { + address, + amountSats: amountSats.toString(), + }; + }); +}; export const sendBtcTransaction = async (options: SendBtcTransactionOptions) => { const { getProvider = getDefaultProvider } = options; @@ -11,16 +26,30 @@ export const sendBtcTransaction = async (options: SendBtcTransactionOptions) => throw new Error('No Bitcoin wallet installed'); } - const { recipients, senderAddress } = options.payload; + const { recipients, senderAddress, network, message } = options.payload; if (!recipients || recipients.length === 0) { throw new Error('At least one recipient is required'); } + if ( + recipients.some( + (item) => typeof item.address !== 'string' || typeof item.amountSats !== 'bigint' + ) + ) { + throw new Error('Incorrect recipient format'); + } if (!senderAddress) { throw new Error('The sender address is required'); } try { - const request = createUnsecuredToken(options.payload as unknown as Json); + const serializedRecipients: SerializedRecipient[] = serializer(recipients); + const serializedPayload: SerializedSendBtcTransactionPayload = { + network, + senderAddress, + message, + recipients: serializedRecipients, + }; + const request = createUnsecuredToken(serializedPayload as unknown as Json); const response = await provider.sendBtcTransaction(request); options.onFinish?.(response); } catch (error) { diff --git a/src/transactions/types.ts b/src/transactions/types.ts index 548e3cb..532ba6d 100644 --- a/src/transactions/types.ts +++ b/src/transactions/types.ts @@ -5,12 +5,20 @@ export interface Recipient { amountSats: bigint; } +export type SerializedRecipient = Omit & { + amountSats: string; +}; + export interface SendBtcTransactionPayload extends RequestPayload { recipients: Recipient[]; senderAddress: string; message?: string; } +export type SerializedSendBtcTransactionPayload = Omit & { + recipients: SerializedRecipient[]; +}; + export type SendBtcTransactionResponse = string; export type SendBtcTransactionOptions = RequestOptions<