From f75f250a86789a8d1a6f5a09842ff81bfa345b96 Mon Sep 17 00:00:00 2001 From: Reed Rosenbluth <331327+reedrosenbluth@users.noreply.github.com> Date: Fri, 12 Feb 2021 15:19:15 -0500 Subject: [PATCH] feat: add attachment arg to broadcastTransaction() --- packages/transactions/src/builders.ts | 34 ++++++++++++++++++++------- 1 file changed, 25 insertions(+), 9 deletions(-) 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 {