Peach2Peach / groupHug

A batching service that does not require coordination
MIT License
8 stars 1 forks source link

GroupHug Server

Batching server that allows combining PSBT into a single batched transaction.

The PSBTs have to be payouts in full (ie no change). Otherwise, the change output can be stolen. In other words, only PSBTs with 1 input and 1 output are accepted. The PSBT inputs have to be signed with SINGLE|ANYONECANPAY sig hash.

The batching server collects all PSBTs and attemps a batch after at least 24 hours. Then all PSBTs are combined, an extra fee output is added and each input is signed by the server with the default ALL sig hash.

The additional output is the 2% service fee. A batch happens, when this service fee meets a mininmum amount threshold, but at most after 1 week. The mining fee used by the batching server is the halfHourFee and is paid using the diff between input and output + service fee. PSBTs are sorted by their density serviceFees * feeRate and then are attempted to be batched in descending order. If a PSBT would cause the bucket to drop below the minimum mining fee rate, it is skipped. This process is repeated until no more PSBTs can be added to the batch.

PSBTs can always be revoked by the user, using the revocation token.

Prerequisites

redis

Follow the redis installation guide until set authentication via requirepass

Installation

npm install

Copy .env.dist to .env Fill in the missing values

environment variables in .env

The following are used to connect to the database

Multisig related variables

The following are used to set the loglevels of various log categories (see code for which categories exist)

The following are used for regtest

Multisig

Some transactions require the batching service to co-sign transactions. For this we need to keep private keys on the server. It's strongly recommend to use a server password to encrypt the master private key.

There are 2 configurations for the escrow:

Starting development

npm run start:dev

Testing

Run unit tests

npm run test

Run integration tests

Run regtest-server docker run -d -p 8082:8080 junderw/bitcoinjs-regtest-server

Now you can run the integration tests npm run integration

Running bitcoin regtest + electrs

Install and run Bitcoin Core

  1. Get and install latest bitcoin core release from https://bitcoincore.org/en/download/

⚠️ Verify bitcoin core before installation, follow the verification guide on the download page.

  1. configure bitcoin.conf to reflect your environment You can find an example file for regtest here.

  2. Setup bitcoind as a service to ensure that bitcoin core is running automatically even after restarts

sudo nano /etc/systemd/system/bitcoin.service

and copy the contents you can find here and replace user and path to bitcoind and the .bitcoin folder as required

  1. start and enable the service sudo systemctl start bitcoin.service

sudo systemctl enable bitcoin.service

Install and run electrs

  1. Get and install blockstream electrs
git clone https://github.com/blockstream/electrs && cd electrs
git checkout new-index
cargo install --bin electrs --path .
sudo mv target/release/electrs /usr/local/bin
  1. Setup electrs as a service to ensure that electrs is running automatically even after restarts

sudo nano /etc/systemd/system/electrs.service

and copy the contents you can find here and replace user and path to .bitcoin folder as required

  1. start and enable the service

sudo systemctl start electrs.service

sudo systemctl enable electrs.service

Run server

  1. build server

npm run build

  1. Copy .env.dist to dist/.env and add the missing values

  2. Setup GroupHug as a service to ensure that it is running automatically even after restarts

sudo nano /etc/systemd/system/group-hug.service

and copy the contents you can find here and replace user and path to dist/server.js folder as required

  1. start and enable the service

sudo systemctl start group-hug.service

sudo systemctl enable group-hug.service

Run server via nginx

  1. Make sure you have nginx running

  2. sudo cp prefs/nginx/group-hug.conf /etc/nginx/conf.d/

  3. sudo nginx -s reload

Debugging

See logs of

bitcoin

sudo journalctl -u bitcoin.service -f

Electrs

sudo journalctl -u electrs.service -f

Troubleshooting electrs

If you are running on regtest you might need to kickstart the process. This can be simply done by generating a few blocks

$ bitcoin-cli getnewaddress
bcrt1qndj6vyt9dqcj8y9c0mfsyg759pv4set4rfanjr

$ bitcoin-cli generatetoaddress 1 bcrt1qndj6vyt9dqcj8y9c0mfsyg759pv4set4rfanjr
[
  "3608f15fce28d244441ff389db49638a2d5e17358f3e3e37518e662dc31e0d9e"
]