# 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


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.b14g.xyz/user-guide/for-btc-holders/guide-btc-staking-babylon/unbonding-via-cli.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
