diff --git a/packages/transactions/src/builders.ts b/packages/transactions/src/builders.ts index 90ca91084..39cb13c64 100644 --- a/packages/transactions/src/builders.ts +++ b/packages/transactions/src/builders.ts @@ -157,12 +157,13 @@ export type TxBroadcastResult = TxBroadcastResultOk | TxBroadcastResultRejected; */ export async function broadcastTransaction( transaction: StacksTransaction, - network: StacksNetwork + network: StacksNetwork, + attachment?: Buffer ): Promise { const rawTx = transaction.serialize(); const url = network.getBroadcastApiUrl(); - return broadcastRawTransaction(rawTx, url); + return broadcastRawTransaction(rawTx, url, attachment); } /** @@ -175,18 +176,33 @@ export async function broadcastTransaction( */ export async function broadcastRawTransaction( rawTx: Buffer, - url: string + url: string, + attachment?: Buffer ): Promise { - const requestHeaders = { - 'Content-Type': 'application/octet-stream', - }; - const options = { + let options = { method: 'POST', - headers: requestHeaders, - body: rawTx, }; + if (attachment) { + options = Object.assign(options, { + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + tx: rawTx.toString('hex'), + attachment: attachment.toString('hex') + }) + }) + } else { + options = Object.assign(options, { + headers: { + 'Content-Type': 'application/octet-stream', + }, + body: rawTx, + }) + } + const response = await fetchPrivate(url, options); if (!response.ok) { try {