Skip to content

Commit

Permalink
chore: use the generic wallet name
Browse files Browse the repository at this point in the history
  • Loading branch information
lesterli committed Oct 20, 2024
1 parent df5c4b6 commit d3854d9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
8 changes: 4 additions & 4 deletions .env.bitcoin.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ NETWORK=regtest # regtest | signet
RPC_PORT=18443 # 18443 (regtest) | 38332 (signet)
RPC_USER=rpcuser
RPC_PASS=rpcpass
BTCSTAKER_WALLET_NAME=btcstaker
BTCSTAKER_WALLET_PASS=walletpass
# btcstaker private key in WIF format
BTCSTAKER_PRIVKEY=
BTC_WALLET_NAME=btcwallet
BTC_WALLET_PASS=walletpass
# btc private key in WIF format
BTC_PRIVKEY=
ZMQ_SEQUENCE_PORT=29000
ZMQ_RAWBLOCK_PORT=29001
ZMQ_RAWTR_PORT=29002
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
```

* The `NETWORK` variable only can be either `regtest` or `signet`.
* The `BTCSTAKER_PRIVKEY` variable must be a valid Bitcoin private key in WIF format.
* The `BTC_PRIVKEY` variable must be a valid Bitcoin private key in WIF format.

2. Start the Bitcoin node

Expand Down Expand Up @@ -37,28 +37,28 @@

## Troubleshooting

1. BTC staker balance null or no unspent outputs
1. BTC wallet balance null or no unspent outputs

After running `verify-bitcoin-sync-balance.sh`, the BTC staker wallet should be loaded to bitcoind. If not, you will run into null balance or no unspent outputs errors when running `create-btc-delegations.sh`.
After running `verify-bitcoin-sync-balance.sh`, the BTC wallet should be loaded to bitcoind. If not, you will run into null balance or no unspent outputs errors when running `create-btc-delegations.sh`.

To check the wallet balance:

```
docker exec bitcoind /bin/sh -c "bitcoin-cli -signet -rpcuser=rpcuser -rpcpassword=rpcpass -rpcwallet=btcstaker listunspent"
docker exec bitcoind /bin/sh -c "bitcoin-cli -signet -rpcuser=rpcuser -rpcpassword=rpcpass -rpcwallet=btcwallet listunspent"
```
To check unspent outputs:
```
docker exec bitcoind /bin/sh -c "bitcoin-cli -signet -rpcuser=rpcuser -rpcpassword=rpcpass -rpcwallet=btcstaker getbalance"
docker exec bitcoind /bin/sh -c "bitcoin-cli -signet -rpcuser=rpcuser -rpcpassword=rpcpass -rpcwallet=btcwallet getbalance"
```
If your wallet balance is 0 or you have no unspent outputs, you may need to re-load the wallet:
```
docker exec bitcoind /bin/sh -c "bitcoin-cli -signet -rpcuser=rpcuser -rpcpassword=rpcpass -rpcwallet=btcstaker unloadwallet btcstaker"
docker exec bitcoind /bin/sh -c "bitcoin-cli -signet -rpcuser=rpcuser -rpcpassword=rpcpass -rpcwallet=btcwallet unloadwallet btcwallet"

