2019-04-14 19:55:40 +03:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.recurring-donations;
|
2019-11-27 16:04:23 +03:00
|
|
|
inherit (config) nix-bitcoin-services;
|
2019-04-14 19:55:40 +03:00
|
|
|
recurring-donations-script = pkgs.writeScript "recurring-donations.sh" ''
|
2019-11-27 16:04:33 +03:00
|
|
|
LNCLI="${pkgs.nix-bitcoin.clightning}/bin/lightning-cli --lightning-dir=${config.services.clightning.dataDir}"
|
2019-04-14 19:55:40 +03:00
|
|
|
pay_tallycoin() {
|
|
|
|
NAME=$1
|
|
|
|
AMOUNT=$2
|
|
|
|
echo Attempting to pay $AMOUNT sat to $NAME
|
|
|
|
INVOICE=$(torsocks curl -d "satoshi_amount=$AMOUNT&payment_method=ln&id=$NAME&type=profile" -X POST https://api.tallyco.in/v1/payment/request/ | jq -r '.lightning_pay_request') 2> /dev/null
|
|
|
|
if [ -z "$INVOICE" ] || [ "$INVOICE" = "null" ]; then
|
|
|
|
echo "ERROR: did not get invoice from tallycoin"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
# Decode invoice and compare amount with requested amount
|
2019-04-22 03:37:45 +03:00
|
|
|
DECODED_AMOUNT=$($LNCLI decodepay "$INVOICE" | jq -r '.amount_msat' | head -c -8)
|
2019-04-14 19:55:40 +03:00
|
|
|
if [ -z "$DECODED_AMOUNT" ] || [ "$DECODED_AMOUNT" = "null" ]; then
|
|
|
|
echo "ERROR: did not get response from clightning"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
if [ $DECODED_AMOUNT -eq $AMOUNT ]; then
|
|
|
|
echo Paying with invoice "$INVOICE"
|
|
|
|
$LNCLI pay "$INVOICE"
|
|
|
|
else
|
|
|
|
echo ERROR: requested amount and invoice amount do not match. $AMOUNT vs $DECODED_AMOUNT
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
${ builtins.foldl'
|
|
|
|
(x: receiver: x +
|
|
|
|
''
|
|
|
|
pay_tallycoin ${receiver} ${toString (builtins.getAttr receiver cfg.tallycoin)}
|
|
|
|
'')
|
|
|
|
""
|
|
|
|
(builtins.attrNames cfg.tallycoin)
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
in {
|
|
|
|
options.services.recurring-donations = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
If enabled, the recurring-donations service will be installed.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
tallycoin = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
This option is used to specify tallycoin donation receivers using an
|
|
|
|
attribute set. For example the following setting instructs the module
|
|
|
|
to repeatedly send 1000 satoshis to djbooth007.
|
|
|
|
{
|
|
|
|
"djbooth007" = 1000;
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
interval = mkOption {
|
2019-10-15 10:37:32 +03:00
|
|
|
type = types.str;
|
2019-04-14 19:55:40 +03:00
|
|
|
default = "Mon *-*-* 00:00:00";
|
|
|
|
description = ''
|
|
|
|
Schedules the donations. Default is weekly on Mon 00:00:00. See `man
|
|
|
|
systemd.time` for further options.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
randomizedDelaySec = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 86400;
|
|
|
|
description = ''
|
|
|
|
Random delay to add to scheduled time for donation. Default is one day.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.recurring-donations = {
|
|
|
|
description = "Run recurring-donations";
|
|
|
|
requires = [ "clightning.service" ];
|
|
|
|
after = [ "clightning.service" ];
|
2019-11-27 16:04:21 +03:00
|
|
|
path = with pkgs; [ nix-bitcoin.clightning curl torsocks sudo jq ];
|
2019-04-14 19:55:40 +03:00
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = "${pkgs.bash}/bin/bash ${recurring-donations-script}";
|
|
|
|
# TODO: would be better if this was operator, but I don't get sudo
|
|
|
|
# working inside the shell script
|
|
|
|
User = "clightning";
|
|
|
|
Type = "oneshot";
|
2019-04-28 02:53:26 +03:00
|
|
|
} // nix-bitcoin-services.defaultHardening
|
|
|
|
// nix-bitcoin-services.allowTor;
|
2019-04-14 19:55:40 +03:00
|
|
|
};
|
|
|
|
systemd.timers.recurring-donations = {
|
|
|
|
requires = [ "clightning.service" ];
|
|
|
|
after = [ "clightning.service" ];
|
|
|
|
timerConfig = {
|
|
|
|
Unit = "recurring-donations.service";
|
|
|
|
OnCalendar = cfg.interval;
|
|
|
|
RandomizedDelaySec = toString cfg.randomizedDelaySec;
|
|
|
|
};
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|