diff --git a/.github/workflows/e2e-ios.yml b/.github/workflows/e2e-ios.yml index 8c42c6bc1..31faaa782 100644 --- a/.github/workflows/e2e-ios.yml +++ b/.github/workflows/e2e-ios.yml @@ -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 diff --git a/.github/workflows/jest.yml b/.github/workflows/jest.yml index d4698e62c..d7b23094f 100644 --- a/.github/workflows/jest.yml +++ b/.github/workflows/jest.yml @@ -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: | diff --git a/.gitignore b/.gitignore index d31e95c29..fef3347ef 100644 --- a/.gitignore +++ b/.gitignore @@ -90,9 +90,10 @@ android/app/pepk.jar # tests coverage/ -__tests__/bitcoin_datadir/ -__tests__/lnd/ artifacts/ +# docker +docker/lnd/ + #Build android/app/release/ diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 6d7f5df98..bf2e16895 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -10,17 +10,13 @@ Make sure you have [setup your environment for React Native](https://reactnative git clone git@github.com: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 @@ -32,7 +28,7 @@ or yarn setup-android ``` -5. Start the project +4. Start the project On iOS Simulator: @@ -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 ``` diff --git a/docker/bitcoin-cli b/docker/bitcoin-cli new file mode 100755 index 000000000..c68fa4953 --- /dev/null +++ b/docker/bitcoin-cli @@ -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 < [options] +Flags: + -h, --help Show this help message +Commands: + fund Fund the wallet + mine [--auto] Generate a number of blocks + send
Send to address or BIP21 URI + getInvoice 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 \`" + 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 diff --git a/__tests__/btc-fee-estimates.json b/docker/btc-fee-estimates.json similarity index 100% rename from __tests__/btc-fee-estimates.json rename to docker/btc-fee-estimates.json diff --git a/__tests__/docker-compose.yml b/docker/docker-compose.yml similarity index 97% rename from __tests__/docker-compose.yml rename to docker/docker-compose.yml index 29fae9a8d..4a50e18d7 100644 --- a/__tests__/docker-compose.yml +++ b/docker/docker-compose.yml @@ -1,6 +1,7 @@ version: '3' services: bitcoind: + container_name: bitcoin image: btcpayserver/bitcoin:25.0 restart: unless-stopped expose: @@ -46,6 +47,7 @@ services: ] electrs: + container_name: electrum image: getumbrel/electrs:v0.9.10 restart: unless-stopped depends_on: @@ -84,6 +86,7 @@ services: - '/www' lnd: + container_name: lnd image: polarlightning/lnd:0.16.2-beta restart: unless-stopped depends_on: diff --git a/__tests__/electrs.toml b/docker/electrs.toml similarity index 100% rename from __tests__/electrs.toml rename to docker/electrs.toml