-
Notifications
You must be signed in to change notification settings - Fork 8
/
entrypoint.sh
executable file
·107 lines (95 loc) · 2.79 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/sh
EXISTING_PUBKEY=""
GETINFO_RESPONSE=""
LIGHTNING_RPC=$CORE_LIGHTNING_PATH"/$APP_CORE_LIGHTNING_BITCOIN_NETWORK""/lightning-rpc"
getinfo_request() {
cat <<EOF
{
"jsonrpc": "2.0",
"id": 1,
"method": "getinfo",
"params": []
}
EOF
}
createrune_request() {
cat <<EOF
{
"jsonrpc": "2.0",
"id": 2,
"method": "createrune",
"params": [null, [["For Application#"]]]
}
EOF
}
commando_datastore_request() {
cat <<EOF
{
"jsonrpc": "2.0",
"id": 3,
"method": "datastore",
"params": [["commando", "runes", "$UNIQUE_ID"], "$RUNE"]
}
EOF
}
generate_new_rune() {
COUNTER=0
RUNE=""
while { [ "$RUNE" = "" ] || [ "$RUNE" = "null" ]; } && [ $COUNTER -lt 10 ]; do
# Send 'createrune' request
echo "Generating rune attempt: $COUNTER"
COUNTER=$((COUNTER+1))
RUNE_RESPONSE=$( (echo "$(createrune_request)"; sleep 2) | socat - UNIX-CONNECT:"$LIGHTNING_RPC")
RUNE=$(echo "$RUNE_RESPONSE" | jq -r '.result.rune')
UNIQUE_ID=$(echo "$RUNE_RESPONSE" | jq -r '.result.unique_id')
echo "RUNE_RESPONSE"
echo "$RUNE_RESPONSE"
echo "RUNE"
echo "$RUNE"
if [ "$RUNE" != "" ] && [ "$RUNE" != "null" ]; then
# Save rune in env file
echo "LIGHTNING_RUNE=\"$RUNE\"" >> "$COMMANDO_CONFIG"
fi
if [ "$UNIQUE_ID" != "" ] && [ "$UNIQUE_ID" != "null" ]; then
# This will fail for v>23.05
DATASTORE_RESPONSE=$( (echo "$(commando_datastore_request)"; sleep 1) | socat - UNIX-CONNECT:"$LIGHTNING_RPC") > /dev/null
fi
done
if [ $COUNTER -eq 10 ] && [ "$RUNE" = "" ]; then
echo "Error: Unable to generate rune for application authentication!"
fi
}
# Read existing pubkey
if [ -f "$COMMANDO_CONFIG" ]; then
EXISTING_PUBKEY=$(head -n1 "$COMMANDO_CONFIG")
EXISTING_RUNE=$(sed -n "2p" "$COMMANDO_CONFIG")
echo "EXISTING_PUBKEY"
echo "$EXISTING_PUBKEY"
echo "EXISTING_RUNE"
echo "$EXISTING_RUNE"
fi
# Getinfo from CLN
until [ "$GETINFO_RESPONSE" != "" ]
do
echo "Waiting for lightningd"
# Send 'getinfo' request
GETINFO_RESPONSE=$( (echo "$(getinfo_request)"; sleep 1) | socat - UNIX-CONNECT:"$LIGHTNING_RPC")
echo "$GETINFO_RESPONSE"
done
# Write 'id' from the response as pubkey
LIGHTNING_PUBKEY="$(jq -n "$GETINFO_RESPONSE" | jq -r '.result.id')"
echo "$LIGHTNING_PUBKEY"
# Compare existing pubkey with current
if [ "$EXISTING_PUBKEY" != "LIGHTNING_PUBKEY=\"$LIGHTNING_PUBKEY\"" ] ||
[ "$EXISTING_RUNE" = "" ] ||
[ "$EXISTING_RUNE" = "LIGHTNING_RUNE=\"\"" ] ||
[ "$EXISTING_RUNE" = "LIGHTNING_RUNE=\"null\"" ]; then
# Pubkey changed or missing rune; rewrite new data on the file.
echo "Pubkey mismatched or missing rune; Rewriting the data."
cat /dev/null > "$COMMANDO_CONFIG"
echo "LIGHTNING_PUBKEY=\"$LIGHTNING_PUBKEY\"" >> "$COMMANDO_CONFIG"
generate_new_rune
else
echo "Pubkey matches with existing pubkey."
fi
exec "$@"