name: bitcoin-regtest
description: Spawns a regtest Bitcoin daemon

inputs:
  version:
    description: "Version to download and run"
    required: false
    default: 24.0.1

runs:
  using: "composite"
  steps:
    - name: Bitcoin Daemon Cache
      id: cache-bitcoind
      uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84
      with:
        path: bitcoin.tar.gz
        key: bitcoind-${{ runner.os }}-${{ runner.arch }}-${{ inputs.version }}

    - name: Download the Bitcoin Daemon
      if: steps.cache-bitcoind.outputs.cache-hit != 'true'
      shell: bash
      run: |
        RUNNER_OS=linux
        RUNNER_ARCH=x86_64
        FILE=bitcoin-${{ inputs.version }}-$RUNNER_ARCH-$RUNNER_OS-gnu.tar.gz

        wget https://bitcoincore.org/bin/bitcoin-core-${{ inputs.version }}/$FILE
        mv $FILE bitcoin.tar.gz

    - name: Extract the Bitcoin Daemon
      shell: bash
      run: |
        tar xzvf bitcoin.tar.gz
        cd bitcoin-${{ inputs.version }}
        sudo mv bin/* /bin && sudo mv lib/* /lib

    - name: Bitcoin Regtest Daemon
      shell: bash
      run: |
        RPC_USER=serai
        RPC_PASS=seraidex

        bitcoind -txindex -regtest \
          -rpcuser=$RPC_USER -rpcpassword=$RPC_PASS \
          -rpcbind=127.0.0.1 -rpcbind=$(hostname) -rpcallowip=0.0.0.0/0 \
          -daemon