aboutsummaryrefslogtreecommitdiff
path: root/bin/ak-data-expand
blob: 17999570a0bd1cc0cfc02fda53c7176d13e328e4 (plain)
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
#!/bin/bash
PROGRAM="$(basename $0)"
logit(){
	ak-logthis "<$PROGRAM>" "$1" "$2"
}
if [ ! -z "$1" ] && [ ! -z "$2" ]
then
	echo -n "$1" | grep -e 'Qm.\{44\}' >/dev/null
	if [ "$?" -ne 0 ]
	then
		logit "[ERROR]" "Invalid hash format for $1"
		exit 1
	fi
	ipfs --timeout=10s cat $1 > /dev/null
	if [ "$?" -ne 0 ]
	then
		logit "[ERROR]" "Error while reading $1"
		exit 1
	fi
	ipfs --timeout=10s cat $1 | json_pp > /dev/null
	if [ "$?" -ne 0 ]
	then
		logit "[ERROR]" "Error while parsing JSON for $1"
		exit 1
	fi
	ipfs --timeout=10s cat $1 | jq | grep ipfs > /dev/null
	if [ "$?" -ne 0 ]
	then
		logit "[ERROR]" "Error while extracting data from JSON for $1"
		exit 1
	fi
	DATA="$(ipfs --timeout=10s cat $1 | jq | grep ipfs | sed -e 's/"ipfs": "//g; s/[",]//g; s/ //g')"
	if [ "$?" -ne 0 ]
	then
		logit "[ERROR]" "Error while extracting data from JSON for $1"
		exit 1
	fi
	ipfs --timeout=10s cat $1 | jq | grep detach > /dev/null
	if [ "$?" -ne 0 ]
	then
		logit "[ERROR]" "Error while extracting data from JSON for $1"
		exit 1
	fi
	DETACH="$(ipfs --timeout=10s cat $1 | jq | grep detach | sed -e 's/"detach": "//g; s/[",]//g; s/ //g')"
	if [ "$?" -ne 0 ]
	then
		logit "[ERROR]" "Error while extracting data from JSON for $1"
		exit 1
	fi
	echo -n "$2" | grep -e 'Qm.\{44\}' >/dev/null
	if [ "$?" -ne 0 ]
	then
	    logit "[ERROR]" "Invalid hash format for $2"
	    exit 1
	fi
    gpg="$2"
    ipfs --timeout=10s get $gpg > /dev/null 2>&1
    if [ "$?" -ne 0 ]
    then
	logit "[ERROR]" "Could not get GPG key: $gpg"
	exit 1
    fi
    gpg2 --import $gpg > /dev/null 2>&1
    if [ "$?" -ne 0 ]
    then
		logit "[ERROR]" "Could not import GPG key: $gpg"
		exit 1
	fi
	ipfs --timeout=10s get $DETACH > /dev/null 2>&1
	if [ "$?" -ne 0 ]
	then
		logit "[ERROR]" "Error while getting signature: $DETACH for data: $DATA"
		exit 1
	fi
	mv $DETACH $DATA.asc
	logit "[INFO]" "Block signature downloaded"
	ipfs --timeout=10s get $DATA > /dev/null 2>&1
	if [ "$?" -ne 0 ]
	then
		logit "[ERROR]" "Error while getting data: $DATA"
		exit 1
	fi
	logit "[INFO]" "Data downloaded: $DATA"
	gpg2 --verify $DATA.asc $DATA > /dev/null 2>&1
	if [ "$?" -ne 0 ]
	then
		logit "[ERROR]" "Error while verifying signature for $DATA"
		exit 1
	fi
	logit "[INFO]" "Block signature verified"
    echo -n '"data":{"'$1'":'$(ipfs --timeout=10s cat $1)'},'
	exit 0
else
	echo "Usage: $PROGRAM HASH GPG"
	exit 1
fi