> For the complete documentation index, see [llms.txt](https://docs.stacks.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stacks.co/reference/stacks.js/stacks-bitcoin-staking/reclaim/finalizereclaim.md).

# finalizeReclaim

Assembles the lockup witness for the chosen branch from the signatures already on a reclaim transaction, and returns the transaction ready to broadcast. No network call: broadcasting is left to the caller.

***

### Usage

```ts
import { buildReclaim, finalizeReclaim } from '@stacks/bitcoin-staking';
import type { BuildReclaimOpts } from '@stacks/bitcoin-staking';

declare const opts: BuildReclaimOpts; // path: 'locktime'
declare const stakerBtcPrivateKey: Uint8Array; // key behind the lockup's unlockBytes

const tx = buildReclaim(opts);
tx.signIdx(stakerBtcPrivateKey, 0);

const { txHex, txid } = finalizeReclaim({ path: 'locktime', tx });
// broadcast txHex through your Bitcoin node or indexer
```

#### Notes

* Reads the `partialSig` entries on input 0, left by `tx.signIdx` or by `tx.updateInput(0, { partialSig })`, and matches each public key against the lockup script to tell the staker's signature from the cosigner's.
* The witness stack is `[stakerSig, 0x01, witnessScript]` for `'locktime'` and `[stakerSig, cosignerSig, preimage, <empty>, witnessScript]` for `'early-exit'`.
* Throws an `Error` if input 0 has no witness script, if the script does not have the pox-5 lockup layout, if the staker subscript has other than one public key, or if the staker's signature is missing. On `'early-exit'` it also throws if the early-unlock subscript has other than one public key or the cosigner's signature is missing. Multi-key subscripts are valid in pox-5 but this function cannot assemble their witness.
* `stxAddress` is not checked against the script. A wrong address produces a witness that Bitcoin rejects at the `OP_EQUALVERIFY` after `OP_SHA256`.
* Mutates `tx`: the witness is written to input 0.
* `txid` is computed locally. It identifies the transaction you broadcast and says nothing about whether it was accepted or confirmed.

[**Reference Link**](https://github.com/stx-labs/stacks.js/blob/6101c99efe5a9616ce7e16cef68e28fd10676e7e/packages/bitcoin-staking/src/reclaim.ts#L263-L284)

***

### Signature

```ts
function finalizeReclaim(opts: FinalizeReclaimOpts): { txHex: string; txid: string };
```

`opts` is a [FinalizeReclaimOpts](/reference/stacks.js/stacks-bitcoin-staking/reclaim/finalizereclaimopts.md).

***

### Returns

`{ txHex: string; txid: string }`

| Field   | Type     | Meaning                                                    |
| ------- | -------- | ---------------------------------------------------------- |
| `txHex` | `string` | The finalized transaction, hex-encoded, ready to broadcast |
| `txid`  | `string` | The transaction ID                                         |

***

### Parameters

#### opts.path (required)

* **Type**: `'locktime' | 'early-exit'`

The branch to spend. Use the `path` the transaction was built with in [buildReclaim](/reference/stacks.js/stacks-bitcoin-staking/reclaim/buildreclaim.md), which sets the locktime and sequence for that branch.

#### opts.tx (required)

* **Type**: `btc.Transaction`

The signed reclaim transaction from [buildReclaim](/reference/stacks.js/stacks-bitcoin-staking/reclaim/buildreclaim.md). `btc` is `@scure/btc-signer`.

#### opts.stxAddress (required for 'early-exit')

* **Type**: `string`

The staker principal the lockup script commits to. Used to compute the preimage with [computeRegisterPreimage](/reference/stacks.js/stacks-bitcoin-staking/script/computeregisterpreimage.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.stacks.co/reference/stacks.js/stacks-bitcoin-staking/reclaim/finalizereclaim.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
