Skip to content

Commit 06d4a22

Browse files
committed
Merge #469: lnd: Use lndinit for wallet creation
a4a5c72 lnd: use `lndinit` for wallet creation (Erik Arvstedt) e793a34 lndinit: init at 0.1.3-beta (Erik Arvstedt) Pull request description: ACKs for top commit: nixbitcoin: ACK a4a5c72 jonasnick: utACK a4a5c72 Tree-SHA512: 5295c1014a5b7bed4b7347b2d671fd877fd719323ea5156bd3bcff6ec94004949f22a9d0698fcc1de925855d36d81d9ea0148dbf0c079ab77e5437a9f53b2bb8
2 parents 26bba2c + a4a5c72 commit 06d4a22

File tree

4 files changed

+71
-30
lines changed

4 files changed

+71
-30
lines changed

modules/lnd.nix

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ let
126126
nbLib = config.nix-bitcoin.lib;
127127
secretsDir = config.nix-bitcoin.secretsDir;
128128
runAsUser = config.nix-bitcoin.runAsUserCmd;
129+
lndinit = "${config.nix-bitcoin.pkgs.lndinit}/bin/lndinit";
129130

130131
bitcoind = config.services.bitcoind;
131132

@@ -202,6 +203,21 @@ in {
202203
echo "externalip=$(${cfg.getPublicAddressCmd})"
203204
''}
204205
} >> '${cfg.dataDir}/lnd.conf'
206+
207+
if [[ ! -f ${networkDir}/wallet.db ]]; then
208+
mnemonic='${cfg.dataDir}/lnd-seed-mnemonic'
209+
210+
if [[ ! -f "$mnemonic" ]]; then
211+
echo "Create lnd seed"
212+
(umask u=r,go=; ${lndinit} gen-seed > "$mnemonic")
213+
fi
214+
215+
echo "Create lnd wallet"
216+
${lndinit} -v init-wallet \
217+
--file.seed="$mnemonic" \
218+
--file.wallet-password='${secretsDir}/lnd-wallet-password' \
219+
--init-file.output-wallet-dir='${cfg.networkDir}'
220+
fi
205221
'';
206222
serviceConfig = nbLib.defaultHardening // {
207223
Type = "notify";
@@ -210,8 +226,7 @@ in {
210226
ExecStart = ''
211227
${cfg.package}/bin/lnd \
212228
--configfile="${cfg.dataDir}/lnd.conf" \
213-
--wallet-unlock-password-file="${secretsDir}/lnd-wallet-password" \
214-
--wallet-unlock-allow-create
229+
--wallet-unlock-password-file="${secretsDir}/lnd-wallet-password"
215230
'';
216231
User = cfg.user;
217232
TimeoutSec = "15min";
@@ -221,33 +236,9 @@ in {
221236
ExecStartPost = let
222237
curl = "${pkgs.curl}/bin/curl -s --show-error --cacert ${cfg.certPath}";
223238
restUrl = "https://${nbLib.addressWithPort cfg.restAddress cfg.restPort}/v1";
224-
in [
225-
(nbLib.script "lnd-create-wallet" ''
226-
if [[ ! -f ${networkDir}/wallet.db ]]; then
227-
mnemonic="${cfg.dataDir}/lnd-seed-mnemonic"
228-
if [[ ! -f "$mnemonic" ]]; then
229-
echo "Create lnd seed"
230-
umask u=r,go=
231-
${curl} -X GET ${restUrl}/genseed | ${pkgs.jq}/bin/jq -c '.cipher_seed_mnemonic' > "$mnemonic"
232-
fi
233-
234-
echo "Create lnd wallet"
235-
${curl} --output /dev/null \
236-
-X POST -d "{\"wallet_password\": \"$(cat ${secretsDir}/lnd-wallet-password | tr -d '\n' | base64 -w0)\", \
237-
\"cipher_seed_mnemonic\": $(cat "$mnemonic" | tr -d '\n')}" \
238-
${restUrl}/initwallet
239-
240-
echo "Wait until wallet is created"
241-
getStatus() {
242-
/run/current-system/systemd/bin/systemctl show -p StatusText lnd | cut -f 2 -d=
243-
}
244-
while [[ $(getStatus) == "Wallet locked" ]]; do
245-
sleep 0.1
246-
done
247-
fi
248-
'')
239+
in
249240
# Setting macaroon permissions for other users needs root permissions
250-
(nbLib.rootScript "lnd-create-macaroons" ''
241+
nbLib.rootScript "lnd-create-macaroons" ''
251242
umask ug=r,o=
252243
${lib.concatMapStrings (macaroon: ''
253244
echo "Create custom macaroon ${macaroon}"
@@ -260,8 +251,7 @@ in {
260251
${pkgs.jq}/bin/jq -c '.macaroon' | ${pkgs.xxd}/bin/xxd -p -r > "$macaroonPath"
261252
chown ${cfg.macaroons.${macaroon}.user}: "$macaroonPath"
262253
'') (attrNames cfg.macaroons)}
263-
'')
264-
];
254+
'';
265255
} // nbLib.allowedIPAddresses cfg.tor.enforce;
266256
};
267257

pkgs/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ let self = {
1010
clboss = pkgs.callPackage ./clboss { };
1111
clightning-plugins = pkgs.recurseIntoAttrs (import ./clightning-plugins pkgs self.nbPython3Packages);
1212
joinmarket = pkgs.callPackage ./joinmarket { inherit (self) nbPython3Packages; };
13+
lndinit = pkgs.callPackage ./lndinit { };
1314
liquid-swap = pkgs.python3Packages.callPackage ./liquid-swap { };
1415
rtl = pkgs.callPackage ./rtl { };
1516
# The secp256k1 version used by joinmarket

pkgs/lndinit/default.nix

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{ lib, buildGoModule, fetchFromGitHub }:
2+
3+
buildGoModule rec {
4+
pname = "lndinit";
5+
version = "0.1.3-beta";
6+
7+
src = fetchFromGitHub {
8+
owner = "lightninglabs";
9+
repo = pname;
10+
rev = "v${version}";
11+
sha256 = "sha256-sO1DpbppCurxr9g9nUl9Vx82FJK1mTcUw3rY1Fm1wEU=";
12+
};
13+
14+
vendorSha256 = "sha256-xdxxixSabcuGzwCctHrP/RV/Z8sCQDmk2PU4j1u8MX8=";
15+
16+
subPackages = [ "." ];
17+
18+
meta = with lib; {
19+
description = "Wallet initializer utility for lnd";
20+
homepage = "https://github.com/lightninglabs/lndinit";
21+
license = licenses.mit;
22+
maintainers = with maintainers; [ earvstedt ];
23+
};
24+
}

pkgs/lndinit/get-sha256.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#! /usr/bin/env nix-shell
2+
#! nix-shell -i bash -p git gnupg curl jq
3+
set -euo pipefail
4+
5+
TMPDIR="$(mktemp -d -p /tmp)"
6+
trap "rm -rf $TMPDIR" EXIT
7+
cd $TMPDIR
8+
9+
echo "Fetching latest release"
10+
repo=lightninglabs/lndinit
11+
latest=$(curl -s --show-error https://api.github.com/repos/$repo/releases/latest | jq -r .tag_name)
12+
echo "Latest release is $latest"
13+
git clone --depth 1 --branch $latest https://github.com/lightninglabs/lndinit 2>/dev/null
14+
cd lndinit
15+
16+
# GPG verification
17+
export GNUPGHOME=$TMPDIR
18+
echo "Fetching Oliver Gugger's key"
19+
gpg --keyserver hkps://keyserver.ubuntu.com --recv-keys F4FC70F07310028424EFC20A8E4256593F177720 2> /dev/null
20+
echo "Verifying latest release"
21+
git verify-tag $latest
22+
23+
echo "tag: $latest"
24+
git checkout -q tags/$latest
25+
rm -rf .git
26+
nix hash path .

0 commit comments

Comments
 (0)