Skip to content

Commit

Permalink
feat: add attachment arg to broadcastTransaction()
Browse files Browse the repository at this point in the history
  • Loading branch information
reedrosenbluth committed Feb 15, 2021
1 parent 036bf37 commit f75f250
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions packages/transactions/src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@ export type TxBroadcastResult = TxBroadcastResultOk | TxBroadcastResultRejected;
*/
export async function broadcastTransaction(
transaction: StacksTransaction,
network: StacksNetwork
network: StacksNetwork,
attachment?: Buffer
): Promise<TxBroadcastResult> {
const rawTx = transaction.serialize();
const url = network.getBroadcastApiUrl();

return broadcastRawTransaction(rawTx, url);
return broadcastRawTransaction(rawTx, url, attachment);
}

/**
Expand All @@ -175,18 +176,33 @@ export async function broadcastTransaction(
*/
export async function broadcastRawTransaction(
rawTx: Buffer,
url: string
url: string,
attachment?: Buffer
): Promise<TxBroadcastResult> {
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 {
Expand Down

0 comments on commit f75f250

Please sign in to comment.