Skip to content

Commit

Permalink
chore: add convenience cli for regtest docker setup
Browse files Browse the repository at this point in the history
  • Loading branch information
pwltr committed Jul 26, 2023
1 parent d9b3e13 commit b53a91b
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e-ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
run: HOMEBREW_NO_AUTO_UPDATE=1 brew tap wix/brew && brew install applesimutils

- name: Run regtest setup
run: cd __tests__ && mkdir lnd && chmod 777 lnd && docker-compose up -d
run: cd docker && mkdir lnd && chmod 777 lnd && docker-compose up -d

- name: Wait for bitcoind
timeout-minutes: 2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/jest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fetch-depth: 1

- name: Run regtest setup
run: cd __tests__ && docker compose up -d
run: cd docker && docker compose up -d

- name: Wait for bitcoind
run: |
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ android/app/pepk.jar

# tests
coverage/
__tests__/bitcoin_datadir/
__tests__/lnd/
artifacts/

# docker
docker/lnd/

#Build
android/app/release/
22 changes: 9 additions & 13 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ Make sure you have [setup your environment for React Native](https://reactnative
git clone [email protected]:synonymdev/bitkit.git && cd bitkit
```

2. Switch Node version

Switch to the Node.js version defined in `.nvmrc`. If `nvm` (or similiar) is installed on your system you can run `nvm use`.

3. Install dependencies
2. Install dependencies

```shell
yarn install
```

4. Setup iOS or Android dependencies
3. Setup iOS or Android dependencies

```shell
yarn setup-ios
Expand All @@ -32,7 +28,7 @@ or
yarn setup-android
```

5. Start the project
4. Start the project

On iOS Simulator:

Expand All @@ -50,18 +46,18 @@ yarn android

Bitkit uses two types of testing: unit and end-to-end (E2E) tests.

### 1. Unit tests

Before running unit tests, you need to install Docker and run bitcoind and the electrum server in regtest mode. You can do this by using the docker-compose.yml file from the **tests** directory:
Before running tests, you need to install [Docker](https://docs.docker.com/get-docker/) and run bitcoind and the electrum server in regtest mode. You can do this by using the `docker-compose.yml` file from the **docker** directory:

```sh
cd __tests__
```shell
cd docker
docker compose up
```

After that, you are ready to run the tests:

```sh
### 1. Unit tests

```shell
yarn test
```

Expand Down
165 changes: 165 additions & 0 deletions docker/bitcoin-cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#!/usr/bin/env bash

set -euo pipefail

CLI_NAME="$(basename $0)"
CLI_DIR="$(dirname "$(readlink -f "$0")")"
CONTAINER="bitcoind"
RPC_USER=polaruser
RPC_PASS=polarpass
RPC_PORT=43782

BASE_COMMAND=(docker compose exec $CONTAINER bitcoin-cli -rpcport=$RPC_PORT -rpcuser=$RPC_USER -rpcpassword=$RPC_PASS)

DEFAULT_AMOUNT=0.001

show_help() {
cat <<EOF
Shortcuts for bitcoin-cli.
Usage: ${CLI_NAME} <command> [options]
Flags:
-h, --help Show this help message
Commands:
fund <amount> Fund the wallet
mine <amount> [--auto] Generate a number of blocks
send <address> <amount> Send to address or BIP21 URI
getInvoice <amount> Get a new BIP21 URI with a bech32 address
EOF
}

if [ -z ${1+x} ]; then
command=""
else
command="$1"
fi

# Fund the wallet
if [[ "$command" = "fund" ]]; then
"${BASE_COMMAND[@]}" -generate 101
exit
fi

# Mine some blocks
if [[ "$command" = "mine" ]]; then
shift

if [ -z ${1+x} ]; then
echo "Specify the number of blocks to generate."
echo "Usage: \`$CLI_NAME $command <amount>\`"
exit 1
fi

POSITIONAL_ARGS=()

auto=false

while [[ $# -gt 0 ]]; do
case $1 in
-l | --auto)
auto=true
shift
;;
-* | --*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done

set -- "${POSITIONAL_ARGS[@]}"

# default to 5 seconds
interval=${2:-5}

if $auto; then
printf "Generating a block every $interval seconds. Press [CTRL+C] to stop...\n\n"

while true; do
"${BASE_COMMAND[@]}" -generate 1
sleep $interval
done
else
"${BASE_COMMAND[@]}" -generate "$@"
fi

exit
fi

# Send to a transaction
if [[ "$command" = "send" ]]; then
shift

if [ -z ${1+x} ]; then
read -p "Enter a BIP21 URI or address: " uri
echo
else
uri="$1"
fi

if [ -z ${2+x} ]; then
amount=$DEFAULT_AMOUNT
else
amount="$2"
fi

protocol=$(echo "${uri%%:*}")

if [[ "$protocol" == "bitcoin" ]]; then
# BIP21 URI
# Remove the protocol
url_no_protocol=$(echo "${uri/$protocol/}" | cut -d":" -f2-)

address=$(echo $url_no_protocol | grep "?" | cut -d"/" -f1 | rev | cut -d"?" -f2- | rev || echo $url_no_protocol)
uri_amount=$(echo $url_no_protocol | cut -d'?' -f 2 | cut -d'=' -f 2 | cut -d'&' -f 1)

if echo "$uri_amount" | grep -qE '^[0-9]*\.?[0-9]+$'; then
amount=$uri_amount
fi
else
address=$uri
fi

tx_id=$("${BASE_COMMAND[@]}" -named sendtoaddress address="$address" amount="$amount" fee_rate="25")

echo "Sent $amount BTC to $address"
echo "Transaction ID: $tx_id"

exit
fi

# Get a new BIP21 URI
if [[ "$command" = "getInvoice" ]]; then
shift

if [ -z ${1+x} ]; then
amount=$DEFAULT_AMOUNT
else
amount="$1"
fi

address=$("${BASE_COMMAND[@]}" getnewaddress -addresstype bech32 | tr -d '\r')
uri="bitcoin:$address?amount=$amount"

# print URI
echo $uri

# copy to clipboard (MacOS)
echo $uri | pbcopy
echo "Copied to clipboard."

exit
fi

# Show usage information for this CLI
if [[ "$command" = "--help" ]] || [[ "$command" = "-h" ]]; then
show_help
exit
fi

# If no command specified pass all args straight to bitcoin-cli
"${BASE_COMMAND[@]}" "$@"
exit
File renamed without changes.
3 changes: 3 additions & 0 deletions __tests__/docker-compose.yml → docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: '3'
services:
bitcoind:
container_name: bitcoin
image: btcpayserver/bitcoin:25.0
restart: unless-stopped
expose:
Expand Down Expand Up @@ -46,6 +47,7 @@ services:
]

electrs:
container_name: electrum
image: getumbrel/electrs:v0.9.10
restart: unless-stopped
depends_on:
Expand Down Expand Up @@ -84,6 +86,7 @@ services:
- '/www'

lnd:
container_name: lnd
image: polarlightning/lnd:0.16.2-beta
restart: unless-stopped
depends_on:
Expand Down
File renamed without changes.

0 comments on commit b53a91b

Please sign in to comment.