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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
#!/bin/bash
# ak-enter
#
# Using this tool, we can seek a whole zchain, if available from
# an IPFS CID or an IPNS link.
#
# Default (no arguments) will retrieve the local ZCHAIN starting
# from the IPFS CID stored in the file that is tracked by the
# $AK_ZLATEST environment variable.
#
# ak-enter [-n IPNS_LINK]
# ak-enter [IPFS CID]
# ak-enter -N
# ak-enter -h
# ak-enter
#
# Returns a JSON array representing the chain retrieved.
# Logs messages to $LOGSFILE.
PROGRAM="$(basename $0)"
logit(){
ak-logthis "<$PROGRAM>" "$1" "$2"
}
usage(){
echo "$PROGRAM - Crawl an arching kaos chain"
echo "-----------------------------------"
echo "$PROGRAM [-N | --no-verify] [-l | --limit <number>] [zblock]"
echo "$PROGRAM [-N | --no-verify] [-l | --limit <number>] -n <zchain>"
echo "Usage:"
echo " --help, -h Print this help and exit"
echo " --chain <ipns-link>, -n <ipns-link> Crawl specified chain"
echo " --no-verify, -N Don't verify signatures"
echo " <ipfs-link> Specify IPFS CID for entrance"
echo ""
echo "Note that combined flags don't work for now"
echo "Running with no flags crawls your chain based on AK_ZLATEST environment variable"
exit 2
}
isIPFSv0 () {
if [ -z $1 ] || [ "$1" != "" ]
then
echo $1 | grep -e 'Qm.\{44\}' > /dev/null
if [ "$?" -ne 0 ]
then
logit "[ERROR]" "Argument provided was not an IPFS CIDv0 string"
exit 1
fi
else
exit 1
fi
}
# Start of tests
#entrance="QmW5WVXCJfhb4peHG6cbEdamC24vZzMX2Vz386vpENh38U"
#entrance="QmNjQq7GkuXGF8kFT1z2Mv3i4JhY7sBXVUmHDiR1zkQjoE"
#entrance="QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"
# End of tests
entrance="$(cat $AK_ZLATEST)"
verify=1
limit=0
fromIpns=0
while [ "$#" ]; do
case "$1" in
-h | --help)
usage
;;
-l | --limit)
limit=$2
shift 2
;;
-N | --no-verify)
verify=0
shift
;;
-n | --chain | --ipns)
fromIpns=1
ipns=$1
shift
ol=$1
entrance="$(ak-ipns-resolve $1)"
if [ "$?" -ne 0 ]
then
logit "[ERROR]" "Could not resolve IPNS name"
exit 1
fi
shift
;;
*)
test="$1"
if [ ! -z "$test" ] && [ "$fromIpns" == "0" ]
then
isIPFSv0 "$test"
entrance="$test"
elif [ -z "$entrance" ] && [ "$fromIpns" == "1" ]
then
entrance="$(cat $AK_ZLATEST)"
fi
break
esac
done
# We assign the IPFS CIDv0 of an empty file as this is used
# as our GENESIS block, hence the "seed" that the tree grows
# from.
seed="$(cat $AK_ZGENESIS)"
# We assume that we found the entrance inside a block, hence
# ZBLOCK is labeled as previous
zblock="$entrance"
# Enter temp folder
TEMPASSIN="$(ak-tempassin)"
cd $TEMPASSIN
counter=0
# The loop
# We break the loop from inside the loop
while true && [ $limit="0" ]
do
if [ $counter == 0 ]
then
echo -n '['
fi
counter=$(expr $counter + 1)
# Check if $zblock exists as variable
if [ ! -v $zblock ]
then
# Check if it is not our seed cause if it is we skip this part
if [ "$zblock" != "$seed" ]
then
# Reset timestamp since it's introduced later
timestamp=''
# Announce to logs which ZBLOCK is being read at the moment
logit "[INFO]" "Examining $zblock"
# We concatenate the zblock's contents, pipe
# them through filter ak-json2bash and output
# them to tmp-file
#
# We check if any $zblock at all
ak-ipfs-cat $zblock > /dev/null 2>&1
if [ "$?" -ne 0 ]
then
logit "[ERROR]" "ZBLOCK $zblock READ failed"
exit 1
fi
logit "[INFO]" "ZBLOCK $zblock READ"
echo -n '{"id":"'$counter'","zblock":"'$zblock'",'
# If it's JSON formated
ak-ipfs-cat $zblock | jq -M > /dev/null 2>&1
if [ "$?" -ne 0 ]
then
logit "[ERROR]" "ZBLOCK $zblock is not JSON"
cat /dev/null > $AK_ZBLOCKDIR/$zblock > /dev/null 2>&1
exit 1
fi
logit "[INFO]" "ZBLOCK $zblock is JSON"
# Then we pass it through the filter and save it
ak-ipfs-cat $zblock | jq -M | ak-json2bash > tmp-zblock
# Be sure that there are the expected values
# We need 'block' and 'block_signature' inside a 'zblock'
# Exit if any is missing
grep -e 'block_signature' tmp-zblock > /dev/null 2>&1
if [ "$?" -ne 0 ]
then
logit "[ERROR]" "ZBLOCK $zblock doesn't contain a block_signature"
exit 1
fi
logit "[INFO]" "ZBLOCK $zblock contains a block_signature"
grep -e 'block=' tmp-zblock > /dev/null 2>&1
if [ "$?" -ne 0 ]
then
logit "[ERROR]" "ZBLOCK $zblock has no block"
exit 1
fi
logit "[INFO]" "ZBLOCK $zblock has block"
# We create files named after each ZBLOCK IPFS CID for later
# reference. Files are empty.
touch $AK_ZBLOCKDIR/$zblock
logit "[INFO]" "Created reference"
# Supposingly you are on a safe environment and you only have
# access to your chain, I would consider mild secure to source
# the files into your bash.
# File an issue/pull request if you think it can be done better!!
source tmp-zblock
logit "[INFO]" "ZBLOCK SOURCED"
# Same as above applies to BLOCK and DATA subparts of each ZBLOCK
# BLOCKS
echo -n '"block":"'$block'",'
ak-ipfs-cat $block | jq -M | ak-json2bash > tmp-block
if [ "$?" -ne 0 ]
then
logit "[ERROR]" "BLOCK $block READ failed"
exit 1
fi
grep -e 'timestamp' -e 'gpg' -e 'data' -e 'action' -e 'detach' -e 'previous' tmp-block > /dev/null 2>&1
if [ "$?" -ne 0 ]
then
logit "[ERROR]" "BLOCK $block is NOT a valid block"
exit 1
fi
logit "[INFO]" "BLOCK $block is a block"
source tmp-block
logit "[INFO]" "BLOCK $block SOURCED"
touch $AK_BLOCKDIR/$block
logit "[INFO]" "BLOCK REFERENCED"
module="$(echo $action | sed -e 's/\// /g' | awk '{ print $1 }')"
logit "[INFO]" "DATA is $module module."
command="$(echo $action | sed -e 's/\// /g' | awk '{ print $2 }')"
logit "[INFO]" "COMMAND is $command"
if [ ! -v $timestamp ]
then
echo -n '"timestamp":"'$timestamp'",'
fi
echo -n '"block_signature":"'$block_signature'",'
echo -n '"detach":"'$detach'",'
echo -n '"module":"'$module'",'
echo -n '"action":"'$command'",'
echo -n '"gpg":"'$gpg'",'
if [ $verify == 1 ]
then
ak-ipfs-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
ak-ipfs-get $block_signature > /dev/null 2>&1
if [ "$?" -ne 0 ]
then
logit "[ERROR]" "Error while getting $block_signature for $block"
exit 1
fi
mv $block_signature $block.asc
logit "[INFO]" "Block signature downloaded"
ak-ipfs-get $block > /dev/null 2>&1
if [ "$?" -ne 0 ]
then
logit "[ERROR]" "Could not get $block block"
exit 1
fi
logit "[INFO]" "Downloaded block $block."
gpg2 --verify $block.asc > /dev/null 2>&1
if [ "$?" -ne 0 ]
then
logit "[ERROR]" "Could not verify $block with GPG key $gpg."
exit 1
fi
logit "[GPG]" "$gpg signature of $block is verified."
fi
ak-data-expand $data $gpg
if [ "$?" -ne 0 ]
then
logit "[ERROR]" "Failed on data signature verification [data: $data, gpg: $gpg, zblock: $zblock]"
exit 1
fi
# DATA (but we don't source it's stuff)
# Only print to stdout
#ak-ipfs-cat $data
touch $AK_DATADIR/$data
# Now, since we sourced the BLOCK to our terminal, we can search
# for $previous variable. In case we don't find one, we append one
# and we exit.
if [ -v $previous ]
then
logit "[WARNING]" "Block $block has no previous zblock, appending pseudo genesis to exit gracefully."
echo -n '"previous":"genesis"},{"genesis":"genesis"}]'
logit "[INFO]" "Reached pseudo-genesis, counted $counter zblocks."
exit 0
# Otherwise, we inform of the sequence
else
#echo "$zblock after $previous"
logit "[INFO]" "Found a previous block for $zblock: $previous"
echo -n '"previous":"'$previous'"}'
zblock=$previous
fi
if [ "$limit" != "0" ] && [ "$limit" == "$counter" ]
then
echo -n ']'
exit 0
else
echo -n ','
fi
# Now check if it is equal to the seed
# which apparently means we reached the seed.
elif [ "$zblock" == "$seed" ]
then
echo -n '{"genesis":"genesis"}]'
logit "[INFO]" "Reached $seed, counted $counter zblocks."
exit 0
fi
# And finally, if nothing is there exit with error
else
echo "Check not passed... No previous IPFS CID"
exit 1
fi
done
|