Changeset View
Changeset View
Standalone View
Standalone View
modules/chronik-client/src/ChronikClient.ts
| Show First 20 Lines • Show All 152 Lines • ▼ Show 20 Lines | export class ChronikClient { | ||||
| * Broadcasts the `rawTx` on the network. | * Broadcasts the `rawTx` on the network. | ||||
| * If `skipTokenChecks` is false, it will be checked that the tx doesn't burn | * If `skipTokenChecks` is false, it will be checked that the tx doesn't burn | ||||
| * any tokens before broadcasting. | * any tokens before broadcasting. | ||||
| */ | */ | ||||
| public async broadcastTx( | public async broadcastTx( | ||||
| rawTx: Uint8Array | string, | rawTx: Uint8Array | string, | ||||
| skipTokenChecks = false, | skipTokenChecks = false, | ||||
| ): Promise<{ txid: string }> { | ): Promise<{ txid: string }> { | ||||
| // Check if user accidentally passed an array (designed for broadcastTxs) | |||||
| if (Array.isArray(rawTx)) { | |||||
| // JS note: Array.isArray(<Uint8Array>) = false | |||||
| throw new Error( | |||||
| 'broadcastTx expects a single rawTx, not an array. Use broadcastTxs for multiple rawTxs.', | |||||
| ); | |||||
| } | |||||
| const request = proto.BroadcastTxRequest.encode({ | const request = proto.BroadcastTxRequest.encode({ | ||||
| rawTx: typeof rawTx === 'string' ? fromHex(rawTx) : rawTx, | rawTx: typeof rawTx === 'string' ? fromHex(rawTx) : rawTx, | ||||
| skipTokenChecks, | skipTokenChecks, | ||||
| }).finish(); | }).finish(); | ||||
| const data = await this._proxyInterface.post('/broadcast-tx', request); | const data = await this._proxyInterface.post('/broadcast-tx', request); | ||||
| const broadcastResponse = proto.BroadcastTxResponse.decode(data); | const broadcastResponse = proto.BroadcastTxResponse.decode(data); | ||||
| return { | return { | ||||
| txid: toHexRev(broadcastResponse.txid), | txid: toHexRev(broadcastResponse.txid), | ||||
| }; | }; | ||||
| } | } | ||||
| /** | /** | ||||
| * Broadcasts the `rawTxs` on the network, only if all of them are valid. | * Broadcasts the `rawTxs` on the network, only if all of them are valid. | ||||
| * If `skipTokenChecks` is false, it will be checked that the txs don't burn | * If `skipTokenChecks` is false, it will be checked that the txs don't burn | ||||
| * any tokens before broadcasting. | * any tokens before broadcasting. | ||||
| */ | */ | ||||
| public async broadcastTxs( | public async broadcastTxs( | ||||
| rawTxs: (Uint8Array | string)[], | rawTxs: (Uint8Array | string)[], | ||||
| skipTokenChecks = false, | skipTokenChecks = false, | ||||
| ): Promise<{ txids: string[] }> { | ): Promise<{ txids: string[] }> { | ||||
| // Check if user accidentally passed broadcastTx input | |||||
| if (!Array.isArray(rawTxs)) { | |||||
| throw new Error( | |||||
| 'broadcastTxs expects an array of rawTxs, not a single rawTx. Use broadcastTx for a single rawTx.', | |||||
| ); | |||||
| } | |||||
| const request = proto.BroadcastTxsRequest.encode({ | const request = proto.BroadcastTxsRequest.encode({ | ||||
| rawTxs: rawTxs.map(rawTx => | rawTxs: rawTxs.map(rawTx => | ||||
| typeof rawTx === 'string' ? fromHex(rawTx) : rawTx, | typeof rawTx === 'string' ? fromHex(rawTx) : rawTx, | ||||
| ), | ), | ||||
| skipTokenChecks, | skipTokenChecks, | ||||
| }).finish(); | }).finish(); | ||||
| const data = await this._proxyInterface.post('/broadcast-txs', request); | const data = await this._proxyInterface.post('/broadcast-txs', request); | ||||
| const broadcastResponse = proto.BroadcastTxsResponse.decode(data); | const broadcastResponse = proto.BroadcastTxsResponse.decode(data); | ||||
| ▲ Show 20 Lines • Show All 2,025 Lines • Show Last 20 Lines | |||||