buildReclaim
Builds the unsigned Bitcoin transaction that spends a P2WSH bond lockup to an address you choose, as a @scure/btc-signer Transaction. Pure computation: no network call.
Usage
import { buildReclaim, finalizeReclaim } from '@stacks/bitcoin-staking';
import type { RegisterMetadata } from '@stacks/bitcoin-staking';
declare const meta: RegisterMetadata; // stored from buildRegisterMetadata
declare const fundingTxid: string; // transaction that funded meta.lockAddress
declare const sweepAddress: string; // your Bitcoin address
declare const stakerBtcPrivateKey: Uint8Array; // key behind the lockup's unlockBytes
const tx = buildReclaim({
path: 'locktime',
utxo: { txid: fundingTxid, vout: 0, value: 100_000n },
network: 'mainnet',
output: { address: sweepAddress, feeSats: 1_000n },
lockScript: meta.lockScript,
});
tx.signIdx(stakerBtcPrivateKey, 0);
const { txHex, txid } = finalizeReclaim({ path: 'locktime', tx });
// broadcast txHex through your Bitcoin node or indexerNotes
The transaction has one input and one output. The input spends
utxowithwitnessUtxoandwitnessScriptset, sotx.toPSBT()gives a complete PSBT. The output paysutxo.value - feeSatssats tooutput.address.'locktime'sets the transactionlockTimeto the unlock height read fromlockScriptand the input sequence to0xfffffffe.'early-exit'setslockTimeto 0 and the sequence to0xffffffff.Throws an
Errorif:lockScriptdoes not have the pox-5 lockup layout from buildLockScript.feeSatsis negative, or is at or aboveutxo.value.The swept amount is below 546 sats, the dust limit this function applies.
utxo.scriptPubKeyis set and differs from the P2WSH form oflockScript.pathis'locktime'andlockScriptencodes no unlock height.
You can change outputs or the fee on the returned transaction before signing. The
SIGHASH_ALLsignature commits to them.With an in-process key, sign with
tx.signIdx(privateKey, 0). For a signer that signs a bare digest, compute it with computeReclaimSighash and attach the result withtx.updateInput(0, { partialSig }). Then call finalizeReclaim.The bond exit process is covered in Ending or changing a bond position.
Signature
btc is @scure/btc-signer. opts is a BuildReclaimOpts.
Returns
btc.Transaction
The unsigned reclaim transaction, a Transaction from @scure/btc-signer.
Parameters
opts.path (required)
Type:
ReclaimPath
The branch to spend, 'locktime' or 'early-exit'. See ReclaimPath.
opts.utxo (required)
Type:
Utxo
The lockup output to spend. value is in sats. See Utxo.
opts.network (required)
Type:
StacksNetworkName | StacksNetwork
The network whose Bitcoin address encoding output.address uses: a name such as 'mainnet' or 'testnet', or a StacksNetwork object.
opts.output (required)
Type:
{ address: string; feeSats: IntegerType }
The Bitcoin address that receives the sats, and the fee in sats.
opts.lockScript (required)
Type:
Uint8Array | string
The lockup witness script, as bytes or hex: the stored RegisterMetadata lockScript, or a rebuild with buildLockScript from the same inputs.
Was this helpful?