-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_mc.sh
executable file
·80 lines (66 loc) · 2.48 KB
/
import_mc.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
#!/bin/bash
# Function to display usage information.
function usage() {
echo "Usage: $0 <pairs> [macaroon_path] [tls_cert] [lnd_rest_host]"
echo
echo "Arguments:"
echo " pairs A list of pairs to import (required)"
echo
echo "Example:"
echo " $0 '[{\"node_from\":\"node_from_pub_key_hex\",\"node_to\":\"node_to_pubkey\",\"history\":{}}]'"
}
# Variables.
pairs=$1
macaroon_path="/root/.lnd/data/chain/bitcoin/regtest/admin.macaroon"
tls_cert="/root/.lnd/tls.cert"
lnd_rest_host="localhost:8080"
# Check if pairs is provided.
if [ -z "$1" ]; then
echo "Error: pairs is required."
usage
exit 1
fi
# Initialize an empty JSON array for modified pairs.
modified_pairs="[]"
# Loop over each pair to encode node_from and node_to.
echo "Original and encoded pairs:"
for pair in $pairs; do
echo "pair" "$pair"
node_from=$(echo "$pair" | jq -r ".node_from")
echo "node_from" $node_from
node_to=$(echo "$pair" | jq -r ".node_to")
echo "node_to" $node_to
history=$(echo "$pair" | jq ".history")
echo "node_to" $node_to
echo "history" "$history"
# # Convert hexadecimal to binary and then to Base64.
node_from_base64=$(echo -n $node_from | xxd -r -p | base64)
node_to_base64=$(echo -n $node_to | xxd -r -p | base64)
# Construct new JSON object with modified node_from and node_to.
new_pair=$(jq -n --arg nf "$node_from_base64" --arg nt "$node_to_base64" --argjson hist "$history" \
'{node_from: $nf, node_to: $nt, history: $hist}')
# # Add to the array of modified pairs.
modified_pairs=$(echo "$modified_pairs" | jq ". + [$new_pair]")
done
echo "macaroon_path" $macaroon_path
# # Convert the macaroon file to hexadecimal.
docker cp lnd:$macaroon_path /tmp/admin.macaroon
macaroon_hex=$(xxd -ps -u -c 1000 /tmp/admin.macaroon)
# Define the URL and the JSON data.
url="https://$lnd_rest_host/v2/router/x/importhistory"
data="{\"pairs\": $modified_pairs, \"force\": false}"
# Docker execution starts here for POST request.
response_code=$(docker exec lnd /bin/bash -c "
curl -s -o /dev/null -w \"%{http_code}\" -X POST \"$url\" \
--header \"Content-Type: application/json\" \
--header \"Grpc-Metadata-macaroon: $macaroon_hex\" \
--data '$data' \
--cacert \"$tls_cert\"
")
echo "RESPONSE CODE: $response_code"
# Check if the request was successful.
if [ "$response_code" -eq 200 ]; then
echo "Import successful"
else
echo "Import failed with response code: $response_code"
fi