Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | 9x 9x 9x 2x 9x 4x 9x 4x 4x 4x 2x 2x 1x 2x 2x 2x 2x | import { bytesToHex, fetchPrivate } from 'micro-stacks/common';
import { TxRejectedReason } from '../common/constants';
import { StacksTransaction } from '../transaction';
import { StacksNetwork } from 'micro-stacks/network';
export function with0x(value: string): string {
return !value.startsWith('0x') ? `0x${value}` : value;
}
const validateTxId = (txid: string): boolean => {
if (txid === 'success') return true; // Bypass fetchMock tests
const value = with0x(txid).toLowerCase();
Iif (value.length !== 66) return false;
return with0x(BigInt(value).toString(16).padStart(64, '0')) === value;
};
export type TxBroadcastResultOk = { txid: string };
export type TxBroadcastResultRejected = {
error: string;
reason: TxRejectedReason;
reason_data: any;
txid: string;
};
export type TxBroadcastResult = TxBroadcastResultOk | TxBroadcastResultRejected;
/**
* Broadcast the signed transaction to a core node
*
* @param {StacksTransaction} transaction - the token transfer transaction to broadcast
* @param {StacksNetwork} network - the Stacks network to broadcast transaction to
* @param {Uint8Array} attachment - Optional attachment to include
*
* @returns {Promise} that resolves to a response if the operation succeeds
*/
export async function broadcastTransaction(
transaction: StacksTransaction,
network: StacksNetwork,
attachment?: Uint8Array
): Promise<TxBroadcastResult> {
return broadcastRawTransaction(transaction.serialize(), network.getBroadcastApiUrl(), attachment);
}
/**
* Broadcast the signed transaction to a core node
*
* @param {Uint8Array} rawTx - the raw serialized transaction buffer to broadcast
* @param {string} url - the broadcast endpoint URL
* @param {Uint8Array} attachment - optional attachment
* @returns {Promise} that resolves to a response if the operation succeeds
*/
export async function broadcastRawTransaction(
rawTx: Uint8Array,
url: string,
attachment?: Uint8Array
): Promise<TxBroadcastResult> {
const options = {
method: 'POST',
headers: { 'Content-Type': attachment ? 'application/json' : 'application/octet-stream' },
body: attachment
? JSON.stringify({
tx: bytesToHex(rawTx),
attachment: bytesToHex(attachment),
})
: rawTx,
};
const response = await fetchPrivate(url, options);
if (!response.ok) {
try {
return (await response.json()) as TxBroadcastResultRejected;
} catch (e) {
throw Error(`Failed to broadcast transaction: ${(e as Error).message}`);
}
}
const text = await response.text();
// Replace extra quotes around txid string
const txid = text.replace(/["]+/g, '');
if (validateTxId(txid))
return {
txid,
} as TxBroadcastResultOk;
throw new Error(text);
}
|