docker exec bitcoind /bin/sh -c "bitcoin-cli -signet -rpcuser=rpcuser -rpcpassword=rpcpass -rpcwallet=btcstaker loadwallet btcstaker"
docker exec bitcoind /bin/sh -c "bitcoin-cli -signet -rpcuser=rpcuser -rpcpassword=rpcpass -rpcwallet=btcwallet loadwallet btcwallet"
```
Now recheck the balance and unspent outputs.
2 changes: 1 addition & 1 deletion docker/docker-compose-bitcoin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
bitcoind:
image: snapchain/bitcoind:1aa7b4b
image: snapchain/bitcoind:latest
container_name: bitcoind
env_file:
- "${PWD}/.env.bitcoin"
Expand Down
28 changes: 14 additions & 14 deletions scripts/bitcoin/setup-wallet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@
set -euo pipefail

echo "NETWORK: $NETWORK"
echo "BTCSTAKER_WALLET_NAME: $BTCSTAKER_WALLET_NAME"
echo "BTC_WALLET_NAME: $BTC_WALLET_NAME"

DATA_DIR=/bitcoind/.bitcoin

if [[ ! -d "${DATA_DIR}/${NETWORK}/wallets/${BTCSTAKER_WALLET_NAME}" ]]; then
echo "Creating a wallet for btcstaker..."
bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" createwallet "$BTCSTAKER_WALLET_NAME" false false "$BTCSTAKER_WALLET_PASS" false false
if [[ ! -d "${DATA_DIR}/${NETWORK}/wallets/${BTC_WALLET_NAME}" ]]; then
echo "Creating a wallet ${BTC_WALLET_NAME}..."
bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" createwallet "$BTC_WALLET_NAME" false false "$BTC_WALLET_PASS" false false
fi

echo "Opening ${BTCSTAKER_WALLET_NAME} wallet..."
bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" -rpcwallet="$BTCSTAKER_WALLET_NAME" walletpassphrase "$BTCSTAKER_WALLET_PASS" 10
echo "Importing the private key to the wallet ${BTCSTAKER_WALLET_NAME} with the label ${BTCSTAKER_WALLET_NAME} without rescan..."
bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" -rpcwallet="$BTCSTAKER_WALLET_NAME" importprivkey "$BTCSTAKER_PRIVKEY" "${BTCSTAKER_WALLET_NAME}" false
echo "Opening wallet ${BTC_WALLET_NAME}..."
bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" -rpcwallet="$BTC_WALLET_NAME" walletpassphrase "$BTC_WALLET_PASS" 10
echo "Importing the private key to the wallet ${BTC_WALLET_NAME} with the label ${BTC_WALLET_NAME} without rescan..."
bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" -rpcwallet="$BTC_WALLET_NAME" importprivkey "$BTC_PRIVKEY" "${BTC_WALLET_NAME}" false

if [[ "$NETWORK" == "regtest" ]]; then
echo "Generating 110 blocks for the first coinbases to mature..."
bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" -rpcwallet="$BTCSTAKER_WALLET_NAME" -generate 110
bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" -rpcwallet="$BTC_WALLET_NAME" -generate 110

# Waiting for the wallet to catch up.
sleep 5
echo "Checking balance..."
bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" -rpcwallet="$BTCSTAKER_WALLET_NAME" getbalance
bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" -rpcwallet="$BTC_WALLET_NAME" getbalance

echo "Getting the imported BTC address for wallet ${BTCSTAKER_WALLET_NAME}..."
BTCSTAKER_ADDR=$(bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" -rpcwallet="$BTCSTAKER_WALLET_NAME" getaddressesbylabel "${BTCSTAKER_WALLET_NAME}" | jq -r 'keys[0]')
echo "Imported BTC address: ${BTCSTAKER_ADDR}"
echo "Getting the imported BTC address for wallet ${BTC_WALLET_NAME}..."
BTC_ADDR=$(bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" -rpcwallet="$BTC_WALLET_NAME" getaddressesbylabel "${BTC_WALLET_NAME}" | jq -r 'keys[0]')
echo "Imported BTC address: ${BTC_ADDR}"

if [[ -z "$GENERATE_INTERVAL_SECS" ]]; then
GENERATE_INTERVAL_SECS=600 # 10 minutes
Expand All @@ -37,7 +37,7 @@ if [[ "$NETWORK" == "regtest" ]]; then
echo "Starting block generation every $GENERATE_INTERVAL_SECS seconds in the background..."
(
while true; do
bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" -rpcwallet="$BTCSTAKER_WALLET_NAME" -generate 1
bitcoin-cli -${NETWORK} -rpcuser="$RPC_USER" -rpcpassword="$RPC_PASS" -rpcwallet="$BTC_WALLET_NAME" -generate 1
sleep "$GENERATE_INTERVAL_SECS"
done
) &
Expand Down
22 changes: 11 additions & 11 deletions scripts/bitcoin/verify-sync-balance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ set -a
source $(pwd)/.env.bitcoin
set +a

if [ -z "$(echo ${BTCSTAKER_WALLET_PASS})" ] || [ -z "$(echo ${BTCSTAKER_PRIVKEY})" ]; then
echo "Error: BTCSTAKER_WALLET_PASS or BTCSTAKER_PRIVKEY environment variable is not set"
if [ -z "$(echo ${BTC_WALLET_PASS})" ] || [ -z "$(echo ${BTC_PRIVKEY})" ]; then
echo "Error: BTC_WALLET_PASS or BTC_PRIVKEY environment variable is not set"
exit 1
fi
echo "Environment variables loaded successfully"
Expand All @@ -29,28 +29,28 @@ fi
echo "Bitcoin node is synced"
echo

# Check btcstaker address
BTCSTAKER_ADDRESS=$(docker exec bitcoind /bin/sh -c "
# Check btc address
BTC_ADDRESS=$(docker exec bitcoind /bin/sh -c "
bitcoin-cli \
-${NETWORK} \
-rpcuser=${RPC_USER} \
-rpcpassword=${RPC_PASS} \
-rpcwallet=${BTCSTAKER_WALLET_NAME} \
getaddressesbylabel \"${BTCSTAKER_WALLET_NAME}\"" \
-rpcwallet=${BTC_WALLET_NAME} \
getaddressesbylabel \"${BTC_WALLET_NAME}\"" \
| jq -r 'keys[0]')
echo "BTCStaker address: ${BTCSTAKER_ADDRESS}"
echo "BTC address: ${BTC_ADDRESS}"

# Check if btcstaker has any unspent transactions
# Check if btc has any unspent transactions
BALANCE_BTC=$(docker exec bitcoind /bin/sh -c "
bitcoin-cli \
-${NETWORK} \
-rpcuser=${RPC_USER} \
-rpcpassword=${RPC_PASS} \
-rpcwallet=${BTCSTAKER_WALLET_NAME} \
-rpcwallet=${BTC_WALLET_NAME} \
listunspent" | jq -r '[.[] | .amount] | add')
if (( $(awk -v balance="$BALANCE_BTC" 'BEGIN {print (balance < 0.01)}') )); then
echo "Warning: BTCStaker balance is less than 0.01 BTC. You may need to fund this address for ${NETWORK}."
echo "Warning: BTC balance is less than 0.01 BTC. You may need to fund this address for ${NETWORK}."
else
echo "BTCStaker balance is sufficient: ${BALANCE_BTC} BTC"
echo "BTC balance is sufficient: ${BALANCE_BTC} BTC"
fi
echo

0 comments on commit d3854d9

Please sign in to comment.