> For the complete documentation index, see [llms.txt](https://docs.b14g.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.b14g.xyz/user-guide/for-btc-holders/guide-btc-staking-babylon/unbonding-via-cli.md).

# Unbonding via CLI

## b14g Babylon BTC Staking Tool

A CLI tool that lets you stake BTC natively on the Bitcoin network and earn co-staking rewards from the Babylon Genesis network via b14g.

{% embed url="<https://github.com/b14glabs/b14g-babylon-btc-staking-tool>" %}

## Unbonding Workflow

The unbonding process lets you initiate unbonding, which is the first step needed before you can withdraw your staked BTC back to your wallet.

**1. Fetch Delegation Data**

* Connects to Babylon staking API using the staking transaction hash
* Retrieves delegation information including:
  * `stakerBtcPkHex`: Your Bitcoin public key
  * `stakingAmount`: Amount of BTC staked (in satoshis)
  * `stakingTimelock`: Original staking period
  * `stakingTxHex`: Original staking transaction
  * `finalityProviderBtcPksHex`: Finality provider public keys
  * `covenantUnbondingSignatures`: Pre-signed signatures from covenant committee
  * `unbondingTxHex`: Pre-created unbonding transaction
  * `paramsVersion`: Version of staking parameters used

**2. Create Signed Unbonding Transaction**

* Uses `createSignedBtcUnbondingTransaction()` from `@babylonlabs-io/btc-staking-ts` with:
  * Staker information (address and public key)
  * Staking input parameters (finality providers, amount, timelock)
  * Parameters version
  * Original staking transaction
  * Pre-created unbonding transaction (unsigned)
  * Covenant committee signatures
* This function:
  * Combines covenant signatures with your signature
  * Creates a valid multi-signature transaction
  * Signs with your BTC private key
  * Produces a fully signed unbonding transaction

**3. Broadcast to Bitcoin Network**

* Broadcasts the signed unbonding transaction to Bitcoin network
* Transaction is confirmed on Bitcoin blockchain
* Starts the unbonding timelock period
* After unbonding timelock expires, funds become available for withdrawal

## Withdrawal Workflow

The withdrawal process lets you retrieve your BTC once the unbonding period has fully completed.

**1. Fetch Delegation Data**

* Connects to Babylon staking API using the staking transaction hash
* Retrieves delegation information including:
  * `stakerBtcPkHex`: Your Bitcoin public key
  * `stakingAmount`: Amount of BTC staked (in satoshis)
  * `stakingTimelock`: Original staking period
  * `finalityProviderBtcPksHex`: Finality provider public keys
  * `unbondingTxHex`: The unbonding transaction (must be already broadcast)
  * `paramsVersion`: Version of staking parameters used

**2. Create Signed Withdrawal Transaction**

* Uses `createSignedBtcWithdrawEarlyUnbondedTransaction()` from `@babylonlabs-io/btc-staking-ts` with:
  * Staker information (address and public key)
  * Staking input parameters (finality provider public keys, amount, timelock)
  * Parameters version
  * Unbonding transaction
  * Fee rate for the withdrawal transaction
* This function:
  * Creates a new transaction that spends the unbonding output
  * Calculates the output amount (unbonding amount minus fees)
  * Signs the transaction with your BTC private key
  * Sends funds back to your Bitcoin address
  * Produces a fully signed withdrawal transaction

**3. Broadcast to Bitcoin Network**

* Broadcasts the signed withdrawal transaction to Bitcoin network
